This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsPrintSettingsX.mm
488 lines (376 loc) · 14.7 KB
/
nsPrintSettingsX.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* -*- 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/. */
#include "nsPrintSettingsX.h"
#include "nsObjCExceptions.h"
#include "plbase64.h"
#include "plstr.h"
#include "nsCocoaUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2"
#define COCOA_PAPER_UNITS_PER_INCH 72.0
NS_IMPL_ISUPPORTS_INHERITED(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
nsPrintSettingsX::nsPrintSettingsX() : mAdjustedPaperWidth{0.0}, mAdjustedPaperHeight{0.0} {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
mWidthScale = COCOA_PAPER_UNITS_PER_INCH;
mHeightScale = COCOA_PAPER_UNITS_PER_INCH;
/*
* Don't save print settings after the user cancels out of the
* print dialog. For saving print settings after a cancellation
* to work properly, in addition to changing |mSaveOnCancel|,
* the print dialog implementation must be updated to save changed
* settings and serialize them back to the child process.
*/
mSaveOnCancel = false;
NS_OBJC_END_TRY_ABORT_BLOCK;
}
nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src) { *this = src; }
nsPrintSettingsX::~nsPrintSettingsX() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
[mPrintInfo release];
NS_OBJC_END_TRY_ABORT_BLOCK;
}
nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
if (this == &rhs) {
return *this;
}
nsPrintSettings::operator=(rhs);
[mPrintInfo release];
mPrintInfo = [rhs.mPrintInfo copy];
return *this;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
}
nsresult nsPrintSettingsX::Init() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
InitUnwriteableMargin();
InitAdjustedPaperSize();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
// Should be called whenever the page format changes.
NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
PMPaper paper;
PMPaperMargins paperMargin;
PMPageFormat pageFormat = GetPMPageFormat();
::PMGetPageFormatPaper(pageFormat, &paper);
::PMPaperGetMargins(paper, &paperMargin);
mUnwriteableMargin.top = NS_POINTS_TO_INT_TWIPS(paperMargin.top);
mUnwriteableMargin.left = NS_POINTS_TO_INT_TWIPS(paperMargin.left);
mUnwriteableMargin.bottom = NS_POINTS_TO_INT_TWIPS(paperMargin.bottom);
mUnwriteableMargin.right = NS_POINTS_TO_INT_TWIPS(paperMargin.right);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP nsPrintSettingsX::InitAdjustedPaperSize() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
PMPageFormat pageFormat = GetPMPageFormat();
PMRect paperRect;
::PMGetAdjustedPaperRect(pageFormat, &paperRect);
mAdjustedPaperWidth = paperRect.right - paperRect.left;
mAdjustedPaperHeight = paperRect.bottom - paperRect.top;
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
void nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo) {
if (mPrintInfo != aPrintInfo) {
[mPrintInfo release];
mPrintInfo = [aPrintInfo retain];
}
}
NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsAutoCString encodedData;
nsresult rv = Preferences::GetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
if (NS_FAILED(rv)) {
return rv;
}
// decode the base64
char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nullptr);
NSData* data = [NSData dataWithBytes:decodedData length:strlen(decodedData)];
if (!data) return NS_ERROR_FAILURE;
PMPageFormat newPageFormat;
OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat);
if (status == noErr) {
SetPMPageFormat(newPageFormat);
}
InitUnwriteableMargin();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
PMPageFormat pageFormat = GetPMPageFormat();
if (pageFormat == kPMNoPageFormat) return NS_ERROR_NOT_INITIALIZED;
NSData* data = nil;
OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data,
kPMDataFormatXMLDefault);
if (err != noErr) return NS_ERROR_FAILURE;
nsAutoCString encodedData;
encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nullptr));
if (!encodedData.get()) return NS_ERROR_OUT_OF_MEMORY;
return Preferences::SetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult nsPrintSettingsX::_Clone(nsIPrintSettings** _retval) {
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nullptr;
nsPrintSettingsX* newSettings = new nsPrintSettingsX(*this);
if (!newSettings) return NS_ERROR_FAILURE;
*_retval = newSettings;
NS_ADDREF(*_retval);
return NS_OK;
}
NS_IMETHODIMP nsPrintSettingsX::_Assign(nsIPrintSettings* aPS) {
nsPrintSettingsX* printSettingsX = static_cast<nsPrintSettingsX*>(aPS);
if (!printSettingsX) return NS_ERROR_UNEXPECTED;
*this = *printSettingsX;
return NS_OK;
}
PMPrintSettings nsPrintSettingsX::GetPMPrintSettings() {
return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]);
}
PMPrintSession nsPrintSettingsX::GetPMPrintSession() {
return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]);
}
PMPageFormat nsPrintSettingsX::GetPMPageFormat() {
return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]);
}
void nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat) {
PMPageFormat oldPageFormat = GetPMPageFormat();
::PMCopyPageFormat(aPageFormat, oldPageFormat);
[mPrintInfo updateFromPMPageFormat];
}
void nsPrintSettingsX::SetInchesScale(float aWidthScale, float aHeightScale) {
if (aWidthScale > 0 && aHeightScale > 0) {
mWidthScale = aWidthScale;
mHeightScale = aHeightScale;
}
}
void nsPrintSettingsX::GetInchesScale(float* aWidthScale, float* aHeightScale) {
*aWidthScale = mWidthScale;
*aHeightScale = mHeightScale;
}
NS_IMETHODIMP nsPrintSettingsX::SetPaperWidth(double aPaperWidth) {
mPaperWidth = aPaperWidth;
mAdjustedPaperWidth = aPaperWidth * mWidthScale;
return NS_OK;
}
NS_IMETHODIMP nsPrintSettingsX::SetPaperHeight(double aPaperHeight) {
mPaperHeight = aPaperHeight;
mAdjustedPaperHeight = aPaperHeight * mHeightScale;
return NS_OK;
}
NS_IMETHODIMP
nsPrintSettingsX::GetEffectivePageSize(double* aWidth, double* aHeight) {
*aWidth = NS_INCHES_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
*aHeight = NS_INCHES_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
return NS_OK;
}
void nsPrintSettingsX::GetFilePageSize(double* aWidth, double* aHeight) {
double height, width;
if (kPaperSizeInches == GetCocoaUnit(mPaperSizeUnit)) {
width = NS_INCHES_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
height = NS_INCHES_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
} else {
width = NS_MILLIMETERS_TO_TWIPS(mAdjustedPaperWidth / mWidthScale);
height = NS_MILLIMETERS_TO_TWIPS(mAdjustedPaperHeight / mHeightScale);
}
width /= TWIPS_PER_POINT_FLOAT;
height /= TWIPS_PER_POINT_FLOAT;
*aWidth = width;
*aHeight = height;
}
void nsPrintSettingsX::SetAdjustedPaperSize(double aWidth, double aHeight) {
mAdjustedPaperWidth = aWidth;
mAdjustedPaperHeight = aHeight;
}
void nsPrintSettingsX::GetAdjustedPaperSize(double* aWidth, double* aHeight) {
*aWidth = mAdjustedPaperWidth;
*aHeight = mAdjustedPaperHeight;
}
NS_IMETHODIMP
nsPrintSettingsX::SetScaling(double aScaling) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
[printInfoDict setObject:[NSNumber numberWithFloat:aScaling] forKey:NSPrintScalingFactor];
} else {
nsPrintSettings::SetScaling(aScaling);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::GetScaling(double* aScaling) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSDictionary* printInfoDict = [mPrintInfo dictionary];
*aScaling = [[printInfoDict objectForKey:NSPrintScalingFactor] doubleValue];
// Limit scaling precision to whole number percent values
*aScaling = round(*aScaling * 100.0) / 100.0;
} else {
nsPrintSettings::GetScaling(aScaling);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::SetToFileName(const nsAString& aToFileName) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (XRE_IsContentProcess() && Preferences::GetBool("print.print_via_parent")) {
// On content sandbox, NSPrintJobSavingURL will returns error since
// sandbox disallows file access.
return nsPrintSettings::SetToFileName(aToFileName);
}
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
if (!aToFileName.IsEmpty()) {
NSURL* jobSavingURL = [NSURL fileURLWithPath:nsCocoaUtils::ToNSString(aToFileName)];
if (jobSavingURL) {
[printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
[printInfoDict setObject:jobSavingURL forKey:NSPrintJobSavingURL];
}
mToFileName = aToFileName;
} else {
mToFileName.Truncate();
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::GetOrientation(int32_t* aOrientation) {
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
if ([mPrintInfo orientation] == NS_PAPER_ORIENTATION_PORTRAIT) {
*aOrientation = nsIPrintSettings::kPortraitOrientation;
} else {
*aOrientation = nsIPrintSettings::kLandscapeOrientation;
}
} else {
nsPrintSettings::GetOrientation(aOrientation);
}
return NS_OK;
}
NS_IMETHODIMP
nsPrintSettingsX::SetOrientation(int32_t aOrientation) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
if (aOrientation == nsIPrintSettings::kPortraitOrientation) {
[printInfoDict setObject:[NSNumber numberWithInt:NS_PAPER_ORIENTATION_PORTRAIT]
forKey:NSPrintOrientation];
} else {
[printInfoDict setObject:[NSNumber numberWithInt:NS_PAPER_ORIENTATION_LANDSCAPE]
forKey:NSPrintOrientation];
}
} else {
nsPrintSettings::SetOrientation(aOrientation);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::SetUnwriteableMarginTop(double aUnwriteableMarginTop) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop);
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
[printInfoDict setObject:[NSNumber numberWithDouble:aUnwriteableMarginTop]
forKey:NSPrintTopMargin];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft);
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
[printInfoDict setObject:[NSNumber numberWithDouble:aUnwriteableMarginLeft]
forKey:NSPrintLeftMargin];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom);
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
[printInfoDict setObject:[NSNumber numberWithDouble:aUnwriteableMarginBottom]
forKey:NSPrintBottomMargin];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsPrintSettingsX::SetUnwriteableMarginRight(double aUnwriteableMarginRight) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight);
// Only use NSPrintInfo data in the parent process. The
// child process' instance is not needed or used.
if (XRE_IsParentProcess()) {
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
[printInfoDict setObject:[NSNumber numberWithDouble:aUnwriteableMarginRight]
forKey:NSPrintRightMargin];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
int nsPrintSettingsX::GetCocoaUnit(int16_t aGeckoUnit) {
if (aGeckoUnit == kPaperSizeMillimeters)
return kPaperSizeMillimeters;
else
return kPaperSizeInches;
}
nsresult nsPrintSettingsX::SetCocoaPaperSize(double aWidth, double aHeight) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NSSize paperSize;
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
if ([mPrintInfo orientation] == NS_PAPER_ORIENTATION_PORTRAIT) {
// switch widths and heights
paperSize = NSMakeSize(aWidth, aHeight);
[printInfoDict setObject:[NSValue valueWithSize:paperSize] forKey:NSPrintPaperSize];
} else {
paperSize = NSMakeSize(aHeight, aWidth);
[printInfoDict setObject:[NSValue valueWithSize:paperSize] forKey:NSPrintPaperSize];
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
void nsPrintSettingsX::SetPrinterNameFromPrintInfo() {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
// Don't attempt to call this from child processes.
// Process sandboxing prevents access to the print
// server and printer-related settings.
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(mPrintInfo);
NSMutableDictionary* printInfoDict = [mPrintInfo dictionary];
NSString* nsPrinterNameValue = [printInfoDict objectForKey:@"NSPrinterName"];
if (nsPrinterNameValue) {
nsAutoString printerName;
nsCocoaUtils::GetStringForNSString(nsPrinterNameValue, printerName);
mPrinter.Assign(printerName);
}
NS_OBJC_END_TRY_ABORT_BLOCK;
}