LCOV - code coverage report
Current view: top level - widget/gtk - nsPSPrinters.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 1 45 2.2 %
Date: 2017-07-14 16:53:18 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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           0 : nsPSPrinterList::nsPSPrinterList()
      30             : {
      31             :     // Should we try cups?
      32           0 :     if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
      33           0 :         !gCupsShim.IsInitialized()) {
      34           0 :         gCupsShim.Init();
      35             :     }
      36           0 : }
      37             : 
      38             : 
      39             : /* Check whether the PostScript module has been disabled at runtime */
      40             : bool
      41           0 : nsPSPrinterList::Enabled()
      42             : {
      43           0 :     const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
      44           0 :     if (val && (val[0] == '0' || !PL_strcasecmp(val, "false")))
      45           0 :         return false;
      46             : 
      47             :     // is the PS module enabled?
      48           0 :     return Preferences::GetBool("print.postscript.enabled", true);
      49             : }
      50             : 
      51             : 
      52             : /* Fetch a list of printers handled by the PostsScript module */
      53             : void
      54           0 : nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
      55             : {
      56           0 :     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           0 :     if (gCupsShim.IsInitialized()) {
      61             :         cups_dest_t *dests;
      62             : 
      63           0 :         int num_dests = (gCupsShim.mCupsGetDests)(&dests);
      64           0 :         if (num_dests) {
      65           0 :             for (int i = 0; i < num_dests; i++) {
      66           0 :                 nsAutoCString fullName(NS_CUPS_PRINTER);
      67           0 :                 fullName.Append(dests[i].name);
      68           0 :                 if (dests[i].instance != nullptr) {
      69           0 :                     fullName.Append('/');
      70           0 :                     fullName.Append(dests[i].instance);
      71             :                 }
      72           0 :                 if (dests[i].is_default)
      73           0 :                     aList.InsertElementAt(0, fullName);
      74             :                 else
      75           0 :                     aList.AppendElement(fullName);
      76             :             }
      77             :         }
      78           0 :         (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           0 :     aList.AppendElement(
      88           0 :             NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
      89             : 
      90           0 :     nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
      91           0 :     if (list.IsEmpty()) {
      92           0 :         list = Preferences::GetCString("print.printer_list");
      93             :     }
      94           0 :     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           0 :         for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state);
     100           0 :                 nullptr != name;
     101             :                 name = PL_strtok_r(nullptr, " ", &state)
     102             :         ) {
     103           0 :             if (0 != strcmp(name, "default")) {
     104           0 :                 nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME);
     105           0 :                 fullName.Append(name);
     106           0 :                 aList.AppendElement(fullName);
     107             :             }
     108             :         }
     109             :     }
     110           0 : }
     111             : 
     112             : 
     113             : /* Identify the printer type */
     114             : nsPSPrinterList::PrinterType
     115           0 : nsPSPrinterList::GetPrinterType(const nsACString& aName)
     116             : {
     117           0 :     if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME)))
     118           0 :         return kTypePS;
     119           0 :     else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER)))
     120           0 :         return kTypeCUPS;
     121             :     else
     122           0 :         return kTypeUnknown;
     123           9 : }

Generated by: LCOV version 1.13