Coverage Report

Created: 2018-09-25 14:53

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