1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_folders.h>
25 #include <rtl/ustrbuf.hxx>
26 #include <sal/log.hxx>
28 #include <xmlscript/xmldlg_imexp.hxx>
29 #include <xmlscript/xml_helper.hxx>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <cppuhelper/bootstrap.hxx>
34 #include <comphelper/processfactory.hxx>
36 #include <vcl/svapp.hxx>
38 #include <com/sun/star/awt/UnoControlDialog.hpp>
39 #include <com/sun/star/awt/Toolkit.hpp>
40 #include <com/sun/star/awt/XToolkit.hpp>
41 #include <com/sun/star/awt/XControlModel.hpp>
42 #include <com/sun/star/awt/XControl.hpp>
43 #include <com/sun/star/awt/XDialog.hpp>
44 #include <com/sun/star/container/XNameContainer.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/XInitialization.hpp>
47 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <com/sun/star/io/XActiveDataSource.hpp>
49 #include <com/sun/star/registry/XSimpleRegistry.hpp>
50 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #include <com/sun/star/uno/XComponentContext.hpp>
53 using namespace ::cppu
;
54 using namespace ::com::sun::star
;
55 using namespace ::com::sun::star::uno
;
57 Reference
< XComponentContext
> createInitialComponentContext(
58 OUString
const & inst_dir
)
60 Reference
< XComponentContext
> xContext
;
65 oslFileError rc
= osl_getFileURLFromSystemPath(
66 inst_dir
.pData
, &file_url
.pData
);
67 OSL_ASSERT( osl_File_E_None
== rc
);
69 OUString unorc
= file_url
+ ("/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE("louno") );
71 return defaultBootstrap_InitialComponentContext( unorc
);
74 catch( const Exception
& rExc
)
76 SAL_WARN( "xmlscript", rExc
);
82 Reference
< container::XNameContainer
> importFile(
84 Reference
< XComponentContext
> const & xContext
)
86 // create the input stream
87 FILE *f
= ::fopen( fname
, "rb" );
90 ::fseek( f
, 0 ,SEEK_END
);
91 int nLength
= ::ftell( f
);
92 ::fseek( f
, 0, SEEK_SET
);
94 ByteSequence
bytes( nLength
);
95 ::fread( bytes
.getArray(), nLength
, 1, f
);
98 Reference
< container::XNameContainer
> xModel( xContext
->getServiceManager()->createInstanceWithContext(
99 "com.sun.star.awt.UnoControlDialogModel", xContext
), UNO_QUERY
);
100 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes
), xModel
, xContext
);
106 throw Exception( "### Cannot read file!" );
112 Reference
< container::XNameContainer
> const & xModel
,
113 Reference
< XComponentContext
> const & xContext
)
115 Reference
< io::XInputStreamProvider
> xProvider( ::xmlscript::exportDialogModel( xModel
, xContext
) );
116 Reference
< io::XInputStream
> xStream( xProvider
->createInputStream() );
118 Sequence
< sal_Int8
> bytes
;
119 sal_Int32 nRead
= xStream
->readBytes( bytes
, xStream
->available() );
122 Sequence
< sal_Int8
> readBytes
;
123 nRead
= xStream
->readBytes( readBytes
, 1024 );
126 OSL_ASSERT( readBytes
.getLength() >= nRead
);
128 sal_Int32 nPos
= bytes
.getLength();
129 bytes
.realloc( nPos
+ nRead
);
130 memcpy( bytes
.getArray() + nPos
, readBytes
.getConstArray(), (sal_uInt32
)nRead
);
133 FILE * f
= ::fopen( fname
, "w" );
134 ::fwrite( bytes
.getConstArray(), 1, bytes
.getLength(), f
);
138 class MyApp
: public Application
148 if (GetCommandLineParamCount() < 2)
150 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]" );
154 Reference
< XComponentContext
> xContext(
155 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
156 Reference
< lang::XMultiServiceFactory
> xMSF(
157 xContext
->getServiceManager(), UNO_QUERY
);
161 ::comphelper::setProcessServiceFactory( xMSF
);
163 Reference
< awt::XToolkit
> xToolkit
= awt::Toolkit::create( xContext
);
166 OString
aParam1( OUStringToOString(
167 OUString( GetCommandLineParam( 1 ) ),
168 RTL_TEXTENCODING_ASCII_US
) );
169 Reference
< container::XNameContainer
> xModel(
170 importFile( aParam1
.getStr(), xContext
) );
171 OSL_ASSERT( xModel
.is() );
173 Reference
< awt::XUnoControlDialog
> xDlg
= UnoControlDialog::create( xContext
);
174 xDlg
->setModel( xModel
);
175 xDlg
->createPeer( xToolkit
, 0 );
178 if (GetCommandLineParamCount() == 3)
180 // write modified dialogs
181 OString
aParam2( OUStringToOString(
182 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US
) );
183 exportToFile( aParam2
.getStr(), xModel
, xContext
);
186 catch (const xml::sax::SAXException
& rExc
)
188 OUString
aStr( rExc
);
190 if (rExc
.WrappedException
>>= exc
)
195 SAL_WARN( "xmlscript", aStr
);
197 catch (const uno::Exception
& rExc
)
199 SAL_WARN( "xmlscript", rExc
);
202 Reference
< lang::XComponent
> xComp( xContext
, UNO_QUERY
);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */