Line data Source code
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 :
11 : // Include this before stdio.h for the __MINGW32__ sake.
12 : // This header contails a define that modifies the way
13 : // formatting strings work for the mingw platforms.
14 : #include <sal/types.h>
15 :
16 : #include <stdio.h>
17 : #ifndef _WIN32
18 : #include <unistd.h>
19 : #endif
20 :
21 : #include <vector>
22 :
23 : #include <unotools/streamwrap.hxx>
24 : #include <unotools/ucbstreamhelper.hxx>
25 :
26 : #include <comphelper/processfactory.hxx>
27 : #include <cppuhelper/bootstrap.hxx>
28 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 : #include <com/sun/star/lang/XInitialization.hpp>
30 : #include <com/sun/star/registry/XSimpleRegistry.hpp>
31 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
32 :
33 : #include <tools/urlobj.hxx>
34 : #include <vcl/vclmain.hxx>
35 :
36 : #include <osl/file.hxx>
37 : #include <osl/process.h>
38 : #include <rtl/bootstrap.hxx>
39 : #include <vcl/svapp.hxx>
40 :
41 : #include <svx/galtheme.hxx>
42 : #include <svx/gallery1.hxx>
43 :
44 : using namespace ::com::sun::star;
45 :
46 11 : class GalApp : public Application
47 : {
48 : bool mbInBuildTree;
49 : bool mbRelativeURLs;
50 : public:
51 11 : GalApp() : mbInBuildTree( false ), mbRelativeURLs( false )
52 : {
53 11 : }
54 : virtual int Main() SAL_OVERRIDE;
55 :
56 : protected:
57 : uno::Reference<lang::XMultiServiceFactory> xMSF;
58 : void Init() SAL_OVERRIDE;
59 : void DeInit() SAL_OVERRIDE;
60 : };
61 :
62 11 : Gallery* createGallery( const OUString& rURL )
63 : {
64 11 : return new Gallery( rURL );
65 : }
66 :
67 11 : void disposeGallery( Gallery* pGallery )
68 : {
69 11 : delete pGallery;
70 11 : }
71 :
72 11 : static void createTheme( const OUString& aThemeName, const OUString& aGalleryURL,
73 : const OUString& aDestDir, std::vector<INetURLObject> &rFiles,
74 : bool bRelativeURLs )
75 : {
76 : Gallery* pGallery;
77 :
78 11 : pGallery = createGallery( aGalleryURL );
79 11 : if (!pGallery ) {
80 : fprintf( stderr, "Could't create '%s'\n",
81 0 : OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
82 0 : exit( 1 );
83 : }
84 : fprintf( stderr, "Work on gallery '%s'\n",
85 11 : OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ).getStr() );
86 :
87 : fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
88 11 : pGallery->GetThemeCount() );
89 :
90 : GalleryTheme *pGalTheme;
91 11 : if( !pGallery->HasTheme( aThemeName) ) {
92 11 : if( !pGallery->CreateTheme( aThemeName ) ) {
93 0 : fprintf( stderr, "Failed to create theme\n" );
94 0 : exit( 1 );
95 : }
96 : }
97 :
98 : fprintf( stderr, "Existing themes: %" SAL_PRI_SIZET "u\n",
99 11 : pGallery->GetThemeCount() );
100 :
101 11 : SfxListener aListener;
102 :
103 11 : if ( !( pGalTheme = pGallery->AcquireTheme( aThemeName, aListener ) ) ) {
104 0 : fprintf( stderr, "Failed to acquire theme\n" );
105 0 : exit( 1 );
106 : }
107 :
108 : fprintf( stderr, "Using DestDir: %s\n",
109 11 : OUStringToOString( aDestDir, RTL_TEXTENCODING_UTF8 ).getStr() );
110 11 : pGalTheme->SetDestDir( aDestDir, bRelativeURLs );
111 :
112 11 : std::vector<INetURLObject>::const_iterator aIter;
113 :
114 514 : for( aIter = rFiles.begin(); aIter != rFiles.end(); ++aIter )
115 : {
116 : // Should/could use:
117 : // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
118 : // Requires a load more components ...
119 :
120 503 : Graphic aGraphic;
121 :
122 503 : if ( ! pGalTheme->InsertURL( *aIter ) )
123 : fprintf( stderr, "Failed to import '%s'\n",
124 0 : OUStringToOString( aIter->GetMainURL(INetURLObject::NO_DECODE), RTL_TEXTENCODING_UTF8 ).getStr() );
125 : else
126 : fprintf( stderr, "Imported file '%s' (%" SAL_PRI_SIZET "u)\n",
127 : OUStringToOString( aIter->GetMainURL(INetURLObject::NO_DECODE), RTL_TEXTENCODING_UTF8 ).getStr(),
128 503 : pGalTheme->GetObjectCount() );
129 503 : }
130 :
131 11 : pGallery->ReleaseTheme( pGalTheme, aListener );
132 :
133 11 : disposeGallery( pGallery );
134 11 : }
135 :
136 0 : static int PrintHelp()
137 : {
138 0 : fprintf( stdout, "Utility to generate LibreOffice gallery files\n\n" );
139 :
140 0 : fprintf( stdout, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
141 0 : fprintf( stdout, " [ files ... ]\n\n" );
142 :
143 0 : fprintf( stdout, "options:\n");
144 0 : fprintf( stdout, " --name <theme>\t\tdefines the user visible name of the created or updated theme.\n");
145 :
146 0 : fprintf( stdout, " --path <dir>\t\tdefines directory where the gallery files are created\n");
147 0 : fprintf( stdout, "\t\t\tor updated.\n");
148 :
149 0 : fprintf( stdout, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
150 0 : fprintf( stdout, "\t\t\tstored in the gallery files. It is useful to create\n");
151 0 : fprintf( stdout, "\t\t\tRPM packages using the BuildRoot feature.\n");
152 :
153 0 : 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");
154 0 : fprintf( stdout, "\t\t\tprimarily used for internal gallery generation at compile time.\n");
155 0 : fprintf( stdout, "\t\t\ttheme files.\n");
156 0 : fprintf( stdout, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
157 0 : fprintf( stdout, "\t\t\tare required.\n");
158 : // --build-tree not documented - only useful during the build ...
159 :
160 0 : return EXIT_SUCCESS;
161 : }
162 :
163 514 : static INetURLObject Smartify( const OUString &rPath )
164 : {
165 514 : INetURLObject aURL;
166 514 : aURL.SetSmartURL( rPath );
167 514 : return aURL;
168 : }
169 :
170 11 : void GalApp::Init()
171 : {
172 : try {
173 11 : if( !mbInBuildTree && getenv( "OOO_INSTALL_PREFIX" ) == NULL ) {
174 11 : OUString fileName = GetAppFileName();
175 11 : int lastSlash = fileName.lastIndexOf( '/' );
176 : #ifdef WNT
177 : // Don't know which directory separators GetAppFileName() returns on Windows.
178 : // Be safe and take into consideration they might be backslashes.
179 : if( fileName.lastIndexOf( '\\' ) > lastSlash )
180 : lastSlash = fileName.lastIndexOf( '\\' );
181 : #endif
182 22 : OUString baseBinDir = fileName.copy( 0, lastSlash );
183 22 : OUString installPrefix = baseBinDir + "/../..";
184 :
185 22 : OUString envVar( "OOO_INSTALL_PREFIX");
186 22 : osl_setEnvironment(envVar.pData, installPrefix.pData);
187 : }
188 : OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
189 :
190 : uno::Reference<uno::XComponentContext> xComponentContext
191 11 : = ::cppu::defaultBootstrap_InitialComponentContext();
192 22 : xMSF = uno::Reference<lang::XMultiServiceFactory>
193 22 : ( xComponentContext->getServiceManager(), uno::UNO_QUERY );
194 11 : if( !xMSF.is() )
195 : {
196 0 : fprintf( stderr, "Failed to bootstrap\n" );
197 0 : exit( 1 );
198 : }
199 11 : ::comphelper::setProcessServiceFactory( xMSF );
200 :
201 : // For backwards compatibility, in case some code still uses plain
202 : // createInstance w/o args directly to obtain an instance:
203 11 : com::sun::star::ucb::UniversalContentBroker::create(xComponentContext);
204 0 : } catch (const uno::Exception &e) {
205 : fprintf( stderr, "Bootstrap exception '%s'\n",
206 0 : rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
207 0 : exit( 1 );
208 : }
209 11 : }
210 :
211 11 : std::vector<OUString> ReadResponseFile_Impl(OUString const& rInput)
212 : {
213 11 : osl::File file(rInput);
214 11 : osl::FileBase::RC rc = file.open(osl_File_OpenFlag_Read);
215 22 : OString const uInput(rtl::OUStringToOString(rInput, RTL_TEXTENCODING_UTF8));
216 11 : if (osl::FileBase::E_None != rc)
217 : {
218 : fprintf(stderr, "error while opening response file: %s (%d)\n",
219 0 : uInput.getStr(), rc);
220 0 : exit(1);
221 : }
222 :
223 11 : std::vector<OUString> ret;
224 22 : OUStringBuffer b;
225 : char buf[1<<16];
226 : while (true)
227 : {
228 22 : sal_uInt64 size(0);
229 22 : rc = file.read(buf, sizeof(buf), size);
230 22 : if (osl::FileBase::E_None != rc)
231 : {
232 : fprintf(stderr, "error while reading response file: %s (%d)\n",
233 0 : uInput.getStr(), rc);
234 0 : exit(1);
235 : }
236 22 : if (!size)
237 11 : break;
238 43126 : for (sal_uInt64 i = 0; i < size; ++i)
239 : {
240 43115 : if (static_cast<unsigned char>(buf[i]) >= 128)
241 : {
242 : fprintf(stderr, "non-ASCII character in response file: %s\n",
243 0 : uInput.getStr());
244 0 : exit(1);
245 : }
246 43115 : switch (buf[i])
247 : {
248 : case ' ' :
249 : case '\t':
250 : case '\r':
251 : case '\n':
252 514 : if (!b.isEmpty())
253 503 : ret.push_back(b.makeStringAndClear());
254 514 : break;
255 : default:
256 42601 : b.append(buf[i]);
257 42601 : break;
258 : }
259 : }
260 : }
261 11 : if (!b.isEmpty())
262 0 : ret.push_back(b.makeStringAndClear());
263 22 : return ret;
264 : }
265 :
266 : void
267 11 : ReadResponseFile(std::vector<INetURLObject> & rFiles, OUString const& rInput)
268 : {
269 11 : std::vector<OUString> files(ReadResponseFile_Impl(rInput));
270 514 : for (size_t i = 0; i < files.size(); ++i)
271 : {
272 503 : rFiles.push_back(Smartify(files[i]));
273 11 : }
274 11 : }
275 :
276 11 : int GalApp::Main()
277 : {
278 22 : OUString aPath, aDestDir;
279 22 : OUString aName( "Default name" );
280 22 : std::vector<INetURLObject> aFiles;
281 :
282 132 : for( sal_uInt32 i = 0; i < GetCommandLineParamCount(); i++ )
283 : {
284 121 : OUString aParam = GetCommandLineParam( i );
285 :
286 121 : if ( aParam.startsWith( "-env:" ) )
287 66 : continue;
288 55 : else if ( aParam == "--help" || aParam == "-h" )
289 0 : return PrintHelp();
290 55 : else if ( aParam == "--build-tree" )
291 : {
292 11 : mbRelativeURLs = true;
293 11 : mbInBuildTree = true;
294 : }
295 44 : else if ( aParam == "--name" )
296 11 : aName = GetCommandLineParam( ++i );
297 33 : else if ( aParam == "--path" )
298 22 : aPath = Smartify( GetCommandLineParam( ++i ) ).
299 11 : GetMainURL(INetURLObject::NO_DECODE);
300 22 : else if ( aParam == "--destdir" )
301 11 : aDestDir = GetCommandLineParam( ++i );
302 11 : else if ( aParam == "--relative-urls" )
303 0 : mbRelativeURLs = true;
304 11 : else if ( aParam == "--number-from" )
305 : fprintf ( stderr, "--number-from is deprecated, themes now "
306 0 : "have filenames based on their names\n" );
307 11 : else if ( aParam == "--filenames" )
308 11 : ReadResponseFile(aFiles, GetCommandLineParam(++i));
309 : else
310 0 : aFiles.push_back( Smartify( aParam ) );
311 55 : }
312 :
313 11 : if( aFiles.empty() )
314 0 : return PrintHelp();
315 :
316 11 : createTheme( aName, aPath, aDestDir, aFiles, mbRelativeURLs );
317 :
318 22 : return EXIT_SUCCESS;
319 : }
320 :
321 11 : void GalApp::DeInit()
322 : {
323 : uno::Reference< lang::XComponent >(
324 : comphelper::getProcessComponentContext(),
325 11 : uno::UNO_QUERY_THROW )-> dispose();
326 11 : ::comphelper::setProcessServiceFactory( NULL );
327 11 : }
328 :
329 11 : void vclmain::createApplication()
330 : {
331 11 : Application::EnableConsoleOnly();
332 11 : static GalApp aGalApp;
333 44 : }
334 :
335 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|