Bump version to 6.4-15
[LibreOffice.git] / svx / source / gengal / gengal.cxx
blob99e7f4820860bba027c72c7d2d9994694b928077
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 */
10 #include <sal/config.h>
11 #include <sal/log.hxx>
13 #include <stdio.h>
14 #ifndef _WIN32
15 #include <unistd.h>
16 #endif
18 #include <vector>
20 #include <unotools/streamwrap.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <cppuhelper/bootstrap.hxx>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/lang/XInitialization.hpp>
26 #include <com/sun/star/registry/XSimpleRegistry.hpp>
27 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
29 #include <tools/urlobj.hxx>
30 #include <vcl/vclmain.hxx>
32 #include <osl/file.hxx>
33 #include <osl/process.h>
34 #include <rtl/bootstrap.hxx>
35 #include <sfx2/app.hxx>
36 #include <sal/types.h>
37 #include <tools/diagnose_ex.h>
38 #include <vcl/svapp.hxx>
40 #include <svx/galtheme.hxx>
41 #include <svx/gallery1.hxx>
43 using namespace ::com::sun::star;
45 class GalApp : public Application
47 bool mbInBuildTree;
48 bool mbRelativeURLs;
49 public:
50 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
53 virtual int Main() override;
55 protected:
56 uno::Reference<lang::XMultiServiceFactory> xMSF;
57 void Init() override;
58 void DeInit() override;
61 static void createTheme( const OUString& aThemeName, const OUString& aGalleryURL,
62 const OUString& aDestDir, std::vector<INetURLObject> &rFiles,
63 bool bRelativeURLs )
65 std::unique_ptr<Gallery> pGallery(new Gallery( aGalleryURL ));
67 fprintf( stderr, "Work on gallery '%s'\n",
68 OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
70 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
71 pGallery->GetThemeCount() );
73 GalleryTheme *pGalTheme;
74 if( !pGallery->HasTheme( aThemeName) ) {
75 if( !pGallery->CreateTheme( aThemeName ) ) {
76 fprintf( stderr, "Failed to create theme\n" );
77 exit( 1 );
81 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
82 pGallery->GetThemeCount() );
84 SfxListener aListener;
86 pGalTheme = pGallery->AcquireTheme( aThemeName, aListener );
87 if ( !pGalTheme ) {
88 fprintf( stderr, "Failed to acquire theme\n" );
89 exit( 1 );
92 fprintf( stderr, "Using DestDir: %s\n",
93 OUStringToOString( aDestDir, RTL_TEXTENCODING_UTF8 ).getStr() );
94 pGalTheme->SetDestDir( aDestDir, bRelativeURLs );
96 for( const auto& rFile : rFiles )
98 // Should/could use:
99 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
100 // Requires a load more components ...
102 Graphic aGraphic;
104 if ( ! pGalTheme->InsertURL( rFile ) )
105 fprintf( stderr, "Failed to import '%s'\n",
106 OUStringToOString( rFile.GetMainURL(INetURLObject::DecodeMechanism::NONE), RTL_TEXTENCODING_UTF8 ).getStr() );
107 else
108 fprintf( stderr, "Imported file '%s' (%" SAL_PRIuUINT32 ")\n",
109 OUStringToOString( rFile.GetMainURL(INetURLObject::DecodeMechanism::NONE), RTL_TEXTENCODING_UTF8 ).getStr(),
110 pGalTheme->GetObjectCount() );
113 pGallery->ReleaseTheme( pGalTheme, aListener );
116 static int PrintHelp()
118 fprintf( stdout, "Utility to generate LibreOffice gallery files\n\n" );
120 fprintf( stdout, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
121 fprintf( stdout, " [ files ... ]\n\n" );
123 fprintf( stdout, "options:\n");
124 fprintf( stdout, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
126 fprintf( stdout, " --path <dir>\t\tdefines directory where the gallery files are created\n");
127 fprintf( stdout, "\t\t\tor updated.\n");
129 fprintf( stdout, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
130 fprintf( stdout, "\t\t\tstored in the gallery files. It is useful to create\n");
131 fprintf( stdout, "\t\t\tRPM packages using the BuildRoot feature.\n");
133 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");
134 fprintf( stdout, "\t\t\tprimarily used for internal gallery generation at compile time.\n");
135 fprintf( stdout, "\t\t\ttheme files.\n");
136 fprintf( stdout, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
137 fprintf( stdout, "\t\t\tare required.\n");
138 // --build-tree not documented - only useful during the build ...
140 return EXIT_SUCCESS;
143 static INetURLObject Smartify( const OUString &rPath )
145 INetURLObject aURL;
146 aURL.SetSmartURL( rPath );
147 return aURL;
150 void GalApp::Init()
152 try {
153 if( !mbInBuildTree && getenv( "OOO_INSTALL_PREFIX" ) == nullptr ) {
154 OUString fileName = GetAppFileName();
155 int lastSlash = fileName.lastIndexOf( '/' );
156 #ifdef _WIN32
157 // Don't know which directory separators GetAppFileName() returns on Windows.
158 // Be safe and take into consideration they might be backslashes.
159 if( fileName.lastIndexOf( '\\' ) > lastSlash )
160 lastSlash = fileName.lastIndexOf( '\\' );
161 #endif
162 OUString baseBinDir = fileName.copy( 0, lastSlash );
163 OUString installPrefix = baseBinDir + "/../..";
165 OUString envVar( "OOO_INSTALL_PREFIX");
166 osl_setEnvironment(envVar.pData, installPrefix.pData);
168 SAL_INFO("svx", "OOO_INSTALL_PREFIX=" << getenv( "OOO_INSTALL_PREFIX" ) );
170 uno::Reference<uno::XComponentContext> xComponentContext
171 = ::cppu::defaultBootstrap_InitialComponentContext();
172 xMSF.set( xComponentContext->getServiceManager(), uno::UNO_QUERY );
173 if( !xMSF.is() )
175 fprintf( stderr, "Failed to bootstrap\n" );
176 exit( 1 );
178 ::comphelper::setProcessServiceFactory( xMSF );
180 // For backwards compatibility, in case some code still uses plain
181 // createInstance w/o args directly to obtain an instance:
182 css::ucb::UniversalContentBroker::create(xComponentContext);
183 } catch (const uno::Exception &e) {
184 fprintf( stderr, "Bootstrap exception '%s'\n",
185 OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
186 exit( 1 );
190 static std::vector<OUString> ReadResponseFile_Impl(OUString const& rInput)
192 osl::File file(rInput);
193 osl::FileBase::RC rc = file.open(osl_File_OpenFlag_Read);
194 OString const uInput(OUStringToOString(rInput, RTL_TEXTENCODING_UTF8));
195 if (osl::FileBase::E_None != rc)
197 fprintf(stderr, "error while opening response file: %s (%d)\n",
198 uInput.getStr(), rc);
199 exit(1);
202 std::vector<OUString> ret;
203 OUStringBuffer b(256);
204 char buf[1<<16];
205 while (true)
207 sal_uInt64 size(0);
208 rc = file.read(buf, sizeof(buf), size);
209 if (osl::FileBase::E_None != rc)
211 fprintf(stderr, "error while reading response file: %s (%d)\n",
212 uInput.getStr(), rc);
213 exit(1);
215 if (!size)
216 break;
217 for (sal_uInt64 i = 0; i < size; ++i)
219 if (static_cast<unsigned char>(buf[i]) >= 128)
221 fprintf(stderr, "non-ASCII character in response file: %s\n",
222 uInput.getStr());
223 exit(1);
225 switch (buf[i])
227 case ' ' :
228 case '\t':
229 case '\r':
230 case '\n':
231 if (!b.isEmpty())
232 ret.push_back(b.makeStringAndClear());
233 break;
234 default:
235 b.append(buf[i]);
236 break;
240 if (!b.isEmpty())
241 ret.push_back(b.makeStringAndClear());
242 return ret;
245 static void
246 ReadResponseFile(std::vector<INetURLObject> & rFiles, OUString const& rInput)
248 std::vector<OUString> files(ReadResponseFile_Impl(rInput));
249 for (size_t i = 0; i < files.size(); ++i)
251 rFiles.push_back(Smartify(files[i]));
255 int GalApp::Main()
259 SfxApplication::GetOrCreate();
261 OUString aPath, aDestDir;
262 OUString aName( "Default name" );
263 std::vector<INetURLObject> aFiles;
265 for( sal_uInt16 i = 0; i < GetCommandLineParamCount(); ++i )
267 OUString aParam = GetCommandLineParam( i );
269 if ( aParam.startsWith( "-env:" ) )
270 continue;
271 else if ( aParam == "--help" || aParam == "-h" )
272 return PrintHelp();
273 else if ( aParam == "--build-tree" )
275 mbRelativeURLs = true;
276 mbInBuildTree = true;
278 else if ( aParam == "--name" )
279 aName = GetCommandLineParam( ++i );
280 else if ( aParam == "--path" )
281 aPath = Smartify( GetCommandLineParam( ++i ) ).
282 GetMainURL(INetURLObject::DecodeMechanism::NONE);
283 else if ( aParam == "--destdir" )
284 aDestDir = GetCommandLineParam( ++i );
285 else if ( aParam == "--relative-urls" )
286 mbRelativeURLs = true;
287 else if ( aParam == "--number-from" )
288 fprintf ( stderr, "--number-from is deprecated, themes now "
289 "have filenames based on their names\n" );
290 else if ( aParam == "--filenames" )
291 ReadResponseFile(aFiles, GetCommandLineParam(++i));
292 else
293 aFiles.push_back( Smartify( aParam ) );
296 if( aFiles.empty() )
297 return PrintHelp();
299 createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
301 catch (const uno::Exception&)
303 TOOLS_WARN_EXCEPTION("svx", "Fatal");
304 return EXIT_FAILURE;
306 catch (const std::exception &e)
308 SAL_WARN("svx", "Fatal: " << e.what());
309 return 1;
312 return EXIT_SUCCESS;
315 void GalApp::DeInit()
317 uno::Reference< lang::XComponent >(
318 comphelper::getProcessComponentContext(),
319 uno::UNO_QUERY_THROW )-> dispose();
320 ::comphelper::setProcessServiceFactory( nullptr );
323 void vclmain::createApplication()
325 Application::EnableConsoleOnly();
326 static GalApp aGalApp;
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */