Files
libreoffice/xmlscript/test/imexp.cxx

214 lines
6.6 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-07-09 14:31:34 +01:00
/*
* This file is part of the LibreOffice project.
*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
*/
2001-02-16 13:14:51 +00:00
#include <config_folders.h>
2001-02-16 13:14:51 +00:00
#include <stdio.h>
#include <osl/file.h>
2001-02-16 13:14:51 +00:00
2001-09-19 07:46:35 +00:00
#include <rtl/ustrbuf.hxx>
2001-02-16 13:14:51 +00:00
#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>
#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/bootstrap.hxx>
2001-02-16 13:14:51 +00:00
#include <cppuhelper/implbase2.hxx>
#include <comphelper/processfactory.hxx>
#include <vcl/svapp.hxx>
#include <com/sun/star/awt/UnoControlDialog.hpp>
#include <com/sun/star/awt/Toolkit.hpp>
#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/awt/XDialog.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
2001-02-16 13:14:51 +00:00
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/io/XActiveDataSource.hpp>
2001-02-16 13:14:51 +00:00
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
2001-02-16 13:14:51 +00:00
using namespace ::rtl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
Reference< XComponentContext > createInitialComponentContext(
OUString const & inst_dir )
2001-02-16 13:14:51 +00:00
{
Reference< XComponentContext > xContext;
2001-02-16 13:14:51 +00:00
try
{
OUString file_url;
oslFileError rc = osl_getFileURLFromSystemPath(
inst_dir.pData, &file_url.pData );
OSL_ASSERT( osl_File_E_None == rc );
2001-02-16 13:14:51 +00:00
OUString unorc = file_url + ("/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("uno") );
return defaultBootstrap_InitialComponentContext( unorc );
2001-02-16 13:14:51 +00:00
}
2012-01-09 03:16:33 +09:00
catch( const Exception& rExc )
2001-02-16 13:14:51 +00:00
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
OSL_FAIL( aStr.getStr() );
2001-02-16 13:14:51 +00:00
}
2001-09-19 07:46:35 +00:00
return xContext;
2001-02-16 13:14:51 +00:00
}
Revert "fdo#46808, Convert awt::UnoControlDialogModel to new style" This reverts commit 6c61b20a8d4a6dcac28801cde82a211fb7e30654. As discussed at <http://lists.freedesktop.org/archives/libreoffice/2013-May/052449.html> "Re: fdo#46808, Convert awt::UnoControlDialogModel to new style problem" why the odd change in 2e2a4827ce6708f0e8677dba9cc92e1479a44086 "scripting: get CreateUnoDialog() work again" appears to fix things again: The problem is that the implementation of the css.awt.UnoControlDialogModel involves UNO aggregation (IMPL_CREATE_INSTANCE_WITH_GEOMETRY(UnoControlDialogModel) in toolkit/soruce/helper/registerservices.cxx creating a OGeometryControlModel<UnoControlDialogModel> instance that aggregates a UnoControlDialogModel instance). That means that queryInterface can return a reference to something that is technically a different object, and that's what's happening here, and explains why calling setPropertyValue in two different ways on what logically appears to be a single object can end up calling two different implementations (of two different physical objects). (UNO aggregation is known to be broken and should not be used. Nevertheless, there's still code that does---code that is a horrible mess and hard to clean up.) That all this worked as intended in the past is just sheer luck, but any way of substantially touching it is asking for trouble. I'm going to revert 6c61b20a8d4a6dcac28801cde82a211fb7e30654 again. I wasn't able to revert without also reverting be50ad28f5bbdaeff527f646481ce263843c2401 "fdo#46808, Convert awt::XUnoControlDialog to new style," as the two were tightly dependant. Also reverts all the follow-up fixes cb4b6dde8fda2a5848e11063028bf44d72f85431 "-Werror,-Wuninitialized" (sans the const-ness fix in UpdateHandler::insertControlModel), 697a007c61b9cabceb9767fad87cd5822b300452 "Fix exception specifications," 2ce6828bbbf6ba181bb2276adeec279e74151ef6 "fix awt::UnoControlModelDialog crash," and 2e2a4827ce6708f0e8677dba9cc92e1479a44086 "scripting: get CreateUnoDialog() work again." Conflicts: basctl/source/dlged/dlged.cxx filter/source/t602/t602filter.cxx xmlscript/test/imexp.cxx Change-Id: I5d133468062f3ca36300db52fbd699be1ac72998
2013-05-24 22:44:30 +02:00
Reference< container::XNameContainer > importFile(
2001-09-19 07:46:35 +00:00
char const * fname,
Reference< XComponentContext > const & xContext )
2001-02-16 13:14:51 +00:00
{
2001-02-27 11:45:17 +00:00
// create the input stream
FILE *f = ::fopen( fname, "rb" );
if (f)
2001-02-16 13:14:51 +00:00
{
2001-02-27 11:45:17 +00:00
::fseek( f, 0 ,SEEK_END );
int nLength = ::ftell( f );
::fseek( f, 0, SEEK_SET );
2001-02-16 13:14:51 +00:00
2001-03-14 15:41:59 +00:00
ByteSequence bytes( nLength );
2001-02-27 11:45:17 +00:00
::fread( bytes.getArray(), nLength, 1, f );
::fclose( f );
2001-02-16 13:14:51 +00:00
Revert "fdo#46808, Convert awt::UnoControlDialogModel to new style" This reverts commit 6c61b20a8d4a6dcac28801cde82a211fb7e30654. As discussed at <http://lists.freedesktop.org/archives/libreoffice/2013-May/052449.html> "Re: fdo#46808, Convert awt::UnoControlDialogModel to new style problem" why the odd change in 2e2a4827ce6708f0e8677dba9cc92e1479a44086 "scripting: get CreateUnoDialog() work again" appears to fix things again: The problem is that the implementation of the css.awt.UnoControlDialogModel involves UNO aggregation (IMPL_CREATE_INSTANCE_WITH_GEOMETRY(UnoControlDialogModel) in toolkit/soruce/helper/registerservices.cxx creating a OGeometryControlModel<UnoControlDialogModel> instance that aggregates a UnoControlDialogModel instance). That means that queryInterface can return a reference to something that is technically a different object, and that's what's happening here, and explains why calling setPropertyValue in two different ways on what logically appears to be a single object can end up calling two different implementations (of two different physical objects). (UNO aggregation is known to be broken and should not be used. Nevertheless, there's still code that does---code that is a horrible mess and hard to clean up.) That all this worked as intended in the past is just sheer luck, but any way of substantially touching it is asking for trouble. I'm going to revert 6c61b20a8d4a6dcac28801cde82a211fb7e30654 again. I wasn't able to revert without also reverting be50ad28f5bbdaeff527f646481ce263843c2401 "fdo#46808, Convert awt::XUnoControlDialog to new style," as the two were tightly dependant. Also reverts all the follow-up fixes cb4b6dde8fda2a5848e11063028bf44d72f85431 "-Werror,-Wuninitialized" (sans the const-ness fix in UpdateHandler::insertControlModel), 697a007c61b9cabceb9767fad87cd5822b300452 "Fix exception specifications," 2ce6828bbbf6ba181bb2276adeec279e74151ef6 "fix awt::UnoControlModelDialog crash," and 2e2a4827ce6708f0e8677dba9cc92e1479a44086 "scripting: get CreateUnoDialog() work again." Conflicts: basctl/source/dlged/dlged.cxx filter/source/t602/t602filter.cxx xmlscript/test/imexp.cxx Change-Id: I5d133468062f3ca36300db52fbd699be1ac72998
2013-05-24 22:44:30 +02:00
Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
2001-09-19 07:46:35 +00:00
::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
2001-03-14 15:41:59 +00:00
return xModel;
2001-02-16 13:14:51 +00:00
}
else
{
throw Exception( "### Cannot read file!", Reference< XInterface >() );
2001-02-16 13:14:51 +00:00
}
}
2001-02-27 11:45:17 +00:00
void exportToFile(
2001-02-16 13:14:51 +00:00
char const * fname,
2001-09-19 07:46:35 +00:00
Reference< container::XNameContainer > const & xModel,
Reference< XComponentContext > const & xContext )
2001-02-16 13:14:51 +00:00
{
2001-09-19 07:46:35 +00:00
Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
2001-03-14 15:41:59 +00:00
Reference< io::XInputStream > xStream( xProvider->createInputStream() );
2001-02-27 11:45:17 +00:00
Sequence< sal_Int8 > bytes;
2001-03-19 09:08:36 +00:00
sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
for (;;)
2001-03-14 15:41:59 +00:00
{
2001-03-19 09:08:36 +00:00
Sequence< sal_Int8 > readBytes;
nRead = xStream->readBytes( readBytes, 1024 );
if (! nRead)
break;
2001-03-19 09:16:58 +00:00
OSL_ASSERT( readBytes.getLength() >= nRead );
2001-03-19 09:08:36 +00:00
sal_Int32 nPos = bytes.getLength();
bytes.realloc( nPos + nRead );
memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
2001-03-14 15:41:59 +00:00
}
2001-02-16 13:14:51 +00:00
FILE * f = ::fopen( fname, "w" );
2001-02-27 11:45:17 +00:00
::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
2001-02-16 13:14:51 +00:00
::fflush( f );
::fclose( f );
}
class MyApp : public Application
{
public:
2001-03-14 15:41:59 +00:00
void Main();
2001-02-16 13:14:51 +00:00
};
MyApp aMyApp;
void MyApp::Main()
{
if (GetCommandLineParamCount() < 2)
2001-02-16 13:14:51 +00:00
{
OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
2001-02-16 13:14:51 +00:00
return;
}
Reference< XComponentContext > xContext(
createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
Reference< lang::XMultiServiceFactory > xMSF(
xContext->getServiceManager(), UNO_QUERY );
2001-02-16 13:14:51 +00:00
try
{
::comphelper::setProcessServiceFactory( xMSF );
Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
2001-02-16 13:14:51 +00:00
2001-02-27 11:45:17 +00:00
// import dialogs
OString aParam1( OUStringToOString(
OUString( GetCommandLineParam( 1 ) ),
RTL_TEXTENCODING_ASCII_US ) );
Reference< container::XNameContainer > xModel(
importFile( aParam1.getStr(), xContext ) );
2001-03-14 15:41:59 +00:00
OSL_ASSERT( xModel.is() );
2001-02-16 13:14:51 +00:00
Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );;
xDlg->setModel( xModel );
2001-03-14 15:41:59 +00:00
xDlg->createPeer( xToolkit, 0 );
xDlg->execute();
2001-02-28 17:22:08 +00:00
if (GetCommandLineParamCount() == 3)
2001-02-28 17:22:08 +00:00
{
// write modified dialogs
OString aParam2( OUStringToOString(
OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
2001-09-19 07:46:35 +00:00
exportToFile( aParam2.getStr(), xModel, xContext );
2001-02-28 17:22:08 +00:00
}
2001-02-16 13:14:51 +00:00
}
2012-01-09 03:16:33 +09:00
catch (const xml::sax::SAXException & rExc)
2001-03-14 15:41:59 +00:00
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
uno::Exception exc;
if (rExc.WrappedException >>= exc)
{
aStr += OString( " >>> " );
aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
}
OSL_FAIL( aStr.getStr() );
2001-03-14 15:41:59 +00:00
}
2012-01-09 03:16:33 +09:00
catch (const uno::Exception & rExc)
2001-02-16 13:14:51 +00:00
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
OSL_FAIL( aStr.getStr() );
2001-02-16 13:14:51 +00:00
}
2001-09-19 07:46:35 +00:00
Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
if (xComp.is())
2001-02-16 13:14:51 +00:00
{
2001-09-19 07:46:35 +00:00
xComp->dispose();
2001-02-16 13:14:51 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */