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

List:       kde-commits
Subject:    [print-manager] /: Filtering which kind of jobs and printers is now working much faster
From:       Daniel Nicoletti <dantti12 () gmail ! com>
Date:       2012-02-23 2:21:11
Message-ID: 20120223022111.4B91EA60A6 () git ! kde ! org
[Download RAW message or body]

Git commit f9b99a2a6d0dd124caa83ea538b360adb4cd0477 by Daniel Nicoletti.
Committed on 23/02/2012 at 03:20.
Pushed by dantti into branch 'master'.

Filtering which kind of jobs and printers is now working much faster

M  +0    -2    libqcups/KCupsRequest.cpp
M  +1    -13   plasmoid-package/contents/ui/JobItem.qml
A  +51   -0    plasmoid-package/contents/ui/PrintManagerDataModel.qml     [License: \
LGPL (v2+)] M  +4    -4    plasmoid-package/contents/ui/PrinterItem.qml
M  +44   -80   plasmoid-package/contents/ui/printmanager.qml
M  +40   -67   printmanager-dataengine/PrintManagerEngine.cpp
M  +2    -6    printmanager-dataengine/PrintManagerEngine.h

http://commits.kde.org/print-manager/f9b99a2a6d0dd124caa83ea538b360adb4cd0477

diff --git a/libqcups/KCupsRequest.cpp b/libqcups/KCupsRequest.cpp
index 6c46c5a..5fba853 100644
--- a/libqcups/KCupsRequest.cpp
+++ b/libqcups/KCupsRequest.cpp
@@ -652,8 +652,6 @@ KCupsRequest::KCupsJobs KCupsRequest::jobs() const
 
 void KCupsRequest::waitTillFinished()
 {
-    kDebug() << QThread::currentThreadId();
-    kDebug() << m_finished;
     if (m_finished) {
         return;
     }
diff --git a/plasmoid-package/contents/ui/JobItem.qml \
b/plasmoid-package/contents/ui/JobItem.qml index abd634f..754d680 100644
--- a/plasmoid-package/contents/ui/JobItem.qml
+++ b/plasmoid-package/contents/ui/JobItem.qml
@@ -33,21 +33,9 @@ Item {
         id: padding
         imagePath: "widgets/viewitem"
         prefix: "hover"
-        opacity: 0
+        opacity: highlightPrinter == jobPrinter ? 0.7 : 0
         Behavior on opacity { PropertyAnimation {} }
         anchors.fill: parent
-        
-        Component.onCompleted: {
-            // Try to avoid calling jobsView directly
-            jobsView.highlight.connect(highlightJobs)
-        }
-        function highlightJobs(printer) {
-            if (printer == jobPrinter) {
-                padding.opacity = 0.7;
-            } else {
-                padding.opacity = 0;
-            }
-        }
     }
     
     MouseArea {
diff --git a/plasmoid-package/contents/ui/PrintManagerDataModel.qml \
b/plasmoid-package/contents/ui/PrintManagerDataModel.qml new file mode 100644
index 0000000..1237693
--- /dev/null
+++ b/plasmoid-package/contents/ui/PrintManagerDataModel.qml
@@ -0,0 +1,51 @@
+/*
+ *   Copyright 2012 Daniel Nicoletti <dantti12@gmail.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License as
+ *   published by the Free Software Foundation; either version 2 or
+ *   (at your option) any later version.
+ *
+ *   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 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.
+ */
+
+import Qt 4.7
+import org.kde.plasma.core 0.1 as PlasmaCore
+import org.kde.plasma.components 0.1 as PlasmaComponents
+import org.kde.qtextracomponents 0.1
+
+PlasmaCore.DataModel {
+    property alias interval: pmDataSource.interval
+    property alias connectedSources: pmDataSource.connectedSources
+    property string filter
+    dataSource: PlasmaCore.DataSource {
+        id: pmDataSource
+        engine: "printmanager"
+        onSourceAdded: {
+            if (source.match("^" + filter)) {
+                console.debug(filter + " ADDED: " + source);
+                connectSource(source);
+            }
+        }
+        onSourceRemoved: {
+            console.debug(filter + " REMOVED: " + source);
+            disconnectSource(source);
+        }
+        Component.onCompleted: {
+            for (var i in sources) {
+                if (sources[i].match("^" + filter)) {
+                    console.debug(filter + "onCompleted ADDED: " + sources[i]);
+                    connectSource(sources[i]);
+                }
+            }
+        }
+    }
+}
diff --git a/plasmoid-package/contents/ui/PrinterItem.qml \
b/plasmoid-package/contents/ui/PrinterItem.qml index f7902bd..9d53a63 100644
--- a/plasmoid-package/contents/ui/PrinterItem.qml
+++ b/plasmoid-package/contents/ui/PrinterItem.qml
@@ -48,19 +48,19 @@ Item {
         hoverEnabled: multipleItems
         onEntered: {
             padding.opacity = 0.6;
-            printersView.highlight(printerName);
+            highlightPrinter = printerName;
         }
         onExited: {
             padding.opacity = 0;
-            printersView.highlight("");
+            highlightPrinter = "";
         }
         onClicked: {
             if (printerItem.ListView.view.currentIndex == index) {
                 printerItem.ListView.view.currentIndex = -1;
-                printersView.highlight(printerName);
+                highlightPrinter = printerName;
             } else if (multipleItems) {
                 printerItem.ListView.view.currentIndex = index;
-                printersView.highlight("");
+                highlightPrinter = "";
             }
         }
     
diff --git a/plasmoid-package/contents/ui/printmanager.qml \
b/plasmoid-package/contents/ui/printmanager.qml index 3f84986..7e89a8c 100644
--- a/plasmoid-package/contents/ui/printmanager.qml
+++ b/plasmoid-package/contents/ui/printmanager.qml
@@ -26,8 +26,8 @@ Item {
     property int minimumWidth: horizontalLayout ? 650 : 300
     property int minimumHeight: 270
     
-    property bool horizontalLayout: printersModel.count > 1
-    property string whichJobs
+    property string highlightPrinter
+    property bool horizontalLayout: printersFilterModel.count > 1
     property string whichPrinter
 
     PlasmaCore.Theme {
@@ -44,23 +44,22 @@ Item {
     
     function configChanged() {
         var activeJobs = plasmoid.readConfig("activeJobs");
-        var allJobs = plasmoid.readConfig("allJobs");
-        var _whichJobs;
+        var completedJobs = plasmoid.readConfig("completedJobs");
+        activeJobsModel.interval = 0;
+        completedJobsModel.interval = 0;
+        allJobsModel.interval = 0;
         if (activeJobs == true) {
-            _whichJobs = "ActiveJobs";
-        } else if (allJobs == true) {
-            _whichJobs = "AllJobs";
+            jobsFilterModel.sourceModel = activeJobsModel;
+            activeJobsModel.interval = 1500;
+        } else if (completedJobs == true) {
+            jobsFilterModel.sourceModel = completedJobsModel;
+            completedJobsModel.interval = 1500;
         } else {
-            _whichJobs = "CompletedJobs";
+            jobsFilterModel.sourceModel = allJobsModel;
+            allJobsModel.interval = 1500;
         }
         
-        if (_whichJobs != whichJobs) {
-            console.debug("--------Which JOBS Changed:" + whichJobs + " to " + \
                _whichJobs);
-            whichJobs = _whichJobs;
-            jobsSource.connectedSources = [whichJobs];
-            jobsSource.onCompleted(sources);
-            console.debug("--------Which JOBS sources:" + jobsSource.sources);
-        }
+        whichPrinter = plasmoid.readConfig("printerName");
 
         printersView.currentIndex = -1;
         jobsView.currentIndex = -1;
@@ -73,64 +72,20 @@ Item {
         }
     }
 
-    PlasmaCore.DataSource {
-        id: printersSource
-        engine: "printmanager"
-        connectedSources: ["Printers"]
-        interval: 1500
-        
-        onSourceAdded: {
-            console.debug(source);
-            var pattern = /Printers(\/[^\/]+)?$/
-            if (source.match(pattern)) {
-                console.debug("Source Connected:" + source);
-                connectSource(source);
-            }            
-        }
-        Component.onCompleted: {
-            var pattern = /Printers(\/[^\/]+)?$/
-            for (var i in sources) {
-                console.debug("COMPLETED " + sources[i]);
-                if (sources[i].match(pattern)) {
-                    console.debug("COMPLETED Source Connected:" + sources[i]);
-                    connectSource(sources[i]);
-                }
-            }
-        }
+    PrintManagerDataModel {
+        id: activeJobsModel
+        connectedSources: ["ActiveJobs"]
+        filter: "ActiveJobs"
     }
-    
-    PlasmaCore.DataSource {
-        id: jobsSource
-        engine: "printmanager"
-//         connectedSources: whichJobs
-        interval: 1500
-        
-        onSourceAdded: {
-            console.debug("Job onSourceAdded " + source);
-//             list.onCountChanged: {
-//                 if (count == 0) {
-//                     plasmoid.status = "PassiveStatus"
-//                 } else {
-//                     plasmoid.status = "PassiveStatus"
-//                 }
-//             }
-            
-            var re = new RegExp(whichJobs + "(/\\d+)?$");
-            if (source.match(re)) {
-                console.debug("Job onSourceAdded =  Source Connected:" + source);
-                connectSource(source);
-            }            
-        }
-        Component.onCompleted: {
-            var re = new RegExp(whichJobs + "(/\\d+)?$")
-            for (var i in sources) {
-                console.debug("Job onCOMPLETED " + sources[i]);
-                if (sources[i].match(re)) {
-                    console.debug("Job onCOMPLETED Source Connected:" + sources[i]);
-                    connectSource(sources[i]);
-                }
-            }
-        }
+    PrintManagerDataModel {
+        id: completedJobsModel
+        connectedSources: ["CompletedJobs"]
+        filter: "CompletedJobs"
+    }
+    PrintManagerDataModel {
+        id: allJobsModel
+        connectedSources: ["AllJobs"]
+        filter: "AllJobs"
     }
     
     Column {
@@ -160,13 +115,20 @@ Item {
         anchors.fill: parent
         ScrollableListView {
             id: printersView
-            signal highlight(string printer)
             width:  horizontalLayout ? parent.width * 0.40 - headerSeparator.width : \
parent.width  height: horizontalLayout ? parent.height : 50
             currentIndex: -1
-            model: PlasmaCore.DataModel {
-                id: printersModel
-                dataSource: printersSource
+            model: PlasmaCore.SortFilterModel {
+                id: printersFilterModel
+                sourceModel: PrintManagerDataModel {
+                    interval: 1500
+                    connectedSources: ["Printers"]
+                    filter: "Printers"
+                }
+                filterRole: "printerName"
+                filterRegExp: whichPrinter
+                sortRole: "order"
+                sortOrder: Qt.DescendingOrder
             }
             delegate: PrinterItem{
                 multipleItems: horizontalLayout
@@ -187,12 +149,14 @@ Item {
         
         ScrollableListView {
             id: jobsView
-            signal highlight(string printer)
             width:  horizontalLayout ? parent.width * 0.60 - headerSeparator.width : \
parent.width  height: horizontalLayout ? parent.height : parent.height - 50
-            model: PlasmaCore.DataModel {
-                id: jobsModel
-                dataSource: jobsSource
+            model: PlasmaCore.SortFilterModel {
+                id: jobsFilterModel
+                filterRole: "jobPrinter"
+                filterRegExp: whichPrinter
+                sortRole: "order"
+                sortOrder: Qt.DescendingOrder
             }
             delegate: JobItem{}
             
diff --git a/printmanager-dataengine/PrintManagerEngine.cpp \
b/printmanager-dataengine/PrintManagerEngine.cpp index f79e4ac..fb47f6c 100644
--- a/printmanager-dataengine/PrintManagerEngine.cpp
+++ b/printmanager-dataengine/PrintManagerEngine.cpp
@@ -57,11 +57,17 @@ void PrintManagerEngine::init()
     m_jobAttributes |= KCupsJob::JobMediaSheetsCompleted;
     m_jobAttributes |= KCupsJob::JobPrinterStatMessage;
     m_jobAttributes |= KCupsJob::JobPreserved;
+
+    m_printerAttributes |= KCupsPrinter::PrinterName;
+    m_printerAttributes |= KCupsPrinter::PrinterInfo;
+    m_printerAttributes |= KCupsPrinter::PrinterState;
+    m_printerAttributes |= KCupsPrinter::PrinterStateMessage;
+    // to get proper icons
+    m_printerAttributes |= KCupsPrinter::PrinterType;
 }
 
 Plasma::Service* PrintManagerEngine::serviceForSource(const QString &source)
 {
-    kDebug() << source;
     QStringList parts = source.split(QLatin1Char('/'));
     if (parts.size() == 2 || parts.size() == 4) {
         // Options
@@ -83,11 +89,12 @@ Plasma::Service* PrintManagerEngine::serviceForSource(const \
QString &source)  
 void PrintManagerEngine::job(const QString &prefix, int order, const KCupsJob &job)
 {
-    QString id = QString::number(job.id());
-    id.prepend(prefix + QLatin1Char('/'));
+    QString source = prefix;
+    source.append(job.idStr());
 
-    Data sourceData = query(id);
+    Data sourceData = query(source);
     bool changed = false;
+
     if (sourceData[QLatin1String("order")] != order) {
         sourceData[QLatin1String("order")] = order;
         changed = true;
@@ -153,11 +160,10 @@ void PrintManagerEngine::job(const QString &prefix, int order, \
const KCupsJob &j  changed = true;
         }
     }
-//    setData(id, QLatin1String("jobPrinterStatMessage"), job.name());
 
     if (changed) {
         // update only if data changes to avoid uneeded updates on the views
-        setData(id, sourceData);
+        setData(source, sourceData);
     }
 }
 
@@ -174,9 +180,9 @@ void PrintManagerEngine::updateJobs(const QString &prefix, const \
KCupsRequest::K  QRegExp rx;
     if (jobsStrList.isEmpty()) {
         // we don't have any jobs remove all sources that start with our prefix
-        rx.setPattern(QLatin1Char('^') + prefix + QLatin1String("/"));
+        rx.setPattern(QLatin1Char('^') + prefix);
     } else {
-        rx.setPattern(QLatin1Char('^') + prefix + QLatin1String("/(?!") + \
jobsStrList.join(QLatin1String("|")) + QLatin1Char(')')); +        \
rx.setPattern(QLatin1Char('^') + prefix + QLatin1String("(?!") + \
jobsStrList.join(QLatin1String("|")) + QLatin1Char(')'));  }
 
     foreach (const QString &source, sources().filter(rx)) {
@@ -185,17 +191,12 @@ void PrintManagerEngine::updateJobs(const QString &prefix, \
const KCupsRequest::K  }
 }
 
-void PrintManagerEngine::requestJobsFinished()
-{
-    // TODO remove invalid sources
-}
-
 void PrintManagerEngine::printer(const QString &prefix, int order, const \
KCupsPrinter &printer)  {
-    QString name = printer.name();
-    name.prepend(prefix + QLatin1Char('/'));
+    QString source = prefix;
+    source.append(printer.name());
 
-    Data sourceData = query(name);
+    Data sourceData = query(source);
     bool changed = false;
 
     if (sourceData[QLatin1String("order")] != order) {
@@ -239,7 +240,7 @@ void PrintManagerEngine::printer(const QString &prefix, int \
order, const KCupsPr  
     if (changed) {
         // update only if data changes to avoid uneeded updates on the views
-        setData(name, sourceData);
+        setData(source, sourceData);
     }
 }
 
@@ -267,77 +268,49 @@ void PrintManagerEngine::updatePrinters(const QString &prefix, \
const KCupsReques  }
 }
 
-void PrintManagerEngine::requestPrintersFinished()
+bool PrintManagerEngine::sourceRequestEvent(const QString &source)
 {
-    // TODO remove invalid sources
-    KCupsRequest *request = qobject_cast<KCupsRequest *>(sender());
-    request->deleteLater();
-}
- 
-bool PrintManagerEngine::sourceRequestEvent(const QString &name)
-{
-    kDebug() << name;
-    QRegExp rx(QLatin1String("^(?:AllJobs|ActiveJobs|CompletedJobs)|Printers(?:/[^/]+/(?:AllJobs|ActiveJobs|CompletedJobs))?$"));
                
-    if (rx.exactMatch(name)) {
-        m_validSources << name;
+    if (source == QLatin1String("Printers") ||
+        source == QLatin1String("ActiveJobs") ||
+        source == QLatin1String("AllJobs") ||
+        source == QLatin1String("CompletedJobs")) {
         // Needed so that DataSource can do polling
         // on e.g. "Printers" or "ActiveJobs"
-        setData(name, Data());
+        setData(source, Data());
         // We do not have any special code to execute the
         // first time a source is requested, so we just call
         // updateSourceEvent().
 
-        return updateSourceEvent(name);
+        return updateSourceEvent(source);
     }
     return false;
 }
  
-bool PrintManagerEngine::updateSourceEvent(const QString &name)
+bool PrintManagerEngine::updateSourceEvent(const QString &source)
 { 
     KCupsRequest *request = new KCupsRequest;
 
-    if (!m_validSources.contains(name)) {
+    if (source != QLatin1String("Printers") &&
+        source != QLatin1String("ActiveJobs") &&
+        source != QLatin1String("AllJobs") &&
+        source != QLatin1String("CompletedJobs")) {
         return false;
     }
 
-    kDebug() << "updating" << name;
-
-    if (name.startsWith(QLatin1String("Printers")) && name.count(QLatin1Char('/')) < \
                2) {
-            KCupsPrinter::Attributes attr;
-            attr |= KCupsPrinter::PrinterName;
-            attr |= KCupsPrinter::PrinterInfo;
-            attr |= KCupsPrinter::PrinterState;
-            attr |= KCupsPrinter::PrinterStateMessage;
-            // to get proper icons
-            attr |= KCupsPrinter::PrinterType;
-            request->getPrinters(attr);
+    if (source == QLatin1String("Printers")) {
+            request->getPrinters(m_printerAttributes);
             request->waitTillFinished();
-            updatePrinters(QLatin1String("Printers"), request->printers());
+            updatePrinters(source + QLatin1Char('/'), request->printers());
     } else {
-        QString printer;
-        QString whichJob;
-        QStringList parts = name.split(QLatin1Char('/'));
-        if (parts.size() == 3) {
-            // Printers PrinterName Kind of the job
-            printer = parts.at(1);
-            whichJob = parts.at(2);
-        } else {
-            whichJob = parts.at(0);
+        if (source == QLatin1String("ActiveJobs")) {
+            request->getJobs(QString(), false, CUPS_WHICHJOBS_ACTIVE, \
m_jobAttributes); +        } else if (source == QLatin1String("AllJobs")) {
+            request->getJobs(QString(), false, CUPS_WHICHJOBS_ALL, m_jobAttributes);
+        } else if (source == QLatin1String("CompletedJobs")) {
+            request->getJobs(QString(), false, CUPS_WHICHJOBS_COMPLETED, \
m_jobAttributes);  }
-
-        if (whichJob == QLatin1String("AllJobs")) {
-            request->getJobs(printer, false, CUPS_WHICHJOBS_ALL, m_jobAttributes);
-        } else if (whichJob == QLatin1String("ActiveJobs")) {
-            request->getJobs(printer, false, CUPS_WHICHJOBS_ACTIVE, \
                m_jobAttributes);
-        } else if (whichJob == QLatin1String("CompletedJobs")) {
-            request->getJobs(printer, false, CUPS_WHICHJOBS_COMPLETED, \
                m_jobAttributes);
-        } else {
-            request->deleteLater();
-            return false;
-        }
-
         request->waitTillFinished();
-        updateJobs(name, request->jobs());
+        updateJobs(source + QLatin1Char('/'), request->jobs());
     }
 
     request->deleteLater();
diff --git a/printmanager-dataengine/PrintManagerEngine.h \
b/printmanager-dataengine/PrintManagerEngine.h index 48eef6f..52ebd01 100644
--- a/printmanager-dataengine/PrintManagerEngine.h
+++ b/printmanager-dataengine/PrintManagerEngine.h
@@ -51,13 +51,9 @@ public:
     // Get the Service class which we run operations on
     virtual Plasma::Service* serviceForSource(const QString &source);
 
-private slots:
-    void requestJobsFinished();
-    void requestPrintersFinished();
-
 protected:
     // this virtual function is called when a new source is requested
-    bool sourceRequestEvent(const QString &name);
+    bool sourceRequestEvent(const QString &source);
 
     // this virtual function is called when an automatic update
     // is triggered for an existing source (ie: when a valid update
@@ -71,9 +67,9 @@ private:
     void updatePrinters(const QString &prefix, const KCupsRequest::KCupsPrinters \
&printers);  
     KCupsJob::Attributes m_jobAttributes;
+    KCupsPrinter::Attributes m_printerAttributes;
     KCupsRequest *m_jobsRequest;
     KCupsRequest *m_printersRequest;
-    QStringList m_validSources;
 };
  
 #endif // PRINT_MANAGER_ENGINE_H


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

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