2
* Copyright (C) 2017 Canonical, Ltd.
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.
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.
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/>.
17
#include "backend/backend_cups.h"
18
#include "printers/printers.h"
19
#include "cupsdnotifier.h" // Note: this file was generated.
21
#include <QDBusConnection>
22
#include <QPrinterInfo>
25
Printers::Printers(QObject *parent)
26
: Printers(new PrinterCupsBackend(new IppClient(), QPrinterInfo(),
27
new OrgCupsCupsdNotifierInterface("", CUPSD_NOTIFIER_DBUS_PATH,
28
QDBusConnection::systemBus())),
33
Printers::Printers(PrinterBackend *backend, QObject *parent)
40
m_allPrinters.setSourceModel(&m_model);
41
m_allPrinters.setSortRole(PrinterModel::Roles::DefaultPrinterRole);
42
m_allPrinters.filterOnPdf(false);
43
m_allPrinters.sort(0, Qt::DescendingOrder);
45
m_allPrintersWithPdf.setSourceModel(&m_model);
46
m_allPrintersWithPdf.setSortRole(PrinterModel::Roles::DefaultPrinterRole);
47
m_allPrintersWithPdf.sort(0, Qt::DescendingOrder);
49
// Let Qt be in charge of RAII.
50
m_backend->setParent(this);
52
connect(&m_drivers, SIGNAL(filterComplete()),
53
this, SIGNAL(driverFilterChanged()));
55
connect(&m_jobs, &QAbstractItemModel::rowsInserted, [this](
56
const QModelIndex &parent, int first, int) {
57
int jobId = m_jobs.data(m_jobs.index(first, 0, parent),
58
JobModel::Roles::IdRole).toInt();
59
jobAdded(m_jobs.getJobById(jobId));
61
connect(&m_model, &QAbstractItemModel::rowsInserted, [this](
62
const QModelIndex &parent, int first, int) {
63
auto printer = m_model.data(
64
m_model.index(first, 0, parent),
65
PrinterModel::Roles::PrinterRole
66
).value<QSharedPointer<Printer>>();
67
printerAdded(printer);
70
// Assign jobmodels to printers right away.
71
for (int i = 0; i < m_model.rowCount(); i++) {
72
printerAdded(m_model.data(
74
PrinterModel::Roles::PrinterRole
75
).value<QSharedPointer<Printer>>()
79
if (m_backend->type() == PrinterEnum::PrinterType::CupsType) {
80
((PrinterCupsBackend*) m_backend)->createSubscription();
83
// Eagerly load the default printer.
84
if (!m_backend->defaultPrinterName().isEmpty()) {}
85
m_backend->requestPrinter(m_backend->defaultPrinterName());
92
QAbstractItemModel* Printers::allPrinters()
94
auto ret = &m_allPrinters;
95
QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
99
QAbstractItemModel* Printers::allPrintersWithPdf()
101
auto ret = &m_allPrintersWithPdf;
102
QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
106
QAbstractItemModel* Printers::recentPrinters()
112
QAbstractItemModel* Printers::printJobs()
115
QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
119
QAbstractItemModel* Printers::drivers()
121
auto ret = &m_drivers;
122
QQmlEngine::setObjectOwnership(ret, QQmlEngine::CppOwnership);
126
QString Printers::driverFilter() const
128
return m_drivers.filter();
131
void Printers::setDriverFilter(const QString &filter)
133
m_drivers.setFilter(filter);
136
QString Printers::defaultPrinterName() const
138
return m_backend->defaultPrinterName();
141
QString Printers::lastMessage() const
143
return m_lastMessage;
146
PrinterJob* Printers::createJob(const QString &printerName)
148
// Note: If called by QML, it gains ownership of this job.
149
return new PrinterJob(printerName, m_backend);
152
void Printers::cancelJob(const QString &printerName, const int jobId)
154
m_backend->cancelJob(printerName, jobId);
157
void Printers::setDefaultPrinterName(const QString &name)
159
QString reply = m_backend->printerSetDefault(name);
161
if (!reply.isEmpty()) {
162
m_lastMessage = reply;
166
QSharedPointer<Printer> Printers::getPrinterByName(const QString &name)
171
return QSharedPointer<Printer>(Q_NULLPTR);
173
QSharedPointer<Printer> Printers::getJobOwner(const int &jobId)
178
return QSharedPointer<Printer>(Q_NULLPTR);
181
void Printers::prepareToAddPrinter()
186
bool Printers::addPrinter(const QString &name, const QString &ppd,
187
const QString &device, const QString &description,
188
const QString &location)
190
QString reply = m_backend->printerAdd(name, device, ppd, description,
192
if (!reply.isEmpty()) {
193
m_lastMessage = reply;
199
bool Printers::addPrinterWithPpdFile(const QString &name,
200
const QString &ppdFileName,
201
const QString &device,
202
const QString &description,
203
const QString &location)
205
QString reply = m_backend->printerAddWithPpd(name, device, ppdFileName,
206
description, location);
207
if (!reply.isEmpty()) {
208
m_lastMessage = reply;
214
bool Printers::removePrinter(const QString &name)
216
QString reply = m_backend->printerDelete(name);
218
if (!reply.isEmpty()) {
219
m_lastMessage = reply;
225
void Printers::jobAdded(QSharedPointer<PrinterJob> job)
227
auto printer = m_model.getPrinterByName(job->printerName());
229
job->setPrinter(printer);
232
void Printers::printerAdded(QSharedPointer<Printer> printer)
234
printer->setJobModel(&m_jobs);
236
// Loop through jobs and associate a printer with it.
237
for (int i = 0; i < m_jobs.rowCount(); i++) {
238
QModelIndex idx = m_jobs.index(i, 0);
240
QString printerName = m_jobs.data(
241
idx, JobModel::Roles::PrinterNameRole
244
int jobId = m_jobs.data(idx, JobModel::Roles::IdRole).toInt();
245
auto job = m_jobs.getJobById(jobId);
246
if (printerName == printer->name() && !job->printer()) {
247
job->setPrinter(printer);