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 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla PostScript driver printer list component.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Kenneth Herron <kherron+mozilla@fmailbox.com>
20 : * Portions created by the Initial Developer are Copyright (C) 2004
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nscore.h"
40 : #include "nsCUPSShim.h"
41 : #include "nsIServiceManager.h"
42 : #include "nsPSPrinters.h"
43 : #include "nsReadableUtils.h" // StringBeginsWith()
44 : #include "nsCUPSShim.h"
45 : #include "mozilla/Preferences.h"
46 :
47 : #include "prlink.h"
48 : #include "prenv.h"
49 : #include "plstr.h"
50 :
51 : using namespace mozilla;
52 :
53 : #define NS_CUPS_PRINTER "CUPS/"
54 : #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
55 :
56 : /* dummy printer name for the gfx/src/ps driver */
57 : #define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
58 :
59 : nsCUPSShim gCupsShim;
60 :
61 0 : nsPSPrinterList::nsPSPrinterList()
62 : {
63 : // Should we try cups?
64 0 : if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
65 0 : !gCupsShim.IsInitialized()) {
66 0 : gCupsShim.Init();
67 : }
68 0 : }
69 :
70 :
71 : /* Check whether the PostScript module has been disabled at runtime */
72 : bool
73 0 : nsPSPrinterList::Enabled()
74 : {
75 0 : const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
76 0 : if (val && (val[0] == '0' || !PL_strcasecmp(val, "false")))
77 0 : return false;
78 :
79 : // is the PS module enabled?
80 0 : return Preferences::GetBool("print.postscript.enabled", true);
81 : }
82 :
83 :
84 : /* Fetch a list of printers handled by the PostsScript module */
85 : void
86 0 : nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
87 : {
88 0 : aList.Clear();
89 :
90 : // Query CUPS for a printer list. The default printer goes to the
91 : // head of the output list; others are appended.
92 0 : if (gCupsShim.IsInitialized()) {
93 : cups_dest_t *dests;
94 :
95 0 : int num_dests = (gCupsShim.mCupsGetDests)(&dests);
96 0 : if (num_dests) {
97 0 : for (int i = 0; i < num_dests; i++) {
98 0 : nsCAutoString fullName(NS_CUPS_PRINTER);
99 0 : fullName.Append(dests[i].name);
100 0 : if (dests[i].instance != NULL) {
101 0 : fullName.Append("/");
102 0 : fullName.Append(dests[i].instance);
103 : }
104 0 : if (dests[i].is_default)
105 0 : aList.InsertElementAt(0, fullName);
106 : else
107 0 : aList.AppendElement(fullName);
108 : }
109 : }
110 0 : (gCupsShim.mCupsFreeDests)(num_dests, dests);
111 : }
112 :
113 : // Build the "classic" list of printers -- those accessed by running
114 : // an opaque command. This list always contains a printer named "default".
115 : // In addition, we look for either an environment variable
116 : // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
117 : // print.printer_list, which contains a space-separated list of printer
118 : // names.
119 : aList.AppendElement(
120 0 : NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
121 :
122 0 : nsCAutoString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
123 0 : if (list.IsEmpty()) {
124 0 : list = Preferences::GetCString("print.printer_list");
125 : }
126 0 : if (!list.IsEmpty()) {
127 : // For each printer (except "default" which was already added),
128 : // construct a string "PostScript/<name>" and append it to the list.
129 : char *state;
130 :
131 0 : for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state);
132 : nsnull != name;
133 : name = PL_strtok_r(nsnull, " ", &state)
134 : ) {
135 0 : if (0 != strcmp(name, "default")) {
136 0 : nsCAutoString fullName(NS_POSTSCRIPT_DRIVER_NAME);
137 0 : fullName.Append(name);
138 0 : aList.AppendElement(fullName);
139 : }
140 : }
141 : }
142 0 : }
143 :
144 :
145 : /* Identify the printer type */
146 : nsPSPrinterList::PrinterType
147 0 : nsPSPrinterList::GetPrinterType(const nsACString& aName)
148 : {
149 0 : if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME)))
150 0 : return kTypePS;
151 0 : else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER)))
152 0 : return kTypeCUPS;
153 : else
154 0 : return kTypeUnknown;
155 : }
|