1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <sal/config.h>
11 #include <sal/log.hxx>
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
;
40 class GalApp
: public Application
45 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
48 virtual int Main() override
;
51 uno::Reference
<lang::XMultiServiceFactory
> xMSF
;
53 void DeInit() override
;
58 static void createTheme( const OUString
& aThemeName
, const OUString
& aGalleryURL
,
59 const OUString
& aDestDir
, std::vector
<INetURLObject
> &rFiles
,
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" );
78 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
79 pGallery
->GetThemeCount() );
81 SfxListener aListener
;
83 pGalTheme
= pGallery
->AcquireTheme( aThemeName
, aListener
);
85 fprintf( stderr
, "Failed to acquire theme\n" );
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
)
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() );
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 ...
138 static INetURLObject
Smartify( const OUString
&rPath
)
141 aURL
.SetSmartURL( rPath
);
148 if( !mbInBuildTree
&& getenv( "OOO_INSTALL_PREFIX" ) == nullptr ) {
149 OUString fileName
= GetAppFileName();
150 int lastSlash
= fileName
.lastIndexOf( '/' );
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( '\\' );
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
);
170 fprintf( stderr
, "Failed to bootstrap\n" );
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() );
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
);
197 std::vector
<OUString
> ret
;
198 OUStringBuffer
b(256);
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
);
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",
227 ret
.push_back(b
.makeStringAndClear());
236 ret
.push_back(b
.makeStringAndClear());
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
]));
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:" ) )
266 else if ( aParam
== "--help" || aParam
== "-h" )
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
));
288 aFiles
.push_back( Smartify( aParam
) );
294 createTheme( aName
, aPath
, aDestDir
, aFiles
, mbRelativeURLs
);
296 catch (const uno::Exception
&)
298 TOOLS_WARN_EXCEPTION("svx", "Fatal");
301 catch (const std::exception
&e
)
303 SAL_WARN("svx", "Fatal: " << e
.what());
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: */