File indexing completed on 2025-01-05 05:09:30
0001 /* 0002 SPDX-FileCopyrightText: 2010-2012 Daniel Nicoletti <dantti12@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef KCUPS_REQUEST_H 0008 #define KCUPS_REQUEST_H 0009 0010 #include <QEventLoop> 0011 #include <QObject> 0012 0013 #include <kcupslib_export.h> 0014 0015 #include "KCupsConnection.h" 0016 #include "KCupsJob.h" 0017 #include "KCupsPrinter.h" 0018 #include "KCupsServer.h" 0019 #include "KIppRequest.h" 0020 0021 class KCUPSLIB_EXPORT KCupsRequest : public QObject 0022 { 0023 Q_OBJECT 0024 public: 0025 /** 0026 * Default constructor, it takes no parent 0027 * because it will move to KCupsConnection thread 0028 * 0029 * Before calling any method connect to finished() signal or 0030 * use waitTillFinished(). 0031 * You must delete the object manually after finished 0032 * using deleteLater(). 0033 */ 0034 explicit KCupsRequest(KCupsConnection *connection = nullptr); 0035 0036 /** 0037 * This method creates an event loop 0038 * and quits after the request is finished 0039 */ 0040 void waitTillFinished(); 0041 0042 /** 0043 * This method returns true if there was an error with the request 0044 */ 0045 bool hasError() const; 0046 ipp_status_t error() const; 0047 http_status_t httpStatus() const; 0048 QString serverError() const; 0049 QString errorMsg() const; 0050 0051 KCupsConnection *connection() const; 0052 0053 /** 0054 * Non empty when getPrinters is called and finish is emitted 0055 */ 0056 KCupsPrinters printers() const; 0057 0058 /** 0059 * Non empty when getPPDs is called and finish is emitted 0060 */ 0061 ReturnArguments ppds() const; 0062 0063 /** 0064 * Non empty when getServerSettings() is called and finish is emitted 0065 */ 0066 KCupsServer serverSettings() const; 0067 0068 /** 0069 * Non empty when \sa getPrinterPPD() is called and finish is emitted 0070 * \warning You must unlik the given file name 0071 */ 0072 QString printerPPD() const; 0073 0074 /** 0075 * Non empty when getJobs is called and finish is emitted 0076 */ 0077 KCupsJobs jobs() const; 0078 0079 /** 0080 * Get all available PPDs from the givem make 0081 * @param make the maker of the printer 0082 */ 0083 Q_INVOKABLE void getPPDS(const QString &make = QString()); 0084 0085 /** 0086 * Get all devices that could be added as a printer 0087 * This method emits device() 0088 */ 0089 Q_INVOKABLE void getDevices(int timeout = CUPS_TIMEOUT_DEFAULT); 0090 0091 /** 0092 * Get all devices that could be added as a printer 0093 * This method emits device() 0094 */ 0095 Q_INVOKABLE void getDevices(int timeout, QStringList includeSchemes, QStringList excludeSchemes); 0096 0097 /** 0098 * Get all available printers 0099 * @param mask filter the kind of printer that will be emitted (-1 to no filter) 0100 * @param requestedAttr the attributes to retrieve from cups 0101 * This method emits printer() 0102 * 0103 * THIS function can get the default server dest through the 0104 * "printer-is-default" attribute BUT it does not get user 0105 * defined default printer, see cupsGetDefault() on www.cups.org for details 0106 */ 0107 Q_INVOKABLE void getPrinters(QStringList attributes, int mask = -1); 0108 0109 /** 0110 * Get attributes from a given printer 0111 * @param printer The printer to apply the change 0112 * @param isClass True it is a printer class 0113 * @param attributes The attributes you are requesting 0114 * 0115 * @return The return will be stored in \sa printers() 0116 */ 0117 Q_INVOKABLE void getPrinterAttributes(const QString &printerName, bool isClass, QStringList attributes); 0118 0119 /** 0120 * Get all jobs 0121 * This method emits job() 0122 * TODO we need to see if we authenticate as root to do some taks 0123 * the myJobs will return the user's jobs or the root's jobs 0124 * @param printer which printer you are requiring jobs for (empty = all printers) 0125 * @param myJobs true if you only want your jobs 0126 * @param whichJobs which kind jobs should be sent 0127 */ 0128 Q_INVOKABLE void getJobs(const QString &printerName, bool myJobs, int whichJobs, QStringList attributes); 0129 0130 /** 0131 * Get attributes from a given printer 0132 * @param printer The printer to apply the change 0133 * @param isClass True it is a printer class 0134 * @param attributes The attributes you are requesting 0135 * 0136 * @return The return will be stored in \sa printers() 0137 */ 0138 Q_INVOKABLE void getJobAttributes(int jobId, const QString &printerUri, QStringList attributes); 0139 0140 /** 0141 * Get the CUPS server settings 0142 * This method emits server() 0143 */ 0144 Q_INVOKABLE void getServerSettings(); 0145 0146 /** 0147 * Get the PPD associated with @arg printerName 0148 * the result is stored at \sa printerPPD() 0149 */ 0150 Q_INVOKABLE void getPrinterPPD(const QString &printerName); 0151 0152 /** 0153 * Get the CUPS server settings 0154 * @param userValues the new server settings 0155 */ 0156 Q_INVOKABLE void setServerSettings(const KCupsServer &server); 0157 0158 // ---- Printer Methods 0159 /** 0160 * Add or Modify a Printer 0161 * @param printerName The printer to apply the change 0162 * @param attributes The new attributes of the printer 0163 * @param filename The file name in case of changing the PPD 0164 */ 0165 void addOrModifyPrinter(const QString &printerName, const QVariantMap &attributes, const QString &filename = QString()); 0166 0167 /** 0168 * Add or Modify a Class 0169 * @param className The class to apply the change 0170 * @param attributes The new attributes of the printer 0171 */ 0172 void addOrModifyClass(const QString &className, const QVariantMap &attributes); 0173 0174 /** 0175 * Set if a given printer should be shared among other cups 0176 * @param printer The printer to apply the change 0177 * @param isClass True it is a printer class 0178 * @param shared True if it should be shared 0179 */ 0180 void setShared(const QString &printerName, bool isClass, bool shared); 0181 0182 /** 0183 * Set if a given printer should be the default one among others 0184 * @param printer The printer to apply the change 0185 */ 0186 void setDefaultPrinter(const QString &printerName); 0187 0188 /** 0189 * Pause the given printer from receiving jobs 0190 * @param printer The printer to apply the change 0191 */ 0192 void pausePrinter(const QString &printerName); 0193 0194 /** 0195 * Resume the given printer from receiving jobs 0196 * @param printer The printer to apply the change 0197 */ 0198 void resumePrinter(const QString &printerName); 0199 0200 /** 0201 * Allows the given printer from receiving jobs 0202 * @param printer The printer to apply the change 0203 */ 0204 void acceptJobs(const QString &printerName); 0205 0206 /** 0207 * Prevents the given printer from receiving jobs 0208 * @param printer The printer to apply the change 0209 */ 0210 void rejectJobs(const QString &printerName); 0211 0212 /** 0213 * Delete the given printer, if it's not local it's not 0214 * possible to delete it 0215 * @param printer The printer to apply the change 0216 */ 0217 void deletePrinter(const QString &printerName); 0218 0219 /** 0220 * Print a test page 0221 * @param printerName The printer where the test should be done 0222 * @param isClass True it is a printer class 0223 */ 0224 void printTestPage(const QString &printerName, bool isClass); 0225 0226 /** 0227 * Print a command test 0228 * @param printerName The printer where the test should be done 0229 * @param command The command to print 0230 * @param title The title of the command 0231 */ 0232 Q_INVOKABLE void printCommand(const QString &printerName, const QString &command, const QString &title); 0233 0234 // Jobs methods 0235 /** 0236 * Cancels tries to cancel a given job 0237 * @param printerName the destination name (printer) 0238 * @param jobId the job identification 0239 */ 0240 void cancelJob(const QString &printerName, int jobId); 0241 0242 /** 0243 * Holds the printing of a given job 0244 * @param printerName the destination name (printer) 0245 * @param jobId the job identification 0246 */ 0247 void holdJob(const QString &printerName, int jobId); 0248 0249 /** 0250 * Holds the printing of a given job 0251 * @param printerName the destination name (printer) 0252 * @param jobId the job identification 0253 */ 0254 void releaseJob(const QString &printerName, int jobId); 0255 0256 /** 0257 * Restart the printing of a given job 0258 * @param printerName the destination name (printer) 0259 * @param jobId the job identification 0260 */ 0261 void restartJob(const QString &printerName, int jobId); 0262 0263 /** 0264 * Holds the printing of a given job 0265 * @param fromDestName the destination name which holds the job 0266 * @param jobId the job identification 0267 * @param toDestName the destination to hold the job 0268 */ 0269 void moveJob(const QString &fromPrinterName, int jobId, const QString &toPrinterName); 0270 0271 void authenticateJob(const QString &printerName, const QStringList authInfo, int jobId); 0272 0273 signals: 0274 void device(const QString &device_class, 0275 const QString &device_id, 0276 const QString &device_info, 0277 const QString &device_make_and_model, 0278 const QString &device_uri, 0279 const QString &device_location); 0280 0281 void finished(KCupsRequest *); 0282 0283 private: 0284 void invokeMethod(const char *method, 0285 const QVariant &arg1 = QVariant(), 0286 const QVariant &arg2 = QVariant(), 0287 const QVariant &arg3 = QVariant(), 0288 const QVariant &arg4 = QVariant(), 0289 const QVariant &arg5 = QVariant(), 0290 const QVariant &arg6 = QVariant(), 0291 const QVariant &arg7 = QVariant(), 0292 const QVariant &arg8 = QVariant()); 0293 Q_INVOKABLE void process(const KIppRequest &request); 0294 void setError(http_status_t httpStatus, ipp_status_t error, const QString &errorMsg); 0295 void setFinished(bool delayed = false); 0296 0297 KCupsConnection *m_connection = nullptr; 0298 QEventLoop m_loop; 0299 bool m_finished = true; 0300 ipp_status_t m_error = IPP_OK; 0301 http_status_t m_httpStatus; 0302 QString m_errorMsg; 0303 ReturnArguments m_ppds; 0304 KCupsServer m_server; 0305 QString m_ppdFile; 0306 KCupsPrinters m_printers; 0307 KCupsJobs m_jobs; 0308 }; 0309 0310 #endif // KCUPS_REQUEST_H