bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / gengal / gengal.cxx
blobd57174ff89131904a103a8002805e4974b2c6374
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>
15 #include <vector>
17 #include <comphelper/processfactory.hxx>
18 #include <cppuhelper/bootstrap.hxx>
19 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
20 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
21 #include <com/sun/star/frame/Desktop.hpp>
23 #include <tools/urlobj.hxx>
24 #include <vcl/vclmain.hxx>
26 #include <osl/file.hxx>
27 #include <osl/process.h>
28 #include <sfx2/app.hxx>
29 #include <sal/types.h>
30 #include <tools/diagnose_ex.h>
31 #include <vcl/svapp.hxx>
33 #include <svx/galtheme.hxx>
34 #include <svx/gallery1.hxx>
36 using namespace ::com::sun::star;
38 namespace {
40 class GalApp : public Application
42 bool mbInBuildTree;
43 bool mbRelativeURLs;
44 public:
45 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
48 virtual int Main() override;
50 protected:
51 uno::Reference<lang::XMultiServiceFactory> xMSF;
52 void Init() override;
53 void DeInit() override;
58 static void createTheme( const OUString& aThemeName, const OUString& aGalleryURL,
59 const OUString& aDestDir, std::vector<INetURLObject> &rFiles,
60 bool bRelativeURLs )
62 std::unique_ptr<Gallery> pGallery(new Gallery( aGalleryURL ));
64 fprintf( stderr, "Work on gallery '%s'\n",
65 OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
67 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
68 pGallery->GetThemeCount() );
70 GalleryTheme *pGalTheme;
71 if( !pGallery->HasTheme( aThemeName) ) {
72 if( !pGallery->CreateTheme( aThemeName ) ) {
73 fprintf( stderr, "Failed to create theme\n" );
74 exit( 1 );
78 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
79 pGallery->GetThemeCount() );
81 SfxListener aListener;
83 pGalTheme = pGallery->AcquireTheme( aThemeName, aListener );
84 if ( !pGalTheme ) {
85 fprintf( stderr, "Failed to acquire theme\n" );
86 exit( 1 );
89 fprintf( stderr, "Using DestDir: %s\n",
90 OUStringToOString( aDestDir, RTL_TEXTENCODING_UTF8 ).getStr() );
91 pGalTheme->SetDestDir( aDestDir, bRelativeURLs );
93 for( const auto& rFile : rFiles )
95 // Should/could use:
96 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
97 // Requires a load more components ...
99 if ( ! pGalTheme->InsertURL( rFile ) )
100 fprintf( stderr, "Failed to import '%s'\n",
101 OUStringToOString( rFile.GetMainURL(INetURLObject::DecodeMechanism::NONE), RTL_TEXTENCODING_UTF8 ).getStr() );
102 else
103 fprintf( stderr, "Imported file '%s' (%" SAL_PRIuUINT32 ")\n",
104 OUStringToOString( rFile.GetMainURL(INetURLObject::DecodeMechanism::NONE), RTL_TEXTENCODING_UTF8 ).getStr(),
105 pGalTheme->GetObjectCount() );
108 pGallery->ReleaseTheme( pGalTheme, aListener );
111 static int PrintHelp()
113 fprintf( stdout, "Utility to generate LibreOffice gallery files\n\n" );
115 fprintf( stdout, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
116 fprintf( stdout, " [ files ... ]\n\n" );
118 fprintf( stdout, "options:\n");
119 fprintf( stdout, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
121 fprintf( stdout, " --path <dir>\t\tdefines directory where the gallery files are created\n");
122 fprintf( stdout, "\t\t\tor updated.\n");
124 fprintf( stdout, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
125 fprintf( stdout, "\t\t\tstored in the gallery files. It is useful to create\n");
126 fprintf( stdout, "\t\t\tRPM packages using the BuildRoot feature.\n");
128 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");
129 fprintf( stdout, "\t\t\tprimarily used for internal gallery generation at compile time.\n");
130 fprintf( stdout, "\t\t\ttheme files.\n");
131 fprintf( stdout, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
132 fprintf( stdout, "\t\t\tare required.\n");
133 // --build-tree not documented - only useful during the build ...
135 return EXIT_SUCCESS;
138 static INetURLObject Smartify( const OUString &rPath )
140 INetURLObject aURL;
141 aURL.SetSmartURL( rPath );
142 return aURL;
145 void GalApp::Init()
147 try {
148 if( !mbInBuildTree && getenv( "OOO_INSTALL_PREFIX" ) == nullptr ) {
149 OUString fileName = GetAppFileName();
150 int lastSlash = fileName.lastIndexOf( '/' );
151 #ifdef _WIN32
152 // Don't know which directory separators GetAppFileName() returns on Windows.
153 // Be safe and take into consideration they might be backslashes.
154 if( fileName.lastIndexOf( '\\' ) > lastSlash )
155 lastSlash = fileName.lastIndexOf( '\\' );
156 #endif
157 OUString baseBinDir = fileName.copy( 0, lastSlash );
158 OUString installPrefix = baseBinDir + "/../..";
160 OUString envVar( "OOO_INSTALL_PREFIX");
161 osl_setEnvironment(envVar.pData, installPrefix.pData);
163 SAL_INFO("svx", "OOO_INSTALL_PREFIX=" << getenv( "OOO_INSTALL_PREFIX" ) );
165 uno::Reference<uno::XComponentContext> xComponentContext
166 = ::cppu::defaultBootstrap_InitialComponentContext();
167 xMSF.set( xComponentContext->getServiceManager(), uno::UNO_QUERY );
168 if( !xMSF.is() )
170 fprintf( stderr, "Failed to bootstrap\n" );
171 exit( 1 );
173 ::comphelper::setProcessServiceFactory( xMSF );
175 // For backwards compatibility, in case some code still uses plain
176 // createInstance w/o args directly to obtain an instance:
177 css::ucb::UniversalContentBroker::create(xComponentContext);
178 } catch (const uno::Exception &e) {
179 fprintf( stderr, "Bootstrap exception '%s'\n",
180 OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
181 exit( 1 );
185 static std::vector<OUString> ReadResponseFile_Impl(OUString const& rInput)
187 osl::File file(rInput);
188 osl::FileBase::RC rc = file.open(osl_File_OpenFlag_Read);
189 OString const uInput(OUStringToOString(rInput, RTL_TEXTENCODING_UTF8));
190 if (osl::FileBase::E_None != rc)
192 fprintf(stderr, "error while opening response file: %s (%d)\n",
193 uInput.getStr(), rc);
194 exit(1);
197 std::vector<OUString> ret;
198 OUStringBuffer b(256);
199 char buf[1<<16];
200 while (true)
202 sal_uInt64 size(0);
203 rc = file.read(buf, sizeof(buf), size);
204 if (osl::FileBase::E_None != rc)
206 fprintf(stderr, "error while reading response file: %s (%d)\n",
207 uInput.getStr(), rc);
208 exit(1);
210 if (!size)
211 break;
212 for (sal_uInt64 i = 0; i < size; ++i)
214 if (static_cast<unsigned char>(buf[i]) >= 128)
216 fprintf(stderr, "non-ASCII character in response file: %s\n",
217 uInput.getStr());
218 exit(1);
220 switch (buf[i])
222 case ' ' :
223 case '\t':
224 case '\r':
225 case '\n':
226 if (!b.isEmpty())
227 ret.push_back(b.makeStringAndClear());
228 break;
229 default:
230 b.append(buf[i]);
231 break;
235 if (!b.isEmpty())
236 ret.push_back(b.makeStringAndClear());
237 return ret;
240 static void
241 ReadResponseFile(std::vector<INetURLObject> & rFiles, OUString const& rInput)
243 std::vector<OUString> files(ReadResponseFile_Impl(rInput));
244 for (size_t i = 0; i < files.size(); ++i)
246 rFiles.push_back(Smartify(files[i]));
250 int GalApp::Main()
254 SfxApplication::GetOrCreate();
256 OUString aPath, aDestDir;
257 OUString aName( "Default name" );
258 std::vector<INetURLObject> aFiles;
260 for( sal_uInt16 i = 0; i < GetCommandLineParamCount(); ++i )
262 OUString aParam = GetCommandLineParam( i );
264 if ( aParam.startsWith( "-env:" ) )
265 continue;
266 else if ( aParam == "--help" || aParam == "-h" )
267 return PrintHelp();
268 else if ( aParam == "--build-tree" )
270 mbRelativeURLs = true;
271 mbInBuildTree = true;
273 else if ( aParam == "--name" )
274 aName = GetCommandLineParam( ++i );
275 else if ( aParam == "--path" )
276 aPath = Smartify( GetCommandLineParam( ++i ) ).
277 GetMainURL(INetURLObject::DecodeMechanism::NONE);
278 else if ( aParam == "--destdir" )
279 aDestDir = GetCommandLineParam( ++i );
280 else if ( aParam == "--relative-urls" )
281 mbRelativeURLs = true;
282 else if ( aParam == "--number-from" )
283 fprintf ( stderr, "--number-from is deprecated, themes now "
284 "have filenames based on their names\n" );
285 else if ( aParam == "--filenames" )
286 ReadResponseFile(aFiles, GetCommandLineParam(++i));
287 else
288 aFiles.push_back( Smartify( aParam ) );
291 if( aFiles.empty() )
292 return PrintHelp();
294 createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
296 catch (const uno::Exception&)
298 TOOLS_WARN_EXCEPTION("svx", "Fatal");
299 return EXIT_FAILURE;
301 catch (const std::exception &e)
303 SAL_WARN("svx", "Fatal: " << e.what());
304 return 1;
307 return EXIT_SUCCESS;
310 void GalApp::DeInit()
312 auto xDesktop = css::frame::Desktop::create(comphelper::getProcessComponentContext());
313 xDesktop->terminate();
314 uno::Reference< lang::XComponent >(
315 comphelper::getProcessComponentContext(),
316 uno::UNO_QUERY_THROW )-> dispose();
317 ::comphelper::setProcessServiceFactory( nullptr );
320 void vclmain::createApplication()
322 Application::EnableConsoleOnly();
323 static GalApp aGalApp;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */