From a79325f4fd6fe3b29aa969a014e8e4676b90adde Mon Sep 17 00:00:00 2001 From: "pete.zha%sun.com" Date: Tue, 10 Sep 2002 05:15:53 +0000 Subject: [PATCH] Print settings on Linux are saved at shutdown but not read at next start bug=166217 r=rods@netscape.com sr=bryner@netscape.com a=asa Roland.Mainz@informatik.med.uni-giessen.de --- gfx/src/nsPrintOptionsImpl.cpp | 31 +------ gfx/src/windows/nsPrintOptionsWin.cpp | 68 ++++++++++++++- gfx/src/windows/nsPrintOptionsWin.h | 3 + .../resources/content/printPageSetup.js | 84 +++++++++++++++++-- .../resources/content/printPageSetup.js | 84 +++++++++++++++++-- xpfe/global/resources/content/printdialog.js | 53 ++++++------ .../resources/content/unix/printjoboptions.js | 39 ++++++--- 7 files changed, 281 insertions(+), 81 deletions(-) diff --git a/gfx/src/nsPrintOptionsImpl.cpp b/gfx/src/nsPrintOptionsImpl.cpp index 7b4866abdf8e..dfed57e5b790 100644 --- a/gfx/src/nsPrintOptionsImpl.cpp +++ b/gfx/src/nsPrintOptionsImpl.cpp @@ -872,20 +872,6 @@ NS_IMETHODIMP nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings * *aGlobal if (!mGlobalPrintSettings) { CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings)); NS_ASSERTION(mGlobalPrintSettings, "Can't be NULL!"); - // The very first we should initialize from the default printer - if (mGlobalPrintSettings) { - nsresult rv; - nsCOMPtr prtEnum = do_GetService(kPrinterEnumeratorCID, &rv); - if (NS_SUCCEEDED(rv)) { - PRUnichar* printerName = nsnull; - // Not sure if all platforms will return the proper error code - // so for insurance, make sure there is a printer name - if (NS_SUCCEEDED(prtEnum->GetDefaultPrinterName(&printerName)) && printerName && *printerName) { - prtEnum->InitPrintSettingsFromPrinter(printerName, mGlobalPrintSettings); - nsMemory::Free(printerName); - } - } - } } // If this still NULL, we have some very big problems going on @@ -904,11 +890,7 @@ NS_IMETHODIMP nsPrintOptions::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings) { NS_ENSURE_ARG_POINTER(aNewPrintSettings); - - nsresult rv = CreatePrintSettings(aNewPrintSettings); - NS_ENSURE_SUCCESS(rv, rv); - rv = InitPrintSettingsFromPrinter(nsnull, *aNewPrintSettings); - return rv; + return CreatePrintSettings(aNewPrintSettings); } //----------------------------------------------------------------------- @@ -930,19 +912,12 @@ NS_IMETHODIMP nsPrintOptions::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings) { NS_ENSURE_ARG_POINTER(aPrintSettings); + NS_ENSURE_ARG_POINTER(aPrinterName); - PRUnichar* printerName = nsnull; - if (!aPrinterName) { - GetDefaultPrinterName(&printerName); - if (!printerName || !*printerName) return NS_OK; - } nsresult rv; nsCOMPtr prtEnum = do_GetService(kPrinterEnumeratorCID, &rv); if (prtEnum) { - rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName?aPrinterName:printerName, aPrintSettings); - } - if (printerName) { - nsMemory::Free(printerName); + rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName, aPrintSettings); } return rv; } diff --git a/gfx/src/windows/nsPrintOptionsWin.cpp b/gfx/src/windows/nsPrintOptionsWin.cpp index 6fe421193142..a62849c55eb8 100644 --- a/gfx/src/windows/nsPrintOptionsWin.cpp +++ b/gfx/src/windows/nsPrintOptionsWin.cpp @@ -39,7 +39,9 @@ #include "nsPrintOptionsWin.h" #include "nsPrintSettingsWin.h" - +#include "nsGfxCIID.h" +#include "nsIServiceManager.h" +const char kPrinterEnumeratorContractID[] = "@mozilla.org/gfx/printerenumerator;1"; /** --------------------------------------------------- * See documentation in nsPrintOptionsWin.h @@ -85,3 +87,67 @@ NS_IMETHODIMP nsPrintOptionsWin::CreatePrintSettings(nsIPrintSettings **_retval) return rv; } + +/* readonly attribute nsIPrintSettings globalPrintSettings; */ +NS_IMETHODIMP +nsPrintOptionsWin::GetGlobalPrintSettings(nsIPrintSettings * *aGlobalPrintSettings) +{ + if (!mGlobalPrintSettings) { + CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings)); + NS_ASSERTION(mGlobalPrintSettings, "Can't be NULL!"); + // If this still NULL, we have some very big problems going on + NS_ENSURE_TRUE(mGlobalPrintSettings, NS_ERROR_FAILURE); + + // The very first time we should initialize from the default printer + nsresult rv; + nsCOMPtr prtEnum = do_GetService(kPrinterEnumeratorContractID, &rv); + if (NS_SUCCEEDED(rv)) { + PRUnichar* printerName = nsnull; + // Not sure if all platforms will return the proper error code + // so for insurance, make sure there is a printer name + if (NS_SUCCEEDED(prtEnum->GetDefaultPrinterName(&printerName)) && printerName && *printerName) { + prtEnum->InitPrintSettingsFromPrinter(printerName, mGlobalPrintSettings); + nsMemory::Free(printerName); + } + } + } + + *aGlobalPrintSettings = mGlobalPrintSettings; + NS_ADDREF(*aGlobalPrintSettings); + + return NS_OK; +} + +/* readonly attribute nsIPrintSettings newPrintSettings; */ +NS_IMETHODIMP +nsPrintOptionsWin::GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings) +{ + NS_ENSURE_ARG_POINTER(aNewPrintSettings); + + nsresult rv = CreatePrintSettings(aNewPrintSettings); + NS_ENSURE_SUCCESS(rv, rv); + return InitPrintSettingsFromPrinter(nsnull, *aNewPrintSettings); +} + +//----------------------------------------------------------------------- +NS_IMETHODIMP +nsPrintOptionsWin::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings) +{ + NS_ENSURE_ARG_POINTER(aPrintSettings); + + PRUnichar* printerName = nsnull; + if (!aPrinterName) { + GetDefaultPrinterName(&printerName); + if (!printerName || !*printerName) return NS_OK; + } + nsresult rv; + nsCOMPtr prtEnum = do_GetService(kPrinterEnumeratorContractID, &rv); + if (prtEnum) { + rv = prtEnum->InitPrintSettingsFromPrinter(aPrinterName?aPrinterName:printerName, aPrintSettings); + } + if (printerName) { + nsMemory::Free(printerName); + } + return rv; +} + diff --git a/gfx/src/windows/nsPrintOptionsWin.h b/gfx/src/windows/nsPrintOptionsWin.h index 1cf4943a48c0..eeb8dbb54c50 100644 --- a/gfx/src/windows/nsPrintOptionsWin.h +++ b/gfx/src/windows/nsPrintOptionsWin.h @@ -36,6 +36,9 @@ public: virtual ~nsPrintOptionsWin(); NS_IMETHOD CreatePrintSettings(nsIPrintSettings **_retval); + NS_IMETHOD GetGlobalPrintSettings(nsIPrintSettings * *aGlobalPrintSettings); + NS_IMETHOD GetNewPrintSettings(nsIPrintSettings * *aNewPrintSettings); + NS_IMETHOD InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings); }; diff --git a/xpfe/communicator/resources/content/printPageSetup.js b/xpfe/communicator/resources/content/printPageSetup.js index cc055e1f2df4..5af4534b5e54 100644 --- a/xpfe/communicator/resources/content/printPageSetup.js +++ b/xpfe/communicator/resources/content/printPageSetup.js @@ -40,6 +40,8 @@ var gDialog; var paramBlock; +var gPrefs = null; +var gPrintService = null; var gPrintSettings = null; var gStringBundle = null; @@ -93,6 +95,19 @@ function initDialog() } +//--------------------------------------------------- +function isListOfPrinterFeaturesAvailable() +{ + var has_printerfeatures = false; + + try { + has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); + } catch(ex) { + } + + return has_printerfeatures; +} + //--------------------------------------------------- function checkDouble(element) { @@ -261,6 +276,23 @@ function hfIdToValue(node) return result; } +function setPrinterDefaultsForSelectedPrinter() +{ + if (gPrintSettings.printerName == "") { + gPrintSettings.printerName = gPrintService.defaultPrinterName; + } + + // First get any defaults from the printer + gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); + + // now augment them with any values from last time + gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll); + + if (gDoDebug) { + dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n"); + } +} + //--------------------------------------------------- function loadDialog() { @@ -270,15 +302,28 @@ function loadDialog() var print_margin_bottom = 0.5; var print_margin_right = 0.5; - if (gPrintSettings) { - print_orientation = gPrintSettings.orientation; - - print_margin_top = gPrintSettings.marginTop; - print_margin_left = gPrintSettings.marginLeft; - print_margin_right = gPrintSettings.marginRight; - print_margin_bottom = gPrintSettings.marginBottom; + try { + gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); + + gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]; + if (gPrintService) { + gPrintService = gPrintService.getService(); + if (gPrintService) { + gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService); + } + } + } catch(ex) { + dump("loadDialog: ex="+ex+"\n"); } + setPrinterDefaultsForSelectedPrinter(); + + print_orientation = gPrintSettings.orientation; + print_margin_top = gPrintSettings.marginTop; + print_margin_left = gPrintSettings.marginLeft; + print_margin_right = gPrintSettings.marginRight; + print_margin_bottom = gPrintSettings.marginBottom; + if (gDoDebug) { dump("print_orientation "+print_orientation+"\n"); @@ -332,6 +377,15 @@ function loadDialog() gDialog.scalingInput.value = getDoubleStr(gPrintSettings.scaling * 100.0, 3); + // Enable/disable widgets based in the information whether the selected + // printer supports the matching feature or not + if (isListOfPrinterFeaturesAvailable()) { + if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation")) + gDialog.orientation.removeAttribute("disabled"); + else + gDialog.orientation.setAttribute("disabled","true"); + } + // Give initial focus to the orientation radio group. // Done on a timeout due to to bug 103197. setTimeout( function() { gDialog.orientation.focus(); }, 0 ); @@ -416,6 +470,22 @@ function onAccept() dump("*** FATAL ERROR: No paramBlock\n"); } + var flags = gPrintSettingsInterface.kInitSaveMargins | + gPrintSettingsInterface.kInitSaveHeaderLeft | + gPrintSettingsInterface.kInitSaveHeaderCenter | + gPrintSettingsInterface.kInitSaveHeaderRight | + gPrintSettingsInterface.kInitSaveFooterLeft | + gPrintSettingsInterface.kInitSaveFooterCenter | + gPrintSettingsInterface.kInitSaveFooterRight | + gPrintSettingsInterface.kInitSaveBGColors | + gPrintSettingsInterface.kInitSaveBGImages | + gPrintSettingsInterface.kInitSaveInColor | + gPrintSettingsInterface.kInitSaveReversed | + gPrintSettingsInterface.kInitSaveOrientation | + gPrintSettingsInterface.kInitSaveOddEvenPages; + + gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags); + return true; } diff --git a/xpfe/global/resources/content/printPageSetup.js b/xpfe/global/resources/content/printPageSetup.js index cc055e1f2df4..5af4534b5e54 100644 --- a/xpfe/global/resources/content/printPageSetup.js +++ b/xpfe/global/resources/content/printPageSetup.js @@ -40,6 +40,8 @@ var gDialog; var paramBlock; +var gPrefs = null; +var gPrintService = null; var gPrintSettings = null; var gStringBundle = null; @@ -93,6 +95,19 @@ function initDialog() } +//--------------------------------------------------- +function isListOfPrinterFeaturesAvailable() +{ + var has_printerfeatures = false; + + try { + has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); + } catch(ex) { + } + + return has_printerfeatures; +} + //--------------------------------------------------- function checkDouble(element) { @@ -261,6 +276,23 @@ function hfIdToValue(node) return result; } +function setPrinterDefaultsForSelectedPrinter() +{ + if (gPrintSettings.printerName == "") { + gPrintSettings.printerName = gPrintService.defaultPrinterName; + } + + // First get any defaults from the printer + gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); + + // now augment them with any values from last time + gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll); + + if (gDoDebug) { + dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n"); + } +} + //--------------------------------------------------- function loadDialog() { @@ -270,15 +302,28 @@ function loadDialog() var print_margin_bottom = 0.5; var print_margin_right = 0.5; - if (gPrintSettings) { - print_orientation = gPrintSettings.orientation; - - print_margin_top = gPrintSettings.marginTop; - print_margin_left = gPrintSettings.marginLeft; - print_margin_right = gPrintSettings.marginRight; - print_margin_bottom = gPrintSettings.marginBottom; + try { + gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); + + gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]; + if (gPrintService) { + gPrintService = gPrintService.getService(); + if (gPrintService) { + gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService); + } + } + } catch(ex) { + dump("loadDialog: ex="+ex+"\n"); } + setPrinterDefaultsForSelectedPrinter(); + + print_orientation = gPrintSettings.orientation; + print_margin_top = gPrintSettings.marginTop; + print_margin_left = gPrintSettings.marginLeft; + print_margin_right = gPrintSettings.marginRight; + print_margin_bottom = gPrintSettings.marginBottom; + if (gDoDebug) { dump("print_orientation "+print_orientation+"\n"); @@ -332,6 +377,15 @@ function loadDialog() gDialog.scalingInput.value = getDoubleStr(gPrintSettings.scaling * 100.0, 3); + // Enable/disable widgets based in the information whether the selected + // printer supports the matching feature or not + if (isListOfPrinterFeaturesAvailable()) { + if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation")) + gDialog.orientation.removeAttribute("disabled"); + else + gDialog.orientation.setAttribute("disabled","true"); + } + // Give initial focus to the orientation radio group. // Done on a timeout due to to bug 103197. setTimeout( function() { gDialog.orientation.focus(); }, 0 ); @@ -416,6 +470,22 @@ function onAccept() dump("*** FATAL ERROR: No paramBlock\n"); } + var flags = gPrintSettingsInterface.kInitSaveMargins | + gPrintSettingsInterface.kInitSaveHeaderLeft | + gPrintSettingsInterface.kInitSaveHeaderCenter | + gPrintSettingsInterface.kInitSaveHeaderRight | + gPrintSettingsInterface.kInitSaveFooterLeft | + gPrintSettingsInterface.kInitSaveFooterCenter | + gPrintSettingsInterface.kInitSaveFooterRight | + gPrintSettingsInterface.kInitSaveBGColors | + gPrintSettingsInterface.kInitSaveBGImages | + gPrintSettingsInterface.kInitSaveInColor | + gPrintSettingsInterface.kInitSaveReversed | + gPrintSettingsInterface.kInitSaveOrientation | + gPrintSettingsInterface.kInitSaveOddEvenPages; + + gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags); + return true; } diff --git a/xpfe/global/resources/content/printdialog.js b/xpfe/global/resources/content/printdialog.js index 40cc6ea893e7..b5522d4d07c7 100644 --- a/xpfe/global/resources/content/printdialog.js +++ b/xpfe/global/resources/content/printdialog.js @@ -30,8 +30,8 @@ var printOptions = null; var gOriginalNumCopies = 1; var paramBlock; -var gPrintSettings = null; -var gPrinterName = ""; +var gPrefs = null; +var gPrintSettings = null; var gWebBrowserPrint = null; var default_file = "mozilla.ps"; var gPrintSetInterface = Components.interfaces.nsIPrintSettings; @@ -195,21 +195,16 @@ function getPrinters() // update gPrintSettings with the defaults for the selected printer function setPrinterDefaultsForSelectedPrinter() { - /* FixMe: We should save the old printer's values here... */ - - if (gPrintSettings.printerName != dialog.printerList.value) { - gPrintSettings.printerName = dialog.printerList.value; - - // First get any defaults from the printer - printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); - - var flags = gPrintSetInterface.kInitSavePaperSizeType | gPrintSetInterface.kInitSavePaperSizeUnit | - gPrintSetInterface.kInitSavePaperWidth | gPrintSetInterface.kInitSavePaperHeight | - gPrintSetInterface.kInitSavePaperName | - gPrintSetInterface.kInitSavePrintCommand; - - // now augment them with any values from last time - printService.initPrintSettingsFromPrefs(gPrintSettings, true, flags); + gPrintSettings.printerName = dialog.printerList.value; + + // First get any defaults from the printer + printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); + + // now augment them with any values from last time + printService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSetInterface.kInitSaveAll); + + if (doDebug) { + dump("setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', paperName='"+gPrintSettings.paperName+"'\n"); } } @@ -256,6 +251,8 @@ function loadDialog() var print_tofile = ""; try { + gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); + printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]; if (printService) { printService = printService.getService(); @@ -266,8 +263,10 @@ function loadDialog() } } catch(e) {} + // Note: getPrinters sets up the PrintToFile radio buttons and initalises gPrintSettings + getPrinters(); + if (gPrintSettings) { - gPrinterName = gPrintSettings.printerName; print_tofile = gPrintSettings.printToFile; gOriginalNumCopies = gPrintSettings.numCopies; @@ -312,9 +311,6 @@ function loadDialog() dialog.fileInput.value = print_file; - // NOTE: getPRinters sets up the PrintToFile radio buttons - getPrinters(); - if (gPrintSettings.toFileName != "") { dialog.fileInput.value = gPrintSettings.toFileName; } @@ -452,15 +448,16 @@ function onAccept() } var saveToPrefs = false; - var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); - if (prefs) { - saveToPrefs = prefs.getBoolPref("print.save_print_settings"); - } + + saveToPrefs = gPrefs.getBoolPref("print.save_print_settings"); if (saveToPrefs && printService != null) { - var flags = gPrintSetInterface.kInitSavePaperSizeType | gPrintSetInterface.kInitSavePaperSizeUnit | - gPrintSetInterface.kInitSavePaperWidth | gPrintSetInterface.kInitSavePaperHeight | - gPrintSetInterface.kInitSavePaperName | + var flags = gPrintSetInterface.kInitSavePaperSizeType | + gPrintSetInterface.kInitSavePaperSizeUnit | + gPrintSetInterface.kInitSavePaperWidth | + gPrintSetInterface.kInitSavePaperHeight | + gPrintSetInterface.kInitSavePaperName | + gPrintSetInterface.kInitSaveInColor | gPrintSetInterface.kInitSavePrintCommand; printService.savePrintSettingsToPrefs(gPrintSettings, true, flags); } diff --git a/xpfe/global/resources/content/unix/printjoboptions.js b/xpfe/global/resources/content/unix/printjoboptions.js index 5a5dd4fe4f8d..c0698db355a9 100644 --- a/xpfe/global/resources/content/unix/printjoboptions.js +++ b/xpfe/global/resources/content/unix/printjoboptions.js @@ -67,6 +67,19 @@ function checkDouble(element, maxVal) } } +//--------------------------------------------------- +function isListOfPrinterFeaturesAvailable() +{ + var has_printerfeatures = false; + + try { + has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); + } catch(ex) { + } + + return has_printerfeatures; +} + //--------------------------------------------------- function getDoubleStr(val, dec) { @@ -225,16 +238,8 @@ function createPaperArrayFromPrinterFeatures() //--------------------------------------------------- function createPaperArray() -{ - var use_paper_array_from_printerfeatures = false; - - try { - use_paper_array_from_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); - } - catch (e) { - } - - if (use_paper_array_from_printerfeatures) { +{ + if (isListOfPrinterFeaturesAvailable()) { createPaperArrayFromPrinterFeatures(); } else { @@ -310,6 +315,20 @@ function loadDialog() } createPaperSizeList(selectedInx); + + // Enable/disable widgets based in the information whether the selected + // printer supports the matching feature or not + if (isListOfPrinterFeaturesAvailable()) { + if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_spoolercommand")) + dialog.cmdInput.removeAttribute("disabled"); + else + dialog.cmdInput.setAttribute("disabled","true"); + + if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_paper_size")) + dialog.paperList.removeAttribute("disabled"); + else + dialog.paperList.setAttribute("disabled","true"); + } if (print_command == "") { print_command = default_command;