2007-01-12 00:54:29 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2007-01-12 00:54:29 +03:00
|
|
|
|
|
|
|
#include "nsPrintSettingsX.h"
|
2008-02-21 02:47:05 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
2007-01-12 00:54:29 +03:00
|
|
|
|
|
|
|
#include "plbase64.h"
|
2009-10-09 09:20:37 +04:00
|
|
|
#include "plstr.h"
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
#include "nsCocoaUtils.h"
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2011-05-27 12:15:20 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
#define MAC_OS_X_PAGE_SETUP_PREFNAME "print.macosx.pagesetup-2"
|
2009-10-09 09:14:31 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsPrintSettingsX, nsPrintSettings, nsPrintSettingsX)
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
nsPrintSettingsX::nsPrintSettingsX()
|
2007-01-12 00:54:29 +03:00
|
|
|
{
|
2009-10-09 09:20:37 +04:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
mPrintInfo = [[NSPrintInfo sharedPrintInfo] copy];
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
nsPrintSettingsX::nsPrintSettingsX(const nsPrintSettingsX& src)
|
2007-01-12 00:54:29 +03:00
|
|
|
{
|
|
|
|
*this = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettingsX::~nsPrintSettingsX()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
[mPrintInfo release];
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettingsX& nsPrintSettingsX::operator=(const nsPrintSettingsX& rhs)
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2007-01-12 00:54:29 +03:00
|
|
|
if (this == &rhs) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintSettings::operator=(rhs);
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
[mPrintInfo release];
|
|
|
|
mPrintInfo = [rhs.mPrintInfo copy];
|
2007-01-12 00:54:29 +03:00
|
|
|
|
|
|
|
return *this;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(*this);
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintSettingsX::Init()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
InitUnwriteableMargin();
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
// Should be called whenever the page format changes.
|
2008-03-19 23:51:42 +03:00
|
|
|
NS_IMETHODIMP nsPrintSettingsX::InitUnwriteableMargin()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
PMPaper paper;
|
|
|
|
PMPaperMargins paperMargin;
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPageFormat pageFormat = GetPMPageFormat();
|
|
|
|
::PMGetPageFormatPaper(pageFormat, &paper);
|
2008-03-19 23:51:42 +03:00
|
|
|
::PMPaperGetMargins(paper, &paperMargin);
|
2010-08-13 13:59:16 +04: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 23:51:42 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
void
|
|
|
|
nsPrintSettingsX::SetCocoaPrintInfo(NSPrintInfo* aPrintInfo)
|
2007-01-12 00:54:29 +03:00
|
|
|
{
|
2015-05-06 21:10:21 +03:00
|
|
|
if (mPrintInfo != aPrintInfo) {
|
|
|
|
[mPrintInfo release];
|
|
|
|
mPrintInfo = [aPrintInfo retain];
|
|
|
|
}
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrintSettingsX::ReadPageFormatFromPrefs()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedData;
|
2011-05-27 12:15:20 +04:00
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, &encodedData);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-01-12 00:54:29 +03:00
|
|
|
return rv;
|
2011-05-27 12:15:20 +04:00
|
|
|
}
|
2007-01-12 00:54:29 +03:00
|
|
|
|
|
|
|
// decode the base64
|
2012-07-30 18:20:58 +04:00
|
|
|
char* decodedData = PL_Base64Decode(encodedData.get(), encodedData.Length(), nullptr);
|
2013-05-08 07:40:12 +04:00
|
|
|
NSData* data = [NSData dataWithBytes:decodedData length:strlen(decodedData)];
|
2009-10-09 09:20:37 +04:00
|
|
|
if (!data)
|
2007-01-12 00:54:29 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPageFormat newPageFormat;
|
|
|
|
OSStatus status = ::PMPageFormatCreateWithDataRepresentation((CFDataRef)data, &newPageFormat);
|
|
|
|
if (status == noErr) {
|
|
|
|
SetPMPageFormat(newPageFormat);
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
2009-10-09 09:20:37 +04:00
|
|
|
InitUnwriteableMargin();
|
2007-01-12 00:54:29 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPrintSettingsX::WritePageFormatToPrefs()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPageFormat pageFormat = GetPMPageFormat();
|
|
|
|
if (pageFormat == kPMNoPageFormat)
|
2007-01-12 00:54:29 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2009-10-09 09:20:37 +04:00
|
|
|
|
|
|
|
NSData* data = nil;
|
|
|
|
OSStatus err = ::PMPageFormatCreateDataRepresentation(pageFormat, (CFDataRef*)&data, kPMDataFormatXMLDefault);
|
2007-01-12 00:54:29 +03:00
|
|
|
if (err != noErr)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encodedData;
|
2012-07-30 18:20:58 +04:00
|
|
|
encodedData.Adopt(PL_Base64Encode((char*)[data bytes], [data length], nullptr));
|
2007-01-12 00:54:29 +03:00
|
|
|
if (!encodedData.get())
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-05-27 12:15:20 +04:00
|
|
|
return Preferences::SetCString(MAC_OS_X_PAGE_SETUP_PREFNAME, encodedData);
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintSettingsX::_Clone(nsIPrintSettings **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2007-01-12 00:54:29 +03:00
|
|
|
|
|
|
|
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 11:08:04 +04:00
|
|
|
nsPrintSettingsX *printSettingsX = static_cast<nsPrintSettingsX*>(aPS);
|
2007-01-12 00:54:29 +03:00
|
|
|
if (!printSettingsX)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
*this = *printSettingsX;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPrintSettings
|
|
|
|
nsPrintSettingsX::GetPMPrintSettings()
|
2007-01-12 00:54:29 +03:00
|
|
|
{
|
2010-02-23 23:04:05 +03:00
|
|
|
return static_cast<PMPrintSettings>([mPrintInfo PMPrintSettings]);
|
2007-01-12 00:54:29 +03:00
|
|
|
}
|
2008-09-25 06:26:58 +04:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPrintSession
|
|
|
|
nsPrintSettingsX::GetPMPrintSession()
|
2007-01-12 00:54:29 +03:00
|
|
|
{
|
2010-02-23 23:04:05 +03:00
|
|
|
return static_cast<PMPrintSession>([mPrintInfo PMPrintSession]);
|
2008-02-21 02:47:05 +03:00
|
|
|
}
|
2009-01-29 11:46:23 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
PMPageFormat
|
|
|
|
nsPrintSettingsX::GetPMPageFormat()
|
2009-01-29 11:46:23 +03:00
|
|
|
{
|
2010-02-23 23:04:05 +03:00
|
|
|
return static_cast<PMPageFormat>([mPrintInfo PMPageFormat]);
|
2009-10-09 09:20:37 +04:00
|
|
|
}
|
2009-01-29 11:46:23 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
void
|
|
|
|
nsPrintSettingsX::SetPMPageFormat(PMPageFormat aPageFormat)
|
|
|
|
{
|
|
|
|
PMPageFormat oldPageFormat = GetPMPageFormat();
|
|
|
|
::PMCopyPageFormat(aPageFormat, oldPageFormat);
|
2010-02-23 23:04:05 +03:00
|
|
|
[mPrintInfo updateFromPMPageFormat];
|
2009-01-29 11:46:23 +03:00
|
|
|
}
|
2009-10-09 09:20:37 +04:00
|
|
|
|