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/.
17 #include <unotools/streamwrap.hxx>
18 #include <unotools/ucbstreamhelper.hxx>
20 #include <comphelper/processfactory.hxx>
21 #include <cppuhelper/bootstrap.hxx>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/registry/XSimpleRegistry.hpp>
25 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
27 #include <tools/urlobj.hxx>
28 #include <vcl/vclmain.hxx>
30 #include <osl/file.hxx>
31 #include <osl/process.h>
32 #include <rtl/bootstrap.hxx>
33 #include <vcl/svapp.hxx>
35 #include <svx/galtheme.hxx>
36 #include <svx/gallery1.hxx>
38 using namespace ::com::sun::star
;
40 typedef ::std::list
<OUString
> FileNameList
;
42 class GalApp
: public Application
47 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
53 uno::Reference
<lang::XMultiServiceFactory
> xMSF
;
58 Gallery
* createGallery( const OUString
& rURL
)
60 return new Gallery( rURL
);
63 void disposeGallery( Gallery
* pGallery
)
68 static void createTheme( OUString aThemeName
, OUString aGalleryURL
,
69 OUString aDestDir
, FileNameList
&rFiles
,
74 pGallery
= createGallery( aGalleryURL
);
76 fprintf( stderr
, "Could't create '%s'\n",
77 OUStringToOString( aGalleryURL
, RTL_TEXTENCODING_UTF8
).getStr() );
80 fprintf( stderr
, "Work on gallery '%s'\n",
81 OUStringToOString( aGalleryURL
, RTL_TEXTENCODING_UTF8
).getStr() );
83 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
84 pGallery
->GetThemeCount() );
86 GalleryTheme
*pGalTheme
;
87 if( !pGallery
->HasTheme( aThemeName
) ) {
88 if( !pGallery
->CreateTheme( aThemeName
) ) {
89 fprintf( stderr
, "Failed to create theme\n" );
94 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
95 pGallery
->GetThemeCount() );
97 SfxListener aListener
;
99 if ( !( pGalTheme
= pGallery
->AcquireTheme( aThemeName
, aListener
) ) ) {
100 fprintf( stderr
, "Failed to acquire theme\n" );
104 fprintf( stderr
, "Using DestDir: %s\n",
105 OUStringToOString( aDestDir
, RTL_TEXTENCODING_UTF8
).getStr() );
106 pGalTheme
->SetDestDir( String( aDestDir
), bRelativeURLs
);
108 FileNameList::const_iterator aIter
;
110 for( aIter
= rFiles
.begin(); aIter
!= rFiles
.end(); ++aIter
)
113 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
114 // Requires a load more components ...
118 if ( ! pGalTheme
->InsertURL( *aIter
) )
119 fprintf( stderr
, "Failed to import '%s'\n",
120 OUStringToOString( *aIter
, RTL_TEXTENCODING_UTF8
).getStr() );
122 fprintf( stderr
, "Imported file '%s' (%" SAL_PRI_SIZET
"u)\n",
123 OUStringToOString( *aIter
, RTL_TEXTENCODING_UTF8
).getStr(),
124 pGalTheme
->GetObjectCount() );
127 pGallery
->ReleaseTheme( pGalTheme
, aListener
);
129 disposeGallery( pGallery
);
132 static int PrintHelp()
134 fprintf( stdout
, "Utility to generate LibreOffice gallery files\n\n" );
136 fprintf( stdout
, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
137 fprintf( stdout
, " [ files ... ]\n\n" );
139 fprintf( stdout
, "options:\n");
140 fprintf( stdout
, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
142 fprintf( stdout
, " --path <dir>\t\tdefines directory where the gallery files are created\n");
143 fprintf( stdout
, "\t\t\tor updated.\n");
145 fprintf( stdout
, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
146 fprintf( stdout
, "\t\t\tstored in the gallery files. It is useful to create\n");
147 fprintf( stdout
, "\t\t\tRPM packages using the BuildRoot feature.\n");
149 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");
150 fprintf( stdout
, "\t\t\tprimarily used for internal gallery generation at compile time.\n");
151 fprintf( stdout
, "\t\t\ttheme files.\n");
152 fprintf( stdout
, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
153 fprintf( stdout
, "\t\t\tare required.\n");
154 // --build-tree not documented - only useful during the build ...
159 static OUString
Smartify( const OUString
&rPath
)
162 aURL
.SetSmartURL( rPath
);
163 return aURL
.GetMainURL( INetURLObject::NO_DECODE
);
169 if( !mbInBuildTree
&& getenv( "OOO_INSTALL_PREFIX" ) == NULL
) {
170 OUString fileName
= GetAppFileName();
171 int lastSlash
= fileName
.lastIndexOf( '/' );
173 // Don't know which directory separators GetAppFileName() returns on Windows.
174 // Be safe and take into consideration they might be backslashes.
175 if( fileName
.lastIndexOf( '\\' ) > lastSlash
)
176 lastSlash
= fileName
.lastIndexOf( '\\' );
178 OUString baseBinDir
= fileName
.copy( 0, lastSlash
);
179 OUString installPrefix
= baseBinDir
+ OUString::createFromAscii( "/../.." );
181 OUString
envVar( "OOO_INSTALL_PREFIX");
182 osl_setEnvironment(envVar
.pData
, installPrefix
.pData
);
184 OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
186 uno::Reference
<uno::XComponentContext
> xComponentContext
187 = ::cppu::defaultBootstrap_InitialComponentContext();
188 xMSF
= uno::Reference
<lang::XMultiServiceFactory
>
189 ( xComponentContext
->getServiceManager(), uno::UNO_QUERY
);
192 fprintf( stderr
, "Failed to bootstrap\n" );
195 ::comphelper::setProcessServiceFactory( xMSF
);
197 // For backwards compatibility, in case some code still uses plain
198 // createInstance w/o args directly to obtain an instance:
199 com::sun::star::ucb::UniversalContentBroker::create(xComponentContext
);
200 } catch (const uno::Exception
&e
) {
201 fprintf( stderr
, "Bootstrap exception '%s'\n",
202 rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_UTF8
).getStr() );
209 OUString aPath
, aDestDir
;
210 OUString
aName( "Default name" );
213 for( sal_uInt32 i
= 0; i
< GetCommandLineParamCount(); i
++ )
215 OUString aParam
= GetCommandLineParam( i
);
217 if ( aParam
.startsWith( "-env:" ) )
219 else if ( aParam
== "--help" || aParam
== "-h" )
221 else if ( aParam
== "--build-tree" )
223 mbRelativeURLs
= true;
224 mbInBuildTree
= true;
226 else if ( aParam
== "--name" )
227 aName
= GetCommandLineParam( ++i
);
228 else if ( aParam
== "--path" )
229 aPath
= Smartify( GetCommandLineParam( ++i
) );
230 else if ( aParam
== "--destdir" )
231 aDestDir
= GetCommandLineParam( ++i
);
232 else if ( aParam
== "--relative-urls" )
233 mbRelativeURLs
= true;
234 else if ( aParam
== "--number-from" )
235 fprintf ( stderr
, "--number-from is deprecated, themes now "
236 "have filenames based on their names\n" );
238 aFiles
.push_back( Smartify( aParam
) );
241 if( aFiles
.size() < 1 )
244 createTheme( aName
, aPath
, aDestDir
, aFiles
, mbRelativeURLs
);
249 void GalApp::DeInit()
251 uno::Reference
< lang::XComponent
>(
252 comphelper::getProcessComponentContext(),
253 uno::UNO_QUERY_THROW
)-> dispose();
254 ::comphelper::setProcessServiceFactory( NULL
);
257 void vclmain::createApplication()
259 Application::EnableConsoleOnly();
260 static GalApp aGalApp
;
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */