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