1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Conrad Carlen <ccarlen@netscape.com>
24 * Markus Stange <mstange@themasta.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsPrintSettingsX.h"
41 #include "nsObjCExceptions.h"
43 #include "nsIPrefService.h"
44 #include "nsIPrefBranch.h"
45 #include "nsServiceManagerUtils.h"
50 #include "nsCocoaUtils.h"
52 #define PRINTING_PREF_BRANCH "print."
53 #define MAC_OS_X_PAGE_SETUP_PREFNAME "macosx.pagesetup-2"
55 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
57 nsPrintSettingsX::nsPrintSettingsX()
59 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
61 mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
63 NS_OBJC_END_TRY_ABORT_BLOCK;
66 nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src)
71 nsPrintSettingsX::~nsPrintSettingsX()
73 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
77 NS_OBJC_END_TRY_ABORT_BLOCK;
80 nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs)
82 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
88 nsPrintSettings::operator=(rhs);
91 mPrintInfo = [rhs.mPrintInfo copy];
95 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
98 nsresult nsPrintSettingsX::Init()
100 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
102 InitUnwriteableMargin();
106 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
109 // Should be called whenever the page format changes.
110 NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin()
112 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
115 PMPaperMargins paperMargin;
116 PMPageFormat pageFormat = GetPMPageFormat();
117 ::PMGetPageFormatPaper(pageFormat, &paper);
118 ::PMPaperGetMargins(paper, &paperMargin);
119 mUnwriteableMargin.top = NS_POINTS_TO_INT_TWIPS(paperMargin.top);
120 mUnwriteableMargin.left = NS_POINTS_TO_INT_TWIPS(paperMargin.left);
121 mUnwriteableMargin.bottom = NS_POINTS_TO_INT_TWIPS(paperMargin.bottom);
122 mUnwriteableMargin.right = NS_POINTS_TO_INT_TWIPS(paperMargin.right);
126 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
130 nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
132 mPrintInfo = aPrintInfo;
135 NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs()
137 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
140 nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
143 nsCOMPtr<nsIPrefBranch> prefBranch;
144 rv = prefService->GetBranch(PRINTING_PREF_BRANCH, getter_AddRefs(prefBranch));
148 nsXPIDLCString encodedData;
149 rv = prefBranch->GetCharPref(MAC_OS_X_PAGE_SETUP_PREFNAME, getter_Copies(encodedData));
154 char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nsnull);
155 NSData* data = [NSData dataWithBytes:decodedData length:PL_strlen(decodedData)];
157 return NS_ERROR_FAILURE;
159 PMPageFormat newPageFormat;
160 OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat);
161 if (status == noErr) {
162 SetPMPageFormat(newPageFormat);
164 InitUnwriteableMargin();
168 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
171 NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs()
173 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
175 PMPageFormat pageFormat = GetPMPageFormat();
176 if (pageFormat == kPMNoPageFormat)
177 return NS_ERROR_NOT_INITIALIZED;
180 nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
183 nsCOMPtr<nsIPrefBranch> prefBranch;
184 rv = prefService->GetBranch(PRINTING_PREF_BRANCH, getter_AddRefs(prefBranch));
189 OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data, kPMDataFormatXMLDefault);
191 return NS_ERROR_FAILURE;
193 nsXPIDLCString encodedData;
194 encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nsnull));
195 if (!encodedData.get())
196 return NS_ERROR_OUT_OF_MEMORY;
198 return prefBranch->SetCharPref(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
200 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
203 nsresult nsPrintSettingsX::_Clone(nsIPrintSettings **_retval)
205 NS_ENSURE_ARG_POINTER(_retval);
208 nsPrintSettingsX *newSettings = new nsPrintSettingsX(*this);
210 return NS_ERROR_FAILURE;
211 *_retval = newSettings;
216 NS_IMETHODIMP nsPrintSettingsX::_Assign(nsIPrintSettings *aPS)
218 nsPrintSettingsX *printSettingsX = static_cast<nsPrintSettingsX*>(aPS);
220 return NS_ERROR_UNEXPECTED;
221 *this = *printSettingsX;
226 nsPrintSettingsX::GetPMPrintSettings()
228 return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]);
232 nsPrintSettingsX::GetPMPrintSession()
234 return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]);
238 nsPrintSettingsX::GetPMPageFormat()
240 return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]);
244 nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat)
246 PMPageFormat oldPageFormat = GetPMPageFormat();
247 ::PMCopyPageFormat(aPageFormat, oldPageFormat);
248 [mPrintInfo updateFromPMPageFormat];