2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Conrad Carlen <ccarlen@netscape.com>
|
2009-10-08 22:20:37 -07:00
|
|
|
* Markus Stange <mstange@themasta.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsPrintSettingsX.h"
|
2008-02-20 15:47:05 -08:00
|
|
|
#include "nsObjCExceptions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "plbase64.h"
|
2009-10-08 22:20:37 -07:00
|
|
|
#include "plstr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
#include "nsCocoaUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-27 01:15:20 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2"
|
2009-10-08 22:14:31 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
nsPrintSettingsX::nsPrintSettingsX()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-08 22:20:37 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*this = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettingsX::~nsPrintSettingsX()
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
[mPrintInfo release];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs)
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (this == &rhs) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettings::operator=(rhs);
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
[mPrintInfo release];
|
|
|
|
mPrintInfo = [rhs.mPrintInfo copy];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return *this;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintSettingsX::Init()
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
InitUnwriteableMargin();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
return NS_OK;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
// Should be called whenever the page format changes.
|
2008-03-19 13:51:42 -07:00
|
|
|
NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
PMPaper paper;
|
|
|
|
PMPaperMargins paperMargin;
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPageFormat pageFormat = GetPMPageFormat();
|
|
|
|
::PMGetPageFormatPaper(pageFormat, &paper);
|
2008-03-19 13:51:42 -07:00
|
|
|
::PMPaperGetMargins(paper, &paperMargin);
|
2010-08-13 02:59:16 -07:00
|
|
|
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);
|
2008-03-19 13:51:42 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
void
|
|
|
|
nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-08 22:20:37 -07:00
|
|
|
mPrintInfo = aPrintInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs()
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2011-05-27 01:15:20 -07:00
|
|
|
nsCAutoString encodedData;
|
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, &encodedData);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
2011-05-27 01:15:20 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// decode the base64
|
2009-10-08 22:20:37 -07:00
|
|
|
char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nsnull);
|
|
|
|
NSData* data = [NSData dataWithBytes:decodedData length:PL_strlen(decodedData)];
|
|
|
|
if (!data)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPageFormat newPageFormat;
|
|
|
|
OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat);
|
|
|
|
if (status == noErr) {
|
|
|
|
SetPMPageFormat(newPageFormat);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-10-08 22:20:37 -07:00
|
|
|
InitUnwriteableMargin();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
return NS_OK;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs()
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPageFormat pageFormat = GetPMPageFormat();
|
|
|
|
if (pageFormat == kPMNoPageFormat)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2009-10-08 22:20:37 -07:00
|
|
|
|
|
|
|
NSData* data = nil;
|
|
|
|
OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data, kPMDataFormatXMLDefault);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (err != noErr)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-05-27 01:15:20 -07:00
|
|
|
nsCAutoString encodedData;
|
2009-10-08 22:20:37 -07:00
|
|
|
encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nsnull));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!encodedData.get())
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-05-27 01:15:20 -07:00
|
|
|
return Preferences::SetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintSettingsX::_Clone(nsIPrintSettings **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsPrintSettingsX *printSettingsX = static_cast<nsPrintSettingsX*>(aPS);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!printSettingsX)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
*this = *printSettingsX;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPrintSettings
|
|
|
|
nsPrintSettingsX::GetPMPrintSettings()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-23 12:04:05 -08:00
|
|
|
return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-09-24 19:26:58 -07:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPrintSession
|
|
|
|
nsPrintSettingsX::GetPMPrintSession()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-23 12:04:05 -08:00
|
|
|
return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]);
|
2008-02-20 15:47:05 -08:00
|
|
|
}
|
2009-01-29 00:46:23 -08:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
PMPageFormat
|
|
|
|
nsPrintSettingsX::GetPMPageFormat()
|
2009-01-29 00:46:23 -08:00
|
|
|
{
|
2010-02-23 12:04:05 -08:00
|
|
|
return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]);
|
2009-10-08 22:20:37 -07:00
|
|
|
}
|
2009-01-29 00:46:23 -08:00
|
|
|
|
2009-10-08 22:20:37 -07:00
|
|
|
void
|
|
|
|
nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat)
|
|
|
|
{
|
|
|
|
PMPageFormat oldPageFormat = GetPMPageFormat();
|
|
|
|
::PMCopyPageFormat(aPageFormat, oldPageFormat);
|
2010-02-23 12:04:05 -08:00
|
|
|
[mPrintInfo updateFromPMPageFormat];
|
2009-01-29 00:46:23 -08:00
|
|
|
}
|
2009-10-08 22:20:37 -07:00
|
|
|
|