Bug 1656631 - Multistage about:welcome - Make static sites in import screen as defaul...
[gecko.git] / widget / gtk / nsPSPrinters.cpp
blob16e48f10f70f58b29fc658e234abf8914995998e
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
7 #include "nscore.h"
8 #include "nsCUPSShim.h"
9 #include "nsPSPrinters.h"
10 #include "nsReadableUtils.h" // StringBeginsWith()
11 #include "nsCUPSShim.h"
12 #include "mozilla/Preferences.h"
14 #include "prlink.h"
15 #include "prenv.h"
16 #include "plstr.h"
18 using namespace mozilla;
20 #define NS_CUPS_PRINTER "CUPS/"
21 #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
23 /* dummy printer name for the gfx/src/ps driver */
24 #define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
26 nsCUPSShim gCupsShim;
28 nsPSPrinterList::nsPSPrinterList() {
29 // Should we try cups?
30 if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
31 !gCupsShim.IsInitialized()) {
32 gCupsShim.Init();
36 /* Check whether the PostScript module has been disabled at runtime */
37 bool nsPSPrinterList::Enabled() {
38 const char* val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
39 if (val && (val[0] == '0' || !PL_strcasecmp(val, "false"))) return false;
41 // is the PS module enabled?
42 return Preferences::GetBool("print.postscript.enabled", true);
45 /* Fetch a list of printers handled by the PostsScript module */
46 void nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList) {
47 aList.Clear();
49 // Query CUPS for a printer list. The default printer goes to the
50 // head of the output list; others are appended.
51 if (gCupsShim.IsInitialized()) {
52 cups_dest_t* dests;
54 int num_dests = (gCupsShim.mCupsGetDests)(&dests);
55 if (num_dests) {
56 for (int i = 0; i < num_dests; i++) {
57 nsAutoCString fullName(NS_CUPS_PRINTER);
58 fullName.Append(dests[i].name);
59 if (dests[i].instance != nullptr) {
60 fullName.Append('/');
61 fullName.Append(dests[i].instance);
63 if (dests[i].is_default)
64 aList.InsertElementAt(0, fullName);
65 else
66 aList.AppendElement(fullName);
69 (gCupsShim.mCupsFreeDests)(num_dests, dests);
72 // Build the "classic" list of printers -- those accessed by running
73 // an opaque command. This list always contains a printer named "default".
74 // In addition, we look for either an environment variable
75 // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
76 // print.printer_list, which contains a space-separated list of printer
77 // names.
78 aList.AppendElement(nsLiteralCString(NS_POSTSCRIPT_DRIVER_NAME "default"));
80 nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
81 if (list.IsEmpty()) {
82 Preferences::GetCString("print.printer_list", list);
84 if (!list.IsEmpty()) {
85 // For each printer (except "default" which was already added),
86 // construct a string "PostScript/<name>" and append it to the list.
87 char* state;
89 for (char* name = PL_strtok_r(list.BeginWriting(), " ", &state);
90 nullptr != name; name = PL_strtok_r(nullptr, " ", &state)) {
91 if (0 != strcmp(name, "default")) {
92 nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME);
93 fullName.Append(name);
94 aList.AppendElement(fullName);
100 /* Identify the printer type */
101 nsPSPrinterList::PrinterType nsPSPrinterList::GetPrinterType(
102 const nsACString& aName) {
103 if (StringBeginsWith(aName, nsLiteralCString(NS_POSTSCRIPT_DRIVER_NAME))) {
104 return kTypePS;
106 if (StringBeginsWith(aName, nsLiteralCString(NS_CUPS_PRINTER))) {
107 return kTypeCUPS;
109 return kTypeUnknown;