Back to index

lightning-sunbird  0.9+nobinonly
nsPrintOptionsImpl.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jessica Blanco <jblanco@us.ibm.com>
00024  *   Bastiaan Jacques <baafie@planet.nl>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsPrintOptionsImpl.h"
00041 #include "nsCoord.h"
00042 #include "nsUnitConversion.h"
00043 #include "nsReadableUtils.h"
00044 #include "nsPrintSettingsImpl.h"
00045 
00046 #include "nsIDOMWindow.h"
00047 #include "nsIServiceManager.h"
00048 #include "nsIDialogParamBlock.h"
00049 #include "nsXPCOM.h"
00050 #include "nsISupportsPrimitives.h"
00051 #include "nsIWindowWatcher.h"
00052 #include "nsIDOMWindowInternal.h"
00053 #include "nsVoidArray.h"
00054 #include "nsSupportsArray.h"
00055 #include "prprf.h"
00056 
00057 // For Prefs
00058 #include "nsIPrefBranch.h"
00059 #include "nsIPrefService.h"
00060 #include "nsIServiceManager.h"
00061 
00062 #include "nsISimpleEnumerator.h"
00063 #include "nsISupportsPrimitives.h"
00064 #include "nsGfxCIID.h"
00065 #include "stdlib.h"
00066 #include "nsAutoPtr.h"
00067 
00068 static NS_DEFINE_IID(kCPrinterEnumerator, NS_PRINTER_ENUMERATOR_CID);
00069 
00070 NS_IMPL_ISUPPORTS2(nsPrintOptions, nsIPrintOptions, nsIPrintSettingsService)
00071 
00072 // Pref Constants
00073     static const char kMarginTop[]       = "print_margin_top";
00074 static const char kMarginLeft[]      = "print_margin_left";
00075 static const char kMarginBottom[]    = "print_margin_bottom";
00076 static const char kMarginRight[]     = "print_margin_right";
00077 
00078 // Prefs for Print Options
00079 static const char kPrintEvenPages[]       = "print_evenpages";
00080 static const char kPrintOddPages[]        = "print_oddpages";
00081 static const char kPrintHeaderStrLeft[]   = "print_headerleft";
00082 static const char kPrintHeaderStrCenter[] = "print_headercenter";
00083 static const char kPrintHeaderStrRight[]  = "print_headerright";
00084 static const char kPrintFooterStrLeft[]   = "print_footerleft";
00085 static const char kPrintFooterStrCenter[] = "print_footercenter";
00086 static const char kPrintFooterStrRight[]  = "print_footerright";
00087 
00088 // Additional Prefs
00089 static const char kPrintPaperSize[]     = "print_paper_size"; // deprecated
00090 
00091 static const char kPrintReversed[]      = "print_reversed";
00092 static const char kPrintInColor[]       = "print_in_color";
00093 static const char kPrintPaperName[]     = "print_paper_name";
00094 static const char kPrintPlexName[]      = "print_plex_name";
00095 static const char kPrintPaperSizeType[] = "print_paper_size_type";
00096 static const char kPrintPaperData[]     = "print_paper_data";
00097 static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
00098 static const char kPrintPaperWidth[]    = "print_paper_width";
00099 static const char kPrintPaperHeight[]   = "print_paper_height";
00100 static const char kPrintColorspace[]    = "print_colorspace";
00101 static const char kPrintResolutionName[]= "print_resolution_name";
00102 static const char kPrintDownloadFonts[] = "print_downloadfonts";
00103 static const char kPrintOrientation[]   = "print_orientation";
00104 static const char kPrintCommand[]       = "print_command";
00105 static const char kPrinterName[]        = "print_printer";
00106 static const char kPrintToFile[]        = "print_to_file";
00107 static const char kPrintToFileName[]    = "print_to_filename";
00108 static const char kPrintPageDelay[]     = "print_pagedelay";
00109 static const char kPrintBGColors[]      = "print_bgcolor";
00110 static const char kPrintBGImages[]      = "print_bgimages";
00111 static const char kPrintShrinkToFit[]   = "print_shrink_to_fit";
00112 static const char kPrintScaling[]       = "print_scaling";
00113 
00114 static const char kJustLeft[]   = "left";
00115 static const char kJustCenter[] = "center";
00116 static const char kJustRight[]  = "right";
00117 
00118 static NS_DEFINE_IID(kPrinterEnumeratorCID, NS_PRINTER_ENUMERATOR_CID);
00119 
00120 
00121 nsPrintOptions::nsPrintOptions()
00122 {
00123 }
00124 
00125 nsPrintOptions::~nsPrintOptions()
00126 {
00127   if (mDefaultFont) {
00128     delete mDefaultFont;
00129   }
00130 }
00131 
00132 
00133 
00134 nsresult
00135 nsPrintOptions::Init()
00136 {
00137   mDefaultFont = new nsFont("Times", NS_FONT_STYLE_NORMAL,
00138                             NS_FONT_VARIANT_NORMAL, NS_FONT_WEIGHT_NORMAL, 0,
00139                             NSIntPointsToTwips(10));
00140   NS_ENSURE_TRUE(mDefaultFont, NS_ERROR_OUT_OF_MEMORY);
00141 
00142   nsresult rv;
00143   nsCOMPtr<nsIPrefService> prefService =
00144       do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
00145   NS_ENSURE_SUCCESS(rv, rv);
00146 
00147   return prefService->GetBranch("print.", getter_AddRefs(mPrefBranch));
00148 }
00149 
00150 
00151 //**************************************************************
00152 //** PageList Enumerator
00153 //**************************************************************
00154 class
00155     nsPrinterListEnumerator : public nsISimpleEnumerator
00156 {
00157   public:
00158     nsPrinterListEnumerator();
00159     virtual ~nsPrinterListEnumerator();
00160 
00161     //nsISupports interface
00162     NS_DECL_ISUPPORTS
00163 
00164     //nsISimpleEnumerator interface
00165     NS_DECL_NSISIMPLEENUMERATOR
00166 
00167     NS_IMETHOD Init();
00168 
00169   protected:
00170     PRUnichar **mPrinters;
00171     PRUint32 mCount;
00172     PRUint32 mIndex;
00173 };
00174 
00175 nsPrinterListEnumerator::nsPrinterListEnumerator() :
00176     mPrinters(nsnull), mCount(0), mIndex(0)
00177 {
00178 }
00179 
00180 nsPrinterListEnumerator::~nsPrinterListEnumerator()
00181 {
00182   if (!mPrinters)
00183     return;
00184 
00185   PRUint32 i;
00186   for (i = 0; i < mCount; i++ ) {
00187     nsMemory::Free(mPrinters[i]);
00188   }
00189   nsMemory::Free(mPrinters);
00190 }
00191 
00192 NS_IMPL_ISUPPORTS1(nsPrinterListEnumerator, nsISimpleEnumerator)
00193 
00194 NS_IMETHODIMP
00195 nsPrinterListEnumerator::Init()
00196 {
00197   nsresult rv;
00198   nsCOMPtr<nsIPrinterEnumerator> printerEnumerator;
00199 
00200   printerEnumerator = do_CreateInstance(kCPrinterEnumerator, &rv);
00201   if (NS_FAILED(rv))
00202     return rv;
00203 
00204   return printerEnumerator->EnumeratePrinters(&mCount, &mPrinters);
00205 }
00206 
00207 NS_IMETHODIMP
00208 nsPrinterListEnumerator::HasMoreElements(PRBool *result)
00209 {
00210   *result = (mIndex < mCount);
00211   return NS_OK;
00212 }
00213 
00214 NS_IMETHODIMP
00215 nsPrinterListEnumerator::GetNext(nsISupports **aPrinter)
00216 {
00217   if (mIndex >= mCount) {
00218     return NS_ERROR_UNEXPECTED;
00219   }
00220 
00221   PRUnichar *printerName = mPrinters[mIndex++];
00222   nsCOMPtr<nsISupportsString> printerNameWrapper;
00223   nsresult rv;
00224 
00225   printerNameWrapper = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00226   NS_ENSURE_SUCCESS(rv, rv);
00227   printerNameWrapper->SetData(nsDependentString(printerName));
00228   *aPrinter = NS_STATIC_CAST(nsISupports*, printerNameWrapper);
00229   NS_ADDREF(*aPrinter);
00230   return NS_OK;
00231 }
00232 
00233 NS_IMETHODIMP 
00234 nsPrintOptions::SetFontNamePointSize(const nsAString& aFontName,
00235                                      PRInt32 aPointSize)
00236 {
00237   if (mDefaultFont && !aFontName.IsEmpty() && aPointSize > 0) {
00238     mDefaultFont->name = aFontName;
00239     mDefaultFont->size = NSIntPointsToTwips(aPointSize);
00240     return NS_OK;
00241   }
00242   return NS_ERROR_FAILURE;
00243 }
00244 
00245 NS_IMETHODIMP
00246 nsPrintOptions::SetDefaultFont(nsFont &aFont)
00247 {
00248   if (mDefaultFont)
00249     delete mDefaultFont;
00250 
00251   mDefaultFont = new nsFont(aFont);
00252   NS_ENSURE_TRUE(mDefaultFont, NS_ERROR_OUT_OF_MEMORY);
00253 
00254   return NS_OK;
00255 }
00256 
00257 NS_IMETHODIMP
00258 nsPrintOptions::GetDefaultFont(nsFont &aFont)
00259 {
00260   NS_ENSURE_STATE(mDefaultFont);
00261   aFont = *mDefaultFont;
00262   return NS_OK;
00263 }
00264 
00265 NS_IMETHODIMP
00266 nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings *aPS)
00267 {
00268   NS_ENSURE_ARG_POINTER(aPS);
00269   nsresult rv;
00270 
00271   // create a nsISupportsArray of the parameters
00272   // being passed to the window
00273   nsCOMPtr<nsISupportsArray> array;
00274   rv = NS_NewISupportsArray(getter_AddRefs(array));
00275   NS_ENSURE_SUCCESS(rv, rv);
00276 
00277   nsCOMPtr<nsISupports> psSupports = do_QueryInterface(aPS);
00278   NS_ASSERTION(psSupports, "PrintSettings must be a supports");
00279   array->AppendElement(psSupports);
00280 
00281   nsCOMPtr<nsIDialogParamBlock> ioParamBlock =
00282       do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &rv);
00283   NS_ENSURE_SUCCESS(rv, rv);
00284 
00285   ioParamBlock->SetInt(0, 0);
00286 
00287   nsCOMPtr<nsISupports> blkSupps = do_QueryInterface(ioParamBlock);
00288   NS_ASSERTION(blkSupps, "IOBlk must be a supports");
00289   array->AppendElement(blkSupps);
00290 
00291   nsCOMPtr<nsIWindowWatcher> wwatch =
00292       do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
00293   NS_ENSURE_SUCCESS(rv, rv);
00294 
00295   nsCOMPtr<nsIDOMWindow> active;
00296   wwatch->GetActiveWindow(getter_AddRefs(active));
00297   nsCOMPtr<nsIDOMWindowInternal> parent = do_QueryInterface(active);
00298   // null |parent| is non-fatal
00299 
00300   nsCOMPtr<nsIDOMWindow> newWindow;
00301 
00302   return wwatch->OpenWindow(parent,
00303                             "chrome://global/content/printPageSetup.xul",
00304                             "_blank","chrome,modal,centerscreen", array,
00305                             getter_AddRefs(newWindow));
00306 }
00307 
00313 const char*
00314 nsPrintOptions::GetPrefName(const char * aPrefName,
00315                             const nsAString& aPrinterName)
00316 {
00317   if (!aPrefName || !*aPrefName) {
00318     NS_ERROR("Must have a valid pref name!");
00319     return aPrefName;
00320   }
00321 
00322   mPrefName.Truncate(); /* mPrefName = ""; */
00323 
00324   if (aPrinterName.Length()) {
00325     mPrefName.Append("printer_");
00326     AppendUTF16toUTF8(aPrinterName, mPrefName);
00327     mPrefName.Append(".");
00328   }
00329   mPrefName += aPrefName;
00330 
00331   return mPrefName.get();
00332 }
00333 
00334 //----------------------------------------------------------------------
00335 // Testing of read/write prefs
00336 // This define controls debug output
00337 #ifdef DEBUG_rods_X
00338 static void WriteDebugStr(const char* aArg1, const char* aArg2,
00339                           const PRUnichar* aStr)
00340 {
00341   nsString str(aStr);
00342   PRUnichar s = '&';
00343   PRUnichar r = '_';
00344   str.ReplaceChar(s, r);
00345 
00346   printf("%s %s = %s \n", aArg1, aArg2, ToNewUTF8String(str));
00347 }
00348 const char* kWriteStr = "Write Pref:";
00349 const char* kReadStr  = "Read Pref:";
00350 #define DUMP_STR(_a1, _a2, _a3)  WriteDebugStr((_a1), GetPrefName((_a2), \
00351 aPrefName), (_a3));
00352 #define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \
00353 GetPrefName((_a2), aPrefName), (_a3)?"T":"F");
00354 #define DUMP_INT(_a1, _a2, _a3)  printf("%s %s = %d \n", (_a1), \
00355 GetPrefName((_a2), aPrefName), (_a3));
00356 #define DUMP_DBL(_a1, _a2, _a3)  printf("%s %s = %10.5f \n", (_a1), \
00357 GetPrefName((_a2), aPrefName), (_a3));
00358 #else
00359 #define DUMP_STR(_a1, _a2, _a3)
00360 #define DUMP_BOOL(_a1, _a2, _a3)
00361 #define DUMP_INT(_a1, _a2, _a3)
00362 #define DUMP_DBL(_a1, _a2, _a3)
00363 #endif /* DEBUG_rods_X */
00364 //----------------------------------------------------------------------
00365 
00371 nsresult 
00372 nsPrintOptions::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName,
00373                           PRUint32 aFlags)
00374 {
00375   NS_ENSURE_STATE(mPrefBranch);
00376   NS_ENSURE_ARG_POINTER(aPS);
00377 
00378   if (aFlags & nsIPrintSettings::kInitSaveMargins) {
00379     nscoord halfInch = NS_INCHES_TO_TWIPS(0.5);
00380     nsMargin margin;
00381     margin.SizeTo(halfInch, halfInch, halfInch, halfInch);
00382     ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top,
00383                           kMarginTop);
00384     DUMP_INT(kReadStr, kMarginTop, margin.top);
00385     ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left,
00386                           kMarginLeft);
00387     DUMP_INT(kReadStr, kMarginLeft, margin.left);
00388     ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
00389                           margin.bottom, kMarginBottom);
00390     DUMP_INT(kReadStr, kMarginBottom, margin.bottom);
00391     ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right,
00392                           kMarginRight);
00393     DUMP_INT(kReadStr, kMarginRight, margin.right);
00394     aPS->SetMarginInTwips(margin);
00395   }
00396 
00397   PRBool   b;
00398   nsAutoString str;
00399   PRInt32  iVal;
00400   double   dbl;
00401 
00402 #define GETBOOLPREF(_prefname, _retval)                 \
00403   NS_SUCCEEDED(                                         \
00404     mPrefBranch->GetBoolPref(                           \
00405       GetPrefName(_prefname, aPrinterName), _retval     \
00406     )                                                   \
00407   )
00408 
00409 #define GETSTRPREF(_prefname, _retval)                  \
00410   NS_SUCCEEDED(                                         \
00411     ReadPrefString(                                     \
00412       GetPrefName(_prefname, aPrinterName), _retval     \
00413     )                                                   \
00414   )
00415 
00416 #define GETINTPREF(_prefname, _retval)                  \
00417   NS_SUCCEEDED(                                         \
00418     mPrefBranch->GetIntPref(                            \
00419       GetPrefName(_prefname, aPrinterName), _retval     \
00420     )                                                   \
00421   )
00422 
00423 #define GETDBLPREF(_prefname, _retval)                  \
00424   NS_SUCCEEDED(                                         \
00425     ReadPrefDouble(                                     \
00426       GetPrefName(_prefname, aPrinterName), _retval     \
00427     )                                                   \
00428   )
00429 
00430   if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
00431   if (GETBOOLPREF(kPrintEvenPages, &b)) {
00432     aPS->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, b);
00433     DUMP_BOOL(kReadStr, kPrintEvenPages, b);
00434   }
00435   }
00436 
00437   if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
00438     if (GETBOOLPREF(kPrintOddPages, &b)) {
00439       aPS->SetPrintOptions(nsIPrintSettings::kPrintOddPages, b);
00440       DUMP_BOOL(kReadStr, kPrintOddPages, b);
00441     }
00442   }
00443 
00444   if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
00445     if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
00446       aPS->SetHeaderStrLeft(str.get());
00447       DUMP_STR(kReadStr, kPrintHeaderStrLeft, str.get());
00448     }
00449   }
00450 
00451   if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
00452     if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
00453       aPS->SetHeaderStrCenter(str.get());
00454       DUMP_STR(kReadStr, kPrintHeaderStrCenter, str.get());
00455     }
00456   }
00457 
00458   if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
00459     if (GETSTRPREF(kPrintHeaderStrRight, str)) {
00460       aPS->SetHeaderStrRight(str.get());
00461       DUMP_STR(kReadStr, kPrintHeaderStrRight, str.get());
00462     }
00463   }
00464 
00465   if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
00466     if (GETSTRPREF(kPrintFooterStrLeft, str)) {
00467       aPS->SetFooterStrLeft(str.get());
00468       DUMP_STR(kReadStr, kPrintFooterStrLeft, str.get());
00469     }
00470   }
00471 
00472   if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
00473     if (GETSTRPREF(kPrintFooterStrCenter, str)) {
00474       aPS->SetFooterStrCenter(str.get());
00475       DUMP_STR(kReadStr, kPrintFooterStrCenter, str.get());
00476     }
00477   }
00478 
00479   if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
00480     if (GETSTRPREF(kPrintFooterStrRight, str)) {
00481       aPS->SetFooterStrRight(str.get());
00482       DUMP_STR(kReadStr, kPrintFooterStrRight, str.get());
00483     }
00484   }
00485 
00486   if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
00487     if (GETBOOLPREF(kPrintBGColors, &b)) {
00488       aPS->SetPrintBGColors(b);
00489       DUMP_BOOL(kReadStr, kPrintBGColors, b);
00490     }
00491   }
00492 
00493   if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
00494     if (GETBOOLPREF(kPrintBGImages, &b)) {
00495       aPS->SetPrintBGImages(b);
00496       DUMP_BOOL(kReadStr, kPrintBGImages, b);
00497     }
00498   }
00499 
00500   if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
00501     if (GETINTPREF(kPrintPaperSize, &iVal)) { // this has been deprecated
00502       aPS->SetPaperSize(iVal);
00503       DUMP_INT(kReadStr, kPrintPaperSize, iVal);
00504     }
00505   }
00506 
00507   if (aFlags & nsIPrintSettings::kInitSaveReversed) {
00508     if (GETBOOLPREF(kPrintReversed, &b)) {
00509       aPS->SetPrintReversed(b);
00510       DUMP_BOOL(kReadStr, kPrintReversed, b);
00511     }
00512   }
00513 
00514   if (aFlags & nsIPrintSettings::kInitSaveInColor) {
00515     if (GETBOOLPREF(kPrintInColor, &b)) {
00516       aPS->SetPrintInColor(b);
00517       DUMP_BOOL(kReadStr, kPrintInColor, b);
00518     }
00519   }
00520 
00521   if (aFlags & nsIPrintSettings::kInitSavePaperName) {
00522     if (GETSTRPREF(kPrintPaperName, str)) {
00523       aPS->SetPaperName(str.get());
00524       DUMP_STR(kReadStr, kPrintPaperName, str.get());
00525     }
00526   }
00527 
00528   if (aFlags & nsIPrintSettings::kInitSavePlexName) {
00529     if (GETSTRPREF(kPrintPlexName, str)) {
00530       aPS->SetPlexName(str.get());
00531       DUMP_STR(kReadStr, kPrintPlexName, str.get());
00532     }
00533   }
00534 
00535   if (aFlags & nsIPrintSettings::kInitSavePaperSizeUnit) {
00536     if (GETINTPREF(kPrintPaperSizeUnit, &iVal)) {
00537       aPS->SetPaperSizeUnit(iVal);
00538       DUMP_INT(kReadStr, kPrintPaperSizeUnit, iVal);
00539     }
00540   }
00541 
00542   if (aFlags & nsIPrintSettings::kInitSavePaperSizeType) {
00543     if (GETINTPREF(kPrintPaperSizeType, &iVal)) {
00544       aPS->SetPaperSizeType(iVal);
00545       DUMP_INT(kReadStr, kPrintPaperSizeType, iVal);
00546     }
00547   }
00548 
00549   if (aFlags & nsIPrintSettings::kInitSavePaperData) {
00550     if (GETINTPREF(kPrintPaperData, &iVal)) {
00551       aPS->SetPaperData(iVal);
00552       DUMP_INT(kReadStr, kPrintPaperData, iVal);
00553     }
00554   }
00555 
00556   if (aFlags & nsIPrintSettings::kInitSavePaperWidth) {
00557     if (GETDBLPREF(kPrintPaperWidth, dbl)) {
00558       aPS->SetPaperWidth(dbl);
00559       DUMP_DBL(kReadStr, kPrintPaperWidth, dbl);
00560     }
00561   }
00562 
00563   if (aFlags & nsIPrintSettings::kInitSavePaperHeight) {
00564     if (GETDBLPREF(kPrintPaperHeight, dbl)) {
00565       aPS->SetPaperHeight(dbl);
00566       DUMP_DBL(kReadStr, kPrintPaperHeight, dbl);
00567     }
00568   }
00569 
00570   if (aFlags & nsIPrintSettings::kInitSaveColorspace) {
00571     if (GETSTRPREF(kPrintColorspace, str)) {
00572       aPS->SetColorspace(str.get());
00573       DUMP_STR(kReadStr, kPrintColorspace, str.get());
00574     }
00575   }
00576 
00577   if (aFlags & nsIPrintSettings::kInitSaveResolutionName) {
00578     if (GETSTRPREF(kPrintResolutionName, str)) {
00579       aPS->SetResolutionName(str.get());
00580       DUMP_STR(kReadStr, kPrintResolutionName, str.get());
00581     }
00582   }
00583 
00584   if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) {
00585     if (GETBOOLPREF(kPrintDownloadFonts, &b)) {
00586       aPS->SetDownloadFonts(b);
00587       DUMP_BOOL(kReadStr, kPrintDownloadFonts, b);
00588     }
00589   }
00590 
00591   if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
00592     if (GETINTPREF(kPrintOrientation, &iVal)) {
00593       aPS->SetOrientation(iVal);
00594       DUMP_INT(kReadStr, kPrintOrientation, iVal);
00595     }
00596   }
00597 
00598   if (aFlags & nsIPrintSettings::kInitSavePrintCommand) {
00599     if (GETSTRPREF(kPrintCommand, str)) {
00600       aPS->SetPrintCommand(str.get());
00601       DUMP_STR(kReadStr, kPrintCommand, str.get());
00602     }
00603   }
00604 
00605   if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
00606     if (GETBOOLPREF(kPrintToFile, &b)) {
00607       aPS->SetPrintToFile(b);
00608       DUMP_BOOL(kReadStr, kPrintToFile, b);
00609     }
00610   }
00611 
00612   if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
00613     if (GETSTRPREF(kPrintToFileName, str)) {
00614       aPS->SetToFileName(str.get());
00615       DUMP_STR(kReadStr, kPrintToFileName, str.get());
00616     }
00617   }
00618 
00619   if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
00620     if (GETINTPREF(kPrintPageDelay, &iVal)) {
00621       aPS->SetPrintPageDelay(iVal);
00622       DUMP_INT(kReadStr, kPrintPageDelay, iVal);
00623     }
00624   }
00625 
00626   if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
00627     if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
00628       aPS->SetShrinkToFit(b);
00629       DUMP_BOOL(kReadStr, kPrintShrinkToFit, b);
00630     }
00631   }
00632 
00633   if (aFlags & nsIPrintSettings::kInitSaveScaling) {
00634     if (GETDBLPREF(kPrintScaling, dbl)) {
00635       aPS->SetScaling(dbl);
00636       DUMP_DBL(kReadStr, kPrintScaling, dbl);
00637     }
00638   }
00639 
00640   // Not Reading In:
00641   //   Number of Copies
00642 
00643   return NS_OK;
00644 }
00645 
00650 nsresult 
00651 nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName,
00652                            PRUint32 aFlags)
00653 {
00654   NS_ENSURE_ARG_POINTER(aPS);
00655   NS_ENSURE_STATE(mPrefBranch);
00656 
00657   nsMargin margin;
00658   if (aFlags & nsIPrintSettings::kInitSaveMargins) {
00659     if (NS_SUCCEEDED(aPS->GetMarginInTwips(margin))) {
00660       WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName),
00661                                margin.top);
00662       DUMP_INT(kWriteStr, kMarginTop, margin.top);
00663       WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
00664                                margin.left);
00665       DUMP_INT(kWriteStr, kMarginLeft, margin.top);
00666       WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
00667                                margin.bottom);
00668       DUMP_INT(kWriteStr, kMarginBottom, margin.top);
00669       WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
00670                                margin.right);
00671       DUMP_INT(kWriteStr, kMarginRight, margin.top);
00672     }
00673   }
00674 
00675   PRBool     b;
00676   PRUnichar* uStr;
00677   PRInt32    iVal;
00678   PRInt16    iVal16;
00679   double     dbl;
00680 
00681   if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
00682     if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintEvenPages,
00683                                           &b))) {
00684           DUMP_BOOL(kWriteStr, kPrintEvenPages, b);
00685           mPrefBranch->SetBoolPref(GetPrefName(kPrintEvenPages, aPrinterName),
00686                                    b);
00687         }
00688   }
00689 
00690   if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
00691     if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintOddPages,
00692                                           &b))) {
00693           DUMP_BOOL(kWriteStr, kPrintOddPages, b);
00694           mPrefBranch->SetBoolPref(GetPrefName(kPrintOddPages, aPrinterName),
00695                                    b);
00696         }
00697   }
00698 
00699   if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
00700     if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(&uStr))) {
00701       DUMP_STR(kWriteStr, kPrintHeaderStrLeft, uStr);
00702       WritePrefString(uStr, GetPrefName(kPrintHeaderStrLeft, aPrinterName));
00703     }
00704   }
00705 
00706   if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
00707     if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(&uStr))) {
00708       DUMP_STR(kWriteStr, kPrintHeaderStrCenter, uStr);
00709       WritePrefString(uStr, GetPrefName(kPrintHeaderStrCenter, aPrinterName));
00710     }
00711   }
00712 
00713   if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
00714     if (NS_SUCCEEDED(aPS->GetHeaderStrRight(&uStr))) {
00715       DUMP_STR(kWriteStr, kPrintHeaderStrRight, uStr);
00716       WritePrefString(uStr, GetPrefName(kPrintHeaderStrRight, aPrinterName));
00717     }
00718   }
00719 
00720   if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
00721     if (NS_SUCCEEDED(aPS->GetFooterStrLeft(&uStr))) {
00722       DUMP_STR(kWriteStr, kPrintFooterStrLeft, uStr);
00723       WritePrefString(uStr, GetPrefName(kPrintFooterStrLeft, aPrinterName));
00724     }
00725   }
00726 
00727   if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
00728     if (NS_SUCCEEDED(aPS->GetFooterStrCenter(&uStr))) {
00729       DUMP_STR(kWriteStr, kPrintFooterStrCenter, uStr);
00730       WritePrefString(uStr, GetPrefName(kPrintFooterStrCenter, aPrinterName));
00731     }
00732   }
00733 
00734   if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
00735     if (NS_SUCCEEDED(aPS->GetFooterStrRight(&uStr))) {
00736       DUMP_STR(kWriteStr, kPrintFooterStrRight, uStr);
00737       WritePrefString(uStr, GetPrefName(kPrintFooterStrRight, aPrinterName));
00738     }
00739   }
00740 
00741   if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
00742     if (NS_SUCCEEDED(aPS->GetPrintBGColors(&b))) {
00743       DUMP_BOOL(kWriteStr, kPrintBGColors, b);
00744       mPrefBranch->SetBoolPref(GetPrefName(kPrintBGColors, aPrinterName), b);
00745     }
00746   }
00747 
00748   if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
00749     if (NS_SUCCEEDED(aPS->GetPrintBGImages(&b))) {
00750       DUMP_BOOL(kWriteStr, kPrintBGImages, b);
00751       mPrefBranch->SetBoolPref(GetPrefName(kPrintBGImages, aPrinterName), b);
00752     }
00753   }
00754 
00755   if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
00756     if (NS_SUCCEEDED(aPS->GetPaperSize(&iVal))) {
00757       DUMP_INT(kWriteStr, kPrintPaperSize, iVal);
00758       mPrefBranch->SetIntPref(GetPrefName(kPrintPaperSize, aPrinterName), iVal);
00759       // this has been deprecated
00760     }
00761   }
00762 
00763   if (aFlags & nsIPrintSettings::kInitSaveReversed) {
00764     if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
00765       DUMP_BOOL(kWriteStr, kPrintReversed, b);
00766       mPrefBranch->SetBoolPref(GetPrefName(kPrintReversed, aPrinterName), b);
00767     }
00768   }
00769 
00770   if (aFlags & nsIPrintSettings::kInitSaveInColor) {
00771     if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
00772       DUMP_BOOL(kWriteStr, kPrintInColor, b);
00773       mPrefBranch->SetBoolPref(GetPrefName(kPrintInColor, aPrinterName), b);
00774     }
00775   }
00776 
00777   if (aFlags & nsIPrintSettings::kInitSavePaperName) {
00778     if (NS_SUCCEEDED(aPS->GetPaperName(&uStr))) {
00779       DUMP_STR(kWriteStr, kPrintPaperName, uStr);
00780       WritePrefString(uStr, GetPrefName(kPrintPaperName, aPrinterName));
00781     }
00782   }
00783 
00784   if (aFlags & nsIPrintSettings::kInitSavePlexName) {
00785     if (NS_SUCCEEDED(aPS->GetPlexName(&uStr))) {
00786       DUMP_STR(kWriteStr, kPrintPlexName, uStr);
00787       WritePrefString(uStr, GetPrefName(kPrintPlexName, aPrinterName));
00788     }
00789   }
00790 
00791   if (aFlags & nsIPrintSettings::kInitSavePaperSizeUnit) {
00792     if (NS_SUCCEEDED(aPS->GetPaperSizeUnit(&iVal16))) {
00793       DUMP_INT(kWriteStr, kPrintPaperSizeUnit, iVal16);
00794       mPrefBranch->SetIntPref(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
00795                               PRInt32(iVal16));
00796     }
00797   }
00798  
00799   if (aFlags & nsIPrintSettings::kInitSavePaperSizeType) {
00800     if (NS_SUCCEEDED(aPS->GetPaperSizeType(&iVal16))) {
00801       DUMP_INT(kWriteStr, kPrintPaperSizeType, iVal16);
00802       mPrefBranch->SetIntPref(GetPrefName(kPrintPaperSizeType, aPrinterName),
00803                               PRInt32(iVal16));
00804     }
00805   }
00806 
00807   if (aFlags & nsIPrintSettings::kInitSavePaperData) {
00808     if (NS_SUCCEEDED(aPS->GetPaperData(&iVal16))) {
00809       DUMP_INT(kWriteStr, kPrintPaperData, iVal16);
00810       mPrefBranch->SetIntPref(GetPrefName(kPrintPaperData, aPrinterName),
00811                               PRInt32(iVal16));
00812     }
00813   }
00814 
00815   if (aFlags & nsIPrintSettings::kInitSavePaperWidth) {
00816     if (NS_SUCCEEDED(aPS->GetPaperWidth(&dbl))) {
00817       DUMP_DBL(kWriteStr, kPrintPaperWidth, dbl);
00818       WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), dbl);
00819     }
00820   }
00821 
00822   if (aFlags & nsIPrintSettings::kInitSavePaperHeight) {
00823     if (NS_SUCCEEDED(aPS->GetPaperHeight(&dbl))) {
00824       DUMP_DBL(kWriteStr, kPrintPaperHeight, dbl);
00825       WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), dbl);
00826     }
00827   }
00828 
00829   if (aFlags & nsIPrintSettings::kInitSaveColorspace) {
00830     if (NS_SUCCEEDED(aPS->GetColorspace(&uStr))) {
00831       DUMP_STR(kWriteStr, kPrintColorspace, uStr);
00832       WritePrefString(uStr, GetPrefName(kPrintColorspace, aPrinterName));
00833     }
00834   }
00835 
00836   if (aFlags & nsIPrintSettings::kInitSaveResolutionName) {
00837     if (NS_SUCCEEDED(aPS->GetResolutionName(&uStr))) {
00838       DUMP_STR(kWriteStr, kPrintResolutionName, uStr);
00839       WritePrefString(uStr, GetPrefName(kPrintResolutionName, aPrinterName));
00840     }
00841   }
00842 
00843   if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) {
00844     if (NS_SUCCEEDED(aPS->GetDownloadFonts(&b))) {
00845       DUMP_BOOL(kWriteStr, kPrintDownloadFonts, b);
00846       mPrefBranch->SetBoolPref(GetPrefName(kPrintDownloadFonts, aPrinterName),
00847                                b);
00848     }
00849   }
00850 
00851   if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
00852     if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
00853       DUMP_INT(kWriteStr, kPrintOrientation, iVal);
00854       mPrefBranch->SetIntPref(GetPrefName(kPrintOrientation, aPrinterName),
00855                               iVal);
00856     }
00857   }
00858 
00859   if (aFlags & nsIPrintSettings::kInitSavePrintCommand) {
00860     if (NS_SUCCEEDED(aPS->GetPrintCommand(&uStr))) {
00861       DUMP_STR(kWriteStr, kPrintCommand, uStr);
00862       WritePrefString(uStr, GetPrefName(kPrintCommand, aPrinterName));
00863     }
00864   }
00865 
00866   // Only the general version of this pref is saved
00867   if ((aFlags & nsIPrintSettings::kInitSavePrinterName)
00868       && aPrinterName.IsEmpty()) {
00869     if (NS_SUCCEEDED(aPS->GetPrinterName(&uStr))) {
00870       DUMP_STR(kWriteStr, kPrinterName, uStr);
00871       WritePrefString(uStr, kPrinterName);
00872     }
00873   }
00874 
00875   if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
00876     if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
00877       DUMP_BOOL(kWriteStr, kPrintToFile, b);
00878       mPrefBranch->SetBoolPref(GetPrefName(kPrintToFile, aPrinterName), b);
00879     }
00880   }
00881 
00882   if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
00883     if (NS_SUCCEEDED(aPS->GetToFileName(&uStr))) {
00884       DUMP_STR(kWriteStr, kPrintToFileName, uStr);
00885       WritePrefString(uStr, GetPrefName(kPrintToFileName, aPrinterName));
00886     }
00887   }
00888 
00889   if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
00890     if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
00891       DUMP_INT(kWriteStr, kPrintPageDelay, iVal);
00892       mPrefBranch->SetIntPref(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
00893     }
00894   }
00895 
00896   if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
00897     if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
00898       DUMP_BOOL(kWriteStr, kPrintShrinkToFit, b);
00899       mPrefBranch->SetBoolPref(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
00900     }
00901   }
00902 
00903   if (aFlags & nsIPrintSettings::kInitSaveScaling) {
00904     if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
00905       DUMP_DBL(kWriteStr, kPrintScaling, dbl);
00906       WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
00907     }
00908   }
00909 
00910   // Not Writing Out:
00911   //   Number of Copies
00912 
00913   return NS_OK;
00914 }
00915 
00916 /* create and return a new |nsPrinterListEnumerator| */
00917 NS_IMETHODIMP
00918 nsPrintOptions::AvailablePrinters(nsISimpleEnumerator **aPrinterEnumerator)
00919 {
00920   nsRefPtr<nsPrinterListEnumerator> printerListEnum =
00921       new nsPrinterListEnumerator();
00922   NS_ENSURE_TRUE(printerListEnum, NS_ERROR_OUT_OF_MEMORY);
00923 
00924   NS_ADDREF(*aPrinterEnumerator = printerListEnum.get());
00925 
00926   nsresult rv = printerListEnum->Init();
00927   if (NS_FAILED(rv))
00928     NS_RELEASE(*aPrinterEnumerator);
00929 
00930   return rv;
00931 }
00932 
00933 NS_IMETHODIMP
00934 nsPrintOptions::DisplayJobProperties(const PRUnichar *aPrinter,
00935                                      nsIPrintSettings* aPrintSettings,
00936                                      PRBool *aDisplayed)
00937 {
00938   NS_ENSURE_ARG_POINTER(aPrinter);
00939   *aDisplayed = PR_FALSE;
00940 
00941   nsresult rv;
00942   nsCOMPtr<nsIPrinterEnumerator> propDlg;
00943 
00944   propDlg = do_CreateInstance(kCPrinterEnumerator, &rv);
00945   NS_ENSURE_SUCCESS(rv, rv);
00946 
00947   NS_ENSURE_ARG_POINTER(aPrintSettings);
00948   rv = propDlg->DisplayPropertiesDlg(aPrinter, aPrintSettings);
00949   NS_ENSURE_SUCCESS(rv, rv);
00950 
00951   *aDisplayed = PR_TRUE;
00952 
00953   return rv;
00954 }
00955 
00956 NS_IMETHODIMP nsPrintOptions::GetNativeData(PRInt16 aDataType, void * *_retval)
00957 {
00958   return NS_ERROR_NOT_IMPLEMENTED;
00959 }
00960 
00961 nsresult nsPrintOptions::_CreatePrintSettings(nsIPrintSettings **_retval)
00962 {
00963   // does not initially ref count
00964   nsPrintSettings * printSettings = new nsPrintSettings();
00965   NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY);
00966 
00967   NS_ADDREF(*_retval = printSettings); // ref count
00968 
00969   nsXPIDLString printerName;
00970   nsresult rv = GetDefaultPrinterName(getter_Copies(printerName));
00971   NS_ENSURE_SUCCESS(rv, rv);
00972   (*_retval)->SetPrinterName(printerName.get());
00973 
00974   (void)InitPrintSettingsFromPrefs(*_retval, PR_FALSE,
00975                                    nsIPrintSettings::kInitSaveAll);
00976 
00977   return NS_OK;
00978 }
00979 
00980 NS_IMETHODIMP nsPrintOptions::CreatePrintSettings(nsIPrintSettings **_retval)
00981 {
00982   return _CreatePrintSettings(_retval);
00983 }
00984 
00985 NS_IMETHODIMP
00986 nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings **aGlobalPrintSettings)
00987 {
00988   nsresult rv;
00989 
00990   rv = CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings));
00991   NS_ENSURE_SUCCESS(rv, rv);
00992 
00993   NS_ADDREF(*aGlobalPrintSettings = mGlobalPrintSettings.get());
00994 
00995   return rv;
00996 }
00997 
00998 NS_IMETHODIMP
00999 nsPrintOptions::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings)
01000 {
01001   return CreatePrintSettings(aNewPrintSettings);
01002 }
01003 
01004 NS_IMETHODIMP
01005 nsPrintOptions::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName)
01006 {
01007   nsresult rv;
01008   nsCOMPtr<nsIPrinterEnumerator> prtEnum = do_GetService(kPrinterEnumeratorCID,
01009                                                          &rv);
01010   NS_ENSURE_SUCCESS(rv, rv);
01011 
01012   // Look up the printer from the last print job
01013   nsAutoString lastPrinterName;
01014   ReadPrefString(kPrinterName, lastPrinterName);
01015   if (!lastPrinterName.IsEmpty()) {
01016     // Verify it's still a valid printer
01017     PRUnichar **printers;
01018     PRUint32 ctPrinters;
01019     rv = prtEnum->EnumeratePrinters(&ctPrinters, &printers);
01020     if (NS_SUCCEEDED(rv)) {
01021       PRBool isValid = PR_FALSE;
01022       for (PRInt32 ii = ctPrinters - 1; ii >= 0; --ii) {
01023         if (lastPrinterName.Equals(printers[ii])) {
01024           isValid = PR_TRUE;
01025           break;
01026         }
01027       }
01028       NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(ctPrinters, printers);
01029       if (isValid) {
01030         *aDefaultPrinterName = ToNewUnicode(lastPrinterName);
01031         return NS_OK;
01032       }
01033     }
01034   }
01035 
01036   // There is no last printer preference, or it doesn't name a valid printer.
01037   // Return the default from the printer enumeration.
01038   return prtEnum->GetDefaultPrinterName(aDefaultPrinterName);
01039 }
01040 
01041 NS_IMETHODIMP
01042 nsPrintOptions::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName,
01043                                              nsIPrintSettings *aPrintSettings)
01044 {
01045   NS_ENSURE_ARG_POINTER(aPrintSettings);
01046   NS_ENSURE_ARG_POINTER(aPrinterName);
01047 
01048 #ifdef NS_DEBUG
01049   nsXPIDLString printerName;
01050   aPrintSettings->GetPrinterName(getter_Copies(printerName));
01051   if (!printerName.Equals(aPrinterName)) {
01052     NS_WARNING("Printer names should match!");
01053   }
01054 #endif
01055 
01056   PRBool isInitialized;
01057   aPrintSettings->GetIsInitializedFromPrinter(&isInitialized);
01058   if (isInitialized)
01059     return NS_OK;
01060 
01061   nsresult rv;
01062   nsCOMPtr<nsIPrinterEnumerator> prtEnum = do_GetService(kPrinterEnumeratorCID,
01063                                                          &rv);
01064   NS_ENSURE_SUCCESS(rv, rv);
01065 
01066   rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings);
01067   NS_ENSURE_SUCCESS(rv, rv);
01068 
01069   aPrintSettings->SetIsInitializedFromPrinter(PR_TRUE);
01070   return rv;
01071 }
01072 
01076 static nsresult 
01077 GetAdjustedPrinterName(nsIPrintSettings* aPS, PRBool aUsePNP,
01078                        nsAString& aPrinterName)
01079 {
01080   NS_ENSURE_ARG_POINTER(aPS);
01081 
01082   aPrinterName.Truncate();
01083   if (!aUsePNP)
01084     return NS_OK;
01085 
01086   // Get the Printer Name from the PrintSettings 
01087   // to use as a prefix for Pref Names
01088   PRUnichar* prtName = nsnull;
01089 
01090   nsresult rv = aPS->GetPrinterName(&prtName);
01091   NS_ENSURE_SUCCESS(rv, rv);
01092 
01093   aPrinterName = nsDependentString(prtName);
01094 
01095   // Convert any whitespaces, carriage returns or newlines to _
01096   // The below algorithm is supposedly faster than using iterators
01097   NS_NAMED_LITERAL_STRING(replSubstr, "_");
01098   const char* replaceStr = " \n\r";
01099 
01100   PRInt32 x;
01101   for (x=0; x < (PRInt32)strlen(replaceStr); x++) {
01102     PRUnichar uChar = replaceStr[x];
01103 
01104     PRInt32 i = 0;
01105     while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) {
01106       aPrinterName.Replace(i, 1, replSubstr);
01107       i++;
01108     }
01109   }
01110   return NS_OK;
01111 }
01112 
01113 NS_IMETHODIMP
01114 nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings *aPrintSettings,
01115                                   const PRUnichar *aPrefName, PRInt32 *_retval)
01116 {
01117   NS_ENSURE_STATE(mPrefBranch);
01118   NS_ENSURE_ARG_POINTER(aPrintSettings);
01119   NS_ENSURE_ARG_POINTER(aPrefName);
01120 
01121   nsAutoString prtName;
01122   // Get the Printer Name from the PrintSettings
01123   // to use as a prefix for Pref Names
01124   GetAdjustedPrinterName(aPrintSettings, PR_TRUE, prtName);
01125 
01126   const char* prefName =
01127     GetPrefName(NS_LossyConvertUCS2toASCII(aPrefName).get(), prtName);
01128 
01129   NS_ENSURE_TRUE(prefName, NS_ERROR_FAILURE);
01130 
01131   PRInt32 iVal;
01132   nsresult rv = mPrefBranch->GetIntPref(prefName, &iVal);
01133   NS_ENSURE_SUCCESS(rv, rv);
01134 
01135   *_retval = iVal;
01136   return rv;
01137 }
01138 
01139 NS_IMETHODIMP 
01140 nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
01141                                            PRBool aUsePNP, PRUint32 aFlags)
01142 {
01143   NS_ENSURE_ARG_POINTER(aPS);
01144 
01145   PRBool isInitialized;
01146   aPS->GetIsInitializedFromPrefs(&isInitialized);
01147 
01148   if (isInitialized)
01149     return NS_OK;
01150 
01151   nsAutoString prtName;
01152   // read any non printer specific prefs
01153   // with empty printer name
01154   nsresult rv = ReadPrefs(aPS, prtName, aFlags);
01155   NS_ENSURE_SUCCESS(rv, rv);
01156 
01157   // Get the Printer Name from the PrintSettings
01158   // to use as a prefix for Pref Names
01159   rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
01160   NS_ENSURE_SUCCESS(rv, rv);
01161  
01162   if (prtName.IsEmpty()) {
01163     NS_WARNING("Caller should supply a printer name.");
01164     return NS_OK;
01165   }
01166 
01167   // Now read any printer specific prefs
01168   rv = ReadPrefs(aPS, prtName, aFlags);
01169   if (NS_SUCCEEDED(rv))
01170     aPS->SetIsInitializedFromPrefs(PR_TRUE);
01171 
01172   return NS_OK;
01173 }
01174 
01179 nsresult
01180 nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings *aPS,
01181                                          PRBool aUsePrinterNamePrefix,
01182                                          PRUint32 aFlags)
01183 {
01184   NS_ENSURE_ARG_POINTER(aPS);
01185   nsAutoString prtName;
01186 
01187   // Get the printer name from the PrinterSettings for an optional prefix.
01188   nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName);
01189   NS_ENSURE_SUCCESS(rv, rv);
01190 
01191   // Write the prefs, with or without a printer name prefix.
01192   return WritePrefs(aPS, prtName, aFlags);
01193 }
01194 
01195 
01196 //-----------------------------------------------------
01197 //-- Protected Methods --------------------------------
01198 //-----------------------------------------------------
01199 nsresult
01200 nsPrintOptions::ReadPrefString(const char * aPrefId, nsAString& aString)
01201 {
01202   NS_ENSURE_STATE(mPrefBranch);
01203   NS_ENSURE_ARG_POINTER(aPrefId);
01204 
01205   nsXPIDLCString str;
01206   nsresult rv = mPrefBranch->GetCharPref(aPrefId, getter_Copies(str));
01207   NS_ENSURE_SUCCESS(rv, rv);
01208 
01209   CopyUTF8toUTF16(str.get(), aString);
01210 
01211   return rv;
01212 }
01213 
01214 nsresult
01215 nsPrintOptions::WritePrefString(PRUnichar*& aStr, const char* aPrefId)
01216 {
01217   NS_ENSURE_STATE(mPrefBranch);
01218   NS_ENSURE_ARG_POINTER(aStr);
01219   NS_ENSURE_ARG_POINTER(aPrefId);
01220 
01221   nsresult rv = mPrefBranch->SetCharPref(aPrefId,
01222                                          NS_ConvertUTF16toUTF8(aStr).get());
01223 
01224   nsMemory::Free(aStr);
01225   aStr = nsnull;
01226   return rv;
01227 }
01228 
01229 nsresult
01230 nsPrintOptions::WritePrefString(const char * aPrefId, const nsAString& aString)
01231 {
01232   NS_ENSURE_STATE(mPrefBranch);
01233   NS_ENSURE_ARG_POINTER(aPrefId);
01234 
01235   return mPrefBranch->SetCharPref(aPrefId,
01236                                   NS_ConvertUTF16toUTF8(aString).get());
01237 }
01238 
01239 nsresult
01240 nsPrintOptions::ReadPrefDouble(const char * aPrefId, double& aVal)
01241 {
01242   NS_ENSURE_STATE(mPrefBranch);
01243   NS_ENSURE_ARG_POINTER(aPrefId);
01244 
01245   char * str;
01246   nsresult rv = mPrefBranch->GetCharPref(aPrefId, &str);
01247   if (NS_SUCCEEDED(rv) && str) {
01248     aVal = atof(str);
01249     nsMemory::Free(str);
01250   }
01251   return rv;
01252 }
01253 
01254 nsresult
01255 nsPrintOptions::WritePrefDouble(const char * aPrefId, double aVal)
01256 {
01257   NS_ENSURE_STATE(mPrefBranch);
01258   NS_ENSURE_ARG_POINTER(aPrefId);
01259 
01260   char str[16]; // max 9 chars in below snprintf(), 16 will do nicely
01261   int ret = PR_snprintf(str, sizeof(str), "%6.2f", aVal);
01262   NS_ENSURE_TRUE(ret >= 0, NS_ERROR_FAILURE);
01263 
01264   return mPrefBranch->SetCharPref(aPrefId, str);
01265 }
01266 
01267 void
01268 nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId, nscoord& aTwips,
01269                                       const char * aMarginPref)
01270 {
01271   if (!mPrefBranch) {
01272     return;
01273   }
01274 
01275   char * str = nsnull;
01276   nsresult rv = mPrefBranch->GetCharPref(aPrefId, &str);
01277   if (NS_FAILED(rv) || !str)
01278     rv = mPrefBranch->GetCharPref(aMarginPref, &str);
01279   if (NS_SUCCEEDED(rv) && str) {
01280     nsAutoString justStr;
01281     justStr.AssignWithConversion(str);
01282     PRInt32 errCode;
01283     float inches = justStr.ToFloat(&errCode);
01284     if (NS_SUCCEEDED(errCode)) {
01285       aTwips = NS_INCHES_TO_TWIPS(inches);
01286     } else {
01287       aTwips = 0;
01288     }
01289     nsMemory::Free(str);
01290   }
01291 }
01292 
01293 void
01294 nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId, nscoord aTwips)
01295 {
01296   if (!mPrefBranch) {
01297     return;
01298   }
01299 
01300   double inches = NS_TWIPS_TO_INCHES(aTwips);
01301   nsCAutoString inchesStr;
01302   inchesStr.AppendFloat(inches);
01303 
01304   mPrefBranch->SetCharPref(aPrefId, inchesStr.get());
01305 }
01306 
01307 void
01308 nsPrintOptions::ReadJustification(const char * aPrefId, PRInt16& aJust,
01309                                   PRInt16 aInitValue)
01310 {
01311   aJust = aInitValue;
01312   nsAutoString justStr;
01313   if (NS_SUCCEEDED(ReadPrefString(aPrefId, justStr))) {
01314     if (justStr.EqualsASCII(kJustRight)) {
01315       aJust = nsIPrintSettings::kJustRight;
01316 
01317     } else if (justStr.EqualsASCII(kJustCenter)) {
01318       aJust = nsIPrintSettings::kJustCenter;
01319 
01320     } else {
01321       aJust = nsIPrintSettings::kJustLeft;
01322     }
01323   }
01324 }
01325 
01326 //---------------------------------------------------
01327 void
01328 nsPrintOptions::WriteJustification(const char * aPrefId, PRInt16 aJust)
01329 {
01330   switch (aJust) {
01331     case nsIPrintSettings::kJustLeft:
01332       mPrefBranch->SetCharPref(aPrefId, kJustLeft);
01333       break;
01334 
01335     case nsIPrintSettings::kJustCenter:
01336       mPrefBranch->SetCharPref(aPrefId, kJustCenter);
01337       break;
01338 
01339     case nsIPrintSettings::kJustRight:
01340       mPrefBranch->SetCharPref(aPrefId, kJustRight);
01341       break;
01342   } //switch
01343 }
01344 
01345 //----------------------------------------------------------------------
01346 // Testing of read/write prefs
01347 // This define turns on the testing module below
01348 // so at start up it writes and reads the prefs.
01349 #ifdef DEBUG_rods_X
01350 class Tester {
01351   public:
01352     Tester();
01353 };
01354 Tester::Tester()
01355 {
01356   nsCOMPtr<nsIPrintSettings> ps;
01357   nsresult rv;
01358   nsCOMPtr<nsIPrintOptions> printService =
01359       do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv);
01360   if (NS_SUCCEEDED(rv)) {
01361     rv = printService->CreatePrintSettings(getter_AddRefs(ps));
01362   }
01363 
01364   if (ps) {
01365     ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages,  PR_TRUE);
01366     ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages,  PR_FALSE);
01367     ps->SetMarginTop(1.0);
01368     ps->SetMarginLeft(1.0);
01369     ps->SetMarginBottom(1.0);
01370     ps->SetMarginRight(1.0);
01371     ps->SetScaling(0.5);
01372     ps->SetPrintBGColors(PR_TRUE);
01373     ps->SetPrintBGImages(PR_TRUE);
01374     ps->SetPrintRange(15);
01375     ps->SetHeaderStrLeft(NS_ConvertUTF8toUCS2("Left").get());
01376     ps->SetHeaderStrCenter(NS_ConvertUTF8toUCS2("Center").get());
01377     ps->SetHeaderStrRight(NS_ConvertUTF8toUCS2("Right").get());
01378     ps->SetFooterStrLeft(NS_ConvertUTF8toUCS2("Left").get());
01379     ps->SetFooterStrCenter(NS_ConvertUTF8toUCS2("Center").get());
01380     ps->SetFooterStrRight(NS_ConvertUTF8toUCS2("Right").get());
01381     ps->SetPaperName(NS_ConvertUTF8toUCS2("Paper Name").get());
01382     ps->SetPlexName(NS_ConvertUTF8toUCS2("Plex Name").get());
01383     ps->SetPaperSizeType(10);
01384     ps->SetPaperData(1);
01385     ps->SetPaperWidth(100.0);
01386     ps->SetPaperHeight(50.0);
01387     ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters);
01388     ps->SetPrintReversed(PR_TRUE);
01389     ps->SetPrintInColor(PR_TRUE);
01390     ps->SetPaperSize(5);
01391     ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
01392     ps->SetPrintCommand(NS_ConvertUTF8toUCS2("Command").get());
01393     ps->SetNumCopies(2);
01394     ps->SetPrinterName(NS_ConvertUTF8toUCS2("Printer Name").get());
01395     ps->SetPrintToFile(PR_TRUE);
01396     ps->SetToFileName(NS_ConvertUTF8toUCS2("File Name").get());
01397     ps->SetPrintPageDelay(1000);
01398     ps->SetShrinkToFit(PR_TRUE);
01399 
01400     struct SettingsType {
01401       const char* mName;
01402       PRUint32    mFlag;
01403     };
01404     SettingsType gSettings[] = {
01405       {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages},
01406       {kPrintHeaderStrLeft, nsIPrintSettings::kInitSaveHeaderLeft},
01407       {kPrintHeaderStrCenter, nsIPrintSettings::kInitSaveHeaderCenter},
01408       {kPrintHeaderStrRight, nsIPrintSettings::kInitSaveHeaderRight},
01409       {kPrintFooterStrLeft, nsIPrintSettings::kInitSaveFooterLeft},
01410       {kPrintFooterStrCenter, nsIPrintSettings::kInitSaveFooterCenter},
01411       {kPrintFooterStrRight, nsIPrintSettings::kInitSaveFooterRight},
01412       {kPrintBGColors, nsIPrintSettings::kInitSaveBGColors},
01413       {kPrintBGImages, nsIPrintSettings::kInitSaveBGImages},
01414       {kPrintShrinkToFit, nsIPrintSettings::kInitSaveShrinkToFit},
01415       {kPrintPaperSize, nsIPrintSettings::kInitSavePaperSize},
01416       {kPrintPaperName, nsIPrintSettings::kInitSavePaperName},
01417       {kPrintPlexName, nsIPrintSettings::kInitSavePlexName},
01418       {kPrintPaperSizeUnit, nsIPrintSettings::kInitSavePaperSizeUnit},
01419       {kPrintPaperSizeType, nsIPrintSettings::kInitSavePaperSizeType},
01420       {kPrintPaperData, nsIPrintSettings::kInitSavePaperData},
01421       {kPrintPaperWidth, nsIPrintSettings::kInitSavePaperWidth},
01422       {kPrintPaperHeight, nsIPrintSettings::kInitSavePaperHeight},
01423       {kPrintReversed, nsIPrintSettings::kInitSaveReversed},
01424       {kPrintInColor, nsIPrintSettings::kInitSaveInColor},
01425       {kPrintColorspace, nsIPrintSettings::kInitSaveColorspace},
01426       {kPrintResolutionName, nsIPrintSettings::kInitSaveResolutionName},
01427       {kPrintDownloadFonts, nsIPrintSettings::kInitSaveDownloadFonts},
01428       {kPrintOrientation, nsIPrintSettings::kInitSaveOrientation},
01429       {kPrintCommand, nsIPrintSettings::kInitSavePrintCommand},
01430       {kPrinterName, nsIPrintSettings::kInitSavePrinterName},
01431       {kPrintToFile, nsIPrintSettings::kInitSavePrintToFile},
01432       {kPrintToFileName, nsIPrintSettings::kInitSaveToFileName},
01433       {kPrintPageDelay, nsIPrintSettings::kInitSavePageDelay},
01434       {"Margins", nsIPrintSettings::kInitSaveMargins},
01435       {"All", nsIPrintSettings::kInitSaveAll},
01436       {nsnull, 0}};
01437 
01438       nsString prefix; prefix.AssignLiteral("Printer Name");
01439       PRInt32 i = 0;
01440       while (gSettings[i].mName != nsnull) {
01441         printf("------------------------------------------------\n");
01442         printf("%d) %s -> 0x%X\n", i, gSettings[i].mName, gSettings[i].mFlag);
01443         printService->SavePrintSettingsToPrefs(ps, PR_TRUE, gSettings[i].mFlag);
01444         printService->InitPrintSettingsFromPrefs(ps, PR_TRUE,
01445                                                  gSettings[i].mFlag);
01446         i++;
01447       }
01448   }
01449 
01450 }
01451 Tester gTester;
01452 #endif