Files
libreoffice/comphelper/source/misc/storagehelper.cxx
Rüdiger Timm 28bbe98abe CWS-TOOLING: integrate CWS dba31d
2008-12-08 16:22:07 +0100 rt  r265005 : Remove DOS lineends
2008-12-05 13:56:24 +0100 fs  r264906 : #i10000# removed unreachable statement
2008-11-20 11:41:26 +0100 fs  r264037 : merged in the fix for #i95865# (it was wrongly committed to CWS dba32a, should have been here)
2008-11-20 11:34:24 +0100 fs  r264036 : line ends
2008-11-14 08:44:50 +0100 lla  r263665 : #i10000# comparsion between int and uint fixed
2008-11-13 13:31:12 +0100 lla  r263641 : #i10000# build problem fixed
2008-11-13 11:20:01 +0100 lla  r263625 : #i96130# hard code name of extension
2008-11-12 11:13:41 +0100 fs  r263582 : #i96096# when opening a SRB-report fails due to the missing SRB extension, log this as warning only, and proceed with the migration
2008-11-12 11:11:35 +0100 fs  r263581 : #i96096# ContentType handling. Now all contents deliver proper results in XContent::getContentType
2008-11-12 11:10:11 +0100 fs  r263580 : #i96096# new ctors taking UNO_QUERY_THROW
2008-11-11 10:10:13 +0100 lla  r263546 : CWS-TOOLING: rebase CWS dba31d to trunk@263288 (milestone: DEV300:m35)
2008-11-06 15:55:39 +0100 oj  r263393 : #i93452# get field from model fallbackis the name
2008-11-06 15:31:47 +0100 oj  r263392 : #i93465# remeber location of floating windows
2008-11-06 13:36:24 +0100 oj  r263381 : #i93450# check typemap for null
2008-11-06 13:28:49 +0100 oj  r263379 : #i93020# empty column list boxes when new relation should be created
2008-11-06 12:33:53 +0100 oj  r263377 : #i93012# set border to default : flat
2008-11-06 12:26:54 +0100 oj  r263375 : #i74927# do some less calls for odbc
2008-11-06 09:34:01 +0100 oj  r263362 : #i93383# grabFocus in suspend to get allmodified cells
2008-11-03 21:01:39 +0100 oj  r263308 : #i86739# check if slash can be valid for tables
2008-11-03 14:40:21 +0100 oj  r263287 : #i86739# check if slash can be valid for tables
2008-11-03 14:32:17 +0100 oj  r263286 : #i95227# column width 
2008-11-03 14:27:26 +0100 oj  r263285 : link fwe
2008-11-03 14:24:54 +0100 oj  r263284 : #i95235# changed to hold no ref only weak
2008-10-31 11:21:48 +0100 oj  r262859 : #i93459# set images add menu entry
2008-10-31 09:06:37 +0100 oj  r262851 : #i88629# correct fileopen filter for database odb files
2008-10-30 15:01:04 +0100 oj  r262828 : #i95229# set filter at the composer
2008-10-29 15:57:41 +0100 oj  r262817 : #i95235# changed to hold no ref only weak
2008-10-29 15:57:19 +0100 oj  r262816 : #i95235# changed to hold no ref only weak
2008-10-29 15:57:03 +0100 oj  r262815 : #i95235# changed to hold no ref only weak
2008-10-29 15:56:15 +0100 oj  r262814 : #i95235# filtermanger changed to hold no ref only weak
2008-10-29 10:32:39 +0100 oj  r262773 : #i93474# use correct table name
2008-10-28 13:49:33 +0100 lla  r262744 : #i95524# make an Invalidate and refresh on Tables
2008-10-28 10:45:02 +0100 fs  r262707 : line ends
2008-10-28 10:34:42 +0100 fs  r262706 : #i95522# don't expect the component to live in a TopWindow
2008-10-28 08:30:40 +0100 lla  r262696 : #i93176# set preview mode on view
2008-10-28 07:56:57 +0100 oj  r262694 : merge cvs svn
2008-10-27 14:13:51 +0100 oj  r262673 : #i94129# use dummy data
2008-10-27 12:38:45 +0100 fs  r262669 : #i94125# rework ScrollColumns
2008-10-23 15:53:57 +0200 oj  r262624 : #i94568# do not load the embeddedobj just copy the storage
2008-10-23 14:39:14 +0200 oj  r262622 : #i94129# handle chart correctly
2008-10-22 10:51:19 +0200 lla  r262582 : #i94115# problem with left walk chart shape fixed
2008-10-22 07:47:48 +0200 oj  r262576 : #i94455# rename now do not use remove insert
2008-10-22 07:47:27 +0200 oj  r262575 : #i94455# rename now do not use remove insert
2008-10-21 12:46:26 +0200 lla  r262567 : #i93845# extra check if default schema doesn't exists, fix assertion
2008-12-09 07:25:38 +00:00

