1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2000
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jessica Blanco <jblanco@us.ibm.com>
24 : * Bastiaan Jacques <b.jacques@planet.nl>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsPrintOptionsImpl.h"
41 : #include "nsReadableUtils.h"
42 : #include "nsPrintSettingsImpl.h"
43 :
44 : #include "nsIDOMWindow.h"
45 : #include "nsIServiceManager.h"
46 : #include "nsIDialogParamBlock.h"
47 : #include "nsXPCOM.h"
48 : #include "nsISupportsPrimitives.h"
49 : #include "nsIWindowWatcher.h"
50 : #include "nsISupportsArray.h"
51 : #include "prprf.h"
52 :
53 : #include "nsIStringEnumerator.h"
54 : #include "nsISupportsPrimitives.h"
55 : #include "stdlib.h"
56 : #include "nsAutoPtr.h"
57 : #include "mozilla/Preferences.h"
58 : #include "nsPrintfCString.h"
59 :
60 : using namespace mozilla;
61 :
62 0 : NS_IMPL_ISUPPORTS2(nsPrintOptions, nsIPrintOptions, nsIPrintSettingsService)
63 :
64 : // Pref Constants
65 : static const char kMarginTop[] = "print_margin_top";
66 : static const char kMarginLeft[] = "print_margin_left";
67 : static const char kMarginBottom[] = "print_margin_bottom";
68 : static const char kMarginRight[] = "print_margin_right";
69 : static const char kEdgeTop[] = "print_edge_top";
70 : static const char kEdgeLeft[] = "print_edge_left";
71 : static const char kEdgeBottom[] = "print_edge_bottom";
72 : static const char kEdgeRight[] = "print_edge_right";
73 : static const char kUnwriteableMarginTop[] = "print_unwriteable_margin_top";
74 : static const char kUnwriteableMarginLeft[] = "print_unwriteable_margin_left";
75 : static const char kUnwriteableMarginBottom[] = "print_unwriteable_margin_bottom";
76 : static const char kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
77 :
78 : // Prefs for Print Options
79 : static const char kPrintEvenPages[] = "print_evenpages";
80 : static const char kPrintOddPages[] = "print_oddpages";
81 : static const char kPrintHeaderStrLeft[] = "print_headerleft";
82 : static const char kPrintHeaderStrCenter[] = "print_headercenter";
83 : static const char kPrintHeaderStrRight[] = "print_headerright";
84 : static const char kPrintFooterStrLeft[] = "print_footerleft";
85 : static const char kPrintFooterStrCenter[] = "print_footercenter";
86 : static const char kPrintFooterStrRight[] = "print_footerright";
87 :
88 : // Additional Prefs
89 : static const char kPrintReversed[] = "print_reversed";
90 : static const char kPrintInColor[] = "print_in_color";
91 : static const char kPrintPaperName[] = "print_paper_name";
92 : static const char kPrintPlexName[] = "print_plex_name";
93 : static const char kPrintPaperSizeType[] = "print_paper_size_type";
94 : static const char kPrintPaperData[] = "print_paper_data";
95 : static const char kPrintPaperSizeUnit[] = "print_paper_size_unit";
96 : static const char kPrintPaperWidth[] = "print_paper_width";
97 : static const char kPrintPaperHeight[] = "print_paper_height";
98 : static const char kPrintColorspace[] = "print_colorspace";
99 : static const char kPrintResolutionName[]= "print_resolution_name";
100 : static const char kPrintDownloadFonts[] = "print_downloadfonts";
101 : static const char kPrintOrientation[] = "print_orientation";
102 : static const char kPrintCommand[] = "print_command";
103 : static const char kPrinterName[] = "print_printer";
104 : static const char kPrintToFile[] = "print_to_file";
105 : static const char kPrintToFileName[] = "print_to_filename";
106 : static const char kPrintPageDelay[] = "print_page_delay";
107 : static const char kPrintBGColors[] = "print_bgcolor";
108 : static const char kPrintBGImages[] = "print_bgimages";
109 : static const char kPrintShrinkToFit[] = "print_shrink_to_fit";
110 : static const char kPrintScaling[] = "print_scaling";
111 :
112 : static const char kJustLeft[] = "left";
113 : static const char kJustCenter[] = "center";
114 : static const char kJustRight[] = "right";
115 :
116 : #define NS_PRINTER_ENUMERATOR_CONTRACTID "@mozilla.org/gfx/printerenumerator;1"
117 :
118 0 : nsPrintOptions::nsPrintOptions()
119 : {
120 0 : }
121 :
122 0 : nsPrintOptions::~nsPrintOptions()
123 : {
124 0 : }
125 :
126 : nsresult
127 0 : nsPrintOptions::Init()
128 : {
129 0 : return NS_OK;
130 : }
131 :
132 : NS_IMETHODIMP
133 0 : nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings *aPS)
134 : {
135 0 : NS_ENSURE_ARG_POINTER(aPS);
136 : nsresult rv;
137 :
138 : // create a nsISupportsArray of the parameters
139 : // being passed to the window
140 0 : nsCOMPtr<nsISupportsArray> array;
141 0 : rv = NS_NewISupportsArray(getter_AddRefs(array));
142 0 : NS_ENSURE_SUCCESS(rv, rv);
143 :
144 0 : nsCOMPtr<nsISupports> psSupports = do_QueryInterface(aPS);
145 0 : NS_ASSERTION(psSupports, "PrintSettings must be a supports");
146 0 : array->AppendElement(psSupports);
147 :
148 : nsCOMPtr<nsIDialogParamBlock> ioParamBlock =
149 0 : do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &rv);
150 0 : NS_ENSURE_SUCCESS(rv, rv);
151 :
152 0 : ioParamBlock->SetInt(0, 0);
153 :
154 0 : nsCOMPtr<nsISupports> blkSupps = do_QueryInterface(ioParamBlock);
155 0 : NS_ASSERTION(blkSupps, "IOBlk must be a supports");
156 0 : array->AppendElement(blkSupps);
157 :
158 : nsCOMPtr<nsIWindowWatcher> wwatch =
159 0 : do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
160 0 : NS_ENSURE_SUCCESS(rv, rv);
161 :
162 0 : nsCOMPtr<nsIDOMWindow> parent;
163 0 : wwatch->GetActiveWindow(getter_AddRefs(parent));
164 : // null |parent| is non-fatal
165 :
166 0 : nsCOMPtr<nsIDOMWindow> newWindow;
167 :
168 0 : return wwatch->OpenWindow(parent,
169 : "chrome://global/content/printPageSetup.xul",
170 : "_blank","chrome,modal,centerscreen", array,
171 0 : getter_AddRefs(newWindow));
172 : }
173 :
174 : /** ---------------------------------------------------
175 : * Helper function - Creates the "prefix" for the pref
176 : * It is either "print."
177 : * or "print.printer_<print name>."
178 : */
179 : const char*
180 0 : nsPrintOptions::GetPrefName(const char * aPrefName,
181 : const nsAString& aPrinterName)
182 : {
183 0 : if (!aPrefName || !*aPrefName) {
184 0 : NS_ERROR("Must have a valid pref name!");
185 0 : return aPrefName;
186 : }
187 :
188 0 : mPrefName.Truncate(); /* mPrefName = ""; */
189 :
190 0 : if (aPrinterName.Length()) {
191 0 : mPrefName.Append("printer_");
192 0 : AppendUTF16toUTF8(aPrinterName, mPrefName);
193 0 : mPrefName.Append(".");
194 : }
195 0 : mPrefName += aPrefName;
196 :
197 0 : return mPrefName.get();
198 : }
199 :
200 : //----------------------------------------------------------------------
201 : // Testing of read/write prefs
202 : // This define controls debug output
203 : #ifdef DEBUG_rods_X
204 : static void WriteDebugStr(const char* aArg1, const char* aArg2,
205 : const PRUnichar* aStr)
206 : {
207 : nsString str(aStr);
208 : PRUnichar s = '&';
209 : PRUnichar r = '_';
210 : str.ReplaceChar(s, r);
211 :
212 : printf("%s %s = %s \n", aArg1, aArg2, ToNewUTF8String(str));
213 : }
214 : const char* kWriteStr = "Write Pref:";
215 : const char* kReadStr = "Read Pref:";
216 : #define DUMP_STR(_a1, _a2, _a3) WriteDebugStr((_a1), GetPrefName((_a2), \
217 : aPrefName), (_a3));
218 : #define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \
219 : GetPrefName((_a2), aPrefName), (_a3)?"T":"F");
220 : #define DUMP_INT(_a1, _a2, _a3) printf("%s %s = %d \n", (_a1), \
221 : GetPrefName((_a2), aPrefName), (_a3));
222 : #define DUMP_DBL(_a1, _a2, _a3) printf("%s %s = %10.5f \n", (_a1), \
223 : GetPrefName((_a2), aPrefName), (_a3));
224 : #else
225 : #define DUMP_STR(_a1, _a2, _a3)
226 : #define DUMP_BOOL(_a1, _a2, _a3)
227 : #define DUMP_INT(_a1, _a2, _a3)
228 : #define DUMP_DBL(_a1, _a2, _a3)
229 : #endif /* DEBUG_rods_X */
230 : //----------------------------------------------------------------------
231 :
232 : /**
233 : * This will either read in the generic prefs (not specific to a printer)
234 : * or read the prefs in using the printer name to qualify.
235 : * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
236 : */
237 : nsresult
238 0 : nsPrintOptions::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName,
239 : PRUint32 aFlags)
240 : {
241 0 : NS_ENSURE_ARG_POINTER(aPS);
242 :
243 0 : if (aFlags & nsIPrintSettings::kInitSaveMargins) {
244 0 : PRInt32 halfInch = NS_INCHES_TO_INT_TWIPS(0.5);
245 0 : nsIntMargin margin(halfInch, halfInch, halfInch, halfInch);
246 : ReadInchesToTwipsPref(GetPrefName(kMarginTop, aPrinterName), margin.top,
247 0 : kMarginTop);
248 : DUMP_INT(kReadStr, kMarginTop, margin.top);
249 : ReadInchesToTwipsPref(GetPrefName(kMarginLeft, aPrinterName), margin.left,
250 0 : kMarginLeft);
251 : DUMP_INT(kReadStr, kMarginLeft, margin.left);
252 : ReadInchesToTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
253 0 : margin.bottom, kMarginBottom);
254 : DUMP_INT(kReadStr, kMarginBottom, margin.bottom);
255 : ReadInchesToTwipsPref(GetPrefName(kMarginRight, aPrinterName), margin.right,
256 0 : kMarginRight);
257 : DUMP_INT(kReadStr, kMarginRight, margin.right);
258 0 : aPS->SetMarginInTwips(margin);
259 : }
260 :
261 0 : if (aFlags & nsIPrintSettings::kInitSaveEdges) {
262 0 : nsIntMargin margin(0,0,0,0);
263 : ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop, aPrinterName), margin.top,
264 0 : kEdgeTop);
265 : DUMP_INT(kReadStr, kEdgeTop, margin.top);
266 : ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft, aPrinterName), margin.left,
267 0 : kEdgeLeft);
268 : DUMP_INT(kReadStr, kEdgeLeft, margin.left);
269 : ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
270 0 : margin.bottom, kEdgeBottom);
271 : DUMP_INT(kReadStr, kEdgeBottom, margin.bottom);
272 : ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight, aPrinterName), margin.right,
273 0 : kEdgeRight);
274 : DUMP_INT(kReadStr, kEdgeRight, margin.right);
275 0 : aPS->SetEdgeInTwips(margin);
276 : }
277 :
278 0 : if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
279 0 : nsIntMargin margin;
280 : ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName), margin.top,
281 0 : kUnwriteableMarginTop);
282 : DUMP_INT(kReadStr, kUnwriteableMarginTop, margin.top);
283 : ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName), margin.left,
284 0 : kUnwriteableMarginLeft);
285 : DUMP_INT(kReadStr, kUnwriteableMarginLeft, margin.left);
286 : ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName),
287 0 : margin.bottom, kUnwriteableMarginBottom);
288 : DUMP_INT(kReadStr, kUnwriteableMarginBottom, margin.bottom);
289 : ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName), margin.right,
290 0 : kUnwriteableMarginRight);
291 : DUMP_INT(kReadStr, kUnwriteableMarginRight, margin.right);
292 0 : aPS->SetUnwriteableMarginInTwips(margin);
293 : }
294 :
295 : bool b;
296 0 : nsAutoString str;
297 : PRInt32 iVal;
298 : double dbl;
299 :
300 : #define GETBOOLPREF(_prefname, _retval) \
301 : NS_SUCCEEDED( \
302 : Preferences::GetBool( \
303 : GetPrefName(_prefname, aPrinterName), _retval \
304 : ) \
305 : )
306 :
307 : #define GETSTRPREF(_prefname, _retval) \
308 : NS_SUCCEEDED( \
309 : Preferences::GetString( \
310 : GetPrefName(_prefname, aPrinterName), _retval \
311 : ) \
312 : )
313 :
314 : #define GETINTPREF(_prefname, _retval) \
315 : NS_SUCCEEDED( \
316 : Preferences::GetInt( \
317 : GetPrefName(_prefname, aPrinterName), _retval \
318 : ) \
319 : )
320 :
321 : #define GETDBLPREF(_prefname, _retval) \
322 : NS_SUCCEEDED( \
323 : ReadPrefDouble( \
324 : GetPrefName(_prefname, aPrinterName), _retval \
325 : ) \
326 : )
327 :
328 : // Paper size prefs are read as a group
329 0 : if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
330 : PRInt32 sizeUnit, sizeType;
331 : double width, height;
332 :
333 0 : bool success = GETINTPREF(kPrintPaperSizeUnit, &sizeUnit)
334 0 : && GETINTPREF(kPrintPaperSizeType, &sizeType)
335 0 : && GETDBLPREF(kPrintPaperWidth, width)
336 0 : && GETDBLPREF(kPrintPaperHeight, height)
337 0 : && GETSTRPREF(kPrintPaperName, &str);
338 :
339 : // Bug 315687: Sanity check paper size to avoid paper size values in
340 : // mm when the size unit flag is inches. The value 100 is arbitrary
341 : // and can be changed.
342 0 : if (success) {
343 : success = (sizeUnit != nsIPrintSettings::kPaperSizeInches)
344 : || (width < 100.0)
345 0 : || (height < 100.0);
346 : }
347 :
348 0 : if (success) {
349 0 : aPS->SetPaperSizeUnit(sizeUnit);
350 : DUMP_INT(kReadStr, kPrintPaperSizeUnit, sizeUnit);
351 0 : aPS->SetPaperSizeType(sizeType);
352 : DUMP_INT(kReadStr, kPrintPaperSizeType, sizeType);
353 0 : aPS->SetPaperWidth(width);
354 : DUMP_DBL(kReadStr, kPrintPaperWidth, width);
355 0 : aPS->SetPaperHeight(height);
356 : DUMP_DBL(kReadStr, kPrintPaperHeight, height);
357 0 : aPS->SetPaperName(str.get());
358 : DUMP_STR(kReadStr, kPrintPaperName, str.get());
359 : }
360 : }
361 :
362 0 : if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
363 0 : if (GETBOOLPREF(kPrintEvenPages, &b)) {
364 0 : aPS->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, b);
365 : DUMP_BOOL(kReadStr, kPrintEvenPages, b);
366 : }
367 : }
368 :
369 0 : if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
370 0 : if (GETBOOLPREF(kPrintOddPages, &b)) {
371 0 : aPS->SetPrintOptions(nsIPrintSettings::kPrintOddPages, b);
372 : DUMP_BOOL(kReadStr, kPrintOddPages, b);
373 : }
374 : }
375 :
376 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
377 0 : if (GETSTRPREF(kPrintHeaderStrLeft, &str)) {
378 0 : aPS->SetHeaderStrLeft(str.get());
379 : DUMP_STR(kReadStr, kPrintHeaderStrLeft, str.get());
380 : }
381 : }
382 :
383 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
384 0 : if (GETSTRPREF(kPrintHeaderStrCenter, &str)) {
385 0 : aPS->SetHeaderStrCenter(str.get());
386 : DUMP_STR(kReadStr, kPrintHeaderStrCenter, str.get());
387 : }
388 : }
389 :
390 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
391 0 : if (GETSTRPREF(kPrintHeaderStrRight, &str)) {
392 0 : aPS->SetHeaderStrRight(str.get());
393 : DUMP_STR(kReadStr, kPrintHeaderStrRight, str.get());
394 : }
395 : }
396 :
397 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
398 0 : if (GETSTRPREF(kPrintFooterStrLeft, &str)) {
399 0 : aPS->SetFooterStrLeft(str.get());
400 : DUMP_STR(kReadStr, kPrintFooterStrLeft, str.get());
401 : }
402 : }
403 :
404 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
405 0 : if (GETSTRPREF(kPrintFooterStrCenter, &str)) {
406 0 : aPS->SetFooterStrCenter(str.get());
407 : DUMP_STR(kReadStr, kPrintFooterStrCenter, str.get());
408 : }
409 : }
410 :
411 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
412 0 : if (GETSTRPREF(kPrintFooterStrRight, &str)) {
413 0 : aPS->SetFooterStrRight(str.get());
414 : DUMP_STR(kReadStr, kPrintFooterStrRight, str.get());
415 : }
416 : }
417 :
418 0 : if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
419 0 : if (GETBOOLPREF(kPrintBGColors, &b)) {
420 0 : aPS->SetPrintBGColors(b);
421 : DUMP_BOOL(kReadStr, kPrintBGColors, b);
422 : }
423 : }
424 :
425 0 : if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
426 0 : if (GETBOOLPREF(kPrintBGImages, &b)) {
427 0 : aPS->SetPrintBGImages(b);
428 : DUMP_BOOL(kReadStr, kPrintBGImages, b);
429 : }
430 : }
431 :
432 0 : if (aFlags & nsIPrintSettings::kInitSaveReversed) {
433 0 : if (GETBOOLPREF(kPrintReversed, &b)) {
434 0 : aPS->SetPrintReversed(b);
435 : DUMP_BOOL(kReadStr, kPrintReversed, b);
436 : }
437 : }
438 :
439 0 : if (aFlags & nsIPrintSettings::kInitSaveInColor) {
440 0 : if (GETBOOLPREF(kPrintInColor, &b)) {
441 0 : aPS->SetPrintInColor(b);
442 : DUMP_BOOL(kReadStr, kPrintInColor, b);
443 : }
444 : }
445 :
446 0 : if (aFlags & nsIPrintSettings::kInitSavePlexName) {
447 0 : if (GETSTRPREF(kPrintPlexName, &str)) {
448 0 : aPS->SetPlexName(str.get());
449 : DUMP_STR(kReadStr, kPrintPlexName, str.get());
450 : }
451 : }
452 :
453 0 : if (aFlags & nsIPrintSettings::kInitSavePaperData) {
454 0 : if (GETINTPREF(kPrintPaperData, &iVal)) {
455 0 : aPS->SetPaperData(iVal);
456 : DUMP_INT(kReadStr, kPrintPaperData, iVal);
457 : }
458 : }
459 :
460 0 : if (aFlags & nsIPrintSettings::kInitSaveColorspace) {
461 0 : if (GETSTRPREF(kPrintColorspace, &str)) {
462 0 : aPS->SetColorspace(str.get());
463 : DUMP_STR(kReadStr, kPrintColorspace, str.get());
464 : }
465 : }
466 :
467 0 : if (aFlags & nsIPrintSettings::kInitSaveResolutionName) {
468 0 : if (GETSTRPREF(kPrintResolutionName, &str)) {
469 0 : aPS->SetResolutionName(str.get());
470 : DUMP_STR(kReadStr, kPrintResolutionName, str.get());
471 : }
472 : }
473 :
474 0 : if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) {
475 0 : if (GETBOOLPREF(kPrintDownloadFonts, &b)) {
476 0 : aPS->SetDownloadFonts(b);
477 : DUMP_BOOL(kReadStr, kPrintDownloadFonts, b);
478 : }
479 : }
480 :
481 0 : if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
482 0 : if (GETINTPREF(kPrintOrientation, &iVal)) {
483 0 : aPS->SetOrientation(iVal);
484 : DUMP_INT(kReadStr, kPrintOrientation, iVal);
485 : }
486 : }
487 :
488 0 : if (aFlags & nsIPrintSettings::kInitSavePrintCommand) {
489 0 : if (GETSTRPREF(kPrintCommand, &str)) {
490 0 : aPS->SetPrintCommand(str.get());
491 : DUMP_STR(kReadStr, kPrintCommand, str.get());
492 : }
493 : }
494 :
495 0 : if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
496 0 : if (GETBOOLPREF(kPrintToFile, &b)) {
497 0 : aPS->SetPrintToFile(b);
498 : DUMP_BOOL(kReadStr, kPrintToFile, b);
499 : }
500 : }
501 :
502 0 : if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
503 0 : if (GETSTRPREF(kPrintToFileName, &str)) {
504 0 : aPS->SetToFileName(str.get());
505 : DUMP_STR(kReadStr, kPrintToFileName, str.get());
506 : }
507 : }
508 :
509 0 : if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
510 0 : if (GETINTPREF(kPrintPageDelay, &iVal)) {
511 0 : aPS->SetPrintPageDelay(iVal);
512 : DUMP_INT(kReadStr, kPrintPageDelay, iVal);
513 : }
514 : }
515 :
516 0 : if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
517 0 : if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
518 0 : aPS->SetShrinkToFit(b);
519 : DUMP_BOOL(kReadStr, kPrintShrinkToFit, b);
520 : }
521 : }
522 :
523 0 : if (aFlags & nsIPrintSettings::kInitSaveScaling) {
524 0 : if (GETDBLPREF(kPrintScaling, dbl)) {
525 0 : aPS->SetScaling(dbl);
526 : DUMP_DBL(kReadStr, kPrintScaling, dbl);
527 : }
528 : }
529 :
530 : // Not Reading In:
531 : // Number of Copies
532 :
533 0 : return NS_OK;
534 : }
535 :
536 : /** ---------------------------------------------------
537 : * See documentation in nsPrintOptionsImpl.h
538 : * @update 1/12/01 rods
539 : */
540 : nsresult
541 0 : nsPrintOptions::WritePrefs(nsIPrintSettings *aPS, const nsAString& aPrinterName,
542 : PRUint32 aFlags)
543 : {
544 0 : NS_ENSURE_ARG_POINTER(aPS);
545 :
546 0 : nsIntMargin margin;
547 0 : if (aFlags & nsIPrintSettings::kInitSaveMargins) {
548 0 : if (NS_SUCCEEDED(aPS->GetMarginInTwips(margin))) {
549 : WriteInchesFromTwipsPref(GetPrefName(kMarginTop, aPrinterName),
550 0 : margin.top);
551 : DUMP_INT(kWriteStr, kMarginTop, margin.top);
552 : WriteInchesFromTwipsPref(GetPrefName(kMarginLeft, aPrinterName),
553 0 : margin.left);
554 : DUMP_INT(kWriteStr, kMarginLeft, margin.top);
555 : WriteInchesFromTwipsPref(GetPrefName(kMarginBottom, aPrinterName),
556 0 : margin.bottom);
557 : DUMP_INT(kWriteStr, kMarginBottom, margin.top);
558 : WriteInchesFromTwipsPref(GetPrefName(kMarginRight, aPrinterName),
559 0 : margin.right);
560 : DUMP_INT(kWriteStr, kMarginRight, margin.top);
561 : }
562 : }
563 :
564 0 : nsIntMargin edge;
565 0 : if (aFlags & nsIPrintSettings::kInitSaveEdges) {
566 0 : if (NS_SUCCEEDED(aPS->GetEdgeInTwips(edge))) {
567 : WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop, aPrinterName),
568 0 : edge.top);
569 : DUMP_INT(kWriteStr, kEdgeTop, edge.top);
570 : WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft, aPrinterName),
571 0 : edge.left);
572 : DUMP_INT(kWriteStr, kEdgeLeft, edge.top);
573 : WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom, aPrinterName),
574 0 : edge.bottom);
575 : DUMP_INT(kWriteStr, kEdgeBottom, edge.top);
576 : WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight, aPrinterName),
577 0 : edge.right);
578 : DUMP_INT(kWriteStr, kEdgeRight, edge.top);
579 : }
580 : }
581 :
582 0 : nsIntMargin unwriteableMargin;
583 0 : if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
584 0 : if (NS_SUCCEEDED(aPS->GetUnwriteableMarginInTwips(unwriteableMargin))) {
585 : WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginTop, aPrinterName),
586 0 : unwriteableMargin.top);
587 : DUMP_INT(kWriteStr, kUnwriteableMarginTop, unwriteableMargin.top);
588 : WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginLeft, aPrinterName),
589 0 : unwriteableMargin.left);
590 : DUMP_INT(kWriteStr, kUnwriteableMarginLeft, unwriteableMargin.top);
591 : WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginBottom, aPrinterName),
592 0 : unwriteableMargin.bottom);
593 : DUMP_INT(kWriteStr, kUnwriteableMarginBottom, unwriteableMargin.top);
594 : WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginRight, aPrinterName),
595 0 : unwriteableMargin.right);
596 : DUMP_INT(kWriteStr, kUnwriteableMarginRight, unwriteableMargin.top);
597 : }
598 : }
599 :
600 : // Paper size prefs are saved as a group
601 0 : if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
602 : PRInt16 sizeUnit, sizeType;
603 : double width, height;
604 : PRUnichar *name;
605 :
606 0 : if (
607 0 : NS_SUCCEEDED(aPS->GetPaperSizeUnit(&sizeUnit)) &&
608 0 : NS_SUCCEEDED(aPS->GetPaperSizeType(&sizeType)) &&
609 0 : NS_SUCCEEDED(aPS->GetPaperWidth(&width)) &&
610 0 : NS_SUCCEEDED(aPS->GetPaperHeight(&height)) &&
611 0 : NS_SUCCEEDED(aPS->GetPaperName(&name))
612 : ) {
613 : DUMP_INT(kWriteStr, kPrintPaperSizeUnit, sizeUnit);
614 : Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit, aPrinterName),
615 0 : PRInt32(sizeUnit));
616 : DUMP_INT(kWriteStr, kPrintPaperSizeType, sizeType);
617 : Preferences::SetInt(GetPrefName(kPrintPaperSizeType, aPrinterName),
618 0 : PRInt32(sizeType));
619 : DUMP_DBL(kWriteStr, kPrintPaperWidth, width);
620 0 : WritePrefDouble(GetPrefName(kPrintPaperWidth, aPrinterName), width);
621 : DUMP_DBL(kWriteStr, kPrintPaperHeight, height);
622 0 : WritePrefDouble(GetPrefName(kPrintPaperHeight, aPrinterName), height);
623 : DUMP_STR(kWriteStr, kPrintPaperName, name);
624 0 : Preferences::SetString(GetPrefName(kPrintPaperName, aPrinterName), name);
625 : }
626 : }
627 :
628 : bool b;
629 : PRUnichar* uStr;
630 : PRInt32 iVal;
631 : PRInt16 iVal16;
632 : double dbl;
633 :
634 0 : if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
635 0 : if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintEvenPages,
636 : &b))) {
637 : DUMP_BOOL(kWriteStr, kPrintEvenPages, b);
638 0 : Preferences::SetBool(GetPrefName(kPrintEvenPages, aPrinterName), b);
639 : }
640 : }
641 :
642 0 : if (aFlags & nsIPrintSettings::kInitSaveOddEvenPages) {
643 0 : if (NS_SUCCEEDED(aPS->GetPrintOptions(nsIPrintSettings::kPrintOddPages,
644 : &b))) {
645 : DUMP_BOOL(kWriteStr, kPrintOddPages, b);
646 0 : Preferences::SetBool(GetPrefName(kPrintOddPages, aPrinterName), b);
647 : }
648 : }
649 :
650 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) {
651 0 : if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(&uStr))) {
652 : DUMP_STR(kWriteStr, kPrintHeaderStrLeft, uStr);
653 : Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
654 0 : uStr);
655 : }
656 : }
657 :
658 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) {
659 0 : if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(&uStr))) {
660 : DUMP_STR(kWriteStr, kPrintHeaderStrCenter, uStr);
661 : Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
662 0 : uStr);
663 : }
664 : }
665 :
666 0 : if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) {
667 0 : if (NS_SUCCEEDED(aPS->GetHeaderStrRight(&uStr))) {
668 : DUMP_STR(kWriteStr, kPrintHeaderStrRight, uStr);
669 : Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
670 0 : uStr);
671 : }
672 : }
673 :
674 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) {
675 0 : if (NS_SUCCEEDED(aPS->GetFooterStrLeft(&uStr))) {
676 : DUMP_STR(kWriteStr, kPrintFooterStrLeft, uStr);
677 : Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
678 0 : uStr);
679 : }
680 : }
681 :
682 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) {
683 0 : if (NS_SUCCEEDED(aPS->GetFooterStrCenter(&uStr))) {
684 : DUMP_STR(kWriteStr, kPrintFooterStrCenter, uStr);
685 : Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
686 0 : uStr);
687 : }
688 : }
689 :
690 0 : if (aFlags & nsIPrintSettings::kInitSaveFooterRight) {
691 0 : if (NS_SUCCEEDED(aPS->GetFooterStrRight(&uStr))) {
692 : DUMP_STR(kWriteStr, kPrintFooterStrRight, uStr);
693 : Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
694 0 : uStr);
695 : }
696 : }
697 :
698 0 : if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
699 0 : if (NS_SUCCEEDED(aPS->GetPrintBGColors(&b))) {
700 : DUMP_BOOL(kWriteStr, kPrintBGColors, b);
701 0 : Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
702 : }
703 : }
704 :
705 0 : if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
706 0 : if (NS_SUCCEEDED(aPS->GetPrintBGImages(&b))) {
707 : DUMP_BOOL(kWriteStr, kPrintBGImages, b);
708 0 : Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
709 : }
710 : }
711 :
712 0 : if (aFlags & nsIPrintSettings::kInitSaveReversed) {
713 0 : if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
714 : DUMP_BOOL(kWriteStr, kPrintReversed, b);
715 0 : Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
716 : }
717 : }
718 :
719 0 : if (aFlags & nsIPrintSettings::kInitSaveInColor) {
720 0 : if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
721 : DUMP_BOOL(kWriteStr, kPrintInColor, b);
722 0 : Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
723 : }
724 : }
725 :
726 0 : if (aFlags & nsIPrintSettings::kInitSavePlexName) {
727 0 : if (NS_SUCCEEDED(aPS->GetPlexName(&uStr))) {
728 : DUMP_STR(kWriteStr, kPrintPlexName, uStr);
729 0 : Preferences::SetString(GetPrefName(kPrintPlexName, aPrinterName), uStr);
730 : }
731 : }
732 :
733 0 : if (aFlags & nsIPrintSettings::kInitSavePaperData) {
734 0 : if (NS_SUCCEEDED(aPS->GetPaperData(&iVal16))) {
735 : DUMP_INT(kWriteStr, kPrintPaperData, iVal16);
736 : Preferences::SetInt(GetPrefName(kPrintPaperData, aPrinterName),
737 0 : PRInt32(iVal16));
738 : }
739 : }
740 :
741 0 : if (aFlags & nsIPrintSettings::kInitSaveColorspace) {
742 0 : if (NS_SUCCEEDED(aPS->GetColorspace(&uStr))) {
743 : DUMP_STR(kWriteStr, kPrintColorspace, uStr);
744 0 : Preferences::SetString(GetPrefName(kPrintColorspace, aPrinterName), uStr);
745 : }
746 : }
747 :
748 0 : if (aFlags & nsIPrintSettings::kInitSaveResolutionName) {
749 0 : if (NS_SUCCEEDED(aPS->GetResolutionName(&uStr))) {
750 : DUMP_STR(kWriteStr, kPrintResolutionName, uStr);
751 : Preferences::SetString(GetPrefName(kPrintResolutionName, aPrinterName),
752 0 : uStr);
753 : }
754 : }
755 :
756 0 : if (aFlags & nsIPrintSettings::kInitSaveDownloadFonts) {
757 0 : if (NS_SUCCEEDED(aPS->GetDownloadFonts(&b))) {
758 : DUMP_BOOL(kWriteStr, kPrintDownloadFonts, b);
759 0 : Preferences::SetBool(GetPrefName(kPrintDownloadFonts, aPrinterName), b);
760 : }
761 : }
762 :
763 0 : if (aFlags & nsIPrintSettings::kInitSaveOrientation) {
764 0 : if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
765 : DUMP_INT(kWriteStr, kPrintOrientation, iVal);
766 0 : Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
767 : }
768 : }
769 :
770 0 : if (aFlags & nsIPrintSettings::kInitSavePrintCommand) {
771 0 : if (NS_SUCCEEDED(aPS->GetPrintCommand(&uStr))) {
772 : DUMP_STR(kWriteStr, kPrintCommand, uStr);
773 0 : Preferences::SetString(GetPrefName(kPrintCommand, aPrinterName), uStr);
774 : }
775 : }
776 :
777 : // Only the general version of this pref is saved
778 0 : if ((aFlags & nsIPrintSettings::kInitSavePrinterName)
779 0 : && aPrinterName.IsEmpty()) {
780 0 : if (NS_SUCCEEDED(aPS->GetPrinterName(&uStr))) {
781 : DUMP_STR(kWriteStr, kPrinterName, uStr);
782 0 : Preferences::SetString(kPrinterName, uStr);
783 : }
784 : }
785 :
786 0 : if (aFlags & nsIPrintSettings::kInitSavePrintToFile) {
787 0 : if (NS_SUCCEEDED(aPS->GetPrintToFile(&b))) {
788 : DUMP_BOOL(kWriteStr, kPrintToFile, b);
789 0 : Preferences::SetBool(GetPrefName(kPrintToFile, aPrinterName), b);
790 : }
791 : }
792 :
793 0 : if (aFlags & nsIPrintSettings::kInitSaveToFileName) {
794 0 : if (NS_SUCCEEDED(aPS->GetToFileName(&uStr))) {
795 : DUMP_STR(kWriteStr, kPrintToFileName, uStr);
796 0 : Preferences::SetString(GetPrefName(kPrintToFileName, aPrinterName), uStr);
797 : }
798 : }
799 :
800 0 : if (aFlags & nsIPrintSettings::kInitSavePageDelay) {
801 0 : if (NS_SUCCEEDED(aPS->GetPrintPageDelay(&iVal))) {
802 : DUMP_INT(kWriteStr, kPrintPageDelay, iVal);
803 0 : Preferences::SetInt(GetPrefName(kPrintPageDelay, aPrinterName), iVal);
804 : }
805 : }
806 :
807 0 : if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) {
808 0 : if (NS_SUCCEEDED(aPS->GetShrinkToFit(&b))) {
809 : DUMP_BOOL(kWriteStr, kPrintShrinkToFit, b);
810 0 : Preferences::SetBool(GetPrefName(kPrintShrinkToFit, aPrinterName), b);
811 : }
812 : }
813 :
814 0 : if (aFlags & nsIPrintSettings::kInitSaveScaling) {
815 0 : if (NS_SUCCEEDED(aPS->GetScaling(&dbl))) {
816 : DUMP_DBL(kWriteStr, kPrintScaling, dbl);
817 0 : WritePrefDouble(GetPrefName(kPrintScaling, aPrinterName), dbl);
818 : }
819 : }
820 :
821 : // Not Writing Out:
822 : // Number of Copies
823 :
824 0 : return NS_OK;
825 : }
826 :
827 : NS_IMETHODIMP
828 0 : nsPrintOptions::DisplayJobProperties(const PRUnichar *aPrinter,
829 : nsIPrintSettings* aPrintSettings,
830 : bool *aDisplayed)
831 : {
832 0 : NS_ENSURE_ARG_POINTER(aPrinter);
833 0 : *aDisplayed = false;
834 :
835 : nsresult rv;
836 : nsCOMPtr<nsIPrinterEnumerator> propDlg =
837 0 : do_CreateInstance(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv);
838 0 : NS_ENSURE_SUCCESS(rv, rv);
839 :
840 0 : NS_ENSURE_ARG_POINTER(aPrintSettings);
841 0 : rv = propDlg->DisplayPropertiesDlg(aPrinter, aPrintSettings);
842 0 : NS_ENSURE_SUCCESS(rv, rv);
843 :
844 0 : *aDisplayed = true;
845 :
846 0 : return rv;
847 : }
848 :
849 0 : NS_IMETHODIMP nsPrintOptions::GetNativeData(PRInt16 aDataType, void * *_retval)
850 : {
851 0 : return NS_ERROR_NOT_IMPLEMENTED;
852 : }
853 :
854 0 : nsresult nsPrintOptions::_CreatePrintSettings(nsIPrintSettings **_retval)
855 : {
856 : // does not initially ref count
857 0 : nsPrintSettings * printSettings = new nsPrintSettings();
858 0 : NS_ENSURE_TRUE(printSettings, NS_ERROR_OUT_OF_MEMORY);
859 :
860 0 : NS_ADDREF(*_retval = printSettings); // ref count
861 :
862 0 : nsXPIDLString printerName;
863 0 : nsresult rv = GetDefaultPrinterName(getter_Copies(printerName));
864 0 : NS_ENSURE_SUCCESS(rv, rv);
865 0 : (*_retval)->SetPrinterName(printerName.get());
866 :
867 : (void)InitPrintSettingsFromPrefs(*_retval, false,
868 0 : nsIPrintSettings::kInitSaveAll);
869 :
870 0 : return NS_OK;
871 : }
872 :
873 0 : NS_IMETHODIMP nsPrintOptions::CreatePrintSettings(nsIPrintSettings **_retval)
874 : {
875 0 : return _CreatePrintSettings(_retval);
876 : }
877 :
878 : NS_IMETHODIMP
879 0 : nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings **aGlobalPrintSettings)
880 : {
881 : nsresult rv;
882 :
883 0 : rv = CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings));
884 0 : NS_ENSURE_SUCCESS(rv, rv);
885 :
886 0 : NS_ADDREF(*aGlobalPrintSettings = mGlobalPrintSettings.get());
887 :
888 0 : return rv;
889 : }
890 :
891 : NS_IMETHODIMP
892 0 : nsPrintOptions::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings)
893 : {
894 0 : return CreatePrintSettings(aNewPrintSettings);
895 : }
896 :
897 : NS_IMETHODIMP
898 0 : nsPrintOptions::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName)
899 : {
900 : nsresult rv;
901 : nsCOMPtr<nsIPrinterEnumerator> prtEnum =
902 0 : do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv);
903 0 : NS_ENSURE_SUCCESS(rv, rv);
904 :
905 : // Look up the printer from the last print job
906 0 : nsAutoString lastPrinterName;
907 0 : Preferences::GetString(kPrinterName, &lastPrinterName);
908 0 : if (!lastPrinterName.IsEmpty()) {
909 : // Verify it's still a valid printer
910 0 : nsCOMPtr<nsIStringEnumerator> printers;
911 0 : rv = prtEnum->GetPrinterNameList(getter_AddRefs(printers));
912 0 : if (NS_SUCCEEDED(rv)) {
913 0 : bool isValid = false;
914 : bool hasMore;
915 0 : while (NS_SUCCEEDED(printers->HasMore(&hasMore)) && hasMore) {
916 0 : nsAutoString printer;
917 0 : if (NS_SUCCEEDED(printers->GetNext(printer)) && lastPrinterName.Equals(printer)) {
918 0 : isValid = true;
919 : break;
920 : }
921 : }
922 0 : if (isValid) {
923 0 : *aDefaultPrinterName = ToNewUnicode(lastPrinterName);
924 0 : return NS_OK;
925 : }
926 : }
927 : }
928 :
929 : // There is no last printer preference, or it doesn't name a valid printer.
930 : // Return the default from the printer enumeration.
931 0 : return prtEnum->GetDefaultPrinterName(aDefaultPrinterName);
932 : }
933 :
934 : NS_IMETHODIMP
935 0 : nsPrintOptions::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName,
936 : nsIPrintSettings *aPrintSettings)
937 : {
938 0 : NS_ENSURE_ARG_POINTER(aPrintSettings);
939 0 : NS_ENSURE_ARG_POINTER(aPrinterName);
940 :
941 : #ifdef NS_DEBUG
942 0 : nsXPIDLString printerName;
943 0 : aPrintSettings->GetPrinterName(getter_Copies(printerName));
944 0 : if (!printerName.Equals(aPrinterName)) {
945 0 : NS_WARNING("Printer names should match!");
946 : }
947 : #endif
948 :
949 : bool isInitialized;
950 0 : aPrintSettings->GetIsInitializedFromPrinter(&isInitialized);
951 0 : if (isInitialized)
952 0 : return NS_OK;
953 :
954 : nsresult rv;
955 : nsCOMPtr<nsIPrinterEnumerator> prtEnum =
956 0 : do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID, &rv);
957 0 : NS_ENSURE_SUCCESS(rv, rv);
958 :
959 0 : rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings);
960 0 : NS_ENSURE_SUCCESS(rv, rv);
961 :
962 0 : aPrintSettings->SetIsInitializedFromPrinter(true);
963 0 : return rv;
964 : }
965 :
966 : /** ---------------------------------------------------
967 : * Helper function - Returns either the name or sets the length to zero
968 : */
969 : static nsresult
970 0 : GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP,
971 : nsAString& aPrinterName)
972 : {
973 0 : NS_ENSURE_ARG_POINTER(aPS);
974 :
975 0 : aPrinterName.Truncate();
976 0 : if (!aUsePNP)
977 0 : return NS_OK;
978 :
979 : // Get the Printer Name from the PrintSettings
980 : // to use as a prefix for Pref Names
981 0 : PRUnichar* prtName = nsnull;
982 :
983 0 : nsresult rv = aPS->GetPrinterName(&prtName);
984 0 : NS_ENSURE_SUCCESS(rv, rv);
985 :
986 0 : aPrinterName = nsDependentString(prtName);
987 :
988 : // Convert any whitespaces, carriage returns or newlines to _
989 : // The below algorithm is supposedly faster than using iterators
990 0 : NS_NAMED_LITERAL_STRING(replSubstr, "_");
991 0 : const char* replaceStr = " \n\r";
992 :
993 : PRInt32 x;
994 0 : for (x=0; x < (PRInt32)strlen(replaceStr); x++) {
995 0 : PRUnichar uChar = replaceStr[x];
996 :
997 0 : PRInt32 i = 0;
998 0 : while ((i = aPrinterName.FindChar(uChar, i)) != kNotFound) {
999 0 : aPrinterName.Replace(i, 1, replSubstr);
1000 0 : i++;
1001 : }
1002 : }
1003 0 : return NS_OK;
1004 : }
1005 :
1006 : NS_IMETHODIMP
1007 0 : nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings *aPrintSettings,
1008 : const PRUnichar *aPrefName, PRInt32 *_retval)
1009 : {
1010 0 : NS_ENSURE_ARG_POINTER(aPrintSettings);
1011 0 : NS_ENSURE_ARG_POINTER(aPrefName);
1012 :
1013 0 : nsAutoString prtName;
1014 : // Get the Printer Name from the PrintSettings
1015 : // to use as a prefix for Pref Names
1016 0 : GetAdjustedPrinterName(aPrintSettings, true, prtName);
1017 :
1018 : const char* prefName =
1019 0 : GetPrefName(NS_LossyConvertUTF16toASCII(aPrefName).get(), prtName);
1020 :
1021 0 : NS_ENSURE_TRUE(prefName, NS_ERROR_FAILURE);
1022 :
1023 : PRInt32 iVal;
1024 0 : nsresult rv = Preferences::GetInt(prefName, &iVal);
1025 0 : NS_ENSURE_SUCCESS(rv, rv);
1026 :
1027 0 : *_retval = iVal;
1028 0 : return rv;
1029 : }
1030 :
1031 : NS_IMETHODIMP
1032 0 : nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings* aPS,
1033 : bool aUsePNP, PRUint32 aFlags)
1034 : {
1035 0 : NS_ENSURE_ARG_POINTER(aPS);
1036 :
1037 : bool isInitialized;
1038 0 : aPS->GetIsInitializedFromPrefs(&isInitialized);
1039 :
1040 0 : if (isInitialized)
1041 0 : return NS_OK;
1042 :
1043 0 : nsAutoString prtName;
1044 : // read any non printer specific prefs
1045 : // with empty printer name
1046 0 : nsresult rv = ReadPrefs(aPS, prtName, aFlags);
1047 0 : NS_ENSURE_SUCCESS(rv, rv);
1048 :
1049 : // Get the Printer Name from the PrintSettings
1050 : // to use as a prefix for Pref Names
1051 0 : rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
1052 0 : NS_ENSURE_SUCCESS(rv, rv);
1053 :
1054 0 : if (prtName.IsEmpty()) {
1055 0 : NS_WARNING("Caller should supply a printer name.");
1056 0 : return NS_OK;
1057 : }
1058 :
1059 : // Now read any printer specific prefs
1060 0 : rv = ReadPrefs(aPS, prtName, aFlags);
1061 0 : if (NS_SUCCEEDED(rv))
1062 0 : aPS->SetIsInitializedFromPrefs(true);
1063 :
1064 0 : return NS_OK;
1065 : }
1066 :
1067 : /**
1068 : * Save all of the printer settings; if we can find a printer name, save
1069 : * printer-specific preferences. Otherwise, save generic ones.
1070 : */
1071 : nsresult
1072 0 : nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings *aPS,
1073 : bool aUsePrinterNamePrefix,
1074 : PRUint32 aFlags)
1075 : {
1076 0 : NS_ENSURE_ARG_POINTER(aPS);
1077 0 : nsAutoString prtName;
1078 :
1079 : // Get the printer name from the PrinterSettings for an optional prefix.
1080 0 : nsresult rv = GetAdjustedPrinterName(aPS, aUsePrinterNamePrefix, prtName);
1081 0 : NS_ENSURE_SUCCESS(rv, rv);
1082 :
1083 : // Write the prefs, with or without a printer name prefix.
1084 0 : return WritePrefs(aPS, prtName, aFlags);
1085 : }
1086 :
1087 :
1088 : //-----------------------------------------------------
1089 : //-- Protected Methods --------------------------------
1090 : //-----------------------------------------------------
1091 : nsresult
1092 0 : nsPrintOptions::ReadPrefDouble(const char * aPrefId, double& aVal)
1093 : {
1094 0 : NS_ENSURE_ARG_POINTER(aPrefId);
1095 :
1096 0 : nsCAutoString str;
1097 0 : nsresult rv = Preferences::GetCString(aPrefId, &str);
1098 0 : if (NS_SUCCEEDED(rv) && !str.IsEmpty()) {
1099 0 : aVal = atof(str.get());
1100 : }
1101 0 : return rv;
1102 : }
1103 :
1104 : nsresult
1105 0 : nsPrintOptions::WritePrefDouble(const char * aPrefId, double aVal)
1106 : {
1107 0 : NS_ENSURE_ARG_POINTER(aPrefId);
1108 :
1109 0 : nsPrintfCString str("%6.2f", aVal);
1110 0 : NS_ENSURE_TRUE(!str.IsEmpty(), NS_ERROR_FAILURE);
1111 :
1112 0 : return Preferences::SetCString(aPrefId, str);
1113 : }
1114 :
1115 : void
1116 0 : nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId, PRInt32& aTwips,
1117 : const char * aMarginPref)
1118 : {
1119 0 : nsAutoString str;
1120 0 : nsresult rv = Preferences::GetString(aPrefId, &str);
1121 0 : if (NS_FAILED(rv) || str.IsEmpty()) {
1122 0 : rv = Preferences::GetString(aMarginPref, &str);
1123 : }
1124 0 : if (NS_SUCCEEDED(rv) && !str.IsEmpty()) {
1125 : PRInt32 errCode;
1126 0 : float inches = str.ToFloat(&errCode);
1127 0 : if (NS_SUCCEEDED(errCode)) {
1128 0 : aTwips = NS_INCHES_TO_INT_TWIPS(inches);
1129 : } else {
1130 0 : aTwips = 0;
1131 : }
1132 : }
1133 0 : }
1134 :
1135 : void
1136 0 : nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId, PRInt32 aTwips)
1137 : {
1138 0 : double inches = NS_TWIPS_TO_INCHES(aTwips);
1139 0 : nsCAutoString inchesStr;
1140 0 : inchesStr.AppendFloat(inches);
1141 :
1142 0 : Preferences::SetCString(aPrefId, inchesStr);
1143 0 : }
1144 :
1145 : void
1146 0 : nsPrintOptions::ReadInchesIntToTwipsPref(const char * aPrefId, PRInt32& aTwips,
1147 : const char * aMarginPref)
1148 : {
1149 : PRInt32 value;
1150 0 : nsresult rv = Preferences::GetInt(aPrefId, &value);
1151 0 : if (NS_FAILED(rv)) {
1152 0 : rv = Preferences::GetInt(aMarginPref, &value);
1153 : }
1154 0 : if (NS_SUCCEEDED(rv)) {
1155 0 : aTwips = NS_INCHES_TO_INT_TWIPS(float(value)/100.0f);
1156 : } else {
1157 0 : aTwips = 0;
1158 : }
1159 0 : }
1160 :
1161 : void
1162 0 : nsPrintOptions::WriteInchesIntFromTwipsPref(const char * aPrefId, PRInt32 aTwips)
1163 : {
1164 : Preferences::SetInt(aPrefId,
1165 0 : PRInt32(NS_TWIPS_TO_INCHES(aTwips) * 100.0f + 0.5f));
1166 0 : }
1167 :
1168 : void
1169 0 : nsPrintOptions::ReadJustification(const char * aPrefId, PRInt16& aJust,
1170 : PRInt16 aInitValue)
1171 : {
1172 0 : aJust = aInitValue;
1173 0 : nsAutoString justStr;
1174 0 : if (NS_SUCCEEDED(Preferences::GetString(aPrefId, &justStr))) {
1175 0 : if (justStr.EqualsASCII(kJustRight)) {
1176 0 : aJust = nsIPrintSettings::kJustRight;
1177 0 : } else if (justStr.EqualsASCII(kJustCenter)) {
1178 0 : aJust = nsIPrintSettings::kJustCenter;
1179 : } else {
1180 0 : aJust = nsIPrintSettings::kJustLeft;
1181 : }
1182 : }
1183 0 : }
1184 :
1185 : //---------------------------------------------------
1186 : void
1187 0 : nsPrintOptions::WriteJustification(const char * aPrefId, PRInt16 aJust)
1188 : {
1189 0 : switch (aJust) {
1190 : case nsIPrintSettings::kJustLeft:
1191 0 : Preferences::SetCString(aPrefId, kJustLeft);
1192 0 : break;
1193 :
1194 : case nsIPrintSettings::kJustCenter:
1195 0 : Preferences::SetCString(aPrefId, kJustCenter);
1196 0 : break;
1197 :
1198 : case nsIPrintSettings::kJustRight:
1199 0 : Preferences::SetCString(aPrefId, kJustRight);
1200 0 : break;
1201 : } //switch
1202 0 : }
1203 :
1204 : //----------------------------------------------------------------------
1205 : // Testing of read/write prefs
1206 : // This define turns on the testing module below
1207 : // so at start up it writes and reads the prefs.
1208 : #ifdef DEBUG_rods_X
1209 : class Tester {
1210 : public:
1211 : Tester();
1212 : };
1213 : Tester::Tester()
1214 : {
1215 : nsCOMPtr<nsIPrintSettings> ps;
1216 : nsresult rv;
1217 : nsCOMPtr<nsIPrintOptions> printService =
1218 : do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv);
1219 : if (NS_SUCCEEDED(rv)) {
1220 : rv = printService->CreatePrintSettings(getter_AddRefs(ps));
1221 : }
1222 :
1223 : if (ps) {
1224 : ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages, true);
1225 : ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, false);
1226 : ps->SetMarginTop(1.0);
1227 : ps->SetMarginLeft(1.0);
1228 : ps->SetMarginBottom(1.0);
1229 : ps->SetMarginRight(1.0);
1230 : ps->SetScaling(0.5);
1231 : ps->SetPrintBGColors(true);
1232 : ps->SetPrintBGImages(true);
1233 : ps->SetPrintRange(15);
1234 : ps->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1235 : ps->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1236 : ps->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get());
1237 : ps->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1238 : ps->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1239 : ps->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get());
1240 : ps->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get());
1241 : ps->SetPlexName(NS_ConvertUTF8toUTF16("Plex Name").get());
1242 : ps->SetPaperSizeType(10);
1243 : ps->SetPaperData(1);
1244 : ps->SetPaperWidth(100.0);
1245 : ps->SetPaperHeight(50.0);
1246 : ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters);
1247 : ps->SetPrintReversed(true);
1248 : ps->SetPrintInColor(true);
1249 : ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
1250 : ps->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get());
1251 : ps->SetNumCopies(2);
1252 : ps->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get());
1253 : ps->SetPrintToFile(true);
1254 : ps->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get());
1255 : ps->SetPrintPageDelay(1000);
1256 : ps->SetShrinkToFit(true);
1257 :
1258 : struct SettingsType {
1259 : const char* mName;
1260 : PRUint32 mFlag;
1261 : };
1262 : SettingsType gSettings[] = {
1263 : {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages},
1264 : {kPrintHeaderStrLeft, nsIPrintSettings::kInitSaveHeaderLeft},
1265 : {kPrintHeaderStrCenter, nsIPrintSettings::kInitSaveHeaderCenter},
1266 : {kPrintHeaderStrRight, nsIPrintSettings::kInitSaveHeaderRight},
1267 : {kPrintFooterStrLeft, nsIPrintSettings::kInitSaveFooterLeft},
1268 : {kPrintFooterStrCenter, nsIPrintSettings::kInitSaveFooterCenter},
1269 : {kPrintFooterStrRight, nsIPrintSettings::kInitSaveFooterRight},
1270 : {kPrintBGColors, nsIPrintSettings::kInitSaveBGColors},
1271 : {kPrintBGImages, nsIPrintSettings::kInitSaveBGImages},
1272 : {kPrintShrinkToFit, nsIPrintSettings::kInitSaveShrinkToFit},
1273 : {kPrintPaperSize, nsIPrintSettings::kInitSavePaperSize},
1274 : {kPrintPlexName, nsIPrintSettings::kInitSavePlexName},
1275 : {kPrintPaperData, nsIPrintSettings::kInitSavePaperData},
1276 : {kPrintReversed, nsIPrintSettings::kInitSaveReversed},
1277 : {kPrintInColor, nsIPrintSettings::kInitSaveInColor},
1278 : {kPrintColorspace, nsIPrintSettings::kInitSaveColorspace},
1279 : {kPrintResolutionName, nsIPrintSettings::kInitSaveResolutionName},
1280 : {kPrintDownloadFonts, nsIPrintSettings::kInitSaveDownloadFonts},
1281 : {kPrintOrientation, nsIPrintSettings::kInitSaveOrientation},
1282 : {kPrintCommand, nsIPrintSettings::kInitSavePrintCommand},
1283 : {kPrinterName, nsIPrintSettings::kInitSavePrinterName},
1284 : {kPrintToFile, nsIPrintSettings::kInitSavePrintToFile},
1285 : {kPrintToFileName, nsIPrintSettings::kInitSaveToFileName},
1286 : {kPrintPageDelay, nsIPrintSettings::kInitSavePageDelay},
1287 : {"Margins", nsIPrintSettings::kInitSaveMargins},
1288 : {"All", nsIPrintSettings::kInitSaveAll},
1289 : {nsnull, 0}};
1290 :
1291 : nsString prefix; prefix.AssignLiteral("Printer Name");
1292 : PRInt32 i = 0;
1293 : while (gSettings[i].mName != nsnull) {
1294 : printf("------------------------------------------------\n");
1295 : printf("%d) %s -> 0x%X\n", i, gSettings[i].mName, gSettings[i].mFlag);
1296 : printService->SavePrintSettingsToPrefs(ps, true, gSettings[i].mFlag);
1297 : printService->InitPrintSettingsFromPrefs(ps, true,
1298 : gSettings[i].mFlag);
1299 : i++;
1300 : }
1301 : }
1302 :
1303 : }
1304 : Tester gTester;
1305 : #endif
|