nsPrintOptionsImpl.cpp 49 KB

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