KIppRequest.cpp 9.38 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2010-2018 by Daniel Nicoletti                           *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *   dantti12@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; see the file COPYING. If not, write to       *
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 *   Boston, MA 02110-1301, USA.                                           *
 ***************************************************************************/

#include "KIppRequest.h"
#include "KIppRequest_p.h"

Jan Grulich's avatar
Jan Grulich committed
24
#include "Debug.h"
25

26
27
28
29
30
31
32
33
34
35
36
KIppRequest::KIppRequest() :
    d_ptr(new KIppRequestPrivate)
{
}

KIppRequest::KIppRequest(const KIppRequest &other) :
    d_ptr(new KIppRequestPrivate)
{
    *this = other;
}

37
KIppRequest::KIppRequest(ipp_op_t operation, const QString &resource, const QString &filename) :
38
39
40
    d_ptr(new KIppRequestPrivate)
{
    Q_D(KIppRequest);
41

42
    d->operation = operation;
43
    d->resource = resource;
44
45
46
    d->filename = filename;

    // send our user name on the request too
47
    addString(IPP_TAG_OPERATION, IPP_TAG_NAME, QLatin1String(KCUPS_REQUESTING_USER_NAME), QString::fromUtf8(cupsUser()));
48
49
50
51
}

KIppRequest::~KIppRequest()
{
52
    delete d_ptr;
53
54
}

55
56
57
58
59
60
ipp_op_t KIppRequest::operation() const
{
    Q_D(const KIppRequest);
    return d->operation;
}

61
QString KIppRequest::resource() const
62
63
{
    Q_D(const KIppRequest);
64
    return d->resource;
65
66
}

67
QString KIppRequest::filename() const
68
{
69
70
71
72
73
74
75
    Q_D(const KIppRequest);
    return d->filename;
}

ipp_t *KIppRequest::sendIppRequest() const
{
    Q_D(const KIppRequest);
76
77
78
79
80

    ipp_t *request = ippNewRequest(d->operation);

    d->addRawRequestsToIpp(request);

81
    if (d->filename.isNull()) {
82
        return cupsDoRequest(CUPS_HTTP_DEFAULT, request, qUtf8Printable(d->resource));
83
    } else {
84
        return cupsDoFileRequest(CUPS_HTTP_DEFAULT, request, qUtf8Printable(d->resource), qUtf8Printable(d->filename));
85
86
87
88
89
90
91
    }
}

void KIppRequest::addString(ipp_tag_t group, ipp_tag_t valueTag, const QString &name, const QString &value)
{
    Q_D(KIppRequest);

92
    d->addRequest(group, valueTag, name, value);
93
94
95
96
97
98
}

void KIppRequest::addStringList(ipp_tag_t group, ipp_tag_t valueTag, const QString &name, const QStringList &value)
{
    Q_D(KIppRequest);

99
    d->addRequest(group, valueTag, name, value);
100
101
102
103
104
105
}

void KIppRequest::addInteger(ipp_tag_t group, ipp_tag_t valueTag, const QString &name, int value)
{
    Q_D(KIppRequest);

106
    d->addRequest(group, valueTag, name, value);
107
108
109
110
111
112
}

void KIppRequest::addBoolean(ipp_tag_t group, const QString &name, bool value)
{
    Q_D(KIppRequest);

113
    d->addRequest(group, IPP_TAG_ZERO, name, value);
114
115
116
117
}

void KIppRequest::addVariantValues(const QVariantHash &values)
{
118
    auto i = values.constBegin();
119
    while (i != values.constEnd()) {
120
121
        const QString &key = i.key();
        const QVariant &value = i.value();
122
123
        switch (value.type()) {
        case QVariant::Bool:
124
            // Still in use at add-printer/PageAddPrinter.cpp
125
126
127
128
129
130
131
            if (key == QLatin1String(KCUPS_PRINTER_IS_ACCEPTING_JOBS)) {
                addBoolean(IPP_TAG_PRINTER, key, value.toBool());
            } else {
                addBoolean(IPP_TAG_OPERATION, key, value.toBool());
            }
            break;
        case QVariant::Int:
132
133
            // Still in use at add-printer/PageAddPrinter.cpp
            if (key == QLatin1String(KCUPS_PRINTER_STATE)) {
134
135
136
137
138
139
                addInteger(IPP_TAG_PRINTER, IPP_TAG_ENUM, key, value.toInt());
            } else {
                addInteger(IPP_TAG_OPERATION, IPP_TAG_ENUM, key, value.toInt());
            }
            break;
        case QVariant::String:
140
            // Still in use at add-printer/*
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
            if (key == QLatin1String(KCUPS_DEVICE_URI)) {
                // device uri has a different TAG
                addString(IPP_TAG_PRINTER, IPP_TAG_URI, key, value.toString());
            } else if (key == QLatin1String(KCUPS_PRINTER_OP_POLICY) ||
                       key == QLatin1String(KCUPS_PRINTER_ERROR_POLICY) ||
                       key == QLatin1String("ppd-name")) {
                // printer-op-policy has a different TAG
                addString(IPP_TAG_PRINTER, IPP_TAG_NAME, key, value.toString());
            } else if (key == QLatin1String(KCUPS_JOB_NAME)) {
                addString(IPP_TAG_OPERATION, IPP_TAG_NAME, key, value.toString());
            } else if (key == QLatin1String(KCUPS_WHICH_JOBS)) {
                addString(IPP_TAG_OPERATION, IPP_TAG_KEYWORD, key, value.toString());
            } else {
                addString(IPP_TAG_PRINTER, IPP_TAG_TEXT, key, value.toString());
            }
            break;
        case QVariant::StringList:
            if (key == QLatin1String(KCUPS_MEMBER_URIS)) {
                addStringList(IPP_TAG_PRINTER, IPP_TAG_URI, key, value.toStringList());
            } else {
                addStringList(IPP_TAG_PRINTER, IPP_TAG_NAME, key, value.toStringList());
            }
            break;
        case QVariant::UInt:
            addInteger(IPP_TAG_OPERATION, IPP_TAG_ENUM, key, value.toInt());
            break;
        default:
Jan Grulich's avatar
Jan Grulich committed
168
            qCWarning(LIBKCUPS) << "type NOT recognized! This will be ignored:" << key << "values" << i.value();
169
170
171
172
173
        }
        ++i;
    }
}

