Merge lp:~jonas-drange/ubuntu-ui-extras/dedupe-requests into lp:ubuntu-ui-extras
- dedupe-requests
- Merge into trunk
Proposed by
Jonas G. Drange
Status: | Superseded |
---|---|
Proposed branch: | lp:~jonas-drange/ubuntu-ui-extras/dedupe-requests |
Merge into: | lp:ubuntu-ui-extras |
Diff against target: |
9609 lines (+9257/-4) 55 files modified
debian/control (+3/-0) modules/Ubuntu/Components/Extras/CMakeLists.txt (+1/-0) modules/Ubuntu/Components/Extras/Example/PrinterQueue.qml (+117/-0) modules/Ubuntu/Components/Extras/Example/Printers.qml (+603/-0) modules/Ubuntu/Components/Extras/Printers/CMakeLists.txt (+68/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend.cpp (+272/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend.h (+201/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.cpp (+722/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.h (+135/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.cpp (+119/-0) modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.h (+47/-0) modules/Ubuntu/Components/Extras/Printers/cups/ippclient.cpp (+988/-0) modules/Ubuntu/Components/Extras/Printers/cups/ippclient.h (+121/-0) modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.cpp (+127/-0) modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.h (+61/-0) modules/Ubuntu/Components/Extras/Printers/cups/printerloader.cpp (+53/-0) modules/Ubuntu/Components/Extras/Printers/cups/printerloader.h (+49/-0) modules/Ubuntu/Components/Extras/Printers/enums.h (+106/-0) modules/Ubuntu/Components/Extras/Printers/i18n.cpp (+42/-0) modules/Ubuntu/Components/Extras/Printers/i18n.h (+29/-0) modules/Ubuntu/Components/Extras/Printers/models/drivermodel.cpp (+175/-0) modules/Ubuntu/Components/Extras/Printers/models/drivermodel.h (+82/-0) modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp (+390/-0) modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h (+125/-0) modules/Ubuntu/Components/Extras/Printers/models/printermodel.cpp (+503/-0) modules/Ubuntu/Components/Extras/Printers/models/printermodel.h (+154/-0) modules/Ubuntu/Components/Extras/Printers/org.cups.cupsd.Notifier.xml (+146/-0) modules/Ubuntu/Components/Extras/Printers/plugin.cpp (+58/-0) modules/Ubuntu/Components/Extras/Printers/plugin.h (+33/-0) modules/Ubuntu/Components/Extras/Printers/printer/printer.cpp (+315/-0) modules/Ubuntu/Components/Extras/Printers/printer/printer.h (+96/-0) modules/Ubuntu/Components/Extras/Printers/printer/printerjob.cpp (+514/-0) modules/Ubuntu/Components/Extras/Printers/printer/printerjob.h (+174/-0) modules/Ubuntu/Components/Extras/Printers/printer/printersignalhandler.cpp (+69/-0) modules/Ubuntu/Components/Extras/Printers/printer/printersignalhandler.h (+55/-0) modules/Ubuntu/Components/Extras/Printers/printers/printers.cpp (+232/-0) modules/Ubuntu/Components/Extras/Printers/printers/printers.h (+102/-0) modules/Ubuntu/Components/Extras/Printers/printers_global.h (+23/-0) modules/Ubuntu/Components/Extras/Printers/qmldir (+2/-0) modules/Ubuntu/Components/Extras/Printers/structs.h (+97/-0) modules/Ubuntu/Components/Extras/Printers/utils.h (+110/-0) po/CMakeLists.txt (+2/-1) po/ubuntu-ui-extras.pot (+27/-3) tests/unittests/CMakeLists.txt (+2/-0) tests/unittests/Printers/CMakeLists.txt (+48/-0) tests/unittests/Printers/mockbackend.h (+382/-0) tests/unittests/Printers/tst_drivermodel.cpp (+136/-0) tests/unittests/Printers/tst_jobfilter.cpp (+55/-0) tests/unittests/Printers/tst_jobmodel.cpp (+120/-0) tests/unittests/Printers/tst_printer.cpp (+284/-0) tests/unittests/Printers/tst_printerfilter.cpp (+126/-0) tests/unittests/Printers/tst_printerjob.cpp (+318/-0) tests/unittests/Printers/tst_printermodel.cpp (+161/-0) tests/unittests/Printers/tst_printers.cpp (+233/-0) tests/unittests/Printers/tst_signalhandler.cpp (+44/-0) |
To merge this branch: | bzr merge lp:~jonas-drange/ubuntu-ui-extras/dedupe-requests |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Ubuntu Phablet Team | Pending | ||
Review via email: mp+318217@code.launchpad.net |
Commit message
guards against excessive loading of printers and drivers
Description of the change
guards against excessive loading of printers and drivers
To post a comment you must log in.
- 135. By Jonas G. Drange
-
qlist->qset
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'debian/control' | |||
2 | --- debian/control 2016-08-24 15:46:52 +0000 | |||
3 | +++ debian/control 2017-02-24 12:29:37 +0000 | |||
4 | @@ -3,8 +3,10 @@ | |||
5 | 3 | Priority: optional | 3 | Priority: optional |
6 | 4 | Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> | 4 | Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> |
7 | 5 | Build-Depends: cmake (>= 2.8.9), | 5 | Build-Depends: cmake (>= 2.8.9), |
8 | 6 | cmake-extras (>= 0.10), | ||
9 | 6 | debhelper (>= 9), | 7 | debhelper (>= 9), |
10 | 7 | gettext, | 8 | gettext, |
11 | 9 | libcups2-dev, | ||
12 | 8 | pkg-config, | 10 | pkg-config, |
13 | 9 | python:any, | 11 | python:any, |
14 | 10 | qt5-default, | 12 | qt5-default, |
15 | @@ -27,6 +29,7 @@ | |||
16 | 27 | Architecture: any | 29 | Architecture: any |
17 | 28 | Depends: ${misc:Depends}, | 30 | Depends: ${misc:Depends}, |
18 | 29 | ${shlibs:Depends}, | 31 | ${shlibs:Depends}, |
19 | 32 | libqt5printsupport5, | ||
20 | 30 | qml-module-qtquick2, | 33 | qml-module-qtquick2, |
21 | 31 | qml-module-ubuntu-components, | 34 | qml-module-ubuntu-components, |
22 | 32 | qml-module-qtquick-window2, | 35 | qml-module-qtquick-window2, |
23 | 33 | 36 | ||
24 | === modified file 'modules/Ubuntu/Components/Extras/CMakeLists.txt' | |||
25 | --- modules/Ubuntu/Components/Extras/CMakeLists.txt 2016-12-08 14:26:41 +0000 | |||
26 | +++ modules/Ubuntu/Components/Extras/CMakeLists.txt 2017-02-24 12:29:37 +0000 | |||
27 | @@ -13,4 +13,5 @@ | |||
28 | 13 | add_subdirectory(plugin) | 13 | add_subdirectory(plugin) |
29 | 14 | add_subdirectory(Example) | 14 | add_subdirectory(Example) |
30 | 15 | add_subdirectory(PhotoEditor) | 15 | add_subdirectory(PhotoEditor) |
31 | 16 | add_subdirectory(Printers) | ||
32 | 16 | add_subdirectory(TabsBar) | 17 | add_subdirectory(TabsBar) |
33 | 17 | 18 | ||
34 | === added file 'modules/Ubuntu/Components/Extras/Example/PrinterQueue.qml' | |||
35 | --- modules/Ubuntu/Components/Extras/Example/PrinterQueue.qml 1970-01-01 00:00:00 +0000 | |||
36 | +++ modules/Ubuntu/Components/Extras/Example/PrinterQueue.qml 2017-02-24 12:29:37 +0000 | |||
37 | @@ -0,0 +1,117 @@ | |||
38 | 1 | /* | ||
39 | 2 | * Copyright 2017 Canonical Ltd. | ||
40 | 3 | * | ||
41 | 4 | * This program is free software; you can redistribute it and/or modify | ||
42 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
43 | 6 | * the Free Software Foundation; version 3. | ||
44 | 7 | * | ||
45 | 8 | * This program is distributed in the hope that it will be useful, | ||
46 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
47 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
48 | 11 | * GNU Lesser General Public License for more details. | ||
49 | 12 | * | ||
50 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
51 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
52 | 15 | * | ||
53 | 16 | * Authored by Jonas G. Drange <jonas.drange@canonical.com> | ||
54 | 17 | * Andrew Hayzen <andrew.hayzen@canonical.com> | ||
55 | 18 | */ | ||
56 | 19 | |||
57 | 20 | import QtQuick 2.4 | ||
58 | 21 | import QtQuick.Layouts 1.1 | ||
59 | 22 | import Ubuntu.Components 1.3 | ||
60 | 23 | import Ubuntu.Components.ListItems 1.3 as ListItems | ||
61 | 24 | import Ubuntu.Components.Extras.Printers 0.1 | ||
62 | 25 | |||
63 | 26 | MainView { | ||
64 | 27 | width: units.gu(50) | ||
65 | 28 | height: units.gu(90) | ||
66 | 29 | |||
67 | 30 | Component { | ||
68 | 31 | id: queuePage | ||
69 | 32 | |||
70 | 33 | Page { | ||
71 | 34 | header: PageHeader { | ||
72 | 35 | title: "Queue: " + printer.name | ||
73 | 36 | flickable: queueView | ||
74 | 37 | } | ||
75 | 38 | visible: false | ||
76 | 39 | |||
77 | 40 | property var printer | ||
78 | 41 | |||
79 | 42 | ListView { | ||
80 | 43 | id: queueView | ||
81 | 44 | anchors { | ||
82 | 45 | fill: parent | ||
83 | 46 | } | ||
84 | 47 | delegate: ListItem { | ||
85 | 48 | height: modelLayout.height + (divider.visible ? divider.height : 0) | ||
86 | 49 | ListItemLayout { | ||
87 | 50 | id: modelLayout | ||
88 | 51 | title.text: displayName | ||
89 | 52 | subtitle.text: "Job: " + model.id + " State: " + model.state | ||
90 | 53 | + " Color: " + model.colorModel + " CreationTime: " | ||
91 | 54 | + model.creationTime + " PageRange: " | ||
92 | 55 | + model.printRange + " Messages: " + model.messages; | ||
93 | 56 | subtitle.wrapMode: Text.WrapAtWordBoundaryOrAnywhere | ||
94 | 57 | subtitle.maximumLineCount: 3 | ||
95 | 58 | } | ||
96 | 59 | onClicked: { | ||
97 | 60 | console.debug("Cancel:", printer.name, model.id); | ||
98 | 61 | Printers.cancelJob(printer.name, model.id); | ||
99 | 62 | } | ||
100 | 63 | } | ||
101 | 64 | model: printer.jobs | ||
102 | 65 | |||
103 | 66 | Label { | ||
104 | 67 | anchors { | ||
105 | 68 | centerIn: parent | ||
106 | 69 | } | ||
107 | 70 | text: "Empty queue" | ||
108 | 71 | visible: queueView.count === 0 | ||
109 | 72 | } | ||
110 | 73 | } | ||
111 | 74 | } | ||
112 | 75 | } | ||
113 | 76 | |||
114 | 77 | PageStack { | ||
115 | 78 | id: pageStack | ||
116 | 79 | |||
117 | 80 | Page { | ||
118 | 81 | id: printersPage | ||
119 | 82 | header: PageHeader { | ||
120 | 83 | title: "Printers" | ||
121 | 84 | flickable: printerList | ||
122 | 85 | } | ||
123 | 86 | visible: false | ||
124 | 87 | |||
125 | 88 | ListView { | ||
126 | 89 | id: printerList | ||
127 | 90 | anchors { fill: parent } | ||
128 | 91 | model: Printers.allPrintersWithPdf | ||
129 | 92 | delegate: ListItem { | ||
130 | 93 | height: modelLayout.height + (divider.visible ? divider.height : 0) | ||
131 | 94 | ListItemLayout { | ||
132 | 95 | id: modelLayout | ||
133 | 96 | title.text: displayName | ||
134 | 97 | title.font.bold: model.default | ||
135 | 98 | subtitle.text: description | ||
136 | 99 | |||
137 | 100 | Icon { | ||
138 | 101 | id: icon | ||
139 | 102 | width: height | ||
140 | 103 | height: units.gu(2.5) | ||
141 | 104 | name: "printer-symbolic" | ||
142 | 105 | SlotsLayout.position: SlotsLayout.First | ||
143 | 106 | } | ||
144 | 107 | |||
145 | 108 | ProgressionSlot {} | ||
146 | 109 | } | ||
147 | 110 | onClicked: pageStack.push(queuePage, { printer: model }) | ||
148 | 111 | } | ||
149 | 112 | } | ||
150 | 113 | } | ||
151 | 114 | |||
152 | 115 | Component.onCompleted: push(printersPage) | ||
153 | 116 | } | ||
154 | 117 | } | ||
155 | 0 | 118 | ||
156 | === added file 'modules/Ubuntu/Components/Extras/Example/Printers.qml' | |||
157 | --- modules/Ubuntu/Components/Extras/Example/Printers.qml 1970-01-01 00:00:00 +0000 | |||
158 | +++ modules/Ubuntu/Components/Extras/Example/Printers.qml 2017-02-24 12:29:37 +0000 | |||
159 | @@ -0,0 +1,603 @@ | |||
160 | 1 | /* | ||
161 | 2 | * Copyright 2017 Canonical Ltd. | ||
162 | 3 | * | ||
163 | 4 | * This program is free software; you can redistribute it and/or modify | ||
164 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
165 | 6 | * the Free Software Foundation; version 3. | ||
166 | 7 | * | ||
167 | 8 | * This program is distributed in the hope that it will be useful, | ||
168 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
169 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
170 | 11 | * GNU Lesser General Public License for more details. | ||
171 | 12 | * | ||
172 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
173 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
174 | 15 | * | ||
175 | 16 | * Authored by Jonas G. Drange <jonas.drange@canonical.com> | ||
176 | 17 | */ | ||
177 | 18 | |||
178 | 19 | import QtQuick 2.4 | ||
179 | 20 | import QtQuick.Layouts 1.1 | ||
180 | 21 | import Ubuntu.Components 1.3 | ||
181 | 22 | import Ubuntu.Components.Popups 1.3 | ||
182 | 23 | import Ubuntu.Components.ListItems 1.3 as ListItems | ||
183 | 24 | import Ubuntu.Components.Extras.Printers 0.1 | ||
184 | 25 | |||
185 | 26 | MainView { | ||
186 | 27 | width: units.gu(50) | ||
187 | 28 | height: units.gu(90) | ||
188 | 29 | |||
189 | 30 | Component { | ||
190 | 31 | id: printerPage | ||
191 | 32 | |||
192 | 33 | Page { | ||
193 | 34 | visible: false | ||
194 | 35 | property var printer | ||
195 | 36 | header: PageHeader { | ||
196 | 37 | id: printerPageHeader | ||
197 | 38 | title: printer.name | ||
198 | 39 | flickable: printerFlickable | ||
199 | 40 | } | ||
200 | 41 | |||
201 | 42 | Component { | ||
202 | 43 | id: printerPageNotYetLoaded | ||
203 | 44 | |||
204 | 45 | Item { | ||
205 | 46 | anchors.fill: parent | ||
206 | 47 | ActivityIndicator { | ||
207 | 48 | anchors.centerIn: parent | ||
208 | 49 | running: true | ||
209 | 50 | } | ||
210 | 51 | } | ||
211 | 52 | } | ||
212 | 53 | |||
213 | 54 | Component.onCompleted: { | ||
214 | 55 | printer.description; | ||
215 | 56 | } | ||
216 | 57 | |||
217 | 58 | Flickable { | ||
218 | 59 | id: printerFlickable | ||
219 | 60 | anchors.fill: parent | ||
220 | 61 | |||
221 | 62 | Loader { | ||
222 | 63 | id: printerPageBitsLoader | ||
223 | 64 | anchors.fill: parent | ||
224 | 65 | sourceComponent: printer.isLoaded ? printerPageLoaded : printerPageNotYetLoaded | ||
225 | 66 | } | ||
226 | 67 | } | ||
227 | 68 | |||
228 | 69 | Component { | ||
229 | 70 | id: printerPageLoaded | ||
230 | 71 | |||
231 | 72 | Column { | ||
232 | 73 | spacing: units.gu(2) | ||
233 | 74 | anchors { | ||
234 | 75 | top: parent.top | ||
235 | 76 | topMargin: units.gu(2) | ||
236 | 77 | left: parent.left | ||
237 | 78 | right: parent.right | ||
238 | 79 | } | ||
239 | 80 | |||
240 | 81 | ListItems.Standard { | ||
241 | 82 | anchors { | ||
242 | 83 | left: parent.left | ||
243 | 84 | right: parent.right | ||
244 | 85 | } | ||
245 | 86 | text: "Enabled" | ||
246 | 87 | |||
247 | 88 | control: Switch { | ||
248 | 89 | checked: printer.printerEnabled | ||
249 | 90 | onCheckedChanged: printer.printerEnabled = checked | ||
250 | 91 | } | ||
251 | 92 | } | ||
252 | 93 | |||
253 | 94 | ListItems.Standard { | ||
254 | 95 | anchors { | ||
255 | 96 | left: parent.left | ||
256 | 97 | right: parent.right | ||
257 | 98 | } | ||
258 | 99 | text: "Accepting jobs" | ||
259 | 100 | |||
260 | 101 | control: Switch { | ||
261 | 102 | checked: printer.acceptJobs | ||
262 | 103 | onCheckedChanged: printer.acceptJobs = checked | ||
263 | 104 | } | ||
264 | 105 | } | ||
265 | 106 | |||
266 | 107 | ListItems.Standard { | ||
267 | 108 | anchors { | ||
268 | 109 | left: parent.left | ||
269 | 110 | right: parent.right | ||
270 | 111 | } | ||
271 | 112 | text: "Jobs" | ||
272 | 113 | progression: true | ||
273 | 114 | onClicked: pageStack.push(jobPage, { printer: printer }) | ||
274 | 115 | } | ||
275 | 116 | |||
276 | 117 | Label { | ||
277 | 118 | anchors { | ||
278 | 119 | left: parent.left | ||
279 | 120 | right: parent.right | ||
280 | 121 | margins: units.gu(2) | ||
281 | 122 | } | ||
282 | 123 | text: "Description" | ||
283 | 124 | } | ||
284 | 125 | |||
285 | 126 | ListItems.SingleControl { | ||
286 | 127 | anchors { | ||
287 | 128 | left: parent.left | ||
288 | 129 | right: parent.right | ||
289 | 130 | } | ||
290 | 131 | |||
291 | 132 | control: TextField { | ||
292 | 133 | anchors { | ||
293 | 134 | margins: units.gu(1) | ||
294 | 135 | left: parent.left | ||
295 | 136 | right: parent.right | ||
296 | 137 | |||
297 | 138 | } | ||
298 | 139 | text: printer.description | ||
299 | 140 | onTextChanged: printer.description = text | ||
300 | 141 | } | ||
301 | 142 | } | ||
302 | 143 | |||
303 | 144 | |||
304 | 145 | ListItems.ValueSelector { | ||
305 | 146 | anchors { | ||
306 | 147 | left: parent.left | ||
307 | 148 | right: parent.right | ||
308 | 149 | } | ||
309 | 150 | enabled: values.length > 1 | ||
310 | 151 | text: "Duplex" | ||
311 | 152 | values: printer.supportedDuplexModes | ||
312 | 153 | onSelectedIndexChanged: printer.duplexMode = selectedIndex | ||
313 | 154 | Component.onCompleted: { | ||
314 | 155 | if (enabled) { | ||
315 | 156 | selectedIndex = printer.duplexMode | ||
316 | 157 | } | ||
317 | 158 | } | ||
318 | 159 | } | ||
319 | 160 | |||
320 | 161 | ListItems.ValueSelector { | ||
321 | 162 | anchors { | ||
322 | 163 | left: parent.left | ||
323 | 164 | right: parent.right | ||
324 | 165 | } | ||
325 | 166 | text: "Page size" | ||
326 | 167 | values: printer.supportedPageSizes | ||
327 | 168 | onSelectedIndexChanged: printer.pageSize = selectedIndex | ||
328 | 169 | Component.onCompleted: selectedIndex = printer.supportedPageSizes.indexOf(printer.pageSize) | ||
329 | 170 | } | ||
330 | 171 | |||
331 | 172 | ListItems.ValueSelector { | ||
332 | 173 | anchors { | ||
333 | 174 | left: parent.left | ||
334 | 175 | right: parent.right | ||
335 | 176 | } | ||
336 | 177 | visible: printer.supportedColorModels.length | ||
337 | 178 | text: "Color model" | ||
338 | 179 | values: printer.supportedColorModels | ||
339 | 180 | enabled: values.length > 1 | ||
340 | 181 | onSelectedIndexChanged: printer.colorModel = selectedIndex | ||
341 | 182 | Component.onCompleted: { | ||
342 | 183 | if (enabled) | ||
343 | 184 | selectedIndex = printer.colorModel | ||
344 | 185 | } | ||
345 | 186 | } | ||
346 | 187 | |||
347 | 188 | ListItems.ValueSelector { | ||
348 | 189 | anchors { | ||
349 | 190 | left: parent.left | ||
350 | 191 | right: parent.right | ||
351 | 192 | } | ||
352 | 193 | visible: printer.supportedPrintQualities.length | ||
353 | 194 | text: "Quality" | ||
354 | 195 | values: printer.supportedPrintQualities | ||
355 | 196 | enabled: values.length > 1 | ||
356 | 197 | onSelectedIndexChanged: printer.printQuality = selectedIndex | ||
357 | 198 | Component.onCompleted: { | ||
358 | 199 | if (enabled) | ||
359 | 200 | selectedIndex = printer.printQuality | ||
360 | 201 | } | ||
361 | 202 | } | ||
362 | 203 | } | ||
363 | 204 | } | ||
364 | 205 | } | ||
365 | 206 | } | ||
366 | 207 | |||
367 | 208 | Component { | ||
368 | 209 | id: jobPage | ||
369 | 210 | Page { | ||
370 | 211 | property var printer | ||
371 | 212 | header: PageHeader { | ||
372 | 213 | id: jobPageHeader | ||
373 | 214 | title: "%1 (%2 jobs)".arg(printer.name).arg(jobList.count) | ||
374 | 215 | flickable: jobList | ||
375 | 216 | } | ||
376 | 217 | |||
377 | 218 | ListView { | ||
378 | 219 | id: jobList | ||
379 | 220 | anchors.fill: parent | ||
380 | 221 | model: printer.jobs | ||
381 | 222 | delegate: ListItem { | ||
382 | 223 | height: jobLayout.height + (divider.visible ? divider.height : 0) | ||
383 | 224 | ListItemLayout { | ||
384 | 225 | id: jobLayout | ||
385 | 226 | title.text: displayName | ||
386 | 227 | |||
387 | 228 | Icon { | ||
388 | 229 | id: icon | ||
389 | 230 | width: height | ||
390 | 231 | height: units.gu(2.5) | ||
391 | 232 | name: "stock_document" | ||
392 | 233 | SlotsLayout.position: SlotsLayout.First | ||
393 | 234 | } | ||
394 | 235 | } | ||
395 | 236 | } | ||
396 | 237 | } | ||
397 | 238 | } | ||
398 | 239 | } | ||
399 | 240 | |||
400 | 241 | |||
401 | 242 | Component { | ||
402 | 243 | id: allJobsPage | ||
403 | 244 | Page { | ||
404 | 245 | header: PageHeader { | ||
405 | 246 | id: allJobsHeader | ||
406 | 247 | title: "Printer jobs" | ||
407 | 248 | flickable: jobsList | ||
408 | 249 | } | ||
409 | 250 | |||
410 | 251 | ListView { | ||
411 | 252 | id: jobsList | ||
412 | 253 | anchors.fill: parent | ||
413 | 254 | model: Printers.printJobs | ||
414 | 255 | delegate: ListItem { | ||
415 | 256 | height: jobsLayout.height + (divider.visible ? divider.height : 0) | ||
416 | 257 | ListItemLayout { | ||
417 | 258 | id: jobsLayout | ||
418 | 259 | title.text: displayName | ||
419 | 260 | |||
420 | 261 | Icon { | ||
421 | 262 | id: icon | ||
422 | 263 | width: height | ||
423 | 264 | height: units.gu(2.5) | ||
424 | 265 | name: "stock_document" | ||
425 | 266 | SlotsLayout.position: SlotsLayout.First | ||
426 | 267 | } | ||
427 | 268 | } | ||
428 | 269 | } | ||
429 | 270 | } | ||
430 | 271 | } | ||
431 | 272 | } | ||
432 | 273 | |||
433 | 274 | |||
434 | 275 | PageStack { | ||
435 | 276 | id: pageStack | ||
436 | 277 | |||
437 | 278 | Component.onCompleted: push(printersPage) | ||
438 | 279 | |||
439 | 280 | Page { | ||
440 | 281 | id: printersPage | ||
441 | 282 | header: PageHeader { | ||
442 | 283 | title: "Printers" | ||
443 | 284 | flickable: printerList | ||
444 | 285 | trailingActionBar { | ||
445 | 286 | actions: [ | ||
446 | 287 | Action { | ||
447 | 288 | iconName: "add" | ||
448 | 289 | text: "Add printer" | ||
449 | 290 | onTriggered: pageStack.push(addPrinterPageComponent) | ||
450 | 291 | }, | ||
451 | 292 | Action { | ||
452 | 293 | iconName: "document-print" | ||
453 | 294 | text: "Printer jobs" | ||
454 | 295 | onTriggered: pageStack.push(allJobsPage) | ||
455 | 296 | } | ||
456 | 297 | ] | ||
457 | 298 | } | ||
458 | 299 | } | ||
459 | 300 | visible: false | ||
460 | 301 | |||
461 | 302 | ListView { | ||
462 | 303 | id: printerList | ||
463 | 304 | anchors { fill: parent } | ||
464 | 305 | model: Printers.allPrintersWithPdf | ||
465 | 306 | delegate: ListItem { | ||
466 | 307 | height: modelLayout.height + (divider.visible ? divider.height : 0) | ||
467 | 308 | trailingActions: ListItemActions { | ||
468 | 309 | actions: [ | ||
469 | 310 | Action { | ||
470 | 311 | iconName: "delete" | ||
471 | 312 | onTriggered: { | ||
472 | 313 | if (!Printers.removePrinter(model.name)) { | ||
473 | 314 | console.error('failed to remove printer', Printers.lastMessage); | ||
474 | 315 | } | ||
475 | 316 | } | ||
476 | 317 | }, | ||
477 | 318 | Action { | ||
478 | 319 | iconName: model.default ? "starred" : "non-starred" | ||
479 | 320 | enabled: !model.default | ||
480 | 321 | onTriggered: Printers.defaultPrinterName = model.name | ||
481 | 322 | } | ||
482 | 323 | |||
483 | 324 | ] | ||
484 | 325 | } | ||
485 | 326 | ListItemLayout { | ||
486 | 327 | id: modelLayout | ||
487 | 328 | title.text: displayName | ||
488 | 329 | title.font.bold: model.default | ||
489 | 330 | |||
490 | 331 | Icon { | ||
491 | 332 | id: icon | ||
492 | 333 | width: height | ||
493 | 334 | height: units.gu(2.5) | ||
494 | 335 | name: "printer-symbolic" | ||
495 | 336 | SlotsLayout.position: SlotsLayout.First | ||
496 | 337 | } | ||
497 | 338 | |||
498 | 339 | ProgressionSlot {} | ||
499 | 340 | } | ||
500 | 341 | onClicked: pageStack.push(printerPage, { printer: model }) | ||
501 | 342 | } | ||
502 | 343 | } | ||
503 | 344 | } | ||
504 | 345 | } | ||
505 | 346 | |||
506 | 347 | Component { | ||
507 | 348 | id: addPrinterPageComponent | ||
508 | 349 | Page { | ||
509 | 350 | id: addPrinterPage | ||
510 | 351 | states: [ | ||
511 | 352 | State { | ||
512 | 353 | name: "success" | ||
513 | 354 | PropertyChanges { | ||
514 | 355 | target: okAction | ||
515 | 356 | enabled: false | ||
516 | 357 | } | ||
517 | 358 | PropertyChanges { | ||
518 | 359 | target: closeAction | ||
519 | 360 | enabled: false | ||
520 | 361 | } | ||
521 | 362 | PropertyChanges { | ||
522 | 363 | target: addPrinterCol | ||
523 | 364 | enabled: false | ||
524 | 365 | } | ||
525 | 366 | StateChangeScript { | ||
526 | 367 | script: okTimer.start() | ||
527 | 368 | } | ||
528 | 369 | }, | ||
529 | 370 | State { | ||
530 | 371 | name: "failure" | ||
531 | 372 | PropertyChanges { | ||
532 | 373 | target: errorMessageContainer | ||
533 | 374 | visible: true | ||
534 | 375 | } | ||
535 | 376 | } | ||
536 | 377 | ] | ||
537 | 378 | header: PageHeader { | ||
538 | 379 | title: "Add printer" | ||
539 | 380 | flickable: addPrinterFlickable | ||
540 | 381 | leadingActionBar.actions: [ | ||
541 | 382 | Action { | ||
542 | 383 | id: closeAction | ||
543 | 384 | iconName: "close" | ||
544 | 385 | text: "Abort" | ||
545 | 386 | onTriggered: pageStack.pop() | ||
546 | 387 | } | ||
547 | 388 | ] | ||
548 | 389 | trailingActionBar { | ||
549 | 390 | actions: [ | ||
550 | 391 | Action { | ||
551 | 392 | id: okAction | ||
552 | 393 | iconName: "ok" | ||
553 | 394 | text: "Complete" | ||
554 | 395 | onTriggered: { | ||
555 | 396 | var ret; | ||
556 | 397 | if (driverSelector.selectedIndex == 0) { | ||
557 | 398 | ret = Printers.addPrinter( | ||
558 | 399 | printerName.text, | ||
559 | 400 | driversView.selectedDriver, | ||
560 | 401 | printerUri.text, | ||
561 | 402 | printerDescription.text, | ||
562 | 403 | printerLocation.text | ||
563 | 404 | ); | ||
564 | 405 | } else { | ||
565 | 406 | ret = Printers.addPrinterWithPpdFile( | ||
566 | 407 | printerName.text, | ||
567 | 408 | printerPpd.text, | ||
568 | 409 | printerUri.text, | ||
569 | 410 | printerDescription.text, | ||
570 | 411 | printerLocation.text | ||
571 | 412 | ); | ||
572 | 413 | } | ||
573 | 414 | if (ret) { | ||
574 | 415 | addPrinterPage.state = "success" | ||
575 | 416 | } else { | ||
576 | 417 | errorMessage.text = Printers.lastMessage; | ||
577 | 418 | addPrinterPage.state = "failure" | ||
578 | 419 | } | ||
579 | 420 | } | ||
580 | 421 | } | ||
581 | 422 | ] | ||
582 | 423 | } | ||
583 | 424 | } | ||
584 | 425 | |||
585 | 426 | Component.onCompleted: { | ||
586 | 427 | Printers.prepareToAddPrinter(); | ||
587 | 428 | } | ||
588 | 429 | |||
589 | 430 | Timer { | ||
590 | 431 | id: okTimer | ||
591 | 432 | interval: 2000 | ||
592 | 433 | onTriggered: pageStack.pop(); | ||
593 | 434 | } | ||
594 | 435 | |||
595 | 436 | Flickable { | ||
596 | 437 | id: addPrinterFlickable | ||
597 | 438 | anchors.fill: parent | ||
598 | 439 | |||
599 | 440 | Column { | ||
600 | 441 | id: addPrinterCol | ||
601 | 442 | property bool enabled: true | ||
602 | 443 | anchors { | ||
603 | 444 | left: parent.left | ||
604 | 445 | right: parent.right | ||
605 | 446 | } | ||
606 | 447 | |||
607 | 448 | Item { | ||
608 | 449 | id: errorMessageContainer | ||
609 | 450 | visible: false | ||
610 | 451 | anchors { | ||
611 | 452 | left: parent.left | ||
612 | 453 | right: parent.right | ||
613 | 454 | margins: units.gu(2) | ||
614 | 455 | } | ||
615 | 456 | height: units.gu(6) | ||
616 | 457 | Label { | ||
617 | 458 | id: errorMessage | ||
618 | 459 | anchors { | ||
619 | 460 | top: parent.top | ||
620 | 461 | topMargin: units.gu(2) | ||
621 | 462 | horizontalCenter: parent.horizontalCenter | ||
622 | 463 | } | ||
623 | 464 | } | ||
624 | 465 | |||
625 | 466 | } | ||
626 | 467 | |||
627 | 468 | ListItems.Standard { | ||
628 | 469 | text: "Device URI" | ||
629 | 470 | control: TextField { | ||
630 | 471 | id: printerUri | ||
631 | 472 | placeholderText: "ipp://server.local/my-queue" | ||
632 | 473 | } | ||
633 | 474 | enabled: parent.enabled | ||
634 | 475 | } | ||
635 | 476 | |||
636 | 477 | ListItems.ValueSelector { | ||
637 | 478 | id: driverSelector | ||
638 | 479 | anchors { | ||
639 | 480 | left: parent.left | ||
640 | 481 | right: parent.right | ||
641 | 482 | } | ||
642 | 483 | text: "Choose driver" | ||
643 | 484 | values: [ | ||
644 | 485 | "Select printer from database", | ||
645 | 486 | "Provide PPD file" | ||
646 | 487 | ] | ||
647 | 488 | enabled: parent.enabled | ||
648 | 489 | } | ||
649 | 490 | |||
650 | 491 | ListItems.Standard { | ||
651 | 492 | anchors { | ||
652 | 493 | left: parent.left | ||
653 | 494 | right: parent.right | ||
654 | 495 | } | ||
655 | 496 | text: "Filter drivers" | ||
656 | 497 | control: TextField { | ||
657 | 498 | id: driverFilter | ||
658 | 499 | onTextChanged: Printers.driverFilter = text | ||
659 | 500 | } | ||
660 | 501 | visible: driverSelector.selectedIndex == 0 | ||
661 | 502 | enabled: parent.enabled | ||
662 | 503 | } | ||
663 | 504 | |||
664 | 505 | ListView { | ||
665 | 506 | id: driversView | ||
666 | 507 | property string selectedDriver | ||
667 | 508 | property bool loading: true | ||
668 | 509 | visible: driverSelector.selectedIndex == 0 | ||
669 | 510 | model: Printers.drivers | ||
670 | 511 | anchors { left: parent.left; right: parent.right } | ||
671 | 512 | height: units.gu(30) | ||
672 | 513 | clip: true | ||
673 | 514 | enabled: parent.enabled | ||
674 | 515 | highlightFollowsCurrentItem: false | ||
675 | 516 | highlight: Rectangle { | ||
676 | 517 | z: 0 | ||
677 | 518 | y: driversView.currentItem.y | ||
678 | 519 | width: driversView.currentItem.width | ||
679 | 520 | height: driversView.currentItem.height | ||
680 | 521 | color: theme.palette.selected.background | ||
681 | 522 | } | ||
682 | 523 | delegate: ListItem { | ||
683 | 524 | height: driverLayout.height + (divider.visible ? divider.height : 0) | ||
684 | 525 | ListItemLayout { | ||
685 | 526 | id: driverLayout | ||
686 | 527 | title.text: displayName | ||
687 | 528 | subtitle.text: name | ||
688 | 529 | summary.text: deviceId | ||
689 | 530 | } | ||
690 | 531 | onClicked: { | ||
691 | 532 | driversView.selectedDriver = name | ||
692 | 533 | driversView.currentIndex = index | ||
693 | 534 | } | ||
694 | 535 | } | ||
695 | 536 | |||
696 | 537 | ActivityIndicator { | ||
697 | 538 | anchors.centerIn: parent | ||
698 | 539 | running: parent.loading | ||
699 | 540 | } | ||
700 | 541 | |||
701 | 542 | Connections { | ||
702 | 543 | target: driversView | ||
703 | 544 | onCountChanged: { | ||
704 | 545 | target = null; | ||
705 | 546 | driversView.loading = false; | ||
706 | 547 | } | ||
707 | 548 | } | ||
708 | 549 | } | ||
709 | 550 | |||
710 | 551 | ListItems.Standard { | ||
711 | 552 | text: "PPD File" | ||
712 | 553 | visible: driverSelector.selectedIndex == 1 | ||
713 | 554 | control: TextField { | ||
714 | 555 | id: printerPpd | ||
715 | 556 | placeholderText: "/usr/share/cups/foo.ppd" | ||
716 | 557 | } | ||
717 | 558 | enabled: parent.enabled | ||
718 | 559 | } | ||
719 | 560 | |||
720 | 561 | ListItems.Standard { | ||
721 | 562 | anchors { | ||
722 | 563 | left: parent.left | ||
723 | 564 | right: parent.right | ||
724 | 565 | } | ||
725 | 566 | text: "Printer name" | ||
726 | 567 | control: TextField { | ||
727 | 568 | id: printerName | ||
728 | 569 | placeholderText: "laserjet" | ||
729 | 570 | } | ||
730 | 571 | enabled: parent.enabled | ||
731 | 572 | } | ||
732 | 573 | |||
733 | 574 | ListItems.Standard { | ||
734 | 575 | anchors { | ||
735 | 576 | left: parent.left | ||
736 | 577 | right: parent.right | ||
737 | 578 | } | ||
738 | 579 | text: "Description (optional)" | ||
739 | 580 | control: TextField { | ||
740 | 581 | id: printerDescription | ||
741 | 582 | placeholderText: "HP Laserjet with Duplexer" | ||
742 | 583 | } | ||
743 | 584 | enabled: parent.enabled | ||
744 | 585 | } | ||
745 | 586 | |||
746 | 587 | ListItems.Standard { | ||
747 | 588 | anchors { | ||
748 | 589 | left: parent.left | ||
749 | 590 | right: parent.right | ||
750 | 591 | } | ||
751 | 592 | text: "Location (optional)" | ||
752 | 593 | control: TextField { | ||
753 | 594 | id: printerLocation | ||
754 | 595 | placeholderText: "Lab 1" | ||
755 | 596 | } | ||
756 | 597 | enabled: parent.enabled | ||
757 | 598 | } | ||
758 | 599 | } | ||
759 | 600 | } | ||
760 | 601 | } | ||
761 | 602 | } | ||
762 | 603 | } | ||
763 | 0 | 604 | ||
764 | === added directory 'modules/Ubuntu/Components/Extras/Printers' | |||
765 | === added file 'modules/Ubuntu/Components/Extras/Printers/CMakeLists.txt' | |||
766 | --- modules/Ubuntu/Components/Extras/Printers/CMakeLists.txt 1970-01-01 00:00:00 +0000 | |||
767 | +++ modules/Ubuntu/Components/Extras/Printers/CMakeLists.txt 2017-02-24 12:29:37 +0000 | |||
768 | @@ -0,0 +1,68 @@ | |||
769 | 1 | project(UbuntuComponentsExtrasPrintersQml) | ||
770 | 2 | |||
771 | 3 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-permissive -pedantic -Wall -Wextra") | ||
772 | 4 | |||
773 | 5 | add_definitions(-DUBUNTUCOMPONENTSEXTRASPRINTERS_LIBRARY) | ||
774 | 6 | |||
775 | 7 | include(FindCups) | ||
776 | 8 | |||
777 | 9 | include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CUPS_INCLUDE_DIR}) | ||
778 | 10 | |||
779 | 11 | find_package(Qt5Gui REQUIRED) | ||
780 | 12 | find_package(Qt5PrintSupport REQUIRED) | ||
781 | 13 | find_package(Qt5Qml REQUIRED) | ||
782 | 14 | find_package(Qt5DBus REQUIRED) | ||
783 | 15 | find_package(Qt5Concurrent REQUIRED) | ||
784 | 16 | |||
785 | 17 | if(NOT CUPS_FOUND) | ||
786 | 18 | message(FATAL_ERROR "Could not find cups.") | ||
787 | 19 | endif() | ||
788 | 20 | |||
789 | 21 | qt5_add_dbus_interface( | ||
790 | 22 | GEN_SOURCES | ||
791 | 23 | ${CMAKE_CURRENT_SOURCE_DIR}/org.cups.cupsd.Notifier.xml | ||
792 | 24 | cupsdnotifier) | ||
793 | 25 | |||
794 | 26 | add_library(UbuntuComponentsExtrasPrintersQml SHARED | ||
795 | 27 | ${GEN_SOURCES} | ||
796 | 28 | backend/backend.cpp | ||
797 | 29 | backend/backend_cups.cpp | ||
798 | 30 | backend/backend_pdf.cpp | ||
799 | 31 | |||
800 | 32 | cups/ippclient.cpp | ||
801 | 33 | cups/printerdriverloader.cpp | ||
802 | 34 | cups/printerloader.cpp | ||
803 | 35 | |||
804 | 36 | models/drivermodel.cpp | ||
805 | 37 | models/jobmodel.cpp | ||
806 | 38 | models/printermodel.cpp | ||
807 | 39 | |||
808 | 40 | printer/printer.cpp | ||
809 | 41 | printer/printerjob.cpp | ||
810 | 42 | printer/printersignalhandler.cpp | ||
811 | 43 | printers/printers.cpp | ||
812 | 44 | |||
813 | 45 | enums.h | ||
814 | 46 | i18n.cpp | ||
815 | 47 | plugin.cpp | ||
816 | 48 | structs.h | ||
817 | 49 | utils.h | ||
818 | 50 | ) | ||
819 | 51 | |||
820 | 52 | target_link_libraries(UbuntuComponentsExtrasPrintersQml | ||
821 | 53 | Qt5::DBus | ||
822 | 54 | Qt5::Gui | ||
823 | 55 | Qt5::PrintSupport | ||
824 | 56 | Qt5::Qml | ||
825 | 57 | Qt5::Concurrent | ||
826 | 58 | ${CUPS_LIBRARIES} | ||
827 | 59 | ) | ||
828 | 60 | |||
829 | 61 | find_package(QmlPlugins) | ||
830 | 62 | |||
831 | 63 | macro(add_plugin PLUGIN VERSION PATH) | ||
832 | 64 | export_qmlfiles(${PLUGIN} ${PATH} DESTINATION ${QT_IMPORTS_DIR} ${ARGN}) | ||
833 | 65 | export_qmlplugin(${PLUGIN} ${VERSION} ${PATH} DESTINATION ${QT_IMPORTS_DIR} ${ARGN}) | ||
834 | 66 | endmacro() | ||
835 | 67 | |||
836 | 68 | add_plugin(Ubuntu.Components.Extras.Printers 0.1 Ubuntu/Components/Extras/Printers TARGETS UbuntuComponentsExtrasPrintersQml) | ||
837 | 0 | 69 | ||
838 | === added directory 'modules/Ubuntu/Components/Extras/Printers/backend' | |||
839 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend.cpp' | |||
840 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend.cpp 1970-01-01 00:00:00 +0000 | |||
841 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend.cpp 2017-02-24 12:29:37 +0000 | |||
842 | @@ -0,0 +1,272 @@ | |||
843 | 1 | /* | ||
844 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
845 | 3 | * | ||
846 | 4 | * This program is free software; you can redistribute it and/or modify | ||
847 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
848 | 6 | * the Free Software Foundation; version 3. | ||
849 | 7 | * | ||
850 | 8 | * This program is distributed in the hope that it will be useful, | ||
851 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
852 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
853 | 11 | * GNU Lesser General Public License for more details. | ||
854 | 12 | * | ||
855 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
856 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
857 | 15 | */ | ||
858 | 16 | |||
859 | 17 | #include "backend/backend.h" | ||
860 | 18 | |||
861 | 19 | PrinterBackend::PrinterBackend(const QString &printerName, QObject *parent) | ||
862 | 20 | : QObject(parent) | ||
863 | 21 | , m_printerName(printerName) | ||
864 | 22 | , m_type(PrinterEnum::PrinterType::ProxyType) | ||
865 | 23 | { | ||
866 | 24 | } | ||
867 | 25 | |||
868 | 26 | PrinterBackend::~PrinterBackend() | ||
869 | 27 | { | ||
870 | 28 | } | ||
871 | 29 | |||
872 | 30 | bool PrinterBackend::holdsDefinition() const | ||
873 | 31 | { | ||
874 | 32 | return false; | ||
875 | 33 | } | ||
876 | 34 | |||
877 | 35 | QString PrinterBackend::printerAdd(const QString &name, | ||
878 | 36 | const QString &uri, | ||
879 | 37 | const QString &ppdFile, | ||
880 | 38 | const QString &info, | ||
881 | 39 | const QString &location) | ||
882 | 40 | { | ||
883 | 41 | Q_UNUSED(name); | ||
884 | 42 | Q_UNUSED(uri); | ||
885 | 43 | Q_UNUSED(ppdFile); | ||
886 | 44 | Q_UNUSED(info); | ||
887 | 45 | Q_UNUSED(location); | ||
888 | 46 | return QString(); | ||
889 | 47 | } | ||
890 | 48 | |||
891 | 49 | QString PrinterBackend::printerAddWithPpd(const QString &name, | ||
892 | 50 | const QString &uri, | ||
893 | 51 | const QString &ppdFileName, | ||
894 | 52 | const QString &info, | ||
895 | 53 | const QString &location) | ||
896 | 54 | { | ||
897 | 55 | Q_UNUSED(name); | ||
898 | 56 | Q_UNUSED(uri); | ||
899 | 57 | Q_UNUSED(ppdFileName); | ||
900 | 58 | Q_UNUSED(info); | ||
901 | 59 | Q_UNUSED(location); | ||
902 | 60 | return QString(); | ||
903 | 61 | } | ||
904 | 62 | |||
905 | 63 | QString PrinterBackend::printerDelete(const QString &name) | ||
906 | 64 | { | ||
907 | 65 | Q_UNUSED(name); | ||
908 | 66 | return QString(); | ||
909 | 67 | } | ||
910 | 68 | |||
911 | 69 | QString PrinterBackend::printerSetDefault(const QString &name) | ||
912 | 70 | { | ||
913 | 71 | Q_UNUSED(name); | ||
914 | 72 | return QString(); | ||
915 | 73 | } | ||
916 | 74 | |||
917 | 75 | QString PrinterBackend::printerSetEnabled(const QString &name, | ||
918 | 76 | const bool enabled) | ||
919 | 77 | { | ||
920 | 78 | Q_UNUSED(name); | ||
921 | 79 | Q_UNUSED(enabled); | ||
922 | 80 | return QString(); | ||
923 | 81 | } | ||
924 | 82 | |||
925 | 83 | QString PrinterBackend::printerSetAcceptJobs( | ||
926 | 84 | const QString &name, | ||
927 | 85 | const bool enabled, | ||
928 | 86 | const QString &reason) | ||
929 | 87 | { | ||
930 | 88 | Q_UNUSED(name); | ||
931 | 89 | Q_UNUSED(enabled); | ||
932 | 90 | Q_UNUSED(reason); | ||
933 | 91 | return QString(); | ||
934 | 92 | } | ||
935 | 93 | |||
936 | 94 | QString PrinterBackend::printerSetInfo(const QString &name, | ||
937 | 95 | const QString &info) | ||
938 | 96 | { | ||
939 | 97 | Q_UNUSED(name); | ||
940 | 98 | Q_UNUSED(info); | ||
941 | 99 | return QString(); | ||
942 | 100 | } | ||
943 | 101 | |||
944 | 102 | QString PrinterBackend::printerAddOption(const QString &name, | ||
945 | 103 | const QString &option, | ||
946 | 104 | const QStringList &values) | ||
947 | 105 | { | ||
948 | 106 | Q_UNUSED(name); | ||
949 | 107 | Q_UNUSED(option); | ||
950 | 108 | Q_UNUSED(values); | ||
951 | 109 | return QString(); | ||
952 | 110 | } | ||
953 | 111 | |||
954 | 112 | QVariant PrinterBackend::printerGetOption(const QString &name, | ||
955 | 113 | const QString &option) const | ||
956 | 114 | { | ||
957 | 115 | Q_UNUSED(name); | ||
958 | 116 | Q_UNUSED(option); | ||
959 | 117 | return QVariant(); | ||
960 | 118 | } | ||
961 | 119 | |||
962 | 120 | QMap<QString, QVariant> PrinterBackend::printerGetOptions( | ||
963 | 121 | const QString &name, const QStringList &options) const | ||
964 | 122 | { | ||
965 | 123 | Q_UNUSED(name); | ||
966 | 124 | Q_UNUSED(options); | ||
967 | 125 | return QMap<QString, QVariant>(); | ||
968 | 126 | } | ||
969 | 127 | |||
970 | 128 | cups_dest_t* PrinterBackend::makeDest(const QString &name, | ||
971 | 129 | const PrinterJob *options) | ||
972 | 130 | { | ||
973 | 131 | Q_UNUSED(name); | ||
974 | 132 | Q_UNUSED(options); | ||
975 | 133 | return Q_NULLPTR; | ||
976 | 134 | } | ||
977 | 135 | |||
978 | 136 | void PrinterBackend::cancelJob(const QString &name, const int jobId) | ||
979 | 137 | { | ||
980 | 138 | Q_UNUSED(jobId); | ||
981 | 139 | Q_UNUSED(name); | ||
982 | 140 | } | ||
983 | 141 | |||
984 | 142 | int PrinterBackend::printFileToDest(const QString &filepath, | ||
985 | 143 | const QString &title, | ||
986 | 144 | const cups_dest_t *dest) | ||
987 | 145 | { | ||
988 | 146 | Q_UNUSED(filepath); | ||
989 | 147 | Q_UNUSED(title); | ||
990 | 148 | Q_UNUSED(dest); | ||
991 | 149 | return -1; | ||
992 | 150 | } | ||
993 | 151 | |||
994 | 152 | QList<QSharedPointer<PrinterJob>> PrinterBackend::printerGetJobs() | ||
995 | 153 | { | ||
996 | 154 | return QList<QSharedPointer<PrinterJob>>{}; | ||
997 | 155 | } | ||
998 | 156 | |||
999 | 157 | QMap<QString, QVariant> PrinterBackend::printerGetJobAttributes( | ||
1000 | 158 | const QString &name, const int jobId) | ||
1001 | 159 | { | ||
1002 | 160 | Q_UNUSED(name); | ||
1003 | 161 | Q_UNUSED(jobId); | ||
1004 | 162 | return QMap<QString, QVariant>(); | ||
1005 | 163 | } | ||
1006 | 164 | |||
1007 | 165 | QString PrinterBackend::printerName() const | ||
1008 | 166 | { | ||
1009 | 167 | return m_printerName; | ||
1010 | 168 | } | ||
1011 | 169 | |||
1012 | 170 | QString PrinterBackend::description() const | ||
1013 | 171 | { | ||
1014 | 172 | return QString(); | ||
1015 | 173 | } | ||
1016 | 174 | |||
1017 | 175 | QString PrinterBackend::location() const | ||
1018 | 176 | { | ||
1019 | 177 | return QString(); | ||
1020 | 178 | } | ||
1021 | 179 | |||
1022 | 180 | QString PrinterBackend::makeAndModel() const | ||
1023 | 181 | { | ||
1024 | 182 | return QString(); | ||
1025 | 183 | } | ||
1026 | 184 | |||
1027 | 185 | PrinterEnum::State PrinterBackend::state() const | ||
1028 | 186 | { | ||
1029 | 187 | return PrinterEnum::State::IdleState; | ||
1030 | 188 | } | ||
1031 | 189 | |||
1032 | 190 | QList<QPageSize> PrinterBackend::supportedPageSizes() const | ||
1033 | 191 | { | ||
1034 | 192 | return QList<QPageSize>(); | ||
1035 | 193 | } | ||
1036 | 194 | |||
1037 | 195 | QPageSize PrinterBackend::defaultPageSize() const | ||
1038 | 196 | { | ||
1039 | 197 | return QPageSize(); | ||
1040 | 198 | } | ||
1041 | 199 | |||
1042 | 200 | bool PrinterBackend::supportsCustomPageSizes() const | ||
1043 | 201 | { | ||
1044 | 202 | return false; | ||
1045 | 203 | } | ||
1046 | 204 | |||
1047 | 205 | QPageSize PrinterBackend::minimumPhysicalPageSize() const | ||
1048 | 206 | { | ||
1049 | 207 | return QPageSize(); | ||
1050 | 208 | } | ||
1051 | 209 | |||
1052 | 210 | QPageSize PrinterBackend::maximumPhysicalPageSize() const | ||
1053 | 211 | { | ||
1054 | 212 | return QPageSize(); | ||
1055 | 213 | } | ||
1056 | 214 | |||
1057 | 215 | QList<int> PrinterBackend::supportedResolutions() const | ||
1058 | 216 | { | ||
1059 | 217 | return QList<int>(); | ||
1060 | 218 | } | ||
1061 | 219 | |||
1062 | 220 | PrinterEnum::DuplexMode PrinterBackend::defaultDuplexMode() const | ||
1063 | 221 | { | ||
1064 | 222 | return PrinterEnum::DuplexMode::DuplexNone; | ||
1065 | 223 | } | ||
1066 | 224 | |||
1067 | 225 | QList<PrinterEnum::DuplexMode> PrinterBackend::supportedDuplexModes() const | ||
1068 | 226 | { | ||
1069 | 227 | return QList<PrinterEnum::DuplexMode>(); | ||
1070 | 228 | } | ||
1071 | 229 | |||
1072 | 230 | QList<QSharedPointer<Printer>> PrinterBackend::availablePrinters() | ||
1073 | 231 | { | ||
1074 | 232 | return QList<QSharedPointer<Printer>>(); | ||
1075 | 233 | } | ||
1076 | 234 | |||
1077 | 235 | QStringList PrinterBackend::availablePrinterNames() | ||
1078 | 236 | { | ||
1079 | 237 | return QStringList(); | ||
1080 | 238 | } | ||
1081 | 239 | |||
1082 | 240 | QSharedPointer<Printer> PrinterBackend::getPrinter(const QString &printerName) | ||
1083 | 241 | { | ||
1084 | 242 | Q_UNUSED(printerName); | ||
1085 | 243 | return QSharedPointer<Printer>(Q_NULLPTR); | ||
1086 | 244 | } | ||
1087 | 245 | |||
1088 | 246 | QString PrinterBackend::defaultPrinterName() | ||
1089 | 247 | { | ||
1090 | 248 | return QString(); | ||
1091 | 249 | } | ||
1092 | 250 | |||
1093 | 251 | void PrinterBackend::requestPrinterDrivers() | ||
1094 | 252 | { | ||
1095 | 253 | } | ||
1096 | 254 | |||
1097 | 255 | void PrinterBackend::requestPrinter(const QString &printerName) | ||
1098 | 256 | { | ||
1099 | 257 | Q_UNUSED(printerName); | ||
1100 | 258 | } | ||
1101 | 259 | |||
1102 | 260 | PrinterEnum::PrinterType PrinterBackend::type() const | ||
1103 | 261 | { | ||
1104 | 262 | return m_type; | ||
1105 | 263 | } | ||
1106 | 264 | |||
1107 | 265 | void PrinterBackend::setPrinterNameInternal(const QString &printerName) | ||
1108 | 266 | { | ||
1109 | 267 | m_printerName = printerName; | ||
1110 | 268 | } | ||
1111 | 269 | |||
1112 | 270 | void PrinterBackend::refresh() | ||
1113 | 271 | { | ||
1114 | 272 | } | ||
1115 | 0 | 273 | ||
1116 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend.h' | |||
1117 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend.h 1970-01-01 00:00:00 +0000 | |||
1118 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend.h 2017-02-24 12:29:37 +0000 | |||
1119 | @@ -0,0 +1,201 @@ | |||
1120 | 1 | /* | ||
1121 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
1122 | 3 | * | ||
1123 | 4 | * This program is free software; you can redistribute it and/or modify | ||
1124 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
1125 | 6 | * the Free Software Foundation; version 3. | ||
1126 | 7 | * | ||
1127 | 8 | * This program is distributed in the hope that it will be useful, | ||
1128 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1129 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1130 | 11 | * GNU Lesser General Public License for more details. | ||
1131 | 12 | * | ||
1132 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
1133 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1134 | 15 | */ | ||
1135 | 16 | |||
1136 | 17 | #ifndef USC_PRINTERS_BACKEND_H | ||
1137 | 18 | #define USC_PRINTERS_BACKEND_H | ||
1138 | 19 | |||
1139 | 20 | #include "printer/printer.h" | ||
1140 | 21 | #include "printer/printerjob.h" | ||
1141 | 22 | |||
1142 | 23 | // TODO: remove cups specific things from this API | ||
1143 | 24 | #include <cups/cups.h> | ||
1144 | 25 | |||
1145 | 26 | #include <QObject> | ||
1146 | 27 | #include <QPageSize> | ||
1147 | 28 | #include <QList> | ||
1148 | 29 | #include <QString> | ||
1149 | 30 | #include <QStringList> | ||
1150 | 31 | |||
1151 | 32 | class Printer; | ||
1152 | 33 | class PrinterJob; | ||
1153 | 34 | class PRINTERS_DECL_EXPORT PrinterBackend : public QObject | ||
1154 | 35 | { | ||
1155 | 36 | Q_OBJECT | ||
1156 | 37 | public: | ||
1157 | 38 | explicit PrinterBackend(QObject *parent = Q_NULLPTR); | ||
1158 | 39 | explicit PrinterBackend(const QString &printerName, | ||
1159 | 40 | QObject *parent = Q_NULLPTR); | ||
1160 | 41 | virtual ~PrinterBackend(); | ||
1161 | 42 | |||
1162 | 43 | virtual bool holdsDefinition() const; | ||
1163 | 44 | |||
1164 | 45 | // Add a printer using an already existing ppd. | ||
1165 | 46 | virtual QString printerAdd(const QString &name, | ||
1166 | 47 | const QString &uri, | ||
1167 | 48 | const QString &ppdFile, | ||
1168 | 49 | const QString &info, | ||
1169 | 50 | const QString &location); | ||
1170 | 51 | |||
1171 | 52 | // Add a printer and provide a ppd file. | ||
1172 | 53 | virtual QString printerAddWithPpd(const QString &name, | ||
1173 | 54 | const QString &uri, | ||
1174 | 55 | const QString &ppdFileName, | ||
1175 | 56 | const QString &info, | ||
1176 | 57 | const QString &location); | ||
1177 | 58 | virtual QString printerDelete(const QString &name); | ||
1178 | 59 | virtual QString printerSetDefault(const QString &name); | ||
1179 | 60 | virtual QString printerSetEnabled(const QString &name, | ||
1180 | 61 | const bool enabled); | ||
1181 | 62 | virtual QString printerSetAcceptJobs( | ||
1182 | 63 | const QString &name, | ||
1183 | 64 | const bool accept, | ||
1184 | 65 | const QString &reason = QString::null); | ||
1185 | 66 | virtual QString printerSetInfo(const QString &name, | ||
1186 | 67 | const QString &info); | ||
1187 | 68 | virtual QString printerAddOption(const QString &name, | ||
1188 | 69 | const QString &option, | ||
1189 | 70 | const QStringList &values); | ||
1190 | 71 | |||
1191 | 72 | virtual QVariant printerGetOption(const QString &name, | ||
1192 | 73 | const QString &option) const; | ||
1193 | 74 | virtual QMap<QString, QVariant> printerGetOptions( | ||
1194 | 75 | const QString &name, const QStringList &options) const; | ||
1195 | 76 | virtual cups_dest_t* makeDest(const QString &name, | ||
1196 | 77 | const PrinterJob *options); | ||
1197 | 78 | |||
1198 | 79 | virtual void cancelJob(const QString &name, const int jobId); | ||
1199 | 80 | virtual int printFileToDest(const QString &filepath, | ||
1200 | 81 | const QString &title, | ||
1201 | 82 | const cups_dest_t *dest); | ||
1202 | 83 | virtual QList<QSharedPointer<PrinterJob>> printerGetJobs(); | ||
1203 | 84 | virtual QMap<QString, QVariant> printerGetJobAttributes( | ||
1204 | 85 | const QString &name, const int jobId); | ||
1205 | 86 | |||
1206 | 87 | virtual QString printerName() const; | ||
1207 | 88 | virtual QString description() const; | ||
1208 | 89 | virtual QString location() const; | ||
1209 | 90 | virtual QString makeAndModel() const; | ||
1210 | 91 | |||
1211 | 92 | virtual PrinterEnum::State state() const; | ||
1212 | 93 | virtual QList<QPageSize> supportedPageSizes() const; | ||
1213 | 94 | virtual QPageSize defaultPageSize() const; | ||
1214 | 95 | virtual bool supportsCustomPageSizes() const; | ||
1215 | 96 | |||
1216 | 97 | virtual QPageSize minimumPhysicalPageSize() const; | ||
1217 | 98 | virtual QPageSize maximumPhysicalPageSize() const; | ||
1218 | 99 | virtual QList<int> supportedResolutions() const; | ||
1219 | 100 | virtual PrinterEnum::DuplexMode defaultDuplexMode() const; | ||
1220 | 101 | virtual QList<PrinterEnum::DuplexMode> supportedDuplexModes() const; | ||
1221 | 102 | |||
1222 | 103 | virtual QList<QSharedPointer<Printer>> availablePrinters(); | ||
1223 | 104 | virtual QStringList availablePrinterNames(); | ||
1224 | 105 | virtual QSharedPointer<Printer> getPrinter(const QString &printerName); | ||
1225 | 106 | virtual QString defaultPrinterName(); | ||
1226 | 107 | |||
1227 | 108 | virtual void requestPrinterDrivers(); | ||
1228 | 109 | virtual void requestPrinter(const QString &printerName); | ||
1229 | 110 | |||
1230 | 111 | virtual PrinterEnum::PrinterType type() const; | ||
1231 | 112 | |||
1232 | 113 | virtual void setPrinterNameInternal(const QString &printerName); | ||
1233 | 114 | |||
1234 | 115 | public Q_SLOTS: | ||
1235 | 116 | virtual void refresh(); | ||
1236 | 117 | |||
1237 | 118 | Q_SIGNALS: | ||
1238 | 119 | void printerDriversLoaded(const QList<PrinterDriver> &drivers); | ||
1239 | 120 | void printerDriversFailedToLoad(const QString &errorMessage); | ||
1240 | 121 | |||
1241 | 122 | void printerLoaded(QSharedPointer<Printer> printers); | ||
1242 | 123 | |||
1243 | 124 | void jobCompleted( | ||
1244 | 125 | const QString &text, | ||
1245 | 126 | const QString &printerUri, | ||
1246 | 127 | const QString &printerName, | ||
1247 | 128 | uint printerState, | ||
1248 | 129 | const QString &printerStateReason, | ||
1249 | 130 | bool acceptingJobs, | ||
1250 | 131 | uint jobId, | ||
1251 | 132 | uint jobState, | ||
1252 | 133 | const QString &jobStateReason, | ||
1253 | 134 | const QString &job_name, | ||
1254 | 135 | uint jobImpressionsCompleted | ||
1255 | 136 | ); | ||
1256 | 137 | void jobCreated( | ||
1257 | 138 | const QString &text, | ||
1258 | 139 | const QString &printerUri, | ||
1259 | 140 | const QString &printerName, | ||
1260 | 141 | uint printerState, | ||
1261 | 142 | const QString &printerStateReason, | ||
1262 | 143 | bool acceptingJobs, | ||
1263 | 144 | uint jobId, | ||
1264 | 145 | uint jobState, | ||
1265 | 146 | const QString &jobStateReason, | ||
1266 | 147 | const QString &job_name, | ||
1267 | 148 | uint jobImpressionsCompleted | ||
1268 | 149 | ); | ||
1269 | 150 | void jobState( | ||
1270 | 151 | const QString &text, | ||
1271 | 152 | const QString &printerUri, | ||
1272 | 153 | const QString &printerName, | ||
1273 | 154 | uint printerState, | ||
1274 | 155 | const QString &printerStateReason, | ||
1275 | 156 | bool acceptingJobs, | ||
1276 | 157 | uint jobId, | ||
1277 | 158 | uint jobState, | ||
1278 | 159 | const QString &jobStateReason, | ||
1279 | 160 | const QString &job_name, | ||
1280 | 161 | uint jobImpressionsCompleted | ||
1281 | 162 | ); | ||
1282 | 163 | void printerAdded( | ||
1283 | 164 | const QString &text, | ||
1284 | 165 | const QString &printerUri, | ||
1285 | 166 | const QString &printerName, | ||
1286 | 167 | uint printerState, | ||
1287 | 168 | const QString &printerStateReason, | ||
1288 | 169 | bool acceptingJobs | ||
1289 | 170 | ); | ||
1290 | 171 | void printerDeleted( | ||
1291 | 172 | const QString &text, | ||
1292 | 173 | const QString &printerUri, | ||
1293 | 174 | const QString &printerName, | ||
1294 | 175 | uint printerState, | ||
1295 | 176 | const QString &printerStateReason, | ||
1296 | 177 | bool acceptingJobs | ||
1297 | 178 | ); | ||
1298 | 179 | void printerModified( | ||
1299 | 180 | const QString &text, | ||
1300 | 181 | const QString &printerUri, | ||
1301 | 182 | const QString &printerName, | ||
1302 | 183 | uint printerState, | ||
1303 | 184 | const QString &printerStateReason, | ||
1304 | 185 | bool acceptingJobs | ||
1305 | 186 | ); | ||
1306 | 187 | void printerStateChanged( | ||
1307 | 188 | const QString &text, | ||
1308 | 189 | const QString &printerUri, | ||
1309 | 190 | const QString &printerName, | ||
1310 | 191 | uint printerState, | ||
1311 | 192 | const QString &printerStateReason, | ||
1312 | 193 | bool acceptingJobs | ||
1313 | 194 | ); | ||
1314 | 195 | |||
1315 | 196 | protected: | ||
1316 | 197 | QString m_printerName; | ||
1317 | 198 | PrinterEnum::PrinterType m_type; | ||
1318 | 199 | }; | ||
1319 | 200 | |||
1320 | 201 | #endif // USC_PRINTERS_BACKEND_H | ||
1321 | 0 | 202 | ||
1322 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.cpp' | |||
1323 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.cpp 1970-01-01 00:00:00 +0000 | |||
1324 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.cpp 2017-02-24 12:29:37 +0000 | |||
1325 | @@ -0,0 +1,722 @@ | |||
1326 | 1 | /* | ||
1327 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
1328 | 3 | * | ||
1329 | 4 | * This program is free software; you can redistribute it and/or modify | ||
1330 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
1331 | 6 | * the Free Software Foundation; version 3. | ||
1332 | 7 | * | ||
1333 | 8 | * This program is distributed in the hope that it will be useful, | ||
1334 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
1335 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
1336 | 11 | * GNU Lesser General Public License for more details. | ||
1337 | 12 | * | ||
1338 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
1339 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
1340 | 15 | */ | ||
1341 | 16 | |||
1342 | 17 | #include "backend/backend_cups.h" | ||
1343 | 18 | #include "cups/printerdriverloader.h" | ||
1344 | 19 | #include "cups/printerloader.h" | ||
1345 | 20 | #include "utils.h" | ||
1346 | 21 | |||
1347 | 22 | #include <cups/http.h> | ||
1348 | 23 | #include <cups/ipp.h> | ||
1349 | 24 | #include <cups/ppd.h> | ||
1350 | 25 | |||
1351 | 26 | #include <QLocale> | ||
1352 | 27 | #include <QThread> | ||
1353 | 28 | #include <QTimeZone> | ||
1354 | 29 | |||
1355 | 30 | #define __CUPS_ADD_OPTION(dest, name, value) dest->num_options = \ | ||
1356 | 31 | cupsAddOption(name, value, dest->num_options, &dest->options); | ||
1357 | 32 | |||
1358 | 33 | #define __CUPS_ATTR_EXISTS(map, attr, type) map.contains(attr) \ | ||
1359 | 34 | && map.value(attr).canConvert<type>() | ||
1360 | 35 | |||
1361 | 36 | PrinterCupsBackend::PrinterCupsBackend(IppClient *client, QPrinterInfo info, | ||
1362 | 37 | OrgCupsCupsdNotifierInterface *notifier, | ||
1363 | 38 | QObject *parent) | ||
1364 | 39 | : PrinterBackend(info.printerName(), parent) | ||
1365 | 40 | , m_knownQualityOptions({ | ||
1366 | 41 | "Quality", "PrintQuality", "HPPrintQuality", "StpQuality", | ||
1367 | 42 | "OutputMode",}) | ||
1368 | 43 | , m_client(client) | ||
1369 | 44 | , m_info(info) | ||
1370 | 45 | , m_notifier(notifier) | ||
1371 | 46 | , m_cupsSubscriptionId(-1) | ||
1372 | 47 | { | ||
1373 | 48 | m_type = PrinterEnum::PrinterType::CupsType; | ||
1374 | 49 | connect(m_notifier, SIGNAL(JobCompleted(const QString&, const QString&, | ||
1375 | 50 | const QString&, uint, | ||
1376 | 51 | const QString&, bool, uint, uint, | ||
1377 | 52 | const QString&, const QString&, uint)), | ||
1378 | 53 | this, SIGNAL(jobCompleted(const QString&, const QString&, | ||
1379 | 54 | const QString&, uint, const QString&, | ||
1380 | 55 | bool, uint, uint, const QString&, | ||
1381 | 56 | const QString&, uint))); | ||
1382 | 57 | connect(m_notifier, SIGNAL(JobCreated(const QString&, const QString&, | ||
1383 | 58 | const QString&, uint, const QString&, | ||
1384 | 59 | bool, uint, uint, const QString&, | ||
1385 | 60 | const QString&, uint)), | ||
1386 | 61 | this, SIGNAL(jobCreated(const QString&, const QString&, | ||
1387 | 62 | const QString&, uint, const QString&, bool, | ||
1388 | 63 | uint, uint, const QString&, const QString&, | ||
1389 | 64 | uint))); | ||
1390 | 65 | connect(m_notifier, SIGNAL(JobState(const QString&, const QString&, | ||
1391 | 66 | const QString&, uint, const QString&, | ||
1392 | 67 | bool, uint, uint, const QString&, | ||
1393 | 68 | const QString&, uint)), | ||
1394 | 69 | this, SIGNAL(jobState(const QString&, const QString&, | ||
1395 | 70 | const QString&, uint, const QString&, bool, | ||
1396 | 71 | uint, uint, const QString&, const QString&, | ||
1397 | 72 | uint))); | ||
1398 | 73 | connect(m_notifier, SIGNAL(PrinterAdded(const QString&, const QString&, | ||
1399 | 74 | const QString&, uint, | ||
1400 | 75 | const QString&, bool)), | ||
1401 | 76 | this, SIGNAL(printerAdded(const QString&, const QString&, | ||
1402 | 77 | const QString&, uint, | ||
1403 | 78 | const QString&, bool))); | ||
1404 | 79 | connect(m_notifier, SIGNAL(PrinterDeleted(const QString&, const QString&, | ||
1405 | 80 | const QString&, uint, | ||
1406 | 81 | const QString&, bool)), | ||
1407 | 82 | this, SIGNAL(printerDeleted(const QString&, const QString&, | ||
1408 | 83 | const QString&, uint, | ||
1409 | 84 | const QString&, bool))); | ||
1410 | 85 | connect(m_notifier, SIGNAL(PrinterModified(const QString&, const QString&, | ||
1411 | 86 | const QString&, uint, | ||
1412 | 87 | const QString&, bool)), | ||
1413 | 88 | this, SIGNAL(printerModified(const QString&, const QString&, | ||
1414 | 89 | const QString&, uint, | ||
1415 | 90 | const QString&, bool))); | ||
1416 | 91 | connect(m_notifier, SIGNAL(PrinterStateChanged(const QString&, | ||
1417 | 92 | const QString&, | ||
1418 | 93 | const QString&, uint, | ||
1419 | 94 | const QString&, bool)), | ||
1420 | 95 | this, SIGNAL(printerStateChanged(const QString&, const QString&, | ||
1421 | 96 | const QString&, uint, | ||
1422 | 97 | const QString&, bool))); | ||
1423 | 98 | |||
1424 | 99 | } | ||
1425 | 100 | |||
1426 | 101 | PrinterCupsBackend::~PrinterCupsBackend() | ||
1427 | 102 | { | ||
1428 | 103 | Q_FOREACH(auto dest, m_dests) { | ||
1429 | 104 | if (dest) | ||
1430 | 105 | cupsFreeDests(1, dest); | ||
1431 | 106 | } | ||
1432 | 107 | Q_FOREACH(auto ppd, m_ppds) { | ||
1433 | 108 | if (ppd) | ||
1434 | 109 | ppdClose(ppd); | ||
1435 | 110 | } | ||
1436 | 111 | |||
1437 | 112 | cancelSubscription(); | ||
1438 | 113 | Q_EMIT cancelWorkers(); | ||
1439 | 114 | } | ||
1440 | 115 | |||
1441 | 116 | QString PrinterCupsBackend::printerAdd(const QString &name, | ||
1442 | 117 | const QString &uri, | ||
1443 | 118 | const QString &ppdFile, | ||
1444 | 119 | const QString &info, | ||
1445 | 120 | const QString &location) | ||
1446 | 121 | { | ||
1447 | 122 | if (!m_client->printerAdd(name, uri, ppdFile, info, location)) { | ||
1448 | 123 | return m_client->getLastError(); | ||
1449 | 124 | } | ||
1450 | 125 | return QString(); | ||
1451 | 126 | } | ||
1452 | 127 | |||
1453 | 128 | QString PrinterCupsBackend::printerAddWithPpd(const QString &name, | ||
1454 | 129 | const QString &uri, | ||
1455 | 130 | const QString &ppdFileName, | ||
1456 | 131 | const QString &info, | ||
1457 | 132 | const QString &location) | ||
1458 | 133 | { | ||
1459 | 134 | if (!m_client->printerAddWithPpdFile(name, uri, ppdFileName, info, location)) { | ||
1460 | 135 | return m_client->getLastError(); | ||
1461 | 136 | } | ||
1462 | 137 | return QString(); | ||
1463 | 138 | } | ||
1464 | 139 | |||
1465 | 140 | bool PrinterCupsBackend::holdsDefinition() const | ||
1466 | 141 | { | ||
1467 | 142 | return !m_info.isNull(); | ||
1468 | 143 | } | ||
1469 | 144 | |||
1470 | 145 | QString PrinterCupsBackend::printerDelete(const QString &name) | ||
1471 | 146 | { | ||
1472 | 147 | if (!m_client->printerDelete(name)) { | ||
1473 | 148 | return m_client->getLastError(); | ||
1474 | 149 | } | ||
1475 | 150 | return QString(); | ||
1476 | 151 | } | ||
1477 | 152 | |||
1478 | 153 | QString PrinterCupsBackend::printerSetDefault(const QString &name) | ||
1479 | 154 | { | ||
1480 | 155 | if (!m_client->printerSetDefault(name)) { | ||
1481 | 156 | return m_client->getLastError(); | ||
1482 | 157 | } | ||
1483 | 158 | return QString(); | ||
1484 | 159 | } | ||
1485 | 160 | |||
1486 | 161 | QString PrinterCupsBackend::printerSetEnabled(const QString &name, | ||
1487 | 162 | const bool enabled) | ||
1488 | 163 | { | ||
1489 | 164 | if (!m_client->printerSetEnabled(name, enabled)) { | ||
1490 | 165 | return m_client->getLastError(); | ||
1491 | 166 | } | ||
1492 | 167 | return QString(); | ||
1493 | 168 | } | ||
1494 | 169 | |||
1495 | 170 | QString PrinterCupsBackend::printerSetAcceptJobs( | ||
1496 | 171 | const QString &name, | ||
1497 | 172 | const bool accept, | ||
1498 | 173 | const QString &reason) | ||
1499 | 174 | { | ||
1500 | 175 | if (!m_client->printerSetAcceptJobs(name, accept, reason)) { | ||
1501 | 176 | return m_client->getLastError(); | ||
1502 | 177 | } | ||
1503 | 178 | return QString(); | ||
1504 | 179 | } | ||
1505 | 180 | |||
1506 | 181 | QString PrinterCupsBackend::printerSetInfo(const QString &name, | ||
1507 | 182 | const QString &info) | ||
1508 | 183 | { | ||
1509 | 184 | if (!m_client->printerClassSetInfo(name, info)) { | ||
1510 | 185 | return m_client->getLastError(); | ||
1511 | 186 | } | ||
1512 | 187 | return QString(); | ||
1513 | 188 | } | ||
1514 | 189 | |||
1515 | 190 | QString PrinterCupsBackend::printerAddOption(const QString &name, | ||
1516 | 191 | const QString &option, | ||
1517 | 192 | const QStringList &values) | ||
1518 | 193 | { | ||
1519 | 194 | if (!m_client->printerClassSetOption(name, option, values)) { | ||
1520 | 195 | return m_client->getLastError(); | ||
1521 | 196 | } | ||
1522 | 197 | |||
1523 | 198 | return QString(); | ||
1524 | 199 | } | ||
1525 | 200 | |||
1526 | 201 | QVariant PrinterCupsBackend::printerGetOption(const QString &name, | ||
1527 | 202 | const QString &option) const | ||
1528 | 203 | { | ||
1529 | 204 | auto res = printerGetOptions(name, QStringList({option})); | ||
1530 | 205 | return res[option]; | ||
1531 | 206 | } | ||
1532 | 207 | |||
1533 | 208 | QMap<QString, QVariant> PrinterCupsBackend::printerGetOptions( | ||
1534 | 209 | const QString &name, const QStringList &options) const | ||
1535 | 210 | { | ||
1536 | 211 | QMap<QString, QVariant> ret; | ||
1537 | 212 | |||
1538 | 213 | cups_dest_t *dest = getDest(name); | ||
1539 | 214 | ppd_file_t* ppd = getPpd(name); | ||
1540 | 215 | |||
1541 | 216 | if (!dest || !ppd) { | ||
1542 | 217 | return ret; | ||
1543 | 218 | } | ||
1544 | 219 | |||
1545 | 220 | Q_FOREACH(const QString &option, options) { | ||
1546 | 221 | if (option == QStringLiteral("DefaultColorModel")) { | ||
1547 | 222 | ColorModel model; | ||
1548 | 223 | ppd_option_t *ppdColorModel = ppdFindOption(ppd, "ColorModel"); | ||
1549 | 224 | if (ppdColorModel) { | ||
1550 | 225 | ppd_choice_t* def = ppdFindChoice(ppdColorModel, | ||
1551 | 226 | ppdColorModel->defchoice); | ||
1552 | 227 | if (def) { | ||
1553 | 228 | model = Utils::parsePpdColorModel(def->choice, | ||
1554 | 229 | def->text, | ||
1555 | 230 | "ColorModel"); | ||
1556 | 231 | } | ||
1557 | 232 | } | ||
1558 | 233 | ret[option] = QVariant::fromValue(model); | ||
1559 | 234 | } else if (option == QStringLiteral("DefaultPrintQuality")) { | ||
1560 | 235 | PrintQuality quality; | ||
1561 | 236 | Q_FOREACH(const QString opt, m_knownQualityOptions) { | ||
1562 | 237 | ppd_option_t *ppdQuality = ppdFindOption(ppd, opt.toUtf8()); | ||
1563 | 238 | if (ppdQuality) { | ||
1564 | 239 | ppd_choice_t* def = ppdFindChoice(ppdQuality, | ||
1565 | 240 | ppdQuality->defchoice); | ||
1566 | 241 | if (def) { | ||
1567 | 242 | quality = Utils::parsePpdPrintQuality(def->choice, | ||
1568 | 243 | def->text, opt); | ||
1569 | 244 | } | ||
1570 | 245 | } | ||
1571 | 246 | } | ||
1572 | 247 | ret[option] = QVariant::fromValue(quality); | ||
1573 | 248 | } else if (option == QStringLiteral("SupportedPrintQualities")) { | ||
1574 | 249 | QList<PrintQuality> qualities; | ||
1575 | 250 | Q_FOREACH(const QString &opt, m_knownQualityOptions) { | ||
1576 | 251 | ppd_option_t *qualityOpt = ppdFindOption(ppd, opt.toUtf8()); | ||
1577 | 252 | if (qualityOpt) { | ||
1578 | 253 | for (int i = 0; i < qualityOpt->num_choices; ++i) { | ||
1579 | 254 | qualities.append( | ||
1580 | 255 | Utils::parsePpdPrintQuality( | ||
1581 | 256 | qualityOpt->choices[i].choice, | ||
1582 | 257 | qualityOpt->choices[i].text, | ||
1583 | 258 | opt | ||
1584 | 259 | ) | ||
1585 | 260 | ); | ||
1586 | 261 | } | ||
1587 | 262 | } | ||
1588 | 263 | } | ||
1589 | 264 | ret[option] = QVariant::fromValue(qualities); | ||
1590 | 265 | } else if (option == QStringLiteral("SupportedColorModels")) { | ||
1591 | 266 | QList<ColorModel> models; | ||
1592 | 267 | ppd_option_t *colorModels = ppdFindOption(ppd, "ColorModel"); | ||
1593 | 268 | if (colorModels) { | ||
1594 | 269 | for (int i = 0; i < colorModels->num_choices; ++i) { | ||
1595 | 270 | models.append( | ||
1596 | 271 | Utils::parsePpdColorModel( | ||
1597 | 272 | colorModels->choices[i].choice, | ||
1598 | 273 | colorModels->choices[i].text, | ||
1599 | 274 | QStringLiteral("ColorModel") | ||
1600 | 275 | ) | ||
1601 | 276 | ); | ||
1602 | 277 | } | ||
1603 | 278 | } | ||
1604 | 279 | ret[option] = QVariant::fromValue(models); | ||
1605 | 280 | } else if (option == QStringLiteral("AcceptJobs")) { | ||
1606 | 281 | // "true" if the destination is accepting new jobs, "false" if not. | ||
1607 | 282 | QString res = cupsGetOption("printer-is-accepting-jobs", | ||
1608 | 283 | dest->num_options, dest->options); | ||
1609 | 284 | ret[option] = res.contains("true"); | ||
1610 | 285 | } else { | ||
1611 | 286 | ppd_option_t *val = ppdFindOption(ppd, option.toUtf8()); | ||
1612 | 287 | |||
1613 | 288 | if (val) { | ||
1614 | 289 | qWarning() << "asking for" << option << "returns" << val->text; | ||
1615 | 290 | } else { | ||
1616 | 291 | qWarning() << "option" << option << "yielded no option"; | ||
1617 | 292 | } | ||
1618 | 293 | } | ||
1619 | 294 | } | ||
1620 | 295 | return ret; | ||
1621 | 296 | } | ||
1622 | 297 | |||
1623 | 298 | cups_dest_t* PrinterCupsBackend::makeDest(const QString &name, | ||
1624 | 299 | const PrinterJob *options) | ||
1625 | 300 | { | ||
1626 | 301 | cups_dest_t *dest = getDest(name); | ||
1627 | 302 | |||
1628 | 303 | if (options->collate()) { | ||
1629 | 304 | __CUPS_ADD_OPTION(dest, "Collate", "True"); | ||
1630 | 305 | } else { | ||
1631 | 306 | __CUPS_ADD_OPTION(dest, "Collate", "False"); | ||
1632 | 307 | } | ||
1633 | 308 | |||
1634 | 309 | if (options->copies() > 1) { | ||
1635 | 310 | __CUPS_ADD_OPTION(dest, "copies", QString::number(options->copies()).toLocal8Bit()); | ||
1636 | 311 | } | ||
1637 | 312 | |||
1638 | 313 | __CUPS_ADD_OPTION(dest, "ColorModel", options->getColorModel().name.toLocal8Bit()); | ||
1639 | 314 | __CUPS_ADD_OPTION(dest, "Duplex", Utils::duplexModeToPpdChoice(options->getDuplexMode()).toLocal8Bit()); | ||
1640 | 315 | |||
1641 | 316 | if (options->landscape()) { | ||
1642 | 317 | __CUPS_ADD_OPTION(dest, "landscape", ""); | ||
1643 | 318 | } | ||
1644 | 319 | |||
1645 | 320 | if (options->printRangeMode() == PrinterEnum::PrintRange::PageRange | ||
1646 | 321 | && !options->printRange().isEmpty()) { | ||
1647 | 322 | __CUPS_ADD_OPTION(dest, "page-ranges", options->printRange().toLocal8Bit()); | ||
1648 | 323 | } | ||
1649 | 324 | |||
1650 | 325 | PrintQuality quality = options->getPrintQuality(); | ||
1651 | 326 | __CUPS_ADD_OPTION(dest, quality.originalOption.toLocal8Bit(), | ||
1652 | 327 | quality.name.toLocal8Bit()); | ||
1653 | 328 | |||
1654 | 329 | if (options->reverse()) { | ||
1655 | 330 | __CUPS_ADD_OPTION(dest, "OutputOrder", "Reverse"); | ||
1656 | 331 | } else { | ||
1657 | 332 | __CUPS_ADD_OPTION(dest, "OutputOrder", "Normal"); | ||
1658 | 333 | } | ||
1659 | 334 | |||
1660 | 335 | // Always scale to fit the page for now | ||
1661 | 336 | __CUPS_ADD_OPTION(dest, "fit-to-page", "True"); | ||
1662 | 337 | |||
1663 | 338 | return dest; | ||
1664 | 339 | } | ||
1665 | 340 | |||
1666 | 341 | void PrinterCupsBackend::cancelJob(const QString &name, const int jobId) | ||
1667 | 342 | { | ||
1668 | 343 | int ret = cupsCancelJob(name.toLocal8Bit(), jobId); | ||
1669 | 344 | |||
1670 | 345 | if (!ret) { | ||
1671 | 346 | qWarning() << "Failed to cancel job:" << jobId << "for" << name; | ||
1672 | 347 | } | ||
1673 | 348 | } | ||
1674 | 349 | |||
1675 | 350 | int PrinterCupsBackend::printFileToDest(const QString &filepath, | ||
1676 | 351 | const QString &title, | ||
1677 | 352 | const cups_dest_t *dest) | ||
1678 | 353 | { | ||
1679 | 354 | qDebug() << "Printing:" << filepath << title << dest->name << dest->num_options; | ||
1680 | 355 | return cupsPrintFile(dest->name, | ||
1681 | 356 | filepath.toLocal8Bit(), | ||
1682 | 357 | title.toLocal8Bit(), | ||
1683 | 358 | dest->num_options, | ||
1684 | 359 | dest->options); | ||
1685 | 360 | } | ||
1686 | 361 | |||
1687 | 362 | |||
1688 | 363 | QList<cups_job_t *> PrinterCupsBackend::getCupsJobs(const QString &name) | ||
1689 | 364 | { | ||
1690 | 365 | QList<cups_job_t *> list; | ||
1691 | 366 | cups_job_t *jobs; | ||
1692 | 367 | |||
1693 | 368 | // Get a list of the jobs that are 'mine' and only active ones | ||
1694 | 369 | // https://www.cups.org/doc/api-cups.html#cupsGetJobs | ||
1695 | 370 | int count; | ||
1696 | 371 | if (name.isEmpty()) { | ||
1697 | 372 | count = cupsGetJobs(&jobs, NULL, 1, CUPS_WHICHJOBS_ACTIVE); | ||
1698 | 373 | } else { | ||
1699 | 374 | count = cupsGetJobs(&jobs, name.toLocal8Bit(), 1, CUPS_WHICHJOBS_ACTIVE); | ||
1700 | 375 | } | ||
1701 | 376 | |||
1702 | 377 | for (int i=0; i < count; i++) { | ||
1703 | 378 | list.append(&jobs[i]); | ||
1704 | 379 | } | ||
1705 | 380 | |||
1706 | 381 | return list; | ||
1707 | 382 | } | ||
1708 | 383 | |||
1709 | 384 | QMap<QString, QVariant> PrinterCupsBackend::printerGetJobAttributes( | ||
1710 | 385 | const QString &name, const int jobId) | ||
1711 | 386 | { | ||
1712 | 387 | Q_UNUSED(name); | ||
1713 | 388 | QMap<QString, QVariant> rawMap = m_client->printerGetJobAttributes(jobId); | ||
1714 | 389 | QMap<QString, QVariant> map; | ||
1715 | 390 | |||
1716 | 391 | // Filter attributes to know values | ||
1717 | 392 | // Do this here so we can use things such as m_knownQualityOptions | ||
1718 | 393 | |||
1719 | 394 | if (__CUPS_ATTR_EXISTS(rawMap, "Collate", bool)) { | ||
1720 | 395 | map.insert("Collate", rawMap.value("Collate")); | ||
1721 | 396 | } else { | ||
1722 | 397 | map.insert("Collate", QVariant(true)); | ||
1723 | 398 | } | ||
1724 | 399 | |||
1725 | 400 | if (__CUPS_ATTR_EXISTS(rawMap, "copies", int)) { | ||
1726 | 401 | map.insert("copies", rawMap.value("copies")); | ||
1727 | 402 | } else { | ||
1728 | 403 | map.insert("copies", QVariant(1)); | ||
1729 | 404 | } | ||
1730 | 405 | |||
1731 | 406 | if (__CUPS_ATTR_EXISTS(rawMap, "ColorModel", QString)) { | ||
1732 | 407 | map.insert("ColorModel", rawMap.value("ColorModel")); | ||
1733 | 408 | } else { | ||
1734 | 409 | map.insert("ColorModel", QVariant("")); | ||
1735 | 410 | } | ||
1736 | 411 | |||
1737 | 412 | if (__CUPS_ATTR_EXISTS(rawMap, "Duplex", QString)) { | ||
1738 | 413 | map.insert("Duplex", rawMap.value("Duplex")); | ||
1739 | 414 | } else { | ||
1740 | 415 | map.insert("Duplex", QVariant("")); | ||
1741 | 416 | } | ||
1742 | 417 | |||
1743 | 418 | if (__CUPS_ATTR_EXISTS(rawMap, "landscape", bool)) { | ||
1744 | 419 | map.insert("landscape", rawMap.value("landscape")); | ||
1745 | 420 | } else { | ||
1746 | 421 | map.insert("landscape", QVariant(false)); | ||
1747 | 422 | } | ||
1748 | 423 | |||
1749 | 424 | if (__CUPS_ATTR_EXISTS(rawMap, "page-ranges", QList<QVariant>)) { | ||
1750 | 425 | QList<QVariant> range = rawMap.value("page-ranges").toList(); | ||
1751 | 426 | QStringList rangeStrings; | ||
1752 | 427 | |||
1753 | 428 | Q_FOREACH(QVariant var, range) { | ||
1754 | 429 | rangeStrings << var.toString(); | ||
1755 | 430 | } | ||
1756 | 431 | |||
1757 | 432 | map.insert("page-ranges", QVariant(rangeStrings)); | ||
1758 | 433 | } else { | ||
1759 | 434 | map.insert("page-ranges", QVariant(QStringList())); | ||
1760 | 435 | } | ||
1761 | 436 | |||
1762 | 437 | Q_FOREACH(QString qualityOption, m_knownQualityOptions) { | ||
1763 | 438 | if (rawMap.contains(qualityOption) | ||
1764 | 439 | && rawMap.value(qualityOption).canConvert<QString>()) { | ||
1765 | 440 | map.insert("quality", rawMap.value(qualityOption).toString()); | ||
1766 | 441 | } | ||
1767 | 442 | } | ||
1768 | 443 | |||
1769 | 444 | if (!map.contains("quality")) { | ||
1770 | 445 | map.insert("quality", QVariant("")); | ||
1771 | 446 | } | ||
1772 | 447 | |||
1773 | 448 | if (__CUPS_ATTR_EXISTS(rawMap, "OutputOrder", QString)) { | ||
1774 | 449 | map.insert("OutputOrder", rawMap.value("OutputOrder")); | ||
1775 | 450 | } else { | ||
1776 | 451 | map.insert("OutputOrder", "Normal"); | ||
1777 | 452 | } | ||
1778 | 453 | |||
1779 | 454 | // Generate a list of messages | ||
1780 | 455 | // TODO: for now just using job-printer-state-message, are there others? | ||
1781 | 456 | QStringList messages; | ||
1782 | 457 | |||
1783 | 458 | if (__CUPS_ATTR_EXISTS(rawMap, "job-printer-state-message", QString)) { | ||
1784 | 459 | messages << rawMap.value("job-printer-state-message").toString(); | ||
1785 | 460 | } | ||
1786 | 461 | |||
1787 | 462 | map.insert("messages", QVariant(messages)); | ||
1788 | 463 | |||
1789 | 464 | return map; | ||
1790 | 465 | } | ||
1791 | 466 | |||
1792 | 467 | |||
1793 | 468 | QList<QSharedPointer<PrinterJob>> PrinterCupsBackend::printerGetJobs() | ||
1794 | 469 | { | ||
1795 | 470 | auto jobs = getCupsJobs(); | ||
1796 | 471 | QList<QSharedPointer<PrinterJob>> list; | ||
1797 | 472 | |||
1798 | 473 | Q_FOREACH(auto job, jobs) { | ||
1799 | 474 | auto newJob = QSharedPointer<PrinterJob>( | ||
1800 | 475 | new PrinterJob(QString::fromUtf8(job->dest), this, job->id) | ||
1801 | 476 | ); | ||
1802 | 477 | |||
1803 | 478 | // Extract the times | ||
1804 | 479 | QDateTime completedTime; | ||
1805 | 480 | completedTime.setTimeZone(QTimeZone::systemTimeZone()); | ||
1806 | 481 | completedTime.setTime_t(job->completed_time); | ||
1807 | 482 | |||
1808 | 483 | QDateTime creationTime; | ||
1809 | 484 | creationTime.setTimeZone(QTimeZone::systemTimeZone()); | ||
1810 | 485 | creationTime.setTime_t(job->creation_time); | ||
1811 | 486 | |||
1812 | 487 | QDateTime processingTime; | ||
1813 | 488 | processingTime.setTimeZone(QTimeZone::systemTimeZone()); | ||
1814 | 489 | processingTime.setTime_t(job->processing_time); | ||
1815 | 490 | |||
1816 | 491 | // Load the information from the cups struct | ||
1817 | 492 | newJob->setCompletedTime(completedTime); | ||
1818 | 493 | newJob->setCreationTime(creationTime); | ||
1819 | 494 | newJob->setProcessingTime(processingTime); | ||
1820 | 495 | newJob->setSize(job->size); | ||
1821 | 496 | newJob->setState(static_cast<PrinterEnum::JobState>(job->state)); | ||
1822 | 497 | newJob->setTitle(QString::fromLocal8Bit(job->title)); | ||
1823 | 498 | newJob->setUser(QString::fromLocal8Bit(job->user)); | ||
1824 | 499 | |||
1825 | 500 | list.append(newJob); | ||
1826 | 501 | } | ||
1827 | 502 | if (!list.isEmpty()) | ||
1828 | 503 | cupsFreeJobs(list.size(), jobs.first()); | ||
1829 | 504 | |||
1830 | 505 | return list; | ||
1831 | 506 | } | ||
1832 | 507 | |||
1833 | 508 | QString PrinterCupsBackend::printerName() const | ||
1834 | 509 | { | ||
1835 | 510 | return m_printerName; | ||
1836 | 511 | } | ||
1837 | 512 | |||
1838 | 513 | QString PrinterCupsBackend::description() const | ||
1839 | 514 | { | ||
1840 | 515 | return m_info.description(); | ||
1841 | 516 | } | ||
1842 | 517 | |||
1843 | 518 | QString PrinterCupsBackend::location() const | ||
1844 | 519 | { | ||
1845 | 520 | return m_info.location(); | ||
1846 | 521 | } | ||
1847 | 522 | |||
1848 | 523 | QString PrinterCupsBackend::makeAndModel() const | ||
1849 | 524 | { | ||
1850 | 525 | return m_info.makeAndModel(); | ||
1851 | 526 | } | ||
1852 | 527 | |||
1853 | 528 | PrinterEnum::State PrinterCupsBackend::state() const | ||
1854 | 529 | { | ||
1855 | 530 | switch (m_info.state()) { | ||
1856 | 531 | case QPrinter::Active: | ||
1857 | 532 | return PrinterEnum::State::ActiveState; | ||
1858 | 533 | case QPrinter::Aborted: | ||
1859 | 534 | return PrinterEnum::State::AbortedState; | ||
1860 | 535 | case QPrinter::Error: | ||
1861 | 536 | return PrinterEnum::State::ErrorState; | ||
1862 | 537 | case QPrinter::Idle: | ||
1863 | 538 | default: | ||
1864 | 539 | return PrinterEnum::State::IdleState; | ||
1865 | 540 | } | ||
1866 | 541 | } | ||
1867 | 542 | |||
1868 | 543 | QList<QPageSize> PrinterCupsBackend::supportedPageSizes() const | ||
1869 | 544 | { | ||
1870 | 545 | return m_info.supportedPageSizes(); | ||
1871 | 546 | } | ||
1872 | 547 | |||
1873 | 548 | QPageSize PrinterCupsBackend::defaultPageSize() const | ||
1874 | 549 | { | ||
1875 | 550 | return m_info.defaultPageSize(); | ||
1876 | 551 | } | ||
1877 | 552 | |||
1878 | 553 | bool PrinterCupsBackend::supportsCustomPageSizes() const | ||
1879 | 554 | { | ||
1880 | 555 | return m_info.supportsCustomPageSizes(); | ||
1881 | 556 | } | ||
1882 | 557 | |||
1883 | 558 | QPageSize PrinterCupsBackend::minimumPhysicalPageSize() const | ||
1884 | 559 | { | ||
1885 | 560 | return m_info.minimumPhysicalPageSize(); | ||
1886 | 561 | } | ||
1887 | 562 | |||
1888 | 563 | QPageSize PrinterCupsBackend::maximumPhysicalPageSize() const | ||
1889 | 564 | { | ||
1890 | 565 | return m_info.maximumPhysicalPageSize(); | ||
1891 | 566 | } | ||
1892 | 567 | |||
1893 | 568 | QList<int> PrinterCupsBackend::supportedResolutions() const | ||
1894 | 569 | { | ||
1895 | 570 | return m_info.supportedResolutions(); | ||
1896 | 571 | } | ||
1897 | 572 | |||
1898 | 573 | PrinterEnum::DuplexMode PrinterCupsBackend::defaultDuplexMode() const | ||
1899 | 574 | { | ||
1900 | 575 | return Utils::qDuplexModeToDuplexMode(m_info.defaultDuplexMode()); | ||
1901 | 576 | } | ||
1902 | 577 | |||
1903 | 578 | QList<PrinterEnum::DuplexMode> PrinterCupsBackend::supportedDuplexModes() const | ||
1904 | 579 | { | ||
1905 | 580 | QList<PrinterEnum::DuplexMode> list; | ||
1906 | 581 | Q_FOREACH(const QPrinter::DuplexMode mode, m_info.supportedDuplexModes()) { | ||
1907 | 582 | if (mode != QPrinter::DuplexAuto) { | ||
1908 | 583 | list.append(Utils::qDuplexModeToDuplexMode(mode)); | ||
1909 | 584 | } | ||
1910 | 585 | } | ||
1911 | 586 | |||
1912 | 587 | if (list.isEmpty()) | ||
1913 | 588 | list.append(PrinterEnum::DuplexMode::DuplexNone); | ||
1914 | 589 | |||
1915 | 590 | return list; | ||
1916 | 591 | } | ||
1917 | 592 | |||
1918 | 593 | QList<QSharedPointer<Printer>> PrinterCupsBackend::availablePrinters() | ||
1919 | 594 | { | ||
1920 | 595 | return QList<QSharedPointer<Printer>>(); | ||
1921 | 596 | } | ||
1922 | 597 | |||
1923 | 598 | QStringList PrinterCupsBackend::availablePrinterNames() | ||
1924 | 599 | { | ||
1925 | 600 | return QPrinterInfo::availablePrinterNames(); | ||
1926 | 601 | } | ||
1927 | 602 | |||
1928 | 603 | QSharedPointer<Printer> PrinterCupsBackend::getPrinter(const QString &printerName) | ||
1929 | 604 | { | ||
1930 | 605 | QPrinterInfo info = QPrinterInfo::printerInfo(printerName); | ||
1931 | 606 | return QSharedPointer<Printer>(new Printer(new PrinterCupsBackend(m_client, info, m_notifier))); | ||
1932 | 607 | } | ||
1933 | 608 | |||
1934 | 609 | QString PrinterCupsBackend::defaultPrinterName() | ||
1935 | 610 | { | ||
1936 | 611 | return QPrinterInfo::defaultPrinterName(); | ||
1937 | 612 | } | ||
1938 | 613 | |||
1939 | 614 | void PrinterCupsBackend::requestPrinter(const QString &printerName) | ||
1940 | 615 | { | ||
1941 | 616 | if (m_activeRequests.contains(printerName)) { | ||
1942 | 617 | return; | ||
1943 | 618 | } | ||
1944 | 619 | |||
1945 | 620 | auto thread = new QThread; | ||
1946 | 621 | auto loader = new PrinterLoader(printerName, m_client, m_notifier); | ||
1947 | 622 | loader->moveToThread(thread); | ||
1948 | 623 | connect(thread, SIGNAL(started()), loader, SLOT(load())); | ||
1949 | 624 | connect(loader, SIGNAL(finished()), thread, SLOT(quit())); | ||
1950 | 625 | connect(loader, SIGNAL(finished()), loader, SLOT(deleteLater())); | ||
1951 | 626 | connect(loader, SIGNAL(loaded(QSharedPointer<Printer>)), | ||
1952 | 627 | this, SIGNAL(printerLoaded(QSharedPointer<Printer>))); | ||
1953 | 628 | connect(loader, SIGNAL(loaded(QSharedPointer<Printer>)), | ||
1954 | 629 | this, SLOT(onPrinterLoaded(QSharedPointer<Printer>))); | ||
1955 | 630 | connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); | ||
1956 | 631 | thread->start(); | ||
1957 | 632 | |||
1958 | 633 | m_activeRequests << printerName; | ||
1959 | 634 | } | ||
1960 | 635 | |||
1961 | 636 | void PrinterCupsBackend::requestPrinterDrivers() | ||
1962 | 637 | { | ||
1963 | 638 | auto thread = new QThread; | ||
1964 | 639 | auto loader = new PrinterDriverLoader(); | ||
1965 | 640 | loader->moveToThread(thread); | ||
1966 | 641 | connect(loader, SIGNAL(error(const QString&)), | ||
1967 | 642 | this, SIGNAL(printerDriversFailedToLoad(const QString&))); | ||
1968 | 643 | connect(this, SIGNAL(requestPrinterDriverCancel()), loader, SLOT(cancel())); | ||
1969 | 644 | connect(thread, SIGNAL(started()), loader, SLOT(process())); | ||
1970 | 645 | connect(loader, SIGNAL(finished()), thread, SLOT(quit())); | ||
1971 | 646 | connect(loader, SIGNAL(finished()), loader, SLOT(deleteLater())); | ||
1972 | 647 | connect(loader, SIGNAL(loaded(const QList<PrinterDriver>&)), | ||
1973 | 648 | this, SIGNAL(printerDriversLoaded(const QList<PrinterDriver>&))); | ||
1974 | 649 | connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); | ||
1975 | 650 | thread->start(); | ||
1976 | 651 | } | ||
1977 | 652 | |||
1978 | 653 | void PrinterCupsBackend::cancelPrinterDriverRequest() | ||
1979 | 654 | { | ||
1980 | 655 | Q_EMIT requestPrinterDriverCancel(); | ||
1981 | 656 | } | ||
1982 | 657 | |||
1983 | 658 | void PrinterCupsBackend::refresh() | ||
1984 | 659 | { | ||
1985 | 660 | if (m_printerName.isEmpty()) { | ||
1986 | 661 | throw std::invalid_argument("Trying to refresh unnamed printer."); | ||
1987 | 662 | } else { | ||
1988 | 663 | m_info = QPrinterInfo::printerInfo(m_printerName); | ||
1989 | 664 | } | ||
1990 | 665 | } | ||
1991 | 666 | |||
1992 | 667 | void PrinterCupsBackend::createSubscription() | ||
1993 | 668 | { | ||
1994 | 669 | m_cupsSubscriptionId = m_client->createSubscription();; | ||
1995 | 670 | } | ||
1996 | 671 | |||
1997 | 672 | void PrinterCupsBackend::cancelSubscription() | ||
1998 | 673 | { | ||
1999 | 674 | if (m_cupsSubscriptionId > 0) | ||
2000 | 675 | m_client->cancelSubscription(m_cupsSubscriptionId); | ||
2001 | 676 | } | ||
2002 | 677 | |||
2003 | 678 | QString PrinterCupsBackend::getPrinterInstance(const QString &name) const | ||
2004 | 679 | { | ||
2005 | 680 | const auto parts = name.splitRef(QLatin1Char('/')); | ||
2006 | 681 | QString instance; | ||
2007 | 682 | if (parts.size() > 1) | ||
2008 | 683 | instance = parts.at(1).toString(); | ||
2009 | 684 | |||
2010 | 685 | return instance; | ||
2011 | 686 | } | ||
2012 | 687 | |||
2013 | 688 | QString PrinterCupsBackend::getPrinterName(const QString &name) const | ||
2014 | 689 | { | ||
2015 | 690 | return name.splitRef(QLatin1Char('/')).first().toString(); | ||
2016 | 691 | } | ||
2017 | 692 | |||
2018 | 693 | cups_dest_t* PrinterCupsBackend::getDest(const QString &name) const | ||
2019 | 694 | { | ||
2020 | 695 | QString printerName = getPrinterName(name); | ||
2021 | 696 | QString instance = getPrinterInstance(name); | ||
2022 | 697 | |||
2023 | 698 | if (m_dests.contains(name)) { | ||
2024 | 699 | return m_dests[name]; | ||
2025 | 700 | } else { | ||
2026 | 701 | m_dests[name] = m_client->getDest(printerName, instance); | ||
2027 | 702 | return m_dests[name]; | ||
2028 | 703 | } | ||
2029 | 704 | } | ||
2030 | 705 | |||
2031 | 706 | ppd_file_t* PrinterCupsBackend::getPpd(const QString &name) const | ||
2032 | 707 | { | ||
2033 | 708 | QString printerName = getPrinterName(name); | ||
2034 | 709 | QString instance = getPrinterInstance(name); | ||
2035 | 710 | |||
2036 | 711 | if (m_ppds.contains(name)) { | ||
2037 | 712 | return m_ppds[name]; | ||
2038 | 713 | } else { | ||
2039 | 714 | m_ppds[name] = m_client->getPpdFile(printerName, instance); | ||
2040 | 715 | return m_ppds[name]; | ||
2041 | 716 | } | ||
2042 | 717 | } | ||
2043 | 718 | |||
2044 | 719 | void PrinterCupsBackend::onPrinterLoaded(QSharedPointer<Printer> printer) | ||
2045 | 720 | { | ||
2046 | 721 | m_activeRequests.removeOne(printer->name()); | ||
2047 | 722 | } | ||
2048 | 0 | 723 | ||
2049 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.h' | |||
2050 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.h 1970-01-01 00:00:00 +0000 | |||
2051 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend_cups.h 2017-02-24 12:29:37 +0000 | |||
2052 | @@ -0,0 +1,135 @@ | |||
2053 | 1 | /* | ||
2054 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
2055 | 3 | * | ||
2056 | 4 | * This program is free software; you can redistribute it and/or modify | ||
2057 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
2058 | 6 | * the Free Software Foundation; version 3. | ||
2059 | 7 | * | ||
2060 | 8 | * This program is distributed in the hope that it will be useful, | ||
2061 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2062 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2063 | 11 | * GNU Lesser General Public License for more details. | ||
2064 | 12 | * | ||
2065 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
2066 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2067 | 15 | */ | ||
2068 | 16 | |||
2069 | 17 | #ifndef USC_PRINTERS_CUPS_BACKEND_H | ||
2070 | 18 | #define USC_PRINTERS_CUPS_BACKEND_H | ||
2071 | 19 | |||
2072 | 20 | #include "backend/backend.h" | ||
2073 | 21 | #include "cups/ippclient.h" | ||
2074 | 22 | #include "cupsdnotifier.h" // Note: this file was generated. | ||
2075 | 23 | |||
2076 | 24 | #include <cups/cups.h> | ||
2077 | 25 | |||
2078 | 26 | #include <QPrinterInfo> | ||
2079 | 27 | |||
2080 | 28 | class PRINTERS_DECL_EXPORT PrinterCupsBackend : public PrinterBackend | ||
2081 | 29 | { | ||
2082 | 30 | Q_OBJECT | ||
2083 | 31 | public: | ||
2084 | 32 | explicit PrinterCupsBackend(IppClient *client, QPrinterInfo info, | ||
2085 | 33 | OrgCupsCupsdNotifierInterface* notifier, | ||
2086 | 34 | QObject *parent = Q_NULLPTR); | ||
2087 | 35 | virtual ~PrinterCupsBackend() override; | ||
2088 | 36 | |||
2089 | 37 | virtual bool holdsDefinition() const override; | ||
2090 | 38 | |||
2091 | 39 | virtual QString printerAdd(const QString &name, | ||
2092 | 40 | const QString &uri, | ||
2093 | 41 | const QString &ppdFile, | ||
2094 | 42 | const QString &info, | ||
2095 | 43 | const QString &location) override; | ||
2096 | 44 | virtual QString printerAddWithPpd(const QString &name, | ||
2097 | 45 | const QString &uri, | ||
2098 | 46 | const QString &ppdFileName, | ||
2099 | 47 | const QString &info, | ||
2100 | 48 | const QString &location) override; | ||
2101 | 49 | virtual QString printerDelete(const QString &name) override; | ||
2102 | 50 | virtual QString printerSetDefault(const QString &name) override; | ||
2103 | 51 | virtual QString printerSetEnabled(const QString &name, | ||
2104 | 52 | const bool enabled) override; | ||
2105 | 53 | virtual QString printerSetAcceptJobs( | ||
2106 | 54 | const QString &name, | ||
2107 | 55 | const bool accept, | ||
2108 | 56 | const QString &reason = QString::null) override; | ||
2109 | 57 | virtual QString printerSetInfo(const QString &name, | ||
2110 | 58 | const QString &info) override; | ||
2111 | 59 | virtual QString printerAddOption(const QString &name, | ||
2112 | 60 | const QString &option, | ||
2113 | 61 | const QStringList &values) override; | ||
2114 | 62 | |||
2115 | 63 | virtual QVariant printerGetOption(const QString &name, | ||
2116 | 64 | const QString &option) const override; | ||
2117 | 65 | virtual QMap<QString, QVariant> printerGetOptions( | ||
2118 | 66 | const QString &name, const QStringList &options | ||
2119 | 67 | ) const override; | ||
2120 | 68 | // FIXME: maybe have a PrinterDest iface that has a CupsDest impl? | ||
2121 | 69 | virtual cups_dest_t* makeDest(const QString &name, | ||
2122 | 70 | const PrinterJob *options) override; | ||
2123 | 71 | |||
2124 | 72 | virtual void cancelJob(const QString &name, const int jobId) override; | ||
2125 | 73 | virtual int printFileToDest(const QString &filepath, | ||
2126 | 74 | const QString &title, | ||
2127 | 75 | const cups_dest_t *dest) override; | ||
2128 | 76 | virtual QList<QSharedPointer<PrinterJob>> printerGetJobs() override; | ||
2129 | 77 | |||
2130 | 78 | virtual QString printerName() const override; | ||
2131 | 79 | virtual QString description() const override; | ||
2132 | 80 | virtual QString location() const override; | ||
2133 | 81 | virtual QString makeAndModel() const override; | ||
2134 | 82 | |||
2135 | 83 | virtual PrinterEnum::State state() const override; | ||
2136 | 84 | virtual QList<QPageSize> supportedPageSizes() const override; | ||
2137 | 85 | virtual QPageSize defaultPageSize() const override; | ||
2138 | 86 | virtual bool supportsCustomPageSizes() const override; | ||
2139 | 87 | |||
2140 | 88 | virtual QPageSize minimumPhysicalPageSize() const override; | ||
2141 | 89 | virtual QPageSize maximumPhysicalPageSize() const override; | ||
2142 | 90 | virtual QList<int> supportedResolutions() const override; | ||
2143 | 91 | virtual PrinterEnum::DuplexMode defaultDuplexMode() const override; | ||
2144 | 92 | virtual QList<PrinterEnum::DuplexMode> supportedDuplexModes() const override; | ||
2145 | 93 | |||
2146 | 94 | virtual QList<QSharedPointer<Printer>> availablePrinters() override; | ||
2147 | 95 | virtual QStringList availablePrinterNames() override; | ||
2148 | 96 | virtual QSharedPointer<Printer> getPrinter(const QString &printerName) override; | ||
2149 | 97 | virtual QString defaultPrinterName() override; | ||
2150 | 98 | virtual void requestPrinterDrivers() override; | ||
2151 | 99 | virtual void requestPrinter(const QString &printerName) override; | ||
2152 | 100 | virtual QMap<QString, QVariant> printerGetJobAttributes( | ||
2153 | 101 | const QString &name, const int jobId) override; | ||
2154 | 102 | |||
2155 | 103 | public Q_SLOTS: | ||
2156 | 104 | virtual void refresh() override; | ||
2157 | 105 | void createSubscription(); | ||
2158 | 106 | |||
2159 | 107 | Q_SIGNALS: | ||
2160 | 108 | void cancelWorkers(); | ||
2161 | 109 | void printerDriversLoaded(const QList<PrinterDriver> &drivers); | ||
2162 | 110 | void printerDriversFailedToLoad(const QString &errorMessage); | ||
2163 | 111 | void requestPrinterDriverCancel(); | ||
2164 | 112 | |||
2165 | 113 | private: | ||
2166 | 114 | void cancelSubscription(); | ||
2167 | 115 | void cancelPrinterDriverRequest(); | ||
2168 | 116 | QList<cups_job_t *> getCupsJobs(const QString &name = QStringLiteral()); | ||
2169 | 117 | |||
2170 | 118 | QString getPrinterName(const QString &name) const; | ||
2171 | 119 | QString getPrinterInstance(const QString &name) const; | ||
2172 | 120 | cups_dest_t* getDest(const QString &name) const; | ||
2173 | 121 | ppd_file_t* getPpd(const QString &name) const; | ||
2174 | 122 | const QStringList m_knownQualityOptions; | ||
2175 | 123 | IppClient *m_client; | ||
2176 | 124 | QPrinterInfo m_info; | ||
2177 | 125 | OrgCupsCupsdNotifierInterface *m_notifier; | ||
2178 | 126 | int m_cupsSubscriptionId; | ||
2179 | 127 | mutable QMap<QString, cups_dest_t*> m_dests; // Printer name, dest. | ||
2180 | 128 | mutable QMap<QString, ppd_file_t*> m_ppds; // Printer name, ppd. | ||
2181 | 129 | QList<QString> m_activeRequests; | ||
2182 | 130 | |||
2183 | 131 | private Q_SLOTS: | ||
2184 | 132 | void onPrinterLoaded(QSharedPointer<Printer> printer); | ||
2185 | 133 | }; | ||
2186 | 134 | |||
2187 | 135 | #endif // USC_PRINTERS_CUPS_BACKEND_H | ||
2188 | 0 | 136 | ||
2189 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.cpp' | |||
2190 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.cpp 1970-01-01 00:00:00 +0000 | |||
2191 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.cpp 2017-02-24 12:29:37 +0000 | |||
2192 | @@ -0,0 +1,119 @@ | |||
2193 | 1 | /* | ||
2194 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
2195 | 3 | * | ||
2196 | 4 | * This program is free software; you can redistribute it and/or modify | ||
2197 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
2198 | 6 | * the Free Software Foundation; version 3. | ||
2199 | 7 | * | ||
2200 | 8 | * This program is distributed in the hope that it will be useful, | ||
2201 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2202 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2203 | 11 | * GNU Lesser General Public License for more details. | ||
2204 | 12 | * | ||
2205 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
2206 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2207 | 15 | */ | ||
2208 | 16 | |||
2209 | 17 | #include "i18n.h" | ||
2210 | 18 | #include "backend/backend_pdf.h" | ||
2211 | 19 | |||
2212 | 20 | PrinterPdfBackend::PrinterPdfBackend(const QString &printerName, | ||
2213 | 21 | QObject *parent) | ||
2214 | 22 | : PrinterBackend(printerName, parent) | ||
2215 | 23 | { | ||
2216 | 24 | m_type = PrinterEnum::PrinterType::PdfType; | ||
2217 | 25 | } | ||
2218 | 26 | |||
2219 | 27 | QVariant PrinterPdfBackend::printerGetOption(const QString &name, | ||
2220 | 28 | const QString &option) const | ||
2221 | 29 | { | ||
2222 | 30 | auto res = printerGetOptions(name, QStringList({option})); | ||
2223 | 31 | return res[option]; | ||
2224 | 32 | } | ||
2225 | 33 | |||
2226 | 34 | QMap<QString, QVariant> PrinterPdfBackend::printerGetOptions( | ||
2227 | 35 | const QString &name, const QStringList &options) const | ||
2228 | 36 | { | ||
2229 | 37 | Q_UNUSED(name); | ||
2230 | 38 | |||
2231 | 39 | QMap<QString, QVariant> ret; | ||
2232 | 40 | |||
2233 | 41 | ColorModel rgb; | ||
2234 | 42 | rgb.colorType = PrinterEnum::ColorModelType::ColorType; | ||
2235 | 43 | rgb.name = "RGB"; | ||
2236 | 44 | rgb.text = __("Color"); | ||
2237 | 45 | |||
2238 | 46 | PrintQuality quality; | ||
2239 | 47 | quality.name = __("Normal"); | ||
2240 | 48 | |||
2241 | 49 | Q_FOREACH(const QString &option, options) { | ||
2242 | 50 | if (option == QLatin1String("DefaultColorModel")) { | ||
2243 | 51 | ret[option] = QVariant::fromValue(rgb); | ||
2244 | 52 | } else if (option == QLatin1String("DefaultPrintQuality")) { | ||
2245 | 53 | ret[option] = QVariant::fromValue(quality); | ||
2246 | 54 | } else if (option == QLatin1String("SupportedPrintQualities")) { | ||
2247 | 55 | auto qualities = QList<PrintQuality>({quality}); | ||
2248 | 56 | ret[option] = QVariant::fromValue(qualities); | ||
2249 | 57 | } else if (option == QLatin1String("SupportedColorModels")) { | ||
2250 | 58 | auto models = QList<ColorModel>{rgb}; | ||
2251 | 59 | ret[option] = QVariant::fromValue(models); | ||
2252 | 60 | } else if (option == QLatin1String("AcceptJobs")) { | ||
2253 | 61 | ret[option] = true; | ||
2254 | 62 | } else { | ||
2255 | 63 | throw std::invalid_argument("Invalid value for PDF printer: " + option.toStdString()); | ||
2256 | 64 | } | ||
2257 | 65 | } | ||
2258 | 66 | |||
2259 | 67 | return ret; | ||
2260 | 68 | } | ||
2261 | 69 | |||
2262 | 70 | QString PrinterPdfBackend::printerName() const | ||
2263 | 71 | { | ||
2264 | 72 | return m_printerName; | ||
2265 | 73 | } | ||
2266 | 74 | |||
2267 | 75 | PrinterEnum::State PrinterPdfBackend::state() const | ||
2268 | 76 | { | ||
2269 | 77 | return PrinterEnum::State::IdleState; | ||
2270 | 78 | } | ||
2271 | 79 | |||
2272 | 80 | QList<QPageSize> PrinterPdfBackend::supportedPageSizes() const | ||
2273 | 81 | { | ||
2274 | 82 | return QList<QPageSize>{QPageSize(QPageSize::A4)}; | ||
2275 | 83 | } | ||
2276 | 84 | |||
2277 | 85 | QPageSize PrinterPdfBackend::defaultPageSize() const | ||
2278 | 86 | { | ||
2279 | 87 | return QPageSize(QPageSize::A4); | ||
2280 | 88 | } | ||
2281 | 89 | |||
2282 | 90 | bool PrinterPdfBackend::supportsCustomPageSizes() const | ||
2283 | 91 | { | ||
2284 | 92 | return false; | ||
2285 | 93 | } | ||
2286 | 94 | |||
2287 | 95 | QPageSize PrinterPdfBackend::minimumPhysicalPageSize() const | ||
2288 | 96 | { | ||
2289 | 97 | return QPageSize(QPageSize::A4); | ||
2290 | 98 | } | ||
2291 | 99 | |||
2292 | 100 | QPageSize PrinterPdfBackend::maximumPhysicalPageSize() const | ||
2293 | 101 | { | ||
2294 | 102 | return QPageSize(QPageSize::A4); | ||
2295 | 103 | } | ||
2296 | 104 | |||
2297 | 105 | QList<int> PrinterPdfBackend::supportedResolutions() const | ||
2298 | 106 | { | ||
2299 | 107 | return QList<int>{}; | ||
2300 | 108 | } | ||
2301 | 109 | |||
2302 | 110 | PrinterEnum::DuplexMode PrinterPdfBackend::defaultDuplexMode() const | ||
2303 | 111 | { | ||
2304 | 112 | return PrinterEnum::DuplexMode::DuplexNone; | ||
2305 | 113 | } | ||
2306 | 114 | |||
2307 | 115 | QList<PrinterEnum::DuplexMode> PrinterPdfBackend::supportedDuplexModes() const | ||
2308 | 116 | { | ||
2309 | 117 | return QList<PrinterEnum::DuplexMode>{PrinterEnum::DuplexMode::DuplexNone}; | ||
2310 | 118 | } | ||
2311 | 119 | |||
2312 | 0 | 120 | ||
2313 | === added file 'modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.h' | |||
2314 | --- modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.h 1970-01-01 00:00:00 +0000 | |||
2315 | +++ modules/Ubuntu/Components/Extras/Printers/backend/backend_pdf.h 2017-02-24 12:29:37 +0000 | |||
2316 | @@ -0,0 +1,47 @@ | |||
2317 | 1 | /* | ||
2318 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
2319 | 3 | * | ||
2320 | 4 | * This program is free software; you can redistribute it and/or modify | ||
2321 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
2322 | 6 | * the Free Software Foundation; version 3. | ||
2323 | 7 | * | ||
2324 | 8 | * This program is distributed in the hope that it will be useful, | ||
2325 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2326 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2327 | 11 | * GNU Lesser General Public License for more details. | ||
2328 | 12 | * | ||
2329 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
2330 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2331 | 15 | */ | ||
2332 | 16 | |||
2333 | 17 | #ifndef USC_PRINTERS_PDF_BACKEND_H | ||
2334 | 18 | #define USC_PRINTERS_PDF_BACKEND_H | ||
2335 | 19 | |||
2336 | 20 | #include "backend/backend.h" | ||
2337 | 21 | |||
2338 | 22 | class PRINTERS_DECL_EXPORT PrinterPdfBackend : public PrinterBackend | ||
2339 | 23 | { | ||
2340 | 24 | public: | ||
2341 | 25 | explicit PrinterPdfBackend(const QString &printerName, | ||
2342 | 26 | QObject *parent = Q_NULLPTR); | ||
2343 | 27 | virtual QVariant printerGetOption(const QString &name, | ||
2344 | 28 | const QString &option) const override; | ||
2345 | 29 | virtual QMap<QString, QVariant> printerGetOptions( | ||
2346 | 30 | const QString &name, const QStringList &options | ||
2347 | 31 | ) const override; | ||
2348 | 32 | |||
2349 | 33 | virtual QString printerName() const override; | ||
2350 | 34 | |||
2351 | 35 | virtual PrinterEnum::State state() const override; | ||
2352 | 36 | virtual QList<QPageSize> supportedPageSizes() const override; | ||
2353 | 37 | virtual QPageSize defaultPageSize() const override; | ||
2354 | 38 | virtual bool supportsCustomPageSizes() const override; | ||
2355 | 39 | |||
2356 | 40 | virtual QPageSize minimumPhysicalPageSize() const override; | ||
2357 | 41 | virtual QPageSize maximumPhysicalPageSize() const override; | ||
2358 | 42 | virtual QList<int> supportedResolutions() const override; | ||
2359 | 43 | virtual PrinterEnum::DuplexMode defaultDuplexMode() const override; | ||
2360 | 44 | virtual QList<PrinterEnum::DuplexMode> supportedDuplexModes() const override; | ||
2361 | 45 | }; | ||
2362 | 46 | |||
2363 | 47 | #endif // USC_PRINTERS_PDF_BACKEND_H | ||
2364 | 0 | 48 | ||
2365 | === added directory 'modules/Ubuntu/Components/Extras/Printers/cups' | |||
2366 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/ippclient.cpp' | |||
2367 | --- modules/Ubuntu/Components/Extras/Printers/cups/ippclient.cpp 1970-01-01 00:00:00 +0000 | |||
2368 | +++ modules/Ubuntu/Components/Extras/Printers/cups/ippclient.cpp 2017-02-24 12:29:37 +0000 | |||
2369 | @@ -0,0 +1,988 @@ | |||
2370 | 1 | /* | ||
2371 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
2372 | 3 | * Copyright (C) 2014 John Layt <jlayt@kde.org> | ||
2373 | 4 | * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014 Red Hat, Inc. | ||
2374 | 5 | * Copyright (C) 2008 Novell, Inc. | ||
2375 | 6 | * | ||
2376 | 7 | * This program is free software; you can redistribute it and/or modify | ||
2377 | 8 | * it under the terms of the GNU Lesser General Public License as published by | ||
2378 | 9 | * the Free Software Foundation; version 3. | ||
2379 | 10 | * | ||
2380 | 11 | * This program is distributed in the hope that it will be useful, | ||
2381 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
2382 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
2383 | 14 | * GNU Lesser General Public License for more details. | ||
2384 | 15 | * | ||
2385 | 16 | * You should have received a copy of the GNU Lesser General Public License | ||
2386 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2387 | 18 | */ | ||
2388 | 19 | |||
2389 | 20 | #include "cups/ippclient.h" | ||
2390 | 21 | |||
2391 | 22 | #include <errno.h> | ||
2392 | 23 | #include <string.h> | ||
2393 | 24 | #include <unistd.h> | ||
2394 | 25 | |||
2395 | 26 | #include <QDebug> | ||
2396 | 27 | #include <QDateTime> | ||
2397 | 28 | #include <QTimeZone> | ||
2398 | 29 | #include <QUrl> | ||
2399 | 30 | |||
2400 | 31 | IppClient::IppClient() | ||
2401 | 32 | : m_connection(httpConnectEncrypt(cupsServer(), | ||
2402 | 33 | ippPort(), | ||
2403 | 34 | cupsEncryption())) | ||
2404 | 35 | { | ||
2405 | 36 | if (!m_connection) { | ||
2406 | 37 | qCritical("Failed to connect to cupsd"); | ||
2407 | 38 | } else { | ||
2408 | 39 | qDebug("Successfully connected to cupsd."); | ||
2409 | 40 | } | ||
2410 | 41 | } | ||
2411 | 42 | |||
2412 | 43 | IppClient::~IppClient() | ||
2413 | 44 | { | ||
2414 | 45 | if (m_connection) | ||
2415 | 46 | httpClose(m_connection); | ||
2416 | 47 | } | ||
2417 | 48 | |||
2418 | 49 | bool IppClient::printerDelete(const QString &printerName) | ||
2419 | 50 | { | ||
2420 | 51 | return sendNewSimpleRequest(CUPS_DELETE_PRINTER, printerName.toUtf8(), | ||
2421 | 52 | CupsResource::CupsResourceAdmin); | ||
2422 | 53 | } | ||
2423 | 54 | |||
2424 | 55 | bool IppClient::printerAdd(const QString &printerName, | ||
2425 | 56 | const QString &printerUri, | ||
2426 | 57 | const QString &ppdFile, | ||
2427 | 58 | const QString &info, | ||
2428 | 59 | const QString &location) | ||
2429 | 60 | { | ||
2430 | 61 | ipp_t *request; | ||
2431 | 62 | |||
2432 | 63 | if (!isPrinterNameValid(printerName)) { | ||
2433 | 64 | setInternalStatus(QString("%1 is not a valid printer name.").arg(printerName)); | ||
2434 | 65 | return false; | ||
2435 | 66 | } | ||
2436 | 67 | |||
2437 | 68 | if (!isStringValid(info)) { | ||
2438 | 69 | setInternalStatus(QString("%1 is not a valid description.").arg(info)); | ||
2439 | 70 | return false; | ||
2440 | 71 | } | ||
2441 | 72 | |||
2442 | 73 | if (!isStringValid(location)) { | ||
2443 | 74 | setInternalStatus(QString("%1 is not a valid location.").arg(location)); | ||
2444 | 75 | return false; | ||
2445 | 76 | } | ||
2446 | 77 | |||
2447 | 78 | if (!isStringValid(ppdFile)) { | ||
2448 | 79 | setInternalStatus(QString("%1 is not a valid ppd file.").arg(ppdFile)); | ||
2449 | 80 | return false; | ||
2450 | 81 | } | ||
2451 | 82 | |||
2452 | 83 | if (!isStringValid(printerUri)) { | ||
2453 | 84 | setInternalStatus(QString("%1 is not a valid printer uri.").arg(printerUri)); | ||
2454 | 85 | return false; | ||
2455 | 86 | } | ||
2456 | 87 | |||
2457 | 88 | |||
2458 | 89 | request = ippNewRequest (CUPS_ADD_MODIFY_PRINTER); | ||
2459 | 90 | addPrinterUri(request, printerName); | ||
2460 | 91 | addRequestingUsername(request, NULL); | ||
2461 | 92 | |||
2462 | 93 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | ||
2463 | 94 | "printer-name", NULL, printerName.toUtf8()); | ||
2464 | 95 | |||
2465 | 96 | if (!ppdFile.isEmpty()) { | ||
2466 | 97 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | ||
2467 | 98 | "ppd-name", NULL, ppdFile.toUtf8()); | ||
2468 | 99 | } | ||
2469 | 100 | if (!printerUri.isEmpty()) { | ||
2470 | 101 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, | ||
2471 | 102 | "device-uri", NULL, printerUri.toUtf8()); | ||
2472 | 103 | } | ||
2473 | 104 | if (!info.isEmpty()) { | ||
2474 | 105 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, | ||
2475 | 106 | "printer-info", NULL, info.toUtf8()); | ||
2476 | 107 | } | ||
2477 | 108 | if (!location.isEmpty()) { | ||
2478 | 109 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, | ||
2479 | 110 | "printer-location", NULL, location.toUtf8()); | ||
2480 | 111 | } | ||
2481 | 112 | |||
2482 | 113 | return sendRequest(request, CupsResourceAdmin); | ||
2483 | 114 | } | ||
2484 | 115 | |||
2485 | 116 | bool IppClient::printerAddWithPpdFile(const QString &printerName, | ||
2486 | 117 | const QString &printerUri, | ||
2487 | 118 | const QString &ppdFileName, | ||
2488 | 119 | const QString &info, | ||
2489 | 120 | const QString &location) | ||
2490 | 121 | { | ||
2491 | 122 | ipp_t *request; | ||
2492 | 123 | |||
2493 | 124 | if (!isPrinterNameValid(printerName)) { | ||
2494 | 125 | setInternalStatus(QString("%1 is not a valid printer name.").arg(printerName)); | ||
2495 | 126 | return false; | ||
2496 | 127 | } | ||
2497 | 128 | |||
2498 | 129 | if (!isStringValid(info)) { | ||
2499 | 130 | setInternalStatus(QString("%1 is not a valid description.").arg(info)); | ||
2500 | 131 | return false; | ||
2501 | 132 | } | ||
2502 | 133 | |||
2503 | 134 | if (!isStringValid(location)) { | ||
2504 | 135 | setInternalStatus(QString("%1 is not a valid location.").arg(location)); | ||
2505 | 136 | return false; | ||
2506 | 137 | } | ||
2507 | 138 | |||
2508 | 139 | if (!isStringValid(ppdFileName)) { | ||
2509 | 140 | setInternalStatus(QString("%1 is not a valid ppd file name.").arg(ppdFileName)); | ||
2510 | 141 | return false; | ||
2511 | 142 | } | ||
2512 | 143 | |||
2513 | 144 | if (!isStringValid(printerUri)) { | ||
2514 | 145 | setInternalStatus(QString("%1 is not a valid printer uri.").arg(printerUri)); | ||
2515 | 146 | return false; | ||
2516 | 147 | } | ||
2517 | 148 | |||
2518 | 149 | request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER); | ||
2519 | 150 | addPrinterUri(request, printerName); | ||
2520 | 151 | addRequestingUsername(request, NULL); | ||
2521 | 152 | |||
2522 | 153 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | ||
2523 | 154 | "printer-name", NULL, printerName.toUtf8()); | ||
2524 | 155 | |||
2525 | 156 | /* In this specific case of ADD_MODIFY, the URI can be NULL/empty since | ||
2526 | 157 | * we provide a complete PPD. And cups fails if we pass an empty | ||
2527 | 158 | * string. */ | ||
2528 | 159 | if (!printerUri.isEmpty()) { | ||
2529 | 160 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, | ||
2530 | 161 | "device-uri", NULL, printerUri.toUtf8()); | ||
2531 | 162 | } | ||
2532 | 163 | |||
2533 | 164 | if (!info.isEmpty()) { | ||
2534 | 165 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, | ||
2535 | 166 | "printer-info", NULL, info.toUtf8()); | ||
2536 | 167 | } | ||
2537 | 168 | if (!location.isEmpty()) { | ||
2538 | 169 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, | ||
2539 | 170 | "printer-location", NULL, location.toUtf8()); | ||
2540 | 171 | } | ||
2541 | 172 | |||
2542 | 173 | return postRequest(request, ppdFileName.toUtf8(), CupsResourceAdmin); | ||
2543 | 174 | } | ||
2544 | 175 | |||
2545 | 176 | bool IppClient::printerSetDefault(const QString &printerName) | ||
2546 | 177 | { | ||
2547 | 178 | return sendNewSimpleRequest(CUPS_SET_DEFAULT, printerName.toUtf8(), | ||
2548 | 179 | CupsResource::CupsResourceAdmin); | ||
2549 | 180 | } | ||
2550 | 181 | |||
2551 | 182 | bool IppClient::printerSetEnabled(const QString &printerName, | ||
2552 | 183 | const bool enabled) | ||
2553 | 184 | { | ||
2554 | 185 | ipp_op_t op; | ||
2555 | 186 | op = enabled ? IPP_RESUME_PRINTER : IPP_PAUSE_PRINTER; | ||
2556 | 187 | return sendNewSimpleRequest(op, printerName, CupsResourceAdmin); | ||
2557 | 188 | } | ||
2558 | 189 | |||
2559 | 190 | /* reason must be empty if accept is true */ | ||
2560 | 191 | bool IppClient::printerSetAcceptJobs(const QString &printerName, | ||
2561 | 192 | const bool accept, | ||
2562 | 193 | const QString &reason) | ||
2563 | 194 | { | ||
2564 | 195 | ipp_t *request; | ||
2565 | 196 | |||
2566 | 197 | if (accept && !reason.isEmpty()) { | ||
2567 | 198 | setInternalStatus("Accepting jobs does not take a reason."); | ||
2568 | 199 | return false; | ||
2569 | 200 | } | ||
2570 | 201 | |||
2571 | 202 | if (!isPrinterNameValid(printerName)) { | ||
2572 | 203 | setInternalStatus(QString("%1 is not a valid printer name.").arg(printerName)); | ||
2573 | 204 | return false; | ||
2574 | 205 | } | ||
2575 | 206 | |||
2576 | 207 | if (!isStringValid(reason)) { | ||
2577 | 208 | setInternalStatus(QString("%1 is not a valid reason.").arg(reason)); | ||
2578 | 209 | return false; | ||
2579 | 210 | } | ||
2580 | 211 | |||
2581 | 212 | if (accept) { | ||
2582 | 213 | return sendNewSimpleRequest(CUPS_ACCEPT_JOBS, printerName.toUtf8(), | ||
2583 | 214 | CupsResourceAdmin); | ||
2584 | 215 | } else { | ||
2585 | 216 | request = ippNewRequest(CUPS_REJECT_JOBS); | ||
2586 | 217 | addPrinterUri(request, printerName); | ||
2587 | 218 | addRequestingUsername(request, NULL); | ||
2588 | 219 | |||
2589 | 220 | if (!reason.isEmpty()) | ||
2590 | 221 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, | ||
2591 | 222 | "printer-state-message", NULL, reason.toUtf8()); | ||
2592 | 223 | |||
2593 | 224 | return sendRequest(request, CupsResourceAdmin); | ||
2594 | 225 | } | ||
2595 | 226 | } | ||
2596 | 227 | |||
2597 | 228 | |||
2598 | 229 | bool IppClient::printerClassSetInfo(const QString &name, | ||
2599 | 230 | const QString &info) | ||
2600 | 231 | { | ||
2601 | 232 | if (!isPrinterNameValid(name)) { | ||
2602 | 233 | setInternalStatus(QString("%1 is not a valid printer name.").arg(name)); | ||
2603 | 234 | return false; | ||
2604 | 235 | } | ||
2605 | 236 | |||
2606 | 237 | if (!isStringValid(info)) { | ||
2607 | 238 | setInternalStatus(QString("%1 is not a valid description.").arg(info)); | ||
2608 | 239 | return false; | ||
2609 | 240 | } | ||
2610 | 241 | |||
2611 | 242 | return sendNewPrinterClassRequest(name, IPP_TAG_PRINTER, IPP_TAG_TEXT, | ||
2612 | 243 | "printer-info", info); | ||
2613 | 244 | } | ||
2614 | 245 | |||
2615 | 246 | bool IppClient::printerClassSetOption(const QString &name, | ||
2616 | 247 | const QString &option, | ||
2617 | 248 | const QStringList &values) | ||
2618 | 249 | { | ||
2619 | 250 | bool isClass; | ||
2620 | 251 | int length = 0; | ||
2621 | 252 | ipp_t *request; | ||
2622 | 253 | ipp_attribute_t *attr; | ||
2623 | 254 | QString newPpdFile; | ||
2624 | 255 | bool retval; | ||
2625 | 256 | |||
2626 | 257 | if (!isPrinterNameValid(name)) { | ||
2627 | 258 | setInternalStatus(QString("%1 is not a valid printer name.").arg(name)); | ||
2628 | 259 | return false; | ||
2629 | 260 | } | ||
2630 | 261 | |||
2631 | 262 | if (!isStringValid(option)) { | ||
2632 | 263 | setInternalStatus(QString("%1 is not a valid option.").arg(option)); | ||
2633 | 264 | return false; | ||
2634 | 265 | } | ||
2635 | 266 | |||
2636 | 267 | Q_FOREACH(const QString &val, values) { | ||
2637 | 268 | if (!isStringValid(val)) { | ||
2638 | 269 | setInternalStatus(QString("%1 is not a valid value.").arg(val)); | ||
2639 | 270 | return false; | ||
2640 | 271 | } | ||
2641 | 272 | length++; | ||
2642 | 273 | } | ||
2643 | 274 | |||
2644 | 275 | if (length == 0) { | ||
2645 | 276 | setInternalStatus("No valid values."); | ||
2646 | 277 | return false; | ||
2647 | 278 | } | ||
2648 | 279 | |||
2649 | 280 | isClass = printerIsClass(name); | ||
2650 | 281 | |||
2651 | 282 | /* We permit only one value to change in PPD file because we are setting | ||
2652 | 283 | * default value in it. */ | ||
2653 | 284 | if (!isClass && length == 1) { | ||
2654 | 285 | cups_option_t *options = NULL; | ||
2655 | 286 | int numOptions = 0; | ||
2656 | 287 | QString ppdfile; | ||
2657 | 288 | |||
2658 | 289 | numOptions = cupsAddOption(option.toUtf8(), | ||
2659 | 290 | values[0].toUtf8(), | ||
2660 | 291 | numOptions, &options); | ||
2661 | 292 | |||
2662 | 293 | ppdfile = QString(cupsGetPPD(name.toUtf8())); | ||
2663 | 294 | |||
2664 | 295 | newPpdFile = preparePpdForOptions(ppdfile.toUtf8(), | ||
2665 | 296 | options, numOptions).toLatin1().data(); | ||
2666 | 297 | |||
2667 | 298 | unlink(ppdfile.toUtf8()); | ||
2668 | 299 | cupsFreeOptions(numOptions, options); | ||
2669 | 300 | } | ||
2670 | 301 | |||
2671 | 302 | if (isClass) { | ||
2672 | 303 | request = ippNewRequest(CUPS_ADD_MODIFY_CLASS); | ||
2673 | 304 | addClassUri(request, name); | ||
2674 | 305 | } else { | ||
2675 | 306 | request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER); | ||
2676 | 307 | addPrinterUri(request, name); | ||
2677 | 308 | } | ||
2678 | 309 | |||
2679 | 310 | addRequestingUsername(request, NULL); | ||
2680 | 311 | |||
2681 | 312 | if (length == 1) { | ||
2682 | 313 | ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | ||
2683 | 314 | option.toUtf8(), | ||
2684 | 315 | NULL, | ||
2685 | 316 | values[0].toUtf8()); | ||
2686 | 317 | } else { | ||
2687 | 318 | int i; | ||
2688 | 319 | |||
2689 | 320 | attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME, | ||
2690 | 321 | option.toUtf8(), length, NULL, NULL); | ||
2691 | 322 | |||
2692 | 323 | for (i = 0; i < length; i++) | ||
2693 | 324 | ippSetString(request, &attr, i, values[i].toUtf8()); | ||
2694 | 325 | } | ||
2695 | 326 | |||
2696 | 327 | if (!newPpdFile.isEmpty()) { | ||
2697 | 328 | retval = postRequest(request, newPpdFile, CupsResourceAdmin); | ||
2698 | 329 | |||
2699 | 330 | unlink(newPpdFile.toUtf8()); | ||
2700 | 331 | // TODO: fix leak here. | ||
2701 | 332 | } else { | ||
2702 | 333 | retval = sendRequest(request, CupsResourceAdmin); | ||
2703 | 334 | } | ||
2704 | 335 | |||
2705 | 336 | return retval; | ||
2706 | 337 | } | ||
2707 | 338 | |||
2708 | 339 | QMap<QString, QVariant> IppClient::printerGetJobAttributes(const int jobId) | ||
2709 | 340 | { | ||
2710 | 341 | ipp_t *request; | ||
2711 | 342 | QMap<QString, QVariant> map; | ||
2712 | 343 | |||
2713 | 344 | // Construct request | ||
2714 | 345 | request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES); | ||
2715 | 346 | QString uri = QStringLiteral("ipp://localhost/jobs/") + QString::number(jobId); | ||
2716 | 347 | qDebug() << "URI:" << uri; | ||
2717 | 348 | |||
2718 | 349 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri.toStdString().data()); | ||
2719 | 350 | |||
2720 | 351 | |||
2721 | 352 | // Send request and construct reply | ||
2722 | 353 | ipp_t *reply; | ||
2723 | 354 | const QString resourceChar = getResource(CupsResourceRoot); | ||
2724 | 355 | reply = cupsDoRequest(m_connection, request, | ||
2725 | 356 | resourceChar.toUtf8()); | ||
2726 | 357 | |||
2727 | 358 | // Check if the reply is OK | ||
2728 | 359 | if (isReplyOk(reply, false)) { | ||
2729 | 360 | // Loop through the attributes | ||
2730 | 361 | ipp_attribute_t *attr; | ||
2731 | 362 | |||
2732 | 363 | for (attr = ippFirstAttribute(reply); attr; attr = ippNextAttribute(reply)) { | ||
2733 | 364 | QVariant value = getAttributeValue(attr); | ||
2734 | 365 | map.insert(ippGetName(attr), value); | ||
2735 | 366 | } | ||
2736 | 367 | } else { | ||
2737 | 368 | qWarning() << "Not able to get attributes of job:" << jobId; | ||
2738 | 369 | } | ||
2739 | 370 | |||
2740 | 371 | // Destruct the reply if valid | ||
2741 | 372 | if (reply) { | ||
2742 | 373 | ippDelete(reply); | ||
2743 | 374 | } | ||
2744 | 375 | |||
2745 | 376 | return map; | ||
2746 | 377 | } | ||
2747 | 378 | |||
2748 | 379 | |||
2749 | 380 | /* This function sets given options to specified values in file 'ppdfile'. | ||
2750 | 381 | * This needs to be done because of applications which use content of PPD files | ||
2751 | 382 | * instead of IPP attributes. | ||
2752 | 383 | * CUPS doesn't do this automatically (but hopefully will starting with 1.6) */ | ||
2753 | 384 | QString IppClient::preparePpdForOptions(const QString &ppdfile, | ||
2754 | 385 | cups_option_t *options, int numOptions) | ||
2755 | 386 | { | ||
2756 | 387 | auto ppdfile_c = ppdfile.toUtf8(); | ||
2757 | 388 | ppd_file_t *ppd; | ||
2758 | 389 | bool ppdchanged = false; | ||
2759 | 390 | QString result; | ||
2760 | 391 | QString error; | ||
2761 | 392 | char newppdfile[PATH_MAX]; | ||
2762 | 393 | cups_file_t *in = NULL; | ||
2763 | 394 | cups_file_t *out = NULL; | ||
2764 | 395 | char line[CPH_STR_MAXLEN]; | ||
2765 | 396 | char keyword[CPH_STR_MAXLEN]; | ||
2766 | 397 | char *keyptr; | ||
2767 | 398 | ppd_choice_t *choice; | ||
2768 | 399 | QString value; | ||
2769 | 400 | QLatin1String defaultStr("*Default"); | ||
2770 | 401 | |||
2771 | 402 | ppd = ppdOpenFile(ppdfile_c); | ||
2772 | 403 | if (!ppd) { | ||
2773 | 404 | error = QString("Unable to open PPD file \"%1\": %2") | ||
2774 | 405 | .arg(ppdfile).arg(strerror(errno)); | ||
2775 | 406 | setInternalStatus(error); | ||
2776 | 407 | goto out; | ||
2777 | 408 | } | ||
2778 | 409 | |||
2779 | 410 | in = cupsFileOpen(ppdfile_c, "r"); | ||
2780 | 411 | if (!in) { | ||
2781 | 412 | error = QString("Unable to open PPD file \"%1\": %2") | ||
2782 | 413 | .arg(ppdfile).arg(strerror(errno)); | ||
2783 | 414 | setInternalStatus(error); | ||
2784 | 415 | goto out; | ||
2785 | 416 | } | ||
2786 | 417 | |||
2787 | 418 | out = cupsTempFile2(newppdfile, sizeof(newppdfile)); | ||
2788 | 419 | if (!out) { | ||
2789 | 420 | setInternalStatus("Unable to create temporary file"); | ||
2790 | 421 | goto out; | ||
2791 | 422 | } | ||
2792 | 423 | |||
2793 | 424 | /* Mark default values and values of options we are changing. */ | ||
2794 | 425 | ppdMarkDefaults(ppd); | ||
2795 | 426 | cupsMarkOptions(ppd, numOptions, options); | ||
2796 | 427 | |||
2797 | 428 | while (cupsFileGets(in, line, sizeof(line))) { | ||
2798 | 429 | QString line_qs(line); | ||
2799 | 430 | if (!line_qs.startsWith(defaultStr)) { | ||
2800 | 431 | cupsFilePrintf(out, "%s\n", line); | ||
2801 | 432 | } else { | ||
2802 | 433 | /* This part parses lines with *Default on their | ||
2803 | 434 | * beginning. For instance: | ||
2804 | 435 | * "*DefaultResolution: 1200dpi" becomes: | ||
2805 | 436 | * - keyword: Resolution | ||
2806 | 437 | * - keyptr: 1200dpi | ||
2807 | 438 | */ | ||
2808 | 439 | strncpy(keyword, line + defaultStr.size(), sizeof(keyword)); | ||
2809 | 440 | |||
2810 | 441 | for (keyptr = keyword; *keyptr; keyptr++) | ||
2811 | 442 | if (*keyptr == ':' || isspace (*keyptr & 255)) | ||
2812 | 443 | break; | ||
2813 | 444 | |||
2814 | 445 | *keyptr++ = '\0'; | ||
2815 | 446 | while (isspace (*keyptr & 255)) | ||
2816 | 447 | keyptr++; | ||
2817 | 448 | |||
2818 | 449 | QString keyword_sq(keyword); | ||
2819 | 450 | QString keyptr_qs(keyptr); | ||
2820 | 451 | |||
2821 | 452 | /* We have to change PageSize if any of PageRegion, | ||
2822 | 453 | * PageSize, PaperDimension or ImageableArea changes. | ||
2823 | 454 | * We change PageRegion if PageSize is not available. */ | ||
2824 | 455 | if (keyword_sq == "PageRegion" || | ||
2825 | 456 | keyword_sq == "PageSize" || | ||
2826 | 457 | keyword_sq == "PaperDimension" || | ||
2827 | 458 | keyword_sq == "ImageableArea") { | ||
2828 | 459 | |||
2829 | 460 | choice = ppdFindMarkedChoice(ppd, "PageSize"); | ||
2830 | 461 | if (!choice) | ||
2831 | 462 | choice = ppdFindMarkedChoice(ppd, "PageRegion"); | ||
2832 | 463 | } else { | ||
2833 | 464 | choice = ppdFindMarkedChoice(ppd, keyword); | ||
2834 | 465 | } | ||
2835 | 466 | |||
2836 | 467 | |||
2837 | 468 | QString choice_qs; | ||
2838 | 469 | if (choice) { | ||
2839 | 470 | choice_qs = choice->choice; | ||
2840 | 471 | } | ||
2841 | 472 | |||
2842 | 473 | if (choice && choice_qs != keyptr_qs) { | ||
2843 | 474 | /* We have to set the value in PPD manually if | ||
2844 | 475 | * a custom value was passed in: | ||
2845 | 476 | * cupsMarkOptions() marks the choice as | ||
2846 | 477 | * "Custom". We want to set this value with our | ||
2847 | 478 | * input. */ | ||
2848 | 479 | if (choice_qs != "Custom") { | ||
2849 | 480 | cupsFilePrintf(out, | ||
2850 | 481 | "*Default%s: %s\n", | ||
2851 | 482 | keyword, | ||
2852 | 483 | choice->choice); | ||
2853 | 484 | ppdchanged = true; | ||
2854 | 485 | } else { | ||
2855 | 486 | value = cupsGetOption(keyword, numOptions, options); | ||
2856 | 487 | if (!value.isEmpty()) { | ||
2857 | 488 | cupsFilePrintf(out, | ||
2858 | 489 | "*Default%s: %s\n", | ||
2859 | 490 | keyword, | ||
2860 | 491 | value.toStdString().c_str()); | ||
2861 | 492 | ppdchanged = true; | ||
2862 | 493 | } else { | ||
2863 | 494 | cupsFilePrintf(out, "%s\n", line); | ||
2864 | 495 | } | ||
2865 | 496 | } | ||
2866 | 497 | } else { | ||
2867 | 498 | cupsFilePrintf(out, "%s\n", line); | ||
2868 | 499 | } | ||
2869 | 500 | } | ||
2870 | 501 | } | ||
2871 | 502 | |||
2872 | 503 | if (ppdchanged) | ||
2873 | 504 | result = QString::fromUtf8(newppdfile); | ||
2874 | 505 | else | ||
2875 | 506 | unlink(newppdfile); | ||
2876 | 507 | |||
2877 | 508 | out: | ||
2878 | 509 | if (in) | ||
2879 | 510 | cupsFileClose(in); | ||
2880 | 511 | if (out) | ||
2881 | 512 | cupsFileClose(out); | ||
2882 | 513 | if (ppd) | ||
2883 | 514 | ppdClose(ppd); | ||
2884 | 515 | |||
2885 | 516 | return result; | ||
2886 | 517 | } | ||
2887 | 518 | |||
2888 | 519 | |||
2889 | 520 | bool IppClient::sendNewPrinterClassRequest(const QString &printerName, | ||
2890 | 521 | ipp_tag_t group, ipp_tag_t type, | ||
2891 | 522 | const QString &name, | ||
2892 | 523 | const QString &value) | ||
2893 | 524 | { | ||
2894 | 525 | ipp_t *request; | ||
2895 | 526 | |||
2896 | 527 | request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER); | ||
2897 | 528 | addPrinterUri(request, printerName); | ||
2898 | 529 | addRequestingUsername(request, QString()); | ||
2899 | 530 | ippAddString(request, group, type, name.toUtf8(), NULL, | ||
2900 | 531 | value.toUtf8()); | ||
2901 | 532 | |||
2902 | 533 | if (sendRequest(request, CupsResource::CupsResourceAdmin)) | ||
2903 | 534 | return true; | ||
2904 | 535 | |||
2905 | 536 | // it failed, maybe it was a class? | ||
2906 | 537 | if (m_lastStatus != IPP_NOT_POSSIBLE) { | ||
2907 | 538 | return false; | ||
2908 | 539 | } | ||
2909 | 540 | |||
2910 | 541 | // TODO: implement class modification <here>. | ||
2911 | 542 | return false; | ||
2912 | 543 | } | ||
2913 | 544 | |||
2914 | 545 | void IppClient::addPrinterUri(ipp_t *request, const QString &name) | ||
2915 | 546 | { | ||
2916 | 547 | QUrl uri(QString("ipp://localhost/printers/%1").arg(name)); | ||
2917 | 548 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | ||
2918 | 549 | "printer-uri", NULL, uri.toEncoded().data()); | ||
2919 | 550 | } | ||
2920 | 551 | |||
2921 | 552 | void IppClient::addRequestingUsername(ipp_t *request, const QString &username) | ||
2922 | 553 | { | ||
2923 | 554 | if (!username.isEmpty()) | ||
2924 | 555 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | ||
2925 | 556 | "requesting-user-name", NULL, | ||
2926 | 557 | username.toUtf8()); | ||
2927 | 558 | else | ||
2928 | 559 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | ||
2929 | 560 | "requesting-user-name", NULL, cupsUser()); | ||
2930 | 561 | } | ||
2931 | 562 | |||
2932 | 563 | QString IppClient::getLastError() const | ||
2933 | 564 | { | ||
2934 | 565 | return m_internalStatus; | ||
2935 | 566 | } | ||
2936 | 567 | |||
2937 | 568 | const QString IppClient::getResource(const IppClient::CupsResource &resource) | ||
2938 | 569 | { | ||
2939 | 570 | switch (resource) { | ||
2940 | 571 | case CupsResourceRoot: | ||
2941 | 572 | return "/"; | ||
2942 | 573 | case CupsResourceAdmin: | ||
2943 | 574 | return "/admin/"; | ||
2944 | 575 | case CupsResourceJobs: | ||
2945 | 576 | return "/jobs/"; | ||
2946 | 577 | default: | ||
2947 | 578 | qCritical("Asking for a resource with no match."); | ||
2948 | 579 | return "/"; | ||
2949 | 580 | } | ||
2950 | 581 | } | ||
2951 | 582 | |||
2952 | 583 | bool IppClient::isPrinterNameValid(const QString &name) | ||
2953 | 584 | { | ||
2954 | 585 | int i; | ||
2955 | 586 | int len; | ||
2956 | 587 | |||
2957 | 588 | /* Quoting the lpadmin man page: | ||
2958 | 589 | * CUPS allows printer names to contain any printable character | ||
2959 | 590 | * except SPACE, TAB, "/", or "#". | ||
2960 | 591 | * On top of that, validate_name() in lpadmin.c (from cups) checks that | ||
2961 | 592 | * the string is 127 characters long, or shorter. */ | ||
2962 | 593 | |||
2963 | 594 | /* no empty string */ | ||
2964 | 595 | if (name.isEmpty()) | ||
2965 | 596 | return false; | ||
2966 | 597 | |||
2967 | 598 | len = name.size(); | ||
2968 | 599 | /* no string that is too long; see comment at the beginning of the | ||
2969 | 600 | * validation code block */ | ||
2970 | 601 | if (len > 127) | ||
2971 | 602 | return false; | ||
2972 | 603 | |||
2973 | 604 | /* only printable characters, no space, no /, no # */ | ||
2974 | 605 | for (i = 0; i < len; i++) { | ||
2975 | 606 | const QChar c = name.at(i); | ||
2976 | 607 | if (!c.isPrint()) | ||
2977 | 608 | return false; | ||
2978 | 609 | if (c.isSpace()) | ||
2979 | 610 | return false; | ||
2980 | 611 | if (c == '/' || c == '#') | ||
2981 | 612 | return false; | ||
2982 | 613 | } | ||
2983 | 614 | return true; | ||
2984 | 615 | } | ||
2985 | 616 | |||
2986 | 617 | bool IppClient::isStringValid(const QString &string, const bool checkNull, | ||
2987 | 618 | const int maxLength) | ||
2988 | 619 | { | ||
2989 | 620 | if (isStringPrintable(string, checkNull, maxLength)) | ||
2990 | 621 | return true; | ||
2991 | 622 | return false; | ||
2992 | 623 | } | ||
2993 | 624 | |||
2994 | 625 | bool IppClient::isStringPrintable(const QString &string, const bool checkNull, | ||
2995 | 626 | const int maxLength) | ||
2996 | 627 | { | ||
2997 | 628 | int i; | ||
2998 | 629 | int len; | ||
2999 | 630 | |||
3000 | 631 | /* no null string */ | ||
3001 | 632 | if (string.isNull()) | ||
3002 | 633 | return !checkNull; | ||
3003 | 634 | |||
3004 | 635 | len = string.size(); | ||
3005 | 636 | if (maxLength > 0 && len > maxLength) | ||
3006 | 637 | return false; | ||
3007 | 638 | |||
3008 | 639 | /* only printable characters */ | ||
3009 | 640 | for (i = 0; i < len; i++) { | ||
3010 | 641 | const QChar c = string.at(i); | ||
3011 | 642 | if (!c.isPrint()) | ||
3012 | 643 | return false; | ||
3013 | 644 | } | ||
3014 | 645 | return true; | ||
3015 | 646 | } | ||
3016 | 647 | |||
3017 | 648 | void IppClient::setInternalStatus(const QString &status) | ||
3018 | 649 | { | ||
3019 | 650 | if (!m_internalStatus.isNull()) { | ||
3020 | 651 | m_internalStatus = QString::null; | ||
3021 | 652 | } | ||
3022 | 653 | |||
3023 | 654 | if (status.isNull()) { | ||
3024 | 655 | m_internalStatus = QString::null; | ||
3025 | 656 | } else { | ||
3026 | 657 | m_internalStatus = status; | ||
3027 | 658 | |||
3028 | 659 | // Only used for errors for now. | ||
3029 | 660 | qCritical() << status; | ||
3030 | 661 | } | ||
3031 | 662 | } | ||
3032 | 663 | |||
3033 | 664 | bool IppClient::postRequest(ipp_t *request, const QString &file, | ||
3034 | 665 | const CupsResource &resource) | ||
3035 | 666 | { | ||
3036 | 667 | ipp_t *reply; | ||
3037 | 668 | QString resourceChar; | ||
3038 | 669 | |||
3039 | 670 | resourceChar = getResource(resource); | ||
3040 | 671 | |||
3041 | 672 | if (!file.isEmpty()) | ||
3042 | 673 | reply = cupsDoFileRequest(m_connection, request, resourceChar.toUtf8(), | ||
3043 | 674 | file.toUtf8()); | ||
3044 | 675 | else | ||
3045 | 676 | reply = cupsDoFileRequest(m_connection, request, resourceChar.toUtf8(), | ||
3046 | 677 | NULL); | ||
3047 | 678 | |||
3048 | 679 | return handleReply(reply); | ||
3049 | 680 | } | ||
3050 | 681 | |||
3051 | 682 | |||
3052 | 683 | bool IppClient::sendRequest(ipp_t *request, const CupsResource &resource) | ||
3053 | 684 | { | ||
3054 | 685 | ipp_t *reply; | ||
3055 | 686 | const QString resourceChar = getResource(resource); | ||
3056 | 687 | reply = cupsDoRequest(m_connection, request, | ||
3057 | 688 | resourceChar.toUtf8()); | ||
3058 | 689 | return handleReply(reply); | ||
3059 | 690 | } | ||
3060 | 691 | |||
3061 | 692 | bool IppClient::sendNewSimpleRequest(ipp_op_t op, const QString &printerName, | ||
3062 | 693 | const IppClient::CupsResource &resource) | ||
3063 | 694 | { | ||
3064 | 695 | ipp_t *request; | ||
3065 | 696 | |||
3066 | 697 | if (!isPrinterNameValid(printerName)) | ||
3067 | 698 | return false; | ||
3068 | 699 | |||
3069 | 700 | request = ippNewRequest(op); | ||
3070 | 701 | addPrinterUri(request, printerName); | ||
3071 | 702 | addRequestingUsername(request, NULL); | ||
3072 | 703 | |||
3073 | 704 | return sendRequest(request, resource); | ||
3074 | 705 | } | ||
3075 | 706 | |||
3076 | 707 | bool IppClient::handleReply(ipp_t *reply) | ||
3077 | 708 | { | ||
3078 | 709 | bool retval; | ||
3079 | 710 | retval = isReplyOk(reply, false); | ||
3080 | 711 | if (reply) | ||
3081 | 712 | ippDelete(reply); | ||
3082 | 713 | |||
3083 | 714 | return retval; | ||
3084 | 715 | } | ||
3085 | 716 | |||
3086 | 717 | bool IppClient::isReplyOk(ipp_t *reply, bool deleteIfReplyNotOk) | ||
3087 | 718 | { | ||
3088 | 719 | /* reset the internal status: we'll use the cups status */ | ||
3089 | 720 | m_lastStatus = IPP_STATUS_CUPS_INVALID; | ||
3090 | 721 | |||
3091 | 722 | if (reply && ippGetStatusCode(reply) <= IPP_OK_CONFLICT) { | ||
3092 | 723 | m_lastStatus = IPP_OK; | ||
3093 | 724 | return true; | ||
3094 | 725 | } else { | ||
3095 | 726 | setErrorFromReply(reply); | ||
3096 | 727 | qWarning() << Q_FUNC_INFO << "Cups HTTP error:" << cupsLastErrorString(); | ||
3097 | 728 | |||
3098 | 729 | if (deleteIfReplyNotOk && reply) | ||
3099 | 730 | ippDelete(reply); | ||
3100 | 731 | |||
3101 | 732 | return false; | ||
3102 | 733 | } | ||
3103 | 734 | } | ||
3104 | 735 | |||
3105 | 736 | void IppClient::setErrorFromReply(ipp_t *reply) | ||
3106 | 737 | { | ||
3107 | 738 | if (reply) | ||
3108 | 739 | m_lastStatus = ippGetStatusCode(reply); | ||
3109 | 740 | else | ||
3110 | 741 | m_lastStatus = cupsLastError(); | ||
3111 | 742 | } | ||
3112 | 743 | |||
3113 | 744 | bool IppClient::printerIsClass(const QString &name) | ||
3114 | 745 | { | ||
3115 | 746 | const char * const attrs[1] = { "member-names" }; | ||
3116 | 747 | ipp_t *request; | ||
3117 | 748 | QString resource; | ||
3118 | 749 | ipp_t *reply; | ||
3119 | 750 | bool retval; | ||
3120 | 751 | |||
3121 | 752 | // Class/Printer name validation is equal. | ||
3122 | 753 | if (!isPrinterNameValid(name)) { | ||
3123 | 754 | setInternalStatus(QString("%1 is not a valid printer name.").arg(name)); | ||
3124 | 755 | return false; | ||
3125 | 756 | } | ||
3126 | 757 | |||
3127 | 758 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | ||
3128 | 759 | addClassUri(request, name); | ||
3129 | 760 | addRequestingUsername(request, QString()); | ||
3130 | 761 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | ||
3131 | 762 | "requested-attributes", 1, NULL, attrs); | ||
3132 | 763 | |||
3133 | 764 | resource = getResource(CupsResource::CupsResourceRoot); | ||
3134 | 765 | reply = cupsDoRequest(m_connection, request, resource.toUtf8()); | ||
3135 | 766 | |||
3136 | 767 | if (!isReplyOk(reply, true)) | ||
3137 | 768 | return true; | ||
3138 | 769 | |||
3139 | 770 | /* Note: we need to look if the attribute is there, since we get a | ||
3140 | 771 | * reply if the name is a printer name and not a class name. The | ||
3141 | 772 | * attribute is the only way to distinguish the two cases. */ | ||
3142 | 773 | retval = ippFindAttribute(reply, attrs[0], IPP_TAG_NAME) != NULL; | ||
3143 | 774 | |||
3144 | 775 | if (reply) | ||
3145 | 776 | ippDelete(reply); | ||
3146 | 777 | |||
3147 | 778 | return retval; | ||
3148 | 779 | } | ||
3149 | 780 | |||
3150 | 781 | void IppClient::addClassUri(ipp_t *request, const QString &name) | ||
3151 | 782 | { | ||
3152 | 783 | QUrl uri(QString("ipp://localhost/printers/%1").arg(name)); | ||
3153 | 784 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | ||
3154 | 785 | "printer-uri", NULL, uri.toEncoded().data()); | ||
3155 | 786 | } | ||
3156 | 787 | |||
3157 | 788 | ppd_file_t* IppClient::getPpdFile(const QString &name, | ||
3158 | 789 | const QString &instance) const | ||
3159 | 790 | { | ||
3160 | 791 | Q_UNUSED(instance); | ||
3161 | 792 | |||
3162 | 793 | ppd_file_t* file = 0; | ||
3163 | 794 | const char *ppdFile = cupsGetPPD(name.toUtf8()); | ||
3164 | 795 | if (ppdFile) { | ||
3165 | 796 | file = ppdOpenFile(ppdFile); | ||
3166 | 797 | unlink(ppdFile); | ||
3167 | 798 | } | ||
3168 | 799 | if (file) { | ||
3169 | 800 | ppdMarkDefaults(file); | ||
3170 | 801 | } else { | ||
3171 | 802 | file = 0; | ||
3172 | 803 | } | ||
3173 | 804 | |||
3174 | 805 | return file; | ||
3175 | 806 | } | ||
3176 | 807 | |||
3177 | 808 | cups_dest_t* IppClient::getDest(const QString &name, | ||
3178 | 809 | const QString &instance) const | ||
3179 | 810 | { | ||
3180 | 811 | cups_dest_t *dest = 0; | ||
3181 | 812 | |||
3182 | 813 | if (instance.isEmpty()) { | ||
3183 | 814 | dest = cupsGetNamedDest(m_connection, name.toUtf8(), NULL); | ||
3184 | 815 | } else { | ||
3185 | 816 | dest = cupsGetNamedDest(m_connection, name.toUtf8(), instance.toUtf8()); | ||
3186 | 817 | } | ||
3187 | 818 | return dest; | ||
3188 | 819 | } | ||
3189 | 820 | |||
3190 | 821 | ipp_t* IppClient::createPrinterDriversRequest( | ||
3191 | 822 | const QString &deviceId, const QString &language, const QString &makeModel, | ||
3192 | 823 | const QString &product, const QStringList &includeSchemes, | ||
3193 | 824 | const QStringList &excludeSchemes | ||
3194 | 825 | ) | ||
3195 | 826 | { | ||
3196 | 827 | Q_UNUSED(includeSchemes); | ||
3197 | 828 | Q_UNUSED(excludeSchemes); | ||
3198 | 829 | |||
3199 | 830 | ipp_t *request; | ||
3200 | 831 | |||
3201 | 832 | request = ippNewRequest(CUPS_GET_PPDS); | ||
3202 | 833 | |||
3203 | 834 | if (!deviceId.isEmpty()) | ||
3204 | 835 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-device-id", | ||
3205 | 836 | NULL, deviceId.toUtf8()); | ||
3206 | 837 | if (!language.isEmpty()) | ||
3207 | 838 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, "ppd-language", | ||
3208 | 839 | NULL, language.toUtf8()); | ||
3209 | 840 | if (!makeModel.isEmpty()) | ||
3210 | 841 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-make-and-model", | ||
3211 | 842 | NULL, makeModel.toUtf8()); | ||
3212 | 843 | if (!product.isEmpty()) | ||
3213 | 844 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-product", | ||
3214 | 845 | NULL, product.toUtf8()); | ||
3215 | 846 | |||
3216 | 847 | // Do the request and get return the response. | ||
3217 | 848 | const QString resourceChar = getResource(CupsResourceRoot); | ||
3218 | 849 | return cupsDoRequest(m_connection, request, | ||
3219 | 850 | resourceChar.toUtf8()); | ||
3220 | 851 | } | ||
3221 | 852 | |||
3222 | 853 | int IppClient::createSubscription() | ||
3223 | 854 | { | ||
3224 | 855 | ipp_t *req; | ||
3225 | 856 | ipp_t *resp; | ||
3226 | 857 | ipp_attribute_t *attr; | ||
3227 | 858 | int subscriptionId = -1; | ||
3228 | 859 | |||
3229 | 860 | req = ippNewRequest(IPP_CREATE_PRINTER_SUBSCRIPTION); | ||
3230 | 861 | ippAddString(req, IPP_TAG_OPERATION, IPP_TAG_URI, | ||
3231 | 862 | "printer-uri", NULL, "/"); | ||
3232 | 863 | ippAddString(req, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD, | ||
3233 | 864 | "notify-events", NULL, "all"); | ||
3234 | 865 | ippAddString(req, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI, | ||
3235 | 866 | "notify-recipient-uri", NULL, "dbus://"); | ||
3236 | 867 | ippAddInteger(req, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER, | ||
3237 | 868 | "notify-lease-duration", 0); | ||
3238 | 869 | |||
3239 | 870 | resp = cupsDoRequest(m_connection, req, | ||
3240 | 871 | getResource(CupsResourceRoot).toUtf8()); | ||
3241 | 872 | if (!isReplyOk(resp, true)) { | ||
3242 | 873 | return subscriptionId; | ||
3243 | 874 | } | ||
3244 | 875 | |||
3245 | 876 | attr = ippFindAttribute(resp, "notify-subscription-id", IPP_TAG_INTEGER); | ||
3246 | 877 | |||
3247 | 878 | if (!attr) { | ||
3248 | 879 | qWarning() << "ipp-create-printer-subscription response doesn't" | ||
3249 | 880 | " contain subscription id."; | ||
3250 | 881 | } else { | ||
3251 | 882 | subscriptionId = ippGetInteger(attr, 0); | ||
3252 | 883 | } | ||
3253 | 884 | |||
3254 | 885 | ippDelete (resp); | ||
3255 | 886 | |||
3256 | 887 | return subscriptionId; | ||
3257 | 888 | } | ||
3258 | 889 | |||
3259 | 890 | void IppClient::cancelSubscription(const int &subscriptionId) | ||
3260 | 891 | { | ||
3261 | 892 | ipp_t *req; | ||
3262 | 893 | ipp_t *resp; | ||
3263 | 894 | |||
3264 | 895 | if (subscriptionId <= 0) { | ||
3265 | 896 | return; | ||
3266 | 897 | } | ||
3267 | 898 | |||
3268 | 899 | req = ippNewRequest(IPP_CANCEL_SUBSCRIPTION); | ||
3269 | 900 | ippAddString(req, IPP_TAG_OPERATION, IPP_TAG_URI, | ||
3270 | 901 | "printer-uri", NULL, "/"); | ||
3271 | 902 | ippAddInteger(req, IPP_TAG_OPERATION, IPP_TAG_INTEGER, | ||
3272 | 903 | "notify-subscription-id", subscriptionId); | ||
3273 | 904 | |||
3274 | 905 | resp = cupsDoRequest(m_connection, req, | ||
3275 | 906 | getResource(CupsResourceRoot).toUtf8()); | ||
3276 | 907 | if (!isReplyOk(resp, true)) { | ||
3277 | 908 | return; | ||
3278 | 909 | } | ||
3279 | 910 | |||
3280 | 911 | ippDelete(resp); | ||
3281 | 912 | } | ||
3282 | 913 | |||
3283 | 914 | QVariant IppClient::getAttributeValue(ipp_attribute_t *attr, int index) const | ||
3284 | 915 | { | ||
3285 | 916 | QVariant var; | ||
3286 | 917 | |||
3287 | 918 | if (ippGetCount(attr) > 1 && index < 0) { | ||
3288 | 919 | QList<QVariant> list; | ||
3289 | 920 | |||
3290 | 921 | for (int i=0; i < ippGetCount(attr); i++) { | ||
3291 | 922 | list.append(getAttributeValue(attr, i)); | ||
3292 | 923 | } | ||
3293 | 924 | |||
3294 | 925 | var = QVariant::fromValue<QList<QVariant>>(list); | ||
3295 | 926 | } else { | ||
3296 | 927 | if (index == -1) { | ||
3297 | 928 | index = 0; | ||
3298 | 929 | } | ||
3299 | 930 | |||
3300 | 931 | switch (ippGetValueTag(attr)) { | ||
3301 | 932 | case IPP_TAG_NAME: | ||
3302 | 933 | case IPP_TAG_TEXT: | ||
3303 | 934 | case IPP_TAG_KEYWORD: | ||
3304 | 935 | case IPP_TAG_URI: | ||
3305 | 936 | case IPP_TAG_CHARSET: | ||
3306 | 937 | case IPP_TAG_MIMETYPE: | ||
3307 | 938 | case IPP_TAG_LANGUAGE: | ||
3308 | 939 | var = QVariant::fromValue<QString>(ippGetString(attr, index, NULL)); | ||
3309 | 940 | break; | ||
3310 | 941 | case IPP_TAG_INTEGER: | ||
3311 | 942 | case IPP_TAG_ENUM: | ||
3312 | 943 | var = QVariant::fromValue<int>(ippGetInteger(attr, index)); | ||
3313 | 944 | break; | ||
3314 | 945 | case IPP_TAG_BOOLEAN: | ||
3315 | 946 | var = QVariant::fromValue<bool>(ippGetBoolean(attr, index)); | ||
3316 | 947 | break; | ||
3317 | 948 | case IPP_TAG_RANGE: { | ||
3318 | 949 | QString range; | ||
3319 | 950 | int upper; | ||
3320 | 951 | int lower = ippGetRange(attr, index, &upper); | ||
3321 | 952 | |||
3322 | 953 | // Build a string similar to "1-3" "5-" "8" "-4" | ||
3323 | 954 | if (lower != INT_MIN) { | ||
3324 | 955 | range += QString::number(lower); | ||
3325 | 956 | } | ||
3326 | 957 | |||
3327 | 958 | if (lower != upper) { | ||
3328 | 959 | range += QStringLiteral("-"); | ||
3329 | 960 | |||
3330 | 961 | if (upper != INT_MAX) { | ||
3331 | 962 | range += QString::number(upper); | ||
3332 | 963 | } | ||
3333 | 964 | } | ||
3334 | 965 | |||
3335 | 966 | var = QVariant(range); | ||
3336 | 967 | break; | ||
3337 | 968 | } | ||
3338 | 969 | case IPP_TAG_NOVALUE: | ||
3339 | 970 | var = QVariant(); | ||
3340 | 971 | break; | ||
3341 | 972 | case IPP_TAG_DATE: { | ||
3342 | 973 | time_t time = ippDateToTime(ippGetDate(attr, index)); | ||
3343 | 974 | QDateTime datetime; | ||
3344 | 975 | datetime.setTimeZone(QTimeZone::systemTimeZone()); | ||
3345 | 976 | datetime.setTime_t(time); | ||
3346 | 977 | |||
3347 | 978 | var = QVariant::fromValue<QDateTime>(datetime); | ||
3348 | 979 | break; | ||
3349 | 980 | } | ||
3350 | 981 | default: | ||
3351 | 982 | qWarning() << "Unknown IPP value tab 0x" << ippGetValueTag(attr); | ||
3352 | 983 | break; | ||
3353 | 984 | } | ||
3354 | 985 | } | ||
3355 | 986 | |||
3356 | 987 | return var; | ||
3357 | 988 | } | ||
3358 | 0 | 989 | ||
3359 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/ippclient.h' | |||
3360 | --- modules/Ubuntu/Components/Extras/Printers/cups/ippclient.h 1970-01-01 00:00:00 +0000 | |||
3361 | +++ modules/Ubuntu/Components/Extras/Printers/cups/ippclient.h 2017-02-24 12:29:37 +0000 | |||
3362 | @@ -0,0 +1,121 @@ | |||
3363 | 1 | /* | ||
3364 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3365 | 3 | * | ||
3366 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3367 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3368 | 6 | * the Free Software Foundation; version 3. | ||
3369 | 7 | * | ||
3370 | 8 | * This program is distributed in the hope that it will be useful, | ||
3371 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3372 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3373 | 11 | * GNU Lesser General Public License for more details. | ||
3374 | 12 | * | ||
3375 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3376 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3377 | 15 | */ | ||
3378 | 16 | |||
3379 | 17 | #ifndef USC_PRINTERS_CUPS_IPPCLIENT_H | ||
3380 | 18 | #define USC_PRINTERS_CUPS_IPPCLIENT_H | ||
3381 | 19 | |||
3382 | 20 | #include "structs.h" | ||
3383 | 21 | |||
3384 | 22 | #include <cups/cups.h> | ||
3385 | 23 | #include <cups/http.h> | ||
3386 | 24 | #include <cups/ipp.h> | ||
3387 | 25 | #include <cups/ppd.h> | ||
3388 | 26 | |||
3389 | 27 | #include <QString> | ||
3390 | 28 | #include <QStringList> | ||
3391 | 29 | |||
3392 | 30 | /* From https://bugzilla.novell.com/show_bug.cgi?id=447444#c5 | ||
3393 | 31 | * We need to define a maximum length for strings to avoid cups | ||
3394 | 32 | * thinking there are multiple lines. | ||
3395 | 33 | */ | ||
3396 | 34 | #define CPH_STR_MAXLEN 512 | ||
3397 | 35 | |||
3398 | 36 | class IppClient | ||
3399 | 37 | { | ||
3400 | 38 | public: | ||
3401 | 39 | explicit IppClient(); | ||
3402 | 40 | ~IppClient(); | ||
3403 | 41 | |||
3404 | 42 | bool printerDelete(const QString &printerName); | ||
3405 | 43 | bool printerAdd(const QString &printerName, | ||
3406 | 44 | const QString &printerUri, | ||
3407 | 45 | const QString &ppdFile, | ||
3408 | 46 | const QString &info, | ||
3409 | 47 | const QString &location); | ||
3410 | 48 | |||
3411 | 49 | bool printerAddWithPpdFile(const QString &printerName, | ||
3412 | 50 | const QString &printerUri, | ||
3413 | 51 | const QString &ppdFileName, | ||
3414 | 52 | const QString &info, | ||
3415 | 53 | const QString &location); | ||
3416 | 54 | bool printerSetDefault(const QString &printerName); | ||
3417 | 55 | bool printerSetEnabled(const QString &printerName, const bool enabled); | ||
3418 | 56 | bool printerSetAcceptJobs(const QString &printerName, const bool accept, | ||
3419 | 57 | const QString &reason); | ||
3420 | 58 | bool printerClassSetInfo(const QString &name, const QString &info); | ||
3421 | 59 | bool printerClassSetOption(const QString &name, const QString &option, | ||
3422 | 60 | const QStringList &values); | ||
3423 | 61 | ppd_file_t* getPpdFile(const QString &name, const QString &instance) const; | ||
3424 | 62 | cups_dest_t* getDest(const QString &name, const QString &instance) const; | ||
3425 | 63 | |||
3426 | 64 | QMap<QString, QVariant> printerGetJobAttributes(const int jobId); | ||
3427 | 65 | |||
3428 | 66 | QString getLastError() const; | ||
3429 | 67 | |||
3430 | 68 | // Note: This response needs to be free by the caller. | ||
3431 | 69 | ipp_t* createPrinterDriversRequest( | ||
3432 | 70 | const QString &deviceId, const QString &language, | ||
3433 | 71 | const QString &makeModel, const QString &product, | ||
3434 | 72 | const QStringList &includeSchemes, const QStringList &excludeSchemes | ||
3435 | 73 | ); | ||
3436 | 74 | int createSubscription(); | ||
3437 | 75 | void cancelSubscription(const int &subscriptionId); | ||
3438 | 76 | |||
3439 | 77 | private: | ||
3440 | 78 | enum CupsResource | ||
3441 | 79 | { | ||
3442 | 80 | CupsResourceRoot = 0, | ||
3443 | 81 | CupsResourceAdmin, | ||
3444 | 82 | CupsResourceJobs, | ||
3445 | 83 | }; | ||
3446 | 84 | |||
3447 | 85 | bool sendNewPrinterClassRequest(const QString &printerName, | ||
3448 | 86 | ipp_tag_t group, | ||
3449 | 87 | ipp_tag_t type, | ||
3450 | 88 | const QString &name, | ||
3451 | 89 | const QString &value); | ||
3452 | 90 | static void addPrinterUri(ipp_t *request, const QString &name); | ||
3453 | 91 | static void addRequestingUsername(ipp_t *request, const QString &username); | ||
3454 | 92 | static const QString getResource(const CupsResource &resource); | ||
3455 | 93 | static bool isPrinterNameValid(const QString &name); | ||
3456 | 94 | static void addClassUri(ipp_t *request, const QString &name); | ||
3457 | 95 | static bool isStringValid(const QString &string, | ||
3458 | 96 | const bool checkNull = false, | ||
3459 | 97 | const int maxLength = 512); | ||
3460 | 98 | static bool isStringPrintable(const QString &string, const bool checkNull, | ||
3461 | 99 | const int maxLength); | ||
3462 | 100 | QString preparePpdForOptions(const QString &ppdfile, | ||
3463 | 101 | cups_option_t *options, | ||
3464 | 102 | int numOptions); | ||
3465 | 103 | bool printerIsClass(const QString &name); | ||
3466 | 104 | void setInternalStatus(const QString &status); | ||
3467 | 105 | bool postRequest(ipp_t *request, const QString &file, | ||
3468 | 106 | const CupsResource &resource); | ||
3469 | 107 | bool sendRequest(ipp_t *request, const CupsResource &resource); | ||
3470 | 108 | bool sendNewSimpleRequest(ipp_op_t op, const QString &printerName, | ||
3471 | 109 | const CupsResource &resource); | ||
3472 | 110 | bool handleReply(ipp_t *reply); | ||
3473 | 111 | bool isReplyOk(ipp_t *reply, bool deleteIfReplyNotOk); | ||
3474 | 112 | void setErrorFromReply(ipp_t *reply); | ||
3475 | 113 | QVariant getAttributeValue(ipp_attribute_t *attr, int index=-1) const; | ||
3476 | 114 | |||
3477 | 115 | http_t *m_connection; | ||
3478 | 116 | ipp_status_t m_lastStatus = IPP_OK; | ||
3479 | 117 | mutable QString m_internalStatus = QString::null; | ||
3480 | 118 | }; | ||
3481 | 119 | |||
3482 | 120 | |||
3483 | 121 | #endif // USC_PRINTERS_CUPS_IPPCLIENT_H | ||
3484 | 0 | 122 | ||
3485 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.cpp' | |||
3486 | --- modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.cpp 1970-01-01 00:00:00 +0000 | |||
3487 | +++ modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.cpp 2017-02-24 12:29:37 +0000 | |||
3488 | @@ -0,0 +1,127 @@ | |||
3489 | 1 | /* | ||
3490 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3491 | 3 | * | ||
3492 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3493 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3494 | 6 | * the Free Software Foundation; version 3. | ||
3495 | 7 | * | ||
3496 | 8 | * This program is distributed in the hope that it will be useful, | ||
3497 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3498 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3499 | 11 | * GNU Lesser General Public License for more details. | ||
3500 | 12 | * | ||
3501 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3502 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3503 | 15 | */ | ||
3504 | 16 | |||
3505 | 17 | #include "printerdriverloader.h" | ||
3506 | 18 | |||
3507 | 19 | PrinterDriverLoader::PrinterDriverLoader( | ||
3508 | 20 | const QString &deviceId, const QString &language, | ||
3509 | 21 | const QString &makeModel, const QString &product, | ||
3510 | 22 | const QStringList &includeSchemes, const QStringList &excludeSchemes) | ||
3511 | 23 | : m_deviceId(deviceId) | ||
3512 | 24 | , m_language(language) | ||
3513 | 25 | , m_makeModel(makeModel) | ||
3514 | 26 | , m_product(product) | ||
3515 | 27 | , m_includeSchemes(includeSchemes) | ||
3516 | 28 | , m_excludeSchemes(excludeSchemes) | ||
3517 | 29 | { | ||
3518 | 30 | } | ||
3519 | 31 | |||
3520 | 32 | PrinterDriverLoader::~PrinterDriverLoader() | ||
3521 | 33 | { | ||
3522 | 34 | } | ||
3523 | 35 | |||
3524 | 36 | void PrinterDriverLoader::process() | ||
3525 | 37 | { | ||
3526 | 38 | m_running = true; | ||
3527 | 39 | |||
3528 | 40 | ipp_t* response = client.createPrinterDriversRequest( | ||
3529 | 41 | m_deviceId, m_language, m_makeModel, m_product, m_includeSchemes, | ||
3530 | 42 | m_excludeSchemes | ||
3531 | 43 | ); | ||
3532 | 44 | |||
3533 | 45 | // Note: if the response somehow fails, we return. | ||
3534 | 46 | if (!response || ippGetStatusCode(response) > IPP_OK_CONFLICT) { | ||
3535 | 47 | QString err(cupsLastErrorString()); | ||
3536 | 48 | qWarning() << Q_FUNC_INFO << "Cups HTTP error:" << err; | ||
3537 | 49 | |||
3538 | 50 | if (response) | ||
3539 | 51 | ippDelete(response); | ||
3540 | 52 | |||
3541 | 53 | Q_EMIT error(err); | ||
3542 | 54 | Q_EMIT finished(); | ||
3543 | 55 | return; | ||
3544 | 56 | } | ||
3545 | 57 | |||
3546 | 58 | ipp_attribute_t *attr; | ||
3547 | 59 | QByteArray ppdDeviceId; | ||
3548 | 60 | QByteArray ppdLanguage; | ||
3549 | 61 | QByteArray ppdMakeModel; | ||
3550 | 62 | QByteArray ppdName; | ||
3551 | 63 | |||
3552 | 64 | QList<PrinterDriver> drivers; | ||
3553 | 65 | |||
3554 | 66 | for (attr = ippFirstAttribute(response); attr != NULL && m_running; attr = ippNextAttribute(response)) { | ||
3555 | 67 | |||
3556 | 68 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER) | ||
3557 | 69 | attr = ippNextAttribute(response); | ||
3558 | 70 | |||
3559 | 71 | if (attr == NULL) | ||
3560 | 72 | break; | ||
3561 | 73 | |||
3562 | 74 | // Pull the needed attributes from this PPD... | ||
3563 | 75 | ppdDeviceId = "NONE"; | ||
3564 | 76 | ppdLanguage.clear(); | ||
3565 | 77 | ppdMakeModel.clear(); | ||
3566 | 78 | ppdName.clear(); | ||
3567 | 79 | |||
3568 | 80 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) { | ||
3569 | 81 | if (!strcmp(ippGetName(attr), "ppd-device-id") && | ||
3570 | 82 | ippGetValueTag(attr) == IPP_TAG_TEXT) { | ||
3571 | 83 | ppdDeviceId = ippGetString(attr, 0, NULL); | ||
3572 | 84 | } else if (!strcmp(ippGetName(attr), "ppd-natural-language") && | ||
3573 | 85 | ippGetValueTag(attr) == IPP_TAG_LANGUAGE) { | ||
3574 | 86 | ppdLanguage = ippGetString(attr, 0, NULL); | ||
3575 | 87 | |||
3576 | 88 | } else if (!strcmp(ippGetName(attr), "ppd-make-and-model") && | ||
3577 | 89 | ippGetValueTag(attr) == IPP_TAG_TEXT) { | ||
3578 | 90 | ppdMakeModel = ippGetString(attr, 0, NULL); | ||
3579 | 91 | } else if (!strcmp(ippGetName(attr), "ppd-name") && | ||
3580 | 92 | ippGetValueTag(attr) == IPP_TAG_NAME) { | ||
3581 | 93 | |||
3582 | 94 | ppdName = ippGetString(attr, 0, NULL); | ||
3583 | 95 | } | ||
3584 | 96 | |||
3585 | 97 | attr = ippNextAttribute(response); | ||
3586 | 98 | } | ||
3587 | 99 | |||
3588 | 100 | // See if we have everything needed... | ||
3589 | 101 | if (ppdLanguage.isEmpty() || ppdMakeModel.isEmpty() || | ||
3590 | 102 | ppdName.isEmpty()) { | ||
3591 | 103 | if (attr == NULL) | ||
3592 | 104 | break; | ||
3593 | 105 | else | ||
3594 | 106 | continue; | ||
3595 | 107 | } | ||
3596 | 108 | |||
3597 | 109 | PrinterDriver m; | ||
3598 | 110 | m.name = ppdName; | ||
3599 | 111 | m.deviceId = ppdDeviceId; | ||
3600 | 112 | m.makeModel = ppdMakeModel; | ||
3601 | 113 | m.language = ppdLanguage; | ||
3602 | 114 | |||
3603 | 115 | drivers.append(m); | ||
3604 | 116 | } | ||
3605 | 117 | |||
3606 | 118 | ippDelete(response); | ||
3607 | 119 | |||
3608 | 120 | Q_EMIT loaded(drivers); | ||
3609 | 121 | Q_EMIT finished(); | ||
3610 | 122 | } | ||
3611 | 123 | |||
3612 | 124 | void PrinterDriverLoader::cancel() | ||
3613 | 125 | { | ||
3614 | 126 | m_running = false; | ||
3615 | 127 | } | ||
3616 | 0 | 128 | ||
3617 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.h' | |||
3618 | --- modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.h 1970-01-01 00:00:00 +0000 | |||
3619 | +++ modules/Ubuntu/Components/Extras/Printers/cups/printerdriverloader.h 2017-02-24 12:29:37 +0000 | |||
3620 | @@ -0,0 +1,61 @@ | |||
3621 | 1 | /* | ||
3622 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3623 | 3 | * | ||
3624 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3625 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3626 | 6 | * the Free Software Foundation; version 3. | ||
3627 | 7 | * | ||
3628 | 8 | * This program is distributed in the hope that it will be useful, | ||
3629 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3630 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3631 | 11 | * GNU Lesser General Public License for more details. | ||
3632 | 12 | * | ||
3633 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3634 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3635 | 15 | */ | ||
3636 | 16 | |||
3637 | 17 | #ifndef USC_PRINTERS_CUPS_DRIVERLOADER_H | ||
3638 | 18 | #define USC_PRINTERS_CUPS_DRIVERLOADER_H | ||
3639 | 19 | |||
3640 | 20 | #include "ippclient.h" | ||
3641 | 21 | #include "structs.h" | ||
3642 | 22 | |||
3643 | 23 | #include <QObject> | ||
3644 | 24 | #include <QString> | ||
3645 | 25 | #include <QStringList> | ||
3646 | 26 | |||
3647 | 27 | class PrinterDriverLoader : public QObject | ||
3648 | 28 | { | ||
3649 | 29 | Q_OBJECT | ||
3650 | 30 | public: | ||
3651 | 31 | PrinterDriverLoader( | ||
3652 | 32 | const QString &deviceId = "", | ||
3653 | 33 | const QString &language = "", | ||
3654 | 34 | const QString &makeModel = "", | ||
3655 | 35 | const QString &product = "", | ||
3656 | 36 | const QStringList &includeSchemes = QStringList(), | ||
3657 | 37 | const QStringList &excludeSchemes = QStringList()); | ||
3658 | 38 | ~PrinterDriverLoader(); | ||
3659 | 39 | |||
3660 | 40 | public Q_SLOTS: | ||
3661 | 41 | void process(); | ||
3662 | 42 | void cancel(); | ||
3663 | 43 | |||
3664 | 44 | Q_SIGNALS: | ||
3665 | 45 | void finished(); | ||
3666 | 46 | void loaded(const QList<PrinterDriver> &drivers); | ||
3667 | 47 | void error(const QString &error); | ||
3668 | 48 | |||
3669 | 49 | private: | ||
3670 | 50 | QString m_deviceId = QString::null; | ||
3671 | 51 | QString m_language = QString::null; | ||
3672 | 52 | QString m_makeModel = QString::null; | ||
3673 | 53 | QString m_product = QString::null; | ||
3674 | 54 | QStringList m_includeSchemes; | ||
3675 | 55 | QStringList m_excludeSchemes; | ||
3676 | 56 | |||
3677 | 57 | bool m_running = false; | ||
3678 | 58 | IppClient client; | ||
3679 | 59 | }; | ||
3680 | 60 | |||
3681 | 61 | #endif // USC_PRINTERS_CUPS_DRIVERLOADER_H | ||
3682 | 0 | 62 | ||
3683 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/printerloader.cpp' | |||
3684 | --- modules/Ubuntu/Components/Extras/Printers/cups/printerloader.cpp 1970-01-01 00:00:00 +0000 | |||
3685 | +++ modules/Ubuntu/Components/Extras/Printers/cups/printerloader.cpp 2017-02-24 12:29:37 +0000 | |||
3686 | @@ -0,0 +1,53 @@ | |||
3687 | 1 | /* | ||
3688 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3689 | 3 | * | ||
3690 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3691 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3692 | 6 | * the Free Software Foundation; version 3. | ||
3693 | 7 | * | ||
3694 | 8 | * This program is distributed in the hope that it will be useful, | ||
3695 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3696 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3697 | 11 | * GNU Lesser General Public License for more details. | ||
3698 | 12 | * | ||
3699 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3700 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3701 | 15 | */ | ||
3702 | 16 | |||
3703 | 17 | #include "backend/backend_pdf.h" | ||
3704 | 18 | #include "backend/backend_cups.h" | ||
3705 | 19 | #include "printerloader.h" | ||
3706 | 20 | |||
3707 | 21 | #include <QPrinterInfo> | ||
3708 | 22 | |||
3709 | 23 | class PrinterCupsBackend; | ||
3710 | 24 | PrinterLoader::PrinterLoader(const QString &printerName, | ||
3711 | 25 | IppClient *client, | ||
3712 | 26 | OrgCupsCupsdNotifierInterface* notifier, | ||
3713 | 27 | QObject *parent) | ||
3714 | 28 | : QObject(parent) | ||
3715 | 29 | , m_printerName(printerName) | ||
3716 | 30 | , m_client(client) | ||
3717 | 31 | , m_notifier(notifier) | ||
3718 | 32 | { | ||
3719 | 33 | } | ||
3720 | 34 | |||
3721 | 35 | PrinterLoader::~PrinterLoader() | ||
3722 | 36 | { | ||
3723 | 37 | } | ||
3724 | 38 | |||
3725 | 39 | void PrinterLoader::load() | ||
3726 | 40 | { | ||
3727 | 41 | QPrinterInfo info = QPrinterInfo::printerInfo(m_printerName); | ||
3728 | 42 | auto backend = new PrinterCupsBackend(m_client, info, m_notifier); | ||
3729 | 43 | |||
3730 | 44 | // Dest or PPD was null, but we know it's name so we will use it. | ||
3731 | 45 | if (info.printerName().isEmpty()) { | ||
3732 | 46 | backend->setPrinterNameInternal(m_printerName); | ||
3733 | 47 | } | ||
3734 | 48 | |||
3735 | 49 | auto p = QSharedPointer<Printer>(new Printer(backend)); | ||
3736 | 50 | |||
3737 | 51 | Q_EMIT loaded(p); | ||
3738 | 52 | Q_EMIT finished(); | ||
3739 | 53 | } | ||
3740 | 0 | 54 | ||
3741 | === added file 'modules/Ubuntu/Components/Extras/Printers/cups/printerloader.h' | |||
3742 | --- modules/Ubuntu/Components/Extras/Printers/cups/printerloader.h 1970-01-01 00:00:00 +0000 | |||
3743 | +++ modules/Ubuntu/Components/Extras/Printers/cups/printerloader.h 2017-02-24 12:29:37 +0000 | |||
3744 | @@ -0,0 +1,49 @@ | |||
3745 | 1 | /* | ||
3746 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3747 | 3 | * | ||
3748 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3749 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3750 | 6 | * the Free Software Foundation; version 3. | ||
3751 | 7 | * | ||
3752 | 8 | * This program is distributed in the hope that it will be useful, | ||
3753 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3754 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3755 | 11 | * GNU Lesser General Public License for more details. | ||
3756 | 12 | * | ||
3757 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3758 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3759 | 15 | */ | ||
3760 | 16 | |||
3761 | 17 | #ifndef USC_PRINTERS_CUPS_PRINTERLOADER_H | ||
3762 | 18 | #define USC_PRINTERS_CUPS_PRINTERLOADER_H | ||
3763 | 19 | |||
3764 | 20 | #include "cups/ippclient.h" | ||
3765 | 21 | #include "cupsdnotifier.h" // Note: this file was generated. | ||
3766 | 22 | #include "printer/printer.h" | ||
3767 | 23 | |||
3768 | 24 | #include <QList> | ||
3769 | 25 | #include <QObject> | ||
3770 | 26 | #include <QSharedPointer> | ||
3771 | 27 | |||
3772 | 28 | class PrinterLoader : public QObject | ||
3773 | 29 | { | ||
3774 | 30 | Q_OBJECT | ||
3775 | 31 | const QString m_printerName; | ||
3776 | 32 | IppClient *m_client; | ||
3777 | 33 | OrgCupsCupsdNotifierInterface *m_notifier; | ||
3778 | 34 | public: | ||
3779 | 35 | explicit PrinterLoader(const QString &printerName, | ||
3780 | 36 | IppClient *client, | ||
3781 | 37 | OrgCupsCupsdNotifierInterface* notifier, | ||
3782 | 38 | QObject *parent = Q_NULLPTR); | ||
3783 | 39 | ~PrinterLoader(); | ||
3784 | 40 | |||
3785 | 41 | public Q_SLOTS: | ||
3786 | 42 | void load(); | ||
3787 | 43 | |||
3788 | 44 | Q_SIGNALS: | ||
3789 | 45 | void finished(); | ||
3790 | 46 | void loaded(QSharedPointer<Printer> printer); | ||
3791 | 47 | }; | ||
3792 | 48 | |||
3793 | 49 | #endif // USC_PRINTERS_CUPS_PRINTERLOADER_H | ||
3794 | 0 | 50 | ||
3795 | === added file 'modules/Ubuntu/Components/Extras/Printers/enums.h' | |||
3796 | --- modules/Ubuntu/Components/Extras/Printers/enums.h 1970-01-01 00:00:00 +0000 | |||
3797 | +++ modules/Ubuntu/Components/Extras/Printers/enums.h 2017-02-24 12:29:37 +0000 | |||
3798 | @@ -0,0 +1,106 @@ | |||
3799 | 1 | /* | ||
3800 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3801 | 3 | * | ||
3802 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3803 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3804 | 6 | * the Free Software Foundation; version 3. | ||
3805 | 7 | * | ||
3806 | 8 | * This program is distributed in the hope that it will be useful, | ||
3807 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3808 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3809 | 11 | * GNU Lesser General Public License for more details. | ||
3810 | 12 | * | ||
3811 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3812 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3813 | 15 | */ | ||
3814 | 16 | |||
3815 | 17 | #ifndef USC_PRINTERS_ENUMS_H | ||
3816 | 18 | #define USC_PRINTERS_ENUMS_H | ||
3817 | 19 | |||
3818 | 20 | #include "printers_global.h" | ||
3819 | 21 | |||
3820 | 22 | #include <QtCore/QObject> | ||
3821 | 23 | |||
3822 | 24 | class PRINTERS_DECL_EXPORT PrinterEnum | ||
3823 | 25 | { | ||
3824 | 26 | Q_GADGET | ||
3825 | 27 | |||
3826 | 28 | public: | ||
3827 | 29 | enum class AccessControl | ||
3828 | 30 | { | ||
3829 | 31 | AccessAllow = 0, | ||
3830 | 32 | AccessDeny, | ||
3831 | 33 | }; | ||
3832 | 34 | Q_ENUM(AccessControl) | ||
3833 | 35 | |||
3834 | 36 | enum class ColorModelType | ||
3835 | 37 | { | ||
3836 | 38 | GrayType = 0, | ||
3837 | 39 | ColorType, | ||
3838 | 40 | UnknownType, | ||
3839 | 41 | }; | ||
3840 | 42 | Q_ENUM(ColorModelType) | ||
3841 | 43 | |||
3842 | 44 | enum class DuplexMode | ||
3843 | 45 | { | ||
3844 | 46 | DuplexNone = 0, | ||
3845 | 47 | DuplexLongSide, | ||
3846 | 48 | DuplexShortSide, | ||
3847 | 49 | }; | ||
3848 | 50 | Q_ENUM(DuplexMode) | ||
3849 | 51 | |||
3850 | 52 | enum class ErrorPolicy | ||
3851 | 53 | { | ||
3852 | 54 | RetryOnError = 0, | ||
3853 | 55 | AbortOnError, | ||
3854 | 56 | StopPrinterOnError, | ||
3855 | 57 | RetryCurrentOnError, | ||
3856 | 58 | }; | ||
3857 | 59 | Q_ENUM(ErrorPolicy) | ||
3858 | 60 | |||
3859 | 61 | // Match enums from ipp_jstate_t | ||
3860 | 62 | enum class JobState | ||
3861 | 63 | { | ||
3862 | 64 | Pending = 3, | ||
3863 | 65 | Held, | ||
3864 | 66 | Processing, | ||
3865 | 67 | Stopped, | ||
3866 | 68 | Canceled, | ||
3867 | 69 | Aborted, | ||
3868 | 70 | Complete, | ||
3869 | 71 | }; | ||
3870 | 72 | Q_ENUM(JobState) | ||
3871 | 73 | |||
3872 | 74 | enum class OperationPolicy | ||
3873 | 75 | { | ||
3874 | 76 | DefaultOperation = 0, | ||
3875 | 77 | AuthenticatedOperation, | ||
3876 | 78 | }; | ||
3877 | 79 | Q_ENUM(OperationPolicy) | ||
3878 | 80 | |||
3879 | 81 | enum class PrintRange | ||
3880 | 82 | { | ||
3881 | 83 | AllPages = 0, | ||
3882 | 84 | PageRange, | ||
3883 | 85 | }; | ||
3884 | 86 | Q_ENUM(PrintRange) | ||
3885 | 87 | |||
3886 | 88 | enum class State | ||
3887 | 89 | { | ||
3888 | 90 | IdleState = 0, | ||
3889 | 91 | ActiveState, | ||
3890 | 92 | AbortedState, | ||
3891 | 93 | ErrorState, | ||
3892 | 94 | }; | ||
3893 | 95 | Q_ENUM(State) | ||
3894 | 96 | |||
3895 | 97 | enum class PrinterType | ||
3896 | 98 | { | ||
3897 | 99 | ProxyType = 0, // Represents a printer not yet loaded. | ||
3898 | 100 | CupsType, | ||
3899 | 101 | PdfType, | ||
3900 | 102 | }; | ||
3901 | 103 | Q_ENUM(PrinterType) | ||
3902 | 104 | }; | ||
3903 | 105 | |||
3904 | 106 | #endif // USC_PRINTERS_ENUMS_H | ||
3905 | 0 | 107 | ||
3906 | === added file 'modules/Ubuntu/Components/Extras/Printers/i18n.cpp' | |||
3907 | --- modules/Ubuntu/Components/Extras/Printers/i18n.cpp 1970-01-01 00:00:00 +0000 | |||
3908 | +++ modules/Ubuntu/Components/Extras/Printers/i18n.cpp 2017-02-24 12:29:37 +0000 | |||
3909 | @@ -0,0 +1,42 @@ | |||
3910 | 1 | /* | ||
3911 | 2 | * Copyright (C) 2014, 2017 Canonical, Ltd. | ||
3912 | 3 | * | ||
3913 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3914 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3915 | 6 | * the Free Software Foundation; version 3. | ||
3916 | 7 | * | ||
3917 | 8 | * This program is distributed in the hope that it will be useful, | ||
3918 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3919 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3920 | 11 | * GNU Lesser General Public License for more details. | ||
3921 | 12 | * | ||
3922 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3923 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3924 | 15 | * | ||
3925 | 16 | * Authored by: Ken VanDine <ken.vandine@canonical.com> | ||
3926 | 17 | * Andrew Hayzen <andrew.hayzen@canonical.com> | ||
3927 | 18 | */ | ||
3928 | 19 | |||
3929 | 20 | #define NO_TR_OVERRIDE | ||
3930 | 21 | #include "i18n.h" | ||
3931 | 22 | |||
3932 | 23 | #include <libintl.h> | ||
3933 | 24 | |||
3934 | 25 | const char *thisDomain = ""; | ||
3935 | 26 | |||
3936 | 27 | void initTr(const char *domain, const char *localeDir) | ||
3937 | 28 | { | ||
3938 | 29 | // Don't bind the domain or set textdomain as it changes the Apps domain | ||
3939 | 30 | // as well. Instead store the domain and use it in the lookups | ||
3940 | 31 | Q_UNUSED(localeDir); | ||
3941 | 32 | |||
3942 | 33 | thisDomain = domain; | ||
3943 | 34 | } | ||
3944 | 35 | |||
3945 | 36 | QString __(const char *text, const char *domain) | ||
3946 | 37 | { | ||
3947 | 38 | Q_UNUSED(domain); | ||
3948 | 39 | |||
3949 | 40 | // Use the stored domain | ||
3950 | 41 | return QString::fromUtf8(dgettext(thisDomain, text)); | ||
3951 | 42 | } | ||
3952 | 0 | 43 | ||
3953 | === added file 'modules/Ubuntu/Components/Extras/Printers/i18n.h' | |||
3954 | --- modules/Ubuntu/Components/Extras/Printers/i18n.h 1970-01-01 00:00:00 +0000 | |||
3955 | +++ modules/Ubuntu/Components/Extras/Printers/i18n.h 2017-02-24 12:29:37 +0000 | |||
3956 | @@ -0,0 +1,29 @@ | |||
3957 | 1 | /* | ||
3958 | 2 | * Copyright (C) 2014, 2017 Canonical, Ltd. | ||
3959 | 3 | * | ||
3960 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3961 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3962 | 6 | * the Free Software Foundation; version 3. | ||
3963 | 7 | * | ||
3964 | 8 | * This program is distributed in the hope that it will be useful, | ||
3965 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
3966 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
3967 | 11 | * GNU Lesser General Public License for more details. | ||
3968 | 12 | * | ||
3969 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
3970 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
3971 | 15 | * | ||
3972 | 16 | * Authored by: Ken VanDine <ken.vandine@canonical.com> | ||
3973 | 17 | * Andrew Hayzen <andrew.hayzen@canonical.com> | ||
3974 | 18 | */ | ||
3975 | 19 | |||
3976 | 20 | #ifndef I18N_H | ||
3977 | 21 | #define I18N_H | ||
3978 | 22 | |||
3979 | 23 | #include <QtCore/QString> | ||
3980 | 24 | |||
3981 | 25 | void initTr(const char *domain, const char *localeDir); | ||
3982 | 26 | QString __(const char *text, const char *domain = 0); | ||
3983 | 27 | |||
3984 | 28 | #endif // I18N_H | ||
3985 | 29 | |||
3986 | 0 | 30 | ||
3987 | === added directory 'modules/Ubuntu/Components/Extras/Printers/models' | |||
3988 | === added file 'modules/Ubuntu/Components/Extras/Printers/models/drivermodel.cpp' | |||
3989 | --- modules/Ubuntu/Components/Extras/Printers/models/drivermodel.cpp 1970-01-01 00:00:00 +0000 | |||
3990 | +++ modules/Ubuntu/Components/Extras/Printers/models/drivermodel.cpp 2017-02-24 12:29:37 +0000 | |||
3991 | @@ -0,0 +1,175 @@ | |||
3992 | 1 | /* | ||
3993 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
3994 | 3 | * | ||
3995 | 4 | * This program is free software; you can redistribute it and/or modify | ||
3996 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
3997 | 6 | * the Free Software Foundation; version 3. | ||
3998 | 7 | * | ||
3999 | 8 | * This program is distributed in the hope that it will be useful, | ||
4000 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4001 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4002 | 11 | * GNU Lesser General Public License for more details. | ||
4003 | 12 | * | ||
4004 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
4005 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4006 | 15 | */ | ||
4007 | 16 | |||
4008 | 17 | #include "backend/backend_cups.h" | ||
4009 | 18 | #include "models/drivermodel.h" | ||
4010 | 19 | |||
4011 | 20 | #include <QDebug> | ||
4012 | 21 | #include <QtConcurrent> | ||
4013 | 22 | |||
4014 | 23 | DriverModel::DriverModel(PrinterBackend *backend, QObject *parent) | ||
4015 | 24 | : QAbstractListModel(parent) | ||
4016 | 25 | , m_backend(backend) | ||
4017 | 26 | { | ||
4018 | 27 | connect(m_backend, SIGNAL(printerDriversLoaded(const QList<PrinterDriver>&)), | ||
4019 | 28 | this, SLOT(printerDriversLoaded(const QList<PrinterDriver>&))); | ||
4020 | 29 | |||
4021 | 30 | QObject::connect(&m_watcher, | ||
4022 | 31 | &QFutureWatcher<PrinterDriver>::finished, | ||
4023 | 32 | this, | ||
4024 | 33 | &DriverModel::filterFinished); | ||
4025 | 34 | |||
4026 | 35 | } | ||
4027 | 36 | |||
4028 | 37 | DriverModel::~DriverModel() | ||
4029 | 38 | { | ||
4030 | 39 | cancel(); | ||
4031 | 40 | } | ||
4032 | 41 | |||
4033 | 42 | int DriverModel::rowCount(const QModelIndex &parent) const | ||
4034 | 43 | { | ||
4035 | 44 | Q_UNUSED(parent); | ||
4036 | 45 | return m_drivers.size(); | ||
4037 | 46 | } | ||
4038 | 47 | |||
4039 | 48 | int DriverModel::count() const | ||
4040 | 49 | { | ||
4041 | 50 | return rowCount(); | ||
4042 | 51 | } | ||
4043 | 52 | |||
4044 | 53 | QVariant DriverModel::data(const QModelIndex &index, int role) const | ||
4045 | 54 | { | ||
4046 | 55 | QVariant ret; | ||
4047 | 56 | |||
4048 | 57 | if ((0 <= index.row()) && (index.row() < m_drivers.size())) { | ||
4049 | 58 | |||
4050 | 59 | auto driver = m_drivers[index.row()]; | ||
4051 | 60 | |||
4052 | 61 | switch (role) { | ||
4053 | 62 | case Qt::DisplayRole: | ||
4054 | 63 | ret = driver.toString(); | ||
4055 | 64 | break; | ||
4056 | 65 | case NameRole: | ||
4057 | 66 | ret = driver.name; | ||
4058 | 67 | break; | ||
4059 | 68 | case DeviceIdRole: | ||
4060 | 69 | ret = driver.deviceId; | ||
4061 | 70 | break; | ||
4062 | 71 | case LanguageRole: | ||
4063 | 72 | ret = driver.language; | ||
4064 | 73 | break; | ||
4065 | 74 | case MakeModelRole: | ||
4066 | 75 | ret = driver.makeModel; | ||
4067 | 76 | break; | ||
4068 | 77 | } | ||
4069 | 78 | } | ||
4070 | 79 | |||
4071 | 80 | return ret; | ||
4072 | 81 | } | ||
4073 | 82 | |||
4074 | 83 | QHash<int, QByteArray> DriverModel::roleNames() const | ||
4075 | 84 | { | ||
4076 | 85 | static QHash<int,QByteArray> names; | ||
4077 | 86 | |||
4078 | 87 | if (Q_UNLIKELY(names.empty())) { | ||
4079 | 88 | names[Qt::DisplayRole] = "displayName"; | ||
4080 | 89 | names[NameRole] = "name"; | ||
4081 | 90 | names[DeviceIdRole] = "deviceId"; | ||
4082 | 91 | names[LanguageRole] = "language"; | ||
4083 | 92 | names[MakeModelRole] = "makeModel"; | ||
4084 | 93 | } | ||
4085 | 94 | |||
4086 | 95 | return names; | ||
4087 | 96 | } | ||
4088 | 97 | |||
4089 | 98 | void DriverModel::setFilter(const QString& pattern) | ||
4090 | 99 | { | ||
4091 | 100 | QList<QByteArray> needles; | ||
4092 | 101 | Q_FOREACH(const QString patternPart, pattern.toLower().split(" ")) { | ||
4093 | 102 | needles.append(patternPart.toUtf8()); | ||
4094 | 103 | } | ||
4095 | 104 | QList<PrinterDriver> list; | ||
4096 | 105 | |||
4097 | 106 | if (m_watcher.isRunning()) | ||
4098 | 107 | m_watcher.cancel(); | ||
4099 | 108 | |||
4100 | 109 | if (pattern.isEmpty()) { | ||
4101 | 110 | setModel(m_originalDrivers); | ||
4102 | 111 | m_filter = pattern; | ||
4103 | 112 | return; | ||
4104 | 113 | } | ||
4105 | 114 | |||
4106 | 115 | if (!m_filter.isEmpty() && !m_drivers.isEmpty() && | ||
4107 | 116 | pattern.startsWith(m_filter)) | ||
4108 | 117 | list = m_drivers; // search in the smaller list | ||
4109 | 118 | else | ||
4110 | 119 | list = m_originalDrivers; //search in the whole list | ||
4111 | 120 | |||
4112 | 121 | m_filter = pattern; | ||
4113 | 122 | |||
4114 | 123 | QFuture<PrinterDriver> future(QtConcurrent::filtered(list, | ||
4115 | 124 | [needles] (const PrinterDriver &driver) { | ||
4116 | 125 | QByteArray haystack = driver.makeModel.toLower(); | ||
4117 | 126 | Q_FOREACH(const QByteArray needle, needles) { | ||
4118 | 127 | if (!haystack.contains(needle)) { | ||
4119 | 128 | return false; | ||
4120 | 129 | } | ||
4121 | 130 | } | ||
4122 | 131 | return true; | ||
4123 | 132 | } | ||
4124 | 133 | ) | ||
4125 | 134 | ); | ||
4126 | 135 | |||
4127 | 136 | Q_EMIT filterBegin(); | ||
4128 | 137 | |||
4129 | 138 | m_watcher.setFuture(future); | ||
4130 | 139 | } | ||
4131 | 140 | |||
4132 | 141 | QString DriverModel::filter() const | ||
4133 | 142 | { | ||
4134 | 143 | return m_filter; | ||
4135 | 144 | } | ||
4136 | 145 | |||
4137 | 146 | void DriverModel::filterFinished() | ||
4138 | 147 | { | ||
4139 | 148 | setModel(m_watcher.future().results()); | ||
4140 | 149 | } | ||
4141 | 150 | |||
4142 | 151 | void DriverModel::load() | ||
4143 | 152 | { | ||
4144 | 153 | m_backend->requestPrinterDrivers(); | ||
4145 | 154 | } | ||
4146 | 155 | |||
4147 | 156 | void DriverModel::cancel() | ||
4148 | 157 | { | ||
4149 | 158 | if (m_watcher.isRunning()) | ||
4150 | 159 | m_watcher.cancel(); | ||
4151 | 160 | } | ||
4152 | 161 | |||
4153 | 162 | void DriverModel::printerDriversLoaded(const QList<PrinterDriver> &drivers) | ||
4154 | 163 | { | ||
4155 | 164 | m_originalDrivers = drivers; | ||
4156 | 165 | setModel(m_originalDrivers); | ||
4157 | 166 | } | ||
4158 | 167 | |||
4159 | 168 | void DriverModel::setModel(const QList<PrinterDriver> &drivers) | ||
4160 | 169 | { | ||
4161 | 170 | beginResetModel(); | ||
4162 | 171 | m_drivers = drivers; | ||
4163 | 172 | endResetModel(); | ||
4164 | 173 | |||
4165 | 174 | Q_EMIT filterComplete(); | ||
4166 | 175 | } | ||
4167 | 0 | 176 | ||
4168 | === added file 'modules/Ubuntu/Components/Extras/Printers/models/drivermodel.h' | |||
4169 | --- modules/Ubuntu/Components/Extras/Printers/models/drivermodel.h 1970-01-01 00:00:00 +0000 | |||
4170 | +++ modules/Ubuntu/Components/Extras/Printers/models/drivermodel.h 2017-02-24 12:29:37 +0000 | |||
4171 | @@ -0,0 +1,82 @@ | |||
4172 | 1 | /* | ||
4173 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
4174 | 3 | * | ||
4175 | 4 | * This program is free software; you can redistribute it and/or modify | ||
4176 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
4177 | 6 | * the Free Software Foundation; version 3. | ||
4178 | 7 | * | ||
4179 | 8 | * This program is distributed in the hope that it will be useful, | ||
4180 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4181 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4182 | 11 | * GNU Lesser General Public License for more details. | ||
4183 | 12 | * | ||
4184 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
4185 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4186 | 15 | */ | ||
4187 | 16 | |||
4188 | 17 | #ifndef USC_PRINTER_DRIVERMODEL_H | ||
4189 | 18 | #define USC_PRINTER_DRIVERMODEL_H | ||
4190 | 19 | |||
4191 | 20 | #include "printers_global.h" | ||
4192 | 21 | |||
4193 | 22 | #include "structs.h" | ||
4194 | 23 | |||
4195 | 24 | #include <QAbstractListModel> | ||
4196 | 25 | #include <QFutureWatcher> | ||
4197 | 26 | #include <QModelIndex> | ||
4198 | 27 | #include <QObject> | ||
4199 | 28 | #include <QVariant> | ||
4200 | 29 | |||
4201 | 30 | class PRINTERS_DECL_EXPORT DriverModel : public QAbstractListModel | ||
4202 | 31 | { | ||
4203 | 32 | Q_OBJECT | ||
4204 | 33 | Q_PROPERTY(int count READ count NOTIFY countChanged) | ||
4205 | 34 | public: | ||
4206 | 35 | explicit DriverModel(PrinterBackend *backend, QObject *parent = Q_NULLPTR); | ||
4207 | 36 | ~DriverModel(); | ||
4208 | 37 | |||
4209 | 38 | enum Roles | ||
4210 | 39 | { | ||
4211 | 40 | // Qt::DisplayRole holds driver name | ||
4212 | 41 | NameRole = Qt::UserRole, | ||
4213 | 42 | DeviceIdRole, | ||
4214 | 43 | LanguageRole, | ||
4215 | 44 | MakeModelRole, | ||
4216 | 45 | LastRole = MakeModelRole, | ||
4217 | 46 | }; | ||
4218 | 47 | |||
4219 | 48 | virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||
4220 | 49 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||
4221 | 50 | virtual QHash<int, QByteArray> roleNames() const override; | ||
4222 | 51 | |||
4223 | 52 | int count() const; | ||
4224 | 53 | |||
4225 | 54 | QString filter() const; | ||
4226 | 55 | void setFilter(const QString& pattern); | ||
4227 | 56 | |||
4228 | 57 | public Q_SLOTS: | ||
4229 | 58 | // Start loading the model. | ||
4230 | 59 | void load(); | ||
4231 | 60 | |||
4232 | 61 | // Cancel loading of the model. | ||
4233 | 62 | void cancel(); | ||
4234 | 63 | |||
4235 | 64 | private Q_SLOTS: | ||
4236 | 65 | void printerDriversLoaded(const QList<PrinterDriver> &drivers); | ||
4237 | 66 | void filterFinished(); | ||
4238 | 67 | |||
4239 | 68 | Q_SIGNALS: | ||
4240 | 69 | void countChanged(); | ||
4241 | 70 | void filterBegin(); | ||
4242 | 71 | void filterComplete(); | ||
4243 | 72 | |||
4244 | 73 | private: | ||
4245 | 74 | void setModel(const QList<PrinterDriver> &drivers); | ||
4246 | 75 | PrinterBackend *m_backend; | ||
4247 | 76 | QList<PrinterDriver> m_drivers; | ||
4248 | 77 | QList<PrinterDriver> m_originalDrivers; | ||
4249 | 78 | QString m_filter; | ||
4250 | 79 | QFutureWatcher<PrinterDriver> m_watcher; | ||
4251 | 80 | }; | ||
4252 | 81 | |||
4253 | 82 | #endif // USC_PRINTER_DRIVERMODEL_H | ||
4254 | 0 | 83 | ||
4255 | === added file 'modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp' | |||
4256 | --- modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp 1970-01-01 00:00:00 +0000 | |||
4257 | +++ modules/Ubuntu/Components/Extras/Printers/models/jobmodel.cpp 2017-02-24 12:29:37 +0000 | |||
4258 | @@ -0,0 +1,390 @@ | |||
4259 | 1 | /* | ||
4260 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
4261 | 3 | * | ||
4262 | 4 | * This program is free software; you can redistribute it and/or modify | ||
4263 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
4264 | 6 | * the Free Software Foundation; version 3. | ||
4265 | 7 | * | ||
4266 | 8 | * This program is distributed in the hope that it will be useful, | ||
4267 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4268 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4269 | 11 | * GNU Lesser General Public License for more details. | ||
4270 | 12 | * | ||
4271 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
4272 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4273 | 15 | */ | ||
4274 | 16 | |||
4275 | 17 | #include "utils.h" | ||
4276 | 18 | |||
4277 | 19 | #include "backend/backend_cups.h" | ||
4278 | 20 | |||
4279 | 21 | #include "models/jobmodel.h" | ||
4280 | 22 | |||
4281 | 23 | #include <QDebug> | ||
4282 | 24 | |||
4283 | 25 | JobModel::JobModel(QObject *parent) : QAbstractListModel(parent) | ||
4284 | 26 | { | ||
4285 | 27 | } | ||
4286 | 28 | |||
4287 | 29 | JobModel::JobModel(PrinterBackend *backend, | ||
4288 | 30 | QObject *parent) | ||
4289 | 31 | : QAbstractListModel(parent) | ||
4290 | 32 | , m_backend(backend) | ||
4291 | 33 | { | ||
4292 | 34 | update(); | ||
4293 | 35 | |||
4294 | 36 | QObject::connect(m_backend, &PrinterBackend::jobCreated, | ||
4295 | 37 | this, &JobModel::jobSignalCatchAll); | ||
4296 | 38 | QObject::connect(m_backend, &PrinterBackend::jobState, | ||
4297 | 39 | this, &JobModel::jobSignalCatchAll); | ||
4298 | 40 | QObject::connect(m_backend, &PrinterBackend::jobCompleted, | ||
4299 | 41 | this, &JobModel::jobSignalCatchAll); | ||
4300 | 42 | } | ||
4301 | 43 | |||
4302 | 44 | JobModel::~JobModel() | ||
4303 | 45 | { | ||
4304 | 46 | } | ||
4305 | 47 | |||
4306 | 48 | void JobModel::jobSignalCatchAll( | ||
4307 | 49 | const QString &text, const QString &printer_uri, | ||
4308 | 50 | const QString &printer_name, uint printer_state, | ||
4309 | 51 | const QString &printer_state_reasons, bool printer_is_accepting_jobs, | ||
4310 | 52 | uint job_id, uint job_state, const QString &job_state_reasons, | ||
4311 | 53 | const QString &job_name, uint job_impressions_completed) | ||
4312 | 54 | { | ||
4313 | 55 | Q_UNUSED(text); | ||
4314 | 56 | Q_UNUSED(printer_uri); | ||
4315 | 57 | Q_UNUSED(printer_name); | ||
4316 | 58 | Q_UNUSED(printer_state); | ||
4317 | 59 | Q_UNUSED(printer_state_reasons); | ||
4318 | 60 | Q_UNUSED(printer_is_accepting_jobs); | ||
4319 | 61 | Q_UNUSED(job_id); | ||
4320 | 62 | Q_UNUSED(job_state); | ||
4321 | 63 | Q_UNUSED(job_state_reasons); | ||
4322 | 64 | Q_UNUSED(job_name); | ||
4323 | 65 | |||
4324 | 66 | auto job = getJobById(job_id); | ||
4325 | 67 | if (job) | ||
4326 | 68 | job->setImpressionsCompleted(job_impressions_completed); | ||
4327 | 69 | |||
4328 | 70 | update(); | ||
4329 | 71 | } | ||
4330 | 72 | |||
4331 | 73 | void JobModel::update() | ||
4332 | 74 | { | ||
4333 | 75 | // Store the old count and get the new printers | ||
4334 | 76 | int oldCount = m_jobs.size(); | ||
4335 | 77 | QList<QSharedPointer<PrinterJob>> newJobs = m_backend->printerGetJobs(); | ||
4336 | 78 | |||
4337 | 79 | // Go through the old model | ||
4338 | 80 | for (int i=0; i < m_jobs.count(); i++) { | ||
4339 | 81 | // Determine if the old printer exists in the new model | ||
4340 | 82 | bool exists = false; | ||
4341 | 83 | |||
4342 | 84 | Q_FOREACH(QSharedPointer<PrinterJob> p, newJobs) { | ||
4343 | 85 | if (p->jobId() == m_jobs.at(i)->jobId()) { | ||
4344 | 86 | exists = true; | ||
4345 | 87 | |||
4346 | 88 | // Ensure the other properties of the job are up to date | ||
4347 | 89 | if (!m_jobs.at(i)->deepCompare(p)) { | ||
4348 | 90 | m_jobs.at(i)->updateFrom(p); | ||
4349 | 91 | |||
4350 | 92 | Q_EMIT dataChanged(index(i), index(i)); | ||
4351 | 93 | } | ||
4352 | 94 | |||
4353 | 95 | break; | ||
4354 | 96 | } | ||
4355 | 97 | } | ||
4356 | 98 | |||
4357 | 99 | // If it doesn't exist then remove it from the old model | ||
4358 | 100 | if (!exists) { | ||
4359 | 101 | beginRemoveRows(QModelIndex(), i, i); | ||
4360 | 102 | QSharedPointer<PrinterJob> p = m_jobs.takeAt(i); | ||
4361 | 103 | endRemoveRows(); | ||
4362 | 104 | |||
4363 | 105 | i--; // as we have removed an item decrement | ||
4364 | 106 | } | ||
4365 | 107 | } | ||
4366 | 108 | |||
4367 | 109 | // Go through the new model | ||
4368 | 110 | for (int i=0; i < newJobs.count(); i++) { | ||
4369 | 111 | // Determine if the new printer exists in the old model | ||
4370 | 112 | bool exists = false; | ||
4371 | 113 | int j; | ||
4372 | 114 | |||
4373 | 115 | for (j=0; j < m_jobs.count(); j++) { | ||
4374 | 116 | if (m_jobs.at(j)->jobId() == newJobs.at(i)->jobId()) { | ||
4375 | 117 | exists = true; | ||
4376 | 118 | break; | ||
4377 | 119 | } | ||
4378 | 120 | } | ||
4379 | 121 | |||
4380 | 122 | if (exists) { | ||
4381 | 123 | if (j == i) { // New printer exists and in correct position | ||
4382 | 124 | continue; | ||
4383 | 125 | } else { | ||
4384 | 126 | // New printer does exist but needs to be moved in old model | ||
4385 | 127 | beginMoveRows(QModelIndex(), j, 1, QModelIndex(), i); | ||
4386 | 128 | m_jobs.move(j, i); | ||
4387 | 129 | endMoveRows(); | ||
4388 | 130 | } | ||
4389 | 131 | } else { | ||
4390 | 132 | // New printer does not exist insert into model | ||
4391 | 133 | beginInsertRows(QModelIndex(), i, i); | ||
4392 | 134 | m_jobs.insert(i, newJobs.at(i)); | ||
4393 | 135 | endInsertRows(); | ||
4394 | 136 | } | ||
4395 | 137 | } | ||
4396 | 138 | |||
4397 | 139 | if (oldCount != m_jobs.size()) { | ||
4398 | 140 | Q_EMIT countChanged(); | ||
4399 | 141 | } | ||
4400 | 142 | } | ||
4401 | 143 | |||
4402 | 144 | int JobModel::rowCount(const QModelIndex &parent) const | ||
4403 | 145 | { | ||
4404 | 146 | Q_UNUSED(parent); | ||
4405 | 147 | return m_jobs.size(); | ||
4406 | 148 | } | ||
4407 | 149 | |||
4408 | 150 | int JobModel::count() const | ||
4409 | 151 | { | ||
4410 | 152 | return rowCount(); | ||
4411 | 153 | } | ||
4412 | 154 | |||
4413 | 155 | QVariant JobModel::data(const QModelIndex &index, int role) const | ||
4414 | 156 | { | ||
4415 | 157 | QVariant ret; | ||
4416 | 158 | |||
4417 | 159 | if ((0<=index.row()) && (index.row()<m_jobs.size())) { | ||
4418 | 160 | |||
4419 | 161 | auto job = m_jobs[index.row()]; | ||
4420 | 162 | |||
4421 | 163 | switch (role) { | ||
4422 | 164 | case CollateRole: | ||
4423 | 165 | ret = job->collate(); | ||
4424 | 166 | break; | ||
4425 | 167 | case ColorModelRole: { | ||
4426 | 168 | if (job->printer()) { | ||
4427 | 169 | ret = job->printer()->supportedColorModels().at(job->colorModel()).text; | ||
4428 | 170 | } else { | ||
4429 | 171 | qWarning() << "Printer is undefined, no colorModel"; | ||
4430 | 172 | ret = ""; | ||
4431 | 173 | } | ||
4432 | 174 | break; | ||
4433 | 175 | } | ||
4434 | 176 | case CompletedTimeRole: | ||
4435 | 177 | ret = job->completedTime().toString(QLocale::system().dateTimeFormat()); | ||
4436 | 178 | break; | ||
4437 | 179 | case CopiesRole: | ||
4438 | 180 | ret = job->copies(); | ||
4439 | 181 | break; | ||
4440 | 182 | case CreationTimeRole: | ||
4441 | 183 | ret = job->creationTime().toString(QLocale::system().dateTimeFormat()); | ||
4442 | 184 | break; | ||
4443 | 185 | case DuplexRole: { | ||
4444 | 186 | if (job->printer()) { | ||
4445 | 187 | ret = job->printer()->supportedDuplexStrings().at(job->duplexMode()); | ||
4446 | 188 | } else { | ||
4447 | 189 | qWarning() << "Printer is undefined, no duplexMode"; | ||
4448 | 190 | ret = ""; | ||
4449 | 191 | } | ||
4450 | 192 | break; | ||
4451 | 193 | } | ||
4452 | 194 | case IdRole: | ||
4453 | 195 | ret = job->jobId(); | ||
4454 | 196 | break; | ||
4455 | 197 | case ImpressionsCompletedRole: | ||
4456 | 198 | ret = job->impressionsCompleted(); | ||
4457 | 199 | break; | ||
4458 | 200 | case LandscapeRole: | ||
4459 | 201 | ret = job->landscape(); | ||
4460 | 202 | break; | ||
4461 | 203 | case MessagesRole: | ||
4462 | 204 | ret = job->messages(); | ||
4463 | 205 | break; | ||
4464 | 206 | case PrinterNameRole: | ||
4465 | 207 | ret = job->printerName(); | ||
4466 | 208 | break; | ||
4467 | 209 | case PrintRangeRole: | ||
4468 | 210 | ret = job->printRange(); | ||
4469 | 211 | break; | ||
4470 | 212 | case PrintRangeModeRole: | ||
4471 | 213 | ret = QVariant::fromValue<PrinterEnum::PrintRange>(job->printRangeMode()); | ||
4472 | 214 | break; | ||
4473 | 215 | case ProcessingTimeRole: | ||
4474 | 216 | ret = job->processingTime().toString(QLocale::system().dateTimeFormat()); | ||
4475 | 217 | break; | ||
4476 | 218 | case QualityRole: { | ||
4477 | 219 | if (job->printer()) { | ||
4478 | 220 | ret = job->printer()->supportedPrintQualities().at(job->quality()).text; | ||
4479 | 221 | } else { | ||
4480 | 222 | qWarning() << "Printer is undefined, no quality"; | ||
4481 | 223 | ret = ""; | ||
4482 | 224 | } | ||
4483 | 225 | break; | ||
4484 | 226 | } | ||
4485 | 227 | case ReverseRole: | ||
4486 | 228 | ret = job->reverse(); | ||
4487 | 229 | break; | ||
4488 | 230 | case SizeRole: | ||
4489 | 231 | ret = job->size(); | ||
4490 | 232 | break; | ||
4491 | 233 | case StateRole: | ||
4492 | 234 | // TODO: improve, for now have a switch | ||
4493 | 235 | switch (job->state()) { | ||
4494 | 236 | case PrinterEnum::JobState::Aborted: | ||
4495 | 237 | ret = "Aborted"; | ||
4496 | 238 | break; | ||
4497 | 239 | case PrinterEnum::JobState::Canceled: | ||
4498 | 240 | ret = "Canceled"; | ||
4499 | 241 | break; | ||
4500 | 242 | case PrinterEnum::JobState::Complete: | ||
4501 | 243 | ret = "Compelete"; | ||
4502 | 244 | break; | ||
4503 | 245 | case PrinterEnum::JobState::Held: | ||
4504 | 246 | ret = "Held"; | ||
4505 | 247 | break; | ||
4506 | 248 | case PrinterEnum::JobState::Pending: | ||
4507 | 249 | ret = "Pending"; | ||
4508 | 250 | break; | ||
4509 | 251 | case PrinterEnum::JobState::Processing: | ||
4510 | 252 | ret = "Processing"; | ||
4511 | 253 | break; | ||
4512 | 254 | case PrinterEnum::JobState::Stopped: | ||
4513 | 255 | ret = "Stopped"; | ||
4514 | 256 | break; | ||
4515 | 257 | } | ||
4516 | 258 | break; | ||
4517 | 259 | case Qt::DisplayRole: | ||
4518 | 260 | case TitleRole: | ||
4519 | 261 | ret = job->title(); | ||
4520 | 262 | break; | ||
4521 | 263 | case UserRole: | ||
4522 | 264 | ret = job->user(); | ||
4523 | 265 | break; | ||
4524 | 266 | } | ||
4525 | 267 | } | ||
4526 | 268 | |||
4527 | 269 | return ret; | ||
4528 | 270 | } | ||
4529 | 271 | |||
4530 | 272 | QHash<int, QByteArray> JobModel::roleNames() const | ||
4531 | 273 | { | ||
4532 | 274 | static QHash<int,QByteArray> names; | ||
4533 | 275 | |||
4534 | 276 | if (Q_UNLIKELY(names.empty())) { | ||
4535 | 277 | names[Qt::DisplayRole] = "displayName"; | ||
4536 | 278 | names[IdRole] = "id"; | ||
4537 | 279 | names[CollateRole] = "collate"; | ||
4538 | 280 | names[ColorModelRole] = "colorModel"; | ||
4539 | 281 | names[CompletedTimeRole] = "completedTime"; | ||
4540 | 282 | names[CopiesRole] = "copies"; | ||
4541 | 283 | names[CreationTimeRole] = "creationTime"; | ||
4542 | 284 | names[DuplexRole] = "duplexMode"; | ||
4543 | 285 | names[ImpressionsCompletedRole] = "impressionsCompleted"; | ||
4544 | 286 | names[LandscapeRole] = "landscape"; | ||
4545 | 287 | names[MessagesRole] = "messages"; | ||
4546 | 288 | names[PrinterNameRole] = "printerName"; | ||
4547 | 289 | names[PrintRangeRole] = "printRange"; | ||
4548 | 290 | names[PrintRangeModeRole] = "printRangeMode"; | ||
4549 | 291 | names[ProcessingTimeRole] = "processingTime"; | ||
4550 | 292 | names[QualityRole] = "quality"; | ||
4551 | 293 | names[ReverseRole] = "reverse"; | ||
4552 | 294 | names[SizeRole] = "size"; | ||
4553 | 295 | names[StateRole] = "state"; | ||
4554 | 296 | names[TitleRole] = "title"; | ||
4555 | 297 | names[UserRole] = "user"; | ||
4556 | 298 | names[LastStateMessageRole] = "lastStateMessage"; | ||
4557 | 299 | } | ||
4558 | 300 | |||
4559 | 301 | return names; | ||
4560 | 302 | } | ||
4561 | 303 | |||
4562 | 304 | QVariantMap JobModel::get(const int row) const | ||
4563 | 305 | { | ||
4564 | 306 | QHashIterator<int, QByteArray> iterator(roleNames()); | ||
4565 | 307 | QVariantMap result; | ||
4566 | 308 | QModelIndex modelIndex = index(row, 0); | ||
4567 | 309 | |||
4568 | 310 | while (iterator.hasNext()) { | ||
4569 | 311 | iterator.next(); | ||
4570 | 312 | result[iterator.value()] = modelIndex.data(iterator.key()); | ||
4571 | 313 | } | ||
4572 | 314 | |||
4573 | 315 | return result; | ||
4574 | 316 | } | ||
4575 | 317 | |||
4576 | 318 | QSharedPointer<PrinterJob> JobModel::getJobById(const int &id) | ||
4577 | 319 | { | ||
4578 | 320 | Q_FOREACH(auto job, m_jobs) { | ||
4579 | 321 | if (job->jobId() == id) { | ||
4580 | 322 | return job; | ||
4581 | 323 | } | ||
4582 | 324 | } | ||
4583 | 325 | return QSharedPointer<PrinterJob>(Q_NULLPTR); | ||
4584 | 326 | } | ||
4585 | 327 | |||
4586 | 328 | |||
4587 | 329 | JobFilter::JobFilter(QObject *parent) : QSortFilterProxyModel(parent) | ||
4588 | 330 | { | ||
4589 | 331 | connect(this, SIGNAL(sourceModelChanged()), SLOT(onSourceModelChanged())); | ||
4590 | 332 | } | ||
4591 | 333 | |||
4592 | 334 | JobFilter::~JobFilter() | ||
4593 | 335 | { | ||
4594 | 336 | } | ||
4595 | 337 | |||
4596 | 338 | QVariantMap JobFilter::get(const int row) const | ||
4597 | 339 | { | ||
4598 | 340 | QHashIterator<int, QByteArray> iterator(roleNames()); | ||
4599 | 341 | QVariantMap result; | ||
4600 | 342 | QModelIndex modelIndex = index(row, 0); | ||
4601 | 343 | |||
4602 | 344 | while (iterator.hasNext()) { | ||
4603 | 345 | iterator.next(); | ||
4604 | 346 | result[iterator.value()] = modelIndex.data(iterator.key()); | ||
4605 | 347 | } | ||
4606 | 348 | |||
4607 | 349 | return result; | ||
4608 | 350 | } | ||
4609 | 351 | |||
4610 | 352 | void JobFilter::onSourceModelChanged() | ||
4611 | 353 | { | ||
4612 | 354 | connect((JobModel*) sourceModel(), | ||
4613 | 355 | SIGNAL(countChanged()), | ||
4614 | 356 | this, | ||
4615 | 357 | SIGNAL(countChanged())); | ||
4616 | 358 | } | ||
4617 | 359 | |||
4618 | 360 | void JobFilter::onSourceModelCountChanged() | ||
4619 | 361 | { | ||
4620 | 362 | Q_EMIT countChanged(); | ||
4621 | 363 | } | ||
4622 | 364 | |||
4623 | 365 | int JobFilter::count() const | ||
4624 | 366 | { | ||
4625 | 367 | return rowCount(); | ||
4626 | 368 | } | ||
4627 | 369 | |||
4628 | 370 | void JobFilter::filterOnPrinterName(const QString &name) | ||
4629 | 371 | { | ||
4630 | 372 | m_printerName = name; | ||
4631 | 373 | m_printerNameFilterEnabled = true; | ||
4632 | 374 | invalidate(); | ||
4633 | 375 | } | ||
4634 | 376 | |||
4635 | 377 | bool JobFilter::filterAcceptsRow(int sourceRow, | ||
4636 | 378 | const QModelIndex &sourceParent) const | ||
4637 | 379 | { | ||
4638 | 380 | bool accepts = true; | ||
4639 | 381 | QModelIndex childIndex = sourceModel()->index(sourceRow, 0, sourceParent); | ||
4640 | 382 | |||
4641 | 383 | if (accepts && m_printerNameFilterEnabled) { | ||
4642 | 384 | QString printerName = childIndex.model()->data( | ||
4643 | 385 | childIndex, JobModel::PrinterNameRole).toString(); | ||
4644 | 386 | accepts = m_printerName == printerName; | ||
4645 | 387 | } | ||
4646 | 388 | |||
4647 | 389 | return accepts; | ||
4648 | 390 | } | ||
4649 | 0 | 391 | ||
4650 | === added file 'modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h' | |||
4651 | --- modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h 1970-01-01 00:00:00 +0000 | |||
4652 | +++ modules/Ubuntu/Components/Extras/Printers/models/jobmodel.h 2017-02-24 12:29:37 +0000 | |||
4653 | @@ -0,0 +1,125 @@ | |||
4654 | 1 | /* | ||
4655 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
4656 | 3 | * | ||
4657 | 4 | * This program is free software; you can redistribute it and/or modify | ||
4658 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
4659 | 6 | * the Free Software Foundation; version 3. | ||
4660 | 7 | * | ||
4661 | 8 | * This program is distributed in the hope that it will be useful, | ||
4662 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4663 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4664 | 11 | * GNU Lesser General Public License for more details. | ||
4665 | 12 | * | ||
4666 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
4667 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4668 | 15 | */ | ||
4669 | 16 | |||
4670 | 17 | #ifndef USC_JOB_MODEL_H | ||
4671 | 18 | #define USC_JOB_MODEL_H | ||
4672 | 19 | |||
4673 | 20 | #include "printers_global.h" | ||
4674 | 21 | #include "backend/backend.h" | ||
4675 | 22 | #include "printer/printerjob.h" | ||
4676 | 23 | |||
4677 | 24 | #include <QAbstractListModel> | ||
4678 | 25 | #include <QByteArray> | ||
4679 | 26 | #include <QModelIndex> | ||
4680 | 27 | #include <QObject> | ||
4681 | 28 | #include <QSharedPointer> | ||
4682 | 29 | #include <QSortFilterProxyModel> | ||
4683 | 30 | #include <QTimer> | ||
4684 | 31 | #include <QVariant> | ||
4685 | 32 | |||
4686 | 33 | class PRINTERS_DECL_EXPORT JobModel : public QAbstractListModel | ||
4687 | 34 | { | ||
4688 | 35 | Q_OBJECT | ||
4689 | 36 | |||
4690 | 37 | Q_PROPERTY(int count READ count NOTIFY countChanged) | ||
4691 | 38 | public: | ||
4692 | 39 | explicit JobModel(QObject *parent = Q_NULLPTR); | ||
4693 | 40 | explicit JobModel(PrinterBackend *backend, | ||
4694 | 41 | QObject *parent = Q_NULLPTR); | ||
4695 | 42 | ~JobModel(); | ||
4696 | 43 | |||
4697 | 44 | enum Roles | ||
4698 | 45 | { | ||
4699 | 46 | // Qt::DisplayRole holds job title | ||
4700 | 47 | IdRole = Qt::UserRole, | ||
4701 | 48 | CollateRole, | ||
4702 | 49 | ColorModelRole, | ||
4703 | 50 | CompletedTimeRole, | ||
4704 | 51 | CopiesRole, | ||
4705 | 52 | CreationTimeRole, | ||
4706 | 53 | DuplexRole, | ||
4707 | 54 | ImpressionsCompletedRole, | ||
4708 | 55 | LandscapeRole, | ||
4709 | 56 | MessagesRole, | ||
4710 | 57 | PrinterNameRole, | ||
4711 | 58 | PrintRangeRole, | ||
4712 | 59 | PrintRangeModeRole, | ||
4713 | 60 | ProcessingTimeRole, | ||
4714 | 61 | QualityRole, | ||
4715 | 62 | ReverseRole, | ||
4716 | 63 | SizeRole, | ||
4717 | 64 | StateRole, | ||
4718 | 65 | TitleRole, | ||
4719 | 66 | UserRole, | ||
4720 | 67 | LastStateMessageRole, | ||
4721 | 68 | LastRole = LastStateMessageRole, | ||
4722 | 69 | }; | ||
4723 | 70 | |||
4724 | 71 | virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||
4725 | 72 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||
4726 | 73 | virtual QHash<int, QByteArray> roleNames() const override; | ||
4727 | 74 | |||
4728 | 75 | int count() const; | ||
4729 | 76 | |||
4730 | 77 | Q_INVOKABLE QVariantMap get(const int row) const; | ||
4731 | 78 | QSharedPointer<PrinterJob> getJobById(const int &id); | ||
4732 | 79 | private: | ||
4733 | 80 | PrinterBackend *m_backend; | ||
4734 | 81 | |||
4735 | 82 | QList<QSharedPointer<PrinterJob>> m_jobs; | ||
4736 | 83 | private Q_SLOTS: | ||
4737 | 84 | void update(); | ||
4738 | 85 | void jobSignalCatchAll(const QString &text, const QString &printer_uri, | ||
4739 | 86 | const QString &printer_name, uint printer_state, | ||
4740 | 87 | const QString &printer_state_reasons, | ||
4741 | 88 | bool printer_is_accepting_jobs, uint job_id, | ||
4742 | 89 | uint job_state, const QString &job_state_reasons, | ||
4743 | 90 | const QString &job_name, | ||
4744 | 91 | uint job_impressions_completed); | ||
4745 | 92 | |||
4746 | 93 | Q_SIGNALS: | ||
4747 | 94 | void countChanged(); | ||
4748 | 95 | }; | ||
4749 | 96 | |||
4750 | 97 | class PRINTERS_DECL_EXPORT JobFilter : public QSortFilterProxyModel | ||
4751 | 98 | { | ||
4752 | 99 | Q_OBJECT | ||
4753 | 100 | Q_PROPERTY(int count READ count NOTIFY countChanged) | ||
4754 | 101 | public: | ||
4755 | 102 | explicit JobFilter(QObject *parent = Q_NULLPTR); | ||
4756 | 103 | ~JobFilter(); | ||
4757 | 104 | |||
4758 | 105 | Q_INVOKABLE QVariantMap get(const int row) const; | ||
4759 | 106 | |||
4760 | 107 | void filterOnPrinterName(const QString &name); | ||
4761 | 108 | int count() const; | ||
4762 | 109 | protected: | ||
4763 | 110 | virtual bool filterAcceptsRow( | ||
4764 | 111 | int sourceRow, const QModelIndex &sourceParent) const override; | ||
4765 | 112 | |||
4766 | 113 | Q_SIGNALS: | ||
4767 | 114 | void countChanged(); | ||
4768 | 115 | |||
4769 | 116 | private Q_SLOTS: | ||
4770 | 117 | void onSourceModelChanged(); | ||
4771 | 118 | void onSourceModelCountChanged(); | ||
4772 | 119 | |||
4773 | 120 | private: | ||
4774 | 121 | QString m_printerName = QString::null; | ||
4775 | 122 | bool m_printerNameFilterEnabled = false; | ||
4776 | 123 | }; | ||
4777 | 124 | |||
4778 | 125 | #endif // USC_JOB_MODEL_H | ||
4779 | 0 | 126 | ||
4780 | === added file 'modules/Ubuntu/Components/Extras/Printers/models/printermodel.cpp' | |||
4781 | --- modules/Ubuntu/Components/Extras/Printers/models/printermodel.cpp 1970-01-01 00:00:00 +0000 | |||
4782 | +++ modules/Ubuntu/Components/Extras/Printers/models/printermodel.cpp 2017-02-24 12:29:37 +0000 | |||
4783 | @@ -0,0 +1,503 @@ | |||
4784 | 1 | /* | ||
4785 | 2 | * Copyright (C) 2017 Canonical, Ltd. | ||
4786 | 3 | * | ||
4787 | 4 | * This program is free software; you can redistribute it and/or modify | ||
4788 | 5 | * it under the terms of the GNU Lesser General Public License as published by | ||
4789 | 6 | * the Free Software Foundation; version 3. | ||
4790 | 7 | * | ||
4791 | 8 | * This program is distributed in the hope that it will be useful, | ||
4792 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
4793 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
4794 | 11 | * GNU Lesser General Public License for more details. | ||
4795 | 12 | * | ||
4796 | 13 | * You should have received a copy of the GNU Lesser General Public License | ||
4797 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
4798 | 15 | */ | ||
4799 | 16 | |||
4800 | 17 | #include "backend/backend_cups.h" | ||
4801 | 18 | #include "backend/backend_pdf.h" | ||
4802 | 19 | #include "i18n.h" | ||
4803 | 20 | #include "models/jobmodel.h" | ||
4804 | 21 | #include "models/printermodel.h" | ||
4805 | 22 | #include "utils.h" | ||
4806 | 23 | |||
4807 | 24 | #include <QDebug> | ||
4808 | 25 | |||
4809 | 26 | PrinterModel::PrinterModel(PrinterBackend *backend, QObject *parent) | ||
4810 | 27 | : QAbstractListModel(parent) | ||
4811 | 28 | , m_backend(backend) | ||
4812 | 29 | { | ||
4813 | 30 | |||
4814 | 31 | QObject::connect(m_backend, &PrinterBackend::printerAdded, | ||
4815 | 32 | this, &PrinterModel::printerAdded); | ||
4816 | 33 | QObject::connect(m_backend, &PrinterBackend::printerModified, | ||
4817 | 34 | &m_signalHandler, &PrinterSignalHandler::onPrinterModified); | ||
4818 | 35 | QObject::connect(m_backend, &PrinterBackend::printerStateChanged, | ||
4819 | 36 | &m_signalHandler, &PrinterSignalHandler::onPrinterModified); | ||
4820 | 37 | QObject::connect(m_backend, &PrinterBackend::printerDeleted, | ||
4821 | 38 | this, &PrinterModel::printerDeleted); | ||
4822 | 39 | |||
4823 | 40 | connect(&m_signalHandler, SIGNAL(printerModified(const QString&)), | ||
4824 | 41 | this, SLOT(printerModified(const QString&))); | ||
4825 | 42 | connect(m_backend, SIGNAL(printerLoaded(QSharedPointer<Printer>)), | ||
4826 | 43 | this, SLOT(printerLoaded(QSharedPointer<Printer>))); | ||
4827 | 44 | |||
4828 | 45 | // Create printer proxies for every printerName. | ||
4829 | 46 | Q_FOREACH(auto printerName, m_backend->availablePrinterNames()) { | ||
4830 | 47 | auto p = QSharedPointer<Printer>(new Printer(new PrinterBackend(printerName))); | ||
4831 | 48 | addPrinter(p, CountChangeSignal::Defer); | ||
4832 | 49 | } | ||
4833 | 50 | |||
4834 | 51 | // Add a PDF printer. | ||
4835 | 52 | auto pdfPrinter = QSharedPointer<Printer>( | ||
4836 | 53 | new Printer(new PrinterPdfBackend(__("Create PDF"))) | ||
4837 | 54 | ); | ||
4838 | 55 | addPrinter(pdfPrinter, CountChangeSignal::Defer); | ||
4839 | 56 | |||
4840 | 57 | Q_EMIT countChanged(); | ||
4841 | 58 | } | ||
4842 | 59 | |||
4843 | 60 | PrinterModel::~PrinterModel() | ||
4844 | 61 | { | ||
4845 | 62 | } | ||
4846 | 63 | |||
4847 | 64 | void PrinterModel::printerLoaded(QSharedPointer<Printer> printer) | ||
4848 | 65 | { | ||
4849 | 66 | // Find and possibly replace an old printer. | ||
4850 | 67 | for (int i=0; i < m_printers.count(); i++) { | ||
4851 | 68 | auto oldPrinter = m_printers.at(i); | ||
4852 | 69 | if (printer->name() == oldPrinter->name()) { | ||
4853 | 70 | if (!oldPrinter->deepCompare(printer)) { | ||
4854 | 71 | updatePrinter(oldPrinter, printer); | ||
4855 | 72 | } | ||
4856 | 73 | |||
4857 | 74 | // We're done. | ||
4858 | 75 | return; | ||
4859 | 76 | } | ||
4860 | 77 | } | ||
4861 | 78 | |||
4862 | 79 | addPrinter(printer, CountChangeSignal::Emit); | ||
4863 | 80 | } | ||
4864 | 81 | |||
4865 | 82 | void PrinterModel::printerModified(const QString &printerName) | ||
4866 | 83 | { | ||
4867 | 84 | // These signals might be emitted of a now deleted printer. | ||
4868 | 85 | if (getPrinterByName(printerName)) | ||
4869 | 86 | m_backend->requestPrinter(printerName); | ||
4870 | 87 | } | ||
4871 | 88 | |||
4872 | 89 | void PrinterModel::printerAdded( | ||
4873 | 90 | const QString &text, const QString &printerUri, | ||
4874 | 91 | const QString &printerName, uint printerState, | ||
4875 | 92 | const QString &printerStateReason, bool acceptingJobs) | ||
4876 | 93 | { | ||
4877 | 94 | Q_UNUSED(text); | ||
4878 | 95 | Q_UNUSED(printerUri); | ||
4879 | 96 | Q_UNUSED(printerState); | ||
4880 | 97 | Q_UNUSED(printerStateReason); | ||
4881 | 98 | Q_UNUSED(acceptingJobs); | ||
4882 | 99 | |||
4883 | 100 | m_backend->requestPrinter(printerName); | ||
4884 | 101 | } | ||
4885 | 102 | |||
4886 | 103 | void PrinterModel::printerDeleted( | ||
4887 | 104 | const QString &text, const QString &printerUri, | ||
4888 | 105 | const QString &printerName, uint printerState, | ||
4889 | 106 | const QString &printerStateReason, bool acceptingJobs) | ||
4890 | 107 | { | ||
4891 | 108 | Q_UNUSED(text); | ||
4892 | 109 | Q_UNUSED(printerUri); | ||
4893 | 110 | Q_UNUSED(printerState); | ||
4894 | 111 | Q_UNUSED(printerStateReason); | ||
4895 | 112 | Q_UNUSED(acceptingJobs); | ||
4896 | 113 | |||
4897 | 114 | auto printer = getPrinterByName(printerName); | ||
4898 | 115 | if (printer) { | ||
4899 | 116 | removePrinter(printer, CountChangeSignal::Emit); | ||
4900 | 117 | } | ||
4901 | 118 | } | ||
4902 | 119 | |||
4903 | 120 | QSharedPointer<Printer> PrinterModel::getPrinterByName(const QString &printerName) | ||
4904 | 121 | { | ||
4905 | 122 | Q_FOREACH(auto p, m_printers) { | ||
4906 | 123 | if (p->name() == printerName) | ||
4907 | 124 | return p; | ||
4908 | 125 | } | ||
4909 | 126 | return QSharedPointer<Printer>(Q_NULLPTR); | ||
4910 | 127 | } | ||
4911 | 128 | |||
4912 | 129 | void PrinterModel::movePrinter(const int &from, const int &to) | ||
4913 | 130 | { | ||
4914 | 131 | int size = m_printers.size(); | ||
4915 | 132 | if (from < 0 || to < 0 || from >= size || to >= size) { | ||
4916 | 133 | qWarning() << Q_FUNC_INFO << "Illegal move operation from" | ||
4917 | 134 | << from << "to" << to << ". Size was" << size; | ||
4918 | 135 | return; | ||
4919 | 136 | } | ||
4920 | 137 | if (!beginMoveRows(QModelIndex(), from, from, QModelIndex(), to)) { | ||
4921 | 138 | qWarning() << Q_FUNC_INFO << "failed to move rows."; | ||
4922 | 139 | return; | ||
4923 | 140 | } | ||
4924 | 141 | m_printers.move(from, to); | ||
4925 | 142 | endMoveRows(); | ||
4926 | 143 | } | ||
4927 | 144 | |||
4928 | 145 | void PrinterModel::removePrinter(QSharedPointer<Printer> printer, const CountChangeSignal ¬ify) | ||
4929 | 146 | { | ||
4930 | 147 | int idx = m_printers.indexOf(printer); | ||
4931 | 148 | beginRemoveRows(QModelIndex(), idx, idx); | ||
4932 | 149 | m_printers.removeAt(idx); | ||
4933 | 150 | endRemoveRows(); | ||
4934 | 151 | |||
4935 | 152 | if (notify == CountChangeSignal::Emit) | ||
4936 | 153 | Q_EMIT countChanged(); | ||
4937 | 154 | } | ||
4938 | 155 | |||
4939 | 156 | void PrinterModel::updatePrinter(QSharedPointer<Printer> old, | ||
4940 | 157 | QSharedPointer<Printer> newPrinter) | ||
4941 | 158 | { | ||
4942 | 159 | int i = m_printers.indexOf(old); | ||
4943 | 160 | QModelIndex idx = index(i); | ||
4944 | 161 | old->updateFrom(newPrinter); | ||
4945 | 162 | Q_EMIT dataChanged(idx, idx); | ||
4946 | 163 | } | ||
4947 | 164 | |||
4948 | 165 | void PrinterModel::addPrinter(QSharedPointer<Printer> printer, const CountChangeSignal ¬ify) | ||
4949 | 166 | { | ||
4950 | 167 | int i = m_printers.size(); | ||
4951 | 168 | beginInsertRows(QModelIndex(), i, i); | ||
4952 | 169 | m_printers.append(printer); | ||
4953 | 170 | endInsertRows(); | ||
4954 | 171 | |||
4955 | 172 | if (notify == CountChangeSignal::Emit) | ||
4956 | 173 | Q_EMIT countChanged(); | ||
4957 | 174 | } | ||
4958 | 175 | |||
4959 | 176 | int PrinterModel::rowCount(const QModelIndex &parent) const | ||
4960 | 177 | { | ||
4961 | 178 | Q_UNUSED(parent); | ||
4962 | 179 | return m_printers.size(); | ||
4963 | 180 | } | ||
4964 | 181 | |||
4965 | 182 | int PrinterModel::count() const | ||
4966 | 183 | { | ||
4967 | 184 | return rowCount(); | ||
4968 | 185 | } | ||
4969 | 186 | |||
4970 | 187 | QVariant PrinterModel::data(const QModelIndex &index, int role) const | ||
4971 | 188 | { | ||
4972 | 189 | QVariant ret; | ||
4973 | 190 | |||
4974 | 191 | if ((0<=index.row()) && (index.row()<m_printers.size())) { | ||
4975 | 192 | |||
4976 | 193 | auto printer = m_printers[index.row()]; | ||
4977 | 194 | |||
4978 | 195 | |||
4979 | 196 | /* If printer is a proxy (not loaded), determine if the requested role | ||
4980 | 197 | is something async and that we need to request the data. */ | ||
4981 | 198 | if (printer->type() == PrinterEnum::PrinterType::ProxyType) { | ||
4982 | 199 | switch (role) { | ||
4983 | 200 | case Qt::DisplayRole: | ||
4984 | 201 | case NameRole: | ||
4985 | 202 | case DefaultPrinterRole: | ||
4986 | 203 | case PrinterRole: | ||
4987 | 204 | case IsPdfRole: | ||
4988 | 205 | case IsLoadedRole: | ||
4989 | 206 | break; // All of these can be inferred from the name (lazily). | ||
4990 | 207 | default: | ||
4991 | 208 | m_backend->requestPrinter(printer->name()); | ||
4992 | 209 | } | ||
4993 | 210 | } | ||
4994 | 211 | |||
4995 | 212 | switch (role) { | ||
4996 | 213 | case NameRole: | ||
4997 | 214 | case Qt::DisplayRole: | ||
4998 | 215 | ret = printer->name(); | ||
4999 | 216 | break; | ||
5000 | 217 | case ColorModelRole: |
The diff has been truncated for viewing.