1 /*
2     SPDX-FileCopyrightText: 2010-2012 Daniel Nicoletti <dantti12@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef KCUPS_REQUEST_H
8 #define KCUPS_REQUEST_H
9 
10 #include <QObject>
11 #include <QEventLoop>
12 
13 #include "KCupsConnection.h"
14 #include "KCupsJob.h"
15 #include "KCupsPrinter.h"
16 #include "KCupsServer.h"
17 #include "KIppRequest.h"
18 
19 class Q_DECL_EXPORT KCupsRequest : public QObject
20 {
21     Q_OBJECT
22 public:
23     /**
24      * Default constructor, it takes no parent
25      * because it will move to KCupsConnection thread
26      *
27      * Before calling any method connect to finished() signal or
28      * use waitTillFinished().
29      * You must delete the object manually after finished
30      * using deleteLater().
31      */
32     explicit KCupsRequest(KCupsConnection *connection = nullptr);
33 
34     /**
35      * This method creates an event loop
36      * and quits after the request is finished
37      */
38     void waitTillFinished();
39 
40     /**
41      * This method returns true if there was an error with the request
42      */
43     bool hasError() const;
44     ipp_status_t error() const;
45     http_status_t httpStatus() const;
46     QString serverError() const;
47     QString errorMsg() const;
48 
49     KCupsConnection* connection() const;
50 
51     /**
52      * Non empty when getPrinters is called and finish is emitted
53      */
54     KCupsPrinters printers() const;
55 
56     /**
57      * Non empty when getPPDs is called and finish is emitted
58      */
59     ReturnArguments ppds() const;
60 
61     /**
62      * Non empty when getServerSettings() is called and finish is emitted
63      */
64     KCupsServer serverSettings() const;
65 
66     /**
67      * Non empty when \sa getPrinterPPD() is called and finish is emitted
68      * \warning You must unlik the given file name
69      */
70     QString printerPPD() const;
71 
72     /**
73      * Non empty when getJobs is called and finish is emitted
74      */
75     KCupsJobs jobs() const;
76 
77     /**
78      * Get all available PPDs from the givem make
79      * @param make the maker of the printer
80      */
81     Q_INVOKABLE void getPPDS(const QString &make = QString());
82 
83     /**
84      * Get all devices that could be added as a printer
85      * This method emits device()
86      */
87     Q_INVOKABLE void getDevices(int timeout = CUPS_TIMEOUT_DEFAULT);
88 
89     /**
90      * Get all devices that could be added as a printer
91      * This method emits device()
92      */
93     Q_INVOKABLE void getDevices(int timeout, QStringList includeSchemes, QStringList excludeSchemes);
94 
95     /**
96      * Get all available printers
97      * @param mask filter the kind of printer that will be emitted (-1 to no filter)
98      * @param requestedAttr the attributes to retrieve from cups
99      * This method emits printer()
100      *
101      * THIS function can get the default server dest through the
102      * "printer-is-default" attribute BUT it does not get user
103      * defined default printer, see cupsGetDefault() on www.cups.org for details
104      */
105     Q_INVOKABLE void getPrinters(QStringList attributes, int mask = -1);
106 
107     /**
108      * Get attributes from a given printer
109      * @param printer The printer to apply the change
110      * @param isClass True it is a printer class
111      * @param attributes The attributes you are requesting
112      *
113      * @return The return will be stored in \sa printers()
114      */
115     Q_INVOKABLE void getPrinterAttributes(const QString &printerName, bool isClass, QStringList attributes);
116 
117     /**
118      * Get all jobs
119      * This method emits job()
120      * TODO we need to see if we authenticate as root to do some taks
121      *      the myJobs will return the user's jobs or the root's jobs
122      * @param printer which printer you are requiring jobs for (empty = all printers)
123      * @param myJobs true if you only want your jobs
124      * @param whichJobs which kind jobs should be sent
125      */
126     Q_INVOKABLE void getJobs(const QString &printerName, bool myJobs, int whichJobs, QStringList attributes);
127 
128     /**
129      * Get attributes from a given printer
130      * @param printer The printer to apply the change
131      * @param isClass True it is a printer class
132      * @param attributes The attributes you are requesting
133      *
134      * @return The return will be stored in \sa printers()
135      */
136     Q_INVOKABLE void getJobAttributes(int jobId, const QString &printerUri, QStringList attributes);
137 
138     /**
139      * Get the CUPS server settings
140      * This method emits server()
141      */
142     Q_INVOKABLE void getServerSettings();
143 
144     /**
145      * Get the PPD associated with @arg printerName
146      * the result is stored at \sa printerPPD()
147      */
148     Q_INVOKABLE void getPrinterPPD(const QString &printerName);
149 
150     /**
151      * Get the CUPS server settings
152      * @param userValues the new server settings
153      */
154     Q_INVOKABLE void setServerSettings(const KCupsServer &server);
155 
156     // ---- Printer Methods
157     /**
158      * Add or Modify a Printer
159      * @param printerName The printer to apply the change
160      * @param attributes The new attributes of the printer
161      * @param filename The file name in case of changing the PPD
162      */
163     void addOrModifyPrinter(const QString &printerName,
164                             const QVariantHash &attributes,
165                             const QString &filename = QString());
166 
167     /**
168      * Add or Modify a Class
169      * @param className The class to apply the change
170      * @param attributes The new attributes of the printer
171      */
172     void addOrModifyClass(const QString &className,
173                           const QVariantHash &attributes);
174 
175     /**
176      * Set if a given printer should be shared among other cups
177      * @param printer The printer to apply the change
178      * @param isClass True it is a printer class
179      * @param shared True if it should be shared
180      */
181     void setShared(const QString &printerName, bool isClass, bool shared);
182 
183     /**
184      * Set if a given printer should be the default one among others
185      * @param printer The printer to apply the change
186      */
187     void setDefaultPrinter(const QString &printerName);
188 
189     /**
190      * Pause the given printer from receiving jobs
191      * @param printer The printer to apply the change
192      */
193     void pausePrinter(const QString &printerName);
194 
195     /**
196      * Resume the given printer from receiving jobs
197      * @param printer The printer to apply the change
198      */
199     void resumePrinter(const QString &printerName);
200 
201     /**
202      * Allows the given printer from receiving jobs
203      * @param printer The printer to apply the change
204      */
205     void acceptJobs(const QString &printerName);
206 
207     /**
208      * Prevents the given printer from receiving jobs
209      * @param printer The printer to apply the change
210      */
211     void rejectJobs(const QString &printerName);
212 
213     /**
214      * Delete the given printer, if it's not local it's not
215      * possible to delete it
216      * @param printer The printer to apply the change
217      */
218     void deletePrinter(const QString &printerName);
219 
220     /**
221      * Print a test page
222      * @param printerName The printer where the test should be done
223      * @param isClass True it is a printer class
224      */
225     void printTestPage(const QString &printerName, bool isClass);
226 
227     /**
228      * Print a command test
229      * @param printerName The printer where the test should be done
230      * @param command The command to print
231      * @param title The title of the command
232      */
233     Q_INVOKABLE void printCommand(const QString &printerName, const QString &command, const QString &title);
234 
235     // Jobs methods
236     /**
237      * Cancels tries to cancel a given job
238      * @param printerName the destination name (printer)
239      * @param jobId the job identification
240      */
241     void cancelJob(const QString &printerName, int jobId);
242 
243     /**
244      * Holds the printing of a given job
245      * @param printerName the destination name (printer)
246      * @param jobId the job identification
247      */
248     void holdJob(const QString &printerName, int jobId);
249 
250     /**
251      * Holds the printing of a given job
252      * @param printerName the destination name (printer)
253      * @param jobId the job identification
254      */
255     void releaseJob(const QString &printerName, int jobId);
256 
257     /**
258      * Restart the printing of a given job
259      * @param printerName the destination name (printer)
260      * @param jobId the job identification
261      */
262     void restartJob(const QString &printerName, int jobId);
263 
264     /**
265      * Holds the printing of a given job
266      * @param fromDestName the destination name which holds the job
267      * @param jobId the job identification
268      * @param toDestName the destination to hold the job
269      */
270     void moveJob(const QString &fromPrinterName, int jobId, const QString &toPrinterName);
271 
272     void authenticateJob(const QString &printerName, const QStringList authInfo, int jobId);
273 
274 signals:
275     void device(const QString &device_class,
276                 const QString &device_id,
277                 const QString &device_info,
278                 const QString &device_make_and_model,
279                 const QString &device_uri,
280                 const QString &device_location);
281 
282     void finished(KCupsRequest *);
283 
284 private:
285     void invokeMethod(const char *method,
286                       const QVariant &arg1 = QVariant(),
287                       const QVariant &arg2 = QVariant(),
288                       const QVariant &arg3 = QVariant(),
289                       const QVariant &arg4 = QVariant(),
290                       const QVariant &arg5 = QVariant(),
291                       const QVariant &arg6 = QVariant(),
292                       const QVariant &arg7 = QVariant(),
293                       const QVariant &arg8 = QVariant());
294     Q_INVOKABLE void process(const KIppRequest &request);
295     void setError(http_status_t httpStatus, ipp_status_t error, const QString &errorMsg);
296     void setFinished(bool delayed = false);
297 
298     KCupsConnection *m_connection;
299     QEventLoop m_loop;
300     bool m_finished = true;
301     ipp_status_t m_error = IPP_OK;
302     http_status_t m_httpStatus;
303     QString m_errorMsg;
304     ReturnArguments m_ppds;
305     KCupsServer m_server;
306     QString m_ppdFile;
307     KCupsPrinters m_printers;
308     KCupsJobs m_jobs;
309 };
310 
311 #endif // KCUPS_REQUEST_H
312