widget/cocoa/nsPrintSettingsX.mm

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:565d93bd48dd
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
5
6 #include "nsPrintSettingsX.h"
7 #include "nsObjCExceptions.h"
8
9 #include "plbase64.h"
10 #include "plstr.h"
11
12 #include "nsCocoaUtils.h"
13
14 #include "mozilla/Preferences.h"
15
16 using namespace mozilla;
17
18 #define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2"
19
20 NS_IMPL_ISUPPORTS_INHERITED(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
21
22 nsPrintSettingsX::nsPrintSettingsX()
23 {
24 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
25
26 mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
27
28 NS_OBJC_END_TRY_ABORT_BLOCK;
29 }
30
31 nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src)
32 {
33 *this = src;
34 }
35
36 nsPrintSettingsX::~nsPrintSettingsX()
37 {
38 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
39
40 [mPrintInfo release];
41
42 NS_OBJC_END_TRY_ABORT_BLOCK;
43 }
44
45 nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs)
46 {
47 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
48
49 if (this == &rhs) {
50 return *this;
51 }
52
53 nsPrintSettings::operator=(rhs);
54
55 [mPrintInfo release];
56 mPrintInfo = [rhs.mPrintInfo copy];
57
58 return *this;
59
60 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
61 }
62
63 nsresult nsPrintSettingsX::Init()
64 {
65 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
66
67 InitUnwriteableMargin();
68
69 return NS_OK;
70
71 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
72 }
73
74 // Should be called whenever the page format changes.
75 NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin()
76 {
77 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
78
79 PMPaper paper;
80 PMPaperMargins paperMargin;
81 PMPageFormat pageFormat = GetPMPageFormat();
82 ::PMGetPageFormatPaper(pageFormat, &paper);
83 ::PMPaperGetMargins(paper, &paperMargin);
84 mUnwriteableMargin.top = NS_POINTS_TO_INT_TWIPS(paperMargin.top);
85 mUnwriteableMargin.left = NS_POINTS_TO_INT_TWIPS(paperMargin.left);
86 mUnwriteableMargin.bottom = NS_POINTS_TO_INT_TWIPS(paperMargin.bottom);
87 mUnwriteableMargin.right = NS_POINTS_TO_INT_TWIPS(paperMargin.right);
88
89 return NS_OK;
90
91 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
92 }
93
94 void
95 nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
96 {
97 mPrintInfo = aPrintInfo;
98 }
99
100 NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs()
101 {
102 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
103
104 nsAutoCString encodedData;
105 nsresult rv =
106 Preferences::GetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, &encodedData);
107 if (NS_FAILED(rv)) {
108 return rv;
109 }
110
111 // decode the base64
112 char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nullptr);
113 NSData* data = [NSData dataWithBytes:decodedData length:strlen(decodedData)];
114 if (!data)
115 return NS_ERROR_FAILURE;
116
117 PMPageFormat newPageFormat;
118 OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat);
119 if (status == noErr) {
120 SetPMPageFormat(newPageFormat);
121 }
122 InitUnwriteableMargin();
123
124 return NS_OK;
125
126 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
127 }
128
129 NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs()
130 {
131 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
132
133 PMPageFormat pageFormat = GetPMPageFormat();
134 if (pageFormat == kPMNoPageFormat)
135 return NS_ERROR_NOT_INITIALIZED;
136
137 NSData* data = nil;
138 OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data, kPMDataFormatXMLDefault);
139 if (err != noErr)
140 return NS_ERROR_FAILURE;
141
142 nsAutoCString encodedData;
143 encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nullptr));
144 if (!encodedData.get())
145 return NS_ERROR_OUT_OF_MEMORY;
146
147 return Preferences::SetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
148
149 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
150 }
151
152 nsresult nsPrintSettingsX::_Clone(nsIPrintSettings **_retval)
153 {
154 NS_ENSURE_ARG_POINTER(_retval);
155 *_retval = nullptr;
156
157 nsPrintSettingsX *newSettings = new nsPrintSettingsX(*this);
158 if (!newSettings)
159 return NS_ERROR_FAILURE;
160 *_retval = newSettings;
161 NS_ADDREF(*_retval);
162 return NS_OK;
163 }
164
165 NS_IMETHODIMP nsPrintSettingsX::_Assign(nsIPrintSettings *aPS)
166 {
167 nsPrintSettingsX *printSettingsX = static_cast<nsPrintSettingsX*>(aPS);
168 if (!printSettingsX)
169 return NS_ERROR_UNEXPECTED;
170 *this = *printSettingsX;
171 return NS_OK;
172 }
173
174 PMPrintSettings
175 nsPrintSettingsX::GetPMPrintSettings()
176 {
177 return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]);
178 }
179
180 PMPrintSession
181 nsPrintSettingsX::GetPMPrintSession()
182 {
183 return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]);
184 }
185
186 PMPageFormat
187 nsPrintSettingsX::GetPMPageFormat()
188 {
189 return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]);
190 }
191
192 void
193 nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat)
194 {
195 PMPageFormat oldPageFormat = GetPMPageFormat();
196 ::PMCopyPageFormat(aPageFormat, oldPageFormat);
197 [mPrintInfo updateFromPMPageFormat];
198 }
199

mercurial