/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// These are legacy versions of the above UnwriteableMargin prefs. The new ones, // which are in twips, were introduced to more accurately record the values. staticconstchar kUnwriteableMarginTop[] = "print_unwriteable_margin_top"; staticconstchar kUnwriteableMarginLeft[] = "print_unwriteable_margin_left"; staticconstchar kUnwriteableMarginBottom[] = "print_unwriteable_margin_bottom"; staticconstchar kUnwriteableMarginRight[] = "print_unwriteable_margin_right";
// Initialize the platform-specific values that don't // default-initialize, so that we don't send uninitialized data over // IPC (which leads to valgrind warnings, and, for bools, fatal // assertions). // data->driverName() default-initializes // data->deviceName() default-initializes // data->GTKPrintSettings() default-initializes
settings->SetOutputDestination(
nsIPrintSettings::OutputDestinationType(data.outputDestination())); // Output stream intentionally unset, child processes shouldn't care about it.
/** --------------------------------------------------- * Helper function - Creates the "prefix" for the pref * It is either "print." * or "print.printer_<print name>."
*/ constchar* nsPrintSettingsService::GetPrefName(constchar* aPrefName, const nsAString& aPrinterName) { if (!aPrefName || !*aPrefName) {
NS_ERROR("Must have a valid pref name!"); return aPrefName;
}
/** * This will either read in the generic prefs (not specific to a printer) * or read the prefs in using the printer name to qualify. * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
*/
nsresult nsPrintSettingsService::ReadPrefs(nsIPrintSettings* aPS, const nsAString& aPrinterName,
uint32_t aFlags) {
NS_ENSURE_ARG_POINTER(aPS);
// Paper size prefs are read as a group if (aFlags & nsIPrintSettings::kInitSavePaperSize) {
gotPaperSizeFromPrefs = GETINTPREF(kPrintPaperSizeUnit, &iVal) &&
GETDBLPREF(kPrintPaperWidth, paperWidth) &&
GETDBLPREF(kPrintPaperHeight, paperHeight) &&
GETSTRPREF(kPrintPaperId, str);
paperSizeUnit = (int16_t)iVal;
if (gotPaperSizeFromPrefs) { // Bug 315687: Sanity check paper size to avoid paper size values in // mm when the size unit flag is inches. The value 100 is arbitrary // and can be changed.
gotPaperSizeFromPrefs =
(paperSizeUnit != nsIPrintSettings::kPaperSizeInches) ||
(paperWidth < 100.0) || (paperHeight < 100.0);
}
if (aFlags & nsIPrintSettings::kInitSaveUnwriteableMargins) {
nsIntMargin margin; bool allPrefsRead =
GETINTPREF(kUnwriteableMarginTopTwips, &margin.top.value) &&
GETINTPREF(kUnwriteableMarginRightTwips, &margin.right.value) &&
GETINTPREF(kUnwriteableMarginBottomTwips, &margin.bottom.value) &&
GETINTPREF(kUnwriteableMarginLeftTwips, &margin.left.value); if (!allPrefsRead) { // We failed to read the new unwritable margin twips prefs. Try to read // the old ones in case they exist.
allPrefsRead = ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginTop, aPrinterName),
margin.top.value) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginLeft, aPrinterName),
margin.left.value) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginBottom, aPrinterName),
margin.bottom.value) &&
ReadInchesIntToTwipsPref(
GetPrefName(kUnwriteableMarginRight, aPrinterName),
margin.right.value);
} // SetUnwriteableMarginInTwips does its own validation and drops negative // values individually. We still want to block overly large values though, // so we do that part of MarginIsOK manually. if (allPrefsRead && margin.LeftRight() < pageSizeInTwips.width &&
margin.TopBottom() < pageSizeInTwips.height) {
aPS->SetUnwriteableMarginInTwips(margin);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSavePrintToFile) { if (GETBOOLPREF(kPrintToFile, &b)) {
aPS->SetOutputDestination(
b ? nsIPrintSettings::kOutputDestinationFile
: nsIPrintSettings::kOutputDestinationPrinter);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveToFileName) { if (GETSTRPREF(kPrintToFileName, str)) { if (StringEndsWith(str, u".ps"_ns)) { // We only support PDF since bug 1425188 landed. Users may still have // prefs with .ps filenames if they last saved a file as Postscript // though, so we fix that up here. (The pref values will be // overwritten the next time they save to file as a PDF.)
str.Truncate(str.Length() - 2);
str.AppendLiteral("pdf");
}
aPS->SetToFileName(str);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveShrinkToFit) { if (GETBOOLPREF(kPrintShrinkToFit, &b)) {
aPS->SetShrinkToFit(b);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveScaling) { // The limits imposed here are fairly arbitrary and mainly intended to // purge bad values which tend to be negative and/or very large. If we // get complaints from users that settings outside these values "aren't // saved" then we can consider increasing them. if (GETDBLPREF(kPrintScaling, dbl) && dbl >= 0.05 && dbl <= 20) {
aPS->SetScaling(dbl);
noValidPrefsFound = false;
}
}
if (aFlags & nsIPrintSettings::kInitSaveDuplex) { if (GETINTPREF(kPrintDuplex, &iVal)) {
aPS->SetDuplex(iVal);
noValidPrefsFound = false;
}
}
// Not Reading In: // Number of Copies // Print Resolution
if (aFlags & nsIPrintSettings::kInitSaveHeaderLeft) { if (NS_SUCCEEDED(aPS->GetHeaderStrLeft(uStr))) {
Preferences::SetString(GetPrefName(kPrintHeaderStrLeft, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveHeaderCenter) { if (NS_SUCCEEDED(aPS->GetHeaderStrCenter(uStr))) {
Preferences::SetString(GetPrefName(kPrintHeaderStrCenter, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveHeaderRight) { if (NS_SUCCEEDED(aPS->GetHeaderStrRight(uStr))) {
Preferences::SetString(GetPrefName(kPrintHeaderStrRight, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterLeft) { if (NS_SUCCEEDED(aPS->GetFooterStrLeft(uStr))) {
Preferences::SetString(GetPrefName(kPrintFooterStrLeft, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterCenter) { if (NS_SUCCEEDED(aPS->GetFooterStrCenter(uStr))) {
Preferences::SetString(GetPrefName(kPrintFooterStrCenter, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveFooterRight) { if (NS_SUCCEEDED(aPS->GetFooterStrRight(uStr))) {
Preferences::SetString(GetPrefName(kPrintFooterStrRight, aPrinterName),
uStr);
}
}
if (aFlags & nsIPrintSettings::kInitSaveBGColors) {
b = aPS->GetPrintBGColors();
Preferences::SetBool(GetPrefName(kPrintBGColors, aPrinterName), b);
}
if (aFlags & nsIPrintSettings::kInitSaveBGImages) {
b = aPS->GetPrintBGImages();
Preferences::SetBool(GetPrefName(kPrintBGImages, aPrinterName), b);
}
if (aFlags & nsIPrintSettings::kInitSaveReversed) { if (NS_SUCCEEDED(aPS->GetPrintReversed(&b))) {
Preferences::SetBool(GetPrefName(kPrintReversed, aPrinterName), b);
}
}
if (aFlags & nsIPrintSettings::kInitSaveInColor) { if (NS_SUCCEEDED(aPS->GetPrintInColor(&b))) {
Preferences::SetBool(GetPrefName(kPrintInColor, aPrinterName), b);
}
}
if (aFlags & nsIPrintSettings::kInitSaveOrientation) { if (NS_SUCCEEDED(aPS->GetOrientation(&iVal))) {
Preferences::SetInt(GetPrefName(kPrintOrientation, aPrinterName), iVal);
}
}
// Only the general version of this pref is saved if ((aFlags & nsIPrintSettings::kInitSavePrinterName) &&
aPrinterName.IsEmpty()) { if (NS_SUCCEEDED(aPS->GetPrinterName(uStr))) {
Preferences::SetString(kPrinterName, uStr);
}
}
// For security reasons, we don't pass the printer name to content processes. // Once bug 1776169 is fixed, we can just assert that this is the parent // process. bool usePrinterName = XRE_IsParentProcess();
if (usePrinterName) {
nsAutoString printerName;
settings->GetPrinterName(printerName); if (printerName.IsEmpty()) {
GetLastUsedPrinterName(printerName);
settings->SetPrinterName(printerName);
}
InitPrintSettingsFromPrinter(printerName, settings);
}
NS_IMETHODIMP
nsPrintSettingsService::InitPrintSettingsFromPrinter( const nsAString& aPrinterName, nsIPrintSettings* aPrintSettings) { // Don't get print settings from the printer in the child when printing via // parent, these will be retrieved in the parent later in the print process. if (XRE_IsContentProcess()) { return NS_OK;
}
NS_ENSURE_ARG_POINTER(aPrintSettings);
#ifdef DEBUG
nsString printerName;
aPrintSettings->GetPrinterName(printerName); if (!printerName.Equals(aPrinterName)) {
NS_WARNING("Printer names should match!");
} #endif
bool isInitialized;
aPrintSettings->GetIsInitializedFromPrinter(&isInitialized); if (isInitialized) return NS_OK;
/** --------------------------------------------------- * Helper function - Returns either the name or sets the length to zero
*/ static nsresult GetAdjustedPrinterName(nsIPrintSettings* aPS, bool aUsePNP,
nsAString& aPrinterName) {
NS_ENSURE_ARG_POINTER(aPS);
aPrinterName.Truncate(); if (!aUsePNP) return NS_OK;
// Get the Printer Name from the PrintSettings // to use as a prefix for Pref Names
nsresult rv = aPS->GetPrinterName(aPrinterName);
NS_ENSURE_SUCCESS(rv, rv);
// Convert any whitespaces, carriage returns or newlines to _ // The below algorithm is supposedly faster than using iterators
constexpr auto replSubstr = u"_"_ns; constchar* replaceStr = " \n\r";
int32_t x; for (x = 0; x < (int32_t)strlen(replaceStr); x++) {
char16_t uChar = replaceStr[x];
auto globalPrintSettings = aFlags; #ifndef MOZ_WIDGET_ANDROID
globalPrintSettings &= nsIPrintSettings::kGlobalSettings; #endif
nsAutoString prtName; // read any non printer specific prefs // with empty printer name
nsresult rv = ReadPrefs(aPS, prtName, globalPrintSettings); if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) {
NS_WARNING("ReadPrefs failed");
}
// Get the Printer Name from the PrintSettings to use as a prefix for Pref // Names
rv = GetAdjustedPrinterName(aPS, aUsePNP, prtName);
NS_ENSURE_SUCCESS(rv, rv);
if (prtName.IsEmpty()) {
NS_WARNING("Caller should supply a printer name."); return NS_OK;
}
// Now read any printer specific prefs
rv = ReadPrefs(aPS, prtName, aFlags); if (NS_SUCCEEDED(rv)) {
aPS->SetIsInitializedFromPrefs(true);
}
return NS_OK;
}
/** * Save all of the printer settings; if we can find a printer name, save * printer-specific preferences. Otherwise, save generic ones.
*/
nsresult nsPrintSettingsService::MaybeSavePrintSettingsToPrefs(
nsIPrintSettings* aPS, uint32_t aFlags) {
NS_ENSURE_ARG_POINTER(aPS);
MOZ_DIAGNOSTIC_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
MOZ_ASSERT(!(aFlags & nsIPrintSettings::kInitSavePrinterName), "Use SaveLastUsedPrintNameToPrefs");
if (!Preferences::GetBool("print.save_print_settings", false)) { return NS_OK;
}
// Get the printer name from the PrinterSettings for an optional prefix.
nsAutoString prtName;
nsresult rv = GetAdjustedPrinterName(aPS, true, prtName);
NS_ENSURE_SUCCESS(rv, rv);
#ifndef MOZ_WIDGET_ANDROID // On most platforms we should always use a prefix when saving print settings // to prefs. Saving without a prefix risks breaking printing for users // without a good way for us to fix things for them (unprefixed prefs act as // defaults and can result in values being inappropriately propagated to // prefixed prefs). if (prtName.IsEmpty()) {
MOZ_DIAGNOSTIC_CRASH("Print settings must be saved with a prefix"); return NS_ERROR_FAILURE;
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.