Merge lp:~jonas-drange/ubuntu-settings-components/buffer-printer-state-events into lp:~phablet-team/ubuntu-settings-components/printer-components

Proposed by Jonas G. Drange
Status: Merged
Approved by: Andrew Hayzen
Approved revision: 254
Merged at revision: 232
Proposed branch: lp:~jonas-drange/ubuntu-settings-components/buffer-printer-state-events
Merge into: lp:~phablet-team/ubuntu-settings-components/printer-components
Prerequisite: lp:~jonas-drange/ubuntu-settings-components/asyncness
Diff against target: 284 lines (+184/-17)
7 files modified
plugins/Ubuntu/Settings/Printers/CMakeLists.txt (+1/-0)
plugins/Ubuntu/Settings/Printers/models/printermodel.cpp (+8/-13)
plugins/Ubuntu/Settings/Printers/models/printermodel.h (+3/-4)
plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.cpp (+69/-0)
plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.h (+55/-0)
tests/unittests/Printers/CMakeLists.txt (+4/-0)
tests/unittests/Printers/tst_signalhandler.cpp (+44/-0)
To merge this branch: bzr merge lp:~jonas-drange/ubuntu-settings-components/buffer-printer-state-events
Reviewer Review Type Date Requested Status
Andrew Hayzen (community) Approve
Unity Team Pending
Review via email: mp+317485@code.launchpad.net

Commit message

* adds PrinterSignalHandler that is responsible for handling (some) printer signals.
* uses PrinterSignalHandler in PrinterModel so that i only will update printers a minimum number of times

To post a comment you must log in.
254. By Jonas G. Drange

undoes spurious change

Revision history for this message
Andrew Hayzen (ahayzen) wrote :

Looks good and resolves the issue, one question about a possible racy situation - don't think it would happen often (if at all) but maybe we should protect?

review: Needs Information
Revision history for this message
Andrew Hayzen (ahayzen) wrote :

As discussed the signals will get queued [0], so this won't be racy :-)

LGTM \o/

0 - https://wiki.qt.io/Threads_Events_QObjects#Signals_and_slots_across_threads
(...an automatic connection (the default) means that if the thread the receiver is living in is the same as the current thread, a direct connection is used; otherwise, a queued connection is used...)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/Ubuntu/Settings/Printers/CMakeLists.txt'
2--- plugins/Ubuntu/Settings/Printers/CMakeLists.txt 2017-02-16 14:10:27 +0000
3+++ plugins/Ubuntu/Settings/Printers/CMakeLists.txt 2017-02-16 14:10:27 +0000
4@@ -31,6 +31,7 @@
5
6 printer/printer.cpp
7 printer/printerjob.cpp
8+ printer/printersignalhandler.cpp
9 printers/printers.cpp
10
11 enums.h
12
13=== modified file 'plugins/Ubuntu/Settings/Printers/models/printermodel.cpp'
14--- plugins/Ubuntu/Settings/Printers/models/printermodel.cpp 2017-02-16 14:10:27 +0000
15+++ plugins/Ubuntu/Settings/Printers/models/printermodel.cpp 2017-02-16 14:10:27 +0000
16@@ -31,12 +31,14 @@
17 QObject::connect(m_backend, &PrinterBackend::printerAdded,
18 this, &PrinterModel::printerAdded);
19 QObject::connect(m_backend, &PrinterBackend::printerModified,
20- this, &PrinterModel::printerModified);
21+ &m_signalHandler, &PrinterSignalHandler::onPrinterModified);
22 QObject::connect(m_backend, &PrinterBackend::printerStateChanged,
23- this, &PrinterModel::printerModified);
24+ &m_signalHandler, &PrinterSignalHandler::onPrinterModified);
25 QObject::connect(m_backend, &PrinterBackend::printerDeleted,
26 this, &PrinterModel::printerDeleted);
27
28+ connect(&m_signalHandler, SIGNAL(printerModified(const QString&)),
29+ this, SLOT(printerModified(const QString&)));
30 connect(m_backend, SIGNAL(printerLoaded(QSharedPointer<Printer>)),
31 this, SLOT(printerLoaded(QSharedPointer<Printer>)));
32
33@@ -77,18 +79,11 @@
34 addPrinter(printer, CountChangeSignal::Emit);
35 }
36
37-void PrinterModel::printerModified(
38- const QString &text, const QString &printerUri,
39- const QString &printerName, uint printerState,
40- const QString &printerStateReason, bool acceptingJobs)
41+void PrinterModel::printerModified(const QString &printerName)
42 {
43- Q_UNUSED(text);
44- Q_UNUSED(printerUri);
45- Q_UNUSED(printerState);
46- Q_UNUSED(printerStateReason);
47- Q_UNUSED(acceptingJobs);
48-
49- m_backend->requestPrinter(printerName);
50+ // These signals might be emitted of a now deleted printer.
51+ if (getPrinterByName(printerName))
52+ m_backend->requestPrinter(printerName);
53 }
54
55 void PrinterModel::printerAdded(
56
57=== modified file 'plugins/Ubuntu/Settings/Printers/models/printermodel.h'
58--- plugins/Ubuntu/Settings/Printers/models/printermodel.h 2017-02-16 14:10:27 +0000
59+++ plugins/Ubuntu/Settings/Printers/models/printermodel.h 2017-02-16 14:10:27 +0000
60@@ -21,13 +21,13 @@
61
62 #include "models/jobmodel.h"
63 #include "printer/printer.h"
64+#include "printer/printersignalhandler.h"
65
66 #include <QAbstractListModel>
67 #include <QByteArray>
68 #include <QModelIndex>
69 #include <QObject>
70 #include <QSortFilterProxyModel>
71-#include <QTimer>
72 #include <QVariant>
73
74 class PRINTERS_DECL_EXPORT PrinterModel : public QAbstractListModel
75@@ -105,12 +105,11 @@
76 PrinterBackend *m_backend;
77
78 QList<QSharedPointer<Printer>> m_printers;
79+ PrinterSignalHandler m_signalHandler;
80
81 private Q_SLOTS:
82 void printerLoaded(QSharedPointer<Printer> printer);
83- void printerModified(const QString &text, const QString &printerUri,
84- const QString &printerName, uint printerState,
85- const QString &printerStateReason, bool acceptingJobs);
86+ void printerModified(const QString &printerName);
87 void printerAdded(const QString &text, const QString &printerUri,
88 const QString &printerName, uint printerState,
89 const QString &printerStateReason, bool acceptingJobs);
90
91=== added file 'plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.cpp'
92--- plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.cpp 1970-01-01 00:00:00 +0000
93+++ plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.cpp 2017-02-16 14:10:27 +0000
94@@ -0,0 +1,69 @@
95+/*
96+ * Copyright (C) 2017 Canonical, Ltd.
97+ *
98+ * This program is free software; you can redistribute it and/or modify
99+ * it under the terms of the GNU Lesser General Public License as published by
100+ * the Free Software Foundation; version 3.
101+ *
102+ * This program is distributed in the hope that it will be useful,
103+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
104+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
105+ * GNU Lesser General Public License for more details.
106+ *
107+ * You should have received a copy of the GNU Lesser General Public License
108+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
109+ */
110+
111+#include "printersignalhandler.h"
112+
113+PrinterSignalHandler::PrinterSignalHandler(int triggerEventDelay,
114+ QObject *parent)
115+ : QObject(parent)
116+{
117+ m_timer.setInterval(triggerEventDelay);
118+ connect(&m_timer, SIGNAL(timeout()), this, SLOT(process()));
119+}
120+
121+PrinterSignalHandler::~PrinterSignalHandler()
122+{
123+}
124+
125+void PrinterSignalHandler::process()
126+{
127+ Q_FOREACH(auto printer, m_unprocessed) {
128+ Q_EMIT printerModified(printer);
129+ }
130+ m_unprocessed.clear();
131+ m_timer.stop();
132+}
133+
134+void PrinterSignalHandler::onPrinterModified(
135+ const QString &text, const QString &printerUri,
136+ const QString &printerName, uint printerState,
137+ const QString &printerStateReason, bool acceptingJobs)
138+{
139+
140+ Q_UNUSED(text);
141+ Q_UNUSED(printerUri);
142+ Q_UNUSED(printerState);
143+ Q_UNUSED(printerStateReason);
144+ Q_UNUSED(acceptingJobs);
145+
146+ m_unprocessed << printerName;
147+ m_timer.start();
148+}
149+
150+void PrinterSignalHandler::onPrinterStateChanged(
151+ const QString &text, const QString &printerUri,
152+ const QString &printerName, uint printerState,
153+ const QString &printerStateReason, bool acceptingJobs)
154+{
155+ Q_UNUSED(text);
156+ Q_UNUSED(printerUri);
157+ Q_UNUSED(printerState);
158+ Q_UNUSED(printerStateReason);
159+ Q_UNUSED(acceptingJobs);
160+
161+ m_unprocessed << printerName;
162+ m_timer.start();
163+}
164
165=== added file 'plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.h'
166--- plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.h 1970-01-01 00:00:00 +0000
167+++ plugins/Ubuntu/Settings/Printers/printer/printersignalhandler.h 2017-02-16 14:10:27 +0000
168@@ -0,0 +1,55 @@
169+/*
170+ * Copyright (C) 2017 Canonical, Ltd.
171+ *
172+ * This program is free software; you can redistribute it and/or modify
173+ * it under the terms of the GNU Lesser General Public License as published by
174+ * the Free Software Foundation; version 3.
175+ *
176+ * This program is distributed in the hope that it will be useful,
177+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
178+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
179+ * GNU Lesser General Public License for more details.
180+ *
181+ * You should have received a copy of the GNU Lesser General Public License
182+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
183+ */
184+
185+#ifndef USC_PRINTER_SIGNAL_HANDLER_H
186+#define USC_PRINTER_SIGNAL_HANDLER_H
187+
188+#include "printers_global.h"
189+
190+#include <QObject>
191+#include <QSet>
192+#include <QTimer>
193+
194+class PRINTERS_DECL_EXPORT PrinterSignalHandler : public QObject
195+{
196+ Q_OBJECT
197+ QTimer m_timer;
198+ QSet<QString> m_unprocessed;
199+public:
200+ explicit PrinterSignalHandler(int triggerEventDelay = 500,
201+ QObject *parent = Q_NULLPTR);
202+ ~PrinterSignalHandler();
203+
204+public Q_SLOTS:
205+ void onPrinterModified(
206+ const QString &text, const QString &printerUri,
207+ const QString &printerName, uint printerState,
208+ const QString &printerStateReason, bool acceptingJobs
209+ );
210+ void onPrinterStateChanged(
211+ const QString &text, const QString &printerUri,
212+ const QString &printerName, uint printerState,
213+ const QString &printerStateReason, bool acceptingJobs
214+ );
215+
216+private Q_SLOTS:
217+ void process();
218+
219+Q_SIGNALS:
220+ void printerModified(const QString &printerName);
221+};
222+
223+#endif // USC_PRINTER_SIGNAL_HANDLER_H
224
225=== modified file 'tests/unittests/Printers/CMakeLists.txt'
226--- tests/unittests/Printers/CMakeLists.txt 2017-02-16 14:10:27 +0000
227+++ tests/unittests/Printers/CMakeLists.txt 2017-02-16 14:10:27 +0000
228@@ -40,3 +40,7 @@
229 add_executable(testPrintersJobFilter tst_jobfilter.cpp ${MOCK_SOURCES})
230 target_link_libraries(testPrintersJobFilter UbuntuSettingsPrintersQml Qt5::Test Qt5::Gui)
231 add_test(tst_jobfilter testPrintersJobFilter)
232+
233+add_executable(testPrintersSignalHandler tst_signalhandler.cpp ${MOCK_SOURCES})
234+target_link_libraries(testPrintersSignalHandler UbuntuSettingsPrintersQml Qt5::Test Qt5::Gui)
235+add_test(tst_signalhandler testPrintersSignalHandler)
236
237=== added file 'tests/unittests/Printers/tst_signalhandler.cpp'
238--- tests/unittests/Printers/tst_signalhandler.cpp 1970-01-01 00:00:00 +0000
239+++ tests/unittests/Printers/tst_signalhandler.cpp 2017-02-16 14:10:27 +0000
240@@ -0,0 +1,44 @@
241+/*
242+ * Copyright (C) 2017 Canonical, Ltd.
243+ *
244+ * This program is free software; you can redistribute it and/or modify
245+ * it under the terms of the GNU Lesser General Public License as published by
246+ * the Free Software Foundation; version 3.
247+ *
248+ * This program is distributed in the hope that it will be useful,
249+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
250+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
251+ * GNU Lesser General Public License for more details.
252+ *
253+ * You should have received a copy of the GNU Lesser General Public License
254+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
255+ */
256+
257+#include "printer/printersignalhandler.h"
258+
259+#include <QDebug>
260+#include <QObject>
261+#include <QSignalSpy>
262+#include <QTest>
263+
264+class TestSignalHandler : public QObject
265+{
266+ Q_OBJECT
267+private Q_SLOTS:
268+ void testEmptyCount()
269+ {
270+ PrinterSignalHandler handler(500);
271+ QSignalSpy modifiedSpy(&handler, SIGNAL(printerModified(const QString&)));
272+
273+ for (int i = 0; i < 500; i++) {
274+ handler.onPrinterStateChanged("spam!", "ipp://bar/baz", "printer-a", 0, "none", true);
275+ }
276+
277+ modifiedSpy.wait(1000);
278+ QCOMPARE(modifiedSpy.count(), 1);
279+ }
280+};
281+
282+QTEST_GUILESS_MAIN(TestSignalHandler)
283+#include "tst_signalhandler.moc"
284+

Subscribers

People subscribed via source and target branches