This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
nsPrintSettingsX.mm
199 lines (150 loc) · 4.79 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
/* -*- 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 "mozilla/Preferences.h"
using namespace mozilla;
#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2"
NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
nsPrintSettingsX::nsPrintSettingsX()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
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();
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;
}
void
nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
{
mPrintInfo = aPrintInfo;
}
NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsCAutoString 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:PL_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;
nsCAutoString 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];
}