[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [muon/muon-exports] /: Create a new muon tool called exporter
From:       Aleix Pol <aleixpol () kde ! org>
Date:       2012-11-21 16:27:27
Message-ID: 20121121162727.740EBA6091 () git ! kde ! org
[Download RAW message or body]

Git commit 90ec59b7eb9e1a1534e8f5da02ace74d377f19ee by Aleix Pol.
Committed on 21/11/2012 at 17:25.
Pushed by apol into branch 'muon-exports'.

Create a new muon tool called exporter

It will be used for dumping the data that muon collects and it will
put it in a json file for consumption.
It's useful for reusing code when it comes to consuming data from
any of the backends that Muon supports and we can't use C++ easily.

M  +1    -0    CMakeLists.txt
A  +15   -0    exporter/CMakeLists.txt
A  +110  -0    exporter/MuonExporter.cpp     [License: BSD]
A  +45   -0    exporter/MuonExporter.h     [License: BSD]
A  +59   -0    exporter/main.cpp     [License: BSD]
M  +10   -0    libmuon/resources/ResourcesModel.cpp
M  +1    -0    libmuon/resources/ResourcesModel.h

http://commits.kde.org/muon/90ec59b7eb9e1a1534e8f5da02ace74d377f19ee

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c8ba008..328f85c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,6 +27,7 @@ if(QAPT_FOUND)
     add_subdirectory(kded)
 endif(QAPT_FOUND)
 add_subdirectory(discover)
+add_subdirectory(exporter)
 
 macro_log_feature(QAPT_FOUND "LibQApt" "Qt wrapper around the libapt-pkg library"
                   "http://www.kde.org" FALSE "" "Required to build the APT backend")
diff --git a/exporter/CMakeLists.txt b/exporter/CMakeLists.txt
new file mode 100644
index 0000000..1f2986b
--- /dev/null
+++ b/exporter/CMakeLists.txt
@@ -0,0 +1,15 @@
+include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/libmuon \
${CMAKE_CURRENT_BINARY_DIR}/..) +
+if(QAPT_FOUND)
+    add_definitions(-DQAPT_ENABLED)
+endif(QAPT_FOUND)
+
+option(LIBATTICA_ENABLED "Enable KNS backend" ON)
+if (LIBATTICA_FOUND AND LIBATTICA_ENABLED)
+    add_definitions(-DATTICA_ENABLED)
+endif (LIBATTICA_FOUND AND LIBATTICA_ENABLED)
+
+kde4_add_executable(muon-exporter main.cpp MuonExporter.cpp)
+
+target_link_libraries(muon-exporter muonprivate ${KDE4_KDEUI_LIBRARY} \
${QJSON_LIBRARIES}) +
diff --git a/exporter/MuonExporter.cpp b/exporter/MuonExporter.cpp
new file mode 100644
index 0000000..08e8420
--- /dev/null
+++ b/exporter/MuonExporter.cpp
@@ -0,0 +1,110 @@
+/*
+ *   Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library/Lesser General Public License
+ *   version 2, or (at your option) any later version, as published by the
+ *   Free Software Foundation
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details
+ *
+ *   You should have received a copy of the GNU Library/Lesser General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include "MuonExporter.h"
+#include <resources/AbstractResourcesBackend.h>
+#include <resources/ResourcesModel.h>
+#include <qjson/serializer.h>
+#include <QFile>
+#include <QDebug>
+
+#ifdef QAPT_ENABLED
+#include <ApplicationBackend/ApplicationBackend.h>
+#endif
+
+#ifdef ATTICA_ENABLED
+#include <KNSBackend/KNSBackend.h>
+#endif
+
+MuonExporter::MuonExporter()
+    : QObject(0)
+{
+    initialize();
+}
+
+MuonExporter::~MuonExporter()
+{}
+
+void MuonExporter::initialize()
+{
+    QList<AbstractResourcesBackend*> backends;
+
+#ifdef ATTICA_ENABLED
+    backends += new KNSBackend("comic.knsrc", "face-smile-big", this);
+    backends += new KNSBackend("plasmoids.knsrc", "plasma", this);
+#endif
+    
+#ifdef QAPT_ENABLED
+    ApplicationBackend* applicationBackend = new ApplicationBackend(this);
+    applicationBackend->integrateMainWindow(this);
+    backends += applicationBackend;
+#endif
+    
+    m_backendsToInitialize = backends.count();
+    ResourcesModel* m = ResourcesModel::global();
+    foreach(AbstractResourcesBackend* b, backends) {
+        connect(b, SIGNAL(backendReady()), SLOT(backendReady()));
+        m->addResourcesBackend(b);
+    }
+}
+
+void MuonExporter::setExportPath(const KUrl& url)
+{
+    m_path = url;
+}
+
+void MuonExporter::backendReady()
+{
+    m_backendsToInitialize--;
+    if(m_backendsToInitialize==0)
+        exportModel();
+}
+
+QVariantMap itemDataToMap(const QMap<int, QVariant>& data, const QHash< int, \
QByteArray >& names) +{
+    QVariantMap ret;
+    for(QMap<int, QVariant>::const_iterator it = data.constBegin(), \
itEnd=data.constEnd(); it!=itEnd; ++it) { +        ret.insert(names.value(it.key()), \
it.value()); +    }
+    return ret;
+}
+
+void MuonExporter::exportModel()
+{
+    QVariantList data;
+    ResourcesModel* m = ResourcesModel::global();
+    QHash< int, QByteArray > names = m->roleNames();
+    
+    for(int i = 0; i<m->rowCount(); i++) {
+        QModelIndex idx = m->index(i, 0);
+        data += itemDataToMap(m->itemData(idx), names);
+        qDebug() << "fuuu" << i << data.last();
+    }
+    qDebug() << "found items: " << data.count();
+    QFile f(m_path.toLocalFile());
+    if(f.open(QIODevice::WriteOnly)) {
+        bool ok=true;
+        QJson::Serializer s;
+        s.serialize(data, &f, &ok);
+        if(!ok)
+            qWarning() << "Could not completely export the data to " << m_path;
+    } else {
+        qWarning() << "Could not write to " << m_path;
+    }
+}
diff --git a/exporter/MuonExporter.h b/exporter/MuonExporter.h
new file mode 100644
index 0000000..64dbdcd
--- /dev/null
+++ b/exporter/MuonExporter.h
@@ -0,0 +1,45 @@
+/*
+ *   Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library/Lesser General Public License
+ *   version 2, or (at your option) any later version, as published by the
+ *   Free Software Foundation
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details
+ *
+ *   You should have received a copy of the GNU Library/Lesser General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef MUONEXPORTER_H
+#define MUONEXPORTER_H
+
+#include "MuonMainWindow.h"
+#include <KUrl>
+
+class MuonExporter : public QObject
+{
+    Q_OBJECT
+    void exportModel();
+    public:
+        explicit MuonExporter();
+        ~MuonExporter();
+
+        void initialize();
+        void setExportPath(const KUrl& url);
+
+    public slots:
+        void backendReady();
+
+    private:
+        KUrl m_path;
+        int m_backendsToInitialize;
+};
+
+#endif // MUONEXPORTER_H
diff --git a/exporter/main.cpp b/exporter/main.cpp
new file mode 100644
index 0000000..edff0b5
--- /dev/null
+++ b/exporter/main.cpp
@@ -0,0 +1,59 @@
+/*
+ *   Copyright (C) 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library/Lesser General Public License
+ *   version 2, or (at your option) any later version, as published by the
+ *   Free Software Foundation
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details
+ *
+ *   You should have received a copy of the GNU Library/Lesser General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <KApplication>
+#include <QWidget>
+#include <KAboutData>
+#include <KCmdLineArgs>
+#include <KUniqueApplication>
+#include <KStandardDirs>
+#include "MuonExporter.h"
+
+static const char description[] =
+    I18N_NOOP("An application exporterer");
+
+static const char version[] = "1.9.60";
+
+int main(int argc, char** argv)
+{
+    KAboutData about("muon-exporter", "muon-exporter", ki18n("Muon Exporter"), \
version, ki18n(description), +                     KAboutData::License_GPL, ki18n(" \
©2010-2012 Jonathan Thomas"), KLocalizedString(), 0); +    \
about.addAuthor(ki18n("Jonathan Thomas"), KLocalizedString(), \
"echidnaman@kubuntu.org"); +    about.addAuthor(ki18n("Aleix Pol Gonzalez"), \
KLocalizedString(), "aleixpol@blue-systems.com"); +    \
about.setProgramIconName("muonexporter"); +    about.setProductName("muon/exporter");
+
+    KCmdLineArgs::init(argc, argv, &about);
+    KCmdLineOptions options;
+    options.add("+file", ki18n("File to which we'll export"));
+    KCmdLineArgs::addCmdLineOptions( options );
+
+    KApplication app;
+    // Translations
+    KGlobal::locale()->insertCatalog("app-install-data");
+    KGlobal::locale()->insertCatalog("libmuon");
+    // Needed for KIcon compatibility w/ application icons from app-install-data
+    KGlobal::dirs()->addResourceDir("appicon", "/usr/share/app-install/icons/");
+    KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
+
+    MuonExporter exp;
+    exp.setExportPath(args->url(0));
+
+    return app.exec();
+}
diff --git a/libmuon/resources/ResourcesModel.cpp \
b/libmuon/resources/ResourcesModel.cpp index 0151ea1..1c8750d 100644
--- a/libmuon/resources/ResourcesModel.cpp
+++ b/libmuon/resources/ResourcesModel.cpp
@@ -288,3 +288,13 @@ void ResourcesModel::transactionChanged(Transaction* t)
     QModelIndex idx = resourceIndex(t->resource());
     dataChanged(idx, idx);
 }
+
+QMap<int, QVariant> ResourcesModel::itemData(const QModelIndex& index) const
+{
+    QMap<int, QVariant> ret;
+    QHash<int, QByteArray> names = roleNames();
+    for(QHash<int, QByteArray>::const_iterator it=names.constBegin(), \
itEnd=names.constEnd(); it!=itEnd; ++it) { +        ret.insert(it.key(), data(index, \
it.key())); +    }
+    return ret;
+}
diff --git a/libmuon/resources/ResourcesModel.h b/libmuon/resources/ResourcesModel.h
index 1d6a954..6a5f331 100644
--- a/libmuon/resources/ResourcesModel.h
+++ b/libmuon/resources/ResourcesModel.h
@@ -70,6 +70,7 @@ class MUONPRIVATE_EXPORT ResourcesModel : public QAbstractListModel
         QModelIndex resourceIndex(AbstractResource* res) const;
         QVector< AbstractResourcesBackend* > backends() const;
         int updatesCount() const;
+        virtual QMap< int, QVariant > itemData(const QModelIndex& index) const;
         
         Q_SCRIPTABLE AbstractResource* resourceByPackageName(const QString& name);
         


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic