~ci-train-bot/ubuntu-settings-components/ubuntu-settings-components-ubuntu-zesty-2381

« back to all changes in this revision

Viewing changes to plugins/Ubuntu/Settings/Printers/cups/cupspkhelper.h

  • Committer: Bileto Bot
  • Date: 2017-01-23 23:56:12 UTC
  • mfrom: (176.2.39 printer-components)
  • Revision ID: ci-train-bot@canonical.com-20170123235612-ugmiiaddrk817pfe
* packaging: suggest cups, depend on libcups2-dev
* adds cups bindings for printer/job management

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2017 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#include <cups/cups.h>
 
18
#include <cups/http.h>
 
19
#include <cups/ipp.h>
 
20
#include <cups/ppd.h>
 
21
 
 
22
 
 
23
#include <QString>
 
24
#include <QStringList>
 
25
 
 
26
/* From https://bugzilla.novell.com/show_bug.cgi?id=447444#c5
 
27
 * We need to define a maximum length for strings to avoid cups
 
28
 * thinking there are multiple lines.
 
29
 */
 
30
#define CPH_STR_MAXLEN 512
 
31
 
 
32
/* This code is only a shim for systems not running the daemon provided by
 
33
cups-pk-helper. Once provided on all platforms, this code should be replaced
 
34
by proper dbus bindings, and subsequently be set on fire. */
 
35
 
 
36
/* TODO: rename to CupsPkHelperShim to emphasize its transient nature.
 
37
   FIXME: set m_internalStatus to mutable and make most of the "is..." methods
 
38
          const.
 
39
*/
 
40
class CupsPkHelper
 
41
{
 
42
public:
 
43
    explicit CupsPkHelper();
 
44
    ~CupsPkHelper();
 
45
 
 
46
    bool printerClassSetInfo(const QString &name, const QString &info);
 
47
    bool printerClassSetOption(const QString &name, const QString &option,
 
48
                               const QStringList &values);
 
49
    ppd_file_t* getPpdFile(const QString &name, const QString &instance) const;
 
50
    cups_dest_t* getDest(const QString &name, const QString &instance) const;
 
51
 
 
52
    QString getLastError() const;
 
53
 
 
54
private:
 
55
    enum CphResource
 
56
    {
 
57
        CphResourceRoot = 0,
 
58
        CphResourceAdmin,
 
59
        CphResourceJobs,
 
60
    };
 
61
 
 
62
    bool sendNewPrinterClassRequest(const QString &printerName,
 
63
                                    ipp_tag_t group,
 
64
                                    ipp_tag_t type,
 
65
                                    const QString &name,
 
66
                                    const QString &value);
 
67
    static void addPrinterUri(ipp_t *request, const QString &name);
 
68
    static void addRequestingUsername(ipp_t *request, const QString &username);
 
69
    static const QString getResource(const CphResource &resource);
 
70
    static bool isPrinterNameValid(const QString &name);
 
71
    static void addClassUri(ipp_t *request, const QString &name);
 
72
    static bool isStringValid(const QString &string,
 
73
                              const bool checkNull = false,
 
74
                              const int maxLength = 512);
 
75
    static bool isStringPrintable(const QString &string, const bool checkNull,
 
76
                                  const int maxLength);
 
77
 
 
78
    QString preparePpdForOptions(const QString &ppdfile,
 
79
                                 cups_option_t *options,
 
80
                                 int numOptions);
 
81
    bool printerIsClass(const QString &name);
 
82
    void setInternalStatus(const QString &status);
 
83
    bool postRequest(ipp_t *request, const QString &file,
 
84
                     const CphResource &resource);
 
85
    bool sendRequest(ipp_t *request, const CphResource &resource);
 
86
    bool handleReply(ipp_t *reply);
 
87
    bool isReplyOk(ipp_t *reply, bool deleteIfReplyNotOk);
 
88
    void setErrorFromReply(ipp_t *reply);
 
89
 
 
90
    http_t *m_connection;
 
91
    ipp_status_t m_lastStatus = IPP_OK;
 
92
    mutable QString m_internalStatus = QString::null;
 
93
};