174
175
176
void KIppRequest::addPrinterUri(const QString &printerName, bool isClass)
{
    QString uri = assembleUrif(printerName, isClass);
177
    addString(IPP_TAG_OPERATION, IPP_TAG_URI, QLatin1String(KCUPS_PRINTER_URI), uri);
178
179
}

180
181
182
183
184
185
QString KIppRequest::assembleUrif(const QString &name, bool isClass)
{
    char  uri[HTTP_MAX_URI]; // printer URI

    QString destination;
    if (isClass) {
186
        destination = QLatin1String("/classes/") + name;
187
    } else {
188
        destination = QLatin1String("/printers/") + name;
189
190
    }

191
192
    httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", cupsUser(), "localhost",
                    ippPort(), destination.toUtf8().constData());
193
    return QString::fromLatin1(uri);
194
195
}

196
197
198
199
200
201
202
203
204
205
206
KIppRequest &KIppRequest::operator =(const KIppRequest &other)
{
    Q_D(KIppRequest);
    if (this == &other)
        return *this;

    *d = *other.d_ptr;

    return *this;
}

207
208
209
210
211
212
213
214
215
216
217
void KIppRequestPrivate::addRequest(ipp_tag_t group, ipp_tag_t valueTag, const QString &name, const QVariant &value)
{
    KCupsRawRequest request;
    request.group = group;
    request.valueTag = valueTag;
    request.name = name;
    request.value = value;

    rawRequests << request;
}

218
void KIppRequestPrivate::addRawRequestsToIpp(ipp_t *ipp) const
219
220
{
    // sort the values as CUPS requires it
221
222
223
    std::sort(rawRequests.begin(), rawRequests.end(), [] (const KCupsRawRequest &a, const KCupsRawRequest &b) {
        return a.group < b.group;
    });
224

225
226
    const QList<KCupsRawRequest> &requests = rawRequests;
    for (const KCupsRawRequest &request :requests) {
227
228
229
230
        switch (request.value.type()) {
        case QVariant::Bool:
            ippAddBoolean(ipp,
                          request.group,
231
                          request.name.toUtf8().constData(),
232
233
234
235
236
237
238
                          request.value.toBool());
            break;
        case QVariant::Int:
        case QVariant::UInt:
            ippAddInteger(ipp,
                          request.group,
                          request.valueTag,
239
                          request.name.toUtf8().constData(),
240
241
242
243
244
245
                          request.value.toInt());
            break;
        case QVariant::String:
            ippAddString(ipp,
                         request.group,
                         request.valueTag,
246
                         request.name.toUtf8().constData(),
247
                         "utf-8",
248
                         request.value.toString().toUtf8().constData());
249
250
251
252
            break;
        case QVariant::StringList:
        {
            QStringList list = request.value.toStringList();
253
254
            QList<QByteArray> valuesQByteArrayList;
            const char **values = qStringListToCharPtrPtr(list, valuesQByteArrayList);
255
256
257
258

            ippAddStrings(ipp,
                          request.group,
                          request.valueTag,
259
                          request.name.toUtf8().constData(),
260
261
262
263
264
265
266
267
268
269
                          list.size(),
                          "utf-8",
                          values);

            // ippAddStrings deep copies everything so we can throw away the values.
            // the QBAList and content is auto discarded when going out of scope.
            delete [] values;
            break;
        }
        default:
Jan Grulich's avatar
Jan Grulich committed
270
            qCWarning(LIBKCUPS) << "type NOT recognized! This will be ignored:" << request.name << "values" << request.value;
271
272
273
        }
    }
}