michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsCUPSShim.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsPSPrinters.h" michael@0: #include "nsReadableUtils.h" // StringBeginsWith() michael@0: #include "nsCUPSShim.h" michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: #include "prlink.h" michael@0: #include "prenv.h" michael@0: #include "plstr.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: #define NS_CUPS_PRINTER "CUPS/" michael@0: #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1) michael@0: michael@0: /* dummy printer name for the gfx/src/ps driver */ michael@0: #define NS_POSTSCRIPT_DRIVER_NAME "PostScript/" michael@0: michael@0: nsCUPSShim gCupsShim; michael@0: michael@0: nsPSPrinterList::nsPSPrinterList() michael@0: { michael@0: // Should we try cups? michael@0: if (Preferences::GetBool("print.postscript.cups.enabled", true) && michael@0: !gCupsShim.IsInitialized()) { michael@0: gCupsShim.Init(); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Check whether the PostScript module has been disabled at runtime */ michael@0: bool michael@0: nsPSPrinterList::Enabled() michael@0: { michael@0: const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED"); michael@0: if (val && (val[0] == '0' || !PL_strcasecmp(val, "false"))) michael@0: return false; michael@0: michael@0: // is the PS module enabled? michael@0: return Preferences::GetBool("print.postscript.enabled", true); michael@0: } michael@0: michael@0: michael@0: /* Fetch a list of printers handled by the PostsScript module */ michael@0: void michael@0: nsPSPrinterList::GetPrinterList(nsTArray& aList) michael@0: { michael@0: aList.Clear(); michael@0: michael@0: // Query CUPS for a printer list. The default printer goes to the michael@0: // head of the output list; others are appended. michael@0: if (gCupsShim.IsInitialized()) { michael@0: cups_dest_t *dests; michael@0: michael@0: int num_dests = (gCupsShim.mCupsGetDests)(&dests); michael@0: if (num_dests) { michael@0: for (int i = 0; i < num_dests; i++) { michael@0: nsAutoCString fullName(NS_CUPS_PRINTER); michael@0: fullName.Append(dests[i].name); michael@0: if (dests[i].instance != nullptr) { michael@0: fullName.Append("/"); michael@0: fullName.Append(dests[i].instance); michael@0: } michael@0: if (dests[i].is_default) michael@0: aList.InsertElementAt(0, fullName); michael@0: else michael@0: aList.AppendElement(fullName); michael@0: } michael@0: } michael@0: (gCupsShim.mCupsFreeDests)(num_dests, dests); michael@0: } michael@0: michael@0: // Build the "classic" list of printers -- those accessed by running michael@0: // an opaque command. This list always contains a printer named "default". michael@0: // In addition, we look for either an environment variable michael@0: // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting michael@0: // print.printer_list, which contains a space-separated list of printer michael@0: // names. michael@0: aList.AppendElement( michael@0: NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default")); michael@0: michael@0: nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST")); michael@0: if (list.IsEmpty()) { michael@0: list = Preferences::GetCString("print.printer_list"); michael@0: } michael@0: if (!list.IsEmpty()) { michael@0: // For each printer (except "default" which was already added), michael@0: // construct a string "PostScript/" and append it to the list. michael@0: char *state; michael@0: michael@0: for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state); michael@0: nullptr != name; michael@0: name = PL_strtok_r(nullptr, " ", &state) michael@0: ) { michael@0: if (0 != strcmp(name, "default")) { michael@0: nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME); michael@0: fullName.Append(name); michael@0: aList.AppendElement(fullName); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* Identify the printer type */ michael@0: nsPSPrinterList::PrinterType michael@0: nsPSPrinterList::GetPrinterType(const nsACString& aName) michael@0: { michael@0: if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME))) michael@0: return kTypePS; michael@0: else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER))) michael@0: return kTypeCUPS; michael@0: else michael@0: return kTypeUnknown; michael@0: }