Bug 1073336 part 5 - Add AnimationPlayerCollection::PlayerUpdated; r=dbaron
[gecko.git] / widget / qt / nsDeviceContextSpecQt.cpp
blobea2fc08088913f06d63b289f4c36291969d8248e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
7 #include <QTemporaryFile>
8 #include <QPrinterInfo>
10 #define SET_PRINTER_FEATURES_VIA_PREFS 1
11 #define PRINTERFEATURES_PREF "print.tmp.printerfeatures"
13 #include "prlog.h"
15 #include "plstr.h"
17 #include "nsDeviceContextSpecQt.h"
19 #include "prenv.h" /* for PR_GetEnv */
21 #include "nsReadableUtils.h"
22 #include "nsStringEnumerator.h"
23 #include "nsIServiceManager.h"
24 #include "nsPrintSettingsQt.h"
25 #include "nsIFileStreams.h"
26 #include "nsIFile.h"
27 #include "nsTArray.h"
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
33 #include "gfxPDFSurface.h"
35 #ifdef PR_LOGGING
36 static PRLogModuleInfo* DeviceContextSpecQtLM =
37 PR_NewLogModule("DeviceContextSpecQt");
38 #endif /* PR_LOGGING */
39 /* Macro to make lines shorter */
40 #define DO_PR_DEBUG_LOG(x) PR_LOG(DeviceContextSpecQtLM, PR_LOG_DEBUG, x)
42 nsDeviceContextSpecQt::nsDeviceContextSpecQt()
44 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::nsDeviceContextSpecQt()\n"));
47 nsDeviceContextSpecQt::~nsDeviceContextSpecQt()
49 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::~nsDeviceContextSpecQt()\n"));
52 NS_IMPL_ISUPPORTS(nsDeviceContextSpecQt,
53 nsIDeviceContextSpec)
55 NS_IMETHODIMP nsDeviceContextSpecQt::GetSurfaceForPrinter(
56 gfxASurface** aSurface)
58 NS_ENSURE_ARG_POINTER(aSurface);
59 *aSurface = nullptr;
61 double width, height;
62 mPrintSettings->GetEffectivePageSize(&width, &height);
64 // If we're in landscape mode, we'll be rotating the output --
65 // need to swap width & height.
66 int32_t orientation;
67 mPrintSettings->GetOrientation(&orientation);
68 if (nsIPrintSettings::kLandscapeOrientation == orientation) {
69 double tmp = width;
70 width = height;
71 height = tmp;
74 // convert twips to points
75 width /= TWIPS_PER_POINT_FLOAT;
76 height /= TWIPS_PER_POINT_FLOAT;
78 DO_PR_DEBUG_LOG(("\"%s\", %f, %f\n", mPath, width, height));
80 QTemporaryFile file;
81 if(!file.open()) {
82 return NS_ERROR_GFX_PRINTER_COULD_NOT_OPEN_FILE;
84 file.setAutoRemove(false);
86 nsresult rv = NS_NewNativeLocalFile(
87 nsDependentCString(file.fileName().toUtf8().constData()),
88 false,
89 getter_AddRefs(mSpoolFile));
90 if (NS_FAILED(rv)) {
91 file.remove();
92 return NS_ERROR_GFX_PRINTER_COULD_NOT_OPEN_FILE;
95 mSpoolName = file.fileName().toUtf8().constData();
97 mSpoolFile->SetPermissions(0600);
99 nsCOMPtr<nsIFileOutputStream> stream =
100 do_CreateInstance("@mozilla.org/network/file-output-stream;1");
102 rv = stream->Init(mSpoolFile, -1, -1, 0);
103 if (NS_FAILED(rv))
104 return rv;
106 int16_t format;
107 mPrintSettings->GetOutputFormat(&format);
109 nsRefPtr<gfxASurface> surface;
110 gfxSize surfaceSize(width, height);
112 if (format == nsIPrintSettings::kOutputFormatNative) {
113 if (mIsPPreview) {
114 // There is nothing to detect on Print Preview, use PS.
115 // TODO: implement for Qt?
116 //format = nsIPrintSettings::kOutputFormatPS;
117 return NS_ERROR_NOT_IMPLEMENTED;
119 format = nsIPrintSettings::kOutputFormatPDF;
121 if (format == nsIPrintSettings::kOutputFormatPDF) {
122 surface = new gfxPDFSurface(stream, surfaceSize);
123 } else {
124 return NS_ERROR_NOT_IMPLEMENTED;
127 NS_ABORT_IF_FALSE(surface, "valid address expected");
129 surface.swap(*aSurface);
130 return NS_OK;
133 NS_IMETHODIMP nsDeviceContextSpecQt::Init(nsIWidget* aWidget,
134 nsIPrintSettings* aPS,
135 bool aIsPrintPreview)
137 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::Init(aPS=%p)\n", aPS));
139 mPrintSettings = aPS;
140 mIsPPreview = aIsPrintPreview;
142 // This is only set by embedders
143 bool toFile;
144 aPS->GetPrintToFile(&toFile);
146 mToPrinter = !toFile && !aIsPrintPreview;
148 nsCOMPtr<nsPrintSettingsQt> printSettingsQt(do_QueryInterface(aPS));
149 if (!printSettingsQt)
150 return NS_ERROR_NO_INTERFACE;
151 return NS_OK;
154 NS_IMETHODIMP nsDeviceContextSpecQt::GetPath(const char** aPath)
156 *aPath = mPath;
157 return NS_OK;
160 NS_IMETHODIMP nsDeviceContextSpecQt::BeginDocument(
161 const nsAString& aTitle,
162 char16_t* aPrintToFileName,
163 int32_t aStartPage,
164 int32_t aEndPage)
166 if (mToPrinter) {
167 return NS_ERROR_NOT_IMPLEMENTED;
169 return NS_OK;
172 NS_IMETHODIMP nsDeviceContextSpecQt::EndDocument()
174 if (mToPrinter) {
175 return NS_ERROR_NOT_IMPLEMENTED;
177 // Handle print-to-file ourselves for the benefit of embedders
178 nsXPIDLString targetPath;
179 nsCOMPtr<nsIFile> destFile;
180 mPrintSettings->GetToFileName(getter_Copies(targetPath));
182 nsresult rv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(targetPath),
183 false, getter_AddRefs(destFile));
184 NS_ENSURE_SUCCESS(rv, rv);
186 nsAutoString destLeafName;
187 rv = destFile->GetLeafName(destLeafName);
188 NS_ENSURE_SUCCESS(rv, rv);
190 nsCOMPtr<nsIFile> destDir;
191 rv = destFile->GetParent(getter_AddRefs(destDir));
192 NS_ENSURE_SUCCESS(rv, rv);
194 rv = mSpoolFile->MoveTo(destDir, destLeafName);
195 NS_ENSURE_SUCCESS(rv, rv);
197 // This is the standard way to get the UNIX umask. Ugh.
198 mode_t mask = umask(0);
199 umask(mask);
200 // If you're not familiar with umasks, they contain the bits of what NOT
201 // to set in the permissions
202 // (thats because files and directories have different numbers of bits
203 // for their permissions)
204 destFile->SetPermissions(0666 & ~(mask));
206 return NS_OK;
209 // Printer Enumerator
210 nsPrinterEnumeratorQt::nsPrinterEnumeratorQt()
214 nsPrinterEnumeratorQt::~nsPrinterEnumeratorQt()
218 NS_IMPL_ISUPPORTS(nsPrinterEnumeratorQt, nsIPrinterEnumerator)
220 NS_IMETHODIMP nsPrinterEnumeratorQt::GetPrinterNameList(
221 nsIStringEnumerator** aPrinterNameList)
223 NS_ENSURE_ARG_POINTER(aPrinterNameList);
224 *aPrinterNameList = nullptr;
226 QList<QPrinterInfo> qprinters = QPrinterInfo::availablePrinters();
227 if (qprinters.size() == 0)
228 return NS_ERROR_NOT_AVAILABLE;
230 nsTArray<nsString>* printers =
231 new nsTArray<nsString>(qprinters.size());
233 for (int32_t i = 0; i < qprinters.size(); ++i) {
234 printers->AppendElement(
235 nsDependentString(
236 (const char16_t*)qprinters[i].printerName().constData()));
239 return NS_NewAdoptingStringEnumerator(aPrinterNameList, printers);
242 NS_IMETHODIMP nsPrinterEnumeratorQt::GetDefaultPrinterName(
243 char16_t** aDefaultPrinterName)
245 DO_PR_DEBUG_LOG(("nsPrinterEnumeratorQt::GetDefaultPrinterName()\n"));
246 NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
248 QString defprinter = QPrinterInfo::defaultPrinter().printerName();
249 *aDefaultPrinterName = ToNewUnicode(nsDependentString(
250 (const char16_t*)defprinter.constData()));
252 DO_PR_DEBUG_LOG(("GetDefaultPrinterName(): default printer='%s'.\n",
253 NS_ConvertUTF16toUTF8(*aDefaultPrinterName).get()));
255 return NS_OK;
258 NS_IMETHODIMP nsPrinterEnumeratorQt::InitPrintSettingsFromPrinter(
259 const char16_t* aPrinterName,
260 nsIPrintSettings* aPrintSettings)
262 DO_PR_DEBUG_LOG(("nsPrinterEnumeratorQt::InitPrintSettingsFromPrinter()"));
263 // XXX Leave NS_OK for now
264 // Probably should use NS_ERROR_NOT_IMPLEMENTED
265 return NS_OK;
268 NS_IMETHODIMP nsPrinterEnumeratorQt::DisplayPropertiesDlg(
269 const char16_t* aPrinter,
270 nsIPrintSettings* aPrintSettings)
272 return NS_ERROR_NOT_IMPLEMENTED;