merge the formfield patch from ooo-build
[ooovba.git] / sd / source / filter / html / buttonset.cxx
blobd5f7774c44f94d44a5ed883154653798ef02e8d2
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: buttonset.cxx,v $
10 * $Revision: 1.3 $
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_sd.hxx"
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <com/sun/star/graphic/XGraphicProvider.hpp>
37 #include <osl/file.hxx>
38 #include <comphelper/storagehelper.hxx>
39 #include <comphelper/oslfile2streamwrap.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <tools/debug.hxx>
42 #include <vcl/graph.hxx>
43 #include <vcl/virdev.hxx>
44 #include <vcl/image.hxx>
45 #include <svtools/pathoptions.hxx>
47 #include <boost/shared_ptr.hpp>
49 #include "buttonset.hxx"
51 using ::rtl::OUString;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::graphic;
54 using namespace ::com::sun::star::embed;
55 using namespace ::com::sun::star::io;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::lang;
59 class ButtonsImpl
61 public:
62 ButtonsImpl( const OUString& rURL );
64 Reference< XInputStream > getInputStream( const OUString& rName );
66 bool getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic );
68 bool copyGraphic( const OUString& rName, const OUString& rPath );
70 private:
71 Reference< XStorage > mxStorage;
74 ButtonsImpl::ButtonsImpl( const OUString& rURL )
76 try
78 mxStorage = comphelper::OStorageHelper::GetStorageFromURL( rURL, ElementModes::READ );
80 catch( Exception& )
82 DBG_ERROR("sd::ButtonsImpl::ButtonsImpl(), exception caught!" );
86 Reference< XInputStream > ButtonsImpl::getInputStream( const OUString& rName )
88 Reference< XInputStream > xInputStream;
89 if( mxStorage.is() ) try
91 Reference< XStream > xStream( mxStorage->openStreamElement( rName, ElementModes::READ ) );
92 if( xStream.is() )
93 xInputStream = xStream->getInputStream();
95 catch( Exception& )
97 DBG_ERROR( "sd::ButtonsImpl::getInputStream(), exception caught!" );
99 return xInputStream;
102 bool ButtonsImpl::getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const rtl::OUString& rName, Graphic& rGraphic )
104 Reference< XInputStream > xInputStream( getInputStream( rName ) );
105 if( xInputStream.is() && xGraphicProvider.is() ) try
107 Sequence< PropertyValue > aMediaProperties( 1 );
108 aMediaProperties[0].Name = ::rtl::OUString::createFromAscii( "InputStream" );
109 aMediaProperties[0].Value <<= xInputStream;
110 Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties ) );
112 if( xGraphic.is() )
114 rGraphic = Graphic( xGraphic );
115 return true;
118 catch( Exception& )
120 DBG_ERROR( "sd::ButtonsImpl::getGraphic(), exception caught!" );
122 return false;
125 bool ButtonsImpl::copyGraphic( const OUString& rName, const OUString& rPath )
127 Reference< XInputStream > xInput( getInputStream( rName ) );
128 if( xInput.is() ) try
130 osl::File::remove( rPath );
131 osl::File aOutputFile( rPath );
132 if( aOutputFile.open( OpenFlag_Write|OpenFlag_Create ) == osl::FileBase::E_None )
134 Reference< XOutputStream > xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
135 comphelper::OStorageHelper::CopyInputToOutput( xInput, xOutput );
136 return true;
139 catch( Exception& )
141 DBG_ERROR( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
144 return false;
147 typedef std::vector< boost::shared_ptr< ButtonsImpl > > ButtonVector;
148 class ButtonSetImpl
150 public:
151 ButtonSetImpl();
153 int getCount() const;
155 bool getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage );
156 bool exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName );
158 void scanForButtonSets( const OUString& rPath );
160 Reference< XGraphicProvider > getGraphicProvider();
162 ButtonVector maButtons;
163 Reference< XGraphicProvider > mxGraphicProvider;
166 ButtonSetImpl::ButtonSetImpl()
168 const OUString sSubPath( RTL_CONSTASCII_USTRINGPARAM( "/wizard/web/buttons" ) );
170 OUString sSharePath( SvtPathOptions().GetConfigPath() );
171 sSharePath += sSubPath;
172 scanForButtonSets( sSharePath );
174 OUString sUserPath( SvtPathOptions().GetUserConfigPath() );
175 sUserPath += sSubPath;
176 scanForButtonSets( sUserPath );
179 void ButtonSetImpl::scanForButtonSets( const OUString& rPath )
181 OUString aSystemPath;
182 osl::Directory aDirectory( rPath );
183 if( aDirectory.open() == osl::FileBase::E_None )
185 osl::DirectoryItem aItem;
186 while( aDirectory.getNextItem( aItem, 2211 ) == osl::FileBase::E_None )
188 osl::FileStatus aStatus( FileStatusMask_FileName|FileStatusMask_FileURL );
189 if( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
191 OUString sFileName( aStatus.getFileName() );
192 if( sFileName.endsWithIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM(".zip" ) ) )
193 maButtons.push_back( boost::shared_ptr< ButtonsImpl >( new ButtonsImpl( aStatus.getFileURL() ) ) );
199 int ButtonSetImpl::getCount() const
201 return maButtons.size();
204 bool ButtonSetImpl::getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage )
206 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
208 ButtonsImpl& rSet = *maButtons[nSet].get();
210 std::vector< Graphic > aGraphics;
212 VirtualDevice aDev;
213 aDev.SetMapMode(MapMode(MAP_PIXEL));
215 Size aSize;
216 std::vector< rtl::OUString >::const_iterator aIter( rButtons.begin() );
217 while( aIter != rButtons.end() )
219 Graphic aGraphic;
220 if( !rSet.getGraphic( getGraphicProvider(), (*aIter++), aGraphic ) )
221 return false;
223 aGraphics.push_back(aGraphic);
225 Size aGraphicSize( aGraphic.GetSizePixel( &aDev ) );
226 aSize.Width() += aGraphicSize.Width();
228 if( aSize.Height() < aGraphicSize.Height() )
229 aSize.Height() = aGraphicSize.Height();
231 if( aIter != rButtons.end() )
232 aSize.Width() += 3;
235 aDev.SetOutputSizePixel( aSize );
237 Point aPos;
239 std::vector< Graphic >::iterator aGraphIter( aGraphics.begin() );
240 while( aGraphIter != aGraphics.end() )
242 Graphic aGraphic( (*aGraphIter++) );
244 aGraphic.Draw( &aDev, aPos );
246 aPos.X() += aGraphic.GetSizePixel().Width() + 3;
249 rImage = Image( aDev.GetBitmapEx( Point(), aSize ) );
250 return true;
252 return false;
255 bool ButtonSetImpl::exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName )
257 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
259 ButtonsImpl& rSet = *maButtons[nSet].get();
261 return rSet.copyGraphic( rName, rPath );
263 return false;
266 Reference< XGraphicProvider > ButtonSetImpl::getGraphicProvider()
268 if( !mxGraphicProvider.is() )
270 Reference< XMultiServiceFactory > xServiceManager( ::comphelper::getProcessServiceFactory() );
271 if( xServiceManager.is() ) try
273 Reference< XGraphicProvider > xGraphProvider(
274 xServiceManager->createInstance(
275 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ), UNO_QUERY_THROW );
277 mxGraphicProvider = xGraphProvider;
279 catch( Exception& )
281 DBG_ERROR("sd::ButtonSetImpl::getGraphicProvider(), could not get graphic provider!");
284 return mxGraphicProvider;
288 ButtonSet::ButtonSet()
289 : mpImpl( new ButtonSetImpl() )
293 ButtonSet::~ButtonSet()
295 delete mpImpl;
298 int ButtonSet::getCount() const
300 return mpImpl->getCount();
303 bool ButtonSet::getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage )
305 return mpImpl->getPreview( nSet, rButtons, rImage );
308 bool ButtonSet::exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName )
310 return mpImpl->exportButton( nSet, rPath, rName );