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

« back to all changes in this revision

Viewing changes to plugins/Ubuntu/Settings/Printers/cups/printerdriverloader.cpp

  • Committer: Bileto Bot
  • Date: 2017-02-21 16:16:01 UTC
  • mfrom: (176.2.56 printer-components)
  • Revision ID: ci-train-bot@canonical.com-20170221161601-b6xdyrpew24xfnpl
* 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 "printerdriverloader.h"
 
18
 
 
19
PrinterDriverLoader::PrinterDriverLoader(
 
20
        const QString &deviceId, const QString &language,
 
21
        const QString &makeModel, const QString &product,
 
22
        const QStringList &includeSchemes, const QStringList &excludeSchemes)
 
23
    : m_deviceId(deviceId)
 
24
    , m_language(language)
 
25
    , m_makeModel(makeModel)
 
26
    , m_product(product)
 
27
    , m_includeSchemes(includeSchemes)
 
28
    , m_excludeSchemes(excludeSchemes)
 
29
{
 
30
}
 
31
 
 
32
PrinterDriverLoader::~PrinterDriverLoader()
 
33
{
 
34
}
 
35
 
 
36
void PrinterDriverLoader::process()
 
37
{
 
38
    m_running = true;
 
39
 
 
40
    ipp_t* response = client.createPrinterDriversRequest(
 
41
        m_deviceId, m_language, m_makeModel, m_product, m_includeSchemes,
 
42
        m_excludeSchemes
 
43
    );
 
44
 
 
45
    // Note: if the response somehow fails, we return.
 
46
    if (!response || ippGetStatusCode(response) > IPP_OK_CONFLICT) {
 
47
        QString err(cupsLastErrorString());
 
48
        qWarning() << Q_FUNC_INFO << "Cups HTTP error:" << err;
 
49
 
 
50
        if (response)
 
51
            ippDelete(response);
 
52
 
 
53
        Q_EMIT error(err);
 
54
        Q_EMIT finished();
 
55
        return;
 
56
    }
 
57
 
 
58
    ipp_attribute_t *attr;
 
59
    QByteArray ppdDeviceId;
 
60
    QByteArray ppdLanguage;
 
61
    QByteArray ppdMakeModel;
 
62
    QByteArray ppdName;
 
63
 
 
64
    // cups_option_t option;
 
65
    QList<PrinterDriver> drivers;
 
66
 
 
67
    for (attr = ippFirstAttribute(response); attr != NULL && m_running; attr = ippNextAttribute(response)) {
 
68
 
 
69
        while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
 
70
            attr = ippNextAttribute(response);
 
71
 
 
72
        if (attr == NULL)
 
73
            break;
 
74
 
 
75
        // Pull the needed attributes from this PPD...
 
76
        ppdDeviceId = "NONE";
 
77
        ppdLanguage.clear();
 
78
        ppdMakeModel.clear();
 
79
        ppdName.clear();
 
80
 
 
81
        while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
 
82
            if (!strcmp(ippGetName(attr), "ppd-device-id") &&
 
83
                 ippGetValueTag(attr) == IPP_TAG_TEXT) {
 
84
                ppdDeviceId = ippGetString(attr, 0, NULL);
 
85
            } else if (!strcmp(ippGetName(attr), "ppd-natural-language") &&
 
86
                       ippGetValueTag(attr) == IPP_TAG_LANGUAGE) {
 
87
                ppdLanguage = ippGetString(attr, 0, NULL);
 
88
 
 
89
            } else if (!strcmp(ippGetName(attr), "ppd-make-and-model") &&
 
90
                       ippGetValueTag(attr) == IPP_TAG_TEXT) {
 
91
                ppdMakeModel = ippGetString(attr, 0, NULL);
 
92
            } else if (!strcmp(ippGetName(attr), "ppd-name") &&
 
93
                       ippGetValueTag(attr) == IPP_TAG_NAME) {
 
94
 
 
95
                ppdName = ippGetString(attr, 0, NULL);
 
96
            }
 
97
 
 
98
            attr = ippNextAttribute(response);
 
99
        }
 
100
 
 
101
        // See if we have everything needed...
 
102
        if (ppdLanguage.isEmpty() || ppdMakeModel.isEmpty() ||
 
103
            ppdName.isEmpty()) {
 
104
            if (attr == NULL)
 
105
                break;
 
106
            else
 
107
                continue;
 
108
        }
 
109
 
 
110
        PrinterDriver m;
 
111
        m.name = ppdName;
 
112
        m.deviceId = ppdDeviceId;
 
113
        m.makeModel = ppdMakeModel;
 
114
        m.language = ppdLanguage;
 
115
 
 
116
        drivers.append(m);
 
117
    }
 
118
 
 
119
    ippDelete(response);
 
120
 
 
121
    Q_EMIT loaded(drivers);
 
122
    Q_EMIT finished();
 
123
}
 
124
 
 
125
void PrinterDriverLoader::cancel()
 
126
{
 
127
    m_running = false;
 
128
}