Commit 08a74431 authored by Daniel Nicoletti's avatar Daniel Nicoletti
Browse files

Make sure we don't crash if the application is closed before we process a CUPS request

BUG: 314428
parent af3fd585
......@@ -74,27 +74,28 @@ void AddPrinterInterface::AddClass(qulonglong wid)
void AddPrinterInterface::ChangePPD(qulonglong wid, const QString &name)
{
// Fist we need to get the printer attributes
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
QStringList attr;
attr << KCUPS_PRINTER_TYPE; // needed to know if it's a remote printer
attr << KCUPS_PRINTER_MAKE_AND_MODEL;
attr << KCUPS_DEVICE_URI;
request->getPrinterAttributes(name, false, attr);
request->waitTillFinished();
if (!request->hasError() && request->printers().size() == 1) {
KCupsPrinter printer = request->printers().first();
if (printer.type() & CUPS_PRINTER_REMOTE) {
kWarning() << "Ignoring request, can not change PPD of remote printer" << name;
if (request) {
if (!request->hasError() && request->printers().size() == 1) {
KCupsPrinter printer = request->printers().first();
if (printer.type() & CUPS_PRINTER_REMOTE) {
kWarning() << "Ignoring request, can not change PPD of remote printer" << name;
} else {
AddPrinterAssistant *wizard = new AddPrinterAssistant();
wizard->initChangePPD(name, printer.deviceUri(), printer.makeAndModel());
show(wizard, wid);
}
} else {
AddPrinterAssistant *wizard = new AddPrinterAssistant();
wizard->initChangePPD(name, printer.deviceUri(), printer.makeAndModel());
show(wizard, wid);
kWarning() << "Ignoring request, printer not found" << name << request->errorMsg();
}
} else {
kWarning() << "Ignoring request, printer not found" << name << request->errorMsg();
request->deleteLater();
}
request->deleteLater();
}
void AddPrinterInterface::NewPrinterFromDevice(qulonglong wid, const QString &name, const QString &device_id)
......
......@@ -124,7 +124,7 @@ bool PageAddPrinter::finishClicked()
QString destName = args[KCUPS_PRINTER_NAME].toString();
QString filename = args.take(FILENAME).toString();
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (isClass) {
args[KCUPS_PRINTER_IS_ACCEPTING_JOBS] = true;
args[KCUPS_PRINTER_STATE] = IPP_PRINTER_IDLE;
......@@ -132,22 +132,23 @@ bool PageAddPrinter::finishClicked()
} else {
request->addOrModifyPrinter(destName, args, filename);
}
request->waitTillFinished();
if (request->hasError()) {
kDebug() << request->error() << request->errorMsg();
QString message;
if (isClass) {
message = i18nc("@info", "Failed to add class: '%1'", request->errorMsg());
if (request) {
if (request->hasError()) {
kDebug() << request->error() << request->errorMsg();
QString message;
if (isClass) {
message = i18nc("@info", "Failed to add class: '%1'", request->errorMsg());
} else {
message = i18nc("@info", "Failed to configure printer: '%1'", request->errorMsg());
}
ui->messageWidget->setText(message);
ui->messageWidget->animatedShow();
} else {
message = i18nc("@info", "Failed to configure printer: '%1'", request->errorMsg());
ret = true;
}
ui->messageWidget->setText(message);
ui->messageWidget->animatedShow();
} else {
ret = true;
request->deleteLater();
}
request->deleteLater();
return ret;
}
......
......@@ -33,6 +33,8 @@
#include <KDebug>
#include <QList>
#include <QPointer>
Q_DECLARE_METATYPE(QList<int>)
ConfigureDialog::ConfigureDialog(const QString &destName, bool isClass, QWidget *parent) :
......@@ -58,9 +60,12 @@ ConfigureDialog::ConfigureDialog(const QString &destName, bool isClass, QWidget
attr << KCUPS_PRINTER_MAKE_AND_MODEL;
KCupsPrinter printer;
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->getPrinterAttributes(destName, isClass, attr);
request->waitTillFinished();
if (!request) {
return;
}
if (!request->hasError() && !request->printers().isEmpty()){
printer = request->printers().first();
}
......
......@@ -67,9 +67,12 @@ void ConfigurePrinterInterface::ConfigurePrinter(const QString &destName)
att << KCUPS_PRINTER_NAME;
att << KCUPS_PRINTER_TYPE;
// Get destinations with these attributes
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->getPrinters(att);
request->waitTillFinished();
if (!request) {
return;
}
bool found = false;
KCupsPrinter printer;
......
......@@ -260,33 +260,34 @@ void ModifyPrinter::save()
}
kDebug() << fileName;
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (m_isClass) {
request->addOrModifyClass(m_destName, args);
} else {
request->addOrModifyPrinter(m_destName, args, fileName);
}
request->waitTillFinished();
if (!request->hasError()) {
if (m_changedValues.contains("ppd-name")) {
emit ppdChanged();
}
request->getPrinterAttributes(m_destName, m_isClass, neededValues());
request->waitTillFinished();
if (!request->hasError() && !request->printers().isEmpty()) {
KCupsPrinter printer = request->printers().first();
setValues(printer);
if (request) {
if (!request->hasError()) {
if (m_changedValues.contains("ppd-name")) {
emit ppdChanged();
}
request->getPrinterAttributes(m_destName, m_isClass, neededValues());
request->waitTillFinished();
if (!request->hasError() && !request->printers().isEmpty()) {
KCupsPrinter printer = request->printers().first();
setValues(printer);
}
} else {
KMessageBox::detailedSorry(this,
m_isClass ? i18nc("@info", "Failed to configure class") :
i18nc("@info", "Failed to configure printer"),
request->errorMsg(),
i18nc("@title:window", "Failed"));
}
} else {
KMessageBox::detailedSorry(this,
m_isClass ? i18nc("@info", "Failed to configure class") :
i18nc("@info", "Failed to configure printer"),
request->errorMsg(),
i18nc("@title:window", "Failed"));
request->deleteLater();
}
request->deleteLater();
}
}
......
......@@ -269,22 +269,24 @@ void PrinterBehavior::save()
}
}
}
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (m_isClass) {
request->addOrModifyClass(m_destName, changedValues);
} else {
request->addOrModifyPrinter(m_destName, changedValues);
}
request->waitTillFinished();
if (!request->hasError()) {
request->getPrinterAttributes(m_destName, m_isClass, neededValues());
request->waitTillFinished();
if (!request->hasError() && !request->printers().isEmpty()){
KCupsPrinter printer = request->printers().first();
setValues(printer);
if (request) {
if (!request->hasError()) {
request->getPrinterAttributes(m_destName, m_isClass, neededValues());
request->waitTillFinished();
if (request && !request->hasError() && !request->printers().isEmpty()){
KCupsPrinter printer = request->printers().first();
setValues(printer);
}
}
request->deleteLater();
}
request->deleteLater();
}
}
......
......@@ -64,10 +64,12 @@ PrinterOptions::PrinterOptions(const QString &destName, bool isClass, bool isRem
void PrinterOptions::on_autoConfigurePB_clicked()
{
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->printCommand(m_destName, "AutoConfigure", i18n("Set Default Options"));
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
void PrinterOptions::reloadPPD()
......@@ -97,9 +99,12 @@ void PrinterOptions::reloadPPD()
m_customValues.clear();
emit changed(false);
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->getPrinterPPD(m_destName);
request->waitTillFinished();
if (!request) {
return;
}
m_filename = request->printerPPD();
m_ppd = ppdOpenFile(m_filename.toUtf8());
request->deleteLater();
......@@ -758,7 +763,7 @@ void PrinterOptions::save()
}
QVariantHash values; // we need null values
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (m_isClass) {
request->addOrModifyClass(m_destName, values);
} else {
......@@ -771,28 +776,31 @@ void PrinterOptions::save()
// finishes we will set the current options as default
setEnabled(false);
request->waitTillFinished();
setEnabled(true);
if (!request->hasError()) {
// if we succefully save the new ppd we need now to
// clear our changes
QHash<QString, QObject*>::const_iterator i = m_customValues.constBegin();
while (i != m_customValues.constEnd()) {
QString currentChoice;
currentChoice = i.value()->property("currentChoice").toString();
// Store the current choice as the default one
i.value()->setProperty(DEFAULT_CHOICE, currentChoice);
i.value()->setProperty("currentChoice", QVariant());
i.value()->setProperty("different", false);
++i;
}
m_changes = 0;
m_customValues.clear();
emit changed(false);
}
request->deleteLater();
// unlink the file
unlink(tempfile);
if (request) {
setEnabled(true);
if (!request->hasError()) {
// if we succefully save the new ppd we need now to
// clear our changes
QHash<QString, QObject*>::const_iterator i = m_customValues.constBegin();
while (i != m_customValues.constEnd()) {
QString currentChoice;
currentChoice = i.value()->property("currentChoice").toString();
// Store the current choice as the default one
i.value()->setProperty(DEFAULT_CHOICE, currentChoice);
i.value()->setProperty("currentChoice", QVariant());
i.value()->setProperty("different", false);
++i;
}
m_changes = 0;
m_customValues.clear();
emit changed(false);
}
request->deleteLater();
}
}
bool PrinterOptions::hasChanges()
......
......@@ -130,9 +130,12 @@ void NewPrinterNotification::NewPrinter(int status,
attr << KCUPS_PRINTER_MAKE_AND_MODEL;
// Get the new printer attributes
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->getPrinterAttributes(name, false, attr);
request->waitTillFinished();
if (!request) {
return;
}
QString driver;
// Get the new printer driver
......@@ -146,6 +149,9 @@ void NewPrinterNotification::NewPrinter(int status,
request = new KCupsRequest;
request->getPrinterPPD(name);
request->waitTillFinished();
if (!request) {
return;
}
ppdFileName = request->printerPPD();
request->deleteLater();
......@@ -238,10 +244,12 @@ void NewPrinterNotification::searchDrivers()
void NewPrinterNotification::printTestPage()
{
kDebug();
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->printTestPage(sender()->property(PRINTER_NAME).toString(), false);
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
void NewPrinterNotification::findDriver()
......
......@@ -298,10 +298,12 @@ void PrintKCM::on_removeTB_clicked()
}
resp = KMessageBox::warningYesNo(this, msg, title);
if (resp == KMessageBox::Yes) {
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->deletePrinter(index.data(PrinterModel::DestName).toString());
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
}
}
......
......@@ -91,28 +91,32 @@ void PrinterDescription::on_openQueuePB_clicked()
void PrinterDescription::on_defaultCB_clicked()
{
bool isDefault = ui->defaultCB->isChecked();
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->setDefaultPrinter(m_destName);
request->waitTillFinished();
setIsDefault(request->hasError() ? !isDefault : isDefault);
request->deleteLater();
if (request) {
setIsDefault(request->hasError() ? !isDefault : isDefault);
request->deleteLater();
}
}
void PrinterDescription::on_sharedCB_clicked()
{
bool shared = ui->sharedCB->isChecked();
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->setShared(m_destName, m_isClass, shared);
request->waitTillFinished();
setIsShared(request->hasError() ? !shared : shared);
request->deleteLater();
if (request) {
setIsShared(request->hasError() ? !shared : shared);
request->deleteLater();
}
}
void PrinterDescription::on_rejectPrintJobsCB_clicked()
{
bool accepting = !ui->rejectPrintJobsCB->isChecked();
kDebug() << accepting;
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (accepting) {
request->acceptJobs(m_destName);
} else {
......@@ -120,8 +124,10 @@ void PrinterDescription::on_rejectPrintJobsCB_clicked()
}
request->waitTillFinished();
setAcceptingJobs(request->hasError() ? !accepting : accepting);
request->deleteLater();
if (request) {
setAcceptingJobs(request->hasError() ? !accepting : accepting);
request->deleteLater();
}
}
void PrinterDescription::setPrinterIcon(const QIcon &icon)
......@@ -239,28 +245,34 @@ void PrinterDescription::on_actionPrintTestPage_triggered(bool checked)
Q_UNUSED(checked)
// TODO Show a msg box if failed
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->printTestPage(m_destName, m_isClass);
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
void PrinterDescription::on_actionCleanPrintHeads_triggered(bool checked)
{
Q_UNUSED(checked)
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->printCommand(m_destName, "Clean all", i18n("Clean Print Heads"));
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
void PrinterDescription::on_actionPrintSelfTestPage_triggered(bool checked)
{
Q_UNUSED(checked)
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->printCommand(m_destName, "PrintSelfTestPage", i18n("Print Self-Test Page"));
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
QString PrinterDescription::destName() const
......
......@@ -62,9 +62,12 @@ void PrintQueueInterface::ShowQueue(const QString &destName)
attr << KCUPS_PRINTER_NAME;
attr << KCUPS_PRINTER_TYPE;
// Get destinations with these attributes
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->getPrinters(attr);
request->waitTillFinished();
if (!request) {
return;
}
bool found = false;
KCupsPrinter printer;
......
......@@ -26,6 +26,8 @@
#include <QDateTime>
#include <QMimeData>
#include <QPointer>
#include <KUser>
#include <KDebug>
#include <KLocale>
......@@ -394,21 +396,22 @@ bool PrintQueueModel::dropMimeData(const QMimeData *data,
continue;
}
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
request->moveJob(fromDestName, jobId, m_destName);
request->waitTillFinished();
request->deleteLater(); // TODO can it be deleted here?
if (request->hasError()) {
// failed to move one job
// we return here to avoid more password tries
KMessageBox::detailedSorryWId(m_parentId,
i18n("Failed to move '%1' to '%2'",
displayName, m_destName),
request->errorMsg(),
i18n("Failed"));
return false;
if (request) {
if (request->hasError()) {
// failed to move one job
// we return here to avoid more password tries
KMessageBox::detailedSorryWId(m_parentId,
i18n("Failed to move '%1' to '%2'",
displayName, m_destName),
request->errorMsg(),
i18n("Failed"));
}
request->deleteLater();
ret = !request->hasError();
}
ret = true;
}
return ret;
}
......
......@@ -301,12 +301,15 @@ void PrintQueueUi::showContextMenu(const QPoint &point)
QMenu *moveToMenu = new QMenu(i18n("Move to"), this);
// get printers we can move to
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
QStringList attr;
attr << KCUPS_PRINTER_NAME;
attr << KCUPS_PRINTER_INFO;
request->getPrinters(attr);
request->waitTillFinished();
if (!request) {
return;
}
KCupsPrinters printers = request->printers();
request->deleteLater();
......@@ -519,7 +522,7 @@ void PrintQueueUi::modifyJob(int action, const QString &destName)
void PrintQueueUi::pausePrinter()
{
// STOP and RESUME printer
KCupsRequest *request = new KCupsRequest;
QPointer<KCupsRequest> request = new KCupsRequest;
if (m_printerPaused) {
kDebug() << m_destName << "m_printerPaused";
request->resumePrinter(m_destName);
......@@ -528,7 +531,9 @@ void PrintQueueUi::pausePrinter()
request->pausePrinter(m_destName);
}
request->waitTillFinished();
request->deleteLater();
if (request) {
request->deleteLater();
}
}
void PrintQueueUi::configurePrinter()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment