bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / gengal / gengal.cxx
blobea4e6188d9a401f4e3b5af32310cb567e25dbef9
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 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);
220 exit(1);
223 std::vector<OUString> ret;
224 OUStringBuffer b;
225 char buf[1<<16];
226 while (true)
228 sal_uInt64 size(0);
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);
234 exit(1);
236 if (!size)
237 break;
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",
243 uInput.getStr());
244 exit(1);
246 switch (buf[i])
248 case ' ' :
249 case '\t':
250 case '\r':
251 case '\n':
252 if (!b.isEmpty())
253 ret.push_back(b.makeStringAndClear());
254 break;
255 default:
256 b.append(buf[i]);
257 break;
261 if (!b.isEmpty())
262 ret.push_back(b.makeStringAndClear());
263 return ret;
266 void
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]));
276 int GalApp::Main()
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:" ) )
289 continue;
290 else if ( aParam == "--help" || aParam == "-h" )
291 return PrintHelp();
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));
311 else
312 aFiles.push_back( Smartify( aParam ) );
315 if( aFiles.empty() )
316 return PrintHelp();
318 createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
320 catch (const uno::Exception& e)
322 SAL_WARN("vcl.app", "Fatal exception: " << e.Message);
323 return EXIT_FAILURE;
326 return EXIT_SUCCESS;
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: */