Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcupsprintersupport.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2014 John Layt <jlayt@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
8#include "qppdprintdevice.h"
9#include <private/qprinterinfo_p.h>
10#include <private/qprintdevice_p.h>
11
12#include <QtPrintSupport/QPrinterInfo>
13
14#if QT_CONFIG(cupspassworddialog)
15#include <QGuiApplication>
16#include <QDialog>
17#include <QDialogButtonBox>
18#include <QFormLayout>
19#include <QLabel>
20#include <QLineEdit>
21#endif // QT_CONFIG(cupspassworddialog)
22
23#include <cups/ppd.h>
24#ifndef QT_LINUXBASE // LSB merges everything into cups.h
25# include <cups/language.h>
26#endif
27
29
30#if QT_CONFIG(cupspassworddialog)
31static const char *getPasswordCB(const char */*prompt*/, http_t *http, const char */*method*/, const char *resource, void */*user_data*/)
32{
33 // cups doesn't free the const char * we return so keep around
34 // the last password so we don't leak memory if called multiple times.
35 static QByteArray password;
36
37 // prompt is always "Password for %s on %s? " but we can't use it since we allow the user to change the user.
38 // That is fine because cups always calls cupsUser after calling this callback.
39 // We build our own prompt with the hostname (if not localhost) and the resource that is being used
40
41 char hostname[HTTP_MAX_HOST];
42 httpGetHostname(http, hostname, HTTP_MAX_HOST);
43
44 const QString username = QString::fromLocal8Bit(cupsUser());
45
47 dialog.setWindowTitle(QCoreApplication::translate("QCupsPrinterSupport", "Authentication Needed"));
48
51
52 QLineEdit *usernameLE = new QLineEdit();
53 usernameLE->setText(username);
54
55 QLineEdit *passwordLE = new QLineEdit();
56 passwordLE->setEchoMode(QLineEdit::Password);
57
58 QString resourceString = QString::fromLocal8Bit(resource);
59 if (resourceString.startsWith(QStringLiteral("/printers/")))
60 resourceString = resourceString.mid(QStringLiteral("/printers/").size());
61
62 QLabel *label = new QLabel();
63 if (hostname == QStringLiteral("localhost")) {
64 label->setText(QCoreApplication::translate("QCupsPrinterSupport", "Authentication needed to use %1.").arg(resourceString));
65 } else {
66 label->setText(QCoreApplication::translate("QCupsPrinterSupport", "Authentication needed to use %1 on %2.").arg(resourceString).arg(hostname));
67 label->setWordWrap(true);
68 }
69
70 layout->addRow(label);
71 layout->addRow(new QLabel(QCoreApplication::translate("QCupsPrinterSupport", "Username:")), usernameLE);
72 layout->addRow(new QLabel(QCoreApplication::translate("QCupsPrinterSupport", "Password:")), passwordLE);
73
75 layout->addRow(buttonBox);
76
79
80 passwordLE->setFocus();
81
83 return nullptr;
84
85 if (usernameLE->text() != username)
86 cupsSetUser(usernameLE->text().toLocal8Bit().constData());
87
88 password = passwordLE->text().toLocal8Bit();
89
90 return password.constData();
91}
92#endif // QT_CONFIG(cupspassworddialog)
93
96{
97#if QT_CONFIG(cupspassworddialog)
98 // Only show password dialog if GUI application
99 if (qobject_cast<QGuiApplication*>(QCoreApplication::instance()))
100 cupsSetPasswordCB2(getPasswordCB, nullptr /* user_data */ );
101#endif // QT_CONFIG(cupspassworddialog)
102}
103
107
109{
110 return new QCupsPrintEngine(printerMode, (deviceId.isEmpty() ? defaultPrintDeviceId() : deviceId));
111}
112
118
123
125{
127 cups_dest_t *dests;
128 int count = cupsGetDests(&dests);
130 for (int i = 0; i < count; ++i) {
131 QString printerId = QString::fromLocal8Bit(dests[i].name);
132 if (dests[i].instance)
133 printerId += u'/' + QString::fromLocal8Bit(dests[i].instance);
134 list.append(printerId);
135 }
136 cupsFreeDests(count, dests);
137 return list;
138}
139
144
146{
147 QString printerId;
148 cups_dest_t *dests;
149 int count = cupsGetDests(&dests);
150 for (int i = 0; i < count; ++i) {
151 if (dests[i].is_default) {
152 printerId = QString::fromLocal8Bit(dests[i].name);
153 if (dests[i].instance) {
154 printerId += u'/' + QString::fromLocal8Bit(dests[i].instance);
155 break;
156 }
157 }
158 }
159 cupsFreeDests(count, dests);
160 return printerId;
161}
162
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
static QString staticDefaultPrintDeviceId()
QPrintEngine * createNativePrintEngine(QPrinter::PrinterMode printerMode, const QString &deviceId=QString()) override
QPrintDevice createPrintDevice(const QString &id) override
QStringList availablePrintDeviceIds() const override
QString defaultPrintDeviceId() const override
QPaintEngine * createPaintEngine(QPrintEngine *printEngine, QPrinter::PrinterMode) override
The QDialogButtonBox class is a widget that presents buttons in a layout that is appropriate to the c...
void accepted()
This signal is emitted when a button inside the button box is clicked, as long as it was defined with...
void rejected()
This signal is emitted when a button inside the button box is clicked, as long as it was defined with...
The QDialog class is the base class of dialog windows.
Definition qdialog.h:19
virtual void reject()
Hides the modal dialog and sets the result code to Rejected.
Definition qdialog.cpp:639
virtual int exec()
Shows the dialog as a \l{QDialog::Modal Dialogs}{modal dialog}, blocking until the user closes it.
Definition qdialog.cpp:543
@ Accepted
Definition qdialog.h:30
virtual void accept()
Hides the modal dialog and sets the result code to Accepted.
Definition qdialog.cpp:628
The QFormLayout class manages forms of input widgets and their associated labels.
Definition qformlayout.h:18
The QLabel widget provides a text or image display.
Definition qlabel.h:20
void setSizeConstraint(SizeConstraint)
Definition qlayout.cpp:1241
@ SetFixedSize
Definition qlayout.h:39
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
void reserve(qsizetype size)
Definition qlist.h:753
void append(parameter_type t)
Definition qlist.h:458
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtGui
The QPlatformPrinterSupport class provides an abstraction for print support.
virtual QPrintDevice createPrintDevice(const QString &id)
\reentrant
PrinterMode
This enum describes the mode the printer should work in.
Definition qprinter.h:31
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void setWindowTitle(const QString &)
Definition qwidget.cpp:6105
Combined button and popup list for selecting options.
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLuint GLsizei const GLchar * label
[43]
GLuint name
SSL_CTX int void * arg
#define QStringLiteral(str)
#define Q_UNUSED(x)
QList< int > list
[14]
QVBoxLayout * layout
QFileDialog dialog(this)
[1]
QJSEngine engine
[0]