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/.
11 // Include this before stdio.h for the __MINGW32__ sake.
12 // This header contails a define that modifies the way
13 // formatting strings work for the mingw platforms.
14 #include <sal/types.h>
23 #include <unotools/streamwrap.hxx>
24 #include <unotools/ucbstreamhelper.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XInitialization.hpp>
30 #include <com/sun/star/registry/XSimpleRegistry.hpp>
31 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
33 #include <tools/urlobj.hxx>
34 #include <vcl/vclmain.hxx>
36 #include <osl/file.hxx>
37 #include <osl/process.h>
38 #include <rtl/bootstrap.hxx>
39 #include <vcl/svapp.hxx>
41 #include <svx/galtheme.hxx>
42 #include <svx/gallery1.hxx>
44 using namespace ::com::sun::star
;
46 class GalApp
: public Application
51 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
54 virtual int Main() SAL_OVERRIDE
;
57 uno::Reference
<lang::XMultiServiceFactory
> xMSF
;
58 void Init() SAL_OVERRIDE
;
59 void DeInit() SAL_OVERRIDE
;
62 Gallery
* createGallery( const OUString
& rURL
)
64 return new Gallery( rURL
);
67 void disposeGallery( Gallery
* pGallery
)
72 static void createTheme( const OUString
& aThemeName
, const OUString
& aGalleryURL
,
73 const OUString
& aDestDir
, std::vector
<INetURLObject
> &rFiles
,
78 pGallery
= createGallery( aGalleryURL
);
80 fprintf( stderr
, "Could't create '%s'\n",
81 OUStringToOString( aGalleryURL
, RTL_TEXTENCODING_UTF8
).getStr() );
84 fprintf( stderr
, "Work on gallery '%s'\n",
85 OUStringToOString( aGalleryURL
, RTL_TEXTENCODING_UTF8
).getStr() );
87 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
88 pGallery
->GetThemeCount() );
90 GalleryTheme
*pGalTheme
;
91 if( !pGallery
->HasTheme( aThemeName
) ) {
92 if( !pGallery
->CreateTheme( aThemeName
) ) {
93 fprintf( stderr
, "Failed to create theme\n" );
98 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
99 pGallery
->GetThemeCount() );
101 SfxListener aListener
;
103 if ( !( pGalTheme
= pGallery
->AcquireTheme( aThemeName
, aListener
) ) ) {
104 fprintf( stderr
, "Failed to acquire theme\n" );
108 fprintf( stderr
, "Using DestDir: %s\n",
109 OUStringToOString( aDestDir
, RTL_TEXTENCODING_UTF8
).getStr() );
110 pGalTheme
->SetDestDir( aDestDir
, bRelativeURLs
);
112 std::vector
<INetURLObject
>::const_iterator aIter
;
114 for( aIter
= rFiles
.begin(); aIter
!= rFiles
.end(); ++aIter
)
117 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
118 // Requires a load more components ...
122 if ( ! pGalTheme
->InsertURL( *aIter
) )
123 fprintf( stderr
, "Failed to import '%s'\n",
124 OUStringToOString( aIter
->GetMainURL(INetURLObject::NO_DECODE
), RTL_TEXTENCODING_UTF8
).getStr() );
126 fprintf( stderr
, "Imported file '%s' (%" SAL_PRI_SIZET
"u)\n",
127 OUStringToOString( aIter
->GetMainURL(INetURLObject::NO_DECODE
), RTL_TEXTENCODING_UTF8
).getStr(),
128 pGalTheme
->GetObjectCount() );
131 pGallery
->ReleaseTheme( pGalTheme
, aListener
);
133 disposeGallery( pGallery
);
136 static int PrintHelp()
138 fprintf( stdout
, "Utility to generate LibreOffice gallery files\n\n" );
140 fprintf( stdout
, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
141 fprintf( stdout
, " [ files ... ]\n\n" );
143 fprintf( stdout
, "options:\n");
144 fprintf( stdout
, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
146 fprintf( stdout
, " --path <dir>\t\tdefines directory where the gallery files are created\n");
147 fprintf( stdout
, "\t\t\tor updated.\n");
149 fprintf( stdout
, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
150 fprintf( stdout
, "\t\t\tstored in the gallery files. It is useful to create\n");
151 fprintf( stdout
, "\t\t\tRPM packages using the BuildRoot feature.\n");
153 fprintf( stdout
, " --relative-urls\t\tflags that after removing the destdir, the URL should be a path relative to the gallery folder in the install\n");
154 fprintf( stdout
, "\t\t\tprimarily used for internal gallery generation at compile time.\n");
155 fprintf( stdout
, "\t\t\ttheme files.\n");
156 fprintf( stdout
, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
157 fprintf( stdout
, "\t\t\tare required.\n");
158 // --build-tree not documented - only useful during the build ...
163 static INetURLObject
Smartify( const OUString
&rPath
)
166 aURL
.SetSmartURL( rPath
);
173 if( !mbInBuildTree
&& getenv( "OOO_INSTALL_PREFIX" ) == NULL
) {
174 OUString fileName
= GetAppFileName();
175 int lastSlash
= fileName
.lastIndexOf( '/' );
177 // Don't know which directory separators GetAppFileName() returns on Windows.
178 // Be safe and take into consideration they might be backslashes.
179 if( fileName
.lastIndexOf( '\\' ) > lastSlash
)
180 lastSlash
= fileName
.lastIndexOf( '\\' );
182 OUString baseBinDir
= fileName
.copy( 0, lastSlash
);
183 OUString installPrefix
= baseBinDir
+ "/../..";
185 OUString
envVar( "OOO_INSTALL_PREFIX");
186 osl_setEnvironment(envVar
.pData
, installPrefix
.pData
);
188 OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
190 uno::Reference
<uno::XComponentContext
> xComponentContext
191 = ::cppu::defaultBootstrap_InitialComponentContext();
192 xMSF
= uno::Reference
<lang::XMultiServiceFactory
>
193 ( xComponentContext
->getServiceManager(), uno::UNO_QUERY
);
196 fprintf( stderr
, "Failed to bootstrap\n" );
199 ::comphelper::setProcessServiceFactory( xMSF
);
201 // For backwards compatibility, in case some code still uses plain
202 // createInstance w/o args directly to obtain an instance:
203 com::sun::star::ucb::UniversalContentBroker::create(xComponentContext
);
204 } catch (const uno::Exception
&e
) {
205 fprintf( stderr
, "Bootstrap exception '%s'\n",
206 rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
211 std::vector
<OUString
> ReadResponseFile_Impl(OUString
const& rInput
)
213 osl::File
file(rInput
);
214 osl::FileBase::RC rc
= file
.open(osl_File_OpenFlag_Read
);
215 OString
const uInput(rtl::OUStringToOString(rInput
, RTL_TEXTENCODING_UTF8
));
216 if (osl::FileBase::E_None
!= rc
)
218 fprintf(stderr
, "error while opening response file: %s (%d)\n",
219 uInput
.getStr(), rc
);
223 std::vector
<OUString
> ret
;
229 rc
= file
.read(buf
, sizeof(buf
), size
);
230 if (osl::FileBase::E_None
!= rc
)
232 fprintf(stderr
, "error while reading response file: %s (%d)\n",
233 uInput
.getStr(), rc
);
238 for (sal_uInt64 i
= 0; i
< size
; ++i
)
240 if (static_cast<unsigned char>(buf
[i
]) >= 128)
242 fprintf(stderr
, "non-ASCII character in response file: %s\n",
253 ret
.push_back(b
.makeStringAndClear());
262 ret
.push_back(b
.makeStringAndClear());
267 ReadResponseFile(std::vector
<INetURLObject
> & rFiles
, OUString
const& rInput
)
269 std::vector
<OUString
> files(ReadResponseFile_Impl(rInput
));
270 for (size_t i
= 0; i
< files
.size(); ++i
)
272 rFiles
.push_back(Smartify(files
[i
]));
280 OUString aPath
, aDestDir
;
281 OUString
aName( "Default name" );
282 std::vector
<INetURLObject
> aFiles
;
284 for( sal_uInt32 i
= 0; i
< GetCommandLineParamCount(); i
++ )
286 OUString aParam
= GetCommandLineParam( i
);
288 if ( aParam
.startsWith( "-env:" ) )
290 else if ( aParam
== "--help" || aParam
== "-h" )
292 else if ( aParam
== "--build-tree" )
294 mbRelativeURLs
= true;
295 mbInBuildTree
= true;
297 else if ( aParam
== "--name" )
298 aName
= GetCommandLineParam( ++i
);
299 else if ( aParam
== "--path" )
300 aPath
= Smartify( GetCommandLineParam( ++i
) ).
301 GetMainURL(INetURLObject::NO_DECODE
);
302 else if ( aParam
== "--destdir" )
303 aDestDir
= GetCommandLineParam( ++i
);
304 else if ( aParam
== "--relative-urls" )
305 mbRelativeURLs
= true;
306 else if ( aParam
== "--number-from" )
307 fprintf ( stderr
, "--number-from is deprecated, themes now "
308 "have filenames based on their names\n" );
309 else if ( aParam
== "--filenames" )
310 ReadResponseFile(aFiles
, GetCommandLineParam(++i
));
312 aFiles
.push_back( Smartify( aParam
) );
318 createTheme( aName
, aPath
, aDestDir
, aFiles
, mbRelativeURLs
);
320 catch (const uno::Exception
& e
)
322 SAL_WARN("vcl.app", "Fatal exception: " << e
.Message
);
329 void GalApp::DeInit()
331 uno::Reference
< lang::XComponent
>(
332 comphelper::getProcessComponentContext(),
333 uno::UNO_QUERY_THROW
)-> dispose();
334 ::comphelper::setProcessServiceFactory( NULL
);
337 void vclmain::createApplication()
339 Application::EnableConsoleOnly();
340 static GalApp aGalApp
;
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */