diff options
Diffstat (limited to 'printing')
-rw-r--r-- | printing/print_job_constants.cc | 3 | ||||
-rw-r--r-- | printing/print_job_constants.h | 1 | ||||
-rw-r--r-- | printing/printing_context_mac.h | 2 | ||||
-rw-r--r-- | printing/printing_context_mac.mm | 41 |
4 files changed, 34 insertions, 13 deletions
diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index 82f9adf..d8eb0ce 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -15,6 +15,9 @@ const char kSettingColor[] = "color"; // Number of copies. const char kSettingCopies[] = "copies"; +// Device name: Unique printer identifier. +const char kSettingDeviceName[] = "deviceName"; + // Print job duplex mode. const char kSettingDuplexMode[] = "duplex"; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index 5fe4bc6..0f43b7f 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -10,6 +10,7 @@ namespace printing { extern const char kSettingCollate[]; extern const char kSettingColor[]; extern const char kSettingCopies[]; +extern const char kSettingDeviceName[]; extern const char kSettingDuplexMode[]; extern const char kSettingLandscape[]; extern const char kSettingPageRange[]; diff --git a/printing/printing_context_mac.h b/printing/printing_context_mac.h index ef7e75c..12a999c 100644 --- a/printing/printing_context_mac.h +++ b/printing/printing_context_mac.h @@ -50,7 +50,7 @@ class PrintingContextMac : public PrintingContext { // Updates |print_info_| to use the given printer. // Returns true if the printer was set else returns false. - bool SetPrinter(const std::string& printer_name); + bool SetPrinter(const std::string& device_name); // Sets |copies| in PMPrintSettings. // Returns true if the number of copies is set. diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm index cb60f9f..9333967 100644 --- a/printing/printing_context_mac.mm +++ b/printing/printing_context_mac.mm @@ -92,13 +92,13 @@ PrintingContext::Result PrintingContextMac::UpdatePrintSettings( print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); bool landscape; - std::string printer_name; + std::string device_name; int copies; bool collate; int duplex_mode; bool color; if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || - !job_settings.GetString(kSettingPrinterName, &printer_name) || + !job_settings.GetString(kSettingDeviceName, &device_name) || !job_settings.GetInteger(kSettingCopies, &copies) || !job_settings.GetBoolean(kSettingCollate, &collate) || !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || @@ -106,7 +106,7 @@ PrintingContext::Result PrintingContextMac::UpdatePrintSettings( return OnError(); } - if (!SetPrinter(printer_name)) + if (!SetPrinter(device_name)) return OnError(); if (!SetCopiesInPrintSettings(copies)) @@ -142,19 +142,36 @@ void PrintingContextMac::InitPrintSettingsFromPrintInfo( printer, page_format, ranges, false, &settings_); } -bool PrintingContextMac::SetPrinter(const std::string& printer_name) { - NSString* new_printer_name = base::SysUTF8ToNSString(printer_name); - if (!new_printer_name) +bool PrintingContextMac::SetPrinter(const std::string& device_name) { + DCHECK(print_info_.get()); + PMPrintSession print_session = + static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); + + PMPrinter current_printer; + if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) + return false; + + CFStringRef current_printer_id = PMPrinterGetID(current_printer); + if (!current_printer_id) return false; - if (![[[print_info_.get() printer] name] isEqualToString:new_printer_name]) { - NSPrinter* new_printer = [NSPrinter printerWithName:new_printer_name]; - if (new_printer == nil) - return false; + base::mac::ScopedCFTypeRef<CFStringRef> new_printer_id( + base::SysUTF8ToCFStringRef(device_name)); + if (!new_printer_id.get()) + return false; - [print_info_.get() setPrinter:new_printer]; + if (CFStringCompare(new_printer_id.get(), current_printer_id, 0) == + kCFCompareEqualTo) { + return true; } - return true; + + PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get()); + if (new_printer == NULL) + return false; + + OSStatus status = PMSessionSetCurrentPMPrinter(print_session, new_printer); + PMRelease(new_printer); + return status == noErr; } bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { |