Bug 1708422: part 16) Rename `mozInlineSpellChecker::SpellCheckerTimeSlice` to `mozIn...
[gecko.git] / widget / nsPrintSettingsService.cpp
blob2703720007b34bc4fc815a86a0d94949adfaf7b1
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/Preferences.h"
27 #include "nsPrintfCString.h"
29 using namespace mozilla;
30 using namespace mozilla::embedding;
32 typedef mozilla::layout::RemotePrintJobChild RemotePrintJobChild;
34 NS_IMPL_ISUPPORTS(nsPrintSettingsService, nsIPrintSettingsService)
36 // Pref Constants
37 static const char kMarginTop[] = "print_margin_top";
38 static const char kMarginLeft[] = "print_margin_left";
39 static const char kMarginBottom[] = "print_margin_bottom";
40 static const char kMarginRight[] = "print_margin_right";
41 static const char kEdgeTop[] = "print_edge_top";
42 static const char kEdgeLeft[] = "print_edge_left";
43 static const char kEdgeBottom[] = "print_edge_bottom";
44 static const char kEdgeRight[] = "print_edge_right";
45 static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top";
46 static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left";
47 static const char kUnwriteableMarginBottom[] =
48 "print_unwriteable_margin_bottom";
49 static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
51 // Prefs for Print Options
52 static const char kPrintHeaderStrLeft[] = "print_headerleft";
53 static const char kPrintHeaderStrCenter[] = "print_headercenter";
54 static const char kPrintHeaderStrRight[] = "print_headerright";
55 static const char kPrintFooterStrLeft[] = "print_footerleft";
56 static const char kPrintFooterStrCenter[] = "print_footercenter";
57 static const char kPrintFooterStrRight[] = "print_footerright";
59 // Additional Prefs
60 static const char kPrintReversed[] = "print_reversed";
61 static const char kPrintInColor[] = "print_in_color";
62 static const char kPrintPaperId[] = "print_paper_id";
63 static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
64 static const char kPrintPaperWidth[] = "print_paper_width";
65 static const char kPrintPaperHeight[] = "print_paper_height";
66 static const char kPrintOrientation[] = "print_orientation";
67 static const char kPrinterName[] = "print_printer";
68 static const char kPrintToFile[] = "print_to_file";
69 static const char kPrintToFileName[] = "print_to_filename";
70 static const char kPrintPageDelay[] = "print_page_delay";
71 static const char kPrintBGColors[] = "print_bgcolor";
72 static const char kPrintBGImages[] = "print_bgimages";
73 static const char kPrintShrinkToFit[] = "print_shrink_to_fit";
74 static const char kPrintScaling[] = "print_scaling";
75 static const char kPrintResolution[] = "print_resolution";
76 static const char kPrintDuplex[] = "print_duplex";
78 static const char kJustLeft[] = "left";
79 static const char kJustCenter[] = "center";
80 static const char kJustRight[] = "right";
82 #define NS_PRINTER_LIST_CONTRACTID "@mozilla.org/gfx/printerlist;1"
84 nsresult nsPrintSettingsService::Init() { return NS_OK; }
86 NS_IMETHODIMP
87 nsPrintSettingsService::SerializeToPrintData(nsIPrintSettings* aSettings,
88 PrintData* data) {
89 aSettings->GetPageRanges(data->pageRanges());
91 aSettings->GetEdgeTop(&data->edgeTop());
92 aSettings->GetEdgeLeft(&data->edgeLeft());
93 aSettings->GetEdgeBottom(&data->edgeBottom());
94 aSettings->GetEdgeRight(&data->edgeRight());
96 aSettings->GetMarginTop(&data->marginTop());
97 aSettings->GetMarginLeft(&data->marginLeft());
98 aSettings->GetMarginBottom(&data->marginBottom());
99 aSettings->GetMarginRight(&data->marginRight());
100 aSettings->GetUnwriteableMarginTop(&data->unwriteableMarginTop());
101 aSettings->GetUnwriteableMarginLeft(&data->unwriteableMarginLeft());
102 aSettings->GetUnwriteableMarginBottom(&data->unwriteableMarginBottom());
103 aSettings->GetUnwriteableMarginRight(&data->unwriteableMarginRight());
105 aSettings->GetScaling(&data->scaling());
107 data->printBGColors() = aSettings->GetPrintBGColors();
108 data->printBGImages() = aSettings->GetPrintBGImages();
110 data->honorPageRuleMargins() = aSettings->GetHonorPageRuleMargins();
111 data->showMarginGuides() = aSettings->GetShowMarginGuides();
112 data->isPrintSelectionRBEnabled() = aSettings->GetIsPrintSelectionRBEnabled();
113 data->printSelectionOnly() = aSettings->GetPrintSelectionOnly();
115 aSettings->GetTitle(data->title());
116 aSettings->GetDocURL(data->docURL());
118 aSettings->GetHeaderStrLeft(data->headerStrLeft());
119 aSettings->GetHeaderStrCenter(data->headerStrCenter());
120 aSettings->GetHeaderStrRight(data->headerStrRight());
122 aSettings->GetFooterStrLeft(data->footerStrLeft());
123 aSettings->GetFooterStrCenter(data->footerStrCenter());
124 aSettings->GetFooterStrRight(data->footerStrRight());
126 aSettings->GetIsCancelled(&data->isCancelled());
127 aSettings->GetPrintSilent(&data->printSilent());
128 aSettings->GetShrinkToFit(&data->shrinkToFit());
129 aSettings->GetShowPrintProgress(&data->showPrintProgress());
131 aSettings->GetPaperId(data->paperId());
132 aSettings->GetPaperWidth(&data->paperWidth());
133 aSettings->GetPaperHeight(&data->paperHeight());
134 aSettings->GetPaperSizeUnit(&data->paperSizeUnit());
136 aSettings->GetPrintReversed(&data->printReversed());
137 aSettings->GetPrintInColor(&data->printInColor());
138 aSettings->GetOrientation(&data->orientation());
140 aSettings->GetNumCopies(&data->numCopies());
141 aSettings->GetNumPagesPerSheet(&data->numPagesPerSheet());
143 aSettings->GetPrinterName(data->printerName());
145 aSettings->GetPrintToFile(&data->printToFile());
147 aSettings->GetToFileName(data->toFileName());
149 aSettings->GetOutputFormat(&data->outputFormat());
150 aSettings->GetPrintPageDelay(&data->printPageDelay());
151 aSettings->GetResolution(&data->resolution());
152 aSettings->GetDuplex(&data->duplex());
153 aSettings->GetIsInitializedFromPrinter(&data->isInitializedFromPrinter());
154 aSettings->GetIsInitializedFromPrefs(&data->isInitializedFromPrefs());
156 // Initialize the platform-specific values that don't
157 // default-initialize, so that we don't send uninitialized data over
158 // IPC (which leads to valgrind warnings, and, for bools, fatal
159 // assertions).
160 // data->driverName() default-initializes
161 // data->deviceName() default-initializes
162 // data->GTKPrintSettings() default-initializes
164 return NS_OK;
167 NS_IMETHODIMP
168 nsPrintSettingsService::DeserializeToPrintSettings(const PrintData& data,
169 nsIPrintSettings* settings) {
170 nsCOMPtr<nsIPrintSession> session;
171 nsresult rv = settings->GetPrintSession(getter_AddRefs(session));
172 if (NS_SUCCEEDED(rv) && session) {
173 session->SetRemotePrintJob(
174 static_cast<RemotePrintJobChild*>(data.remotePrintJobChild()));
177 settings->SetPageRanges(data.pageRanges());
179 settings->SetEdgeTop(data.edgeTop());
180 settings->SetEdgeLeft(data.edgeLeft());
181 settings->SetEdgeBottom(data.edgeBottom());
182 settings->SetEdgeRight(data.edgeRight());
184 settings->SetMarginTop(data.marginTop());
185 settings->SetMarginLeft(data.marginLeft());
186 settings->SetMarginBottom(data.marginBottom());
187 settings->SetMarginRight(data.marginRight());
188 settings->SetUnwriteableMarginTop(data.unwriteableMarginTop());
189 settings->SetUnwriteableMarginLeft(data.unwriteableMarginLeft());
190 settings->SetUnwriteableMarginBottom(data.unwriteableMarginBottom());
191 settings->SetUnwriteableMarginRight(data.unwriteableMarginRight());
193 settings->SetScaling(data.scaling());
195 settings->SetPrintBGColors(data.printBGColors());
196 settings->SetPrintBGImages(data.printBGImages());
197 settings->SetHonorPageRuleMargins(data.honorPageRuleMargins());
198 settings->SetShowMarginGuides(data.showMarginGuides());
199 settings->SetIsPrintSelectionRBEnabled(data.isPrintSelectionRBEnabled());
200 settings->SetPrintSelectionOnly(data.printSelectionOnly());
202 settings->SetTitle(data.title());
203 settings->SetDocURL(data.docURL());
205 // Header strings...
206 settings->SetHeaderStrLeft(data.headerStrLeft());
207 settings->SetHeaderStrCenter(data.headerStrCenter());
208 settings->SetHeaderStrRight(data.headerStrRight());
210 // Footer strings...
211 settings->SetFooterStrLeft(data.footerStrLeft());
212 settings->SetFooterStrCenter(data.footerStrCenter());
213 settings->SetFooterStrRight(data.footerStrRight());
215 settings->SetIsCancelled(data.isCancelled());
216 settings->SetPrintSilent(data.printSilent());
217 settings->SetShrinkToFit(data.shrinkToFit());
218 settings->SetShowPrintProgress(data.showPrintProgress());
220 settings->SetPaperId(data.paperId());
222 settings->SetPaperWidth(data.paperWidth());
223 settings->SetPaperHeight(data.paperHeight());
224 settings->SetPaperSizeUnit(data.paperSizeUnit());
226 settings->SetPrintReversed(data.printReversed());
227 settings->SetPrintInColor(data.printInColor());
228 settings->SetOrientation(data.orientation());
230 settings->SetNumCopies(data.numCopies());
231 settings->SetNumPagesPerSheet(data.numPagesPerSheet());
233 settings->SetPrinterName(data.printerName());
235 settings->SetPrintToFile(data.printToFile());
237 settings->SetToFileName(data.toFileName());
239 settings->SetOutputFormat(data.outputFormat());
240 settings->SetPrintPageDelay(data.printPageDelay());
241 settings->SetResolution(data.resolution());
242 settings->SetDuplex(data.duplex());
243 settings->SetIsInitializedFromPrinter(data.isInitializedFromPrinter());
244 settings->SetIsInitializedFromPrefs(data.isInitializedFromPrefs());
246 return NS_OK;
249 /** ---------------------------------------------------
250 * Helper function - Creates the "prefix" for the pref
251 * It is either "print."
252 * or "print.printer_<print name>."
254 const char* nsPrintSettingsService::GetPrefName(const char* aPrefName,
255 const nsAString& aPrinterName) {
256 if (!aPrefName || !*aPrefName) {
257 NS_ERROR("Must have a valid pref name!");
258 return aPrefName;
261 mPrefName.AssignLiteral("print.");
263 if (aPrinterName.Length()) {
264 mPrefName.AppendLiteral("printer_");
265 AppendUTF16toUTF8(aPrinterName, mPrefName);
266 mPrefName.Append('.');
268 mPrefName += aPrefName;
270 return mPrefName.get();
274 * This will either read in the generic prefs (not specific to a printer)
275 * or read the prefs in using the printer name to qualify.
276 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
278 nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS,
279 const nsAString& aPrinterName,
280 uint32_t aFlags) {
281 NS_ENSURE_ARG_POINTER(aPS);
283 bool b;
284 nsAutoString str;
285 int32_t iVal;
286 double dbl;
288 #define GETBOOLPREF(_prefname, _retval) \
289 NS_SUCCEEDED( \
290 Preferences::GetBool(GetPrefName(_prefname, aPrinterName), _retval))
292 #define GETSTRPREF(_prefname, _retval) \
293 NS_SUCCEEDED( \
294 Preferences::GetString(GetPrefName(_prefname, aPrinterName), _retval))
296 #define GETINTPREF(_prefname, _retval) \
297 NS_SUCCEEDED( \
298 Preferences::GetInt(GetPrefName(_prefname, aPrinterName), _retval))
300 #define GETDBLPREF(_prefname, _retval) \
301 NS_SUCCEEDED(ReadPrefDouble(GetPrefName(_prefname, aPrinterName), _retval))
303 bool gotPaperSizeFromPrefs = false;
304 int16_t paperSizeUnit;
305 double paperWidth, paperHeight;
307 // Paper size prefs are read as a group
308 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
309 gotPaperSizeFromPrefs = GETINTPREF(kPrintPaperSizeUnit, &iVal) &&
310 GETDBLPREF(kPrintPaperWidth, paperWidth) &&
311 GETDBLPREF(kPrintPaperHeight, paperHeight) &&
312 GETSTRPREF(kPrintPaperId, str);
313 paperSizeUnit = (int16_t)iVal;
315 if (gotPaperSizeFromPrefs &&
316 paperSizeUnit != nsIPrintSettings::kPaperSizeInches &&
317 paperSizeUnit != nsIPrintSettings::kPaperSizeMillimeters) {
318 gotPaperSizeFromPrefs = false;
321 if (gotPaperSizeFromPrefs) {
322 // Bug 315687: Sanity check paper size to avoid paper size values in
323 // mm when the size unit flag is inches. The value 100 is arbitrary
324 // and can be changed.
325 gotPaperSizeFromPrefs =
326 (paperSizeUnit != nsIPrintSettings::kPaperSizeInches) ||
327 (paperWidth < 100.0) || (paperHeight < 100.0);
330 if (gotPaperSizeFromPrefs) {
331 aPS->SetPaperSizeUnit(paperSizeUnit);
332 aPS->SetPaperWidth(paperWidth);
333 aPS->SetPaperHeight(paperHeight);
334 aPS->SetPaperId(str);
338 nsIntSize pageSizeInTwips; // to sanity check margins
339 if (!gotPaperSizeFromPrefs) {
340 aPS->GetPaperSizeUnit(&paperSizeUnit);
341 aPS->GetPaperWidth(&paperWidth);
342 aPS->GetPaperHeight(&paperHeight);
344 if (paperSizeUnit == nsIPrintSettings::kPaperSizeMillimeters) {
345 pageSizeInTwips = nsIntSize((int)NS_MILLIMETERS_TO_TWIPS(paperWidth),
346 (int)NS_MILLIMETERS_TO_TWIPS(paperHeight));
347 } else {
348 pageSizeInTwips = nsIntSize((int)NS_INCHES_TO_TWIPS(paperWidth),
349 (int)NS_INCHES_TO_TWIPS(paperHeight));
352 auto MarginIsOK = [&pageSizeInTwips](const nsIntMargin& aMargin) {
353 return aMargin.top >= 0 && aMargin.right >= 0 && aMargin.bottom >= 0 &&
354 aMargin.left >= 0 && aMargin.LeftRight() < pageSizeInTwips.width &&
355 aMargin.TopBottom() < pageSizeInTwips.height;
358 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
359 nsIntMargin margin;
360 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName),
361 margin.top, kUnwriteableMarginTop);
362 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName),
363 margin.left, kUnwriteableMarginLeft);
364 ReadInchesIntToTwipsPref(
365 GetPrefName(kUnwriteableMarginBottom, aPrinterName), margin.bottom,
366 kUnwriteableMarginBottom);
367 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName),
368 margin.right, kUnwriteableMarginRight);
369 // SetUnwriteableMarginInTwips does its own validation and drops negative
370 // values individually. We still want to block overly large values though,
371 // so we do that part of MarginIsOK manually.
372 if (margin.LeftRight() < pageSizeInTwips.width &&
373 margin.TopBottom() < pageSizeInTwips.height) {
374 aPS->SetUnwriteableMarginInTwips(margin);
378 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
379 int32_t halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
380 nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
381 ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top,
382 kMarginTop);
383 ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left,
384 kMarginLeft);
385 ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
386 margin.bottom, kMarginBottom);
387 ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right,
388 kMarginRight);
389 if (MarginIsOK(margin)) {
390 aPS->SetMarginInTwips(margin);
394 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
395 nsIntMargin margin(0, 0, 0, 0);
396 ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top,
397 kEdgeTop);
398 ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left,
399 kEdgeLeft);
400 ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
401 margin.bottom, kEdgeBottom);
402 ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
403 margin.right, kEdgeRight);
404 if (MarginIsOK(margin)) {
405 aPS->SetEdgeInTwips(margin);
409 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
410 if (GETSTRPREF(kPrintHeaderStrLeft, str)) {
411 aPS->SetHeaderStrLeft(str);
415 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
416 if (GETSTRPREF(kPrintHeaderStrCenter, str)) {
417 aPS->SetHeaderStrCenter(str);
421 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
422 if (GETSTRPREF(kPrintHeaderStrRight, str)) {
423 aPS->SetHeaderStrRight(str);
427 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
428 if (GETSTRPREF(kPrintFooterStrLeft, str)) {
429 aPS->SetFooterStrLeft(str);
433 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
434 if (GETSTRPREF(kPrintFooterStrCenter, str)) {
435 aPS->SetFooterStrCenter(str);
439 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
440 if (GETSTRPREF(kPrintFooterStrRight, str)) {
441 aPS->SetFooterStrRight(str);
445 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
446 if (GETBOOLPREF(kPrintBGColors, &b)) {
447 aPS->SetPrintBGColors(b);
451 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
452 if (GETBOOLPREF(kPrintBGImages, &b)) {
453 aPS->SetPrintBGImages(b);
457 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
458 if (GETBOOLPREF(kPrintReversed, &b)) {
459 aPS->SetPrintReversed(b);
463 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
464 if (GETBOOLPREF(kPrintInColor, &b)) {
465 aPS->SetPrintInColor(b);
469 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
470 if (GETINTPREF(kPrintOrientation, &iVal) &&
471 (iVal == nsIPrintSettings::kPortraitOrientation ||
472 iVal == nsIPrintSettings::kLandscapeOrientation)) {
473 aPS->SetOrientation(iVal);
477 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
478 if (GETBOOLPREF(kPrintToFile, &b)) {
479 aPS->SetPrintToFile(b);
483 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
484 if (GETSTRPREF(kPrintToFileName, str)) {
485 if (StringEndsWith(str, u".ps"_ns)) {
486 // We only support PDF since bug 1425188 landed. Users may still have
487 // prefs with .ps filenames if they last saved a file as Postscript
488 // though, so we fix that up here. (The pref values will be
489 // overwritten the next time they save to file as a PDF.)
490 str.Truncate(str.Length() - 2);
491 str.AppendLiteral("pdf");
493 aPS->SetToFileName(str);
497 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
498 // milliseconds
499 if (GETINTPREF(kPrintPageDelay, &iVal) && iVal >= 0 && iVal <= 1000) {
500 aPS->SetPrintPageDelay(iVal);
504 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
505 if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
506 aPS->SetShrinkToFit(b);
510 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
511 // The limits imposed here are fairly arbitrary and mainly intended to
512 // purge bad values which tend to be negative and/or very large. If we
513 // get complaints from users that settings outside these values "aren't
514 // saved" then we can consider increasing them.
515 if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
516 aPS->SetScaling(dbl);
520 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
521 // DPI. Again, an arbitrary range mainly to purge bad values that have made
522 // their way into user prefs.
523 if (GETINTPREF(kPrintResolution, &iVal) && iVal >= 50 && iVal <= 12000) {
524 aPS->SetResolution(iVal);
528 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
529 if (GETINTPREF(kPrintDuplex, &iVal)) {
530 aPS->SetDuplex(iVal);
534 // Not Reading In:
535 // Number of Copies
537 return NS_OK;
540 nsresult nsPrintSettingsService::WritePrefs(nsIPrintSettings* aPS,
541 const nsAString& aPrinterName,
542 uint32_t aFlags) {
543 NS_ENSURE_ARG_POINTER(aPS);
545 if (aFlags & nsIPrintSettings::kInitSaveMargins) {
546 nsIntMargin margin = aPS->GetMarginInTwips();
547 WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top);
548 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
549 margin.left);
550 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
551 margin.bottom);
552 WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
553 margin.right);
556 if (aFlags & nsIPrintSettings::kInitSaveEdges) {
557 nsIntMargin edge = aPS->GetEdgeInTwips();
558 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName), edge.top);
559 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
560 edge.left);
561 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
562 edge.bottom);
563 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
564 edge.right);
567 if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
568 nsIntMargin unwriteableMargin = aPS->GetUnwriteableMarginInTwips();
569 WriteInchesIntFromTwipsPref(
570 GetPrefName(kUnwriteableMarginTop, aPrinterName),
571 unwriteableMargin.top);
572 WriteInchesIntFromTwipsPref(
573 GetPrefName(kUnwriteableMarginLeft, aPrinterName),
574 unwriteableMargin.left);
575 WriteInchesIntFromTwipsPref(
576 GetPrefName(kUnwriteableMarginBottom, aPrinterName),
577 unwriteableMargin.bottom);
578 WriteInchesIntFromTwipsPref(
579 GetPrefName(kUnwriteableMarginRight, aPrinterName),
580 unwriteableMargin.right);
583 // Paper size prefs are saved as a group
584 if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
585 int16_t sizeUnit;
586 double width, height;
587 nsString name;
589 if (NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) &&
590 NS_SUCCEEDED(aPS->GetPaperWidth(&width)) &&
591 NS_SUCCEEDED(aPS->GetPaperHeight(&height)) &&
592 NS_SUCCEEDED(aPS->GetPaperId(name))) {
593 Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
594 int32_t(sizeUnit));
595 WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width);
596 WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height);
597 Preferences::SetString(GetPrefName(kPrintPaperId, aPrinterName), name);
601 bool b;
602 nsString uStr;
603 int32_t iVal;
604 double dbl;
606 if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
607 if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(uStr))) {
608 Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
609 uStr);
613 if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
614 if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(uStr))) {
615 Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
616 uStr);
620 if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
621 if (NS_SUCCEEDED(aPS->GetHeaderStrRight(uStr))) {
622 Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
623 uStr);
627 if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
628 if (NS_SUCCEEDED(aPS->GetFooterStrLeft(uStr))) {
629 Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
630 uStr);
634 if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
635 if (NS_SUCCEEDED(aPS->GetFooterStrCenter(uStr))) {
636 Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
637 uStr);
641 if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
642 if (NS_SUCCEEDED(aPS->GetFooterStrRight(uStr))) {
643 Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
644 uStr);
648 if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
649 b = aPS->GetPrintBGColors();
650 Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
653 if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
654 b = aPS->GetPrintBGImages();
655 Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
658 if (aFlags & nsIPrintSettings::kInitSaveReversed) {
659 if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
660 Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
664 if (aFlags & nsIPrintSettings::kInitSaveInColor) {
665 if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
666 Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
670 if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
671 if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
672 Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
676 // Only the general version of this pref is saved
677 if ((aFlags & nsIPrintSettings::kInitSavePrinterName) &&
678 aPrinterName.IsEmpty()) {
679 if (NS_SUCCEEDED(aPS->GetPrinterName(uStr))) {
680 Preferences::SetString(kPrinterName, uStr);
684 if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
685 if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
686 Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b);
690 if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
691 if (NS_SUCCEEDED(aPS->GetToFileName(uStr))) {
692 Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr);
696 if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
697 if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
698 Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
702 if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
703 if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
704 Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
708 if (aFlags & nsIPrintSettings::kInitSaveScaling) {
709 if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
710 WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
714 if (aFlags & nsIPrintSettings::kInitSaveResolution) {
715 if (NS_SUCCEEDED(aPS->GetResolution(&iVal))) {
716 Preferences::SetInt(GetPrefName(kPrintResolution, aPrinterName), iVal);
720 if (aFlags & nsIPrintSettings::kInitSaveDuplex) {
721 if (NS_SUCCEEDED(aPS->GetDuplex(&iVal))) {
722 Preferences::SetInt(GetPrefName(kPrintDuplex, aPrinterName), iVal);
726 // Not Writing Out:
727 // Number of Copies
729 return NS_OK;
732 NS_IMETHODIMP
733 nsPrintSettingsService::GetDefaultPrintSettingsForPrinting(
734 nsIPrintSettings** aPrintSettings) {
735 nsresult rv = GetNewPrintSettings(aPrintSettings);
736 NS_ENSURE_SUCCESS(rv, rv);
738 nsIPrintSettings* settings = *aPrintSettings;
740 nsAutoString printerName;
741 settings->GetPrinterName(printerName);
742 if (printerName.IsEmpty()) {
743 GetLastUsedPrinterName(printerName);
744 settings->SetPrinterName(printerName);
746 InitPrintSettingsFromPrinter(printerName, settings);
747 InitPrintSettingsFromPrefs(settings, true, nsIPrintSettings::kInitSaveAll);
748 return NS_OK;
751 NS_IMETHODIMP
752 nsPrintSettingsService::GetNewPrintSettings(
753 nsIPrintSettings** aNewPrintSettings) {
754 return _CreatePrintSettings(aNewPrintSettings);
757 NS_IMETHODIMP
758 nsPrintSettingsService::GetLastUsedPrinterName(
759 nsAString& aLastUsedPrinterName) {
760 aLastUsedPrinterName.Truncate();
761 Preferences::GetString(kPrinterName, aLastUsedPrinterName);
762 return NS_OK;
765 NS_IMETHODIMP
766 nsPrintSettingsService::InitPrintSettingsFromPrinter(
767 const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) {
768 // Don't get print settings from the printer in the child when printing via
769 // parent, these will be retrieved in the parent later in the print process.
770 if (XRE_IsContentProcess() &&
771 Preferences::GetBool("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