widget/gtk/nsPSPrinters.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/gtk/nsPSPrinters.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nscore.h"
    1.11 +#include "nsCUPSShim.h"
    1.12 +#include "nsIServiceManager.h"
    1.13 +#include "nsPSPrinters.h"
    1.14 +#include "nsReadableUtils.h"        // StringBeginsWith()
    1.15 +#include "nsCUPSShim.h"
    1.16 +#include "mozilla/Preferences.h"
    1.17 +
    1.18 +#include "prlink.h"
    1.19 +#include "prenv.h"
    1.20 +#include "plstr.h"
    1.21 +
    1.22 +using namespace mozilla;
    1.23 +
    1.24 +#define NS_CUPS_PRINTER "CUPS/"
    1.25 +#define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
    1.26 +
    1.27 +/* dummy printer name for the gfx/src/ps driver */
    1.28 +#define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
    1.29 +
    1.30 +nsCUPSShim gCupsShim;
    1.31 +
    1.32 +nsPSPrinterList::nsPSPrinterList()
    1.33 +{
    1.34 +    // Should we try cups?
    1.35 +    if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
    1.36 +        !gCupsShim.IsInitialized()) {
    1.37 +        gCupsShim.Init();
    1.38 +    }
    1.39 +}
    1.40 +
    1.41 +
    1.42 +/* Check whether the PostScript module has been disabled at runtime */
    1.43 +bool
    1.44 +nsPSPrinterList::Enabled()
    1.45 +{
    1.46 +    const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
    1.47 +    if (val && (val[0] == '0' || !PL_strcasecmp(val, "false")))
    1.48 +        return false;
    1.49 +
    1.50 +    // is the PS module enabled?
    1.51 +    return Preferences::GetBool("print.postscript.enabled", true);
    1.52 +}
    1.53 +
    1.54 +
    1.55 +/* Fetch a list of printers handled by the PostsScript module */
    1.56 +void
    1.57 +nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
    1.58 +{
    1.59 +    aList.Clear();
    1.60 +
    1.61 +    // Query CUPS for a printer list. The default printer goes to the
    1.62 +    // head of the output list; others are appended.
    1.63 +    if (gCupsShim.IsInitialized()) {
    1.64 +        cups_dest_t *dests;
    1.65 +
    1.66 +        int num_dests = (gCupsShim.mCupsGetDests)(&dests);
    1.67 +        if (num_dests) {
    1.68 +            for (int i = 0; i < num_dests; i++) {
    1.69 +                nsAutoCString fullName(NS_CUPS_PRINTER);
    1.70 +                fullName.Append(dests[i].name);
    1.71 +                if (dests[i].instance != nullptr) {
    1.72 +                    fullName.Append("/");
    1.73 +                    fullName.Append(dests[i].instance);
    1.74 +                }
    1.75 +                if (dests[i].is_default)
    1.76 +                    aList.InsertElementAt(0, fullName);
    1.77 +                else
    1.78 +                    aList.AppendElement(fullName);
    1.79 +            }
    1.80 +        }
    1.81 +        (gCupsShim.mCupsFreeDests)(num_dests, dests);
    1.82 +    }
    1.83 +
    1.84 +    // Build the "classic" list of printers -- those accessed by running
    1.85 +    // an opaque command. This list always contains a printer named "default".
    1.86 +    // In addition, we look for either an environment variable
    1.87 +    // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
    1.88 +    // print.printer_list, which contains a space-separated list of printer
    1.89 +    // names.
    1.90 +    aList.AppendElement(
    1.91 +            NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
    1.92 +
    1.93 +    nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
    1.94 +    if (list.IsEmpty()) {
    1.95 +        list = Preferences::GetCString("print.printer_list");
    1.96 +    }
    1.97 +    if (!list.IsEmpty()) {
    1.98 +        // For each printer (except "default" which was already added),
    1.99 +        // construct a string "PostScript/<name>" and append it to the list.
   1.100 +        char *state;
   1.101 +
   1.102 +        for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state);
   1.103 +                nullptr != name;
   1.104 +                name = PL_strtok_r(nullptr, " ", &state)
   1.105 +        ) {
   1.106 +            if (0 != strcmp(name, "default")) {
   1.107 +                nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME);
   1.108 +                fullName.Append(name);
   1.109 +                aList.AppendElement(fullName);
   1.110 +            }
   1.111 +        }
   1.112 +    }
   1.113 +}
   1.114 +
   1.115 +
   1.116 +/* Identify the printer type */
   1.117 +nsPSPrinterList::PrinterType
   1.118 +nsPSPrinterList::GetPrinterType(const nsACString& aName)
   1.119 +{
   1.120 +    if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME)))
   1.121 +        return kTypePS;
   1.122 +    else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER)))
   1.123 +        return kTypeCUPS;
   1.124 +    else
   1.125 +        return kTypeUnknown;
   1.126 +}

mercurial