1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: buttonset.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <com/sun/star/graphic/XGraphicProvider.hpp>
37 #include <osl/file.hxx>
38 #include <comphelper/storagehelper.hxx>
39 #include <comphelper/oslfile2streamwrap.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <tools/debug.hxx>
42 #include <vcl/graph.hxx>
43 #include <vcl/virdev.hxx>
44 #include <vcl/image.hxx>
45 #include <svtools/pathoptions.hxx>
47 #include <boost/shared_ptr.hpp>
49 #include "buttonset.hxx"
51 using ::rtl::OUString
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::graphic
;
54 using namespace ::com::sun::star::embed
;
55 using namespace ::com::sun::star::io
;
56 using namespace ::com::sun::star::beans
;
57 using namespace ::com::sun::star::lang
;
62 ButtonsImpl( const OUString
& rURL
);
64 Reference
< XInputStream
> getInputStream( const OUString
& rName
);
66 bool getGraphic( const Reference
< XGraphicProvider
>& xGraphicProvider
, const OUString
& rName
, Graphic
& rGraphic
);
68 bool copyGraphic( const OUString
& rName
, const OUString
& rPath
);
71 Reference
< XStorage
> mxStorage
;
74 ButtonsImpl::ButtonsImpl( const OUString
& rURL
)
78 mxStorage
= comphelper::OStorageHelper::GetStorageFromURL( rURL
, ElementModes::READ
);
82 DBG_ERROR("sd::ButtonsImpl::ButtonsImpl(), exception caught!" );
86 Reference
< XInputStream
> ButtonsImpl::getInputStream( const OUString
& rName
)
88 Reference
< XInputStream
> xInputStream
;
89 if( mxStorage
.is() ) try
91 Reference
< XStream
> xStream( mxStorage
->openStreamElement( rName
, ElementModes::READ
) );
93 xInputStream
= xStream
->getInputStream();
97 DBG_ERROR( "sd::ButtonsImpl::getInputStream(), exception caught!" );
102 bool ButtonsImpl::getGraphic( const Reference
< XGraphicProvider
>& xGraphicProvider
, const rtl::OUString
& rName
, Graphic
& rGraphic
)
104 Reference
< XInputStream
> xInputStream( getInputStream( rName
) );
105 if( xInputStream
.is() && xGraphicProvider
.is() ) try
107 Sequence
< PropertyValue
> aMediaProperties( 1 );
108 aMediaProperties
[0].Name
= ::rtl::OUString::createFromAscii( "InputStream" );
109 aMediaProperties
[0].Value
<<= xInputStream
;
110 Reference
< XGraphic
> xGraphic( xGraphicProvider
->queryGraphic( aMediaProperties
) );
114 rGraphic
= Graphic( xGraphic
);
120 DBG_ERROR( "sd::ButtonsImpl::getGraphic(), exception caught!" );
125 bool ButtonsImpl::copyGraphic( const OUString
& rName
, const OUString
& rPath
)
127 Reference
< XInputStream
> xInput( getInputStream( rName
) );
128 if( xInput
.is() ) try
130 osl::File::remove( rPath
);
131 osl::File
aOutputFile( rPath
);
132 if( aOutputFile
.open( OpenFlag_Write
|OpenFlag_Create
) == osl::FileBase::E_None
)
134 Reference
< XOutputStream
> xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile
) );
135 comphelper::OStorageHelper::CopyInputToOutput( xInput
, xOutput
);
141 DBG_ERROR( "sd::ButtonsImpl::copyGraphic(), exception caught!" );
147 typedef std::vector
< boost::shared_ptr
< ButtonsImpl
> > ButtonVector
;
153 int getCount() const;
155 bool getPreview( int nSet
, const std::vector
< rtl::OUString
>& rButtons
, Image
& rImage
);
156 bool exportButton( int nSet
, const rtl::OUString
& rPath
, const rtl::OUString
& rName
);
158 void scanForButtonSets( const OUString
& rPath
);
160 Reference
< XGraphicProvider
> getGraphicProvider();
162 ButtonVector maButtons
;
163 Reference
< XGraphicProvider
> mxGraphicProvider
;
166 ButtonSetImpl::ButtonSetImpl()
168 const OUString
sSubPath( RTL_CONSTASCII_USTRINGPARAM( "/wizard/web/buttons" ) );
170 OUString
sSharePath( SvtPathOptions().GetConfigPath() );
171 sSharePath
+= sSubPath
;
172 scanForButtonSets( sSharePath
);
174 OUString
sUserPath( SvtPathOptions().GetUserConfigPath() );
175 sUserPath
+= sSubPath
;
176 scanForButtonSets( sUserPath
);
179 void ButtonSetImpl::scanForButtonSets( const OUString
& rPath
)
181 OUString aSystemPath
;
182 osl::Directory
aDirectory( rPath
);
183 if( aDirectory
.open() == osl::FileBase::E_None
)
185 osl::DirectoryItem aItem
;
186 while( aDirectory
.getNextItem( aItem
, 2211 ) == osl::FileBase::E_None
)
188 osl::FileStatus
aStatus( FileStatusMask_FileName
|FileStatusMask_FileURL
);
189 if( aItem
.getFileStatus( aStatus
) == osl::FileBase::E_None
)
191 OUString
sFileName( aStatus
.getFileName() );
192 if( sFileName
.endsWithIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM(".zip" ) ) )
193 maButtons
.push_back( boost::shared_ptr
< ButtonsImpl
>( new ButtonsImpl( aStatus
.getFileURL() ) ) );
199 int ButtonSetImpl::getCount() const
201 return maButtons
.size();
204 bool ButtonSetImpl::getPreview( int nSet
, const std::vector
< rtl::OUString
>& rButtons
, Image
& rImage
)
206 if( (nSet
>= 0) && (nSet
< static_cast<int>(maButtons
.size())))
208 ButtonsImpl
& rSet
= *maButtons
[nSet
].get();
210 std::vector
< Graphic
> aGraphics
;
213 aDev
.SetMapMode(MapMode(MAP_PIXEL
));
216 std::vector
< rtl::OUString
>::const_iterator
aIter( rButtons
.begin() );
217 while( aIter
!= rButtons
.end() )
220 if( !rSet
.getGraphic( getGraphicProvider(), (*aIter
++), aGraphic
) )
223 aGraphics
.push_back(aGraphic
);
225 Size
aGraphicSize( aGraphic
.GetSizePixel( &aDev
) );
226 aSize
.Width() += aGraphicSize
.Width();
228 if( aSize
.Height() < aGraphicSize
.Height() )
229 aSize
.Height() = aGraphicSize
.Height();
231 if( aIter
!= rButtons
.end() )
235 aDev
.SetOutputSizePixel( aSize
);
239 std::vector
< Graphic
>::iterator
aGraphIter( aGraphics
.begin() );
240 while( aGraphIter
!= aGraphics
.end() )
242 Graphic
aGraphic( (*aGraphIter
++) );
244 aGraphic
.Draw( &aDev
, aPos
);
246 aPos
.X() += aGraphic
.GetSizePixel().Width() + 3;
249 rImage
= Image( aDev
.GetBitmapEx( Point(), aSize
) );
255 bool ButtonSetImpl::exportButton( int nSet
, const rtl::OUString
& rPath
, const rtl::OUString
& rName
)
257 if( (nSet
>= 0) && (nSet
< static_cast<int>(maButtons
.size())))
259 ButtonsImpl
& rSet
= *maButtons
[nSet
].get();
261 return rSet
.copyGraphic( rName
, rPath
);
266 Reference
< XGraphicProvider
> ButtonSetImpl::getGraphicProvider()
268 if( !mxGraphicProvider
.is() )
270 Reference
< XMultiServiceFactory
> xServiceManager( ::comphelper::getProcessServiceFactory() );
271 if( xServiceManager
.is() ) try
273 Reference
< XGraphicProvider
> xGraphProvider(
274 xServiceManager
->createInstance(
275 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ), UNO_QUERY_THROW
);
277 mxGraphicProvider
= xGraphProvider
;
281 DBG_ERROR("sd::ButtonSetImpl::getGraphicProvider(), could not get graphic provider!");
284 return mxGraphicProvider
;
288 ButtonSet::ButtonSet()
289 : mpImpl( new ButtonSetImpl() )
293 ButtonSet::~ButtonSet()
298 int ButtonSet::getCount() const
300 return mpImpl
->getCount();
303 bool ButtonSet::getPreview( int nSet
, const std::vector
< rtl::OUString
>& rButtons
, Image
& rImage
)
305 return mpImpl
->getPreview( nSet
, rButtons
, rImage
);
308 bool ButtonSet::exportButton( int nSet
, const rtl::OUString
& rPath
, const rtl::OUString
& rName
)
310 return mpImpl
->exportButton( nSet
, rPath
, rName
);