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