Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/widget/gtk/nsPSPrinters.cpp
Line
Count
Source (jump to first uncovered line)
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
0
#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
0
#define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
26
27
nsCUPSShim gCupsShim;
28
29
nsPSPrinterList::nsPSPrinterList()
30
0
{
31
0
    // Should we try cups?
32
0
    if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
33
0
        !gCupsShim.IsInitialized()) {
34
0
        gCupsShim.Init();
35
0
    }
36
0
}
37
38
39
/* Check whether the PostScript module has been disabled at runtime */
40
bool
41
nsPSPrinterList::Enabled()
42
0
{
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
0
47
0
    // is the PS module enabled?
48
0
    return Preferences::GetBool("print.postscript.enabled", true);
49
0
}
50
51
52
/* Fetch a list of printers handled by the PostsScript module */
53
void
54
nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
55
0
{
56
0
    aList.Clear();
57
0
58
0
    // Query CUPS for a printer list. The default printer goes to the
59
0
    // head of the output list; others are appended.
60
0
    if (gCupsShim.IsInitialized()) {
61
0
        cups_dest_t *dests;
62
0
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
0
                }
72
0
                if (dests[i].is_default)
73
0
                    aList.InsertElementAt(0, fullName);
74
0
                else
75
0
                    aList.AppendElement(fullName);
76
0
            }
77
0
        }
78
0
        (gCupsShim.mCupsFreeDests)(num_dests, dests);
79
0
    }
80
0
81
0
    // Build the "classic" list of printers -- those accessed by running
82
0
    // an opaque command. This list always contains a printer named "default".
83
0
    // In addition, we look for either an environment variable
84
0
    // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
85
0
    // print.printer_list, which contains a space-separated list of printer
86
0
    // names.
87
0
    aList.AppendElement(
88
0
            NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
89
0
90
0
    nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
91
0
    if (list.IsEmpty()) {
92
0
        Preferences::GetCString("print.printer_list", list);
93
0
    }
94
0
    if (!list.IsEmpty()) {
95
0
        // For each printer (except "default" which was already added),
96
0
        // construct a string "PostScript/<name>" and append it to the list.
97
0
        char *state;
98
0
99
0
        for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state);
100
0
                nullptr != name;
101
0
                name = PL_strtok_r(nullptr, " ", &state)
102
0
        ) {
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
0
            }
108
0
        }
109
0
    }
110
0
}
111
112
113
/* Identify the printer type */
114
nsPSPrinterList::PrinterType
115
nsPSPrinterList::GetPrinterType(const nsACString& aName)
116
0
{
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
0
    else
122
0
        return kTypeUnknown;
123
0
}