bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / filter / html / buttonset.cxx
blob50ea0d7c251a6164fb0e5d54c1e94fa86c786365
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 <memory>
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 explicit 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 class ButtonSetImpl
138 public:
139 ButtonSetImpl();
141 int getCount() const;
143 bool getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage );
144 bool exportButton( int nSet, const OUString& rPath, const OUString& rName );
146 void scanForButtonSets( const OUString& rPath );
148 Reference< XGraphicProvider > const & getGraphicProvider();
150 std::vector< std::shared_ptr< ButtonsImpl > > maButtons;
151 Reference< XGraphicProvider > mxGraphicProvider;
154 ButtonSetImpl::ButtonSetImpl()
156 const OUString sSubPath( "/wizard/web/buttons" );
158 OUString sSharePath( SvtPathOptions().GetConfigPath() );
159 sSharePath += sSubPath;
160 scanForButtonSets( sSharePath );
162 OUString sUserPath( SvtPathOptions().GetUserConfigPath() );
163 sUserPath += sSubPath;
164 scanForButtonSets( sUserPath );
167 void ButtonSetImpl::scanForButtonSets( const OUString& rPath )
169 osl::Directory aDirectory( rPath );
170 if( aDirectory.open() != osl::FileBase::E_None )
171 return;
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( std::make_shared< ButtonsImpl >( aStatus.getFileURL() ) );
186 int ButtonSetImpl::getCount() const
188 return maButtons.size();
191 bool ButtonSetImpl::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
193 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
195 ButtonsImpl& rSet = *maButtons[nSet].get();
197 std::vector< Graphic > aGraphics;
199 ScopedVclPtrInstance< VirtualDevice > pDev;
200 pDev->SetMapMode(MapMode(MapUnit::MapPixel));
202 Size aSize;
203 std::vector< OUString >::const_iterator aIter( rButtons.begin() );
204 while( aIter != rButtons.end() )
206 Graphic aGraphic;
207 if( !rSet.getGraphic( getGraphicProvider(), (*aIter++), aGraphic ) )
208 return false;
210 aGraphics.push_back(aGraphic);
212 Size aGraphicSize( aGraphic.GetSizePixel( pDev ) );
213 aSize.AdjustWidth(aGraphicSize.Width() );
215 if( aSize.Height() < aGraphicSize.Height() )
216 aSize.setHeight( aGraphicSize.Height() );
218 if( aIter != rButtons.end() )
219 aSize.AdjustWidth(3 );
222 pDev->SetOutputSizePixel( aSize );
224 Point aPos;
226 for( Graphic& aGraphic : aGraphics )
228 aGraphic.Draw( pDev, aPos );
230 aPos.AdjustX(aGraphic.GetSizePixel().Width() + 3 );
233 rImage = Image( pDev->GetBitmapEx( Point(), aSize ) );
234 return true;
236 return false;
239 bool ButtonSetImpl::exportButton( int nSet, const OUString& rPath, const OUString& rName )
241 if( (nSet >= 0) && (nSet < static_cast<int>(maButtons.size())))
243 ButtonsImpl& rSet = *maButtons[nSet].get();
245 return rSet.copyGraphic( rName, rPath );
247 return false;
250 Reference< XGraphicProvider > const & ButtonSetImpl::getGraphicProvider()
252 if( !mxGraphicProvider.is() )
254 Reference< XComponentContext > xComponentContext = ::comphelper::getProcessComponentContext();
255 mxGraphicProvider = GraphicProvider::create(xComponentContext);
257 return mxGraphicProvider;
260 ButtonSet::ButtonSet()
261 : mpImpl( new ButtonSetImpl() )
265 ButtonSet::~ButtonSet()
269 int ButtonSet::getCount() const
271 return mpImpl->getCount();
274 bool ButtonSet::getPreview( int nSet, const std::vector< OUString >& rButtons, Image& rImage )
276 return mpImpl->getPreview( nSet, rButtons, rImage );
279 bool ButtonSet::exportButton( int nSet, const OUString& rPath, const OUString& rName )
281 return mpImpl->exportButton( nSet, rPath, rName );
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */