From f3ad57e90ecc33f3d4de7bf8d612abc0ccb2fb6e Mon Sep 17 00:00:00 2001 From: John Layt Date: Mon, 14 May 2012 20:27:21 +0100 Subject: [PATCH] QtPrintSupport - Fix Mac printerName() use Mac in Qt4 and 5 has been using the PMPrinter Name for the QPrinter and QPrinterInfo printerName() value, but this is incorrect. This is in fact the CUPS Description field, is in human readable form and is not guaranteed to be unique. The CUPS Name field is the PMPrinter ID value and should be used as the unique identifier when accessing printers. This has worked up to now due to an undocumented feature in the OSX api that accepted the Name when the ID should be used. Changing all uses of PMPrinterGetName to PMPrinterGetID fixes this and allows the QPrinterInfo test of names to pass without munging the names. Change-Id: I25322aa1a924bed9f67f4ad5e208274c8b700e17 Reviewed-by: Lars Knoll Reviewed-by: Bradley T. Hughes Reviewed-by: John Layt --- .../platforms/cocoa/qcocoaprintersupport.mm | 2 +- src/plugins/platforms/cocoa/qprintengine_mac.mm | 4 ++-- .../kernel/qprinterinfo/tst_qprinterinfo.cpp | 15 --------------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoaprintersupport.mm b/src/plugins/platforms/cocoa/qcocoaprintersupport.mm index 6653c81a4a..a6eb5ddeb8 100644 --- a/src/plugins/platforms/cocoa/qcocoaprintersupport.mm +++ b/src/plugins/platforms/cocoa/qcocoaprintersupport.mm @@ -108,7 +108,7 @@ QList QCocoaPrinterSupport::availablePrinters() CFIndex count = CFArrayGetCount(printerList); for (CFIndex i = 0; i < count; ++i) { PMPrinter printer = static_cast(const_cast(CFArrayGetValueAtIndex(printerList, i))); - QString printerName = QCFString::toQString(PMPrinterGetName(printer)); + QString printerName = QCFString::toQString(PMPrinterGetID(printer)); returnValue += QPlatformPrinterSupport::printerInfo(printerName, PMPrinterIsDefault(printer)); } } diff --git a/src/plugins/platforms/cocoa/qprintengine_mac.mm b/src/plugins/platforms/cocoa/qprintengine_mac.mm index 921dd2f78f..7921832555 100644 --- a/src/plugins/platforms/cocoa/qprintengine_mac.mm +++ b/src/plugins/platforms/cocoa/qprintengine_mac.mm @@ -626,7 +626,7 @@ void QMacPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &va CFIndex count = CFArrayGetCount(printerList); for (CFIndex i=0; i(const_cast(CFArrayGetValueAtIndex(printerList, i))); - QString name = QCFString::toQString(PMPrinterGetName(printer)); + QString name = QCFString::toQString(PMPrinterGetID(printer)); if (name == value.toString()) { status = PMSessionSetCurrentPMPrinter(d->session(), printer); printerNameSet = true; @@ -777,7 +777,7 @@ QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const if (status != noErr) qWarning("QMacPrintEngine::printerName: Failed getting current PMPrinter: %ld", long(status)); if (printer) - ret = QCFString::toQString(PMPrinterGetName(printer)); + ret = QCFString::toQString(PMPrinterGetID(printer)); break; } case PPK_Resolution: { ret = d->resolution.hRes; diff --git a/tests/auto/printsupport/kernel/qprinterinfo/tst_qprinterinfo.cpp b/tests/auto/printsupport/kernel/qprinterinfo/tst_qprinterinfo.cpp index 404aa06399..70a00edb10 100644 --- a/tests/auto/printsupport/kernel/qprinterinfo/tst_qprinterinfo.cpp +++ b/tests/auto/printsupport/kernel/qprinterinfo/tst_qprinterinfo.cpp @@ -70,7 +70,6 @@ private slots: void testAssignment(); private: - void macFixNameFormat(QString *printerName); QString getDefaultPrinterFromSystem(); QStringList getPrintersFromSystem(); @@ -86,18 +85,6 @@ void tst_QPrinterInfo::initTestCase() #else -void tst_QPrinterInfo::macFixNameFormat(QString *printerName) -{ -// Modify the format of the printer name to match Qt, lpstat returns -// foo___domain_no, Qt returns foo @ domain.no -#ifdef Q_OS_MAC - printerName->replace(QLatin1String("___"), QLatin1String(" @ ")); - printerName->replace(QLatin1String("_"), QLatin1String(".")); -#else - Q_UNUSED(printerName); -#endif -} - QString tst_QPrinterInfo::getDefaultPrinterFromSystem() { QStringList command; @@ -113,7 +100,6 @@ QString tst_QPrinterInfo::getDefaultPrinterFromSystem() QRegExp defaultReg("default.*: *([a-zA-Z0-9_-]+)"); defaultReg.indexIn(output); QString printer = defaultReg.cap(1); - macFixNameFormat(&printer); return printer; } @@ -130,7 +116,6 @@ QStringList tst_QPrinterInfo::getPrintersFromSystem() for (int c = 0; c < list.size(); ++c) { if (reg.indexIn(list[c]) >= 0) { QString printer = reg.cap(1); - macFixNameFormat(&printer); ans << printer; } }