2004-09-07 17:55:27 +00: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 11:12:37 +00: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 17:55:27 +00:00
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCUPSShim.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsPSPrinters.h"
|
|
|
|
#include "nsReadableUtils.h" // StringBeginsWith()
|
2010-12-23 00:59:41 +00:00
|
|
|
#include "nsCUPSShim.h"
|
2011-05-27 08:15:20 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2004-09-07 17:55:27 +00:00
|
|
|
|
|
|
|
#include "prlink.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
|
2011-05-27 08:15:20 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2004-09-07 17:55:27 +00:00
|
|
|
#define NS_CUPS_PRINTER "CUPS/"
|
|
|
|
#define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
|
|
|
|
|
2006-11-30 18:27:27 +00:00
|
|
|
/* dummy printer name for the gfx/src/ps driver */
|
|
|
|
#define NS_POSTSCRIPT_DRIVER_NAME "PostScript/"
|
2004-09-07 17:55:27 +00:00
|
|
|
|
2010-12-23 00:59:41 +00:00
|
|
|
nsCUPSShim gCupsShim;
|
|
|
|
|
2011-08-25 08:46:02 +00:00
|
|
|
nsPSPrinterList::nsPSPrinterList()
|
2004-09-07 17:55:27 +00:00
|
|
|
{
|
|
|
|
// Should we try cups?
|
2011-09-29 06:19:26 +00:00
|
|
|
if (Preferences::GetBool("print.postscript.cups.enabled", true) &&
|
2011-08-25 08:46:02 +00:00
|
|
|
!gCupsShim.IsInitialized()) {
|
2010-12-23 00:59:41 +00:00
|
|
|
gCupsShim.Init();
|
2011-05-27 08:15:20 +00:00
|
|
|
}
|
2004-09-07 17:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check whether the PostScript module has been disabled at runtime */
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2004-09-07 17:55:27 +00:00
|
|
|
nsPSPrinterList::Enabled()
|
|
|
|
{
|
|
|
|
const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
|
2005-08-16 12:16:08 +00:00
|
|
|
if (val && (val[0] == '0' || !PL_strcasecmp(val, "false")))
|
2011-10-03 07:56:21 +00:00
|
|
|
return false;
|
2004-09-07 17:55:27 +00:00
|
|
|
|
|
|
|
// is the PS module enabled?
|
2011-09-29 06:19:26 +00:00
|
|
|
return Preferences::GetBool("print.postscript.enabled", true);
|
2004-09-07 17:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Fetch a list of printers handled by the PostsScript module */
|
|
|
|
void
|
2009-01-22 04:15:34 +00:00
|
|
|
nsPSPrinterList::GetPrinterList(nsTArray<nsCString>& aList)
|
2004-09-07 17:55:27 +00: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 00:59:41 +00:00
|
|
|
if (gCupsShim.IsInitialized()) {
|
2004-09-07 17:55:27 +00:00
|
|
|
cups_dest_t *dests;
|
|
|
|
|
2010-12-23 00:59:41 +00:00
|
|
|
int num_dests = (gCupsShim.mCupsGetDests)(&dests);
|
2004-09-07 17:55:27 +00:00
|
|
|
if (num_dests) {
|
|
|
|
for (int i = 0; i < num_dests; i++) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString fullName(NS_CUPS_PRINTER);
|
2004-09-07 17:55:27 +00:00
|
|
|
fullName.Append(dests[i].name);
|
2013-10-08 18:47:37 +00:00
|
|
|
if (dests[i].instance != nullptr) {
|
2005-03-20 16:28:58 +00:00
|
|
|
fullName.Append("/");
|
|
|
|
fullName.Append(dests[i].instance);
|
|
|
|
}
|
2004-09-07 17:55:27 +00:00
|
|
|
if (dests[i].is_default)
|
2009-01-22 04:15:34 +00:00
|
|
|
aList.InsertElementAt(0, fullName);
|
2004-09-07 17:55:27 +00:00
|
|
|
else
|
2009-01-22 04:15:34 +00:00
|
|
|
aList.AppendElement(fullName);
|
2004-09-07 17:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-23 00:59:41 +00:00
|
|
|
(gCupsShim.mCupsFreeDests)(num_dests, dests);
|
2004-09-07 17:55:27 +00: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 04:15:34 +00:00
|
|
|
aList.AppendElement(
|
2004-09-07 17:55:27 +00:00
|
|
|
NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString list(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
|
2011-05-27 08:15:20 +00:00
|
|
|
if (list.IsEmpty()) {
|
|
|
|
list = Preferences::GetCString("print.printer_list");
|
|
|
|
}
|
2004-09-07 17:55:27 +00: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 14:20:58 +00:00
|
|
|
nullptr != name;
|
|
|
|
name = PL_strtok_r(nullptr, " ", &state)
|
2004-09-07 17:55:27 +00:00
|
|
|
) {
|
|
|
|
if (0 != strcmp(name, "default")) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString fullName(NS_POSTSCRIPT_DRIVER_NAME);
|
2004-09-07 17:55:27 +00:00
|
|
|
fullName.Append(name);
|
2009-01-22 04:15:34 +00:00
|
|
|
aList.AppendElement(fullName);
|
2004-09-07 17:55:27 +00: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;
|
|
|
|
}
|