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