Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsPrintOptionsImpl.h" |
michael@0 | 7 | #include "nsReadableUtils.h" |
michael@0 | 8 | #include "nsPrintSettingsImpl.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIDOMWindow.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsIDialogParamBlock.h" |
michael@0 | 13 | #include "nsXPCOM.h" |
michael@0 | 14 | #include "nsISupportsPrimitives.h" |
michael@0 | 15 | #include "nsIWindowWatcher.h" |
michael@0 | 16 | #include "nsISupportsArray.h" |
michael@0 | 17 | #include "prprf.h" |
michael@0 | 18 | |
michael@0 | 19 | #include "nsIStringEnumerator.h" |
michael@0 | 20 | #include "nsISupportsPrimitives.h" |
michael@0 | 21 | #include "stdlib.h" |
michael@0 | 22 | #include "nsAutoPtr.h" |
michael@0 | 23 | #include "mozilla/Preferences.h" |
michael@0 | 24 | #include "nsPrintfCString.h" |
michael@0 | 25 | |
michael@0 | 26 | using namespace mozilla; |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsPrintOptions, nsIPrintOptions, nsIPrintSettingsService) |
michael@0 | 29 | |
michael@0 | 30 | // Pref Constants |
michael@0 | 31 | static const char kMarginTop[] = "print_margin_top"; |
michael@0 | 32 | static const char kMarginLeft[] = "print_margin_left"; |
michael@0 | 33 | static const char kMarginBottom[] = "print_margin_bottom"; |
michael@0 | 34 | static const char kMarginRight[] = "print_margin_right"; |
michael@0 | 35 | static const char kEdgeTop[] = "print_edge_top"; |
michael@0 | 36 | static const char kEdgeLeft[] = "print_edge_left"; |
michael@0 | 37 | static const char kEdgeBottom[] = "print_edge_bottom"; |
michael@0 | 38 | static const char kEdgeRight[] = "print_edge_right"; |
michael@0 | 39 | static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top"; |
michael@0 | 40 | static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left"; |
michael@0 | 41 | static const char kUnwriteableMarginBottom[] = "print_unwriteable_margin_bottom"; |
michael@0 | 42 | static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right"; |
michael@0 | 43 | |
michael@0 | 44 | // Prefs for Print Options |
michael@0 | 45 | static const char kPrintEvenPages[] = "print_evenpages"; |
michael@0 | 46 | static const char kPrintOddPages[] = "print_oddpages"; |
michael@0 | 47 | static const char kPrintHeaderStrLeft[] = "print_headerleft"; |
michael@0 | 48 | static const char kPrintHeaderStrCenter[] = "print_headercenter"; |
michael@0 | 49 | static const char kPrintHeaderStrRight[] = "print_headerright"; |
michael@0 | 50 | static const char kPrintFooterStrLeft[] = "print_footerleft"; |
michael@0 | 51 | static const char kPrintFooterStrCenter[] = "print_footercenter"; |
michael@0 | 52 | static const char kPrintFooterStrRight[] = "print_footerright"; |
michael@0 | 53 | |
michael@0 | 54 | // Additional Prefs |
michael@0 | 55 | static const char kPrintReversed[] = "print_reversed"; |
michael@0 | 56 | static const char kPrintInColor[] = "print_in_color"; |
michael@0 | 57 | static const char kPrintPaperName[] = "print_paper_name"; |
michael@0 | 58 | static const char kPrintPlexName[] = "print_plex_name"; |
michael@0 | 59 | static const char kPrintPaperSizeType[] = "print_paper_size_type"; |
michael@0 | 60 | static const char kPrintPaperData[] = "print_paper_data"; |
michael@0 | 61 | static const char kPrintPaperSizeUnit[] = "print_paper_size_unit"; |
michael@0 | 62 | static const char kPrintPaperWidth[] = "print_paper_width"; |
michael@0 | 63 | static const char kPrintPaperHeight[] = "print_paper_height"; |
michael@0 | 64 | static const char kPrintColorspace[] = "print_colorspace"; |
michael@0 | 65 | static const char kPrintResolutionName[]= "print_resolution_name"; |
michael@0 | 66 | static const char kPrintDownloadFonts[] = "print_downloadfonts"; |
michael@0 | 67 | static const char kPrintOrientation[] = "print_orientation"; |
michael@0 | 68 | static const char kPrintCommand[] = "print_command"; |
michael@0 | 69 | static const char kPrinterName[] = "print_printer"; |
michael@0 | 70 | static const char kPrintToFile[] = "print_to_file"; |
michael@0 | 71 | static const char kPrintToFileName[] = "print_to_filename"; |
michael@0 | 72 | static const char kPrintPageDelay[] = "print_page_delay"; |
michael@0 | 73 | static const char kPrintBGColors[] = "print_bgcolor"; |
michael@0 | 74 | static const char kPrintBGImages[] = "print_bgimages"; |
michael@0 | 75 | static const char kPrintShrinkToFit[] = "print_shrink_to_fit"; |
michael@0 | 76 | static const char kPrintScaling[] = "print_scaling"; |
michael@0 | 77 | static const char kPrintResolution[] = "print_resolution"; |
michael@0 | 78 | static const char kPrintDuplex[] = "print_duplex"; |
michael@0 | 79 | |
michael@0 | 80 | static const char kJustLeft[] = "left"; |
michael@0 | 81 | static const char kJustCenter[] = "center"; |
michael@0 | 82 | static const char kJustRight[] = "right"; |
michael@0 | 83 | |
michael@0 | 84 | #define NS_PRINTER_ENUMERATOR_CONTRACTID "@mozilla.org/gfx/printerenumerator;1" |
michael@0 | 85 | |
michael@0 | 86 | nsPrintOptions::nsPrintOptions() |
michael@0 | 87 | { |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | nsPrintOptions::~nsPrintOptions() |
michael@0 | 91 | { |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | nsresult |
michael@0 | 95 | nsPrintOptions::Init() |
michael@0 | 96 | { |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | NS_IMETHODIMP |
michael@0 | 101 | nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings *aPS) |
michael@0 | 102 | { |
michael@0 | 103 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 104 | nsresult rv; |
michael@0 | 105 | |
michael@0 | 106 | // create a nsISupportsArray of the parameters |
michael@0 | 107 | // being passed to the window |
michael@0 | 108 | nsCOMPtr<nsISupportsArray> array; |
michael@0 | 109 | rv = NS_NewISupportsArray(getter_AddRefs(array)); |
michael@0 | 110 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 111 | |
michael@0 | 112 | nsCOMPtr<nsISupports> psSupports = do_QueryInterface(aPS); |
michael@0 | 113 | NS_ASSERTION(psSupports, "PrintSettings must be a supports"); |
michael@0 | 114 | array->AppendElement(psSupports); |
michael@0 | 115 | |
michael@0 | 116 | nsCOMPtr<nsIDialogParamBlock> ioParamBlock = |
michael@0 | 117 | do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &rv); |
michael@0 | 118 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 119 | |
michael@0 | 120 | ioParamBlock->SetInt(0, 0); |
michael@0 | 121 | |
michael@0 | 122 | nsCOMPtr<nsISupports> blkSupps = do_QueryInterface(ioParamBlock); |
michael@0 | 123 | NS_ASSERTION(blkSupps, "IOBlk must be a supports"); |
michael@0 | 124 | array->AppendElement(blkSupps); |
michael@0 | 125 | |
michael@0 | 126 | nsCOMPtr<nsIWindowWatcher> wwatch = |
michael@0 | 127 | do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); |
michael@0 | 128 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 129 | |
michael@0 | 130 | nsCOMPtr<nsIDOMWindow> parent; |
michael@0 | 131 | wwatch->GetActiveWindow(getter_AddRefs(parent)); |
michael@0 | 132 | // null |parent| is non-fatal |
michael@0 | 133 | |
michael@0 | 134 | nsCOMPtr<nsIDOMWindow> newWindow; |
michael@0 | 135 | |
michael@0 | 136 | return wwatch->OpenWindow(parent, |
michael@0 | 137 | "chrome://global/content/printPageSetup.xul", |
michael@0 | 138 | "_blank","chrome,modal,centerscreen", array, |
michael@0 | 139 | getter_AddRefs(newWindow)); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /** --------------------------------------------------- |
michael@0 | 143 | * Helper function - Creates the "prefix" for the pref |
michael@0 | 144 | * It is either "print." |
michael@0 | 145 | * or "print.printer_<print name>." |
michael@0 | 146 | */ |
michael@0 | 147 | const char* |
michael@0 | 148 | nsPrintOptions::GetPrefName(const char * aPrefName, |
michael@0 | 149 | const nsAString& aPrinterName) |
michael@0 | 150 | { |
michael@0 | 151 | if (!aPrefName || !*aPrefName) { |
michael@0 | 152 | NS_ERROR("Must have a valid pref name!"); |
michael@0 | 153 | return aPrefName; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | mPrefName.AssignLiteral("print."); |
michael@0 | 157 | |
michael@0 | 158 | if (aPrinterName.Length()) { |
michael@0 | 159 | mPrefName.AppendLiteral("printer_"); |
michael@0 | 160 | AppendUTF16toUTF8(aPrinterName, mPrefName); |
michael@0 | 161 | mPrefName.AppendLiteral("."); |
michael@0 | 162 | } |
michael@0 | 163 | mPrefName += aPrefName; |
michael@0 | 164 | |
michael@0 | 165 | return mPrefName.get(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | //---------------------------------------------------------------------- |
michael@0 | 169 | // Testing of read/write prefs |
michael@0 | 170 | // This define controls debug output |
michael@0 | 171 | #ifdef DEBUG_rods_X |
michael@0 | 172 | static void WriteDebugStr(const char* aArg1, const char* aArg2, |
michael@0 | 173 | const char16_t* aStr) |
michael@0 | 174 | { |
michael@0 | 175 | nsString str(aStr); |
michael@0 | 176 | char16_t s = '&'; |
michael@0 | 177 | char16_t r = '_'; |
michael@0 | 178 | str.ReplaceChar(s, r); |
michael@0 | 179 | |
michael@0 | 180 | printf("%s %s = %s \n", aArg1, aArg2, ToNewUTF8String(str)); |
michael@0 | 181 | } |
michael@0 | 182 | const char* kWriteStr = "Write Pref:"; |
michael@0 | 183 | const char* kReadStr = "Read Pref:"; |
michael@0 | 184 | #define DUMP_STR(_a1, _a2, _a3) WriteDebugStr((_a1), GetPrefName((_a2), \ |
michael@0 | 185 | aPrefName), (_a3)); |
michael@0 | 186 | #define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \ |
michael@0 | 187 | GetPrefName((_a2), aPrefName), (_a3)?"T":"F"); |
michael@0 | 188 | #define DUMP_INT(_a1, _a2, _a3) printf("%s %s = %d \n", (_a1), \ |
michael@0 | 189 | GetPrefName((_a2), aPrefName), (_a3)); |
michael@0 | 190 | #define DUMP_DBL(_a1, _a2, _a3) printf("%s %s = %10.5f \n", (_a1), \ |
michael@0 | 191 | GetPrefName((_a2), aPrefName), (_a3)); |
michael@0 | 192 | #else |
michael@0 | 193 | #define DUMP_STR(_a1, _a2, _a3) |
michael@0 | 194 | #define DUMP_BOOL(_a1, _a2, _a3) |
michael@0 | 195 | #define DUMP_INT(_a1, _a2, _a3) |
michael@0 | 196 | #define DUMP_DBL(_a1, _a2, _a3) |
michael@0 | 197 | #endif /* DEBUG_rods_X */ |
michael@0 | 198 | //---------------------------------------------------------------------- |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * This will either read in the generic prefs (not specific to a printer) |
michael@0 | 202 | * or read the prefs in using the printer name to qualify. |
michael@0 | 203 | * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name" |
michael@0 | 204 | */ |
michael@0 | 205 | nsresult |
michael@0 | 206 | nsPrintOptions::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName, |
michael@0 | 207 | uint32_t aFlags) |
michael@0 | 208 | { |
michael@0 | 209 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 210 | |
michael@0 | 211 | if (aFlags & nsIPrintSettings::kInitSaveMargins) { |
michael@0 | 212 | int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5); |
michael@0 | 213 | nsIntMargin margin(halfInch, halfInch, halfInch, halfInch); |
michael@0 | 214 | ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top, |
michael@0 | 215 | kMarginTop); |
michael@0 | 216 | DUMP_INT(kReadStr, kMarginTop, margin.top); |
michael@0 | 217 | ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left, |
michael@0 | 218 | kMarginLeft); |
michael@0 | 219 | DUMP_INT(kReadStr, kMarginLeft, margin.left); |
michael@0 | 220 | ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName), |
michael@0 | 221 | margin.bottom, kMarginBottom); |
michael@0 | 222 | DUMP_INT(kReadStr, kMarginBottom, margin.bottom); |
michael@0 | 223 | ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right, |
michael@0 | 224 | kMarginRight); |
michael@0 | 225 | DUMP_INT(kReadStr, kMarginRight, margin.right); |
michael@0 | 226 | aPS->SetMarginInTwips(margin); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | if (aFlags & nsIPrintSettings::kInitSaveEdges) { |
michael@0 | 230 | nsIntMargin margin(0,0,0,0); |
michael@0 | 231 | ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top, |
michael@0 | 232 | kEdgeTop); |
michael@0 | 233 | DUMP_INT(kReadStr, kEdgeTop, margin.top); |
michael@0 | 234 | ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left, |
michael@0 | 235 | kEdgeLeft); |
michael@0 | 236 | DUMP_INT(kReadStr, kEdgeLeft, margin.left); |
michael@0 | 237 | ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName), |
michael@0 | 238 | margin.bottom, kEdgeBottom); |
michael@0 | 239 | DUMP_INT(kReadStr, kEdgeBottom, margin.bottom); |
michael@0 | 240 | ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName), margin.right, |
michael@0 | 241 | kEdgeRight); |
michael@0 | 242 | DUMP_INT(kReadStr, kEdgeRight, margin.right); |
michael@0 | 243 | aPS->SetEdgeInTwips(margin); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) { |
michael@0 | 247 | nsIntMargin margin; |
michael@0 | 248 | ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top, |
michael@0 | 249 | kUnwriteableMarginTop); |
michael@0 | 250 | DUMP_INT(kReadStr, kUnwriteableMarginTop, margin.top); |
michael@0 | 251 | ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left, |
michael@0 | 252 | kUnwriteableMarginLeft); |
michael@0 | 253 | DUMP_INT(kReadStr, kUnwriteableMarginLeft, margin.left); |
michael@0 | 254 | ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName), |
michael@0 | 255 | margin.bottom, kUnwriteableMarginBottom); |
michael@0 | 256 | DUMP_INT(kReadStr, kUnwriteableMarginBottom, margin.bottom); |
michael@0 | 257 | ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right, |
michael@0 | 258 | kUnwriteableMarginRight); |
michael@0 | 259 | DUMP_INT(kReadStr, kUnwriteableMarginRight, margin.right); |
michael@0 | 260 | aPS->SetUnwriteableMarginInTwips(margin); |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | bool b; |
michael@0 | 264 | nsAutoString str; |
michael@0 | 265 | int32_t iVal; |
michael@0 | 266 | double dbl; |
michael@0 | 267 | |
michael@0 | 268 | #define GETBOOLPREF(_prefname, _retval) \ |
michael@0 | 269 | NS_SUCCEEDED( \ |
michael@0 | 270 | Preferences::GetBool( \ |
michael@0 | 271 | GetPrefName(_prefname, aPrinterName), _retval \ |
michael@0 | 272 | ) \ |
michael@0 | 273 | ) |
michael@0 | 274 | |
michael@0 | 275 | #define GETSTRPREF(_prefname, _retval) \ |
michael@0 | 276 | NS_SUCCEEDED( \ |
michael@0 | 277 | Preferences::GetString( \ |
michael@0 | 278 | GetPrefName(_prefname, aPrinterName), _retval \ |
michael@0 | 279 | ) \ |
michael@0 | 280 | ) |
michael@0 | 281 | |
michael@0 | 282 | #define GETINTPREF(_prefname, _retval) \ |
michael@0 | 283 | NS_SUCCEEDED( \ |
michael@0 | 284 | Preferences::GetInt( \ |
michael@0 | 285 | GetPrefName(_prefname, aPrinterName), _retval \ |
michael@0 | 286 | ) \ |
michael@0 | 287 | ) |
michael@0 | 288 | |
michael@0 | 289 | #define GETDBLPREF(_prefname, _retval) \ |
michael@0 | 290 | NS_SUCCEEDED( \ |
michael@0 | 291 | ReadPrefDouble( \ |
michael@0 | 292 | GetPrefName(_prefname, aPrinterName), _retval \ |
michael@0 | 293 | ) \ |
michael@0 | 294 | ) |
michael@0 | 295 | |
michael@0 | 296 | // Paper size prefs are read as a group |
michael@0 | 297 | if (aFlags & nsIPrintSettings::kInitSavePaperSize) { |
michael@0 | 298 | int32_t sizeUnit, sizeType; |
michael@0 | 299 | double width, height; |
michael@0 | 300 | |
michael@0 | 301 | bool success = GETINTPREF(kPrintPaperSizeUnit, &sizeUnit) |
michael@0 | 302 | && GETINTPREF(kPrintPaperSizeType, &sizeType) |
michael@0 | 303 | && GETDBLPREF(kPrintPaperWidth, width) |
michael@0 | 304 | && GETDBLPREF(kPrintPaperHeight, height) |
michael@0 | 305 | && GETSTRPREF(kPrintPaperName, &str); |
michael@0 | 306 | |
michael@0 | 307 | // Bug 315687: Sanity check paper size to avoid paper size values in |
michael@0 | 308 | // mm when the size unit flag is inches. The value 100 is arbitrary |
michael@0 | 309 | // and can be changed. |
michael@0 | 310 | if (success) { |
michael@0 | 311 | success = (sizeUnit != nsIPrintSettings::kPaperSizeInches) |
michael@0 | 312 | || (width < 100.0) |
michael@0 | 313 | || (height < 100.0); |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | if (success) { |
michael@0 | 317 | aPS->SetPaperSizeUnit(sizeUnit); |
michael@0 | 318 | DUMP_INT(kReadStr, kPrintPaperSizeUnit, sizeUnit); |
michael@0 | 319 | aPS->SetPaperSizeType(sizeType); |
michael@0 | 320 | DUMP_INT(kReadStr, kPrintPaperSizeType, sizeType); |
michael@0 | 321 | aPS->SetPaperWidth(width); |
michael@0 | 322 | DUMP_DBL(kReadStr, kPrintPaperWidth, width); |
michael@0 | 323 | aPS->SetPaperHeight(height); |
michael@0 | 324 | DUMP_DBL(kReadStr, kPrintPaperHeight, height); |
michael@0 | 325 | aPS->SetPaperName(str.get()); |
michael@0 | 326 | DUMP_STR(kReadStr, kPrintPaperName, str.get()); |
michael@0 | 327 | } |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { |
michael@0 | 331 | if (GETBOOLPREF(kPrintEvenPages, &b)) { |
michael@0 | 332 | aPS->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, b); |
michael@0 | 333 | DUMP_BOOL(kReadStr, kPrintEvenPages, b); |
michael@0 | 334 | } |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { |
michael@0 | 338 | if (GETBOOLPREF(kPrintOddPages, &b)) { |
michael@0 | 339 | aPS->SetPrintOptions(nsIPrintSettings::kPrintOddPages, b); |
michael@0 | 340 | DUMP_BOOL(kReadStr, kPrintOddPages, b); |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { |
michael@0 | 345 | if (GETSTRPREF(kPrintHeaderStrLeft, &str)) { |
michael@0 | 346 | aPS->SetHeaderStrLeft(str.get()); |
michael@0 | 347 | DUMP_STR(kReadStr, kPrintHeaderStrLeft, str.get()); |
michael@0 | 348 | } |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { |
michael@0 | 352 | if (GETSTRPREF(kPrintHeaderStrCenter, &str)) { |
michael@0 | 353 | aPS->SetHeaderStrCenter(str.get()); |
michael@0 | 354 | DUMP_STR(kReadStr, kPrintHeaderStrCenter, str.get()); |
michael@0 | 355 | } |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { |
michael@0 | 359 | if (GETSTRPREF(kPrintHeaderStrRight, &str)) { |
michael@0 | 360 | aPS->SetHeaderStrRight(str.get()); |
michael@0 | 361 | DUMP_STR(kReadStr, kPrintHeaderStrRight, str.get()); |
michael@0 | 362 | } |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { |
michael@0 | 366 | if (GETSTRPREF(kPrintFooterStrLeft, &str)) { |
michael@0 | 367 | aPS->SetFooterStrLeft(str.get()); |
michael@0 | 368 | DUMP_STR(kReadStr, kPrintFooterStrLeft, str.get()); |
michael@0 | 369 | } |
michael@0 | 370 | } |
michael@0 | 371 | |
michael@0 | 372 | if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { |
michael@0 | 373 | if (GETSTRPREF(kPrintFooterStrCenter, &str)) { |
michael@0 | 374 | aPS->SetFooterStrCenter(str.get()); |
michael@0 | 375 | DUMP_STR(kReadStr, kPrintFooterStrCenter, str.get()); |
michael@0 | 376 | } |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { |
michael@0 | 380 | if (GETSTRPREF(kPrintFooterStrRight, &str)) { |
michael@0 | 381 | aPS->SetFooterStrRight(str.get()); |
michael@0 | 382 | DUMP_STR(kReadStr, kPrintFooterStrRight, str.get()); |
michael@0 | 383 | } |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | if (aFlags & nsIPrintSettings::kInitSaveBGColors) { |
michael@0 | 387 | if (GETBOOLPREF(kPrintBGColors, &b)) { |
michael@0 | 388 | aPS->SetPrintBGColors(b); |
michael@0 | 389 | DUMP_BOOL(kReadStr, kPrintBGColors, b); |
michael@0 | 390 | } |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | if (aFlags & nsIPrintSettings::kInitSaveBGImages) { |
michael@0 | 394 | if (GETBOOLPREF(kPrintBGImages, &b)) { |
michael@0 | 395 | aPS->SetPrintBGImages(b); |
michael@0 | 396 | DUMP_BOOL(kReadStr, kPrintBGImages, b); |
michael@0 | 397 | } |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | if (aFlags & nsIPrintSettings::kInitSaveReversed) { |
michael@0 | 401 | if (GETBOOLPREF(kPrintReversed, &b)) { |
michael@0 | 402 | aPS->SetPrintReversed(b); |
michael@0 | 403 | DUMP_BOOL(kReadStr, kPrintReversed, b); |
michael@0 | 404 | } |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | if (aFlags & nsIPrintSettings::kInitSaveInColor) { |
michael@0 | 408 | if (GETBOOLPREF(kPrintInColor, &b)) { |
michael@0 | 409 | aPS->SetPrintInColor(b); |
michael@0 | 410 | DUMP_BOOL(kReadStr, kPrintInColor, b); |
michael@0 | 411 | } |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | if (aFlags & nsIPrintSettings::kInitSavePlexName) { |
michael@0 | 415 | if (GETSTRPREF(kPrintPlexName, &str)) { |
michael@0 | 416 | aPS->SetPlexName(str.get()); |
michael@0 | 417 | DUMP_STR(kReadStr, kPrintPlexName, str.get()); |
michael@0 | 418 | } |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | if (aFlags & nsIPrintSettings::kInitSavePaperData) { |
michael@0 | 422 | if (GETINTPREF(kPrintPaperData, &iVal)) { |
michael@0 | 423 | aPS->SetPaperData(iVal); |
michael@0 | 424 | DUMP_INT(kReadStr, kPrintPaperData, iVal); |
michael@0 | 425 | } |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | if (aFlags & nsIPrintSettings::kInitSaveColorspace) { |
michael@0 | 429 | if (GETSTRPREF(kPrintColorspace, &str)) { |
michael@0 | 430 | aPS->SetColorspace(str.get()); |
michael@0 | 431 | DUMP_STR(kReadStr, kPrintColorspace, str.get()); |
michael@0 | 432 | } |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | if (aFlags & nsIPrintSettings::kInitSaveResolutionName) { |
michael@0 | 436 | if (GETSTRPREF(kPrintResolutionName, &str)) { |
michael@0 | 437 | aPS->SetResolutionName(str.get()); |
michael@0 | 438 | DUMP_STR(kReadStr, kPrintResolutionName, str.get()); |
michael@0 | 439 | } |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) { |
michael@0 | 443 | if (GETBOOLPREF(kPrintDownloadFonts, &b)) { |
michael@0 | 444 | aPS->SetDownloadFonts(b); |
michael@0 | 445 | DUMP_BOOL(kReadStr, kPrintDownloadFonts, b); |
michael@0 | 446 | } |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | if (aFlags & nsIPrintSettings::kInitSaveOrientation) { |
michael@0 | 450 | if (GETINTPREF(kPrintOrientation, &iVal)) { |
michael@0 | 451 | aPS->SetOrientation(iVal); |
michael@0 | 452 | DUMP_INT(kReadStr, kPrintOrientation, iVal); |
michael@0 | 453 | } |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | if (aFlags & nsIPrintSettings::kInitSavePrintCommand) { |
michael@0 | 457 | if (GETSTRPREF(kPrintCommand, &str)) { |
michael@0 | 458 | aPS->SetPrintCommand(str.get()); |
michael@0 | 459 | DUMP_STR(kReadStr, kPrintCommand, str.get()); |
michael@0 | 460 | } |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | if (aFlags & nsIPrintSettings::kInitSavePrintToFile) { |
michael@0 | 464 | if (GETBOOLPREF(kPrintToFile, &b)) { |
michael@0 | 465 | aPS->SetPrintToFile(b); |
michael@0 | 466 | DUMP_BOOL(kReadStr, kPrintToFile, b); |
michael@0 | 467 | } |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | if (aFlags & nsIPrintSettings::kInitSaveToFileName) { |
michael@0 | 471 | if (GETSTRPREF(kPrintToFileName, &str)) { |
michael@0 | 472 | aPS->SetToFileName(str.get()); |
michael@0 | 473 | DUMP_STR(kReadStr, kPrintToFileName, str.get()); |
michael@0 | 474 | } |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | if (aFlags & nsIPrintSettings::kInitSavePageDelay) { |
michael@0 | 478 | if (GETINTPREF(kPrintPageDelay, &iVal)) { |
michael@0 | 479 | aPS->SetPrintPageDelay(iVal); |
michael@0 | 480 | DUMP_INT(kReadStr, kPrintPageDelay, iVal); |
michael@0 | 481 | } |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) { |
michael@0 | 485 | if (GETBOOLPREF(kPrintShrinkToFit, &b)) { |
michael@0 | 486 | aPS->SetShrinkToFit(b); |
michael@0 | 487 | DUMP_BOOL(kReadStr, kPrintShrinkToFit, b); |
michael@0 | 488 | } |
michael@0 | 489 | } |
michael@0 | 490 | |
michael@0 | 491 | if (aFlags & nsIPrintSettings::kInitSaveScaling) { |
michael@0 | 492 | if (GETDBLPREF(kPrintScaling, dbl)) { |
michael@0 | 493 | aPS->SetScaling(dbl); |
michael@0 | 494 | DUMP_DBL(kReadStr, kPrintScaling, dbl); |
michael@0 | 495 | } |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | if (aFlags & nsIPrintSettings::kInitSaveResolution) { |
michael@0 | 499 | if (GETINTPREF(kPrintResolution, &iVal)) { |
michael@0 | 500 | aPS->SetResolution(iVal); |
michael@0 | 501 | DUMP_INT(kReadStr, kPrintResolution, iVal); |
michael@0 | 502 | } |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | if (aFlags & nsIPrintSettings::kInitSaveDuplex) { |
michael@0 | 506 | if (GETINTPREF(kPrintDuplex, &iVal)) { |
michael@0 | 507 | aPS->SetDuplex(iVal); |
michael@0 | 508 | DUMP_INT(kReadStr, kPrintDuplex, iVal); |
michael@0 | 509 | } |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | // Not Reading In: |
michael@0 | 513 | // Number of Copies |
michael@0 | 514 | |
michael@0 | 515 | return NS_OK; |
michael@0 | 516 | } |
michael@0 | 517 | |
michael@0 | 518 | /** --------------------------------------------------- |
michael@0 | 519 | * See documentation in nsPrintOptionsImpl.h |
michael@0 | 520 | * @update 1/12/01 rods |
michael@0 | 521 | */ |
michael@0 | 522 | nsresult |
michael@0 | 523 | nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName, |
michael@0 | 524 | uint32_t aFlags) |
michael@0 | 525 | { |
michael@0 | 526 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 527 | |
michael@0 | 528 | bool persistMarginBoxSettings; |
michael@0 | 529 | aPS->GetPersistMarginBoxSettings(&persistMarginBoxSettings); |
michael@0 | 530 | |
michael@0 | 531 | nsIntMargin margin; |
michael@0 | 532 | if (aFlags & nsIPrintSettings::kInitSaveMargins) { |
michael@0 | 533 | if (NS_SUCCEEDED(aPS->GetMarginInTwips(margin))) { |
michael@0 | 534 | WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), |
michael@0 | 535 | margin.top); |
michael@0 | 536 | DUMP_INT(kWriteStr, kMarginTop, margin.top); |
michael@0 | 537 | WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName), |
michael@0 | 538 | margin.left); |
michael@0 | 539 | DUMP_INT(kWriteStr, kMarginLeft, margin.top); |
michael@0 | 540 | WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName), |
michael@0 | 541 | margin.bottom); |
michael@0 | 542 | DUMP_INT(kWriteStr, kMarginBottom, margin.top); |
michael@0 | 543 | WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName), |
michael@0 | 544 | margin.right); |
michael@0 | 545 | DUMP_INT(kWriteStr, kMarginRight, margin.top); |
michael@0 | 546 | } |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | nsIntMargin edge; |
michael@0 | 550 | if (aFlags & nsIPrintSettings::kInitSaveEdges) { |
michael@0 | 551 | if (NS_SUCCEEDED(aPS->GetEdgeInTwips(edge))) { |
michael@0 | 552 | WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), |
michael@0 | 553 | edge.top); |
michael@0 | 554 | DUMP_INT(kWriteStr, kEdgeTop, edge.top); |
michael@0 | 555 | WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), |
michael@0 | 556 | edge.left); |
michael@0 | 557 | DUMP_INT(kWriteStr, kEdgeLeft, edge.top); |
michael@0 | 558 | WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName), |
michael@0 | 559 | edge.bottom); |
michael@0 | 560 | DUMP_INT(kWriteStr, kEdgeBottom, edge.top); |
michael@0 | 561 | WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName), |
michael@0 | 562 | edge.right); |
michael@0 | 563 | DUMP_INT(kWriteStr, kEdgeRight, edge.top); |
michael@0 | 564 | } |
michael@0 | 565 | } |
michael@0 | 566 | |
michael@0 | 567 | nsIntMargin unwriteableMargin; |
michael@0 | 568 | if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) { |
michael@0 | 569 | if (NS_SUCCEEDED(aPS->GetUnwriteableMarginInTwips(unwriteableMargin))) { |
michael@0 | 570 | WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName), |
michael@0 | 571 | unwriteableMargin.top); |
michael@0 | 572 | DUMP_INT(kWriteStr, kUnwriteableMarginTop, unwriteableMargin.top); |
michael@0 | 573 | WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName), |
michael@0 | 574 | unwriteableMargin.left); |
michael@0 | 575 | DUMP_INT(kWriteStr, kUnwriteableMarginLeft, unwriteableMargin.top); |
michael@0 | 576 | WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName), |
michael@0 | 577 | unwriteableMargin.bottom); |
michael@0 | 578 | DUMP_INT(kWriteStr, kUnwriteableMarginBottom, unwriteableMargin.top); |
michael@0 | 579 | WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName), |
michael@0 | 580 | unwriteableMargin.right); |
michael@0 | 581 | DUMP_INT(kWriteStr, kUnwriteableMarginRight, unwriteableMargin.top); |
michael@0 | 582 | } |
michael@0 | 583 | } |
michael@0 | 584 | |
michael@0 | 585 | // Paper size prefs are saved as a group |
michael@0 | 586 | if (aFlags & nsIPrintSettings::kInitSavePaperSize) { |
michael@0 | 587 | int16_t sizeUnit, sizeType; |
michael@0 | 588 | double width, height; |
michael@0 | 589 | char16_t *name; |
michael@0 | 590 | |
michael@0 | 591 | if ( |
michael@0 | 592 | NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) && |
michael@0 | 593 | NS_SUCCEEDED(aPS->GetPaperSizeType(&sizeType)) && |
michael@0 | 594 | NS_SUCCEEDED(aPS->GetPaperWidth(&width)) && |
michael@0 | 595 | NS_SUCCEEDED(aPS->GetPaperHeight(&height)) && |
michael@0 | 596 | NS_SUCCEEDED(aPS->GetPaperName(&name)) |
michael@0 | 597 | ) { |
michael@0 | 598 | DUMP_INT(kWriteStr, kPrintPaperSizeUnit, sizeUnit); |
michael@0 | 599 | Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName), |
michael@0 | 600 | int32_t(sizeUnit)); |
michael@0 | 601 | DUMP_INT(kWriteStr, kPrintPaperSizeType, sizeType); |
michael@0 | 602 | Preferences::SetInt(GetPrefName(kPrintPaperSizeType, aPrinterName), |
michael@0 | 603 | int32_t(sizeType)); |
michael@0 | 604 | DUMP_DBL(kWriteStr, kPrintPaperWidth, width); |
michael@0 | 605 | WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width); |
michael@0 | 606 | DUMP_DBL(kWriteStr, kPrintPaperHeight, height); |
michael@0 | 607 | WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height); |
michael@0 | 608 | DUMP_STR(kWriteStr, kPrintPaperName, name); |
michael@0 | 609 | Preferences::SetString(GetPrefName(kPrintPaperName, aPrinterName), name); |
michael@0 | 610 | } |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | bool b; |
michael@0 | 614 | char16_t* uStr; |
michael@0 | 615 | int32_t iVal; |
michael@0 | 616 | int16_t iVal16; |
michael@0 | 617 | double dbl; |
michael@0 | 618 | |
michael@0 | 619 | if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { |
michael@0 | 620 | if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintEvenPages, |
michael@0 | 621 | &b))) { |
michael@0 | 622 | DUMP_BOOL(kWriteStr, kPrintEvenPages, b); |
michael@0 | 623 | Preferences::SetBool(GetPrefName(kPrintEvenPages, aPrinterName), b); |
michael@0 | 624 | } |
michael@0 | 625 | } |
michael@0 | 626 | |
michael@0 | 627 | if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) { |
michael@0 | 628 | if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintOddPages, |
michael@0 | 629 | &b))) { |
michael@0 | 630 | DUMP_BOOL(kWriteStr, kPrintOddPages, b); |
michael@0 | 631 | Preferences::SetBool(GetPrefName(kPrintOddPages, aPrinterName), b); |
michael@0 | 632 | } |
michael@0 | 633 | } |
michael@0 | 634 | |
michael@0 | 635 | if (persistMarginBoxSettings) { |
michael@0 | 636 | if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { |
michael@0 | 637 | if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(&uStr))) { |
michael@0 | 638 | DUMP_STR(kWriteStr, kPrintHeaderStrLeft, uStr); |
michael@0 | 639 | Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName), |
michael@0 | 640 | uStr); |
michael@0 | 641 | } |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { |
michael@0 | 645 | if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(&uStr))) { |
michael@0 | 646 | DUMP_STR(kWriteStr, kPrintHeaderStrCenter, uStr); |
michael@0 | 647 | Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName), |
michael@0 | 648 | uStr); |
michael@0 | 649 | } |
michael@0 | 650 | } |
michael@0 | 651 | |
michael@0 | 652 | if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { |
michael@0 | 653 | if (NS_SUCCEEDED(aPS->GetHeaderStrRight(&uStr))) { |
michael@0 | 654 | DUMP_STR(kWriteStr, kPrintHeaderStrRight, uStr); |
michael@0 | 655 | Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName), |
michael@0 | 656 | uStr); |
michael@0 | 657 | } |
michael@0 | 658 | } |
michael@0 | 659 | |
michael@0 | 660 | if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { |
michael@0 | 661 | if (NS_SUCCEEDED(aPS->GetFooterStrLeft(&uStr))) { |
michael@0 | 662 | DUMP_STR(kWriteStr, kPrintFooterStrLeft, uStr); |
michael@0 | 663 | Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName), |
michael@0 | 664 | uStr); |
michael@0 | 665 | } |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { |
michael@0 | 669 | if (NS_SUCCEEDED(aPS->GetFooterStrCenter(&uStr))) { |
michael@0 | 670 | DUMP_STR(kWriteStr, kPrintFooterStrCenter, uStr); |
michael@0 | 671 | Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName), |
michael@0 | 672 | uStr); |
michael@0 | 673 | } |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { |
michael@0 | 677 | if (NS_SUCCEEDED(aPS->GetFooterStrRight(&uStr))) { |
michael@0 | 678 | DUMP_STR(kWriteStr, kPrintFooterStrRight, uStr); |
michael@0 | 679 | Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName), |
michael@0 | 680 | uStr); |
michael@0 | 681 | } |
michael@0 | 682 | } |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | if (aFlags & nsIPrintSettings::kInitSaveBGColors) { |
michael@0 | 686 | if (NS_SUCCEEDED(aPS->GetPrintBGColors(&b))) { |
michael@0 | 687 | DUMP_BOOL(kWriteStr, kPrintBGColors, b); |
michael@0 | 688 | Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b); |
michael@0 | 689 | } |
michael@0 | 690 | } |
michael@0 | 691 | |
michael@0 | 692 | if (aFlags & nsIPrintSettings::kInitSaveBGImages) { |
michael@0 | 693 | if (NS_SUCCEEDED(aPS->GetPrintBGImages(&b))) { |
michael@0 | 694 | DUMP_BOOL(kWriteStr, kPrintBGImages, b); |
michael@0 | 695 | Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b); |
michael@0 | 696 | } |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | if (aFlags & nsIPrintSettings::kInitSaveReversed) { |
michael@0 | 700 | if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) { |
michael@0 | 701 | DUMP_BOOL(kWriteStr, kPrintReversed, b); |
michael@0 | 702 | Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b); |
michael@0 | 703 | } |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | if (aFlags & nsIPrintSettings::kInitSaveInColor) { |
michael@0 | 707 | if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) { |
michael@0 | 708 | DUMP_BOOL(kWriteStr, kPrintInColor, b); |
michael@0 | 709 | Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b); |
michael@0 | 710 | } |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | if (aFlags & nsIPrintSettings::kInitSavePlexName) { |
michael@0 | 714 | if (NS_SUCCEEDED(aPS->GetPlexName(&uStr))) { |
michael@0 | 715 | DUMP_STR(kWriteStr, kPrintPlexName, uStr); |
michael@0 | 716 | Preferences::SetString(GetPrefName(kPrintPlexName, aPrinterName), uStr); |
michael@0 | 717 | } |
michael@0 | 718 | } |
michael@0 | 719 | |
michael@0 | 720 | if (aFlags & nsIPrintSettings::kInitSavePaperData) { |
michael@0 | 721 | if (NS_SUCCEEDED(aPS->GetPaperData(&iVal16))) { |
michael@0 | 722 | DUMP_INT(kWriteStr, kPrintPaperData, iVal16); |
michael@0 | 723 | Preferences::SetInt(GetPrefName(kPrintPaperData, aPrinterName), |
michael@0 | 724 | int32_t(iVal16)); |
michael@0 | 725 | } |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | if (aFlags & nsIPrintSettings::kInitSaveColorspace) { |
michael@0 | 729 | if (NS_SUCCEEDED(aPS->GetColorspace(&uStr))) { |
michael@0 | 730 | DUMP_STR(kWriteStr, kPrintColorspace, uStr); |
michael@0 | 731 | Preferences::SetString(GetPrefName(kPrintColorspace, aPrinterName), uStr); |
michael@0 | 732 | } |
michael@0 | 733 | } |
michael@0 | 734 | |
michael@0 | 735 | if (aFlags & nsIPrintSettings::kInitSaveResolutionName) { |
michael@0 | 736 | if (NS_SUCCEEDED(aPS->GetResolutionName(&uStr))) { |
michael@0 | 737 | DUMP_STR(kWriteStr, kPrintResolutionName, uStr); |
michael@0 | 738 | Preferences::SetString(GetPrefName(kPrintResolutionName, aPrinterName), |
michael@0 | 739 | uStr); |
michael@0 | 740 | } |
michael@0 | 741 | } |
michael@0 | 742 | |
michael@0 | 743 | if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) { |
michael@0 | 744 | if (NS_SUCCEEDED(aPS->GetDownloadFonts(&b))) { |
michael@0 | 745 | DUMP_BOOL(kWriteStr, kPrintDownloadFonts, b); |
michael@0 | 746 | Preferences::SetBool(GetPrefName(kPrintDownloadFonts, aPrinterName), b); |
michael@0 | 747 | } |
michael@0 | 748 | } |
michael@0 | 749 | |
michael@0 | 750 | if (aFlags & nsIPrintSettings::kInitSaveOrientation) { |
michael@0 | 751 | if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) { |
michael@0 | 752 | DUMP_INT(kWriteStr, kPrintOrientation, iVal); |
michael@0 | 753 | Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal); |
michael@0 | 754 | } |
michael@0 | 755 | } |
michael@0 | 756 | |
michael@0 | 757 | if (aFlags & nsIPrintSettings::kInitSavePrintCommand) { |
michael@0 | 758 | if (NS_SUCCEEDED(aPS->GetPrintCommand(&uStr))) { |
michael@0 | 759 | DUMP_STR(kWriteStr, kPrintCommand, uStr); |
michael@0 | 760 | Preferences::SetString(GetPrefName(kPrintCommand, aPrinterName), uStr); |
michael@0 | 761 | } |
michael@0 | 762 | } |
michael@0 | 763 | |
michael@0 | 764 | // Only the general version of this pref is saved |
michael@0 | 765 | if ((aFlags & nsIPrintSettings::kInitSavePrinterName) |
michael@0 | 766 | && aPrinterName.IsEmpty()) { |
michael@0 | 767 | if (NS_SUCCEEDED(aPS->GetPrinterName(&uStr))) { |
michael@0 | 768 | DUMP_STR(kWriteStr, kPrinterName, uStr); |
michael@0 | 769 | Preferences::SetString(kPrinterName, uStr); |
michael@0 | 770 | } |
michael@0 | 771 | } |
michael@0 | 772 | |
michael@0 | 773 | if (aFlags & nsIPrintSettings::kInitSavePrintToFile) { |
michael@0 | 774 | if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) { |
michael@0 | 775 | DUMP_BOOL(kWriteStr, kPrintToFile, b); |
michael@0 | 776 | Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b); |
michael@0 | 777 | } |
michael@0 | 778 | } |
michael@0 | 779 | |
michael@0 | 780 | if (aFlags & nsIPrintSettings::kInitSaveToFileName) { |
michael@0 | 781 | if (NS_SUCCEEDED(aPS->GetToFileName(&uStr))) { |
michael@0 | 782 | DUMP_STR(kWriteStr, kPrintToFileName, uStr); |
michael@0 | 783 | Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr); |
michael@0 | 784 | } |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | if (aFlags & nsIPrintSettings::kInitSavePageDelay) { |
michael@0 | 788 | if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) { |
michael@0 | 789 | DUMP_INT(kWriteStr, kPrintPageDelay, iVal); |
michael@0 | 790 | Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal); |
michael@0 | 791 | } |
michael@0 | 792 | } |
michael@0 | 793 | |
michael@0 | 794 | if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) { |
michael@0 | 795 | if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) { |
michael@0 | 796 | DUMP_BOOL(kWriteStr, kPrintShrinkToFit, b); |
michael@0 | 797 | Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b); |
michael@0 | 798 | } |
michael@0 | 799 | } |
michael@0 | 800 | |
michael@0 | 801 | if (aFlags & nsIPrintSettings::kInitSaveScaling) { |
michael@0 | 802 | if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) { |
michael@0 | 803 | DUMP_DBL(kWriteStr, kPrintScaling, dbl); |
michael@0 | 804 | WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl); |
michael@0 | 805 | } |
michael@0 | 806 | } |
michael@0 | 807 | |
michael@0 | 808 | if (aFlags & nsIPrintSettings::kInitSaveResolution) { |
michael@0 | 809 | if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) { |
michael@0 | 810 | DUMP_INT(kWriteStr, kPrintResolution, iVal); |
michael@0 | 811 | Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal); |
michael@0 | 812 | } |
michael@0 | 813 | } |
michael@0 | 814 | |
michael@0 | 815 | if (aFlags & nsIPrintSettings::kInitSaveDuplex) { |
michael@0 | 816 | if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) { |
michael@0 | 817 | DUMP_INT(kWriteStr, kPrintDuplex, iVal); |
michael@0 | 818 | Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal); |
michael@0 | 819 | } |
michael@0 | 820 | } |
michael@0 | 821 | |
michael@0 | 822 | // Not Writing Out: |
michael@0 | 823 | // Number of Copies |
michael@0 | 824 | |
michael@0 | 825 | return NS_OK; |
michael@0 | 826 | } |
michael@0 | 827 | |
michael@0 | 828 | NS_IMETHODIMP |
michael@0 | 829 | nsPrintOptions::DisplayJobProperties(const char16_t *aPrinter, |
michael@0 | 830 | nsIPrintSettings* aPrintSettings, |
michael@0 | 831 | bool *aDisplayed) |
michael@0 | 832 | { |
michael@0 | 833 | NS_ENSURE_ARG_POINTER(aPrinter); |
michael@0 | 834 | *aDisplayed = false; |
michael@0 | 835 | |
michael@0 | 836 | nsresult rv; |
michael@0 | 837 | nsCOMPtr<nsIPrinterEnumerator> propDlg = |
michael@0 | 838 | do_CreateInstance(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); |
michael@0 | 839 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 840 | |
michael@0 | 841 | NS_ENSURE_ARG_POINTER(aPrintSettings); |
michael@0 | 842 | rv = propDlg->DisplayPropertiesDlg(aPrinter, aPrintSettings); |
michael@0 | 843 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 844 | |
michael@0 | 845 | *aDisplayed = true; |
michael@0 | 846 | |
michael@0 | 847 | return rv; |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | NS_IMETHODIMP nsPrintOptions::GetNativeData(int16_t aDataType, void * *_retval) |
michael@0 | 851 | { |
michael@0 | 852 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 853 | } |
michael@0 | 854 | |
michael@0 | 855 | nsresult nsPrintOptions::_CreatePrintSettings(nsIPrintSettings **_retval) |
michael@0 | 856 | { |
michael@0 | 857 | // does not initially ref count |
michael@0 | 858 | nsPrintSettings * printSettings = new nsPrintSettings(); |
michael@0 | 859 | NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 860 | |
michael@0 | 861 | NS_ADDREF(*_retval = printSettings); // ref count |
michael@0 | 862 | |
michael@0 | 863 | nsXPIDLString printerName; |
michael@0 | 864 | nsresult rv = GetDefaultPrinterName(getter_Copies(printerName)); |
michael@0 | 865 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 866 | (*_retval)->SetPrinterName(printerName.get()); |
michael@0 | 867 | |
michael@0 | 868 | (void)InitPrintSettingsFromPrefs(*_retval, false, |
michael@0 | 869 | nsIPrintSettings::kInitSaveAll); |
michael@0 | 870 | |
michael@0 | 871 | return NS_OK; |
michael@0 | 872 | } |
michael@0 | 873 | |
michael@0 | 874 | NS_IMETHODIMP nsPrintOptions::CreatePrintSettings(nsIPrintSettings **_retval) |
michael@0 | 875 | { |
michael@0 | 876 | return _CreatePrintSettings(_retval); |
michael@0 | 877 | } |
michael@0 | 878 | |
michael@0 | 879 | NS_IMETHODIMP |
michael@0 | 880 | nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings **aGlobalPrintSettings) |
michael@0 | 881 | { |
michael@0 | 882 | nsresult rv; |
michael@0 | 883 | |
michael@0 | 884 | rv = CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings)); |
michael@0 | 885 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 886 | |
michael@0 | 887 | NS_ADDREF(*aGlobalPrintSettings = mGlobalPrintSettings.get()); |
michael@0 | 888 | |
michael@0 | 889 | return rv; |
michael@0 | 890 | } |
michael@0 | 891 | |
michael@0 | 892 | NS_IMETHODIMP |
michael@0 | 893 | nsPrintOptions::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings) |
michael@0 | 894 | { |
michael@0 | 895 | return CreatePrintSettings(aNewPrintSettings); |
michael@0 | 896 | } |
michael@0 | 897 | |
michael@0 | 898 | NS_IMETHODIMP |
michael@0 | 899 | nsPrintOptions::GetDefaultPrinterName(char16_t * *aDefaultPrinterName) |
michael@0 | 900 | { |
michael@0 | 901 | nsresult rv; |
michael@0 | 902 | nsCOMPtr<nsIPrinterEnumerator> prtEnum = |
michael@0 | 903 | do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); |
michael@0 | 904 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 905 | |
michael@0 | 906 | // Look up the printer from the last print job |
michael@0 | 907 | nsAutoString lastPrinterName; |
michael@0 | 908 | Preferences::GetString(kPrinterName, &lastPrinterName); |
michael@0 | 909 | if (!lastPrinterName.IsEmpty()) { |
michael@0 | 910 | // Verify it's still a valid printer |
michael@0 | 911 | nsCOMPtr<nsIStringEnumerator> printers; |
michael@0 | 912 | rv = prtEnum->GetPrinterNameList(getter_AddRefs(printers)); |
michael@0 | 913 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 914 | bool isValid = false; |
michael@0 | 915 | bool hasMore; |
michael@0 | 916 | while (NS_SUCCEEDED(printers->HasMore(&hasMore)) && hasMore) { |
michael@0 | 917 | nsAutoString printer; |
michael@0 | 918 | if (NS_SUCCEEDED(printers->GetNext(printer)) && lastPrinterName.Equals(printer)) { |
michael@0 | 919 | isValid = true; |
michael@0 | 920 | break; |
michael@0 | 921 | } |
michael@0 | 922 | } |
michael@0 | 923 | if (isValid) { |
michael@0 | 924 | *aDefaultPrinterName = ToNewUnicode(lastPrinterName); |
michael@0 | 925 | return NS_OK; |
michael@0 | 926 | } |
michael@0 | 927 | } |
michael@0 | 928 | } |
michael@0 | 929 | |
michael@0 | 930 | // There is no last printer preference, or it doesn't name a valid printer. |
michael@0 | 931 | // Return the default from the printer enumeration. |
michael@0 | 932 | return prtEnum->GetDefaultPrinterName(aDefaultPrinterName); |
michael@0 | 933 | } |
michael@0 | 934 | |
michael@0 | 935 | NS_IMETHODIMP |
michael@0 | 936 | nsPrintOptions::InitPrintSettingsFromPrinter(const char16_t *aPrinterName, |
michael@0 | 937 | nsIPrintSettings *aPrintSettings) |
michael@0 | 938 | { |
michael@0 | 939 | NS_ENSURE_ARG_POINTER(aPrintSettings); |
michael@0 | 940 | NS_ENSURE_ARG_POINTER(aPrinterName); |
michael@0 | 941 | |
michael@0 | 942 | #ifdef DEBUG |
michael@0 | 943 | nsXPIDLString printerName; |
michael@0 | 944 | aPrintSettings->GetPrinterName(getter_Copies(printerName)); |
michael@0 | 945 | if (!printerName.Equals(aPrinterName)) { |
michael@0 | 946 | NS_WARNING("Printer names should match!"); |
michael@0 | 947 | } |
michael@0 | 948 | #endif |
michael@0 | 949 | |
michael@0 | 950 | bool isInitialized; |
michael@0 | 951 | aPrintSettings->GetIsInitializedFromPrinter(&isInitialized); |
michael@0 | 952 | if (isInitialized) |
michael@0 | 953 | return NS_OK; |
michael@0 | 954 | |
michael@0 | 955 | nsresult rv; |
michael@0 | 956 | nsCOMPtr<nsIPrinterEnumerator> prtEnum = |
michael@0 | 957 | do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv); |
michael@0 | 958 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 959 | |
michael@0 | 960 | rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings); |
michael@0 | 961 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 962 | |
michael@0 | 963 | aPrintSettings->SetIsInitializedFromPrinter(true); |
michael@0 | 964 | return rv; |
michael@0 | 965 | } |
michael@0 | 966 | |
michael@0 | 967 | /** --------------------------------------------------- |
michael@0 | 968 | * Helper function - Returns either the name or sets the length to zero |
michael@0 | 969 | */ |
michael@0 | 970 | static nsresult |
michael@0 | 971 | GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP, |
michael@0 | 972 | nsAString& aPrinterName) |
michael@0 | 973 | { |
michael@0 | 974 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 975 | |
michael@0 | 976 | aPrinterName.Truncate(); |
michael@0 | 977 | if (!aUsePNP) |
michael@0 | 978 | return NS_OK; |
michael@0 | 979 | |
michael@0 | 980 | // Get the Printer Name from the PrintSettings |
michael@0 | 981 | // to use as a prefix for Pref Names |
michael@0 | 982 | char16_t* prtName = nullptr; |
michael@0 | 983 | |
michael@0 | 984 | nsresult rv = aPS->GetPrinterName(&prtName); |
michael@0 | 985 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 986 | |
michael@0 | 987 | aPrinterName = nsDependentString(prtName); |
michael@0 | 988 | |
michael@0 | 989 | // Convert any whitespaces, carriage returns or newlines to _ |
michael@0 | 990 | // The below algorithm is supposedly faster than using iterators |
michael@0 | 991 | NS_NAMED_LITERAL_STRING(replSubstr, "_"); |
michael@0 | 992 | const char* replaceStr = " \n\r"; |
michael@0 | 993 | |
michael@0 | 994 | int32_t x; |
michael@0 | 995 | for (x=0; x < (int32_t)strlen(replaceStr); x++) { |
michael@0 | 996 | char16_t uChar = replaceStr[x]; |
michael@0 | 997 | |
michael@0 | 998 | int32_t i = 0; |
michael@0 | 999 | while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) { |
michael@0 | 1000 | aPrinterName.Replace(i, 1, replSubstr); |
michael@0 | 1001 | i++; |
michael@0 | 1002 | } |
michael@0 | 1003 | } |
michael@0 | 1004 | return NS_OK; |
michael@0 | 1005 | } |
michael@0 | 1006 | |
michael@0 | 1007 | NS_IMETHODIMP |
michael@0 | 1008 | nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings *aPrintSettings, |
michael@0 | 1009 | const char16_t *aPrefName, int32_t *_retval) |
michael@0 | 1010 | { |
michael@0 | 1011 | NS_ENSURE_ARG_POINTER(aPrintSettings); |
michael@0 | 1012 | NS_ENSURE_ARG_POINTER(aPrefName); |
michael@0 | 1013 | |
michael@0 | 1014 | nsAutoString prtName; |
michael@0 | 1015 | // Get the Printer Name from the PrintSettings |
michael@0 | 1016 | // to use as a prefix for Pref Names |
michael@0 | 1017 | GetAdjustedPrinterName(aPrintSettings, true, prtName); |
michael@0 | 1018 | |
michael@0 | 1019 | const char* prefName = |
michael@0 | 1020 | GetPrefName(NS_LossyConvertUTF16toASCII(aPrefName).get(), prtName); |
michael@0 | 1021 | |
michael@0 | 1022 | NS_ENSURE_TRUE(prefName, NS_ERROR_FAILURE); |
michael@0 | 1023 | |
michael@0 | 1024 | int32_t iVal; |
michael@0 | 1025 | nsresult rv = Preferences::GetInt(prefName, &iVal); |
michael@0 | 1026 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1027 | |
michael@0 | 1028 | *_retval = iVal; |
michael@0 | 1029 | return rv; |
michael@0 | 1030 | } |
michael@0 | 1031 | |
michael@0 | 1032 | NS_IMETHODIMP |
michael@0 | 1033 | nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS, |
michael@0 | 1034 | bool aUsePNP, uint32_t aFlags) |
michael@0 | 1035 | { |
michael@0 | 1036 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 1037 | |
michael@0 | 1038 | bool isInitialized; |
michael@0 | 1039 | aPS->GetIsInitializedFromPrefs(&isInitialized); |
michael@0 | 1040 | |
michael@0 | 1041 | if (isInitialized) |
michael@0 | 1042 | return NS_OK; |
michael@0 | 1043 | |
michael@0 | 1044 | nsAutoString prtName; |
michael@0 | 1045 | // read any non printer specific prefs |
michael@0 | 1046 | // with empty printer name |
michael@0 | 1047 | nsresult rv = ReadPrefs(aPS, prtName, aFlags); |
michael@0 | 1048 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1049 | |
michael@0 | 1050 | // Do not use printer name in Linux because GTK backend does not support |
michael@0 | 1051 | // per printer settings. |
michael@0 | 1052 | #ifndef MOZ_X11 |
michael@0 | 1053 | // Get the Printer Name from the PrintSettings |
michael@0 | 1054 | // to use as a prefix for Pref Names |
michael@0 | 1055 | rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName); |
michael@0 | 1056 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1057 | |
michael@0 | 1058 | if (prtName.IsEmpty()) { |
michael@0 | 1059 | NS_WARNING("Caller should supply a printer name."); |
michael@0 | 1060 | return NS_OK; |
michael@0 | 1061 | } |
michael@0 | 1062 | |
michael@0 | 1063 | // Now read any printer specific prefs |
michael@0 | 1064 | rv = ReadPrefs(aPS, prtName, aFlags); |
michael@0 | 1065 | if (NS_SUCCEEDED(rv)) |
michael@0 | 1066 | aPS->SetIsInitializedFromPrefs(true); |
michael@0 | 1067 | #endif |
michael@0 | 1068 | |
michael@0 | 1069 | return NS_OK; |
michael@0 | 1070 | } |
michael@0 | 1071 | |
michael@0 | 1072 | /** |
michael@0 | 1073 | * Save all of the printer settings; if we can find a printer name, save |
michael@0 | 1074 | * printer-specific preferences. Otherwise, save generic ones. |
michael@0 | 1075 | */ |
michael@0 | 1076 | nsresult |
michael@0 | 1077 | nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings *aPS, |
michael@0 | 1078 | bool aUsePrinterNamePrefix, |
michael@0 | 1079 | uint32_t aFlags) |
michael@0 | 1080 | { |
michael@0 | 1081 | NS_ENSURE_ARG_POINTER(aPS); |
michael@0 | 1082 | nsAutoString prtName; |
michael@0 | 1083 | |
michael@0 | 1084 | // Do not use printer name in Linux because GTK backend does not support |
michael@0 | 1085 | // per printer settings. |
michael@0 | 1086 | #ifndef MOZ_X11 |
michael@0 | 1087 | // Get the printer name from the PrinterSettings for an optional prefix. |
michael@0 | 1088 | nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName); |
michael@0 | 1089 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1090 | #endif |
michael@0 | 1091 | |
michael@0 | 1092 | // Write the prefs, with or without a printer name prefix. |
michael@0 | 1093 | return WritePrefs(aPS, prtName, aFlags); |
michael@0 | 1094 | } |
michael@0 | 1095 | |
michael@0 | 1096 | |
michael@0 | 1097 | //----------------------------------------------------- |
michael@0 | 1098 | //-- Protected Methods -------------------------------- |
michael@0 | 1099 | //----------------------------------------------------- |
michael@0 | 1100 | nsresult |
michael@0 | 1101 | nsPrintOptions::ReadPrefDouble(const char * aPrefId, double& aVal) |
michael@0 | 1102 | { |
michael@0 | 1103 | NS_ENSURE_ARG_POINTER(aPrefId); |
michael@0 | 1104 | |
michael@0 | 1105 | nsAutoCString str; |
michael@0 | 1106 | nsresult rv = Preferences::GetCString(aPrefId, &str); |
michael@0 | 1107 | if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { |
michael@0 | 1108 | aVal = atof(str.get()); |
michael@0 | 1109 | } |
michael@0 | 1110 | return rv; |
michael@0 | 1111 | } |
michael@0 | 1112 | |
michael@0 | 1113 | nsresult |
michael@0 | 1114 | nsPrintOptions::WritePrefDouble(const char * aPrefId, double aVal) |
michael@0 | 1115 | { |
michael@0 | 1116 | NS_ENSURE_ARG_POINTER(aPrefId); |
michael@0 | 1117 | |
michael@0 | 1118 | nsPrintfCString str("%6.2f", aVal); |
michael@0 | 1119 | NS_ENSURE_TRUE(!str.IsEmpty(), NS_ERROR_FAILURE); |
michael@0 | 1120 | |
michael@0 | 1121 | return Preferences::SetCString(aPrefId, str); |
michael@0 | 1122 | } |
michael@0 | 1123 | |
michael@0 | 1124 | void |
michael@0 | 1125 | nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId, int32_t& aTwips, |
michael@0 | 1126 | const char * aMarginPref) |
michael@0 | 1127 | { |
michael@0 | 1128 | nsAutoString str; |
michael@0 | 1129 | nsresult rv = Preferences::GetString(aPrefId, &str); |
michael@0 | 1130 | if (NS_FAILED(rv) || str.IsEmpty()) { |
michael@0 | 1131 | rv = Preferences::GetString(aMarginPref, &str); |
michael@0 | 1132 | } |
michael@0 | 1133 | if (NS_SUCCEEDED(rv) && !str.IsEmpty()) { |
michael@0 | 1134 | nsresult errCode; |
michael@0 | 1135 | float inches = str.ToFloat(&errCode); |
michael@0 | 1136 | if (NS_SUCCEEDED(errCode)) { |
michael@0 | 1137 | aTwips = NS_INCHES_TO_INT_TWIPS(inches); |
michael@0 | 1138 | } else { |
michael@0 | 1139 | aTwips = 0; |
michael@0 | 1140 | } |
michael@0 | 1141 | } |
michael@0 | 1142 | } |
michael@0 | 1143 | |
michael@0 | 1144 | void |
michael@0 | 1145 | nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId, int32_t aTwips) |
michael@0 | 1146 | { |
michael@0 | 1147 | double inches = NS_TWIPS_TO_INCHES(aTwips); |
michael@0 | 1148 | nsAutoCString inchesStr; |
michael@0 | 1149 | inchesStr.AppendFloat(inches); |
michael@0 | 1150 | |
michael@0 | 1151 | Preferences::SetCString(aPrefId, inchesStr); |
michael@0 | 1152 | } |
michael@0 | 1153 | |
michael@0 | 1154 | void |
michael@0 | 1155 | nsPrintOptions::ReadInchesIntToTwipsPref(const char * aPrefId, int32_t& aTwips, |
michael@0 | 1156 | const char * aMarginPref) |
michael@0 | 1157 | { |
michael@0 | 1158 | int32_t value; |
michael@0 | 1159 | nsresult rv = Preferences::GetInt(aPrefId, &value); |
michael@0 | 1160 | if (NS_FAILED(rv)) { |
michael@0 | 1161 | rv = Preferences::GetInt(aMarginPref, &value); |
michael@0 | 1162 | } |
michael@0 | 1163 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 1164 | aTwips = NS_INCHES_TO_INT_TWIPS(float(value)/100.0f); |
michael@0 | 1165 | } else { |
michael@0 | 1166 | aTwips = 0; |
michael@0 | 1167 | } |
michael@0 | 1168 | } |
michael@0 | 1169 | |
michael@0 | 1170 | void |
michael@0 | 1171 | nsPrintOptions::WriteInchesIntFromTwipsPref(const char * aPrefId, int32_t aTwips) |
michael@0 | 1172 | { |
michael@0 | 1173 | Preferences::SetInt(aPrefId, |
michael@0 | 1174 | int32_t(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f)); |
michael@0 | 1175 | } |
michael@0 | 1176 | |
michael@0 | 1177 | void |
michael@0 | 1178 | nsPrintOptions::ReadJustification(const char * aPrefId, int16_t& aJust, |
michael@0 | 1179 | int16_t aInitValue) |
michael@0 | 1180 | { |
michael@0 | 1181 | aJust = aInitValue; |
michael@0 | 1182 | nsAutoString justStr; |
michael@0 | 1183 | if (NS_SUCCEEDED(Preferences::GetString(aPrefId, &justStr))) { |
michael@0 | 1184 | if (justStr.EqualsASCII(kJustRight)) { |
michael@0 | 1185 | aJust = nsIPrintSettings::kJustRight; |
michael@0 | 1186 | } else if (justStr.EqualsASCII(kJustCenter)) { |
michael@0 | 1187 | aJust = nsIPrintSettings::kJustCenter; |
michael@0 | 1188 | } else { |
michael@0 | 1189 | aJust = nsIPrintSettings::kJustLeft; |
michael@0 | 1190 | } |
michael@0 | 1191 | } |
michael@0 | 1192 | } |
michael@0 | 1193 | |
michael@0 | 1194 | //--------------------------------------------------- |
michael@0 | 1195 | void |
michael@0 | 1196 | nsPrintOptions::WriteJustification(const char * aPrefId, int16_t aJust) |
michael@0 | 1197 | { |
michael@0 | 1198 | switch (aJust) { |
michael@0 | 1199 | case nsIPrintSettings::kJustLeft: |
michael@0 | 1200 | Preferences::SetCString(aPrefId, kJustLeft); |
michael@0 | 1201 | break; |
michael@0 | 1202 | |
michael@0 | 1203 | case nsIPrintSettings::kJustCenter: |
michael@0 | 1204 | Preferences::SetCString(aPrefId, kJustCenter); |
michael@0 | 1205 | break; |
michael@0 | 1206 | |
michael@0 | 1207 | case nsIPrintSettings::kJustRight: |
michael@0 | 1208 | Preferences::SetCString(aPrefId, kJustRight); |
michael@0 | 1209 | break; |
michael@0 | 1210 | } //switch |
michael@0 | 1211 | } |
michael@0 | 1212 | |
michael@0 | 1213 | //---------------------------------------------------------------------- |
michael@0 | 1214 | // Testing of read/write prefs |
michael@0 | 1215 | // This define turns on the testing module below |
michael@0 | 1216 | // so at start up it writes and reads the prefs. |
michael@0 | 1217 | #ifdef DEBUG_rods_X |
michael@0 | 1218 | class Tester { |
michael@0 | 1219 | public: |
michael@0 | 1220 | Tester(); |
michael@0 | 1221 | }; |
michael@0 | 1222 | Tester::Tester() |
michael@0 | 1223 | { |
michael@0 | 1224 | nsCOMPtr<nsIPrintSettings> ps; |
michael@0 | 1225 | nsresult rv; |
michael@0 | 1226 | nsCOMPtr<nsIPrintOptions> printService = |
michael@0 | 1227 | do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv); |
michael@0 | 1228 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 1229 | rv = printService->CreatePrintSettings(getter_AddRefs(ps)); |
michael@0 | 1230 | } |
michael@0 | 1231 | |
michael@0 | 1232 | if (ps) { |
michael@0 | 1233 | ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages, true); |
michael@0 | 1234 | ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, false); |
michael@0 | 1235 | ps->SetMarginTop(1.0); |
michael@0 | 1236 | ps->SetMarginLeft(1.0); |
michael@0 | 1237 | ps->SetMarginBottom(1.0); |
michael@0 | 1238 | ps->SetMarginRight(1.0); |
michael@0 | 1239 | ps->SetScaling(0.5); |
michael@0 | 1240 | ps->SetPrintBGColors(true); |
michael@0 | 1241 | ps->SetPrintBGImages(true); |
michael@0 | 1242 | ps->SetPrintRange(15); |
michael@0 | 1243 | ps->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get()); |
michael@0 | 1244 | ps->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get()); |
michael@0 | 1245 | ps->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get()); |
michael@0 | 1246 | ps->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get()); |
michael@0 | 1247 | ps->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get()); |
michael@0 | 1248 | ps->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get()); |
michael@0 | 1249 | ps->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get()); |
michael@0 | 1250 | ps->SetPlexName(NS_ConvertUTF8toUTF16("Plex Name").get()); |
michael@0 | 1251 | ps->SetPaperSizeType(10); |
michael@0 | 1252 | ps->SetPaperData(1); |
michael@0 | 1253 | ps->SetPaperWidth(100.0); |
michael@0 | 1254 | ps->SetPaperHeight(50.0); |
michael@0 | 1255 | ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters); |
michael@0 | 1256 | ps->SetPrintReversed(true); |
michael@0 | 1257 | ps->SetPrintInColor(true); |
michael@0 | 1258 | ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation); |
michael@0 | 1259 | ps->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get()); |
michael@0 | 1260 | ps->SetNumCopies(2); |
michael@0 | 1261 | ps->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get()); |
michael@0 | 1262 | ps->SetPrintToFile(true); |
michael@0 | 1263 | ps->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get()); |
michael@0 | 1264 | ps->SetPrintPageDelay(1000); |
michael@0 | 1265 | ps->SetShrinkToFit(true); |
michael@0 | 1266 | |
michael@0 | 1267 | struct SettingsType { |
michael@0 | 1268 | const char* mName; |
michael@0 | 1269 | uint32_t mFlag; |
michael@0 | 1270 | }; |
michael@0 | 1271 | SettingsType gSettings[] = { |
michael@0 | 1272 | {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages}, |
michael@0 | 1273 | {kPrintHeaderStrLeft, nsIPrintSettings::kInitSaveHeaderLeft}, |
michael@0 | 1274 | {kPrintHeaderStrCenter, nsIPrintSettings::kInitSaveHeaderCenter}, |
michael@0 | 1275 | {kPrintHeaderStrRight, nsIPrintSettings::kInitSaveHeaderRight}, |
michael@0 | 1276 | {kPrintFooterStrLeft, nsIPrintSettings::kInitSaveFooterLeft}, |
michael@0 | 1277 | {kPrintFooterStrCenter, nsIPrintSettings::kInitSaveFooterCenter}, |
michael@0 | 1278 | {kPrintFooterStrRight, nsIPrintSettings::kInitSaveFooterRight}, |
michael@0 | 1279 | {kPrintBGColors, nsIPrintSettings::kInitSaveBGColors}, |
michael@0 | 1280 | {kPrintBGImages, nsIPrintSettings::kInitSaveBGImages}, |
michael@0 | 1281 | {kPrintShrinkToFit, nsIPrintSettings::kInitSaveShrinkToFit}, |
michael@0 | 1282 | {kPrintPaperSize, nsIPrintSettings::kInitSavePaperSize}, |
michael@0 | 1283 | {kPrintPlexName, nsIPrintSettings::kInitSavePlexName}, |
michael@0 | 1284 | {kPrintPaperData, nsIPrintSettings::kInitSavePaperData}, |
michael@0 | 1285 | {kPrintReversed, nsIPrintSettings::kInitSaveReversed}, |
michael@0 | 1286 | {kPrintInColor, nsIPrintSettings::kInitSaveInColor}, |
michael@0 | 1287 | {kPrintColorspace, nsIPrintSettings::kInitSaveColorspace}, |
michael@0 | 1288 | {kPrintResolutionName, nsIPrintSettings::kInitSaveResolutionName}, |
michael@0 | 1289 | {kPrintDownloadFonts, nsIPrintSettings::kInitSaveDownloadFonts}, |
michael@0 | 1290 | {kPrintOrientation, nsIPrintSettings::kInitSaveOrientation}, |
michael@0 | 1291 | {kPrintCommand, nsIPrintSettings::kInitSavePrintCommand}, |
michael@0 | 1292 | {kPrinterName, nsIPrintSettings::kInitSavePrinterName}, |
michael@0 | 1293 | {kPrintToFile, nsIPrintSettings::kInitSavePrintToFile}, |
michael@0 | 1294 | {kPrintToFileName, nsIPrintSettings::kInitSaveToFileName}, |
michael@0 | 1295 | {kPrintPageDelay, nsIPrintSettings::kInitSavePageDelay}, |
michael@0 | 1296 | {"Margins", nsIPrintSettings::kInitSaveMargins}, |
michael@0 | 1297 | {"All", nsIPrintSettings::kInitSaveAll}, |
michael@0 | 1298 | {nullptr, 0}}; |
michael@0 | 1299 | |
michael@0 | 1300 | nsString prefix; prefix.AssignLiteral("Printer Name"); |
michael@0 | 1301 | int32_t i = 0; |
michael@0 | 1302 | while (gSettings[i].mName != nullptr) { |
michael@0 | 1303 | printf("------------------------------------------------\n"); |
michael@0 | 1304 | printf("%d) %s -> 0x%X\n", i, gSettings[i].mName, gSettings[i].mFlag); |
michael@0 | 1305 | printService->SavePrintSettingsToPrefs(ps, true, gSettings[i].mFlag); |
michael@0 | 1306 | printService->InitPrintSettingsFromPrefs(ps, true, |
michael@0 | 1307 | gSettings[i].mFlag); |
michael@0 | 1308 | i++; |
michael@0 | 1309 | } |
michael@0 | 1310 | } |
michael@0 | 1311 | |
michael@0 | 1312 | } |
michael@0 | 1313 | Tester gTester; |
michael@0 | 1314 | #endif |