1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
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
);
60 Reference
< XStorage
> mxStorage
;
63 ButtonsImpl::ButtonsImpl( const OUString
& rURL
)
67 mxStorage
= comphelper::OStorageHelper::GetStorageOfFormatFromURL( ZIP_STORAGE_FORMAT_STRING
, rURL
, ElementModes::READ
);
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
) );
82 xInputStream
= xStream
->getInputStream();
86 OSL_FAIL( "sd::ButtonsImpl::getInputStream(), exception caught!" );
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
) );
103 rGraphic
= Graphic( xGraphic
);
109 OSL_FAIL( "sd::ButtonsImpl::getGraphic(), exception caught!" );
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
);
130 OSL_FAIL( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
136 typedef std::vector
< boost::shared_ptr
< ButtonsImpl
> > ButtonVector
;
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
));
204 std::vector
< OUString
>::const_iterator
aIter( rButtons
.begin() );
205 while( aIter
!= rButtons
.end() )
208 if( !rSet
.getGraphic( getGraphicProvider(), (*aIter
++), aGraphic
) )
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() )
223 pDev
->SetOutputSizePixel( aSize
);
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
) );
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
);
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()
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: */