bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / filter / html / buttonset.cxx
blobab7590f09bee4c219abed488ecdd2106f790b043
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
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>
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>
37 #include <boost/shared_ptr.hpp>
39 #include "buttonset.hxx"
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;
48 class ButtonsImpl
50 public:
51 ButtonsImpl( const OUString& rURL );
53 Reference< XInputStream > getInputStream( const OUString& rName );
55 bool getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic );
57 bool copyGraphic( const OUString& rName, const OUString& rPath );
59 private:
60 Reference< XStorage > mxStorage;
63 ButtonsImpl::ButtonsImpl( const OUString& rURL )
65 try
67 mxStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL( ZIP_STORAGE_FORMAT_STRING, rURL, ElementModes::READ );
69 catch( Exception& )
71 OSL_FAIL("sd::ButtonsImpl::ButtonsImpl(), exception caught!" );
75 Reference< XInputStream > ButtonsImpl::getInputStream( const OUString& rName )
77 Reference< XInputStream > xInputStream;
78 if( mxStorage.is() ) try
80 Reference< XStream > xStream( mxStorage->openStreamElement( rName, ElementModes::READ ) );
81 if( xStream.is() )
82 xInputStream = xStream->getInputStream();
84 catch( Exception& )
86 OSL_FAIL( "sd::ButtonsImpl::getInputStream(), exception caught!" );
88 return xInputStream;
91 bool ButtonsImpl::getGraphic( const Reference< XGraphicProvider >& xGraphicProvider, const OUString& rName, Graphic& rGraphic )
93 Reference< XInputStream > xInputStream( getInputStream( rName ) );
94 if( xInputStream.is() && xGraphicProvider.is() ) try
96 Sequence< PropertyValue > aMediaProperties( 1 );
97 aMediaProperties[0].Name = "InputStream";
98 aMediaProperties[0].Value <<= xInputStream;
99 Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties ) );
101 if( xGraphic.is() )
103 rGraphic = Graphic( xGraphic );
104 return true;
107 catch( Exception& )
109 OSL_FAIL( "sd::ButtonsImpl::getGraphic(), exception caught!" );
111 return false;
114 bool ButtonsImpl::copyGraphic( const OUString& rName, const OUString& rPath )
116 Reference< XInputStream > xInput( getInputStream( rName ) );
117 if( xInput.is() ) try
119 osl::File::remove( rPath );
120 osl::File aOutputFile( rPath );
121 if( aOutputFile.open( osl_File_OpenFlag_Write|osl_File_OpenFlag_Create ) == osl::FileBase::E_None )
123 Reference< XOutputStream > xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile ) );
124 comphelper::OStorageHelper::CopyInputToOutput( xInput, xOutput );
125 return true;
128 catch( Exception& )
130 OSL_FAIL( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
133 return false;
136 typedef std::vector< boost::shared_ptr< ButtonsImpl > > ButtonVector;
137 class ButtonSetImpl
139 public:
140 ButtonSetImpl();
142 int getCount() const;
144 bool getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage );
145 bool exportButton( int nSet, const OUString& rPath, const OUString& rName );
147 void scanForButtonSets( const OUString& rPath );
149 Reference< XGraphicProvider > getGraphicProvider();
151 ButtonVector maButtons;
152 Reference< XGraphicProvider > mxGraphicProvider;
155 ButtonSetImpl::ButtonSetImpl()
157 const OUString sSubPath( "/wizard/web/buttons" );
159 OUString sSharePath( SvtPathOptions().GetConfigPath() );
160 sSharePath += sSubPath;
161 scanForButtonSets( sSharePath );
163 OUString sUserPath( SvtPathOptions().GetUserConfigPath() );
164 sUserPath += sSubPath;
165 scanForButtonSets( sUserPath );
168 void ButtonSetImpl::scanForButtonSets( const OUString& rPath )
170 osl::Directory aDirectory( rPath );
171 if( aDirectory.open() == osl::FileBase::E_None )
173 osl::DirectoryItem aItem;
174 while( aDirectory.getNextItem( aItem, 2211 ) == osl::FileBase::E_None )
176 osl::FileStatus aStatus( osl_FileStatus_Mask_FileName|osl_FileStatus_Mask_FileURL );
177 if( aItem.getFileStatus( aStatus ) == osl::FileBase::E_None )
179 OUString sFileName( aStatus.getFileName() );
180 if( sFileName.endsWithIgnoreAsciiCase( ".zip" ) )
181 maButtons.push_back( boost::shared_ptr< ButtonsImpl >( new ButtonsImpl( aStatus.getFileURL() ) ) );
187 int ButtonSetImpl::getCount() const
189 return maButtons.size();
192 bool ButtonSetImpl::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
194 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
196 ButtonsImpl& rSet = *maButtons[nSet].get();
198 std::vector< Graphic > aGraphics;
200 ScopedVclPtrInstance< VirtualDevice > pDev;
201 pDev->SetMapMode(MapMode(MAP_PIXEL));
203 Size aSize;
204 std::vector< OUString >::const_iterator aIter( rButtons.begin() );
205 while( aIter != rButtons.end() )
207 Graphic aGraphic;
208 if( !rSet.getGraphic( getGraphicProvider(), (*aIter++), aGraphic ) )
209 return false;
211 aGraphics.push_back(aGraphic);
213 Size aGraphicSize( aGraphic.GetSizePixel( pDev ) );
214 aSize.Width() += aGraphicSize.Width();
216 if( aSize.Height() < aGraphicSize.Height() )
217 aSize.Height() = aGraphicSize.Height();
219 if( aIter != rButtons.end() )
220 aSize.Width() += 3;
223 pDev->SetOutputSizePixel( aSize );
225 Point aPos;
227 std::vector< Graphic >::iterator aGraphIter( aGraphics.begin() );
228 while( aGraphIter != aGraphics.end() )
230 Graphic aGraphic( (*aGraphIter++) );
232 aGraphic.Draw( pDev, aPos );
234 aPos.X() += aGraphic.GetSizePixel().Width() + 3;
237 rImage = Image( pDev->GetBitmapEx( Point(), aSize ) );
238 return true;
240 return false;
243 bool ButtonSetImpl::exportButton( int nSet, const OUString& rPath, const OUString& rName )
245 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
247 ButtonsImpl& rSet = *maButtons[nSet].get();
249 return rSet.copyGraphic( rName, rPath );
251 return false;
254 Reference< XGraphicProvider > ButtonSetImpl::getGraphicProvider()
256 if( !mxGraphicProvider.is() )
258 Reference< XComponentContext > xComponentContext = ::comphelper::getProcessComponentContext();
259 mxGraphicProvider = GraphicProvider::create(xComponentContext);
261 return mxGraphicProvider;
264 ButtonSet::ButtonSet()
265 : mpImpl( new ButtonSetImpl() )
269 ButtonSet::~ButtonSet()
271 delete mpImpl;
274 int ButtonSet::getCount() const
276 return mpImpl->getCount();
279 bool ButtonSet::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
281 return mpImpl->getPreview( nSet, rButtons, rImage );
284 bool ButtonSet::exportButton( int nSet, const OUString& rPath, const OUString& rName )
286 return mpImpl->exportButton( nSet, rPath, rName );
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */