Merge lp:~ahayzen/ubuntu-settings-components/add-job-model into lp:~phablet-team/ubuntu-settings-components/printer-components

Proposed by Andrew Hayzen
Status: Merged
Approved by: Jonas G. Drange
Approved revision: 227
Merged at revision: 224
Proposed branch: lp:~ahayzen/ubuntu-settings-components/add-job-model
Merge into: lp:~phablet-team/ubuntu-settings-components/printer-components
Prerequisite: lp:~ahayzen/ubuntu-settings-components/add-cancel-method
Diff against target: 823 lines (+527/-12)
16 files modified
examples/PrinterQueue.qml (+113/-0)
plugins/Ubuntu/Settings/Printers/CMakeLists.txt (+1/-0)
plugins/Ubuntu/Settings/Printers/backend/backend.cpp (+7/-0)
plugins/Ubuntu/Settings/Printers/backend/backend.h (+1/-0)
plugins/Ubuntu/Settings/Printers/backend/backend_cups.cpp (+17/-0)
plugins/Ubuntu/Settings/Printers/backend/backend_cups.h (+1/-0)
plugins/Ubuntu/Settings/Printers/cups/cupsfacade.cpp (+18/-0)
plugins/Ubuntu/Settings/Printers/cups/cupsfacade.h (+1/-0)
plugins/Ubuntu/Settings/Printers/enums.h (+13/-0)
plugins/Ubuntu/Settings/Printers/models/jobmodel.cpp (+222/-0)
plugins/Ubuntu/Settings/Printers/models/jobmodel.h (+75/-0)
plugins/Ubuntu/Settings/Printers/models/printermodel.cpp (+17/-0)
plugins/Ubuntu/Settings/Printers/models/printermodel.h (+3/-0)
plugins/Ubuntu/Settings/Printers/printer/printerjob.cpp (+24/-8)
plugins/Ubuntu/Settings/Printers/printer/printerjob.h (+9/-4)
tests/unittests/Printers/mockbackend.h (+5/-0)
To merge this branch: bzr merge lp:~ahayzen/ubuntu-settings-components/add-job-model
Reviewer Review Type Date Requested Status
Jonas G. Drange (community) Approve
Review via email: mp+316242@code.launchpad.net

Commit message

* Add JobModel which lists the jobs for a certain printer
* Add JobRole to PrinterModel to access jobs
* Add JobState enum to track enums from cups
* Add example Queue which lists jobs for a printer with their name, id, status and allows you to cancel the job by clicking

Description of the change

* Add JobModel which lists the jobs for a certain printer
* Add JobRole to PrinterModel to access jobs
* Add JobState enum to track enums from cups
* Add example Queue which lists jobs for a printer with their name, id, status and allows you to cancel the job by clicking

To post a comment you must log in.
226. By Andrew Hayzen

* Remove some tracing

Revision history for this message
Jonas G. Drange (jonas-drange) wrote :

Added a couple of comments.

review: Needs Fixing
227. By Andrew Hayzen

* Use QSharedPointer instead of raw pointers for PrinterJob
* Renamed Queue.qml to PrinterQueue.qml

Revision history for this message
Jonas G. Drange (jonas-drange) wrote :

THX! LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'examples/PrinterQueue.qml'
--- examples/PrinterQueue.qml 1970-01-01 00:00:00 +0000
+++ examples/PrinterQueue.qml 2017-02-02 17:23:30 +0000
@@ -0,0 +1,113 @@
1/*
2 * Copyright 2017 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by Jonas G. Drange <jonas.drange@canonical.com>
17 * Andrew Hayzen <andrew.hayzen@canonical.com>
18 */
19
20import QtQuick 2.4
21import QtQuick.Layouts 1.1
22import Ubuntu.Components 1.3
23import Ubuntu.Components.ListItems 1.3 as ListItems
24import Ubuntu.Settings.Components 0.1
25import Ubuntu.Settings.Printers 0.1
26
27MainView {
28 width: units.gu(50)
29 height: units.gu(90)
30
31 Component {
32 id: queuePage
33
34 Page {
35 header: PageHeader {
36 title: "Queue: " + printer.name
37 flickable: queueView
38 }
39 visible: false
40
41 property var printer
42
43 ListView {
44 id: queueView
45 anchors {
46 fill: parent
47 }
48 delegate: ListItem {
49 height: modelLayout.height + (divider.visible ? divider.height : 0)
50 ListItemLayout {
51 id: modelLayout
52 title.text: displayName
53 subtitle.text: "Job: " + model.id + " State: " + model.state
54 }
55 onClicked: {
56 console.debug("Cancel:", printer.name, model.id);
57 Printers.cancelJob(printer.name, model.id);
58 }
59 }
60 model: printer.jobs
61
62 Label {
63 anchors {
64 centerIn: parent
65 }
66 text: "Empty queue"
67 visible: queueView.count === 0
68 }
69 }
70 }
71 }
72
73 PageStack {
74 id: pageStack
75
76 Page {
77 id: printersPage
78 header: PageHeader {
79 title: "Printers"
80 flickable: printerList
81 }
82 visible: false
83
84 ListView {
85 id: printerList
86 anchors { fill: parent }
87 model: Printers.allPrintersWithPdf
88 delegate: ListItem {
89 height: modelLayout.height + (divider.visible ? divider.height : 0)
90 ListItemLayout {
91 id: modelLayout
92 title.text: displayName
93 title.font.bold: model.default
94 subtitle.text: description
95
96 Icon {
97 id: icon
98 width: height
99 height: units.gu(2.5)
100 name: "printer-symbolic"
101 SlotsLayout.position: SlotsLayout.First
102 }
103
104 ProgressionSlot {}
105 }
106 onClicked: pageStack.push(queuePage, { printer: model })
107 }
108 }
109 }
110
111 Component.onCompleted: push(printersPage)
112 }
113}
0114
=== modified file 'plugins/Ubuntu/Settings/Printers/CMakeLists.txt'
--- plugins/Ubuntu/Settings/Printers/CMakeLists.txt 2017-01-26 21:47:06 +0000
+++ plugins/Ubuntu/Settings/Printers/CMakeLists.txt 2017-02-02 17:23:30 +0000
@@ -18,6 +18,7 @@
18 enums.h18 enums.h
19 i18n.cpp19 i18n.cpp
20 models/drivermodel.cpp20 models/drivermodel.cpp
21 models/jobmodel.cpp
21 models/printermodel.cpp22 models/printermodel.cpp
22 printer/printer.cpp23 printer/printer.cpp
23 printer/printerjob.cpp24 printer/printerjob.cpp
2425
=== modified file 'plugins/Ubuntu/Settings/Printers/backend/backend.cpp'
--- plugins/Ubuntu/Settings/Printers/backend/backend.cpp 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/backend/backend.cpp 2017-02-02 17:23:30 +0000
@@ -202,6 +202,13 @@
202 return -1;202 return -1;
203}203}
204204
205QList<QSharedPointer<PrinterJob>> PrinterBackend::printerGetJobs(const QString &name)
206{
207 Q_UNUSED(name);
208
209 return QList<QSharedPointer<PrinterJob>>{};
210}
211
205QString PrinterBackend::printerName() const212QString PrinterBackend::printerName() const
206{213{
207 return QString();214 return QString();
208215
=== modified file 'plugins/Ubuntu/Settings/Printers/backend/backend.h'
--- plugins/Ubuntu/Settings/Printers/backend/backend.h 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/backend/backend.h 2017-02-02 17:23:30 +0000
@@ -118,6 +118,7 @@
118 virtual int printFileToDest(const QString &filepath,118 virtual int printFileToDest(const QString &filepath,
119 const QString &title,119 const QString &title,
120 const cups_dest_t *dest);120 const cups_dest_t *dest);
121 virtual QList<QSharedPointer<PrinterJob>> printerGetJobs(const QString &name);
121122
122 virtual QString printerName() const;123 virtual QString printerName() const;
123 virtual QString description() const;124 virtual QString description() const;
124125
=== modified file 'plugins/Ubuntu/Settings/Printers/backend/backend_cups.cpp'
--- plugins/Ubuntu/Settings/Printers/backend/backend_cups.cpp 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/backend/backend_cups.cpp 2017-02-02 17:23:30 +0000
@@ -210,6 +210,23 @@
210 return m_cups->printFileToDest(filepath, title, dest);210 return m_cups->printFileToDest(filepath, title, dest);
211}211}
212212
213QList<QSharedPointer<PrinterJob>> PrinterCupsBackend::printerGetJobs(const QString &name)
214{
215 auto jobs = m_cups->printerGetJobs(name);
216 QList<QSharedPointer<PrinterJob>> list;
217
218 Q_FOREACH(auto job, jobs) {
219 auto newJob = QSharedPointer<PrinterJob>(new PrinterJob(name, this, job->id));
220
221 newJob->setState(static_cast<PrinterEnum::JobState>(job->state));
222 newJob->setTitle(QString::fromLocal8Bit(job->title));
223
224 list.append(newJob);
225 }
226
227 return list;
228}
229
213QString PrinterCupsBackend::printerName() const230QString PrinterCupsBackend::printerName() const
214{231{
215 return m_info.printerName();232 return m_info.printerName();
216233
=== modified file 'plugins/Ubuntu/Settings/Printers/backend/backend_cups.h'
--- plugins/Ubuntu/Settings/Printers/backend/backend_cups.h 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/backend/backend_cups.h 2017-02-02 17:23:30 +0000
@@ -97,6 +97,7 @@
97 virtual int printFileToDest(const QString &filepath,97 virtual int printFileToDest(const QString &filepath,
98 const QString &title,98 const QString &title,
99 const cups_dest_t *dest) override;99 const cups_dest_t *dest) override;
100 virtual QList<QSharedPointer<PrinterJob>> printerGetJobs(const QString &name) override;
100101
101 virtual QString printerName() const override;102 virtual QString printerName() const override;
102 virtual QString description() const override;103 virtual QString description() const override;
103104
=== modified file 'plugins/Ubuntu/Settings/Printers/cups/cupsfacade.cpp'
--- plugins/Ubuntu/Settings/Printers/cups/cupsfacade.cpp 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/cups/cupsfacade.cpp 2017-02-02 17:23:30 +0000
@@ -376,6 +376,24 @@
376 }376 }
377}377}
378378
379QList<cups_job_t *> CupsFacade::printerGetJobs(const QString &name)
380{
381 QList<cups_job_t *> list;
382 cups_job_t *jobs;
383
384 // Get a list of the jobs that are 'mine' and only active ones
385 // https://www.cups.org/doc/api-cups.html#cupsGetJobs
386 int count = cupsGetJobs(&jobs, name.toLocal8Bit(), 1, CUPS_WHICHJOBS_ACTIVE);
387
388 for (int i=0; i < count; i++) {
389 list.append(&jobs[i]);
390 }
391
392 // FIXME: needs to run cupsFreeJobs();
393
394 return list;
395}
396
379int CupsFacade::printFileToDest(const QString &filepath, const QString &title,397int CupsFacade::printFileToDest(const QString &filepath, const QString &title,
380 const cups_dest_t *dest)398 const cups_dest_t *dest)
381{399{
382400
=== modified file 'plugins/Ubuntu/Settings/Printers/cups/cupsfacade.h'
--- plugins/Ubuntu/Settings/Printers/cups/cupsfacade.h 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/cups/cupsfacade.h 2017-02-02 17:23:30 +0000
@@ -89,6 +89,7 @@
89 QList<PrintQuality> printerGetSupportedQualities(const QString &name) const;89 QList<PrintQuality> printerGetSupportedQualities(const QString &name) const;
9090
91 void cancelJob(const QString &name, const int jobId);91 void cancelJob(const QString &name, const int jobId);
92 QList<cups_job_t *> printerGetJobs(const QString &name);
92 int printFileToDest(const QString &filepath, const QString &title,93 int printFileToDest(const QString &filepath, const QString &title,
93 const cups_dest_t *dest);94 const cups_dest_t *dest);
9495
9596
=== modified file 'plugins/Ubuntu/Settings/Printers/enums.h'
--- plugins/Ubuntu/Settings/Printers/enums.h 2017-01-19 21:00:28 +0000
+++ plugins/Ubuntu/Settings/Printers/enums.h 2017-02-02 17:23:30 +0000
@@ -93,6 +93,19 @@
93 };93 };
94 Q_ENUM(ErrorPolicy)94 Q_ENUM(ErrorPolicy)
9595
96 // Match enums from ipp_jstate_t
97 enum class JobState
98 {
99 Pending = 3,
100 Held,
101 Processing,
102 Stopped,
103 Canceled,
104 Aborted,
105 Complete,
106 };
107 Q_ENUM(JobState)
108
96 enum class OperationPolicy109 enum class OperationPolicy
97 {110 {
98 DefaultOperation = 0,111 DefaultOperation = 0,
99112
=== added file 'plugins/Ubuntu/Settings/Printers/models/jobmodel.cpp'
--- plugins/Ubuntu/Settings/Printers/models/jobmodel.cpp 1970-01-01 00:00:00 +0000
+++ plugins/Ubuntu/Settings/Printers/models/jobmodel.cpp 2017-02-02 17:23:30 +0000
@@ -0,0 +1,222 @@
1/*
2 * Copyright (C) 2017 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#include "utils.h"
18
19#include "backend/backend_cups.h"
20#include "cups/cupsfacade.h"
21
22#include "models/jobmodel.h"
23
24#include <QDebug>
25
26JobModel::JobModel(const int updateIntervalMSecs, QObject *parent)
27 : JobModel(QStringLiteral(""), new PrinterCupsBackend, updateIntervalMSecs, parent)
28{
29}
30
31JobModel::JobModel(const QString &printerName, PrinterBackend *backend,
32 const int updateIntervalMSecs,
33 QObject *parent)
34 : QAbstractListModel(parent)
35 , m_backend(backend)
36 , m_printer_name(printerName)
37{
38 update();
39 startUpdateTimer(updateIntervalMSecs);
40}
41
42JobModel::~JobModel()
43{
44}
45
46void JobModel::startUpdateTimer(const int &msecs)
47{
48 // Start a timer to poll for changes in the printers
49 m_update_timer.setParent(this);
50 connect(&m_update_timer, SIGNAL(timeout()), this, SLOT(update()));
51 m_update_timer.start(msecs);
52}
53
54void JobModel::update()
55{
56 // Store the old count and get the new printers
57 int oldCount = m_jobs.size();
58 QList<QSharedPointer<PrinterJob>> newJobs = m_backend->printerGetJobs(m_printer_name);
59
60 /* If any printers returned from the backend are irrelevant, we delete
61 them. This a list of indices that corresponds to printers scheduled for
62 deletion in newPrinters. */
63 QList<uint> forDeletion;
64
65 // Go through the old model
66 for (int i=0; i < m_jobs.count(); i++) {
67 // Determine if the old printer exists in the new model
68 bool exists = false;
69
70 Q_FOREACH(QSharedPointer<PrinterJob> p, newJobs) {
71 // TODO: update status here
72 if (p->jobId() == m_jobs.at(i)->jobId()) {
73 exists = true;
74 break;
75 }
76 }
77
78 // If it doesn't exist then remove it from the old model
79 if (!exists) {
80 beginRemoveRows(QModelIndex(), i, i);
81 QSharedPointer<PrinterJob> p = m_jobs.takeAt(i);
82 p->deleteLater();
83 endRemoveRows();
84
85 i--; // as we have removed an item decrement
86 }
87 }
88
89 // Go through the new model
90 for (int i=0; i < newJobs.count(); i++) {
91 // Determine if the new printer exists in the old model
92 bool exists = false;
93 int j;
94
95 for (j=0; j < m_jobs.count(); j++) {
96 if (m_jobs.at(j)->jobId() == newJobs.at(i)->jobId()) {
97 exists = true;
98 forDeletion << i;
99 break;
100 }
101 }
102
103 if (exists) {
104 if (j == i) { // New printer exists and in correct position
105 continue;
106 } else {
107 // New printer does exist but needs to be moved in old model
108 beginMoveRows(QModelIndex(), j, 1, QModelIndex(), i);
109 m_jobs.move(j, i);
110 endMoveRows();
111 }
112
113 // We can safely delete the newPrinter as it already exists.
114 forDeletion << i;
115 } else {
116 // New printer does not exist insert into model
117 beginInsertRows(QModelIndex(), i, i);
118 m_jobs.insert(i, newJobs.at(i));
119 endInsertRows();
120 }
121 }
122
123 Q_FOREACH(const int &index, forDeletion) {
124 newJobs.at(index)->deleteLater();
125 }
126
127 if (oldCount != m_jobs.size()) {
128 Q_EMIT countChanged();
129 }
130}
131
132int JobModel::rowCount(const QModelIndex &parent) const
133{
134 Q_UNUSED(parent);
135 return m_jobs.size();
136}
137
138int JobModel::count() const
139{
140 return rowCount();
141}
142
143QVariant JobModel::data(const QModelIndex &index, int role) const
144{
145 QVariant ret;
146
147 if ((0<=index.row()) && (index.row()<m_jobs.size())) {
148
149 auto job = m_jobs[index.row()];
150
151 switch (role) {
152 case IdRole:
153 ret = job->jobId();
154 break;
155 case OwnerRole:
156 ret = m_printer_name;
157 break;
158 case StateRole:
159 // TODO: improve, for now have a switch
160 switch (job->state()) {
161 case PrinterEnum::JobState::Aborted:
162 ret = "Aborted";
163 break;
164 case PrinterEnum::JobState::Canceled:
165 ret = "Canceled";
166 break;
167 case PrinterEnum::JobState::Complete:
168 ret = "Compelete";
169 break;
170 case PrinterEnum::JobState::Held:
171 ret = "Held";
172 break;
173 case PrinterEnum::JobState::Pending:
174 ret = "Pending";
175 break;
176 case PrinterEnum::JobState::Processing:
177 ret = "Processing";
178 break;
179 case PrinterEnum::JobState::Stopped:
180 ret = "Stopped";
181 break;
182 }
183 break;
184 case Qt::DisplayRole:
185 case TitleRole:
186 ret = job->title();
187 break;
188 }
189 }
190
191 return ret;
192}
193
194QHash<int, QByteArray> JobModel::roleNames() const
195{
196 static QHash<int,QByteArray> names;
197
198 if (Q_UNLIKELY(names.empty())) {
199 names[Qt::DisplayRole] = "displayName";
200 names[IdRole] = "id";
201 names[OwnerRole] = "owner";
202 names[StateRole] = "state";
203 names[TitleRole] = "title";
204 names[LastStateMessageRole] = "lastStateMessage";
205 }
206
207 return names;
208}
209
210QVariantMap JobModel::get(const int row) const
211{
212 QHashIterator<int, QByteArray> iterator(roleNames());
213 QVariantMap result;
214 QModelIndex modelIndex = index(row, 0);
215
216 while (iterator.hasNext()) {
217 iterator.next();
218 result[iterator.value()] = modelIndex.data(iterator.key());
219 }
220
221 return result;
222}
0223
=== added file 'plugins/Ubuntu/Settings/Printers/models/jobmodel.h'
--- plugins/Ubuntu/Settings/Printers/models/jobmodel.h 1970-01-01 00:00:00 +0000
+++ plugins/Ubuntu/Settings/Printers/models/jobmodel.h 2017-02-02 17:23:30 +0000
@@ -0,0 +1,75 @@
1/*
2 * Copyright (C) 2017 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef USC_JOB_MODEL_H
18#define USC_JOB_MODEL_H
19
20#include "printers_global.h"
21
22#include "printer/printer.h"
23
24#include <QAbstractListModel>
25#include <QByteArray>
26#include <QModelIndex>
27#include <QObject>
28#include <QSortFilterProxyModel>
29#include <QTimer>
30#include <QVariant>
31
32class PRINTERS_DECL_EXPORT JobModel : public QAbstractListModel
33{
34 Q_OBJECT
35
36 Q_PROPERTY(int count READ count NOTIFY countChanged)
37public:
38 explicit JobModel(const int updateIntervalMSecs=5000, QObject *parent = Q_NULLPTR);
39 explicit JobModel(const QString &printerName, PrinterBackend *backend, const int updateIntervalMSecs=5000,
40 QObject *parent = Q_NULLPTR);
41 ~JobModel();
42
43 enum Roles
44 {
45 // Qt::DisplayRole holds job title
46 IdRole = Qt::UserRole,
47 OwnerRole,
48 StateRole,
49 TitleRole,
50 LastStateMessageRole,
51 LastRole = LastStateMessageRole,
52 };
53
54 virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
55 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
56 virtual QHash<int, QByteArray> roleNames() const override;
57
58 int count() const;
59
60 Q_INVOKABLE QVariantMap get(const int row) const;
61private:
62 QTimer m_update_timer;
63 PrinterBackend *m_backend;
64 QString m_printer_name;
65
66 QList<QSharedPointer<PrinterJob>> m_jobs;
67private Q_SLOTS:
68 void startUpdateTimer(const int &msecs);
69 void update();
70
71Q_SIGNALS:
72 void countChanged();
73};
74
75#endif // USC_JOB_MODEL_H
076
=== modified file 'plugins/Ubuntu/Settings/Printers/models/printermodel.cpp'
--- plugins/Ubuntu/Settings/Printers/models/printermodel.cpp 2017-01-22 19:31:39 +0000
+++ plugins/Ubuntu/Settings/Printers/models/printermodel.cpp 2017-02-02 17:23:30 +0000
@@ -18,9 +18,11 @@
1818
19#include "backend/backend_cups.h"19#include "backend/backend_cups.h"
20#include "cups/cupsfacade.h"20#include "cups/cupsfacade.h"
21#include "models/jobmodel.h"
21#include "models/printermodel.h"22#include "models/printermodel.h"
2223
23#include <QDebug>24#include <QDebug>
25#include <QQmlEngine>
2426
25PrinterModel::PrinterModel(const int updateIntervalMSecs, QObject *parent)27PrinterModel::PrinterModel(const int updateIntervalMSecs, QObject *parent)
26 : PrinterModel(new PrinterCupsBackend, updateIntervalMSecs, parent)28 : PrinterModel(new PrinterCupsBackend, updateIntervalMSecs, parent)
@@ -76,7 +78,11 @@
76 if (!exists) {78 if (!exists) {
77 beginRemoveRows(QModelIndex(), i, i);79 beginRemoveRows(QModelIndex(), i, i);
78 Printer *p = m_printers.takeAt(i);80 Printer *p = m_printers.takeAt(i);
81 JobModel *jobModel = m_job_models.take(p->name());
82
79 p->deleteLater();83 p->deleteLater();
84 jobModel->deleteLater();
85
80 endRemoveRows();86 endRemoveRows();
8187
82 i--; // as we have removed an item decrement88 i--; // as we have removed an item decrement
@@ -112,7 +118,13 @@
112 } else {118 } else {
113 // New printer does not exist insert into model119 // New printer does not exist insert into model
114 beginInsertRows(QModelIndex(), i, i);120 beginInsertRows(QModelIndex(), i, i);
121
115 m_printers.insert(i, newPrinters.at(i));122 m_printers.insert(i, newPrinters.at(i));
123
124 JobModel *model = new JobModel(newPrinters.at(i)->name(), m_backend, 5000, this);
125 QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership);
126 m_job_models.insert(newPrinters.at(i)->name(), model);
127
116 endInsertRows();128 endInsertRows();
117 }129 }
118 }130 }
@@ -226,6 +238,10 @@
226 case IsPdfRole:238 case IsPdfRole:
227 ret = printer->isPdf();239 ret = printer->isPdf();
228 break;240 break;
241 case JobRole: {
242 ret = QVariant::fromValue(m_job_models.value(printer->name()));
243 break;
244 }
229 // case LastStateMessageRole:245 // case LastStateMessageRole:
230 // ret = printer->lastStateMessage();246 // ret = printer->lastStateMessage();
231 // break;247 // break;
@@ -311,6 +327,7 @@
311 names[StateRole] = "state";327 names[StateRole] = "state";
312 names[PrinterRole] = "printer";328 names[PrinterRole] = "printer";
313 names[IsPdfRole] = "isPdf";329 names[IsPdfRole] = "isPdf";
330 names[JobRole] = "jobs";
314 names[LastStateMessageRole] = "lastStateMessage";331 names[LastStateMessageRole] = "lastStateMessage";
315 }332 }
316333
317334
=== modified file 'plugins/Ubuntu/Settings/Printers/models/printermodel.h'
--- plugins/Ubuntu/Settings/Printers/models/printermodel.h 2017-01-22 14:21:11 +0000
+++ plugins/Ubuntu/Settings/Printers/models/printermodel.h 2017-02-02 17:23:30 +0000
@@ -19,6 +19,7 @@
1919
20#include "printers_global.h"20#include "printers_global.h"
2121
22#include "models/jobmodel.h"
22#include "printer/printer.h"23#include "printer/printer.h"
2324
24#include <QAbstractListModel>25#include <QAbstractListModel>
@@ -64,6 +65,7 @@
64 PrinterRole,65 PrinterRole,
65 LastStateMessageRole,66 LastStateMessageRole,
66 IsPdfRole,67 IsPdfRole,
68 JobRole,
67 LastRole = LastStateMessageRole,69 LastRole = LastStateMessageRole,
68 };70 };
6971
@@ -84,6 +86,7 @@
84 /* FIXME: there's currently no need to share the Printer obj with QML, so86 /* FIXME: there's currently no need to share the Printer obj with QML, so
85 this should be normal pointers that are deletedLater. */87 this should be normal pointers that are deletedLater. */
86 QList<Printer*> m_printers;88 QList<Printer*> m_printers;
89 QMap<QString, JobModel *> m_job_models;
8790
88private Q_SLOTS:91private Q_SLOTS:
89 void startUpdateTimer(const int &msecs);92 void startUpdateTimer(const int &msecs);
9093
=== modified file 'plugins/Ubuntu/Settings/Printers/printer/printerjob.cpp'
--- plugins/Ubuntu/Settings/Printers/printer/printerjob.cpp 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/printer/printerjob.cpp 2017-02-02 17:23:30 +0000
@@ -40,18 +40,33 @@
40 , m_backend(backend)40 , m_backend(backend)
41 , m_duplex_mode(0)41 , m_duplex_mode(0)
42 , m_is_two_sided(false)42 , m_is_two_sided(false)
43 , m_job_id(-1)
43 , m_printer(printer)44 , m_printer(printer)
44 , m_printer_name(QStringLiteral(""))45 , m_printer_name(QStringLiteral(""))
45 , m_print_range(QStringLiteral(""))46 , m_print_range(QStringLiteral(""))
46 , m_print_range_mode(PrinterEnum::PrintRange::AllPages)47 , m_print_range_mode(PrinterEnum::PrintRange::AllPages)
47 , m_quality(0)48 , m_quality(0)
48 , m_state(PrinterEnum::State::IdleState)49 , m_state(PrinterEnum::JobState::Pending)
49 , m_reverse(false)50 , m_reverse(false)
50 , m_title(QStringLiteral(""))51 , m_title(QStringLiteral(""))
51{52{
53 if (m_printer) {
54 m_printer_name = printer->name();
55 }
56
52 loadDefaults();57 loadDefaults();
53}58}
5459
60PrinterJob::PrinterJob(const QString &name, PrinterBackend *backend, int jobId, QObject *parent)
61 : QObject(parent)
62 , m_backend(backend)
63 , m_job_id(jobId)
64{
65 setPrinterName(name);
66
67 // TODO: load other options from job
68}
69
5570
56PrinterJob::~PrinterJob()71PrinterJob::~PrinterJob()
57{72{
@@ -115,6 +130,11 @@
115 return m_is_two_sided;130 return m_is_two_sided;
116}131}
117132
133int PrinterJob::jobId() const
134{
135 return m_job_id;
136}
137
118bool PrinterJob::landscape() const138bool PrinterJob::landscape() const
119{139{
120 return m_landscape;140 return m_landscape;
@@ -143,11 +163,7 @@
143void PrinterJob::printFile(const QUrl &url)163void PrinterJob::printFile(const QUrl &url)
144{164{
145 if (m_printer) {165 if (m_printer) {
146 int jobId = m_printer->printFile(url.toLocalFile(), this);166 m_job_id = m_printer->printFile(url.toLocalFile(), this);
147
148 // TODO: should we track the job and state of it here?
149 // so then we can do cancel() and show in the UI when the job is done?
150 Q_UNUSED(jobId);
151 } else {167 } else {
152 qWarning() << "No valid printer in PrinterJob";168 qWarning() << "No valid printer in PrinterJob";
153 }169 }
@@ -173,7 +189,7 @@
173 return m_reverse;189 return m_reverse;
174}190}
175191
176PrinterEnum::State PrinterJob::state() const192PrinterEnum::JobState PrinterJob::state() const
177{193{
178 return m_state;194 return m_state;
179}195}
@@ -311,7 +327,7 @@
311 }327 }
312}328}
313329
314void PrinterJob::setState(const PrinterEnum::State &state)330void PrinterJob::setState(const PrinterEnum::JobState &state)
315{331{
316 if (m_state != state) {332 if (m_state != state) {
317 m_state = state;333 m_state = state;
318334
=== modified file 'plugins/Ubuntu/Settings/Printers/printer/printerjob.h'
--- plugins/Ubuntu/Settings/Printers/printer/printerjob.h 2017-02-02 17:23:30 +0000
+++ plugins/Ubuntu/Settings/Printers/printer/printerjob.h 2017-02-02 17:23:30 +0000
@@ -47,13 +47,16 @@
47 Q_PROPERTY(PrinterEnum::PrintRange printRangeMode READ printRangeMode WRITE setPrintRangeMode NOTIFY printRangeModeChanged)47 Q_PROPERTY(PrinterEnum::PrintRange printRangeMode READ printRangeMode WRITE setPrintRangeMode NOTIFY printRangeModeChanged)
48 Q_PROPERTY(int quality READ quality WRITE setQuality NOTIFY qualityChanged)48 Q_PROPERTY(int quality READ quality WRITE setQuality NOTIFY qualityChanged)
49 Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged)49 Q_PROPERTY(bool reverse READ reverse WRITE setReverse NOTIFY reverseChanged)
50 Q_PROPERTY(PrinterEnum::State state READ state NOTIFY stateChanged)50 Q_PROPERTY(PrinterEnum::JobState state READ state NOTIFY stateChanged)
51 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)51 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
52
53 friend class PrinterCupsBackend;
52public:54public:
53 explicit PrinterJob(QObject *parent=Q_NULLPTR);55 explicit PrinterJob(QObject *parent=Q_NULLPTR);
54 explicit PrinterJob(Printer *printer, QObject *parent=Q_NULLPTR);56 explicit PrinterJob(Printer *printer, QObject *parent=Q_NULLPTR);
55 explicit PrinterJob(Printer *printer, PrinterBackend *backend,57 explicit PrinterJob(Printer *printer, PrinterBackend *backend,
56 QObject *parent=Q_NULLPTR);58 QObject *parent=Q_NULLPTR);
59 explicit PrinterJob(const QString &name, PrinterBackend *backend, int jobId, QObject *parent=Q_NULLPTR);
57 ~PrinterJob();60 ~PrinterJob();
5861
59 bool collate() const;62 bool collate() const;
@@ -62,6 +65,7 @@
62 int copies() const;65 int copies() const;
63 int duplexMode() const;66 int duplexMode() const;
64 bool isTwoSided() const;67 bool isTwoSided() const;
68 int jobId() const; // TODO: implement
65 bool landscape() const;69 bool landscape() const;
66// Printer *printer() const;70// Printer *printer() const;
67 QString printerName() const;71 QString printerName() const;
@@ -69,7 +73,7 @@
69 PrinterEnum::PrintRange printRangeMode() const;73 PrinterEnum::PrintRange printRangeMode() const;
70 int quality() const;74 int quality() const;
71 bool reverse() const;75 bool reverse() const;
72 PrinterEnum::State state() const;76 PrinterEnum::JobState state() const;
73 QString title() const;77 QString title() const;
74public Q_SLOTS:78public Q_SLOTS:
75 PrinterEnum::DuplexMode getDuplexMode() const;79 PrinterEnum::DuplexMode getDuplexMode() const;
@@ -91,7 +95,7 @@
91private Q_SLOTS:95private Q_SLOTS:
92 void loadDefaults();96 void loadDefaults();
93 void setIsTwoSided(const bool isTwoSided);97 void setIsTwoSided(const bool isTwoSided);
94 void setState(const PrinterEnum::State &state);98 void setState(const PrinterEnum::JobState &state);
95Q_SIGNALS:99Q_SIGNALS:
96 void collateChanged();100 void collateChanged();
97 void colorModelChanged();101 void colorModelChanged();
@@ -115,6 +119,7 @@
115 PrinterBackend *m_backend; // TODO: Maybe use the printer's backend?119 PrinterBackend *m_backend; // TODO: Maybe use the printer's backend?
116 int m_duplex_mode;120 int m_duplex_mode;
117 bool m_is_two_sided;121 bool m_is_two_sided;
122 int m_job_id;
118 bool m_landscape;123 bool m_landscape;
119 Printer *m_printer;124 Printer *m_printer;
120 QString m_printer_name;125 QString m_printer_name;
@@ -122,7 +127,7 @@
122 PrinterEnum::PrintRange m_print_range_mode;127 PrinterEnum::PrintRange m_print_range_mode;
123 int m_quality;128 int m_quality;
124 bool m_reverse;129 bool m_reverse;
125 PrinterEnum::State m_state;130 PrinterEnum::JobState m_state;
126 QString m_title;131 QString m_title;
127};132};
128133
129134
=== modified file 'tests/unittests/Printers/mockbackend.h'
--- tests/unittests/Printers/mockbackend.h 2017-02-02 17:23:30 +0000
+++ tests/unittests/Printers/mockbackend.h 2017-02-02 17:23:30 +0000
@@ -222,6 +222,11 @@
222222
223 }223 }
224224
225 virtual QList<QSharedPointer<PrinterJob>> printerGetJobs(const QString &name) override
226 {
227
228 }
229
225230
226 virtual QString printerName() const override231 virtual QString printerName() const override
227 {232 {

Subscribers

People subscribed via source and target branches