435 lines
17 KiB
C++

/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2008 by Sun Microsystems, Inc.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: storagehelper.cxx,v $
* $Revision: 1.13 $
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_comphelper.hxx"
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/XEncryptionProtectedSource.hpp>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/IllegalTypeException.hpp>
#include <ucbhelper/content.hxx>
#include <comphelper/fileformat.h>
#include <comphelper/processfactory.hxx>
#include <comphelper/documentconstants.hxx>
#include <comphelper/storagehelper.hxx>
using namespace ::com::sun::star;
namespace comphelper {
// ----------------------------------------------------------------------
uno::Reference< lang::XSingleServiceFactory > OStorageHelper::GetStorageFactory(
const uno::Reference< lang::XMultiServiceFactory >& xSF )
throw ( uno::Exception )
{
uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
if ( !xFactory.is() )
throw uno::RuntimeException();
uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
uno::UNO_QUERY );
if ( !xStorageFactory.is() )
throw uno::RuntimeException();
return xStorageFactory;
}
// ----------------------------------------------------------------------
uno::Reference< lang::XSingleServiceFactory > OStorageHelper::GetFileSystemStorageFactory(
const uno::Reference< lang::XMultiServiceFactory >& xSF )
throw ( uno::Exception )
{
uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
if ( !xFactory.is() )
throw uno::RuntimeException();
uno::Reference < lang::XSingleServiceFactory > xStorageFactory(
xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.FileSystemStorageFactory" ) ),
uno::UNO_QUERY );
if ( !xStorageFactory.is() )
throw uno::RuntimeException();
return xStorageFactory;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetTemporaryStorage(
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstance(),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromURL(
const ::rtl::OUString& aURL,
sal_Int32 nStorageMode,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< uno::Any > aArgs( 2 );
aArgs[0] <<= aURL;
aArgs[1] <<= nStorageMode;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromURL2(
const ::rtl::OUString& aURL,
sal_Int32 nStorageMode,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< uno::Any > aArgs( 2 );
aArgs[0] <<= aURL;
aArgs[1] <<= nStorageMode;
uno::Reference< lang::XSingleServiceFactory > xFact;
try {
::ucbhelper::Content aCntnt( aURL,
uno::Reference< ::com::sun::star::ucb::XCommandEnvironment > () );
if (aCntnt.isDocument()) {
xFact = GetStorageFactory( xFactory );
} else {
xFact = GetFileSystemStorageFactory( xFactory );
}
} catch (uno::Exception &) { }
if (!xFact.is()) throw uno::RuntimeException();
uno::Reference< embed::XStorage > xTempStorage(
xFact->createInstanceWithArguments( aArgs ), uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromInputStream(
const uno::Reference < io::XInputStream >& xStream,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< uno::Any > aArgs( 2 );
aArgs[0] <<= xStream;
aArgs[1] <<= embed::ElementModes::READ;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageFromStream(
const uno::Reference < io::XStream >& xStream,
sal_Int32 nStorageMode,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< uno::Any > aArgs( 2 );
aArgs[0] <<= xStream;
aArgs[1] <<= nStorageMode;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
void OStorageHelper::CopyInputToOutput(
const uno::Reference< io::XInputStream >& xInput,
const uno::Reference< io::XOutputStream >& xOutput )
throw ( uno::Exception )
{
static const sal_Int32 nConstBufferSize = 32000;
sal_Int32 nRead;
uno::Sequence < sal_Int8 > aSequence ( nConstBufferSize );
do
{
nRead = xInput->readBytes ( aSequence, nConstBufferSize );
if ( nRead < nConstBufferSize )
{
uno::Sequence < sal_Int8 > aTempBuf ( aSequence.getConstArray(), nRead );
xOutput->writeBytes ( aTempBuf );
}
else
xOutput->writeBytes ( aSequence );
}
while ( nRead == nConstBufferSize );
}
// ----------------------------------------------------------------------
uno::Reference< io::XInputStream > OStorageHelper::GetInputStreamFromURL(
const ::rtl::OUString& aURL,
const uno::Reference< lang::XMultiServiceFactory >& xSF )
throw ( uno::Exception )
{
uno::Reference< lang::XMultiServiceFactory > xFactory = xSF.is() ? xSF : ::comphelper::getProcessServiceFactory();
if ( !xFactory.is() )
throw uno::RuntimeException();
uno::Reference < ::com::sun::star::ucb::XSimpleFileAccess > xTempAccess(
xFactory->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
uno::UNO_QUERY );
if ( !xTempAccess.is() )
throw uno::RuntimeException();
uno::Reference< io::XInputStream > xInputStream = xTempAccess->openFileRead( aURL );
if ( !xInputStream.is() )
throw uno::RuntimeException();
return xInputStream;
}
// ----------------------------------------------------------------------
void OStorageHelper::SetCommonStoragePassword(
const uno::Reference< embed::XStorage >& xStorage,
const ::rtl::OUString& aPass )
throw ( uno::Exception )
{
uno::Reference< embed::XEncryptionProtectedSource > xEncrSet( xStorage, uno::UNO_QUERY );
if ( !xEncrSet.is() )
throw io::IOException(); // TODO
xEncrSet->setEncryptionPassword( aPass );
}
// ----------------------------------------------------------------------
sal_Int32 OStorageHelper::GetXStorageFormat(
const uno::Reference< embed::XStorage >& xStorage )
throw ( uno::Exception )
{
uno::Reference< beans::XPropertySet > xStorProps( xStorage, uno::UNO_QUERY_THROW );
::rtl::OUString aMediaType;
xStorProps->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType;
sal_Int32 nResult = 0;
// TODO/LATER: the filter configuration could be used to detect it later, or batter a special service
if (
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_DRAW_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_IMPRESS_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CALC_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CHART_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_MATH_ASCII )
)
{
nResult = SOFFICE_FILEFORMAT_60;
}
else
if (
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII ) ||
aMediaType.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII )
)
{
nResult = SOFFICE_FILEFORMAT_8;
}
else
{
// the mediatype is not known
throw beans::IllegalTypeException();
}
return nResult;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetTemporaryStorageOfFormat(
const ::rtl::OUString& aFormat,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Reference< lang::XMultiServiceFactory > xFactoryToUse = xFactory.is() ? xFactory : ::comphelper::getProcessServiceFactory();
if ( !xFactoryToUse.is() )
throw uno::RuntimeException();
uno::Reference< io::XStream > xTmpStream(
xFactoryToUse->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.TempFile" ) ) ),
uno::UNO_QUERY_THROW );
return GetStorageOfFormatFromStream( aFormat, xTmpStream, embed::ElementModes::READWRITE, xFactoryToUse );
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromURL(
const ::rtl::OUString& aFormat,
const ::rtl::OUString& aURL,
sal_Int32 nStorageMode,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< beans::PropertyValue > aProps( 1 );
aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
aProps[0].Value <<= aFormat;
uno::Sequence< uno::Any > aArgs( 3 );
aArgs[0] <<= aURL;
aArgs[1] <<= nStorageMode;
aArgs[2] <<= aProps;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromInputStream(
const ::rtl::OUString& aFormat,
const uno::Reference < io::XInputStream >& xStream,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< beans::PropertyValue > aProps( 1 );
aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
aProps[0].Value <<= aFormat;
uno::Sequence< uno::Any > aArgs( 3 );
aArgs[0] <<= xStream;
aArgs[1] <<= embed::ElementModes::READ;
aArgs[2] <<= aProps;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
uno::Reference< embed::XStorage > OStorageHelper::GetStorageOfFormatFromStream(
const ::rtl::OUString& aFormat,
const uno::Reference < io::XStream >& xStream,
sal_Int32 nStorageMode,
const uno::Reference< lang::XMultiServiceFactory >& xFactory )
throw ( uno::Exception )
{
uno::Sequence< beans::PropertyValue > aProps( 1 );
aProps[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
aProps[0].Value <<= aFormat;
uno::Sequence< uno::Any > aArgs( 3 );
aArgs[0] <<= xStream;
aArgs[1] <<= nStorageMode;
aArgs[2] <<= aProps;
uno::Reference< embed::XStorage > xTempStorage( GetStorageFactory( xFactory )->createInstanceWithArguments( aArgs ),
uno::UNO_QUERY );
if ( !xTempStorage.is() )
throw uno::RuntimeException();
return xTempStorage;
}
// ----------------------------------------------------------------------
sal_Bool IsValidZipEntryFileName(
const sal_Unicode *pChar, sal_Int32 nLength, sal_Bool bSlashAllowed )
{
for ( sal_Int32 i = 0 ; i < nLength ; i++ )
{
switch ( pChar[i] )
{
case '\\':
case '?':
case '<':
case '>':
case '\"':
case '|':
case ':':
return sal_False;
case '/':
if ( !bSlashAllowed )
return sal_False;
break;
default:
if ( pChar[i] < 32 || pChar[i] > 127 )
return sal_False;
// Note: in case this ever supports unicode, watch out for surrogate pairs!
}
}
return sal_True;
}
}