1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/xpwidgets/nsPrintOptionsImpl.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1314 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsPrintOptionsImpl.h" 1.10 +#include "nsReadableUtils.h" 1.11 +#include "nsPrintSettingsImpl.h" 1.12 + 1.13 +#include "nsIDOMWindow.h" 1.14 +#include "nsIServiceManager.h" 1.15 +#include "nsIDialogParamBlock.h" 1.16 +#include "nsXPCOM.h" 1.17 +#include "nsISupportsPrimitives.h" 1.18 +#include "nsIWindowWatcher.h" 1.19 +#include "nsISupportsArray.h" 1.20 +#include "prprf.h" 1.21 + 1.22 +#include "nsIStringEnumerator.h" 1.23 +#include "nsISupportsPrimitives.h" 1.24 +#include "stdlib.h" 1.25 +#include "nsAutoPtr.h" 1.26 +#include "mozilla/Preferences.h" 1.27 +#include "nsPrintfCString.h" 1.28 + 1.29 +using namespace mozilla; 1.30 + 1.31 +NS_IMPL_ISUPPORTS(nsPrintOptions, nsIPrintOptions, nsIPrintSettingsService) 1.32 + 1.33 +// Pref Constants 1.34 +static const char kMarginTop[] = "print_margin_top"; 1.35 +static const char kMarginLeft[] = "print_margin_left"; 1.36 +static const char kMarginBottom[] = "print_margin_bottom"; 1.37 +static const char kMarginRight[] = "print_margin_right"; 1.38 +static const char kEdgeTop[] = "print_edge_top"; 1.39 +static const char kEdgeLeft[] = "print_edge_left"; 1.40 +static const char kEdgeBottom[] = "print_edge_bottom"; 1.41 +static const char kEdgeRight[] = "print_edge_right"; 1.42 +static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top"; 1.43 +static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left"; 1.44 +static const char kUnwriteableMarginBottom[] = "print_unwriteable_margin_bottom"; 1.45 +static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right"; 1.46 + 1.47 +// Prefs for Print Options 1.48 +static const char kPrintEvenPages[] = "print_evenpages"; 1.49 +static const char kPrintOddPages[] = "print_oddpages"; 1.50 +static const char kPrintHeaderStrLeft[] = "print_headerleft"; 1.51 +static const char kPrintHeaderStrCenter[] = "print_headercenter"; 1.52 +static const char kPrintHeaderStrRight[] = "print_headerright"; 1.53 +static const char kPrintFooterStrLeft[] = "print_footerleft"; 1.54 +static const char kPrintFooterStrCenter[] = "print_footercenter"; 1.55 +static const char kPrintFooterStrRight[] = "print_footerright"; 1.56 + 1.57 +// Additional Prefs 1.58 +static const char kPrintReversed[] = "print_reversed"; 1.59 +static const char kPrintInColor[] = "print_in_color"; 1.60 +static const char kPrintPaperName[] = "print_paper_name"; 1.61 +static const char kPrintPlexName[] = "print_plex_name"; 1.62 +static const char kPrintPaperSizeType[] = "print_paper_size_type"; 1.63 +static const char kPrintPaperData[] = "print_paper_data"; 1.64 +static const char kPrintPaperSizeUnit[] = "print_paper_size_unit"; 1.65 +static const char kPrintPaperWidth[] = "print_paper_width"; 1.66 +static const char kPrintPaperHeight[] = "print_paper_height"; 1.67 +static const char kPrintColorspace[] = "print_colorspace"; 1.68 +static const char kPrintResolutionName[]= "print_resolution_name"; 1.69 +static const char kPrintDownloadFonts[] = "print_downloadfonts"; 1.70 +static const char kPrintOrientation[] = "print_orientation"; 1.71 +static const char kPrintCommand[] = "print_command"; 1.72 +static const char kPrinterName[] = "print_printer"; 1.73 +static const char kPrintToFile[] = "print_to_file"; 1.74 +static const char kPrintToFileName[] = "print_to_filename"; 1.75 +static const char kPrintPageDelay[] = "print_page_delay"; 1.76 +static const char kPrintBGColors[] = "print_bgcolor"; 1.77 +static const char kPrintBGImages[] = "print_bgimages"; 1.78 +static const char kPrintShrinkToFit[] = "print_shrink_to_fit"; 1.79 +static const char kPrintScaling[] = "print_scaling"; 1.80 +static const char kPrintResolution[] = "print_resolution"; 1.81 +static const char kPrintDuplex[] = "print_duplex"; 1.82 + 1.83 +static const char kJustLeft[] = "left"; 1.84 +static const char kJustCenter[] = "center"; 1.85 +static const char kJustRight[] = "right"; 1.86 + 1.87 +#define NS_PRINTER_ENUMERATOR_CONTRACTID "@mozilla.org/gfx/printerenumerator;1" 1.88 + 1.89 +nsPrintOptions::nsPrintOptions() 1.90 +{ 1.91 +} 1.92 + 1.93 +nsPrintOptions::~nsPrintOptions() 1.94 +{ 1.95 +} 1.96 + 1.97 +nsresult 1.98 +nsPrintOptions::Init() 1.99 +{ 1.100 + return NS_OK; 1.101 +} 1.102 + 1.103 +NS_IMETHODIMP 1.104 +nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings *aPS) 1.105 +{ 1.106 + NS_ENSURE_ARG_POINTER(aPS); 1.107 + nsresult rv; 1.108 + 1.109 + // create a nsISupportsArray of the parameters 1.110 + // being passed to the window 1.111 + nsCOMPtr<nsISupportsArray> array; 1.112 + rv = NS_NewISupportsArray(getter_AddRefs(array)); 1.113 + NS_ENSURE_SUCCESS(rv, rv); 1.114 + 1.115 + nsCOMPtr<nsISupports> psSupports = do_QueryInterface(aPS); 1.116 + NS_ASSERTION(psSupports, "PrintSettings must be a supports"); 1.117 + array->AppendElement(psSupports); 1.118 + 1.119 + nsCOMPtr<nsIDialogParamBlock> ioParamBlock = 1.120 + do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &rv); 1.121 + NS_ENSURE_SUCCESS(rv, rv); 1.122 + 1.123 + ioParamBlock->SetInt(0, 0); 1.124 + 1.125 + nsCOMPtr<nsISupports> blkSupps = do_QueryInterface(ioParamBlock); 1.126 + NS_ASSERTION(blkSupps, "IOBlk must be a supports"); 1.127 + array->AppendElement(blkSupps); 1.128 + 1.129 + nsCOMPtr<nsIWindowWatcher> wwatch = 1.130 + do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); 1.131 + NS_ENSURE_SUCCESS(rv, rv); 1.132 + 1.133 + nsCOMPtr<nsIDOMWindow> parent; 1.134 + wwatch->GetActiveWindow(getter_AddRefs(parent)); 1.135 + // null |parent| is non-fatal 1.136 + 1.137 + nsCOMPtr<nsIDOMWindow> newWindow; 1.138 + 1.139 + return wwatch->OpenWindow(parent, 1.140 + "chrome://global/content/printPageSetup.xul", 1.141 + "_blank","chrome,modal,centerscreen", array, 1.142 + getter_AddRefs(newWindow)); 1.143 +} 1.144 + 1.145 +/** --------------------------------------------------- 1.146 + * Helper function - Creates the "prefix" for the pref 1.147 + * It is either "print." 1.148 + * or "print.printer_<print name>." 1.149 + */ 1.150 +const char* 1.151 +nsPrintOptions::GetPrefName(const char * aPrefName, 1.152 + const nsAString& aPrinterName) 1.153 +{ 1.154 + if (!aPrefName || !*aPrefName) { 1.155 + NS_ERROR("Must have a valid pref name!"); 1.156 + return aPrefName; 1.157 + } 1.158 + 1.159 + mPrefName.AssignLiteral("print."); 1.160 + 1.161 + if (aPrinterName.Length()) { 1.162 + mPrefName.AppendLiteral("printer_"); 1.163 + AppendUTF16toUTF8(aPrinterName, mPrefName); 1.164 + mPrefName.AppendLiteral("."); 1.165 + } 1.166 + mPrefName += aPrefName; 1.167 + 1.168 + return mPrefName.get(); 1.169 +} 1.170 + 1.171 +//---------------------------------------------------------------------- 1.172 +// Testing of read/write prefs 1.173 +// This define controls debug output 1.174 +#ifdef DEBUG_rods_X 1.175 +static void WriteDebugStr(const char* aArg1, const char* aArg2, 1.176 + const char16_t* aStr) 1.177 +{ 1.178 + nsString str(aStr); 1.179 + char16_t s = '&'; 1.180 + char16_t r = '_'; 1.181 + str.ReplaceChar(s, r); 1.182 + 1.183 + printf("%s %s = %s \n", aArg1, aArg2, ToNewUTF8String(str)); 1.184 +} 1.185 +const char* kWriteStr = "Write Pref:"; 1.186 +const char* kReadStr = "Read Pref:"; 1.187 +#define DUMP_STR(_a1, _a2, _a3) WriteDebugStr((_a1), GetPrefName((_a2), \ 1.188 +aPrefName), (_a3)); 1.189 +#define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \ 1.190 +GetPrefName((_a2), aPrefName), (_a3)?"T":"F"); 1.191 +#define DUMP_INT(_a1, _a2, _a3) printf("%s %s = %d \n", (_a1), \ 1.192 +GetPrefName((_a2), aPrefName), (_a3)); 1.193 +#define DUMP_DBL(_a1, _a2, _a3) printf("%s %s = %10.5f \n", (_a1), \ 1.194 +GetPrefName((_a2), aPrefName), (_a3)); 1.195 +#else 1.196 +#define DUMP_STR(_a1, _a2, _a3) 1.197 +#define DUMP_BOOL(_a1, _a2, _a3) 1.198 +#define DUMP_INT(_a1, _a2, _a3) 1.199 +#define DUMP_DBL(_a1, _a2, _a3) 1.200 +#endif /* DEBUG_rods_X */ 1.201 +//---------------------------------------------------------------------- 1.202 + 1.203 +/** 1.204 + * This will either read in the generic prefs (not specific to a printer) 1.205 + * or read the prefs in using the printer name to qualify. 1.206 + * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name" 1.207 + */ 1.208 +nsresult 1.209 +nsPrintOptions::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName, 1.210 + uint32_t aFlags) 1.211 +{ 1.212 + NS_ENSURE_ARG_POINTER(aPS); 1.213 + 1.214 + if (aFlags & nsIPrintSettings::kInitSaveMargins) { 1.215 + int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5); 1.216 + nsIntMargin margin(halfInch, halfInch, halfInch, halfInch); 1.217 + ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top, 1.218 + kMarginTop); 1.219 + DUMP_INT(kReadStr, kMarginTop, margin.top); 1.220 + ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left, 1.221 + kMarginLeft); 1.222 + DUMP_INT(kReadStr, kMarginLeft, margin.left); 1.223 + ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName), 1.224 + margin.bottom, kMarginBottom); 1.225 + DUMP_INT(kReadStr, kMarginBottom, margin.bottom); 1.226 + ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right, 1.227 + kMarginRight); 1.228 + DUMP_INT(kReadStr, kMarginRight, margin.right); 1.229 + aPS->SetMarginInTwips(margin); 1.230 + } 1.231 + 1.232 + if (aFlags & nsIPrintSettings::kInitSaveEdges) { 1.233 + nsIntMargin margin(0,0,0,0); 1.234 + ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top, 1.235 + kEdgeTop); 1.236 + DUMP_INT(kReadStr, kEdgeTop, margin.top); 1.237 + ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left, 1.238 + kEdgeLeft); 1.239 + DUMP_INT(kReadStr, kEdgeLeft, margin.left); 1.240 + ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName), 1.241 + margin.bottom, kEdgeBottom); 1.242 + DUMP_INT(kReadStr, kEdgeBottom, margin.bottom); 1.243 + ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName), margin.right, 1.244 + kEdgeRight); 1.245 + DUMP_INT(kReadStr, kEdgeRight, margin.right); 1.246 + aPS->SetEdgeInTwips(margin); 1.247 + } 1.248 + 1.249 + if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) { 1.250 + nsIntMargin margin; 1.251 + ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top, 1.252 + kUnwriteableMarginTop); 1.253 + DUMP_INT(kReadStr, kUnwriteableMarginTop, margin.top); 1.254 + ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left, 1.255 + kUnwriteableMarginLeft); 1.256 + DUMP_INT(kReadStr, kUnwriteableMarginLeft, margin.left); 1.257 + ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName), 1.258 + margin.bottom, kUnwriteableMarginBottom); 1.259 + DUMP_INT(kReadStr, kUnwriteableMarginBottom, margin.bottom); 1.260 + ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right, 1.261 + kUnwriteableMarginRight); 1.262 + DUMP_INT(kReadStr, kUnwriteableMarginRight, margin.right); 1.263 + aPS->SetUnwriteableMarginInTwips(margin); 1.264 + } 1.265 + 1.266 + bool b; 1.267 + nsAutoString str; 1.268 + int32_t iVal; 1.269 + double dbl; 1.270 + 1.271 +#define GETBOOLPREF(_prefname, _retval) \ 1.272 + NS_SUCCEEDED( \ 1.273 + Preferences::GetBool( \ 1.274 + GetPrefName(_prefname, aPrinterName), _retval \ 1.275 + ) \ 1.276 + ) 1.277 + 1.278 +#define GETSTRPREF(_prefname, _retval) \ 1.279 + NS_SUCCEEDED( \ 1.280 + Preferences::GetString( \ 1.281 + GetPrefName(_prefname, aPrinterName), _retval \ 1.282 + ) \ 1.283 + ) 1.284 + 1.285 +#define GETINTPREF(_prefname, _retval) \ 1.286 + NS_SUCCEEDED( \ 1.287 + Preferences::GetInt( \ 1.288 + GetPrefName(_prefname, aPrinterName), _retval \ 1.289 + ) \ 1.290 + ) 1.291 + 1.292 +#define GETDBLPREF(_prefname, _retval) \ 1.293 + NS_SUCCEEDED( \ 1.294 + ReadPrefDouble( \ 1.295 + GetPrefName(_prefname, aPrinterName), _retval \ 1.296 + ) \ 1.297 + ) 1.298 + 1.299 + // Paper size prefs are read as a group 1.300 + if (aFlags & nsIPrintSettings::kInitSavePaperSize) { 1.301 + int32_t sizeUnit, sizeType; 1.302 + double width, height; 1.303 + 1.304 + bool success = GETINTPREF(kPrintPaperSizeUnit, &sizeUnit) 1.305 + && GETINTPREF(kPrintPaperSizeType, &sizeType) 1.306 + && GETDBLPREF(kPrintPaperWidth, width) 1.307 + && GETDBLPREF(kPrintPaperHeight, height) 1.308 + && GETSTRPREF(kPrintPaperName, &str); 1.309 + 1.310 + // Bug 315687: Sanity check paper size to avoid paper size values in 1.311 + // mm when the size unit flag is inches. The value 100 is arbitrary 1.312 + // and can be changed. 1.313 + if (success) { 1.314 + success = (sizeUnit != nsIPrintSettings::kPaperSizeInches) 1.315 + || (width < 100.0) 1.316 + || (height < 100.0); 1.317 + } 1.318 + 1.319 + if (success) { 1.320 + aPS->SetPaperSizeUnit(sizeUnit); 1.321 + DUMP_INT(kReadStr, kPrintPaperSizeUnit, sizeUnit); 1.322 + aPS->SetPaperSizeType(sizeType); 1.323 + DUMP_INT(kReadStr, kPrintPaperSizeType, sizeType); 1.324 + aPS->SetPaperWidth(width); 1.325 + DUMP_DBL(kReadStr, kPrintPaperWidth, width); 1.326 + aPS->SetPaperHeight(height); 1.327 + DUMP_DBL(kReadStr, kPrintPaperHeight, height); 1.328 + aPS->SetPaperName(str.get()); 1.329 + DUMP_STR(kReadStr, kPrintPaperName, str.get()); 1.330 + } 1.331 + } 1.332 + 1.333 + if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { 1.334 + if (GETBOOLPREF(kPrintEvenPages, &b)) { 1.335 + aPS->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, b); 1.336 + DUMP_BOOL(kReadStr, kPrintEvenPages, b); 1.337 + } 1.338 + } 1.339 + 1.340 + if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { 1.341 + if (GETBOOLPREF(kPrintOddPages, &b)) { 1.342 + aPS->SetPrintOptions(nsIPrintSettings::kPrintOddPages, b); 1.343 + DUMP_BOOL(kReadStr, kPrintOddPages, b); 1.344 + } 1.345 + } 1.346 + 1.347 + if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { 1.348 + if (GETSTRPREF(kPrintHeaderStrLeft, &str)) { 1.349 + aPS->SetHeaderStrLeft(str.get()); 1.350 + DUMP_STR(kReadStr, kPrintHeaderStrLeft, str.get()); 1.351 + } 1.352 + } 1.353 + 1.354 + if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { 1.355 + if (GETSTRPREF(kPrintHeaderStrCenter, &str)) { 1.356 + aPS->SetHeaderStrCenter(str.get()); 1.357 + DUMP_STR(kReadStr, kPrintHeaderStrCenter, str.get()); 1.358 + } 1.359 + } 1.360 + 1.361 + if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { 1.362 + if (GETSTRPREF(kPrintHeaderStrRight, &str)) { 1.363 + aPS->SetHeaderStrRight(str.get()); 1.364 + DUMP_STR(kReadStr, kPrintHeaderStrRight, str.get()); 1.365 + } 1.366 + } 1.367 + 1.368 + if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { 1.369 + if (GETSTRPREF(kPrintFooterStrLeft, &str)) { 1.370 + aPS->SetFooterStrLeft(str.get()); 1.371 + DUMP_STR(kReadStr, kPrintFooterStrLeft, str.get()); 1.372 + } 1.373 + } 1.374 + 1.375 + if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { 1.376 + if (GETSTRPREF(kPrintFooterStrCenter, &str)) { 1.377 + aPS->SetFooterStrCenter(str.get()); 1.378 + DUMP_STR(kReadStr, kPrintFooterStrCenter, str.get()); 1.379 + } 1.380 + } 1.381 + 1.382 + if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { 1.383 + if (GETSTRPREF(kPrintFooterStrRight, &str)) { 1.384 + aPS->SetFooterStrRight(str.get()); 1.385 + DUMP_STR(kReadStr, kPrintFooterStrRight, str.get()); 1.386 + } 1.387 + } 1.388 + 1.389 + if (aFlags & nsIPrintSettings::kInitSaveBGColors) { 1.390 + if (GETBOOLPREF(kPrintBGColors, &b)) { 1.391 + aPS->SetPrintBGColors(b); 1.392 + DUMP_BOOL(kReadStr, kPrintBGColors, b); 1.393 + } 1.394 + } 1.395 + 1.396 + if (aFlags & nsIPrintSettings::kInitSaveBGImages) { 1.397 + if (GETBOOLPREF(kPrintBGImages, &b)) { 1.398 + aPS->SetPrintBGImages(b); 1.399 + DUMP_BOOL(kReadStr, kPrintBGImages, b); 1.400 + } 1.401 + } 1.402 + 1.403 + if (aFlags & nsIPrintSettings::kInitSaveReversed) { 1.404 + if (GETBOOLPREF(kPrintReversed, &b)) { 1.405 + aPS->SetPrintReversed(b); 1.406 + DUMP_BOOL(kReadStr, kPrintReversed, b); 1.407 + } 1.408 + } 1.409 + 1.410 + if (aFlags & nsIPrintSettings::kInitSaveInColor) { 1.411 + if (GETBOOLPREF(kPrintInColor, &b)) { 1.412 + aPS->SetPrintInColor(b); 1.413 + DUMP_BOOL(kReadStr, kPrintInColor, b); 1.414 + } 1.415 + } 1.416 + 1.417 + if (aFlags & nsIPrintSettings::kInitSavePlexName) { 1.418 + if (GETSTRPREF(kPrintPlexName, &str)) { 1.419 + aPS->SetPlexName(str.get()); 1.420 + DUMP_STR(kReadStr, kPrintPlexName, str.get()); 1.421 + } 1.422 + } 1.423 + 1.424 + if (aFlags & nsIPrintSettings::kInitSavePaperData) { 1.425 + if (GETINTPREF(kPrintPaperData, &iVal)) { 1.426 + aPS->SetPaperData(iVal); 1.427 + DUMP_INT(kReadStr, kPrintPaperData, iVal); 1.428 + } 1.429 + } 1.430 + 1.431 + if (aFlags & nsIPrintSettings::kInitSaveColorspace) { 1.432 + if (GETSTRPREF(kPrintColorspace, &str)) { 1.433 + aPS->SetColorspace(str.get()); 1.434 + DUMP_STR(kReadStr, kPrintColorspace, str.get()); 1.435 + } 1.436 + } 1.437 + 1.438 + if (aFlags & nsIPrintSettings::kInitSaveResolutionName) { 1.439 + if (GETSTRPREF(kPrintResolutionName, &str)) { 1.440 + aPS->SetResolutionName(str.get()); 1.441 + DUMP_STR(kReadStr, kPrintResolutionName, str.get()); 1.442 + } 1.443 + } 1.444 + 1.445 + if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) { 1.446 + if (GETBOOLPREF(kPrintDownloadFonts, &b)) { 1.447 + aPS->SetDownloadFonts(b); 1.448 + DUMP_BOOL(kReadStr, kPrintDownloadFonts, b); 1.449 + } 1.450 + } 1.451 + 1.452 + if (aFlags & nsIPrintSettings::kInitSaveOrientation) { 1.453 + if (GETINTPREF(kPrintOrientation, &iVal)) { 1.454 + aPS->SetOrientation(iVal); 1.455 + DUMP_INT(kReadStr, kPrintOrientation, iVal); 1.456 + } 1.457 + } 1.458 + 1.459 + if (aFlags & nsIPrintSettings::kInitSavePrintCommand) { 1.460 + if (GETSTRPREF(kPrintCommand, &str)) { 1.461 + aPS->SetPrintCommand(str.get()); 1.462 + DUMP_STR(kReadStr, kPrintCommand, str.get()); 1.463 + } 1.464 + } 1.465 + 1.466 + if (aFlags & nsIPrintSettings::kInitSavePrintToFile) { 1.467 + if (GETBOOLPREF(kPrintToFile, &b)) { 1.468 + aPS->SetPrintToFile(b); 1.469 + DUMP_BOOL(kReadStr, kPrintToFile, b); 1.470 + } 1.471 + } 1.472 + 1.473 + if (aFlags & nsIPrintSettings::kInitSaveToFileName) { 1.474 + if (GETSTRPREF(kPrintToFileName, &str)) { 1.475 + aPS->SetToFileName(str.get()); 1.476 + DUMP_STR(kReadStr, kPrintToFileName, str.get()); 1.477 + } 1.478 + } 1.479 + 1.480 + if (aFlags & nsIPrintSettings::kInitSavePageDelay) { 1.481 + if (GETINTPREF(kPrintPageDelay, &iVal)) { 1.482 + aPS->SetPrintPageDelay(iVal); 1.483 + DUMP_INT(kReadStr, kPrintPageDelay, iVal); 1.484 + } 1.485 + } 1.486 + 1.487 + if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) { 1.488 + if (GETBOOLPREF(kPrintShrinkToFit, &b)) { 1.489 + aPS->SetShrinkToFit(b); 1.490 + DUMP_BOOL(kReadStr, kPrintShrinkToFit, b); 1.491 + } 1.492 + } 1.493 + 1.494 + if (aFlags & nsIPrintSettings::kInitSaveScaling) { 1.495 + if (GETDBLPREF(kPrintScaling, dbl)) { 1.496 + aPS->SetScaling(dbl); 1.497 + DUMP_DBL(kReadStr, kPrintScaling, dbl); 1.498 + } 1.499 + } 1.500 + 1.501 + if (aFlags & nsIPrintSettings::kInitSaveResolution) { 1.502 + if (GETINTPREF(kPrintResolution, &iVal)) { 1.503 + aPS->SetResolution(iVal); 1.504 + DUMP_INT(kReadStr, kPrintResolution, iVal); 1.505 + } 1.506 + } 1.507 + 1.508 + if (aFlags & nsIPrintSettings::kInitSaveDuplex) { 1.509 + if (GETINTPREF(kPrintDuplex, &iVal)) { 1.510 + aPS->SetDuplex(iVal); 1.511 + DUMP_INT(kReadStr, kPrintDuplex, iVal); 1.512 + } 1.513 + } 1.514 + 1.515 + // Not Reading In: 1.516 + // Number of Copies 1.517 + 1.518 + return NS_OK; 1.519 +} 1.520 + 1.521 +/** --------------------------------------------------- 1.522 + * See documentation in nsPrintOptionsImpl.h 1.523 + * @update 1/12/01 rods 1.524 + */ 1.525 +nsresult 1.526 +nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, 1.527 + uint32_t aFlags) 1.528 +{ 1.529 + NS_ENSURE_ARG_POINTER(aPS); 1.530 + 1.531 + bool persistMarginBoxSettings; 1.532 + aPS->GetPersistMarginBoxSettings(&persistMarginBoxSettings); 1.533 + 1.534 + nsIntMargin margin; 1.535 + if (aFlags & nsIPrintSettings::kInitSaveMargins) { 1.536 + if (NS_SUCCEEDED(aPS->GetMarginInTwips(margin))) { 1.537 + WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), 1.538 + margin.top); 1.539 + DUMP_INT(kWriteStr, kMarginTop, margin.top); 1.540 + WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName), 1.541 + margin.left); 1.542 + DUMP_INT(kWriteStr, kMarginLeft, margin.top); 1.543 + WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName), 1.544 + margin.bottom); 1.545 + DUMP_INT(kWriteStr, kMarginBottom, margin.top); 1.546 + WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName), 1.547 + margin.right); 1.548 + DUMP_INT(kWriteStr, kMarginRight, margin.top); 1.549 + } 1.550 + } 1.551 + 1.552 + nsIntMargin edge; 1.553 + if (aFlags & nsIPrintSettings::kInitSaveEdges) { 1.554 + if (NS_SUCCEEDED(aPS->GetEdgeInTwips(edge))) { 1.555 + WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), 1.556 + edge.top); 1.557 + DUMP_INT(kWriteStr, kEdgeTop, edge.top); 1.558 + WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), 1.559 + edge.left); 1.560 + DUMP_INT(kWriteStr, kEdgeLeft, edge.top); 1.561 + WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName), 1.562 + edge.bottom); 1.563 + DUMP_INT(kWriteStr, kEdgeBottom, edge.top); 1.564 + WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName), 1.565 + edge.right); 1.566 + DUMP_INT(kWriteStr, kEdgeRight, edge.top); 1.567 + } 1.568 + } 1.569 + 1.570 + nsIntMargin unwriteableMargin; 1.571 + if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) { 1.572 + if (NS_SUCCEEDED(aPS->GetUnwriteableMarginInTwips(unwriteableMargin))) { 1.573 + WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName), 1.574 + unwriteableMargin.top); 1.575 + DUMP_INT(kWriteStr, kUnwriteableMarginTop, unwriteableMargin.top); 1.576 + WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName), 1.577 + unwriteableMargin.left); 1.578 + DUMP_INT(kWriteStr, kUnwriteableMarginLeft, unwriteableMargin.top); 1.579 + WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName), 1.580 + unwriteableMargin.bottom); 1.581 + DUMP_INT(kWriteStr, kUnwriteableMarginBottom, unwriteableMargin.top); 1.582 + WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName), 1.583 + unwriteableMargin.right); 1.584 + DUMP_INT(kWriteStr, kUnwriteableMarginRight, unwriteableMargin.top); 1.585 + } 1.586 + } 1.587 + 1.588 + // Paper size prefs are saved as a group 1.589 + if (aFlags & nsIPrintSettings::kInitSavePaperSize) { 1.590 + int16_t sizeUnit, sizeType; 1.591 + double width, height; 1.592 + char16_t *name; 1.593 + 1.594 + if ( 1.595 + NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) && 1.596 + NS_SUCCEEDED(aPS->GetPaperSizeType(&sizeType)) && 1.597 + NS_SUCCEEDED(aPS->GetPaperWidth(&width)) && 1.598 + NS_SUCCEEDED(aPS->GetPaperHeight(&height)) && 1.599 + NS_SUCCEEDED(aPS->GetPaperName(&name)) 1.600 + ) { 1.601 + DUMP_INT(kWriteStr, kPrintPaperSizeUnit, sizeUnit); 1.602 + Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName), 1.603 + int32_t(sizeUnit)); 1.604 + DUMP_INT(kWriteStr, kPrintPaperSizeType, sizeType); 1.605 + Preferences::SetInt(GetPrefName(kPrintPaperSizeType, aPrinterName), 1.606 + int32_t(sizeType)); 1.607 + DUMP_DBL(kWriteStr, kPrintPaperWidth, width); 1.608 + WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width); 1.609 + DUMP_DBL(kWriteStr, kPrintPaperHeight, height); 1.610 + WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height); 1.611 + DUMP_STR(kWriteStr, kPrintPaperName, name); 1.612 + Preferences::SetString(GetPrefName(kPrintPaperName, aPrinterName), name); 1.613 + } 1.614 + } 1.615 + 1.616 + bool b; 1.617 + char16_t* uStr; 1.618 + int32_t iVal; 1.619 + int16_t iVal16; 1.620 + double dbl; 1.621 + 1.622 + if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { 1.623 + if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintEvenPages, 1.624 + &b))) { 1.625 + DUMP_BOOL(kWriteStr, kPrintEvenPages, b); 1.626 + Preferences::SetBool(GetPrefName(kPrintEvenPages, aPrinterName), b); 1.627 + } 1.628 + } 1.629 + 1.630 + if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { 1.631 + if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintOddPages, 1.632 + &b))) { 1.633 + DUMP_BOOL(kWriteStr, kPrintOddPages, b); 1.634 + Preferences::SetBool(GetPrefName(kPrintOddPages, aPrinterName), b); 1.635 + } 1.636 + } 1.637 + 1.638 + if (persistMarginBoxSettings) { 1.639 + if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { 1.640 + if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(&uStr))) { 1.641 + DUMP_STR(kWriteStr, kPrintHeaderStrLeft, uStr); 1.642 + Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName), 1.643 + uStr); 1.644 + } 1.645 + } 1.646 + 1.647 + if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { 1.648 + if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(&uStr))) { 1.649 + DUMP_STR(kWriteStr, kPrintHeaderStrCenter, uStr); 1.650 + Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName), 1.651 + uStr); 1.652 + } 1.653 + } 1.654 + 1.655 + if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { 1.656 + if (NS_SUCCEEDED(aPS->GetHeaderStrRight(&uStr))) { 1.657 + DUMP_STR(kWriteStr, kPrintHeaderStrRight, uStr); 1.658 + Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName), 1.659 + uStr); 1.660 + } 1.661 + } 1.662 + 1.663 + if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { 1.664 + if (NS_SUCCEEDED(aPS->GetFooterStrLeft(&uStr))) { 1.665 + DUMP_STR(kWriteStr, kPrintFooterStrLeft, uStr); 1.666 + Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName), 1.667 + uStr); 1.668 + } 1.669 + } 1.670 + 1.671 + if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { 1.672 + if (NS_SUCCEEDED(aPS->GetFooterStrCenter(&uStr))) { 1.673 + DUMP_STR(kWriteStr, kPrintFooterStrCenter, uStr); 1.674 + Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName), 1.675 + uStr); 1.676 + } 1.677 + } 1.678 + 1.679 + if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { 1.680 + if (NS_SUCCEEDED(aPS->GetFooterStrRight(&uStr))) { 1.681 + DUMP_STR(kWriteStr, kPrintFooterStrRight, uStr); 1.682 + Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName), 1.683 + uStr); 1.684 + } 1.685 + } 1.686 + } 1.687 + 1.688 + if (aFlags & nsIPrintSettings::kInitSaveBGColors) { 1.689 + if (NS_SUCCEEDED(aPS->GetPrintBGColors(&b))) { 1.690 + DUMP_BOOL(kWriteStr, kPrintBGColors, b); 1.691 + Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b); 1.692 + } 1.693 + } 1.694 + 1.695 + if (aFlags & nsIPrintSettings::kInitSaveBGImages) { 1.696 + if (NS_SUCCEEDED(aPS->GetPrintBGImages(&b))) { 1.697 + DUMP_BOOL(kWriteStr, kPrintBGImages, b); 1.698 + Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b); 1.699 + } 1.700 + } 1.701 + 1.702 + if (aFlags & nsIPrintSettings::kInitSaveReversed) { 1.703 + if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) { 1.704 + DUMP_BOOL(kWriteStr, kPrintReversed, b); 1.705 + Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b); 1.706 + } 1.707 + } 1.708 + 1.709 + if (aFlags & nsIPrintSettings::kInitSaveInColor) { 1.710 + if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) { 1.711 + DUMP_BOOL(kWriteStr, kPrintInColor, b); 1.712 + Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b); 1.713 + } 1.714 + } 1.715 + 1.716 + if (aFlags & nsIPrintSettings::kInitSavePlexName) { 1.717 + if (NS_SUCCEEDED(aPS->GetPlexName(&uStr))) { 1.718 + DUMP_STR(kWriteStr, kPrintPlexName, uStr); 1.719 + Preferences::SetString(GetPrefName(kPrintPlexName, aPrinterName), uStr); 1.720 + } 1.721 + } 1.722 + 1.723 + if (aFlags & nsIPrintSettings::kInitSavePaperData) { 1.724 + if (NS_SUCCEEDED(aPS->GetPaperData(&iVal16))) { 1.725 + DUMP_INT(kWriteStr, kPrintPaperData, iVal16); 1.726 + Preferences::SetInt(GetPrefName(kPrintPaperData, aPrinterName), 1.727 + int32_t(iVal16)); 1.728 + } 1.729 + } 1.730 + 1.731 + if (aFlags & nsIPrintSettings::kInitSaveColorspace) { 1.732 + if (NS_SUCCEEDED(aPS->GetColorspace(&uStr))) { 1.733 + DUMP_STR(kWriteStr, kPrintColorspace, uStr); 1.734 + Preferences::SetString(GetPrefName(kPrintColorspace, aPrinterName), uStr); 1.735 + } 1.736 + } 1.737 + 1.738 + if (aFlags & nsIPrintSettings::kInitSaveResolutionName) { 1.739 + if (NS_SUCCEEDED(aPS->GetResolutionName(&uStr))) { 1.740 + DUMP_STR(kWriteStr, kPrintResolutionName, uStr); 1.741 + Preferences::SetString(GetPrefName(kPrintResolutionName, aPrinterName), 1.742 + uStr); 1.743 + } 1.744 + } 1.745 + 1.746 + if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) { 1.747 + if (NS_SUCCEEDED(aPS->GetDownloadFonts(&b))) { 1.748 + DUMP_BOOL(kWriteStr, kPrintDownloadFonts, b); 1.749 + Preferences::SetBool(GetPrefName(kPrintDownloadFonts, aPrinterName), b); 1.750 + } 1.751 + } 1.752 + 1.753 + if (aFlags & nsIPrintSettings::kInitSaveOrientation) { 1.754 + if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) { 1.755 + DUMP_INT(kWriteStr, kPrintOrientation, iVal); 1.756 + Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal); 1.757 + } 1.758 + } 1.759 + 1.760 + if (aFlags & nsIPrintSettings::kInitSavePrintCommand) { 1.761 + if (NS_SUCCEEDED(aPS->GetPrintCommand(&uStr))) { 1.762 + DUMP_STR(kWriteStr, kPrintCommand, uStr); 1.763 + Preferences::SetString(GetPrefName(kPrintCommand, aPrinterName), uStr); 1.764 + } 1.765 + } 1.766 + 1.767 + // Only the general version of this pref is saved 1.768 + if ((aFlags & nsIPrintSettings::kInitSavePrinterName) 1.769 + && aPrinterName.IsEmpty()) { 1.770 + if (NS_SUCCEEDED(aPS->GetPrinterName(&uStr))) { 1.771 + DUMP_STR(kWriteStr, kPrinterName, uStr); 1.772 + Preferences::SetString(kPrinterName, uStr); 1.773 + } 1.774 + } 1.775 + 1.776 + if (aFlags & nsIPrintSettings::kInitSavePrintToFile) { 1.777 + if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) { 1.778 + DUMP_BOOL(kWriteStr, kPrintToFile, b); 1.779 + Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b); 1.780 + } 1.781 + } 1.782 + 1.783 + if (aFlags & nsIPrintSettings::kInitSaveToFileName) { 1.784 + if (NS_SUCCEEDED(aPS->GetToFileName(&uStr))) { 1.785 + DUMP_STR(kWriteStr, kPrintToFileName, uStr); 1.786 + Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr); 1.787 + } 1.788 + } 1.789 + 1.790 + if (aFlags & nsIPrintSettings::kInitSavePageDelay) { 1.791 + if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) { 1.792 + DUMP_INT(kWriteStr, kPrintPageDelay, iVal); 1.793 + Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal); 1.794 + } 1.795 + } 1.796 + 1.797 + if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) { 1.798 + if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) { 1.799 + DUMP_BOOL(kWriteStr, kPrintShrinkToFit, b); 1.800 + Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b); 1.801 + } 1.802 + } 1.803 + 1.804 + if (aFlags & nsIPrintSettings::kInitSaveScaling) { 1.805 + if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) { 1.806 + DUMP_DBL(kWriteStr, kPrintScaling, dbl); 1.807 + WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl); 1.808 + } 1.809 + } 1.810 + 1.811 + if (aFlags & nsIPrintSettings::kInitSaveResolution) { 1.812 + if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) { 1.813 + DUMP_INT(kWriteStr, kPrintResolution, iVal); 1.814 + Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal); 1.815 + } 1.816 + } 1.817 + 1.818 + if (aFlags & nsIPrintSettings::kInitSaveDuplex) { 1.819 + if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) { 1.820 + DUMP_INT(kWriteStr, kPrintDuplex, iVal); 1.821 + Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal); 1.822 + } 1.823 + } 1.824 + 1.825 + // Not Writing Out: 1.826 + // Number of Copies 1.827 + 1.828 + return NS_OK; 1.829 +} 1.830 + 1.831 +NS_IMETHODIMP 1.832 +nsPrintOptions::DisplayJobProperties(const char16_t *aPrinter, 1.833 + nsIPrintSettings* aPrintSettings, 1.834 + bool *aDisplayed) 1.835 +{ 1.836 + NS_ENSURE_ARG_POINTER(aPrinter); 1.837 + *aDisplayed = false; 1.838 + 1.839 + nsresult rv; 1.840 + nsCOMPtr<nsIPrinterEnumerator> propDlg = 1.841 + do_CreateInstance(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); 1.842 + NS_ENSURE_SUCCESS(rv, rv); 1.843 + 1.844 + NS_ENSURE_ARG_POINTER(aPrintSettings); 1.845 + rv = propDlg->DisplayPropertiesDlg(aPrinter, aPrintSettings); 1.846 + NS_ENSURE_SUCCESS(rv, rv); 1.847 + 1.848 + *aDisplayed = true; 1.849 + 1.850 + return rv; 1.851 +} 1.852 + 1.853 +NS_IMETHODIMP nsPrintOptions::GetNativeData(int16_t aDataType, void * *_retval) 1.854 +{ 1.855 + return NS_ERROR_NOT_IMPLEMENTED; 1.856 +} 1.857 + 1.858 +nsresult nsPrintOptions::_CreatePrintSettings(nsIPrintSettings **_retval) 1.859 +{ 1.860 + // does not initially ref count 1.861 + nsPrintSettings * printSettings = new nsPrintSettings(); 1.862 + NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY); 1.863 + 1.864 + NS_ADDREF(*_retval = printSettings); // ref count 1.865 + 1.866 + nsXPIDLString printerName; 1.867 + nsresult rv = GetDefaultPrinterName(getter_Copies(printerName)); 1.868 + NS_ENSURE_SUCCESS(rv, rv); 1.869 + (*_retval)->SetPrinterName(printerName.get()); 1.870 + 1.871 + (void)InitPrintSettingsFromPrefs(*_retval, false, 1.872 + nsIPrintSettings::kInitSaveAll); 1.873 + 1.874 + return NS_OK; 1.875 +} 1.876 + 1.877 +NS_IMETHODIMP nsPrintOptions::CreatePrintSettings(nsIPrintSettings **_retval) 1.878 +{ 1.879 + return _CreatePrintSettings(_retval); 1.880 +} 1.881 + 1.882 +NS_IMETHODIMP 1.883 +nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings **aGlobalPrintSettings) 1.884 +{ 1.885 + nsresult rv; 1.886 + 1.887 + rv = CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings)); 1.888 + NS_ENSURE_SUCCESS(rv, rv); 1.889 + 1.890 + NS_ADDREF(*aGlobalPrintSettings = mGlobalPrintSettings.get()); 1.891 + 1.892 + return rv; 1.893 +} 1.894 + 1.895 +NS_IMETHODIMP 1.896 +nsPrintOptions::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings) 1.897 +{ 1.898 + return CreatePrintSettings(aNewPrintSettings); 1.899 +} 1.900 + 1.901 +NS_IMETHODIMP 1.902 +nsPrintOptions::GetDefaultPrinterName(char16_t * *aDefaultPrinterName) 1.903 +{ 1.904 + nsresult rv; 1.905 + nsCOMPtr<nsIPrinterEnumerator> prtEnum = 1.906 + do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); 1.907 + NS_ENSURE_SUCCESS(rv, rv); 1.908 + 1.909 + // Look up the printer from the last print job 1.910 + nsAutoString lastPrinterName; 1.911 + Preferences::GetString(kPrinterName, &lastPrinterName); 1.912 + if (!lastPrinterName.IsEmpty()) { 1.913 + // Verify it's still a valid printer 1.914 + nsCOMPtr<nsIStringEnumerator> printers; 1.915 + rv = prtEnum->GetPrinterNameList(getter_AddRefs(printers)); 1.916 + if (NS_SUCCEEDED(rv)) { 1.917 + bool isValid = false; 1.918 + bool hasMore; 1.919 + while (NS_SUCCEEDED(printers->HasMore(&hasMore)) && hasMore) { 1.920 + nsAutoString printer; 1.921 + if (NS_SUCCEEDED(printers->GetNext(printer)) && lastPrinterName.Equals(printer)) { 1.922 + isValid = true; 1.923 + break; 1.924 + } 1.925 + } 1.926 + if (isValid) { 1.927 + *aDefaultPrinterName = ToNewUnicode(lastPrinterName); 1.928 + return NS_OK; 1.929 + } 1.930 + } 1.931 + } 1.932 + 1.933 + // There is no last printer preference, or it doesn't name a valid printer. 1.934 + // Return the default from the printer enumeration. 1.935 + return prtEnum->GetDefaultPrinterName(aDefaultPrinterName); 1.936 +} 1.937 + 1.938 +NS_IMETHODIMP 1.939 +nsPrintOptions::InitPrintSettingsFromPrinter(const char16_t *aPrinterName, 1.940 + nsIPrintSettings *aPrintSettings) 1.941 +{ 1.942 + NS_ENSURE_ARG_POINTER(aPrintSettings); 1.943 + NS_ENSURE_ARG_POINTER(aPrinterName); 1.944 + 1.945 +#ifdef DEBUG 1.946 + nsXPIDLString printerName; 1.947 + aPrintSettings->GetPrinterName(getter_Copies(printerName)); 1.948 + if (!printerName.Equals(aPrinterName)) { 1.949 + NS_WARNING("Printer names should match!"); 1.950 + } 1.951 +#endif 1.952 + 1.953 + bool isInitialized; 1.954 + aPrintSettings->GetIsInitializedFromPrinter(&isInitialized); 1.955 + if (isInitialized) 1.956 + return NS_OK; 1.957 + 1.958 + nsresult rv; 1.959 + nsCOMPtr<nsIPrinterEnumerator> prtEnum = 1.960 + do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); 1.961 + NS_ENSURE_SUCCESS(rv, rv); 1.962 + 1.963 + rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings); 1.964 + NS_ENSURE_SUCCESS(rv, rv); 1.965 + 1.966 + aPrintSettings->SetIsInitializedFromPrinter(true); 1.967 + return rv; 1.968 +} 1.969 + 1.970 +/** --------------------------------------------------- 1.971 + * Helper function - Returns either the name or sets the length to zero 1.972 + */ 1.973 +static nsresult 1.974 +GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP, 1.975 + nsAString& aPrinterName) 1.976 +{ 1.977 + NS_ENSURE_ARG_POINTER(aPS); 1.978 + 1.979 + aPrinterName.Truncate(); 1.980 + if (!aUsePNP) 1.981 + return NS_OK; 1.982 + 1.983 + // Get the Printer Name from the PrintSettings 1.984 + // to use as a prefix for Pref Names 1.985 + char16_t* prtName = nullptr; 1.986 + 1.987 + nsresult rv = aPS->GetPrinterName(&prtName); 1.988 + NS_ENSURE_SUCCESS(rv, rv); 1.989 + 1.990 + aPrinterName = nsDependentString(prtName); 1.991 + 1.992 + // Convert any whitespaces, carriage returns or newlines to _ 1.993 + // The below algorithm is supposedly faster than using iterators 1.994 + NS_NAMED_LITERAL_STRING(replSubstr, "_"); 1.995 + const char* replaceStr = " \n\r"; 1.996 + 1.997 + int32_t x; 1.998 + for (x=0; x < (int32_t)strlen(replaceStr); x++) { 1.999 + char16_t uChar = replaceStr[x]; 1.1000 + 1.1001 + int32_t i = 0; 1.1002 + while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) { 1.1003 + aPrinterName.Replace(i, 1, replSubstr); 1.1004 + i++; 1.1005 + } 1.1006 + } 1.1007 + return NS_OK; 1.1008 +} 1.1009 + 1.1010 +NS_IMETHODIMP 1.1011 +nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings *aPrintSettings, 1.1012 + const char16_t *aPrefName, int32_t *_retval) 1.1013 +{ 1.1014 + NS_ENSURE_ARG_POINTER(aPrintSettings); 1.1015 + NS_ENSURE_ARG_POINTER(aPrefName); 1.1016 + 1.1017 + nsAutoString prtName; 1.1018 + // Get the Printer Name from the PrintSettings 1.1019 + // to use as a prefix for Pref Names 1.1020 + GetAdjustedPrinterName(aPrintSettings, true, prtName); 1.1021 + 1.1022 + const char* prefName = 1.1023 + GetPrefName(NS_LossyConvertUTF16toASCII(aPrefName).get(), prtName); 1.1024 + 1.1025 + NS_ENSURE_TRUE(prefName, NS_ERROR_FAILURE); 1.1026 + 1.1027 + int32_t iVal; 1.1028 + nsresult rv = Preferences::GetInt(prefName, &iVal); 1.1029 + NS_ENSURE_SUCCESS(rv, rv); 1.1030 + 1.1031 + *_retval = iVal; 1.1032 + return rv; 1.1033 +} 1.1034 + 1.1035 +NS_IMETHODIMP 1.1036 +nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS, 1.1037 + bool aUsePNP, uint32_t aFlags) 1.1038 +{ 1.1039 + NS_ENSURE_ARG_POINTER(aPS); 1.1040 + 1.1041 + bool isInitialized; 1.1042 + aPS->GetIsInitializedFromPrefs(&isInitialized); 1.1043 + 1.1044 + if (isInitialized) 1.1045 + return NS_OK; 1.1046 + 1.1047 + nsAutoString prtName; 1.1048 + // read any non printer specific prefs 1.1049 + // with empty printer name 1.1050 + nsresult rv = ReadPrefs(aPS, prtName, aFlags); 1.1051 + NS_ENSURE_SUCCESS(rv, rv); 1.1052 + 1.1053 + // Do not use printer name in Linux because GTK backend does not support 1.1054 + // per printer settings. 1.1055 +#ifndef MOZ_X11 1.1056 + // Get the Printer Name from the PrintSettings 1.1057 + // to use as a prefix for Pref Names 1.1058 + rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName); 1.1059 + NS_ENSURE_SUCCESS(rv, rv); 1.1060 + 1.1061 + if (prtName.IsEmpty()) { 1.1062 + NS_WARNING("Caller should supply a printer name."); 1.1063 + return NS_OK; 1.1064 + } 1.1065 + 1.1066 + // Now read any printer specific prefs 1.1067 + rv = ReadPrefs(aPS, prtName, aFlags); 1.1068 + if (NS_SUCCEEDED(rv)) 1.1069 + aPS->SetIsInitializedFromPrefs(true); 1.1070 +#endif 1.1071 + 1.1072 + return NS_OK; 1.1073 +} 1.1074 + 1.1075 +/** 1.1076 + * Save all of the printer settings; if we can find a printer name, save 1.1077 + * printer-specific preferences. Otherwise, save generic ones. 1.1078 + */ 1.1079 +nsresult 1.1080 +nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings *aPS, 1.1081 + bool aUsePrinterNamePrefix, 1.1082 + uint32_t aFlags) 1.1083 +{ 1.1084 + NS_ENSURE_ARG_POINTER(aPS); 1.1085 + nsAutoString prtName; 1.1086 + 1.1087 + // Do not use printer name in Linux because GTK backend does not support 1.1088 + // per printer settings. 1.1089 +#ifndef MOZ_X11 1.1090 + // Get the printer name from the PrinterSettings for an optional prefix. 1.1091 + nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName); 1.1092 + NS_ENSURE_SUCCESS(rv, rv); 1.1093 +#endif 1.1094 + 1.1095 + // Write the prefs, with or without a printer name prefix. 1.1096 + return WritePrefs(aPS, prtName, aFlags); 1.1097 +} 1.1098 + 1.1099 + 1.1100 +//----------------------------------------------------- 1.1101 +//-- Protected Methods -------------------------------- 1.1102 +//----------------------------------------------------- 1.1103 +nsresult 1.1104 +nsPrintOptions::ReadPrefDouble(const char * aPrefId, double& aVal) 1.1105 +{ 1.1106 + NS_ENSURE_ARG_POINTER(aPrefId); 1.1107 + 1.1108 + nsAutoCString str; 1.1109 + nsresult rv = Preferences::GetCString(aPrefId, &str); 1.1110 + if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { 1.1111 + aVal = atof(str.get()); 1.1112 + } 1.1113 + return rv; 1.1114 +} 1.1115 + 1.1116 +nsresult 1.1117 +nsPrintOptions::WritePrefDouble(const char * aPrefId, double aVal) 1.1118 +{ 1.1119 + NS_ENSURE_ARG_POINTER(aPrefId); 1.1120 + 1.1121 + nsPrintfCString str("%6.2f", aVal); 1.1122 + NS_ENSURE_TRUE(!str.IsEmpty(), NS_ERROR_FAILURE); 1.1123 + 1.1124 + return Preferences::SetCString(aPrefId, str); 1.1125 +} 1.1126 + 1.1127 +void 1.1128 +nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId, int32_t& aTwips, 1.1129 + const char * aMarginPref) 1.1130 +{ 1.1131 + nsAutoString str; 1.1132 + nsresult rv = Preferences::GetString(aPrefId, &str); 1.1133 + if (NS_FAILED(rv) || str.IsEmpty()) { 1.1134 + rv = Preferences::GetString(aMarginPref, &str); 1.1135 + } 1.1136 + if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { 1.1137 + nsresult errCode; 1.1138 + float inches = str.ToFloat(&errCode); 1.1139 + if (NS_SUCCEEDED(errCode)) { 1.1140 + aTwips = NS_INCHES_TO_INT_TWIPS(inches); 1.1141 + } else { 1.1142 + aTwips = 0; 1.1143 + } 1.1144 + } 1.1145 +} 1.1146 + 1.1147 +void 1.1148 +nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId, int32_t aTwips) 1.1149 +{ 1.1150 + double inches = NS_TWIPS_TO_INCHES(aTwips); 1.1151 + nsAutoCString inchesStr; 1.1152 + inchesStr.AppendFloat(inches); 1.1153 + 1.1154 + Preferences::SetCString(aPrefId, inchesStr); 1.1155 +} 1.1156 + 1.1157 +void 1.1158 +nsPrintOptions::ReadInchesIntToTwipsPref(const char * aPrefId, int32_t& aTwips, 1.1159 + const char * aMarginPref) 1.1160 +{ 1.1161 + int32_t value; 1.1162 + nsresult rv = Preferences::GetInt(aPrefId, &value); 1.1163 + if (NS_FAILED(rv)) { 1.1164 + rv = Preferences::GetInt(aMarginPref, &value); 1.1165 + } 1.1166 + if (NS_SUCCEEDED(rv)) { 1.1167 + aTwips = NS_INCHES_TO_INT_TWIPS(float(value)/100.0f); 1.1168 + } else { 1.1169 + aTwips = 0; 1.1170 + } 1.1171 +} 1.1172 + 1.1173 +void 1.1174 +nsPrintOptions::WriteInchesIntFromTwipsPref(const char * aPrefId, int32_t aTwips) 1.1175 +{ 1.1176 + Preferences::SetInt(aPrefId, 1.1177 + int32_t(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f)); 1.1178 +} 1.1179 + 1.1180 +void 1.1181 +nsPrintOptions::ReadJustification(const char * aPrefId, int16_t& aJust, 1.1182 + int16_t aInitValue) 1.1183 +{ 1.1184 + aJust = aInitValue; 1.1185 + nsAutoString justStr; 1.1186 + if (NS_SUCCEEDED(Preferences::GetString(aPrefId, &justStr))) { 1.1187 + if (justStr.EqualsASCII(kJustRight)) { 1.1188 + aJust = nsIPrintSettings::kJustRight; 1.1189 + } else if (justStr.EqualsASCII(kJustCenter)) { 1.1190 + aJust = nsIPrintSettings::kJustCenter; 1.1191 + } else { 1.1192 + aJust = nsIPrintSettings::kJustLeft; 1.1193 + } 1.1194 + } 1.1195 +} 1.1196 + 1.1197 +//--------------------------------------------------- 1.1198 +void 1.1199 +nsPrintOptions::WriteJustification(const char * aPrefId, int16_t aJust) 1.1200 +{ 1.1201 + switch (aJust) { 1.1202 + case nsIPrintSettings::kJustLeft: 1.1203 + Preferences::SetCString(aPrefId, kJustLeft); 1.1204 + break; 1.1205 + 1.1206 + case nsIPrintSettings::kJustCenter: 1.1207 + Preferences::SetCString(aPrefId, kJustCenter); 1.1208 + break; 1.1209 + 1.1210 + case nsIPrintSettings::kJustRight: 1.1211 + Preferences::SetCString(aPrefId, kJustRight); 1.1212 + break; 1.1213 + } //switch 1.1214 +} 1.1215 + 1.1216 +//---------------------------------------------------------------------- 1.1217 +// Testing of read/write prefs 1.1218 +// This define turns on the testing module below 1.1219 +// so at start up it writes and reads the prefs. 1.1220 +#ifdef DEBUG_rods_X 1.1221 +class Tester { 1.1222 + public: 1.1223 + Tester(); 1.1224 +}; 1.1225 +Tester::Tester() 1.1226 +{ 1.1227 + nsCOMPtr<nsIPrintSettings> ps; 1.1228 + nsresult rv; 1.1229 + nsCOMPtr<nsIPrintOptions> printService = 1.1230 + do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv); 1.1231 + if (NS_SUCCEEDED(rv)) { 1.1232 + rv = printService->CreatePrintSettings(getter_AddRefs(ps)); 1.1233 + } 1.1234 + 1.1235 + if (ps) { 1.1236 + ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages, true); 1.1237 + ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, false); 1.1238 + ps->SetMarginTop(1.0); 1.1239 + ps->SetMarginLeft(1.0); 1.1240 + ps->SetMarginBottom(1.0); 1.1241 + ps->SetMarginRight(1.0); 1.1242 + ps->SetScaling(0.5); 1.1243 + ps->SetPrintBGColors(true); 1.1244 + ps->SetPrintBGImages(true); 1.1245 + ps->SetPrintRange(15); 1.1246 + ps->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get()); 1.1247 + ps->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get()); 1.1248 + ps->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get()); 1.1249 + ps->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get()); 1.1250 + ps->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get()); 1.1251 + ps->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get()); 1.1252 + ps->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get()); 1.1253 + ps->SetPlexName(NS_ConvertUTF8toUTF16("Plex Name").get()); 1.1254 + ps->SetPaperSizeType(10); 1.1255 + ps->SetPaperData(1); 1.1256 + ps->SetPaperWidth(100.0); 1.1257 + ps->SetPaperHeight(50.0); 1.1258 + ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters); 1.1259 + ps->SetPrintReversed(true); 1.1260 + ps->SetPrintInColor(true); 1.1261 + ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation); 1.1262 + ps->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get()); 1.1263 + ps->SetNumCopies(2); 1.1264 + ps->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get()); 1.1265 + ps->SetPrintToFile(true); 1.1266 + ps->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get()); 1.1267 + ps->SetPrintPageDelay(1000); 1.1268 + ps->SetShrinkToFit(true); 1.1269 + 1.1270 + struct SettingsType { 1.1271 + const char* mName; 1.1272 + uint32_t mFlag; 1.1273 + }; 1.1274 + SettingsType gSettings[] = { 1.1275 + {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages}, 1.1276 + {kPrintHeaderStrLeft, nsIPrintSettings::kInitSaveHeaderLeft}, 1.1277 + {kPrintHeaderStrCenter, nsIPrintSettings::kInitSaveHeaderCenter}, 1.1278 + {kPrintHeaderStrRight, nsIPrintSettings::kInitSaveHeaderRight}, 1.1279 + {kPrintFooterStrLeft, nsIPrintSettings::kInitSaveFooterLeft}, 1.1280 + {kPrintFooterStrCenter, nsIPrintSettings::kInitSaveFooterCenter}, 1.1281 + {kPrintFooterStrRight, nsIPrintSettings::kInitSaveFooterRight}, 1.1282 + {kPrintBGColors, nsIPrintSettings::kInitSaveBGColors}, 1.1283 + {kPrintBGImages, nsIPrintSettings::kInitSaveBGImages}, 1.1284 + {kPrintShrinkToFit, nsIPrintSettings::kInitSaveShrinkToFit}, 1.1285 + {kPrintPaperSize, nsIPrintSettings::kInitSavePaperSize}, 1.1286 + {kPrintPlexName, nsIPrintSettings::kInitSavePlexName}, 1.1287 + {kPrintPaperData, nsIPrintSettings::kInitSavePaperData}, 1.1288 + {kPrintReversed, nsIPrintSettings::kInitSaveReversed}, 1.1289 + {kPrintInColor, nsIPrintSettings::kInitSaveInColor}, 1.1290 + {kPrintColorspace, nsIPrintSettings::kInitSaveColorspace}, 1.1291 + {kPrintResolutionName, nsIPrintSettings::kInitSaveResolutionName}, 1.1292 + {kPrintDownloadFonts, nsIPrintSettings::kInitSaveDownloadFonts}, 1.1293 + {kPrintOrientation, nsIPrintSettings::kInitSaveOrientation}, 1.1294 + {kPrintCommand, nsIPrintSettings::kInitSavePrintCommand}, 1.1295 + {kPrinterName, nsIPrintSettings::kInitSavePrinterName}, 1.1296 + {kPrintToFile, nsIPrintSettings::kInitSavePrintToFile}, 1.1297 + {kPrintToFileName, nsIPrintSettings::kInitSaveToFileName}, 1.1298 + {kPrintPageDelay, nsIPrintSettings::kInitSavePageDelay}, 1.1299 + {"Margins", nsIPrintSettings::kInitSaveMargins}, 1.1300 + {"All", nsIPrintSettings::kInitSaveAll}, 1.1301 + {nullptr, 0}}; 1.1302 + 1.1303 + nsString prefix; prefix.AssignLiteral("Printer Name"); 1.1304 + int32_t i = 0; 1.1305 + while (gSettings[i].mName != nullptr) { 1.1306 + printf("------------------------------------------------\n"); 1.1307 + printf("%d) %s -> 0x%X\n", i, gSettings[i].mName, gSettings[i].mFlag); 1.1308 + printService->SavePrintSettingsToPrefs(ps, true, gSettings[i].mFlag); 1.1309 + printService->InitPrintSettingsFromPrefs(ps, true, 1.1310 + gSettings[i].mFlag); 1.1311 + i++; 1.1312 + } 1.1313 + } 1.1314 + 1.1315 +} 1.1316 +Tester gTester; 1.1317 +#endif