Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nscore.h" |
michael@0 | 8 | #include "nsCUPSShim.h" |
michael@0 | 9 | #include "nsIServiceManager.h" |
michael@0 | 10 | #include "nsPSPrinters.h" |
michael@0 | 11 | #include "nsReadableUtils.h" // StringBeginsWith() |
michael@0 | 12 | #include "nsCUPSShim.h" |
michael@0 | 13 | #include "mozilla/Preferences.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "prlink.h" |
michael@0 | 16 | #include "prenv.h" |
michael@0 | 17 | #include "plstr.h" |
michael@0 | 18 | |
michael@0 | 19 | using namespace mozilla; |
michael@0 | 20 | |
michael@0 | 21 | #define NS_CUPS_PRINTER "CUPS/" |
michael@0 | 22 | #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1) |
michael@0 | 23 | |
michael@0 | 24 | /* dummy printer name for the gfx/src/ps driver */ |
michael@0 | 25 | #define NS_POSTSCRIPT_DRIVER_NAME "PostScript/" |
michael@0 | 26 | |
michael@0 | 27 | nsCUPSShim gCupsShim; |
michael@0 | 28 | |
michael@0 | 29 | nsPSPrinterList::nsPSPrinterList() |
michael@0 | 30 | { |
michael@0 | 31 | // Should we try cups? |
michael@0 | 32 | if (Preferences::GetBool("print.postscript.cups.enabled", true) && |
michael@0 | 33 | !gCupsShim.IsInitialized()) { |
michael@0 | 34 | gCupsShim.Init(); |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | /* Check whether the PostScript module has been disabled at runtime */ |
michael@0 | 40 | bool |
michael@0 | 41 | nsPSPrinterList::Enabled() |
michael@0 | 42 | { |
michael@0 | 43 | const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED"); |
michael@0 | 44 | if (val && (val[0] == '0' || !PL_strcasecmp(val, "false"))) |
michael@0 | 45 | return false; |
michael@0 | 46 | |
michael@0 | 47 | // is the PS module enabled? |
michael@0 | 48 | return Preferences::GetBool("print.postscript.enabled", true); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | |
michael@0 | 52 | /* Fetch a list of printers handled by the PostsScript module */ |
michael@0 | 53 | void |
michael@0 | 54 | nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList) |
michael@0 | 55 | { |
michael@0 | 56 | aList.Clear(); |
michael@0 | 57 | |
michael@0 | 58 | // Query CUPS for a printer list. The default printer goes to the |
michael@0 | 59 | // head of the output list; others are appended. |
michael@0 | 60 | if (gCupsShim.IsInitialized()) { |
michael@0 | 61 | cups_dest_t *dests; |
michael@0 | 62 | |
michael@0 | 63 | int num_dests = (gCupsShim.mCupsGetDests)(&dests); |
michael@0 | 64 | if (num_dests) { |
michael@0 | 65 | for (int i = 0; i < num_dests; i++) { |
michael@0 | 66 | nsAutoCString fullName(NS_CUPS_PRINTER); |
michael@0 | 67 | fullName.Append(dests[i].name); |
michael@0 | 68 | if (dests[i].instance != nullptr) { |
michael@0 | 69 | fullName.Append("/"); |
michael@0 | 70 | fullName.Append(dests[i].instance); |
michael@0 | 71 | } |
michael@0 | 72 | if (dests[i].is_default) |
michael@0 | 73 | aList.InsertElementAt(0, fullName); |
michael@0 | 74 | else |
michael@0 | 75 | aList.AppendElement(fullName); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | (gCupsShim.mCupsFreeDests)(num_dests, dests); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // Build the "classic" list of printers -- those accessed by running |
michael@0 | 82 | // an opaque command. This list always contains a printer named "default". |
michael@0 | 83 | // In addition, we look for either an environment variable |
michael@0 | 84 | // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting |
michael@0 | 85 | // print.printer_list, which contains a space-separated list of printer |
michael@0 | 86 | // names. |
michael@0 | 87 | aList.AppendElement( |
michael@0 | 88 | NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default")); |
michael@0 | 89 | |
michael@0 | 90 | nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST")); |
michael@0 | 91 | if (list.IsEmpty()) { |
michael@0 | 92 | list = Preferences::GetCString("print.printer_list"); |
michael@0 | 93 | } |
michael@0 | 94 | if (!list.IsEmpty()) { |
michael@0 | 95 | // For each printer (except "default" which was already added), |
michael@0 | 96 | // construct a string "PostScript/<name>" and append it to the list. |
michael@0 | 97 | char *state; |
michael@0 | 98 | |
michael@0 | 99 | for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state); |
michael@0 | 100 | nullptr != name; |
michael@0 | 101 | name = PL_strtok_r(nullptr, " ", &state) |
michael@0 | 102 | ) { |
michael@0 | 103 | if (0 != strcmp(name, "default")) { |
michael@0 | 104 | nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME); |
michael@0 | 105 | fullName.Append(name); |
michael@0 | 106 | aList.AppendElement(fullName); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | /* Identify the printer type */ |
michael@0 | 114 | nsPSPrinterList::PrinterType |
michael@0 | 115 | nsPSPrinterList::GetPrinterType(const nsACString& aName) |
michael@0 | 116 | { |
michael@0 | 117 | if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME))) |
michael@0 | 118 | return kTypePS; |
michael@0 | 119 | else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER))) |
michael@0 | 120 | return kTypeCUPS; |
michael@0 | 121 | else |
michael@0 | 122 | return kTypeUnknown; |
michael@0 | 123 | } |