Bug 1753131 - wait for focus before dispatching devicechange events r=jib
[gecko.git] / widget / nsPrintSettingsService.cpp
blob15db22477edcf6df5610ef5d4d461bf9f118ac05
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/. */
6 #include "nsPrintSettingsService.h"
8 #include "mozilla/embedding/PPrinting.h"
9 #include "mozilla/layout/RemotePrintJobChild.h"
10 #include "mozilla/RefPtr.h"
11 #include "nsCoord.h"
12 #include "nsIPrinterList.h"
13 #include "nsPrintingProxy.h"
14 #include "nsReadableUtils.h"
15 #include "nsPrintSettingsImpl.h"
16 #include "nsIPrintSession.h"
17 #include "nsServiceManagerUtils.h"
18 #include "nsSize.h"
20 #include "nsArray.h"
21 #include "nsXPCOM.h"
22 #include "nsXULAppAPI.h"
24 #include "nsIStringEnumerator.h"
25 #include "stdlib.h"
26 #include "mozilla/StaticPrefs_print.h"
27 #include "mozilla/Preferences.h"
28 #include "nsPrintfCString.h"
30 using namespace mozilla;
31 using namespace mozilla::embedding;
33 typedef mozilla::layout::RemotePrintJobChild RemotePrintJobChild;
35 NS_IMPL_ISUPPORTS(nsPrintSettingsService, nsIPrintSettingsService)
37 // Pref Constants
38 static const char kMarginTop[] = "print_margin_top";
39 static const char kMarginLeft[] = "print_margin_left";
40 static const char kMarginBottom[] = "print_margin_bottom";
41 static const char kMarginRight[] = "print_margin_right";
42 static const char kEdgeTop[] = "print_edge_top";
43 static const char kEdgeLeft[] = "print_edge_left";
44 static const char kEdgeBottom[] = "print_edge_bottom";
45 static const char kEdgeRight[] = "print_edge_right";
47 static const char kUnwriteableMarginTopTwips[] =
48 "print_unwriteable_margin_top_twips";
49 static const char kUnwriteableMarginLeftTwips[] =
50 "print_unwriteable_margin_left_twips";
51 static const char kUnwriteableMarginBottomTwips[] =
52 "print_unwriteable_margin_bottom_twips";
53 static const char kUnwriteableMarginRightTwips[] =
54 "print_unwriteable_margin_right_twips";
56 // These are legacy versions of the above UnwriteableMargin prefs. The new ones,
57 // which are in twips, were introduced to more accurately record the values.
58 static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top";
59 static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left";
60 static const char kUnwriteableMarginBottom[] =
61 "print_unwriteable_margin_bottom";
62 static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
64 // Prefs for Print Options
65 static const char kPrintHeaderStrLeft[] = "print_headerleft";
66 static const char kPrintHeaderStrCenter[] = "print_headercenter";
67 static const char kPrintHeaderStrRight[] = "print_headerright";
68 static const char kPrintFooterStrLeft[] = "print_footerleft";
69 static const char kPrintFooterStrCenter[] = "print_footercenter";
70 static const char kPrintFooterStrRight[] = "print_footerright";
72 // Additional Prefs
73 static const char kPrintReversed[] = "print_reversed";
74 static const char kPrintInColor[] = "print_in_color";
75 static const char kPrintPaperId[] = "print_paper_id";
76 static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
77 static const char kPrintPaperWidth[] = "print_paper_width";
78 static const char kPrintPaperHeight[] = "print_paper_height";
79 static const char kPrintOrientation[] = "print_orientation";
80 static const char kPrinterName[] = "print_printer";
81 static const char kPrintToFile[] = "print_to_file";
82 static const char kPrintToFileName[] = "print_to_filename";
83 static const char kPrintPageDelay[] = "print_page_delay";
84 static const char kPrintBGColors[] = "print_bgcolor";
85 static const char kPrintBGImages[] = "print_bgimages";
86 static const char kPrintShrinkToFit[] = "print_shrink_to_fit";
87 static const char kPrintScaling[] = "print_scaling";
88 static const char kPrintResolution[] = "print_resolution";
89 static const char kPrintDuplex[] = "print_duplex";
91 static const char kJustLeft[] = "left";
92 static const char kJustCenter[] = "center";
93 static const char kJustRight[] = "right";
95 #define NS_PRINTER_LIST_CONTRACTID "@mozilla.org/gfx/printerlist;1"
97 nsresult nsPrintSettingsService::Init() { return NS_OK; }
99 NS_IMETHODIMP
100 nsPrintSettingsService::SerializeToPrintData(nsIPrintSettings* aSettings,
101 PrintData* data) {
102 aSettings->GetPageRanges(data->pageRanges());
104 aSettings->GetEdgeTop(&data->edgeTop());
105 aSettings->GetEdgeLeft(&data->edgeLeft());
106 aSettings->GetEdgeBottom(&data->edgeBottom());
107 aSettings->GetEdgeRight(&data->edgeRight());
109 aSettings->GetMarginTop(&data->marginTop());
110 aSettings->GetMarginLeft(&data->marginLeft());
111 aSettings->GetMarginBottom(&data->marginBottom());
112 aSettings->GetMarginRight(&data->marginRight());
113 aSettings->GetUnwriteableMarginTop(&data->unwriteableMarginTop());
114 aSettings->GetUnwriteableMarginLeft(&data->unwriteableMarginLeft());
115 aSettings->GetUnwriteableMarginBottom(&data->unwriteableMarginBottom());
116 aSettings->GetUnwriteableMarginRight(&data->unwriteableMarginRight());
118 aSettings->GetScaling(&data->scaling());
120 data->printBGColors() = aSettings->GetPrintBGColors();
121 data->printBGImages() = aSettings->GetPrintBGImages();
123 data->honorPageRuleMargins() = aSettings->GetHonorPageRuleMargins();
124 data->showMarginGuides() = aSettings->GetShowMarginGuides();
125 data->isPrintSelectionRBEnabled() = aSettings->GetIsPrintSelectionRBEnabled();
126 data->printSelectionOnly() = aSettings->GetPrintSelectionOnly();
128 aSettings->GetTitle(data->title());
129 aSettings->GetDocURL(data->docURL());
131 aSettings->GetHeaderStrLeft(data->headerStrLeft());
132 aSettings->GetHeaderStrCenter(data->headerStrCenter());
133 aSettings->GetHeaderStrRight(data->headerStrRight());
135 aSettings->GetFooterStrLeft(data->footerStrLeft());
136 aSettings->GetFooterStrCenter(data->footerStrCenter());
137 aSettings->GetFooterStrRight(data->footerStrRight());
139 aSettings->GetIsCancelled(&data->isCancelled());
140 aSettings->GetPrintSilent(&data->printSilent());
141 aSettings->GetShrinkToFit(&data->shrinkToFit());
143 aSettings->GetPaperId(data->paperId());
144 aSettings->GetPaperWidth(&data->paperWidth());
145 aSettings->GetPaperHeight(&data->paperHeight());
146 aSettings->GetPaperSizeUnit(&data->paperSizeUnit());
148 aSettings->GetPrintReversed(&data->printReversed());
149 aSettings->GetPrintInColor(&data->printInColor());
150 aSettings->GetOrientation(&data->orientation());
152 aSettings->GetNumCopies(&data->numCopies());
153 aSettings->GetNumPagesPerSheet(&data->numPagesPerSheet());
155 aSettings->GetPrinterName(data->printerName());
157 aSettings->GetPrintToFile(&data->printToFile());
159 aSettings->GetToFileName(data->toFileName());
161 aSettings->GetOutputFormat(&data->outputFormat());
162 aSettings->GetPrintPageDelay(&data->printPageDelay());
163 aSettings->GetResolution(&data->resolution());
164 aSettings->GetDuplex(&data->duplex());
165 aSettings->GetIsInitializedFromPrinter(&data->isInitializedFromPrinter());
166 aSettings->GetIsInitializedFromPrefs(&data->isInitializedFromPrefs());
168 // Initialize the platform-specific values that don't
169 // default-initialize, so that we don't send uninitialized data over
170 // IPC (which leads to valgrind warnings, and, for bools, fatal
171 // assertions).
172 // data->driverName() default-initializes
173 // data->deviceName() default-initializes
174 // data->GTKPrintSettings() default-initializes
176 return NS_OK;
179 NS_IMETHODIMP
180 nsPrintSettingsService::DeserializeToPrintSettings(const PrintData& data,
181 nsIPrintSettings* settings) {
182 nsCOMPtr<nsIPrintSession> session;
183 nsresult rv = settings->GetPrintSession(getter_AddRefs(session));
184 if (NS_SUCCEEDED(rv) && session) {
185 session->SetRemotePrintJob(
186 static_cast<RemotePrintJobChild*>(data.remotePrintJobChild()));
189 settings->SetPageRanges(data.pageRanges());
191 settings->SetEdgeTop(data.edgeTop());
192 settings->SetEdgeLeft(data.edgeLeft());
193 settings->SetEdgeBottom(data.edgeBottom());
194 settings->SetEdgeRight(data.edgeRight());
196 settings->SetMarginTop(data.marginTop());
197 settings->SetMarginLeft(data.marginLeft());
198 settings->SetMarginBottom(data.marginBottom());
199 settings->SetMarginRight(data.marginRight());
200 settings->SetUnwriteableMarginTop(data.unwriteableMarginTop());
201 settings->SetUnwriteableMarginLeft(data.unwriteableMarginLeft());
202 settings->SetUnwriteableMarginBottom(data.unwriteableMarginBottom());
203 settings->SetUnwriteableMarginRight(data.unwriteableMarginRight());
205 settings->SetScaling(data.scaling());
207 settings->SetPrintBGColors(data.printBGColors());
208 settings->SetPrintBGImages(data.printBGImages());
209 settings->SetHonorPageRuleMargins(data.honorPageRuleMargins());
210 settings->SetShowMarginGuides(data.showMarginGuides());
211 settings->SetIsPrintSelectionRBEnabled(data.isPrintSelectionRBEnabled());
212 settings->SetPrintSelectionOnly(data.printSelectionOnly());
214 settings->SetTitle(data.title());
215 settings->SetDocURL(data.docURL());
217 // Header strings...
218 settings->SetHeaderStrLeft(data.headerStrLeft());
219 settings->SetHeaderStrCenter(data.headerStrCenter());
220 settings->SetHeaderStrRight(data.headerStrRight());
222 // Footer strings...
223 settings->SetFooterStrLeft(data.footerStrLeft());
224 settings->SetFooterStrCenter(data.footerStrCenter());
225 settings->SetFooterStrRight(data.footerStrRight());
227 settings->SetIsCancelled(data.isCancelled());
228 settings->SetPrintSilent(data.printSilent());
229 settings->SetShrinkToFit(data.shrinkToFit());
231 settings->SetPaperId(data.paperId());
233 settings->SetPaperWidth(data.paperWidth());
234 settings->SetPaperHeight(data.paperHeight());
235 settings->SetPaperSizeUnit(data.paperSizeUnit());
237 settings->SetPrintReversed(data.printReversed());
238 settings->SetPrintInColor(data.printInColor());
239 settings->SetOrientation(data.orientation());
241 settings->SetNumCopies(data.numCopies());
242 settings->SetNumPagesPerSheet(data.numPagesPerSheet());
244 settings->SetPrinterName(data.printerName());
246 settings->SetPrintToFile(data.printToFile());
248 settings->SetToFileName(data.toFileName());
250 settings->SetOutputFormat(data.outputFormat());
251 settings->SetPrintPageDelay(data.printPageDelay());
252 settings->SetResolution(data.resolution());
253 settings->SetDuplex(data.duplex());
254 settings->SetIsInitializedFromPrinter(data.isInitializedFromPrinter());
255 settings->SetIsInitializedFromPrefs(data.isInitializedFromPrefs());
257 return NS_OK;
260 /** ---------------------------------------------------
261 * Helper function - Creates the "prefix" for the pref
262 * It is either "print."
263 * or "print.printer_<print name>."
265 const char* nsPrintSettingsService::GetPrefName(const char* aPrefName,
266 const nsAString& aPrinterName) {
267 if (!aPrefName || !*aPrefName) {
268 NS_ERROR("Must have a valid pref name!");
269 return aPrefName;
272 mPrefName.AssignLiteral("print.");
274 if (aPrinterName.Length()) {
275 mPrefName.AppendLiteral("printer_");
276 AppendUTF16toUTF8(aPrinterName, mPrefName);
277 mPrefName.Append('.');
279 mPrefName += aPrefName;
281 return mPrefName.get();
285 * This will either read in the generic prefs (not specific to a printer)
286 * or read the prefs in using the printer name to qualify.
287 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
289 nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
290 const nsAString& aPrinterName,
291 uint32_t aFlags) {
292 NS_ENSURE_ARG_POINTER(aPS);
294 bool noValidPrefsFound = true;
295 bool b;
296 nsAutoString str;
297 int32_t iVal;
298 double dbl;
300 #define GETBOOLPREF(_prefname, _retval) \
301 NS_SUCCEEDED( \
302 Preferences::GetBool(GetPrefName(_prefname, aPrinterName), _retval))
304 #define GETSTRPREF(_prefname, _retval) \
305 NS_SUCCEEDED( \
306 Preferences::GetString(GetPrefName(_prefname, aPrinterName), _retval))
308 #define GETINTPREF(_prefname, _retval) \
309 NS_SUCCEEDED( \
310 Preferences::GetInt(GetPrefName(_prefname, aPrinterName), _retval))
312 #define GETDBLPREF(_prefname, _retval) \
313 NS_SUCCEEDED(ReadPrefDouble(GetPrefName(_prefname, aPrinterName), _retval))
315 bool gotPaperSizeFromPrefs = false;
316 int16_t paperSizeUnit;
317 double paperWidth, paperHeight;
319 // Paper size prefs are read as a group
320 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
321 gotPaperSizeFromPrefs = GETINTPREF(kPrintPaperSizeUnit, &iVal) &&
322 GETDBLPREF(kPrintPaperWidth, paperWidth) &&
323 GETDBLPREF(kPrintPaperHeight, paperHeight) &&
324 GETSTRPREF(kPrintPaperId, str);
325 paperSizeUnit = (int16_t)iVal;
327 if (gotPaperSizeFromPrefs &&
328 paperSizeUnit != nsIPrintSettings::kPaperSizeInches &&
329 paperSizeUnit != nsIPrintSettings::kPaperSizeMillimeters) {
330 gotPaperSizeFromPrefs = false;
333 if (gotPaperSizeFromPrefs) {
334 // Bug 315687: Sanity check paper size to avoid paper size values in
335 // mm when the size unit flag is inches. The value 100 is arbitrary
336 // and can be changed.
337 gotPaperSizeFromPrefs =
338 (paperSizeUnit != nsIPrintSettings::kPaperSizeInches) ||
339 (paperWidth < 100.0) || (paperHeight < 100.0);
342 if (gotPaperSizeFromPrefs) {
343 aPS->SetPaperSizeUnit(paperSizeUnit);
344 aPS->SetPaperWidth(paperWidth);
345 aPS->SetPaperHeight(paperHeight);
346 aPS->SetPaperId(str);
347 noValidPrefsFound = false;
351 nsIntSize pageSizeInTwips; // to sanity check margins
352 if (!gotPaperSizeFromPrefs) {
353 aPS->GetPaperSizeUnit(&paperSizeUnit);
354 aPS->GetPaperWidth(&paperWidth);
355 aPS->GetPaperHeight(&paperHeight);
357 if (paperSizeUnit == nsIPrintSettings::kPaperSizeMillimeters) {
358 pageSizeInTwips = nsIntSize((int)NS_MILLIMETERS_TO_TWIPS(paperWidth),
359 (int)NS_MILLIMETERS_TO_TWIPS(paperHeight));
360 } else {
361 pageSizeInTwips = nsIntSize((int)NS_INCHES_TO_TWIPS(paperWidth),
362 (int)NS_INCHES_TO_TWIPS(paperHeight));
365 auto MarginIsOK = [&pageSizeInTwips](const nsIntMargin& aMargin) {
366 return aMargin.top >= 0 && aMargin.right >= 0 && aMargin.bottom >= 0 &&
367 aMargin.left >= 0 && aMargin.LeftRight() < pageSizeInTwips.width &&
368 aMargin.TopBottom() < pageSizeInTwips.height;
371 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
372 nsIntMargin margin;
373 bool allPrefsRead =
374 GETINTPREF(kUnwriteableMarginTopTwips, &margin.top) &&
375 GETINTPREF(kUnwriteableMarginRightTwips, &margin.right) &&
376 GETINTPREF(kUnwriteableMarginBottomTwips, &margin.bottom) &&
377 GETINTPREF(kUnwriteableMarginLeftTwips, &margin.left);
378 if (!allPrefsRead) {
379 // We failed to read the new unwritable margin twips prefs. Try to read
380 // the old ones in case they exist.
381 allPrefsRead =
382 ReadInchesIntToTwipsPref(
383 GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top) &&
384 ReadInchesIntToTwipsPref(
385 GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left) &&
386 ReadInchesIntToTwipsPref(
387 GetPrefName(kUnwriteableMarginBottom, aPrinterName),
388 margin.bottom) &&
389 ReadInchesIntToTwipsPref(
390 GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right);
392 // SetUnwriteableMarginInTwips does its own validation and drops negative
393 // values individually. We still want to block overly large values though,
394 // so we do that part of MarginIsOK manually.
395 if (allPrefsRead && margin.LeftRight() < pageSizeInTwips.width &&
396 margin.TopBottom() < pageSizeInTwips.height) {
397 aPS->SetUnwriteableMarginInTwips(margin);
398 noValidPrefsFound = false;
402 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
403 int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
404 nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
405 bool prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName),
406 margin.top);
407 prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
408 margin.left) ||
409 prefRead;
410 prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
411 margin.bottom) ||
412 prefRead;
414 prefRead = ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName),
415 margin.right) ||
416 prefRead;
418 if (prefRead && MarginIsOK(margin)) {
419 aPS->SetMarginInTwips(margin);
420 noValidPrefsFound = false;
424 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
425 nsIntMargin margin(0, 0, 0, 0);
426 bool prefRead = ReadInchesIntToTwipsPref(
427 GetPrefName(kEdgeTop, aPrinterName), margin.top);
428 prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
429 margin.left) ||
430 prefRead;
432 prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
433 margin.bottom) ||
434 prefRead;
436 prefRead = ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
437 margin.right) ||
438 prefRead;
440 if (prefRead && MarginIsOK(margin)) {
441 aPS->SetEdgeInTwips(margin);
442 noValidPrefsFound = false;
446 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
447 if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
448 aPS->SetHeaderStrLeft(str);
449 noValidPrefsFound = false;
453 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
454 if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
455 aPS->SetHeaderStrCenter(str);
456 noValidPrefsFound = false;
460 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
461 if (GETSTRPREF(kPrintHeaderStrRight, str)) {
462 aPS->SetHeaderStrRight(str);
463 noValidPrefsFound = false;
467 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
468 if (GETSTRPREF(kPrintFooterStrLeft, str)) {
469 aPS->SetFooterStrLeft(str);
470 noValidPrefsFound = false;
474 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
475 if (GETSTRPREF(kPrintFooterStrCenter, str)) {
476 aPS->SetFooterStrCenter(str);
477 noValidPrefsFound = false;
481 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
482 if (GETSTRPREF(kPrintFooterStrRight, str)) {
483 aPS->SetFooterStrRight(str);
484 noValidPrefsFound = false;
488 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
489 if (GETBOOLPREF(kPrintBGColors, &b)) {
490 aPS->SetPrintBGColors(b);
491 noValidPrefsFound = false;
495 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
496 if (GETBOOLPREF(kPrintBGImages, &b)) {
497 aPS->SetPrintBGImages(b);
498 noValidPrefsFound = false;
502 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
503 if (GETBOOLPREF(kPrintReversed, &b)) {
504 aPS->SetPrintReversed(b);
505 noValidPrefsFound = false;
509 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
510 if (GETBOOLPREF(kPrintInColor, &b)) {
511 aPS->SetPrintInColor(b);
512 noValidPrefsFound = false;
516 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
517 if (GETINTPREF(kPrintOrientation, &iVal) &&
518 (iVal == nsIPrintSettings::kPortraitOrientation ||
519 iVal == nsIPrintSettings::kLandscapeOrientation)) {
520 aPS->SetOrientation(iVal);
521 noValidPrefsFound = false;
525 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
526 if (GETBOOLPREF(kPrintToFile, &b)) {
527 aPS->SetPrintToFile(b);
528 noValidPrefsFound = false;
532 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
533 if (GETSTRPREF(kPrintToFileName, str)) {
534 if (StringEndsWith(str, u".ps"_ns)) {
535 // We only support PDF since bug 1425188 landed. Users may still have
536 // prefs with .ps filenames if they last saved a file as Postscript
537 // though, so we fix that up here. (The pref values will be
538 // overwritten the next time they save to file as a PDF.)
539 str.Truncate(str.Length() - 2);
540 str.AppendLiteral("pdf");
542 aPS->SetToFileName(str);
543 noValidPrefsFound = false;
547 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
548 // milliseconds
549 if (GETINTPREF(kPrintPageDelay, &iVal) && iVal >= 0 && iVal <= 1000) {
550 aPS->SetPrintPageDelay(iVal);
551 noValidPrefsFound = false;
555 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
556 if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
557 aPS->SetShrinkToFit(b);
558 noValidPrefsFound = false;
562 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
563 // The limits imposed here are fairly arbitrary and mainly intended to
564 // purge bad values which tend to be negative and/or very large. If we
565 // get complaints from users that settings outside these values "aren't
566 // saved" then we can consider increasing them.
567 if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
568 aPS->SetScaling(dbl);
569 noValidPrefsFound = false;
573 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
574 // DPI. Again, an arbitrary range mainly to purge bad values that have made
575 // their way into user prefs.
576 if (GETINTPREF(kPrintResolution, &iVal) && iVal >= 50 && iVal <= 12000) {
577 aPS->SetResolution(iVal);
578 noValidPrefsFound = false;
582 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
583 if (GETINTPREF(kPrintDuplex, &iVal)) {
584 aPS->SetDuplex(iVal);
585 noValidPrefsFound = false;
589 // Not Reading In:
590 // Number of Copies
592 return noValidPrefsFound ? NS_ERROR_NOT_AVAILABLE : NS_OK;
595 nsresult nsPrintSettingsService::WritePrefs(nsIPrintSettings* aPS,
596 const nsAString& aPrinterName,
597 uint32_t aFlags) {
598 NS_ENSURE_ARG_POINTER(aPS);
600 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
601 nsIntMargin margin = aPS->GetMarginInTwips();
602 WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top);
603 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
604 margin.left);
605 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
606 margin.bottom);
607 WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
608 margin.right);
611 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
612 nsIntMargin edge = aPS->GetEdgeInTwips();
613 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), edge.top);
614 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
615 edge.left);
616 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
617 edge.bottom);
618 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
619 edge.right);
622 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
623 nsIntMargin unwriteableMargin = aPS->GetUnwriteableMarginInTwips();
624 Preferences::SetInt(GetPrefName(kUnwriteableMarginTopTwips, aPrinterName),
625 unwriteableMargin.top);
626 Preferences::SetInt(GetPrefName(kUnwriteableMarginLeftTwips, aPrinterName),
627 unwriteableMargin.left);
628 Preferences::SetInt(
629 GetPrefName(kUnwriteableMarginBottomTwips, aPrinterName),
630 unwriteableMargin.bottom);
631 Preferences::SetInt(GetPrefName(kUnwriteableMarginRightTwips, aPrinterName),
632 unwriteableMargin.right);
634 // Remove the old unwriteableMargin prefs.
635 Preferences::ClearUser(GetPrefName(kUnwriteableMarginTop, aPrinterName));
636 Preferences::ClearUser(GetPrefName(kUnwriteableMarginRight, aPrinterName));
637 Preferences::ClearUser(GetPrefName(kUnwriteableMarginBottom, aPrinterName));
638 Preferences::ClearUser(GetPrefName(kUnwriteableMarginLeft, aPrinterName));
641 // Paper size prefs are saved as a group
642 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
643 int16_t sizeUnit;
644 double width, height;
645 nsString name;
647 if (NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) &&
648 NS_SUCCEEDED(aPS->GetPaperWidth(&width)) &&
649 NS_SUCCEEDED(aPS->GetPaperHeight(&height)) &&
650 NS_SUCCEEDED(aPS->GetPaperId(name))) {
651 Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
652 int32_t(sizeUnit));
653 WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width);
654 WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height);
655 Preferences::SetString(GetPrefName(kPrintPaperId, aPrinterName), name);
659 bool b;
660 nsString uStr;
661 int32_t iVal;
662 double dbl;
664 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
665 if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(uStr))) {
666 Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
667 uStr);
671 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
672 if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(uStr))) {
673 Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
674 uStr);
678 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
679 if (NS_SUCCEEDED(aPS->GetHeaderStrRight(uStr))) {
680 Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
681 uStr);
685 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
686 if (NS_SUCCEEDED(aPS->GetFooterStrLeft(uStr))) {
687 Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
688 uStr);
692 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
693 if (NS_SUCCEEDED(aPS->GetFooterStrCenter(uStr))) {
694 Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
695 uStr);
699 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
700 if (NS_SUCCEEDED(aPS->GetFooterStrRight(uStr))) {
701 Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
702 uStr);
706 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
707 b = aPS->GetPrintBGColors();
708 Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
711 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
712 b = aPS->GetPrintBGImages();
713 Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
716 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
717 if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
718 Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
722 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
723 if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
724 Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
728 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
729 if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
730 Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
734 // Only the general version of this pref is saved
735 if ((aFlags & nsIPrintSettings::kInitSavePrinterName) &&
736 aPrinterName.IsEmpty()) {
737 if (NS_SUCCEEDED(aPS->GetPrinterName(uStr))) {
738 Preferences::SetString(kPrinterName, uStr);
742 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
743 if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
744 Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b);
748 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
749 if (NS_SUCCEEDED(aPS->GetToFileName(uStr))) {
750 Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr);
754 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
755 if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
756 Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
760 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
761 if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
762 Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
766 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
767 if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
768 WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
772 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
773 if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) {
774 Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal);
778 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
779 if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) {
780 Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal);
784 // Not Writing Out:
785 // Number of Copies
787 return NS_OK;
790 NS_IMETHODIMP
791 nsPrintSettingsService::GetDefaultPrintSettingsForPrinting(
792 nsIPrintSettings** aPrintSettings) {
793 nsresult rv = GetNewPrintSettings(aPrintSettings);
794 NS_ENSURE_SUCCESS(rv, rv);
796 nsIPrintSettings* settings = *aPrintSettings;
798 nsAutoString printerName;
799 settings->GetPrinterName(printerName);
800 if (printerName.IsEmpty()) {
801 GetLastUsedPrinterName(printerName);
802 settings->SetPrinterName(printerName);
804 InitPrintSettingsFromPrinter(printerName, settings);
805 InitPrintSettingsFromPrefs(settings, true, nsIPrintSettings::kInitSaveAll);
806 return NS_OK;
809 NS_IMETHODIMP
810 nsPrintSettingsService::GetNewPrintSettings(
811 nsIPrintSettings** aNewPrintSettings) {
812 return _CreatePrintSettings(aNewPrintSettings);
815 NS_IMETHODIMP
816 nsPrintSettingsService::GetLastUsedPrinterName(
817 nsAString& aLastUsedPrinterName) {
818 aLastUsedPrinterName.Truncate();
819 Preferences::GetString(kPrinterName, aLastUsedPrinterName);
820 return NS_OK;
823 NS_IMETHODIMP
824 nsPrintSettingsService::InitPrintSettingsFromPrinter(
825 const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
826 // Don't get print settings from the printer in the child when printing via
827 // parent, these will be retrieved in the parent later in the print process.
828 if (XRE_IsContentProcess() && StaticPrefs::print_print_via_parent()) {
829 return NS_OK;
832 NS_ENSURE_ARG_POINTER(aPrintSettings);
834 #ifdef DEBUG
835 nsString printerName;
836 aPrintSettings->GetPrinterName(printerName);
837 if (!printerName.Equals(aPrinterName)) {
838 NS_WARNING("Printer names should match!");
840 #endif
842 bool isInitialized;
843 aPrintSettings->GetIsInitializedFromPrinter(&isInitialized);
844 if (isInitialized) return NS_OK;
846 nsresult rv;
847 nsCOMPtr<nsIPrinterList> printerList =
848 do_GetService(NS_PRINTER_LIST_CONTRACTID, &rv);
849 NS_ENSURE_SUCCESS(rv, rv);
851 rv = printerList->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings);
852 NS_ENSURE_SUCCESS(rv, rv);
854 aPrintSettings->SetIsInitializedFromPrinter(true);
855 return rv;
858 /** ---------------------------------------------------
859 * Helper function - Returns either the name or sets the length to zero
861 static nsresult GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP,
862 nsAString& aPrinterName) {
863 NS_ENSURE_ARG_POINTER(aPS);
865 aPrinterName.Truncate();
866 if (!aUsePNP) return NS_OK;
868 // Get the Printer Name from the PrintSettings
869 // to use as a prefix for Pref Names
870 nsresult rv = aPS->GetPrinterName(aPrinterName);
871 NS_ENSURE_SUCCESS(rv, rv);
873 // Convert any whitespaces, carriage returns or newlines to _
874 // The below algorithm is supposedly faster than using iterators
875 constexpr auto replSubstr = u"_"_ns;
876 const char* replaceStr = " \n\r";
878 int32_t x;
879 for (x = 0; x < (int32_t)strlen(replaceStr); x++) {
880 char16_t uChar = replaceStr[x];
882 int32_t i = 0;
883 while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) {
884 aPrinterName.Replace(i, 1, replSubstr);
885 i++;
888 return NS_OK;
891 NS_IMETHODIMP
892 nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
893 bool aUsePNP,
894 uint32_t aFlags) {
895 NS_ENSURE_ARG_POINTER(aPS);
897 bool isInitialized;
898 aPS->GetIsInitializedFromPrefs(&isInitialized);
900 if (isInitialized) {
901 return NS_OK;
904 auto globalPrintSettings = aFlags;
905 #ifndef MOZ_WIDGET_ANDROID
906 globalPrintSettings &= nsIPrintSettings::kGlobalSettings;
907 #endif
909 nsAutoString prtName;
910 // read any non printer specific prefs
911 // with empty printer name
912 nsresult rv = ReadPrefs(aPS, prtName, globalPrintSettings);
913 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) {
914 NS_WARNING("ReadPrefs failed");
917 // Get the Printer Name from the PrintSettings to use as a prefix for Pref
918 // Names
919 rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
920 NS_ENSURE_SUCCESS(rv, rv);
922 if (prtName.IsEmpty()) {
923 NS_WARNING("Caller should supply a printer name.");
924 return NS_OK;
927 // Now read any printer specific prefs
928 rv = ReadPrefs(aPS, prtName, aFlags);
929 if (NS_SUCCEEDED(rv)) {
930 aPS->SetIsInitializedFromPrefs(true);
933 return NS_OK;
937 * Save all of the printer settings; if we can find a printer name, save
938 * printer-specific preferences. Otherwise, save generic ones.
940 nsresult nsPrintSettingsService::SavePrintSettingsToPrefs(
941 nsIPrintSettings* aPS, bool aUsePrinterNamePrefix, uint32_t aFlags) {
942 NS_ENSURE_ARG_POINTER(aPS);
944 if (GeckoProcessType_Content == XRE_GetProcessType()) {
945 // If we're in the content process, we can't directly write to the
946 // Preferences service - we have to proxy the save up to the
947 // parent process.
948 RefPtr<nsPrintingProxy> proxy = nsPrintingProxy::GetInstance();
949 return proxy->SavePrintSettings(aPS, aUsePrinterNamePrefix, aFlags);
952 nsAutoString prtName;
954 // Get the printer name from the PrinterSettings for an optional prefix.
955 nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName);
956 NS_ENSURE_SUCCESS(rv, rv);
958 // Write the prefs, with or without a printer name prefix.
959 return WritePrefs(aPS, prtName, aFlags);
962 //-----------------------------------------------------
963 //-- Protected Methods --------------------------------
964 //-----------------------------------------------------
965 nsresult nsPrintSettingsService::ReadPrefDouble(const char* aPrefId,
966 double& aVal) {
967 NS_ENSURE_ARG_POINTER(aPrefId);
969 nsAutoCString str;
970 nsresult rv = Preferences::GetCString(aPrefId, str);
971 if (NS_FAILED(rv) || str.IsEmpty()) {
972 return NS_ERROR_NOT_AVAILABLE;
975 double value = str.ToDouble(&rv);
976 if (NS_FAILED(rv)) {
977 return rv;
980 aVal = value;
981 return NS_OK;
984 nsresult nsPrintSettingsService::WritePrefDouble(const char* aPrefId,
985 double aVal) {
986 NS_ENSURE_ARG_POINTER(aPrefId);
988 nsAutoCString str;
989 str.AppendFloat(aVal);
990 return Preferences::SetCString(aPrefId, str);
993 bool nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId,
994 int32_t& aTwips) {
995 nsAutoString str;
996 nsresult rv = Preferences::GetString(aPrefId, str);
997 if (NS_FAILED(rv) || str.IsEmpty()) {
998 return false;
1001 float inches = str.ToFloat(&rv);
1002 if (NS_FAILED(rv)) {
1003 return false;
1006 aTwips = NS_INCHES_TO_INT_TWIPS(inches);
1007 return true;
1010 void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId,
1011 int32_t aTwips) {
1012 double inches = NS_TWIPS_TO_INCHES(aTwips);
1013 nsAutoCString inchesStr;
1014 inchesStr.AppendFloat(inches);
1016 Preferences::SetCString(aPrefId, inchesStr);
1019 bool nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId,
1020 int32_t& aTwips) {
1021 int32_t value;
1022 nsresult rv = Preferences::GetInt(aPrefId, &value);
1023 if (NS_FAILED(rv)) {
1024 return false;
1027 aTwips = NS_INCHES_TO_INT_TWIPS(float(value) / 100.0f);
1028 return true;
1031 void nsPrintSettingsService::WriteInchesIntFromTwipsPref(const char* aPrefId,
1032 int32_t aTwips) {
1033 Preferences::SetInt(aPrefId,
1034 int32_t(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f));
1037 void nsPrintSettingsService::ReadJustification(const char* aPrefId,
1038 int16_t& aJust,
1039 int16_t aInitValue) {
1040 aJust = aInitValue;
1041 nsAutoString justStr;
1042 if (NS_SUCCEEDED(Preferences::GetString(aPrefId, justStr))) {
1043 if (justStr.EqualsASCII(kJustRight)) {
1044 aJust = nsIPrintSettings::kJustRight;
1045 } else if (justStr.EqualsASCII(kJustCenter)) {
1046 aJust = nsIPrintSettings::kJustCenter;
1047 } else {
1048 aJust = nsIPrintSettings::kJustLeft;
1053 //---------------------------------------------------
1054 void nsPrintSettingsService::WriteJustification(const char* aPrefId,
1055 int16_t aJust) {
1056 switch (aJust) {
1057 case nsIPrintSettings::kJustLeft:
1058 Preferences::SetCString(aPrefId, kJustLeft);
1059 break;
1061 case nsIPrintSettings::kJustCenter:
1062 Preferences::SetCString(aPrefId, kJustCenter);
1063 break;
1065 case nsIPrintSettings::kJustRight:
1066 Preferences::SetCString(aPrefId, kJustRight);
1067 break;
1068 } // switch