Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "sal/config.h"
30 : :
31 : : #include <com/sun/star/embed/ElementModes.hpp>
32 : : #include <com/sun/star/embed/XStorage.hpp>
33 : : #include <com/sun/star/graphic/GraphicProvider.hpp>
34 : : #include <com/sun/star/graphic/XGraphicProvider.hpp>
35 : : #include <com/sun/star/io/XStream.hpp>
36 : :
37 : : #include <osl/file.hxx>
38 : : #include <comphelper/storagehelper.hxx>
39 : : #include <comphelper/oslfile2streamwrap.hxx>
40 : : #include <comphelper/processfactory.hxx>
41 : : #include <vcl/graph.hxx>
42 : : #include <vcl/virdev.hxx>
43 : : #include <vcl/image.hxx>
44 : : #include <unotools/pathoptions.hxx>
45 : :
46 : : #include <boost/shared_ptr.hpp>
47 : :
48 : : #include "buttonset.hxx"
49 : :
50 : : using ::rtl::OUString;
51 : : using namespace ::com::sun::star::uno;
52 : : using namespace ::com::sun::star::graphic;
53 : : using namespace ::com::sun::star::embed;
54 : : using namespace ::com::sun::star::io;
55 : : using namespace ::com::sun::star::beans;
56 : : using namespace ::com::sun::star::lang;
57 : :
58 : 0 : class ButtonsImpl
59 : : {
60 : : public:
61 : : ButtonsImpl( const OUString& rURL );
62 : :
63 : : Reference< XInputStream > getInputStream( const OUString& rName );
64 : :
65 : : bool getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic );
66 : :
67 : : bool copyGraphic( const OUString& rName, const OUString& rPath );
68 : :
69 : : private:
70 : : Reference< XStorage > mxStorage;
71 : : };
72 : :
73 : 0 : ButtonsImpl::ButtonsImpl( const OUString& rURL )
74 : : {
75 : : try
76 : : {
77 [ # # ][ # # ]: 0 : mxStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL( ZIP_STORAGE_FORMAT_STRING, rURL, ElementModes::READ );
[ # # ][ # # ]
78 : : }
79 [ # # ]: 0 : catch( Exception& )
80 : : {
81 : : OSL_FAIL("sd::ButtonsImpl::ButtonsImpl(), exception caught!" );
82 : : }
83 : 0 : }
84 : :
85 : 0 : Reference< XInputStream > ButtonsImpl::getInputStream( const OUString& rName )
86 : : {
87 : 0 : Reference< XInputStream > xInputStream;
88 [ # # ]: 0 : if( mxStorage.is() ) try
89 : : {
90 [ # # ][ # # ]: 0 : Reference< XStream > xStream( mxStorage->openStreamElement( rName, ElementModes::READ ) );
91 [ # # ]: 0 : if( xStream.is() )
92 [ # # ][ # # ]: 0 : xInputStream = xStream->getInputStream();
[ # # ][ # # ]
93 : : }
94 [ # # ]: 0 : catch( Exception& )
95 : : {
96 : : OSL_FAIL( "sd::ButtonsImpl::getInputStream(), exception caught!" );
97 : : }
98 : 0 : return xInputStream;
99 : : }
100 : :
101 : 0 : bool ButtonsImpl::getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const rtl::OUString& rName, Graphic& rGraphic )
102 : : {
103 [ # # ]: 0 : Reference< XInputStream > xInputStream( getInputStream( rName ) );
104 [ # # ][ # # ]: 0 : if( xInputStream.is() && xGraphicProvider.is() ) try
[ # # ]
105 : : {
106 [ # # ]: 0 : Sequence< PropertyValue > aMediaProperties( 1 );
107 [ # # ]: 0 : aMediaProperties[0].Name = ::rtl::OUString("InputStream" );
108 [ # # ][ # # ]: 0 : aMediaProperties[0].Value <<= xInputStream;
109 [ # # ][ # # ]: 0 : Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties ) );
110 : :
111 [ # # ]: 0 : if( xGraphic.is() )
112 : : {
113 [ # # ][ # # ]: 0 : rGraphic = Graphic( xGraphic );
[ # # ]
114 : 0 : return true;
115 [ # # ][ # # ]: 0 : }
[ # # ][ # # ]
116 : : }
117 [ # # ]: 0 : catch( Exception& )
118 : : {
119 : : OSL_FAIL( "sd::ButtonsImpl::getGraphic(), exception caught!" );
120 : : }
121 : 0 : return false;
122 : : }
123 : :
124 : 0 : bool ButtonsImpl::copyGraphic( const OUString& rName, const OUString& rPath )
125 : : {
126 [ # # ]: 0 : Reference< XInputStream > xInput( getInputStream( rName ) );
127 [ # # ]: 0 : if( xInput.is() ) try
128 : : {
129 [ # # ]: 0 : osl::File::remove( rPath );
130 : 0 : osl::File aOutputFile( rPath );
131 [ # # ][ # # ]: 0 : if( aOutputFile.open( osl_File_OpenFlag_Write|osl_File_OpenFlag_Create ) == osl::FileBase::E_None )
132 : : {
133 [ # # ][ # # ]: 0 : Reference< XOutputStream > xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
[ # # ]
134 [ # # ]: 0 : comphelper::OStorageHelper::CopyInputToOutput( xInput, xOutput );
135 : 0 : return true;
136 [ # # ][ # # ]: 0 : }
[ # # ]
137 : : }
138 [ # # ]: 0 : catch( Exception& )
139 : : {
140 : : OSL_FAIL( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
141 : : }
142 : :
143 : 0 : return false;
144 : : }
145 : :
146 : : typedef std::vector< boost::shared_ptr< ButtonsImpl > > ButtonVector;
147 : 0 : class ButtonSetImpl
148 : : {
149 : : public:
150 : : ButtonSetImpl();
151 : :
152 : : int getCount() const;
153 : :
154 : : bool getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage );
155 : : bool exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName );
156 : :
157 : : void scanForButtonSets( const OUString& rPath );
158 : :
159 : : Reference< XGraphicProvider > getGraphicProvider();
160 : :
161 : : ButtonVector maButtons;
162 : : Reference< XGraphicProvider > mxGraphicProvider;
163 : : };
164 : :
165 : 0 : ButtonSetImpl::ButtonSetImpl()
166 : : {
167 : 0 : const OUString sSubPath( "/wizard/web/buttons" );
168 : :
169 [ # # ][ # # ]: 0 : OUString sSharePath( SvtPathOptions().GetConfigPath() );
[ # # ][ # # ]
170 : 0 : sSharePath += sSubPath;
171 [ # # ]: 0 : scanForButtonSets( sSharePath );
172 : :
173 [ # # ][ # # ]: 0 : OUString sUserPath( SvtPathOptions().GetUserConfigPath() );
[ # # ][ # # ]
174 : 0 : sUserPath += sSubPath;
175 [ # # ]: 0 : scanForButtonSets( sUserPath );
176 : 0 : }
177 : :
178 : 0 : void ButtonSetImpl::scanForButtonSets( const OUString& rPath )
179 : : {
180 : 0 : osl::Directory aDirectory( rPath );
181 [ # # ][ # # ]: 0 : if( aDirectory.open() == osl::FileBase::E_None )
182 : : {
183 : 0 : osl::DirectoryItem aItem;
184 [ # # ][ # # ]: 0 : while( aDirectory.getNextItem( aItem, 2211 ) == osl::FileBase::E_None )
185 : : {
186 : 0 : osl::FileStatus aStatus( osl_FileStatus_Mask_FileName|osl_FileStatus_Mask_FileURL );
187 [ # # ][ # # ]: 0 : if( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
188 : : {
189 [ # # ]: 0 : OUString sFileName( aStatus.getFileName() );
190 [ # # ]: 0 : if( sFileName.endsWithIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM(".zip" ) ) )
191 [ # # ][ # # ]: 0 : maButtons.push_back( boost::shared_ptr< ButtonsImpl >( new ButtonsImpl( aStatus.getFileURL() ) ) );
[ # # ][ # # ]
[ # # ][ # # ]
192 : : }
193 [ # # ]: 0 : }
194 [ # # ]: 0 : }
195 : 0 : }
196 : :
197 : 0 : int ButtonSetImpl::getCount() const
198 : : {
199 : 0 : return maButtons.size();
200 : : }
201 : :
202 : 0 : bool ButtonSetImpl::getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage )
203 : : {
204 [ # # ][ # # ]: 0 : if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
[ # # ]
205 : : {
206 : 0 : ButtonsImpl& rSet = *maButtons[nSet].get();
207 : :
208 [ # # ]: 0 : std::vector< Graphic > aGraphics;
209 : :
210 [ # # ]: 0 : VirtualDevice aDev;
211 [ # # ][ # # ]: 0 : aDev.SetMapMode(MapMode(MAP_PIXEL));
[ # # ]
212 : :
213 : 0 : Size aSize;
214 : 0 : std::vector< rtl::OUString >::const_iterator aIter( rButtons.begin() );
215 [ # # ][ # # ]: 0 : while( aIter != rButtons.end() )
216 : : {
217 [ # # ]: 0 : Graphic aGraphic;
218 [ # # ][ # # ]: 0 : if( !rSet.getGraphic( getGraphicProvider(), (*aIter++), aGraphic ) )
[ # # ][ # # ]
219 : 0 : return false;
220 : :
221 [ # # ]: 0 : aGraphics.push_back(aGraphic);
222 : :
223 [ # # ]: 0 : Size aGraphicSize( aGraphic.GetSizePixel( &aDev ) );
224 : 0 : aSize.Width() += aGraphicSize.Width();
225 : :
226 [ # # ]: 0 : if( aSize.Height() < aGraphicSize.Height() )
227 : 0 : aSize.Height() = aGraphicSize.Height();
228 : :
229 [ # # ][ # # ]: 0 : if( aIter != rButtons.end() )
230 : 0 : aSize.Width() += 3;
231 [ # # ][ # # ]: 0 : }
232 : :
233 [ # # ]: 0 : aDev.SetOutputSizePixel( aSize );
234 : :
235 : 0 : Point aPos;
236 : :
237 : 0 : std::vector< Graphic >::iterator aGraphIter( aGraphics.begin() );
238 [ # # ][ # # ]: 0 : while( aGraphIter != aGraphics.end() )
239 : : {
240 [ # # ][ # # ]: 0 : Graphic aGraphic( (*aGraphIter++) );
[ # # ]
241 : :
242 [ # # ]: 0 : aGraphic.Draw( &aDev, aPos );
243 : :
244 [ # # ]: 0 : aPos.X() += aGraphic.GetSizePixel().Width() + 3;
245 [ # # ]: 0 : }
246 : :
247 [ # # ][ # # ]: 0 : rImage = Image( aDev.GetBitmapEx( Point(), aSize ) );
[ # # ][ # # ]
[ # # ]
248 [ # # ]: 0 : return true;
249 : : }
250 : 0 : return false;
251 : : }
252 : :
253 : 0 : bool ButtonSetImpl::exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName )
254 : : {
255 [ # # ][ # # ]: 0 : if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
[ # # ]
256 : : {
257 : 0 : ButtonsImpl& rSet = *maButtons[nSet].get();
258 : :
259 : 0 : return rSet.copyGraphic( rName, rPath );
260 : : }
261 : 0 : return false;
262 : : }
263 : :
264 : 0 : Reference< XGraphicProvider > ButtonSetImpl::getGraphicProvider()
265 : : {
266 [ # # ]: 0 : if( !mxGraphicProvider.is() )
267 : : {
268 [ # # ]: 0 : Reference< XComponentContext > xComponentContext = ::comphelper::getProcessComponentContext();
269 [ # # ][ # # ]: 0 : mxGraphicProvider = GraphicProvider::create(xComponentContext);
270 : : }
271 : 0 : return mxGraphicProvider;
272 : : }
273 : :
274 : :
275 : 0 : ButtonSet::ButtonSet()
276 [ # # ]: 0 : : mpImpl( new ButtonSetImpl() )
277 : : {
278 : 0 : }
279 : :
280 : 0 : ButtonSet::~ButtonSet()
281 : : {
282 [ # # ]: 0 : delete mpImpl;
283 : 0 : }
284 : :
285 : 0 : int ButtonSet::getCount() const
286 : : {
287 : 0 : return mpImpl->getCount();
288 : : }
289 : :
290 : 0 : bool ButtonSet::getPreview( int nSet, const std::vector< rtl::OUString >& rButtons, Image& rImage )
291 : : {
292 : 0 : return mpImpl->getPreview( nSet, rButtons, rImage );
293 : : }
294 : :
295 : 0 : bool ButtonSet::exportButton( int nSet, const rtl::OUString& rPath, const rtl::OUString& rName )
296 : : {
297 : 0 : return mpImpl->exportButton( nSet, rPath, rName );
298 : : }
299 : :
300 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|