LibreOffice Module svx (master) 1
gengal.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#include <sal/config.h>
11#include <sal/log.hxx>
12
13#include <stdio.h>
14
15#include <vector>
16
19#include <com/sun/star/lang/XMultiServiceFactory.hpp>
20#include <com/sun/star/ucb/UniversalContentBroker.hpp>
21#include <com/sun/star/frame/Desktop.hpp>
22
23#include <tools/urlobj.hxx>
24#include <vcl/vclmain.hxx>
25
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>
32
33#include <svx/galtheme.hxx>
34#include <svx/gallery1.hxx>
35
36using namespace ::com::sun::star;
37
38namespace {
39
40class GalApp : public Application
41{
42 bool mbInBuildTree;
43 bool mbRelativeURLs;
44public:
45 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
46 {
47 }
48 virtual int Main() override;
49
50protected:
51 uno::Reference<lang::XMultiServiceFactory> xMSF;
52 void Init() override;
53 void DeInit() override;
54};
55
56}
57
58static void createTheme( const OUString& aThemeName, std::u16string_view aGalleryURL,
59 const OUString& aDestDir, std::vector<INetURLObject> &rFiles,
60 bool bRelativeURLs )
61{
62 std::unique_ptr<Gallery> pGallery(new Gallery( aGalleryURL ));
63
64 fprintf( stderr, "Work on gallery '%s'\n",
65 OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
66
67 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
68 pGallery->GetThemeCount() );
69
70 GalleryTheme *pGalTheme;
71 if( !pGallery->HasTheme( aThemeName) ) {
72 if( !pGallery->CreateTheme( aThemeName ) ) {
73 fprintf( stderr, "Failed to create theme\n" );
74 exit( 1 );
75 }
76 }
77
78 fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
79 pGallery->GetThemeCount() );
80
81 SfxListener aListener;
82
83 pGalTheme = pGallery->AcquireTheme( aThemeName, aListener );
84 if ( !pGalTheme ) {
85 fprintf( stderr, "Failed to acquire theme\n" );
86 exit( 1 );
87 }
88
89 fprintf( stderr, "Using DestDir: %s\n",
90 OUStringToOString( aDestDir, RTL_TEXTENCODING_UTF8 ).getStr() );
91 pGalTheme->SetDestDir( aDestDir, bRelativeURLs );
92
93 for( const auto& rFile : rFiles )
94 {
95// Should/could use:
96// if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
97// Requires a load more components ...
98
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() );
106 }
107
108 pGallery->ReleaseTheme( pGalTheme, aListener );
109}
110
111static int PrintHelp()
112{
113 fprintf( stdout, "Utility to generate LibreOffice gallery files\n\n" );
114
115 fprintf( stdout, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
116 fprintf( stdout, " [ files ... ]\n\n" );
117
118 fprintf( stdout, "options:\n");
119 fprintf( stdout, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
120
121 fprintf( stdout, " --path <dir>\t\tdefines directory where the gallery files are created\n");
122 fprintf( stdout, "\t\t\tor updated.\n");
123
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");
127
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 ...
134
135 return EXIT_SUCCESS;
136}
137
138static INetURLObject Smartify( std::u16string_view rPath )
139{
141 aURL.SetSmartURL( rPath );
142 return aURL;
143}
144
145void GalApp::Init()
146{
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 + "/../..";
159
160 OUString envVar( "OOO_INSTALL_PREFIX");
161 osl_setEnvironment(envVar.pData, installPrefix.pData);
162 }
163 SAL_INFO("svx", "OOO_INSTALL_PREFIX=" << getenv( "OOO_INSTALL_PREFIX" ) );
164
165 uno::Reference<uno::XComponentContext> xComponentContext
166 = ::cppu::defaultBootstrap_InitialComponentContext();
167 xMSF.set( xComponentContext->getServiceManager(), uno::UNO_QUERY );
168 if( !xMSF.is() )
169 {
170 fprintf( stderr, "Failed to bootstrap\n" );
171 exit( 1 );
172 }
173 ::comphelper::setProcessServiceFactory( xMSF );
174
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 );
182 }
183}
184
185static std::vector<OUString> ReadResponseFile_Impl(OUString const& rInput)
186{
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)
191 {
192 fprintf(stderr, "error while opening response file: %s (%d)\n",
193 uInput.getStr(), rc);
194 exit(1);
195 }
196
197 std::vector<OUString> ret;
198 OUStringBuffer b(256);
199 char buf[1<<16];
200 while (true)
201 {
202 sal_uInt64 size(0);
203 rc = file.read(buf, sizeof(buf), size);
204 if (osl::FileBase::E_None != rc)
205 {
206 fprintf(stderr, "error while reading response file: %s (%d)\n",
207 uInput.getStr(), rc);
208 exit(1);
209 }
210 if (!size)
211 break;
212 for (sal_uInt64 i = 0; i < size; ++i)
213 {
214 if (static_cast<unsigned char>(buf[i]) >= 128)
215 {
216 fprintf(stderr, "non-ASCII character in response file: %s\n",
217 uInput.getStr());
218 exit(1);
219 }
220 switch (buf[i])
221 {
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;
232 }
233 }
234 }
235 if (!b.isEmpty())
236 ret.push_back(b.makeStringAndClear());
237 return ret;
238}
239
240static void
241ReadResponseFile(std::vector<INetURLObject> & rFiles, OUString const& rInput)
242{
243 std::vector<OUString> files(ReadResponseFile_Impl(rInput));
244 for (size_t i = 0; i < files.size(); ++i)
245 {
246 rFiles.push_back(Smartify(files[i]));
247 }
248}
249
250int GalApp::Main()
251{
252 try
253 {
255
256 OUString aPath, aDestDir;
257 OUString aName( "Default name" );
258 std::vector<INetURLObject> aFiles;
259
260 for( sal_uInt16 i = 0; i < GetCommandLineParamCount(); ++i )
261 {
262 OUString aParam = GetCommandLineParam( i );
263
264 if ( aParam.startsWith( "-env:" ) )
265 continue;
266 else if ( aParam == "--help" || aParam == "-h" )
267 return PrintHelp();
268 else if ( aParam == "--build-tree" )
269 {
270 mbRelativeURLs = true;
271 mbInBuildTree = true;
272 }
273 else if ( aParam == "--name" )
274 aName = GetCommandLineParam( ++i );
275 else if ( aParam == "--path" )
276 aPath = Smartify( GetCommandLineParam( ++i ) ).
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 ) );
289 }
290
291 if( aFiles.empty() )
292 return PrintHelp();
293
294 createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
295 }
296 catch (const uno::Exception&)
297 {
298 TOOLS_WARN_EXCEPTION("svx", "Fatal");
299 return EXIT_FAILURE;
300 }
301 catch (const std::exception &e)
302 {
303 SAL_WARN("svx", "Fatal: " << e.what());
304 return 1;
305 }
306
307 return EXIT_SUCCESS;
308}
309
310void GalApp::DeInit()
311{
312 auto xDesktop = css::frame::Desktop::create(comphelper::getProcessComponentContext());
313 xDesktop->terminate();
314 uno::Reference< lang::XComponent >(
316 uno::UNO_QUERY_THROW )-> dispose();
317 ::comphelper::setProcessServiceFactory( nullptr );
318}
319
321{
323 static GalApp aGalApp;
324}
325
326/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void Init()
virtual int Main()
virtual void DeInit()
static void EnableConsoleOnly()
bool InsertURL(const INetURLObject &rURL, sal_uInt32 nInsertPos=SAL_MAX_UINT32)
Definition: galtheme.cxx:497
void SetDestDir(const OUString &rDestDir, bool bRelative)
Definition: galtheme.cxx:79
SAL_DLLPRIVATE sal_uInt32 GetObjectCount() const
Definition: galtheme.hxx:82
static SfxApplication * GetOrCreate()
#define TOOLS_WARN_EXCEPTION(area, stream)
URL aURL
static int PrintHelp()
Definition: gengal.cxx:111
static std::vector< OUString > ReadResponseFile_Impl(OUString const &rInput)
Definition: gengal.cxx:185
static INetURLObject Smartify(std::u16string_view rPath)
Definition: gengal.cxx:138
static void createTheme(const OUString &aThemeName, std::u16string_view aGalleryURL, const OUString &aDestDir, std::vector< INetURLObject > &rFiles, bool bRelativeURLs)
Definition: gengal.cxx:58
static void ReadResponseFile(std::vector< INetURLObject > &rFiles, OUString const &rInput)
Definition: gengal.cxx:241
OUString aName
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
size
Reference< XComponentContext > getProcessComponentContext()
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
void dispose()
void createApplication()
Definition: gengal.cxx:320
#define SAL_PRI_SIZET