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 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <com/sun/star/embed/ElementModes.hpp>
23 : #include <com/sun/star/embed/XStorage.hpp>
24 : #include <com/sun/star/graphic/GraphicProvider.hpp>
25 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
26 : #include <com/sun/star/io/XStream.hpp>
27 :
28 : #include <osl/file.hxx>
29 : #include <comphelper/storagehelper.hxx>
30 : #include <comphelper/oslfile2streamwrap.hxx>
31 : #include <comphelper/processfactory.hxx>
32 : #include <vcl/graph.hxx>
33 : #include <vcl/virdev.hxx>
34 : #include <vcl/image.hxx>
35 : #include <unotools/pathoptions.hxx>
36 :
37 : #include <boost/shared_ptr.hpp>
38 :
39 : #include "buttonset.hxx"
40 :
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::graphic;
43 : using namespace ::com::sun::star::embed;
44 : using namespace ::com::sun::star::io;
45 : using namespace ::com::sun::star::beans;
46 : using namespace ::com::sun::star::lang;
47 :
48 0 : class ButtonsImpl
49 : {
50 : public:
51 : ButtonsImpl( const OUString& rURL );
52 :
53 : Reference< XInputStream > getInputStream( const OUString& rName );
54 :
55 : bool getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic );
56 :
57 : bool copyGraphic( const OUString& rName, const OUString& rPath );
58 :
59 : private:
60 : Reference< XStorage > mxStorage;
61 : };
62 :
63 0 : ButtonsImpl::ButtonsImpl( const OUString& rURL )
64 : {
65 : try
66 : {
67 0 : mxStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL( ZIP_STORAGE_FORMAT_STRING, rURL, ElementModes::READ );
68 : }
69 0 : catch( Exception& )
70 : {
71 : OSL_FAIL("sd::ButtonsImpl::ButtonsImpl(), exception caught!" );
72 : }
73 0 : }
74 :
75 0 : Reference< XInputStream > ButtonsImpl::getInputStream( const OUString& rName )
76 : {
77 0 : Reference< XInputStream > xInputStream;
78 0 : if( mxStorage.is() ) try
79 : {
80 0 : Reference< XStream > xStream( mxStorage->openStreamElement( rName, ElementModes::READ ) );
81 0 : if( xStream.is() )
82 0 : xInputStream = xStream->getInputStream();
83 : }
84 0 : catch( Exception& )
85 : {
86 : OSL_FAIL( "sd::ButtonsImpl::getInputStream(), exception caught!" );
87 : }
88 0 : return xInputStream;
89 : }
90 :
91 0 : bool ButtonsImpl::getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic )
92 : {
93 0 : Reference< XInputStream > xInputStream( getInputStream( rName ) );
94 0 : if( xInputStream.is() && xGraphicProvider.is() ) try
95 : {
96 0 : Sequence< PropertyValue > aMediaProperties( 1 );
97 0 : aMediaProperties[0].Name = "InputStream";
98 0 : aMediaProperties[0].Value <<= xInputStream;
99 0 : Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties ) );
100 :
101 0 : if( xGraphic.is() )
102 : {
103 0 : rGraphic = Graphic( xGraphic );
104 0 : return true;
105 0 : }
106 : }
107 0 : catch( Exception& )
108 : {
109 : OSL_FAIL( "sd::ButtonsImpl::getGraphic(), exception caught!" );
110 : }
111 0 : return false;
112 : }
113 :
114 0 : bool ButtonsImpl::copyGraphic( const OUString& rName, const OUString& rPath )
115 : {
116 0 : Reference< XInputStream > xInput( getInputStream( rName ) );
117 0 : if( xInput.is() ) try
118 : {
119 0 : osl::File::remove( rPath );
120 0 : osl::File aOutputFile( rPath );
121 0 : if( aOutputFile.open( osl_File_OpenFlag_Write|osl_File_OpenFlag_Create ) == osl::FileBase::E_None )
122 : {
123 0 : Reference< XOutputStream > xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
124 0 : comphelper::OStorageHelper::CopyInputToOutput( xInput, xOutput );
125 0 : return true;
126 0 : }
127 : }
128 0 : catch( Exception& )
129 : {
130 : OSL_FAIL( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
131 : }
132 :
133 0 : return false;
134 : }
135 :
136 : typedef std::vector< boost::shared_ptr< ButtonsImpl > > ButtonVector;
137 0 : class ButtonSetImpl
138 : {
139 : public:
140 : ButtonSetImpl();
141 :
142 : int getCount() const;
143 :
144 : bool getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage );
145 : bool exportButton( int nSet, const OUString& rPath, const OUString& rName );
146 :
147 : void scanForButtonSets( const OUString& rPath );
148 :
149 : Reference< XGraphicProvider > getGraphicProvider();
150 :
151 : ButtonVector maButtons;
152 : Reference< XGraphicProvider > mxGraphicProvider;
153 : };
154 :
155 0 : ButtonSetImpl::ButtonSetImpl()
156 : {
157 0 : const OUString sSubPath( "/wizard/web/buttons" );
158 :
159 0 : OUString sSharePath( SvtPathOptions().GetConfigPath() );
160 0 : sSharePath += sSubPath;
161 0 : scanForButtonSets( sSharePath );
162 :
163 0 : OUString sUserPath( SvtPathOptions().GetUserConfigPath() );
164 0 : sUserPath += sSubPath;
165 0 : scanForButtonSets( sUserPath );
166 0 : }
167 :
168 0 : void ButtonSetImpl::scanForButtonSets( const OUString& rPath )
169 : {
170 0 : osl::Directory aDirectory( rPath );
171 0 : if( aDirectory.open() == osl::FileBase::E_None )
172 : {
173 0 : osl::DirectoryItem aItem;
174 0 : while( aDirectory.getNextItem( aItem, 2211 ) == osl::FileBase::E_None )
175 : {
176 0 : osl::FileStatus aStatus( osl_FileStatus_Mask_FileName|osl_FileStatus_Mask_FileURL );
177 0 : if( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
178 : {
179 0 : OUString sFileName( aStatus.getFileName() );
180 0 : if( sFileName.endsWithIgnoreAsciiCase( ".zip" ) )
181 0 : maButtons.push_back( boost::shared_ptr< ButtonsImpl >( new ButtonsImpl( aStatus.getFileURL() ) ) );
182 : }
183 0 : }
184 0 : }
185 0 : }
186 :
187 0 : int ButtonSetImpl::getCount() const
188 : {
189 0 : return maButtons.size();
190 : }
191 :
192 0 : bool ButtonSetImpl::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
193 : {
194 0 : if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
195 : {
196 0 : ButtonsImpl& rSet = *maButtons[nSet].get();
197 :
198 0 : std::vector< Graphic > aGraphics;
199 :
200 0 : VirtualDevice aDev;
201 0 : aDev.SetMapMode(MapMode(MAP_PIXEL));
202 :
203 0 : Size aSize;
204 0 : std::vector< OUString >::const_iterator aIter( rButtons.begin() );
205 0 : while( aIter != rButtons.end() )
206 : {
207 0 : Graphic aGraphic;
208 0 : if( !rSet.getGraphic( getGraphicProvider(), (*aIter++), aGraphic ) )
209 0 : return false;
210 :
211 0 : aGraphics.push_back(aGraphic);
212 :
213 0 : Size aGraphicSize( aGraphic.GetSizePixel( &aDev ) );
214 0 : aSize.Width() += aGraphicSize.Width();
215 :
216 0 : if( aSize.Height() < aGraphicSize.Height() )
217 0 : aSize.Height() = aGraphicSize.Height();
218 :
219 0 : if( aIter != rButtons.end() )
220 0 : aSize.Width() += 3;
221 0 : }
222 :
223 0 : aDev.SetOutputSizePixel( aSize );
224 :
225 0 : Point aPos;
226 :
227 0 : std::vector< Graphic >::iterator aGraphIter( aGraphics.begin() );
228 0 : while( aGraphIter != aGraphics.end() )
229 : {
230 0 : Graphic aGraphic( (*aGraphIter++) );
231 :
232 0 : aGraphic.Draw( &aDev, aPos );
233 :
234 0 : aPos.X() += aGraphic.GetSizePixel().Width() + 3;
235 0 : }
236 :
237 0 : rImage = Image( aDev.GetBitmapEx( Point(), aSize ) );
238 0 : return true;
239 : }
240 0 : return false;
241 : }
242 :
243 0 : bool ButtonSetImpl::exportButton( int nSet, const OUString& rPath, const OUString& rName )
244 : {
245 0 : if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
246 : {
247 0 : ButtonsImpl& rSet = *maButtons[nSet].get();
248 :
249 0 : return rSet.copyGraphic( rName, rPath );
250 : }
251 0 : return false;
252 : }
253 :
254 0 : Reference< XGraphicProvider > ButtonSetImpl::getGraphicProvider()
255 : {
256 0 : if( !mxGraphicProvider.is() )
257 : {
258 0 : Reference< XComponentContext > xComponentContext = ::comphelper::getProcessComponentContext();
259 0 : mxGraphicProvider = GraphicProvider::create(xComponentContext);
260 : }
261 0 : return mxGraphicProvider;
262 : }
263 :
264 :
265 0 : ButtonSet::ButtonSet()
266 0 : : mpImpl( new ButtonSetImpl() )
267 : {
268 0 : }
269 :
270 0 : ButtonSet::~ButtonSet()
271 : {
272 0 : delete mpImpl;
273 0 : }
274 :
275 0 : int ButtonSet::getCount() const
276 : {
277 0 : return mpImpl->getCount();
278 : }
279 :
280 0 : bool ButtonSet::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
281 : {
282 0 : return mpImpl->getPreview( nSet, rButtons, rImage );
283 : }
284 :
285 0 : bool ButtonSet::exportButton( int nSet, const OUString& rPath, const OUString& rName )
286 : {
287 0 : return mpImpl->exportButton( nSet, rPath, rName );
288 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|