Back to index

lightning-sunbird  0.9+nobinonly
nsPSPrinters.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ex: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla PostScript driver printer list component.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Kenneth Herron <kherron+mozilla@fmailbox.com>
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nscore.h"
00040 #include "nsCUPSShim.h"
00041 #include "nsIDeviceContext.h"       // GFX error codes
00042 #include "nsIDeviceContextSpecPS.h" // NS_POSTSCRIPT_DRIVER_NAME
00043 #include "nsIPrefBranch.h"
00044 #include "nsIPrefService.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsPrintfCString.h"
00047 #include "nsPSPrinters.h"
00048 #include "nsReadableUtils.h"        // StringBeginsWith()
00049 
00050 #include "prlink.h"
00051 #include "prenv.h"
00052 #include "plstr.h"
00053 
00054 #define NS_CUPS_PRINTER "CUPS/"
00055 #define NS_CUPS_PRINTER_LEN (sizeof(NS_CUPS_PRINTER) - 1)
00056 
00057 
00058 /* Initialize the printer manager object */
00059 nsresult
00060 nsPSPrinterList::Init()
00061 {
00062     nsresult rv;
00063 
00064     mPrefSvc = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
00065     if (NS_SUCCEEDED(rv))
00066         rv = mPrefSvc->GetBranch("print.", getter_AddRefs(mPref));
00067     NS_ENSURE_SUCCESS(rv, NS_ERROR_NOT_INITIALIZED);
00068 
00069     // Should we try cups?
00070     PRBool useCups = PR_TRUE;
00071     rv = mPref->GetBoolPref("postscript.cups.enabled", &useCups);
00072     if (useCups)
00073         mCups.Init();
00074     return NS_OK;
00075 }
00076 
00077 
00078 /* Check whether the PostScript module has been disabled at runtime */
00079 PRBool
00080 nsPSPrinterList::Enabled()
00081 {
00082     const char *val = PR_GetEnv("MOZILLA_POSTSCRIPT_ENABLED");
00083     if (val && (val[0] == '0' || !PL_strcasecmp(val, "false")))
00084         return PR_FALSE;
00085 
00086     // is the PS module enabled?
00087     PRBool setting = PR_TRUE;
00088     mPref->GetBoolPref("postscript.enabled", &setting);
00089     return setting;
00090 }
00091 
00092 
00093 /* Fetch a list of printers handled by the PostsScript module */
00094 void
00095 nsPSPrinterList::GetPrinterList(nsCStringArray& aList)
00096 {
00097     aList.Clear();
00098 
00099     // Query CUPS for a printer list. The default printer goes to the
00100     // head of the output list; others are appended.
00101     if (mCups.IsInitialized()) {
00102         cups_dest_t *dests;
00103 
00104         int num_dests = (mCups.mCupsGetDests)(&dests);
00105         if (num_dests) {
00106             for (int i = 0; i < num_dests; i++) {
00107                 nsCAutoString fullName(NS_CUPS_PRINTER);
00108                 fullName.Append(dests[i].name);
00109                 if (dests[i].instance != NULL) {
00110                     fullName.Append("/");
00111                     fullName.Append(dests[i].instance);
00112                 }
00113                 if (dests[i].is_default)
00114                     aList.InsertCStringAt(fullName, 0);
00115                 else
00116                     aList.AppendCString(fullName);
00117             }
00118         }
00119         (mCups.mCupsFreeDests)(num_dests, dests);
00120     }
00121 
00122     // Build the "classic" list of printers -- those accessed by running
00123     // an opaque command. This list always contains a printer named "default".
00124     // In addition, we look for either an environment variable
00125     // MOZILLA_POSTSCRIPT_PRINTER_LIST or a preference setting
00126     // print.printer_list, which contains a space-separated list of printer
00127     // names.
00128     aList.AppendCString(
00129             NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME "default"));
00130 
00131     nsXPIDLCString list;
00132     list.Assign(PR_GetEnv("MOZILLA_POSTSCRIPT_PRINTER_LIST"));
00133     if (list.IsEmpty())
00134         mPref->GetCharPref("printer_list", getter_Copies(list));
00135     if (!list.IsEmpty()) {
00136         // For each printer (except "default" which was already added),
00137         // construct a string "PostScript/<name>" and append it to the list.
00138         char *state;
00139 
00140         for (char *name = PL_strtok_r(list.BeginWriting(), " ", &state);
00141                 nsnull != name;
00142                 name = PL_strtok_r(nsnull, " ", &state)
00143         ) {
00144             if (0 != strcmp(name, "default")) {
00145                 nsCAutoString fullName(NS_POSTSCRIPT_DRIVER_NAME);
00146                 fullName.Append(name);
00147                 aList.AppendCString(fullName);
00148             }
00149         }
00150     }
00151 }
00152 
00153 
00154 /* Identify the printer type */
00155 nsPSPrinterList::PrinterType
00156 nsPSPrinterList::GetPrinterType(const nsACString& aName)
00157 {
00158     if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_POSTSCRIPT_DRIVER_NAME)))
00159         return kTypePS;
00160     else if (StringBeginsWith(aName, NS_LITERAL_CSTRING(NS_CUPS_PRINTER)))
00161         return kTypeCUPS;
00162     else
00163         return kTypeUnknown;
00164 }