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"
14 #define FORCE_PR_LOG 1 /* Allow logging in the release build */
15 #endif /* MOZ_LOGGING */
20 #include "nsDeviceContextSpecQt.h"
22 #include "prenv.h" /* for PR_GetEnv */
24 #include "nsReadableUtils.h"
25 #include "nsStringEnumerator.h"
26 #include "nsIServiceManager.h"
27 #include "nsPrintSettingsQt.h"
28 #include "nsIFileStreams.h"
33 #include <sys/types.h>
36 #include "gfxPDFSurface.h"
39 static PRLogModuleInfo
* DeviceContextSpecQtLM
=
40 PR_NewLogModule("DeviceContextSpecQt");
41 #endif /* PR_LOGGING */
42 /* Macro to make lines shorter */
43 #define DO_PR_DEBUG_LOG(x) PR_LOG(DeviceContextSpecQtLM, PR_LOG_DEBUG, x)
45 nsDeviceContextSpecQt::nsDeviceContextSpecQt()
47 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::nsDeviceContextSpecQt()\n"));
50 nsDeviceContextSpecQt::~nsDeviceContextSpecQt()
52 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::~nsDeviceContextSpecQt()\n"));
55 NS_IMPL_ISUPPORTS1(nsDeviceContextSpecQt
,
58 NS_IMETHODIMP
nsDeviceContextSpecQt::GetSurfaceForPrinter(
59 gfxASurface
** aSurface
)
61 NS_ENSURE_ARG_POINTER(aSurface
);
65 mPrintSettings
->GetEffectivePageSize(&width
, &height
);
67 // If we're in landscape mode, we'll be rotating the output --
68 // need to swap width & height.
70 mPrintSettings
->GetOrientation(&orientation
);
71 if (nsIPrintSettings::kLandscapeOrientation
== orientation
) {
77 // convert twips to points
78 width
/= TWIPS_PER_POINT_FLOAT
;
79 height
/= TWIPS_PER_POINT_FLOAT
;
81 DO_PR_DEBUG_LOG(("\"%s\", %f, %f\n", mPath
, width
, height
));
85 return NS_ERROR_GFX_PRINTER_COULD_NOT_OPEN_FILE
;
87 file
.setAutoRemove(false);
89 nsresult rv
= NS_NewNativeLocalFile(
90 nsDependentCString(file
.fileName().toUtf8().constData()),
92 getter_AddRefs(mSpoolFile
));
95 return NS_ERROR_GFX_PRINTER_COULD_NOT_OPEN_FILE
;
98 mSpoolName
= file
.fileName().toUtf8().constData();
100 mSpoolFile
->SetPermissions(0600);
102 nsCOMPtr
<nsIFileOutputStream
> stream
=
103 do_CreateInstance("@mozilla.org/network/file-output-stream;1");
105 rv
= stream
->Init(mSpoolFile
, -1, -1, 0);
110 mPrintSettings
->GetOutputFormat(&format
);
112 nsRefPtr
<gfxASurface
> surface
;
113 gfxSize
surfaceSize(width
, height
);
115 if (format
== nsIPrintSettings::kOutputFormatNative
) {
117 // There is nothing to detect on Print Preview, use PS.
118 // TODO: implement for Qt?
119 //format = nsIPrintSettings::kOutputFormatPS;
120 return NS_ERROR_NOT_IMPLEMENTED
;
122 format
= nsIPrintSettings::kOutputFormatPDF
;
124 if (format
== nsIPrintSettings::kOutputFormatPDF
) {
125 surface
= new gfxPDFSurface(stream
, surfaceSize
);
127 return NS_ERROR_NOT_IMPLEMENTED
;
130 NS_ABORT_IF_FALSE(surface
, "valid address expected");
132 surface
.swap(*aSurface
);
136 NS_IMETHODIMP
nsDeviceContextSpecQt::Init(nsIWidget
* aWidget
,
137 nsIPrintSettings
* aPS
,
138 bool aIsPrintPreview
)
140 DO_PR_DEBUG_LOG(("nsDeviceContextSpecQt::Init(aPS=%p)\n", aPS
));
142 mPrintSettings
= aPS
;
143 mIsPPreview
= aIsPrintPreview
;
145 // This is only set by embedders
147 aPS
->GetPrintToFile(&toFile
);
149 mToPrinter
= !toFile
&& !aIsPrintPreview
;
151 nsCOMPtr
<nsPrintSettingsQt
> printSettingsQt(do_QueryInterface(aPS
));
152 if (!printSettingsQt
)
153 return NS_ERROR_NO_INTERFACE
;
157 NS_IMETHODIMP
nsDeviceContextSpecQt::GetPath(const char** aPath
)
163 NS_IMETHODIMP
nsDeviceContextSpecQt::BeginDocument(
164 const nsAString
& aTitle
,
165 PRUnichar
* aPrintToFileName
,
170 return NS_ERROR_NOT_IMPLEMENTED
;
175 NS_IMETHODIMP
nsDeviceContextSpecQt::EndDocument()
178 return NS_ERROR_NOT_IMPLEMENTED
;
180 // Handle print-to-file ourselves for the benefit of embedders
181 nsXPIDLString targetPath
;
182 nsCOMPtr
<nsIFile
> destFile
;
183 mPrintSettings
->GetToFileName(getter_Copies(targetPath
));
185 nsresult rv
= NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(targetPath
),
186 false, getter_AddRefs(destFile
));
187 NS_ENSURE_SUCCESS(rv
, rv
);
189 nsAutoString destLeafName
;
190 rv
= destFile
->GetLeafName(destLeafName
);
191 NS_ENSURE_SUCCESS(rv
, rv
);
193 nsCOMPtr
<nsIFile
> destDir
;
194 rv
= destFile
->GetParent(getter_AddRefs(destDir
));
195 NS_ENSURE_SUCCESS(rv
, rv
);
197 rv
= mSpoolFile
->MoveTo(destDir
, destLeafName
);
198 NS_ENSURE_SUCCESS(rv
, rv
);
200 // This is the standard way to get the UNIX umask. Ugh.
201 mode_t mask
= umask(0);
203 // If you're not familiar with umasks, they contain the bits of what NOT
204 // to set in the permissions
205 // (thats because files and directories have different numbers of bits
206 // for their permissions)
207 destFile
->SetPermissions(0666 & ~(mask
));
212 // Printer Enumerator
213 nsPrinterEnumeratorQt::nsPrinterEnumeratorQt()
217 NS_IMPL_ISUPPORTS1(nsPrinterEnumeratorQt
, nsIPrinterEnumerator
)
219 NS_IMETHODIMP
nsPrinterEnumeratorQt::GetPrinterNameList(
220 nsIStringEnumerator
** aPrinterNameList
)
222 NS_ENSURE_ARG_POINTER(aPrinterNameList
);
223 *aPrinterNameList
= nullptr;
225 QList
<QPrinterInfo
> qprinters
= QPrinterInfo::availablePrinters();
226 if (qprinters
.size() == 0)
227 return NS_ERROR_NOT_AVAILABLE
;
229 nsTArray
<nsString
>* printers
=
230 new nsTArray
<nsString
>(qprinters
.size());
232 for (int32_t i
= 0; i
< qprinters
.size(); ++i
) {
233 printers
->AppendElement(
235 (const PRUnichar
*)qprinters
[i
].printerName().constData()));
238 return NS_NewAdoptingStringEnumerator(aPrinterNameList
, printers
);
241 NS_IMETHODIMP
nsPrinterEnumeratorQt::GetDefaultPrinterName(
242 PRUnichar
** aDefaultPrinterName
)
244 DO_PR_DEBUG_LOG(("nsPrinterEnumeratorQt::GetDefaultPrinterName()\n"));
245 NS_ENSURE_ARG_POINTER(aDefaultPrinterName
);
247 QString defprinter
= QPrinterInfo::defaultPrinter().printerName();
248 *aDefaultPrinterName
= ToNewUnicode(nsDependentString(
249 (const PRUnichar
*)defprinter
.constData()));
251 DO_PR_DEBUG_LOG(("GetDefaultPrinterName(): default printer='%s'.\n",
252 NS_ConvertUTF16toUTF8(*aDefaultPrinterName
).get()));
257 NS_IMETHODIMP
nsPrinterEnumeratorQt::InitPrintSettingsFromPrinter(
258 const PRUnichar
* aPrinterName
,
259 nsIPrintSettings
* aPrintSettings
)
261 DO_PR_DEBUG_LOG(("nsPrinterEnumeratorQt::InitPrintSettingsFromPrinter()"));
262 // XXX Leave NS_OK for now
263 // Probably should use NS_ERROR_NOT_IMPLEMENTED
267 NS_IMETHODIMP
nsPrinterEnumeratorQt::DisplayPropertiesDlg(
268 const PRUnichar
* aPrinter
,
269 nsIPrintSettings
* aPrintSettings
)
271 return NS_ERROR_NOT_IMPLEMENTED
;