merge the formfield patch from ooo-build
[ooovba.git] / svx / source / gengal / gengal.cxx
bloba12dc6559743d179776938f3d73cd916c5b404f7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gengal.cxx,v $
10 * $Revision: 1.10.288.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <memory>
37 #include <list>
39 #include <unotools/streamwrap.hxx>
40 #include <unotools/ucbstreamhelper.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include <comphelper/regpathhelper.hxx>
44 #include <cppuhelper/servicefactory.hxx>
45 #include <cppuhelper/bootstrap.hxx>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/lang/XInitialization.hpp>
48 #include <com/sun/star/registry/XSimpleRegistry.hpp>
50 #include <ucbhelper/contentbroker.hxx>
51 #include <ucbhelper/configurationkeys.hxx>
53 #include <tools/urlobj.hxx>
54 #include <tools/fsys.hxx>
55 #include <svtools/filedlg.hxx>
57 #include <vcl/window.hxx>
58 #include <vcl/svapp.hxx>
59 #include <vcl/font.hxx>
60 #include <vcl/sound.hxx>
61 #include <vcl/print.hxx>
62 #include <vcl/toolbox.hxx>
63 #include <vcl/help.hxx>
64 #include <vcl/scrbar.hxx>
65 #include <vcl/wrkwin.hxx>
66 #include <vcl/msgbox.hxx>
68 #include <osl/file.hxx>
69 #include <osl/process.h>
70 #include <rtl/bootstrap.hxx>
72 #include <galtheme.hxx>
73 #include <gallery1.hxx>
75 using namespace ::vos;
76 using namespace ::rtl;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::registry;
79 using namespace ::com::sun::star::lang;
81 typedef ::std::list<rtl::OUString> FileNameList;
83 class GalApp : public Application
85 public:
86 virtual void Main();
88 protected:
89 Reference<XMultiServiceFactory> xMSF;
90 void Init();
91 void InitUCB();
94 Gallery* createGallery( const rtl::OUString& aGalleryURL )
96 return new Gallery( aGalleryURL );
99 void disposeGallery( Gallery* pGallery )
101 delete pGallery;
104 static void createTheme( rtl::OUString aThemeName,
105 rtl::OUString aGalleryURL,
106 rtl::OUString aDestDir,
107 UINT32 nNumFrom,
108 FileNameList &rFiles )
110 Gallery * pGallery( createGallery( aGalleryURL ) );
112 if (!pGallery ) {
113 fprintf( stderr, "Could't acquire '%s'\n",
114 (const sal_Char *) rtl::OUStringToOString( aGalleryURL,
115 RTL_TEXTENCODING_UTF8 ) );
116 exit( 1 );
118 fprintf( stderr, "Work on gallery '%s'\n",
119 (const sal_Char *) rtl::OUStringToOString( aGalleryURL, RTL_TEXTENCODING_UTF8 ) );
121 fprintf( stderr, "Existing themes: %lu\n",
122 sal::static_int_cast< unsigned long >(
123 pGallery->GetThemeCount() ) );
125 if( !pGallery->HasTheme( aThemeName) ) {
126 if( !pGallery->CreateTheme( aThemeName, nNumFrom ) ) {
127 fprintf( stderr, "Failed to create theme\n" );
128 disposeGallery( pGallery );
129 exit( 1 );
133 fprintf( stderr, "Existing themes: %lu\n",
134 sal::static_int_cast< unsigned long >(
135 pGallery->GetThemeCount() ) );
137 SfxListener aListener;
139 GalleryTheme *pGalTheme = pGallery->AcquireTheme( aThemeName, aListener );
140 if ( pGalTheme == NULL ) {
141 fprintf( stderr, "Failed to acquire theme\n" );
142 disposeGallery( pGallery );
143 exit( 1 );
146 fprintf( stderr, "Using DestDir: %s\n",
147 (const sal_Char *) rtl::OUStringToOString( aDestDir, RTL_TEXTENCODING_UTF8 ) );
148 pGalTheme->SetDestDir(String(aDestDir));
150 FileNameList::const_iterator aIter;
152 for( aIter = rFiles.begin(); aIter != rFiles.end(); aIter++ )
154 // Should/could use:
155 // if ( ! pGalTheme->InsertFileOrDirURL( aURL ) ) {
156 // Requires a load more components ...
158 Graphic aGraphic;
159 String aFormat;
161 #if 1
162 if ( ! pGalTheme->InsertURL( *aIter ) )
163 fprintf( stderr, "Failed to import '%s'\n",
164 (const sal_Char *) rtl::OUStringToOString( *aIter, RTL_TEXTENCODING_UTF8 ) );
165 else
166 fprintf( stderr, "Imported file '%s' (%lu)\n",
167 (const sal_Char *) rtl::OUStringToOString( *aIter, RTL_TEXTENCODING_UTF8 ),
168 sal::static_int_cast< unsigned long >(
169 pGalTheme->GetObjectCount() ) );
171 #else // only loads BMPs
172 SvStream *pStream = ::utl::UcbStreamHelper::CreateStream( *aIter, STREAM_READ );
173 if (!pStream) {
174 fprintf( stderr, "Can't find image to import\n" );
175 disposeGallery( pGallery );
176 exit (1);
178 *pStream >> aGraphic;
179 delete pStream;
180 if( aGraphic.GetType() == GRAPHIC_NONE )
182 fprintf( stderr, "Failed to load '%s'\n",
183 (const sal_Char *) rtl::OUStringToOString( *aIter, RTL_TEXTENCODING_UTF8 ) );
184 continue;
187 SgaObjectBmp aObject( aGraphic, *aIter, aFormat );
188 if ( ! aObject.IsValid() ) {
189 fprintf( stderr, "Failed to create thumbnail for image\n" );
190 continue;
193 if ( ! pGalTheme->InsertObject( aObject ) ) {
194 fprintf( stderr, "Failed to insert file or URL\n" );
195 continue;
197 #endif
200 pGallery->ReleaseTheme( pGalTheme, aListener );
201 disposeGallery( pGallery );
204 static void PrintHelp()
206 fprintf( stdout, "Utility to generate OO.o gallery files\n\n" );
208 fprintf( stdout, "using: gengal --name <name> --path <dir> [ --destdir <path> ]\n");
209 fprintf( stdout, " [ --number-from <num> ] [ files ... ]\n\n" );
211 fprintf( stdout, "options:\n");
212 fprintf( stdout, " --name <theme>\t\tdefines a name of the created or updated theme.\n");
213 fprintf( stdout, " --path <dir>\t\tdefines directory where the gallery files are created\n");
214 fprintf( stdout, "\t\t\tor updated.\n");
215 fprintf( stdout, " --destdir <dir>\tdefines a path prefix to be removed from the paths\n");
216 fprintf( stdout, "\t\t\tstored in the gallery files. It is useful to create\n");
217 fprintf( stdout, "\t\t\tRPM packages using the BuildRoot feature.\n");
218 fprintf( stdout, " --number-from <num>\tdefines minimal number for the newly created gallery\n");
219 fprintf( stdout, "\t\t\ttheme files.\n");
220 fprintf( stdout, " files\t\t\tlists files to be added to the gallery. Absolute paths\n");
221 fprintf( stdout, "\t\t\tare required.\n");
224 static OUString Smartify( const OUString &rPath )
226 INetURLObject aURL;
227 aURL.SetSmartURL( rPath );
228 return aURL.GetMainURL( INetURLObject::NO_DECODE );
231 #define OUSTRING_CSTR( str ) \
232 rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US ).getStr()
234 void GalApp::Init()
236 if( getenv( "OOO_INSTALL_PREFIX" ) == NULL ) {
237 OUString fileName = GetAppFileName();
238 int lastSlash = fileName.lastIndexOf( '/' );
239 #ifdef WNT
240 // Don't know which directory separators GetAppFileName() returns on Windows.
241 // Be safe and take into consideration they might be backslashes.
242 if( fileName.lastIndexOf( '\\' ) > lastSlash )
243 lastSlash = fileName.lastIndexOf( '\\' );
244 #endif
245 OUString baseBinDir = fileName.copy( 0, lastSlash );
246 OUString installPrefix = baseBinDir + OUString::createFromAscii( "/../.." );
247 OUString assignment = OUString::createFromAscii( "OOO_INSTALL_PREFIX=" ) + installPrefix;
248 putenv( strdup( OUSTRING_CSTR( assignment )));
250 OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
252 Reference<XComponentContext> xComponentContext
253 = ::cppu::defaultBootstrap_InitialComponentContext();
254 xMSF = Reference<XMultiServiceFactory>
255 ( xComponentContext->getServiceManager(), UNO_QUERY );
256 if( !xMSF.is() )
257 fprintf( stderr, "Failed to bootstrap\n" );
258 ::comphelper::setProcessServiceFactory( xMSF );
260 InitUCB();
263 void GalApp::InitUCB()
265 OUString aEmpty;
266 Sequence< Any > aArgs(6);
267 aArgs[0]
268 <<= rtl::OUString::createFromAscii(UCB_CONFIGURATION_KEY1_LOCAL);
269 aArgs[1]
270 <<= rtl::OUString::createFromAscii(UCB_CONFIGURATION_KEY2_OFFICE);
271 aArgs[2] <<= rtl::OUString::createFromAscii("PIPE");
272 aArgs[3] <<= aEmpty;
273 aArgs[4] <<= rtl::OUString::createFromAscii("PORTAL");
274 aArgs[5] <<= aEmpty;
276 if (! ::ucbhelper::ContentBroker::initialize( xMSF, aArgs ) )
277 fprintf( stderr, "Failed to init content broker\n" );
280 void GalApp::Main()
282 bool bHelp = false;
283 rtl::OUString aPath, aDestDir;
284 rtl::OUString aName = rtl::OUString::createFromAscii( "Default name" );
285 UINT32 nNumFrom = 0;
286 FileNameList aFiles;
288 for( USHORT i = 0; i < GetCommandLineParamCount(); i++ )
290 OUString aParam = GetCommandLineParam( i );
292 if( aParam.equalsAscii( "--help" ) ||
293 aParam.equalsAscii( "-h" ) )
294 bHelp = true;
296 else if ( aParam.equalsAscii( "--name" ) )
297 aName = GetCommandLineParam( ++i );
299 else if ( aParam.equalsAscii( "--path" ) )
300 aPath = Smartify( GetCommandLineParam( ++i ) );
302 else if ( aParam.equalsAscii( "--destdir" ) )
303 aDestDir = GetCommandLineParam( ++i );
305 else if ( aParam.equalsAscii( "--number-from" ) )
306 nNumFrom = GetCommandLineParam( ++i ).ToInt32();
308 else
309 aFiles.push_back( Smartify( aParam ) );
312 if( bHelp )
314 PrintHelp();
315 return;
318 createTheme( aName, aPath, aDestDir, nNumFrom, aFiles );
321 GalApp aGalApp;