Bug 1726647 [wpt PR 30094] - Ignore exceptions as some command is not implemented...
[gecko.git] / widget / nsPrintSettingsService.cpp
blobeb08b3aaa4211c6a23f49c4bec9744ab18a108f2
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";
46 static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top";
47 static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left";
48 static const char kUnwriteableMarginBottom[] =
49 "print_unwriteable_margin_bottom";
50 static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
52 // Prefs for Print Options
53 static const char kPrintHeaderStrLeft[] = "print_headerleft";
54 static const char kPrintHeaderStrCenter[] = "print_headercenter";
55 static const char kPrintHeaderStrRight[] = "print_headerright";
56 static const char kPrintFooterStrLeft[] = "print_footerleft";
57 static const char kPrintFooterStrCenter[] = "print_footercenter";
58 static const char kPrintFooterStrRight[] = "print_footerright";
60 // Additional Prefs
61 static const char kPrintReversed[] = "print_reversed";
62 static const char kPrintInColor[] = "print_in_color";
63 static const char kPrintPaperId[] = "print_paper_id";
64 static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
65 static const char kPrintPaperWidth[] = "print_paper_width";
66 static const char kPrintPaperHeight[] = "print_paper_height";
67 static const char kPrintOrientation[] = "print_orientation";
68 static const char kPrinterName[] = "print_printer";
69 static const char kPrintToFile[] = "print_to_file";
70 static const char kPrintToFileName[] = "print_to_filename";
71 static const char kPrintPageDelay[] = "print_page_delay";
72 static const char kPrintBGColors[] = "print_bgcolor";
73 static const char kPrintBGImages[] = "print_bgimages";
74 static const char kPrintShrinkToFit[] = "print_shrink_to_fit";
75 static const char kPrintScaling[] = "print_scaling";
76 static const char kPrintResolution[] = "print_resolution";
77 static const char kPrintDuplex[] = "print_duplex";
79 static const char kJustLeft[] = "left";
80 static const char kJustCenter[] = "center";
81 static const char kJustRight[] = "right";
83 #define NS_PRINTER_LIST_CONTRACTID "@mozilla.org/gfx/printerlist;1"
85 nsresult nsPrintSettingsService::Init() { return NS_OK; }
87 NS_IMETHODIMP
88 nsPrintSettingsService::SerializeToPrintData(nsIPrintSettings* aSettings,
89 PrintData* data) {
90 aSettings->GetPageRanges(data->pageRanges());
92 aSettings->GetEdgeTop(&data->edgeTop());
93 aSettings->GetEdgeLeft(&data->edgeLeft());
94 aSettings->GetEdgeBottom(&data->edgeBottom());
95 aSettings->GetEdgeRight(&data->edgeRight());
97 aSettings->GetMarginTop(&data->marginTop());
98 aSettings->GetMarginLeft(&data->marginLeft());
99 aSettings->GetMarginBottom(&data->marginBottom());
100 aSettings->GetMarginRight(&data->marginRight());
101 aSettings->GetUnwriteableMarginTop(&data->unwriteableMarginTop());
102 aSettings->GetUnwriteableMarginLeft(&data->unwriteableMarginLeft());
103 aSettings->GetUnwriteableMarginBottom(&data->unwriteableMarginBottom());
104 aSettings->GetUnwriteableMarginRight(&data->unwriteableMarginRight());
106 aSettings->GetScaling(&data->scaling());
108 data->printBGColors() = aSettings->GetPrintBGColors();
109 data->printBGImages() = aSettings->GetPrintBGImages();
111 data->honorPageRuleMargins() = aSettings->GetHonorPageRuleMargins();
112 data->showMarginGuides() = aSettings->GetShowMarginGuides();
113 data->isPrintSelectionRBEnabled() = aSettings->GetIsPrintSelectionRBEnabled();
114 data->printSelectionOnly() = aSettings->GetPrintSelectionOnly();
116 aSettings->GetTitle(data->title());
117 aSettings->GetDocURL(data->docURL());
119 aSettings->GetHeaderStrLeft(data->headerStrLeft());
120 aSettings->GetHeaderStrCenter(data->headerStrCenter());
121 aSettings->GetHeaderStrRight(data->headerStrRight());
123 aSettings->GetFooterStrLeft(data->footerStrLeft());
124 aSettings->GetFooterStrCenter(data->footerStrCenter());
125 aSettings->GetFooterStrRight(data->footerStrRight());
127 aSettings->GetIsCancelled(&data->isCancelled());
128 aSettings->GetPrintSilent(&data->printSilent());
129 aSettings->GetShrinkToFit(&data->shrinkToFit());
130 aSettings->GetShowPrintProgress(&data->showPrintProgress());
132 aSettings->GetPaperId(data->paperId());
133 aSettings->GetPaperWidth(&data->paperWidth());
134 aSettings->GetPaperHeight(&data->paperHeight());
135 aSettings->GetPaperSizeUnit(&data->paperSizeUnit());
137 aSettings->GetPrintReversed(&data->printReversed());
138 aSettings->GetPrintInColor(&data->printInColor());
139 aSettings->GetOrientation(&data->orientation());
141 aSettings->GetNumCopies(&data->numCopies());
142 aSettings->GetNumPagesPerSheet(&data->numPagesPerSheet());
144 aSettings->GetPrinterName(data->printerName());
146 aSettings->GetPrintToFile(&data->printToFile());
148 aSettings->GetToFileName(data->toFileName());
150 aSettings->GetOutputFormat(&data->outputFormat());
151 aSettings->GetPrintPageDelay(&data->printPageDelay());
152 aSettings->GetResolution(&data->resolution());
153 aSettings->GetDuplex(&data->duplex());
154 aSettings->GetIsInitializedFromPrinter(&data->isInitializedFromPrinter());
155 aSettings->GetIsInitializedFromPrefs(&data->isInitializedFromPrefs());
157 // Initialize the platform-specific values that don't
158 // default-initialize, so that we don't send uninitialized data over
159 // IPC (which leads to valgrind warnings, and, for bools, fatal
160 // assertions).
161 // data->driverName() default-initializes
162 // data->deviceName() default-initializes
163 // data->GTKPrintSettings() default-initializes
165 return NS_OK;
168 NS_IMETHODIMP
169 nsPrintSettingsService::DeserializeToPrintSettings(const PrintData& data,
170 nsIPrintSettings* settings) {
171 nsCOMPtr<nsIPrintSession> session;
172 nsresult rv = settings->GetPrintSession(getter_AddRefs(session));
173 if (NS_SUCCEEDED(rv) && session) {
174 session->SetRemotePrintJob(
175 static_cast<RemotePrintJobChild*>(data.remotePrintJobChild()));
178 settings->SetPageRanges(data.pageRanges());
180 settings->SetEdgeTop(data.edgeTop());
181 settings->SetEdgeLeft(data.edgeLeft());
182 settings->SetEdgeBottom(data.edgeBottom());
183 settings->SetEdgeRight(data.edgeRight());
185 settings->SetMarginTop(data.marginTop());
186 settings->SetMarginLeft(data.marginLeft());
187 settings->SetMarginBottom(data.marginBottom());
188 settings->SetMarginRight(data.marginRight());
189 settings->SetUnwriteableMarginTop(data.unwriteableMarginTop());
190 settings->SetUnwriteableMarginLeft(data.unwriteableMarginLeft());
191 settings->SetUnwriteableMarginBottom(data.unwriteableMarginBottom());
192 settings->SetUnwriteableMarginRight(data.unwriteableMarginRight());
194 settings->SetScaling(data.scaling());
196 settings->SetPrintBGColors(data.printBGColors());
197 settings->SetPrintBGImages(data.printBGImages());
198 settings->SetHonorPageRuleMargins(data.honorPageRuleMargins());
199 settings->SetShowMarginGuides(data.showMarginGuides());
200 settings->SetIsPrintSelectionRBEnabled(data.isPrintSelectionRBEnabled());
201 settings->SetPrintSelectionOnly(data.printSelectionOnly());
203 settings->SetTitle(data.title());
204 settings->SetDocURL(data.docURL());
206 // Header strings...
207 settings->SetHeaderStrLeft(data.headerStrLeft());
208 settings->SetHeaderStrCenter(data.headerStrCenter());
209 settings->SetHeaderStrRight(data.headerStrRight());
211 // Footer strings...
212 settings->SetFooterStrLeft(data.footerStrLeft());
213 settings->SetFooterStrCenter(data.footerStrCenter());
214 settings->SetFooterStrRight(data.footerStrRight());
216 settings->SetIsCancelled(data.isCancelled());
217 settings->SetPrintSilent(data.printSilent());
218 settings->SetShrinkToFit(data.shrinkToFit());
219 settings->SetShowPrintProgress(data.showPrintProgress());
221 settings->SetPaperId(data.paperId());
223 settings->SetPaperWidth(data.paperWidth());
224 settings->SetPaperHeight(data.paperHeight());
225 settings->SetPaperSizeUnit(data.paperSizeUnit());
227 settings->SetPrintReversed(data.printReversed());
228 settings->SetPrintInColor(data.printInColor());
229 settings->SetOrientation(data.orientation());
231 settings->SetNumCopies(data.numCopies());
232 settings->SetNumPagesPerSheet(data.numPagesPerSheet());
234 settings->SetPrinterName(data.printerName());
236 settings->SetPrintToFile(data.printToFile());
238 settings->SetToFileName(data.toFileName());
240 settings->SetOutputFormat(data.outputFormat());
241 settings->SetPrintPageDelay(data.printPageDelay());
242 settings->SetResolution(data.resolution());
243 settings->SetDuplex(data.duplex());
244 settings->SetIsInitializedFromPrinter(data.isInitializedFromPrinter());
245 settings->SetIsInitializedFromPrefs(data.isInitializedFromPrefs());
247 return NS_OK;
250 /** ---------------------------------------------------
251 * Helper function - Creates the "prefix" for the pref
252 * It is either "print."
253 * or "print.printer_<print name>."
255 const char* nsPrintSettingsService::GetPrefName(const char* aPrefName,
256 const nsAString& aPrinterName) {
257 if (!aPrefName || !*aPrefName) {
258 NS_ERROR("Must have a valid pref name!");
259 return aPrefName;
262 mPrefName.AssignLiteral("print.");
264 if (aPrinterName.Length()) {
265 mPrefName.AppendLiteral("printer_");
266 AppendUTF16toUTF8(aPrinterName, mPrefName);
267 mPrefName.Append('.');
269 mPrefName += aPrefName;
271 return mPrefName.get();
275 * This will either read in the generic prefs (not specific to a printer)
276 * or read the prefs in using the printer name to qualify.
277 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
279 nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
280 const nsAString& aPrinterName,
281 uint32_t aFlags) {
282 NS_ENSURE_ARG_POINTER(aPS);
284 bool b;
285 nsAutoString str;
286 int32_t iVal;
287 double dbl;
289 #define GETBOOLPREF(_prefname, _retval) \
290 NS_SUCCEEDED( \
291 Preferences::GetBool(GetPrefName(_prefname, aPrinterName), _retval))
293 #define GETSTRPREF(_prefname, _retval) \
294 NS_SUCCEEDED( \
295 Preferences::GetString(GetPrefName(_prefname, aPrinterName), _retval))
297 #define GETINTPREF(_prefname, _retval) \
298 NS_SUCCEEDED( \
299 Preferences::GetInt(GetPrefName(_prefname, aPrinterName), _retval))
301 #define GETDBLPREF(_prefname, _retval) \
302 NS_SUCCEEDED(ReadPrefDouble(GetPrefName(_prefname, aPrinterName), _retval))
304 bool gotPaperSizeFromPrefs = false;
305 int16_t paperSizeUnit;
306 double paperWidth, paperHeight;
308 // Paper size prefs are read as a group
309 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
310 gotPaperSizeFromPrefs = GETINTPREF(kPrintPaperSizeUnit, &iVal) &&
311 GETDBLPREF(kPrintPaperWidth, paperWidth) &&
312 GETDBLPREF(kPrintPaperHeight, paperHeight) &&
313 GETSTRPREF(kPrintPaperId, str);
314 paperSizeUnit = (int16_t)iVal;
316 if (gotPaperSizeFromPrefs &&
317 paperSizeUnit != nsIPrintSettings::kPaperSizeInches &&
318 paperSizeUnit != nsIPrintSettings::kPaperSizeMillimeters) {
319 gotPaperSizeFromPrefs = false;
322 if (gotPaperSizeFromPrefs) {
323 // Bug 315687: Sanity check paper size to avoid paper size values in
324 // mm when the size unit flag is inches. The value 100 is arbitrary
325 // and can be changed.
326 gotPaperSizeFromPrefs =
327 (paperSizeUnit != nsIPrintSettings::kPaperSizeInches) ||
328 (paperWidth < 100.0) || (paperHeight < 100.0);
331 if (gotPaperSizeFromPrefs) {
332 aPS->SetPaperSizeUnit(paperSizeUnit);
333 aPS->SetPaperWidth(paperWidth);
334 aPS->SetPaperHeight(paperHeight);
335 aPS->SetPaperId(str);
339 nsIntSize pageSizeInTwips; // to sanity check margins
340 if (!gotPaperSizeFromPrefs) {
341 aPS->GetPaperSizeUnit(&paperSizeUnit);
342 aPS->GetPaperWidth(&paperWidth);
343 aPS->GetPaperHeight(&paperHeight);
345 if (paperSizeUnit == nsIPrintSettings::kPaperSizeMillimeters) {
346 pageSizeInTwips = nsIntSize((int)NS_MILLIMETERS_TO_TWIPS(paperWidth),
347 (int)NS_MILLIMETERS_TO_TWIPS(paperHeight));
348 } else {
349 pageSizeInTwips = nsIntSize((int)NS_INCHES_TO_TWIPS(paperWidth),
350 (int)NS_INCHES_TO_TWIPS(paperHeight));
353 auto MarginIsOK = [&pageSizeInTwips](const nsIntMargin& aMargin) {
354 return aMargin.top >= 0 && aMargin.right >= 0 && aMargin.bottom >= 0 &&
355 aMargin.left >= 0 && aMargin.LeftRight() < pageSizeInTwips.width &&
356 aMargin.TopBottom() < pageSizeInTwips.height;
359 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
360 nsIntMargin margin;
361 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName),
362 margin.top, kUnwriteableMarginTop);
363 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName),
364 margin.left, kUnwriteableMarginLeft);
365 ReadInchesIntToTwipsPref(
366 GetPrefName(kUnwriteableMarginBottom, aPrinterName), margin.bottom,
367 kUnwriteableMarginBottom);
368 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName),
369 margin.right, kUnwriteableMarginRight);
370 // SetUnwriteableMarginInTwips does its own validation and drops negative
371 // values individually. We still want to block overly large values though,
372 // so we do that part of MarginIsOK manually.
373 if (margin.LeftRight() < pageSizeInTwips.width &&
374 margin.TopBottom() < pageSizeInTwips.height) {
375 aPS->SetUnwriteableMarginInTwips(margin);
379 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
380 int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
381 nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
382 ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top,
383 kMarginTop);
384 ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left,
385 kMarginLeft);
386 ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
387 margin.bottom, kMarginBottom);
388 ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right,
389 kMarginRight);
390 if (MarginIsOK(margin)) {
391 aPS->SetMarginInTwips(margin);
395 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
396 nsIntMargin margin(0, 0, 0, 0);
397 ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top,
398 kEdgeTop);
399 ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left,
400 kEdgeLeft);
401 ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
402 margin.bottom, kEdgeBottom);
403 ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
404 margin.right, kEdgeRight);
405 if (MarginIsOK(margin)) {
406 aPS->SetEdgeInTwips(margin);
410 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
411 if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
412 aPS->SetHeaderStrLeft(str);
416 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
417 if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
418 aPS->SetHeaderStrCenter(str);
422 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
423 if (GETSTRPREF(kPrintHeaderStrRight, str)) {
424 aPS->SetHeaderStrRight(str);
428 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
429 if (GETSTRPREF(kPrintFooterStrLeft, str)) {
430 aPS->SetFooterStrLeft(str);
434 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
435 if (GETSTRPREF(kPrintFooterStrCenter, str)) {
436 aPS->SetFooterStrCenter(str);
440 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
441 if (GETSTRPREF(kPrintFooterStrRight, str)) {
442 aPS->SetFooterStrRight(str);
446 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
447 if (GETBOOLPREF(kPrintBGColors, &b)) {
448 aPS->SetPrintBGColors(b);
452 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
453 if (GETBOOLPREF(kPrintBGImages, &b)) {
454 aPS->SetPrintBGImages(b);
458 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
459 if (GETBOOLPREF(kPrintReversed, &b)) {
460 aPS->SetPrintReversed(b);
464 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
465 if (GETBOOLPREF(kPrintInColor, &b)) {
466 aPS->SetPrintInColor(b);
470 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
471 if (GETINTPREF(kPrintOrientation, &iVal) &&
472 (iVal == nsIPrintSettings::kPortraitOrientation ||
473 iVal == nsIPrintSettings::kLandscapeOrientation)) {
474 aPS->SetOrientation(iVal);
478 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
479 if (GETBOOLPREF(kPrintToFile, &b)) {
480 aPS->SetPrintToFile(b);
484 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
485 if (GETSTRPREF(kPrintToFileName, str)) {
486 if (StringEndsWith(str, u".ps"_ns)) {
487 // We only support PDF since bug 1425188 landed. Users may still have
488 // prefs with .ps filenames if they last saved a file as Postscript
489 // though, so we fix that up here. (The pref values will be
490 // overwritten the next time they save to file as a PDF.)
491 str.Truncate(str.Length() - 2);
492 str.AppendLiteral("pdf");
494 aPS->SetToFileName(str);
498 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
499 // milliseconds
500 if (GETINTPREF(kPrintPageDelay, &iVal) && iVal >= 0 && iVal <= 1000) {
501 aPS->SetPrintPageDelay(iVal);
505 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
506 if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
507 aPS->SetShrinkToFit(b);
511 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
512 // The limits imposed here are fairly arbitrary and mainly intended to
513 // purge bad values which tend to be negative and/or very large. If we
514 // get complaints from users that settings outside these values "aren't
515 // saved" then we can consider increasing them.
516 if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
517 aPS->SetScaling(dbl);
521 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
522 // DPI. Again, an arbitrary range mainly to purge bad values that have made
523 // their way into user prefs.
524 if (GETINTPREF(kPrintResolution, &iVal) && iVal >= 50 && iVal <= 12000) {
525 aPS->SetResolution(iVal);
529 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
530 if (GETINTPREF(kPrintDuplex, &iVal)) {
531 aPS->SetDuplex(iVal);
535 // Not Reading In:
536 // Number of Copies
538 return NS_OK;
541 nsresult nsPrintSettingsService::WritePrefs(nsIPrintSettings* aPS,
542 const nsAString& aPrinterName,
543 uint32_t aFlags) {
544 NS_ENSURE_ARG_POINTER(aPS);
546 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
547 nsIntMargin margin = aPS->GetMarginInTwips();
548 WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top);
549 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
550 margin.left);
551 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
552 margin.bottom);
553 WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
554 margin.right);
557 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
558 nsIntMargin edge = aPS->GetEdgeInTwips();
559 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), edge.top);
560 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
561 edge.left);
562 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
563 edge.bottom);
564 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
565 edge.right);
568 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
569 nsIntMargin unwriteableMargin = aPS->GetUnwriteableMarginInTwips();
570 WriteInchesIntFromTwipsPref(
571 GetPrefName(kUnwriteableMarginTop, aPrinterName),
572 unwriteableMargin.top);
573 WriteInchesIntFromTwipsPref(
574 GetPrefName(kUnwriteableMarginLeft, aPrinterName),
575 unwriteableMargin.left);
576 WriteInchesIntFromTwipsPref(
577 GetPrefName(kUnwriteableMarginBottom, aPrinterName),
578 unwriteableMargin.bottom);
579 WriteInchesIntFromTwipsPref(
580 GetPrefName(kUnwriteableMarginRight, aPrinterName),
581 unwriteableMargin.right);
584 // Paper size prefs are saved as a group
585 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
586 int16_t sizeUnit;
587 double width, height;
588 nsString name;
590 if (NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) &&
591 NS_SUCCEEDED(aPS->GetPaperWidth(&width)) &&
592 NS_SUCCEEDED(aPS->GetPaperHeight(&height)) &&
593 NS_SUCCEEDED(aPS->GetPaperId(name))) {
594 Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
595 int32_t(sizeUnit));
596 WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width);
597 WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height);
598 Preferences::SetString(GetPrefName(kPrintPaperId, aPrinterName), name);
602 bool b;
603 nsString uStr;
604 int32_t iVal;
605 double dbl;
607 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
608 if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(uStr))) {
609 Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
610 uStr);
614 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
615 if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(uStr))) {
616 Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
617 uStr);
621 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
622 if (NS_SUCCEEDED(aPS->GetHeaderStrRight(uStr))) {
623 Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
624 uStr);
628 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
629 if (NS_SUCCEEDED(aPS->GetFooterStrLeft(uStr))) {
630 Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
631 uStr);
635 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
636 if (NS_SUCCEEDED(aPS->GetFooterStrCenter(uStr))) {
637 Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
638 uStr);
642 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
643 if (NS_SUCCEEDED(aPS->GetFooterStrRight(uStr))) {
644 Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
645 uStr);
649 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
650 b = aPS->GetPrintBGColors();
651 Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
654 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
655 b = aPS->GetPrintBGImages();
656 Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
659 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
660 if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
661 Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
665 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
666 if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
667 Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
671 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
672 if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
673 Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
677 // Only the general version of this pref is saved
678 if ((aFlags & nsIPrintSettings::kInitSavePrinterName) &&
679 aPrinterName.IsEmpty()) {
680 if (NS_SUCCEEDED(aPS->GetPrinterName(uStr))) {
681 Preferences::SetString(kPrinterName, uStr);
685 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
686 if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
687 Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b);
691 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
692 if (NS_SUCCEEDED(aPS->GetToFileName(uStr))) {
693 Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr);
697 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
698 if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
699 Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
703 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
704 if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
705 Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
709 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
710 if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
711 WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
715 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
716 if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) {
717 Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal);
721 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
722 if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) {
723 Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal);
727 // Not Writing Out:
728 // Number of Copies
730 return NS_OK;
733 NS_IMETHODIMP
734 nsPrintSettingsService::GetDefaultPrintSettingsForPrinting(
735 nsIPrintSettings** aPrintSettings) {
736 nsresult rv = GetNewPrintSettings(aPrintSettings);
737 NS_ENSURE_SUCCESS(rv, rv);
739 nsIPrintSettings* settings = *aPrintSettings;
741 nsAutoString printerName;
742 settings->GetPrinterName(printerName);
743 if (printerName.IsEmpty()) {
744 GetLastUsedPrinterName(printerName);
745 settings->SetPrinterName(printerName);
747 InitPrintSettingsFromPrinter(printerName, settings);
748 InitPrintSettingsFromPrefs(settings, true, nsIPrintSettings::kInitSaveAll);
749 return NS_OK;
752 NS_IMETHODIMP
753 nsPrintSettingsService::GetNewPrintSettings(
754 nsIPrintSettings** aNewPrintSettings) {
755 return _CreatePrintSettings(aNewPrintSettings);
758 NS_IMETHODIMP
759 nsPrintSettingsService::GetLastUsedPrinterName(
760 nsAString& aLastUsedPrinterName) {
761 aLastUsedPrinterName.Truncate();
762 Preferences::GetString(kPrinterName, aLastUsedPrinterName);
763 return NS_OK;
766 NS_IMETHODIMP
767 nsPrintSettingsService::InitPrintSettingsFromPrinter(
768 const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
769 // Don't get print settings from the printer in the child when printing via
770 // parent, these will be retrieved in the parent later in the print process.
771 if (XRE_IsContentProcess() && StaticPrefs::print_print_via_parent()) {
772 return NS_OK;
775 NS_ENSURE_ARG_POINTER(aPrintSettings);
777 #ifdef DEBUG
778 nsString printerName;
779 aPrintSettings->GetPrinterName(printerName);
780 if (!printerName.Equals(aPrinterName)) {
781 NS_WARNING("Printer names should match!");
783 #endif
785 bool isInitialized;
786 aPrintSettings->GetIsInitializedFromPrinter(&isInitialized);
787 if (isInitialized) return NS_OK;
789 nsresult rv;
790 nsCOMPtr<nsIPrinterList> printerList =
791 do_GetService(NS_PRINTER_LIST_CONTRACTID, &rv);
792 NS_ENSURE_SUCCESS(rv, rv);
794 rv = printerList->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings);
795 NS_ENSURE_SUCCESS(rv, rv);
797 aPrintSettings->SetIsInitializedFromPrinter(true);
798 return rv;
801 /** ---------------------------------------------------
802 * Helper function - Returns either the name or sets the length to zero
804 static nsresult GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP,
805 nsAString& aPrinterName) {
806 NS_ENSURE_ARG_POINTER(aPS);
808 aPrinterName.Truncate();
809 if (!aUsePNP) return NS_OK;
811 // Get the Printer Name from the PrintSettings
812 // to use as a prefix for Pref Names
813 nsresult rv = aPS->GetPrinterName(aPrinterName);
814 NS_ENSURE_SUCCESS(rv, rv);
816 // Convert any whitespaces, carriage returns or newlines to _
817 // The below algorithm is supposedly faster than using iterators
818 constexpr auto replSubstr = u"_"_ns;
819 const char* replaceStr = " \n\r";
821 int32_t x;
822 for (x = 0; x < (int32_t)strlen(replaceStr); x++) {
823 char16_t uChar = replaceStr[x];
825 int32_t i = 0;
826 while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) {
827 aPrinterName.Replace(i, 1, replSubstr);
828 i++;
831 return NS_OK;
834 NS_IMETHODIMP
835 nsPrintSettingsService::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
836 bool aUsePNP,
837 uint32_t aFlags) {
838 NS_ENSURE_ARG_POINTER(aPS);
840 bool isInitialized;
841 aPS->GetIsInitializedFromPrefs(&isInitialized);
843 if (isInitialized) {
844 return NS_OK;
847 auto globalPrintSettings = aFlags;
848 #ifndef MOZ_WIDGET_ANDROID
849 globalPrintSettings &= nsIPrintSettings::kGlobalSettings;
850 #endif
852 nsAutoString prtName;
853 // read any non printer specific prefs
854 // with empty printer name
855 nsresult rv = ReadPrefs(aPS, prtName, globalPrintSettings);
856 NS_ENSURE_SUCCESS(rv, rv);
858 // Get the Printer Name from the PrintSettings to use as a prefix for Pref
859 // Names
860 rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
861 NS_ENSURE_SUCCESS(rv, rv);
863 if (prtName.IsEmpty()) {
864 NS_WARNING("Caller should supply a printer name.");
865 return NS_OK;
868 // Now read any printer specific prefs
869 rv = ReadPrefs(aPS, prtName, aFlags);
870 if (NS_SUCCEEDED(rv)) aPS->SetIsInitializedFromPrefs(true);
872 return NS_OK;
876 * Save all of the printer settings; if we can find a printer name, save
877 * printer-specific preferences. Otherwise, save generic ones.
879 nsresult nsPrintSettingsService::SavePrintSettingsToPrefs(
880 nsIPrintSettings* aPS, bool aUsePrinterNamePrefix, uint32_t aFlags) {
881 NS_ENSURE_ARG_POINTER(aPS);
883 if (GeckoProcessType_Content == XRE_GetProcessType()) {
884 // If we're in the content process, we can't directly write to the
885 // Preferences service - we have to proxy the save up to the
886 // parent process.
887 RefPtr<nsPrintingProxy> proxy = nsPrintingProxy::GetInstance();
888 return proxy->SavePrintSettings(aPS, aUsePrinterNamePrefix, aFlags);
891 nsAutoString prtName;
893 // Get the printer name from the PrinterSettings for an optional prefix.
894 nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName);
895 NS_ENSURE_SUCCESS(rv, rv);
897 // Write the prefs, with or without a printer name prefix.
898 return WritePrefs(aPS, prtName, aFlags);
901 //-----------------------------------------------------
902 //-- Protected Methods --------------------------------
903 //-----------------------------------------------------
904 nsresult nsPrintSettingsService::ReadPrefDouble(const char* aPrefId,
905 double& aVal) {
906 NS_ENSURE_ARG_POINTER(aPrefId);
908 nsAutoCString str;
909 nsresult rv = Preferences::GetCString(aPrefId, str);
910 if (NS_FAILED(rv) || str.IsEmpty()) {
911 return NS_ERROR_NOT_AVAILABLE;
914 double value = str.ToDouble(&rv);
915 if (NS_FAILED(rv)) {
916 return rv;
919 aVal = value;
920 return NS_OK;
923 nsresult nsPrintSettingsService::WritePrefDouble(const char* aPrefId,
924 double aVal) {
925 NS_ENSURE_ARG_POINTER(aPrefId);
927 nsAutoCString str;
928 // We cast to a float so we only get up to 6 digits precision in the prefs.
929 str.AppendFloat((float)aVal);
930 return Preferences::SetCString(aPrefId, str);
933 void nsPrintSettingsService::ReadInchesToTwipsPref(const char* aPrefId,
934 int32_t& aTwips,
935 const char* aMarginPref) {
936 nsAutoString str;
937 nsresult rv = Preferences::GetString(aPrefId, str);
938 if (NS_FAILED(rv) || str.IsEmpty()) {
939 rv = Preferences::GetString(aMarginPref, str);
941 if (NS_SUCCEEDED(rv) && !str.IsEmpty()) {
942 nsresult errCode;
943 float inches = str.ToFloat(&errCode);
944 if (NS_SUCCEEDED(errCode)) {
945 aTwips = NS_INCHES_TO_INT_TWIPS(inches);
946 } else {
947 aTwips = 0;
952 void nsPrintSettingsService::WriteInchesFromTwipsPref(const char* aPrefId,
953 int32_t aTwips) {
954 double inches = NS_TWIPS_TO_INCHES(aTwips);
955 nsAutoCString inchesStr;
956 inchesStr.AppendFloat(inches);
958 Preferences::SetCString(aPrefId, inchesStr);
961 void nsPrintSettingsService::ReadInchesIntToTwipsPref(const char* aPrefId,
962 int32_t& aTwips,
963 const char* aMarginPref) {
964 int32_t value;
965 nsresult rv = Preferences::GetInt(aPrefId, &value);
966 if (NS_FAILED(rv)) {
967 rv = Preferences::GetInt(aMarginPref, &value);
969 if (NS_SUCCEEDED(rv)) {
970 aTwips = NS_INCHES_TO_INT_TWIPS(float(value) / 100.0f);
971 } else {
972 aTwips = 0;
976 void nsPrintSettingsService::WriteInchesIntFromTwipsPref(const char* aPrefId,
977 int32_t aTwips) {
978 Preferences::SetInt(aPrefId,
979 int32_t(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f));
982 void nsPrintSettingsService::ReadJustification(const char* aPrefId,
983 int16_t& aJust,
984 int16_t aInitValue) {
985 aJust = aInitValue;
986 nsAutoString justStr;
987 if (NS_SUCCEEDED(Preferences::GetString(aPrefId, justStr))) {
988 if (justStr.EqualsASCII(kJustRight)) {
989 aJust = nsIPrintSettings::kJustRight;
990 } else if (justStr.EqualsASCII(kJustCenter)) {
991 aJust = nsIPrintSettings::kJustCenter;
992 } else {
993 aJust = nsIPrintSettings::kJustLeft;
998 //---------------------------------------------------
999 void nsPrintSettingsService::WriteJustification(const char* aPrefId,
1000 int16_t aJust) {
1001 switch (aJust) {
1002 case nsIPrintSettings::kJustLeft:
1003 Preferences::SetCString(aPrefId, kJustLeft);
1004 break;
1006 case nsIPrintSettings::kJustCenter:
1007 Preferences::SetCString(aPrefId, kJustCenter);
1008 break;
1010 case nsIPrintSettings::kJustRight:
1011 Preferences::SetCString(aPrefId, kJustRight);
1012 break;
1013 } // switch