~jonas-drange/ubuntu-settings-components/printers-async

« back to all changes in this revision

Viewing changes to plugins/Ubuntu/Settings/Printers/backend/backend_cups.cpp

  • Committer: Jonas G. Drange
  • Date: 2017-02-07 13:37:33 UTC
  • Revision ID: jonas.drange@canonical.com-20170207133733-h3y9kpl3rh8xslfj
makes tests pass with the more async approach

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include "utils.h"
21
21
 
22
22
#include <QDBusConnection>
 
23
#include <QThread>
23
24
 
24
25
PrinterCupsBackend::PrinterCupsBackend(QObject *parent)
25
26
    : PrinterCupsBackend(new CupsFacade(), QPrinterInfo(),
376
377
    return list;
377
378
}
378
379
 
379
 
QList<Printer*> PrinterCupsBackend::availablePrinters()
 
380
QList<QSharedPointer<Printer>> PrinterCupsBackend::availablePrinters()
380
381
{
381
 
    QList<Printer*> list;
 
382
    QList<QSharedPointer<Printer>> list;
382
383
 
383
384
    // Use availablePrinterNames as this gives us a name for even null printers
384
385
    Q_FOREACH(QString name, QPrinterInfo::availablePrinterNames()) {
385
 
        QPrinterInfo info = QPrinterInfo::printerInfo(name);
386
 
 
387
 
        if (!info.isNull()) {
388
 
            list.append(new Printer(new PrinterCupsBackend(m_cups, info, m_notifier)));
389
 
        } else {
390
 
            qWarning() << "Printer is null so skipping (" << name << ")";
 
386
        auto printer = getPrinter(name);
 
387
        if (printer) {
 
388
            list.append(printer);
391
389
        }
392
390
    }
393
391
 
394
392
    // Cups allows a faux PDF printer.
395
 
    list.append(new Printer(new PrinterPdfBackend(__("Create PDF"))));
 
393
    list.append(QSharedPointer<Printer>(new Printer(new PrinterPdfBackend(__("Create PDF")))));
396
394
 
397
395
    return list;
398
396
}
402
400
    return QPrinterInfo::availablePrinterNames();
403
401
}
404
402
 
405
 
Printer* PrinterCupsBackend::getPrinter(const QString &printerName)
 
403
QSharedPointer<Printer> PrinterCupsBackend::getPrinter(const QString &printerName)
406
404
{
407
 
    // TODO: implement
408
 
    Q_UNUSED(printerName);
409
 
    return Q_NULLPTR;
 
405
    QPrinterInfo info = QPrinterInfo::printerInfo(printerName);
 
406
 
 
407
    if (!info.isNull()) {
 
408
        return QSharedPointer<Printer>(new Printer(new PrinterCupsBackend(m_cups, info, m_notifier)));
 
409
    } else {
 
410
        qWarning() << "Printer is null so skipping (" << printerName << ")";
 
411
    }
 
412
 
 
413
    return QSharedPointer<Printer>(Q_NULLPTR);
410
414
}
411
415
 
412
416
QString PrinterCupsBackend::defaultPrinterName()
414
418
    return QPrinterInfo::defaultPrinterName();
415
419
}
416
420
 
 
421
void PrinterCupsBackend::requestAvailablePrinters()
 
422
{
 
423
    qWarning() << "requestAvailablePrinters";
 
424
    auto thread = new QThread;
 
425
    auto loader = new PrintersLoader(m_cups, m_notifier);
 
426
    loader->moveToThread(thread);
 
427
    connect(thread, SIGNAL(started()), loader, SLOT(load()));
 
428
    connect(loader, SIGNAL(finished()), thread, SLOT(quit()));
 
429
    connect(loader, SIGNAL(finished()), loader, SLOT(deleteLater()));
 
430
    connect(loader, SIGNAL(loaded(QList<QSharedPointer<Printer>>)),
 
431
            this, SIGNAL(availablePrintersLoaded(QList<QSharedPointer<Printer>>)));
 
432
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
 
433
    thread->start();
 
434
}
 
435
 
417
436
void PrinterCupsBackend::requestAvailablePrinterDrivers()
418
437
{
419
438
    return m_cups->requestPrinterDrivers();
443
462
    if (m_cupsSubscriptionId > 0)
444
463
        m_cups->cancelSubscription(m_cupsSubscriptionId);
445
464
}
 
465
 
 
466
PrintersLoader::PrintersLoader(CupsFacade *cups,
 
467
                               OrgCupsCupsdNotifierInterface* notifier,
 
468
                               QObject *parent)
 
469
    : QObject(parent)
 
470
    , m_cups(cups)
 
471
    , m_notifier(notifier)
 
472
{
 
473
}
 
474
 
 
475
PrintersLoader::~PrintersLoader()
 
476
{
 
477
}
 
478
 
 
479
void PrintersLoader::load()
 
480
{
 
481
    QList<QSharedPointer<Printer>> list;
 
482
 
 
483
    // Use availablePrinterNames as this gives us a name for even null printers
 
484
    Q_FOREACH(QString name, QPrinterInfo::availablePrinterNames()) {
 
485
        QPrinterInfo info = QPrinterInfo::printerInfo(name);
 
486
 
 
487
        if (!info.isNull()) {
 
488
            auto p = QSharedPointer<Printer>(new Printer(new PrinterCupsBackend(m_cups, info, m_notifier)));
 
489
            list.append(p);
 
490
        } else {
 
491
            qWarning() << "Printer is null so skipping (" << name << ")";
 
492
        }
 
493
    }
 
494
 
 
495
    // Cups allows a faux PDF printer.
 
496
    auto faux = QSharedPointer<Printer>(new Printer(new PrinterPdfBackend(__("Create PDF"))));
 
497
    list.append(faux);
 
498
 
 
499
    Q_EMIT loaded(list);
 
500
    Q_EMIT finished();
 
501
}
 
502
 
 
503
// Q_SIGNALS:
 
504
//     void finished(QList<QSharedPointer<Printer>> printers);
 
505
//     void error(const QString &errorMessage);
 
506
// };