Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / gengal / gengal.cxx
blob547fb4f969be22ac0e8a8780b7257419b5ff7a09
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
16 #include <stdio.h>
17 #ifndef _WIN32
18 #include <unistd.h>
19 #endif
21 #include <vector>
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
48 bool mbInBuildTree;
49 bool mbRelativeURLs;
50 public:
51 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
54 virtual int Main() SAL_OVERRIDE;
56 protected:
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 )
69 delete pGallery;
72 static void createTheme( const OUString& aThemeName, const OUString& aGalleryURL,
73 const OUString& aDestDir, std::vector<INetURLObject> &rFiles,
74 bool bRelativeURLs )
76 Gallery* pGallery;
78 pGallery = createGallery( aGalleryURL );
79 if (!pGallery ) {
80 fprintf( stderr, "Could't create '%s'\n",
81 OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
82 exit( 1 );
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" );
94 exit( 1 );
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" );
105 exit( 1 );
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 )
116 // Should/could use:
117 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
118 // Requires a load more components ...
120 Graphic aGraphic;
122 if ( ! pGalTheme->InsertURL( *aIter ) )
123 fprintf( stderr, "Failed to import '%s'\n",
124 OUStringToOString( aIter->GetMainURL(INetURLObject::NO_DECODE), RTL_TEXTENCODING_UTF8 ).getStr() );
125 else
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 ...
160 return EXIT_SUCCESS;
163 static INetURLObject Smartify( const OUString &rPath )
165 INetURLObject aURL;
166 aURL.SetSmartURL( rPath );
167 return aURL;
170 void GalApp::Init()
172 try {
173 if( !mbInBuildTree && getenv( "OOO_INSTALL_PREFIX" ) == NULL ) {
174 OUString fileName = GetAppFileName();
175 int lastSlash = fileName.lastIndexOf( '/' );
176 #ifdef WNT
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( '\\' );
181 #endif
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 );
194 if( !xMSF.is() )
196 fprintf( stderr, "Failed to bootstrap\n" );
197 exit( 1 );
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() );
207 exit( 1 );
211 int GalApp::Main()
213 OUString aPath, aDestDir;
214 OUString aName( "Default name" );
215 std::vector<INetURLObject> aFiles;
217 for( sal_uInt32 i = 0; i < GetCommandLineParamCount(); i++ )
219 OUString aParam = GetCommandLineParam( i );
221 if ( aParam.startsWith( "-env:" ) )
222 continue;
223 else if ( aParam == "--help" || aParam == "-h" )
224 return PrintHelp();
225 else if ( aParam == "--build-tree" )
227 mbRelativeURLs = true;
228 mbInBuildTree = true;
230 else if ( aParam == "--name" )
231 aName = GetCommandLineParam( ++i );
232 else if ( aParam == "--path" )
233 aPath = Smartify( GetCommandLineParam( ++i ) ).
234 GetMainURL(INetURLObject::NO_DECODE);
235 else if ( aParam == "--destdir" )
236 aDestDir = GetCommandLineParam( ++i );
237 else if ( aParam == "--relative-urls" )
238 mbRelativeURLs = true;
239 else if ( aParam == "--number-from" )
240 fprintf ( stderr, "--number-from is deprecated, themes now "
241 "have filenames based on their names\n" );
242 else
243 aFiles.push_back( Smartify( aParam ) );
246 if( aFiles.empty() )
247 return PrintHelp();
249 createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
251 return EXIT_SUCCESS;
254 void GalApp::DeInit()
256 uno::Reference< lang::XComponent >(
257 comphelper::getProcessComponentContext(),
258 uno::UNO_QUERY_THROW )-> dispose();
259 ::comphelper::setProcessServiceFactory( NULL );
262 void vclmain::createApplication()
264 Application::EnableConsoleOnly();
265 static GalApp aGalApp;
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */