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>
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
50 GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
53 virtual int Main() override
;
56 uno::Reference
<lang::XMultiServiceFactory
> xMSF
;
58 void DeInit() override
;
61 static void createTheme( const OUString
& aThemeName
, const OUString
& aGalleryURL
,
62 const OUString
& aDestDir
, std::vector
<INetURLObject
> &rFiles
,
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" );
81 fprintf( stderr
, "Existing themes: %" SAL_PRI_SIZET
"u\n",
82 pGallery
->GetThemeCount() );
84 SfxListener aListener
;
86 pGalTheme
= pGallery
->AcquireTheme( aThemeName
, aListener
);
88 fprintf( stderr
, "Failed to acquire theme\n" );
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
)
99 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
100 // Requires a load more components ...
104 if ( ! pGalTheme
->InsertURL( rFile
) )
105 fprintf( stderr
, "Failed to import '%s'\n",
106 OUStringToOString( rFile
.GetMainURL(INetURLObject::DecodeMechanism::NONE
), RTL_TEXTENCODING_UTF8
).getStr() );
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 ...
143 static INetURLObject
Smartify( const OUString
&rPath
)
146 aURL
.SetSmartURL( rPath
);
153 if( !mbInBuildTree
&& getenv( "OOO_INSTALL_PREFIX" ) == nullptr ) {
154 OUString fileName
= GetAppFileName();
155 int lastSlash
= fileName
.lastIndexOf( '/' );
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( '\\' );
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
);
175 fprintf( stderr
, "Failed to bootstrap\n" );
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() );
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
);
202 std::vector
<OUString
> ret
;
203 OUStringBuffer
b(256);
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
);
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",
232 ret
.push_back(b
.makeStringAndClear());
241 ret
.push_back(b
.makeStringAndClear());
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
]));
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:" ) )
271 else if ( aParam
== "--help" || aParam
== "-h" )
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
));
293 aFiles
.push_back( Smartify( aParam
) );
299 createTheme( aName
, aPath
, aDestDir
, aFiles
, mbRelativeURLs
);
301 catch (const uno::Exception
&)
303 TOOLS_WARN_EXCEPTION("svx", "Fatal");
306 catch (const std::exception
&e
)
308 SAL_WARN("svx", "Fatal: " << e
.what());
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: */