commit c523f5c0036542fc1ed36b78e46b99f1f86c8728 Author: Gijs Kruitbosch gijskruitbosch@gmail.com Date: Tue Jul 15 14:46:52 2014 +0100
Backed out changeset 810977d38c0d (bug 701479) for jetpack orange, DONTBUILD because strict backout and RyanVM says so. --- widget/windows/nsDeviceContextSpecWin.cpp | 196 +++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+)
diff --git a/widget/windows/nsDeviceContextSpecWin.cpp b/widget/windows/nsDeviceContextSpecWin.cpp index 1da105f..134d74f 100644 --- a/widget/windows/nsDeviceContextSpecWin.cpp +++ b/widget/windows/nsDeviceContextSpecWin.cpp @@ -181,6 +181,189 @@ static PRUnichar * GetDefaultPrinterNameFromGlobalPrinters() return ToNewUnicode(printerName); }
+//---------------------------------------------------------------- +static nsresult +EnumerateNativePrinters(DWORD aWhichPrinters, LPWSTR aPrinterName, bool& aIsFound, bool& aIsFile) +{ + DWORD dwSizeNeeded = 0; + DWORD dwNumItems = 0; + LPPRINTER_INFO_2W lpInfo = NULL; + + // Get buffer size + if (::EnumPrintersW(aWhichPrinters, NULL, 2, NULL, 0, &dwSizeNeeded, + &dwNumItems)) { + return NS_ERROR_FAILURE; + } + + // allocate memory + lpInfo = (LPPRINTER_INFO_2W) malloc(dwSizeNeeded); + if (!lpInfo) { + return NS_ERROR_OUT_OF_MEMORY; + } + + if (::EnumPrintersW(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE)lpInfo, + dwSizeNeeded, &dwSizeNeeded, &dwNumItems) == 0) { + free(lpInfo); + return NS_OK; + } + + for (DWORD i = 0; i < dwNumItems; i++ ) { + if (wcscmp(lpInfo[i].pPrinterName, aPrinterName) == 0) { + aIsFound = true; + aIsFile = wcscmp(lpInfo[i].pPortName, L"FILE:") == 0; + break; + } + } + + free(lpInfo); + return NS_OK; +} + +//---------------------------------------------------------------- +static void +CheckForPrintToFileWithName(LPWSTR aPrinterName, bool& aIsFile) +{ + bool isFound = false; + aIsFile = false; + nsresult rv = EnumerateNativePrinters(PRINTER_ENUM_LOCAL, aPrinterName, isFound, aIsFile); + if (isFound) return; + + rv = EnumerateNativePrinters(PRINTER_ENUM_NETWORK, aPrinterName, isFound, aIsFile); + if (isFound) return; + + rv = EnumerateNativePrinters(PRINTER_ENUM_SHARED, aPrinterName, isFound, aIsFile); + if (isFound) return; + + rv = EnumerateNativePrinters(PRINTER_ENUM_REMOTE, aPrinterName, isFound, aIsFile); + if (isFound) return; +} + +static nsresult +GetFileNameForPrintSettings(nsIPrintSettings* aPS) +{ + // for testing +#ifdef DEBUG_rods + return NS_OK; +#endif + + nsresult rv; + + nsCOMPtr<nsIFilePicker> filePicker = do_CreateInstance("@mozilla.org/filepicker;1", &rv); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr<nsIStringBundleService> bundleService = + mozilla::services::GetStringBundleService(); + if (!bundleService) + return NS_ERROR_FAILURE; + nsCOMPtr<nsIStringBundle> bundle; + rv = bundleService->CreateBundle(NS_ERROR_GFX_PRINTER_BUNDLE_URL, getter_AddRefs(bundle)); + NS_ENSURE_SUCCESS(rv, rv); + + nsXPIDLString title; + rv = bundle->GetStringFromName(NS_LITERAL_STRING("PrintToFile").get(), getter_Copies(title)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr<nsIWindowWatcher> wwatch = + (do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr<nsIDOMWindow> window; + wwatch->GetActiveWindow(getter_AddRefs(window)); + + rv = filePicker->Init(window, title, nsIFilePicker::modeSave); + NS_ENSURE_SUCCESS(rv, rv); + + rv = filePicker->AppendFilters(nsIFilePicker::filterAll); + NS_ENSURE_SUCCESS(rv, rv); + + PRUnichar* fileName; + aPS->GetToFileName(&fileName); + + if (fileName) { + if (*fileName) { + nsAutoString leafName; + nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1")); + if (file) { + rv = file->InitWithPath(nsDependentString(fileName)); + if (NS_SUCCEEDED(rv)) { + file->GetLeafName(leafName); + filePicker->SetDisplayDirectory(file); + } + } + if (!leafName.IsEmpty()) { + rv = filePicker->SetDefaultString(leafName); + } + NS_ENSURE_SUCCESS(rv, rv); + } + nsMemory::Free(fileName); + } + + int16_t dialogResult; + filePicker->Show(&dialogResult); + + if (dialogResult == nsIFilePicker::returnCancel) { + return NS_ERROR_ABORT; + } + + nsCOMPtr<nsIFile> localFile; + rv = filePicker->GetFile(getter_AddRefs(localFile)); + NS_ENSURE_SUCCESS(rv, rv); + + if (dialogResult == nsIFilePicker::returnReplace) { + // be extra safe and only delete when the file is really a file + bool isFile; + rv = localFile->IsFile(&isFile); + if (NS_SUCCEEDED(rv) && isFile) { + rv = localFile->Remove(false /* recursive delete */); + NS_ENSURE_SUCCESS(rv, rv); + } + } + + nsAutoString unicodePath; + rv = localFile->GetPath(unicodePath); + NS_ENSURE_SUCCESS(rv,rv); + + if (unicodePath.IsEmpty()) { + rv = NS_ERROR_ABORT; + } + + if (NS_SUCCEEDED(rv)) aPS->SetToFileName(unicodePath.get()); + + return rv; +} + +//---------------------------------------------------------------------------------- +static nsresult +CheckForPrintToFile(nsIPrintSettings* aPS, LPWSTR aPrinterName, PRUnichar* aUPrinterName) +{ + nsresult rv = NS_OK; + + if (!aPrinterName && !aUPrinterName) return rv; + + bool toFile; + CheckForPrintToFileWithName(aPrinterName?aPrinterName:aUPrinterName, toFile); + // Since the driver wasn't a "Print To File" Driver, check to see + // if the name of the file has been set to the special "FILE:" + if (!toFile) { + nsXPIDLString toFileName; + aPS->GetToFileName(getter_Copies(toFileName)); + if (toFileName) { + if (*toFileName) { + if (toFileName.EqualsLiteral("FILE:")) { + // this skips the setting of the "print to file" info below + // which we don't want to do. + return NS_OK; + } + } + } + } + aPS->SetPrintToFile(toFile); + if (toFile) { + rv = GetFileNameForPrintSettings(aPS); + } + return rv; +} + //---------------------------------------------------------------------------------- NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget, nsIPrintSettings* aPrintSettings, @@ -215,6 +398,15 @@ NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget, SetDriverName(driverName); SetDevMode(devMode);
+ if (!aIsPrintPreview) { + rv = CheckForPrintToFile(mPrintSettings, deviceName, nullptr); + if (NS_FAILED(rv)) { + nsCRT::free(deviceName); + nsCRT::free(driverName); + return NS_ERROR_FAILURE; + } + } + // clean up nsCRT::free(deviceName); nsCRT::free(driverName); @@ -245,6 +437,10 @@ NS_IMETHODIMP nsDeviceContextSpecWin::Init(nsIWidget* aWidget, NS_ASSERTION(printerName, "We have to have a printer name"); if (!printerName || !*printerName) return rv;
+ if (!aIsPrintPreview) { + CheckForPrintToFile(mPrintSettings, nullptr, printerName); + } + return GetDataFromPrinter(printerName, mPrintSettings); }
tor-commits@lists.torproject.org