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