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"
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"
30 #include <sys/types.h>
33 #include "gfxPDFSurface.h"
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
,
55 NS_IMETHODIMP
nsDeviceContextSpecQt::GetSurfaceForPrinter(
56 gfxASurface
** aSurface
)
58 NS_ENSURE_ARG_POINTER(aSurface
);
62 mPrintSettings
->GetEffectivePageSize(&width
, &height
);
64 // If we're in landscape mode, we'll be rotating the output --
65 // need to swap width & height.
67 mPrintSettings
->GetOrientation(&orientation
);
68 if (nsIPrintSettings::kLandscapeOrientation
== orientation
) {
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
));
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()),
89 getter_AddRefs(mSpoolFile
));
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);
107 mPrintSettings
->GetOutputFormat(&format
);
109 nsRefPtr
<gfxASurface
> surface
;
110 gfxSize
surfaceSize(width
, height
);
112 if (format
== nsIPrintSettings::kOutputFormatNative
) {
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
);
124 return NS_ERROR_NOT_IMPLEMENTED
;
127 NS_ABORT_IF_FALSE(surface
, "valid address expected");
129 surface
.swap(*aSurface
);
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
144 aPS
->GetPrintToFile(&toFile
);
146 mToPrinter
= !toFile
&& !aIsPrintPreview
;
148 nsCOMPtr
<nsPrintSettingsQt
> printSettingsQt(do_QueryInterface(aPS
));
149 if (!printSettingsQt
)
150 return NS_ERROR_NO_INTERFACE
;
154 NS_IMETHODIMP
nsDeviceContextSpecQt::GetPath(const char** aPath
)
160 NS_IMETHODIMP
nsDeviceContextSpecQt::BeginDocument(
161 const nsAString
& aTitle
,
162 char16_t
* aPrintToFileName
,
167 return NS_ERROR_NOT_IMPLEMENTED
;
172 NS_IMETHODIMP
nsDeviceContextSpecQt::EndDocument()
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);
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
));
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(
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()));
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
268 NS_IMETHODIMP
nsPrinterEnumeratorQt::DisplayPropertiesDlg(
269 const char16_t
* aPrinter
,
270 nsIPrintSettings
* aPrintSettings
)
272 return NS_ERROR_NOT_IMPLEMENTED
;