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