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