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 <o3tl/safeint.hxx>
29 #include <osl/file.hxx>
30 #include <comphelper/storagehelper.hxx>
31 #include <comphelper/oslfile2streamwrap.hxx>
32 #include <comphelper/processfactory.hxx>
33 #include <comphelper/propertyvalue.hxx>
34 #include <vcl/graph.hxx>
35 #include <vcl/virdev.hxx>
36 #include <vcl/image.hxx>
37 #include <unotools/pathoptions.hxx>
38 #include <comphelper/diagnose_ex.hxx>
42 #include "buttonset.hxx"
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::graphic
;
46 using namespace ::com::sun::star::embed
;
47 using namespace ::com::sun::star::io
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::lang
;
56 explicit ButtonsImpl( const OUString
& rURL
);
58 Reference
< XInputStream
> getInputStream( const OUString
& rName
);
60 bool getGraphic( const Reference
< XGraphicProvider
>& xGraphicProvider
, const OUString
& rName
, Graphic
& rGraphic
);
62 bool copyGraphic( const OUString
& rName
, const OUString
& rPath
);
65 Reference
< XStorage
> mxStorage
;
70 ButtonsImpl::ButtonsImpl( const OUString
& rURL
)
74 mxStorage
= comphelper::OStorageHelper::GetStorageOfFormatFromURL( ZIP_STORAGE_FORMAT_STRING
, rURL
, ElementModes::READ
);
78 TOOLS_WARN_EXCEPTION( "sd", "sd::ButtonsImpl::ButtonsImpl()" );
82 Reference
< XInputStream
> ButtonsImpl::getInputStream( const OUString
& rName
)
84 Reference
< XInputStream
> xInputStream
;
85 if( mxStorage
.is() ) try
87 Reference
< XStream
> xStream( mxStorage
->openStreamElement( rName
, ElementModes::READ
) );
89 xInputStream
= xStream
->getInputStream();
93 TOOLS_WARN_EXCEPTION( "sd", "sd::ButtonsImpl::getInputStream()" );
98 bool ButtonsImpl::getGraphic( const Reference
< XGraphicProvider
>& xGraphicProvider
, const OUString
& rName
, Graphic
& rGraphic
)
100 Reference
< XInputStream
> xInputStream( getInputStream( rName
) );
101 if( xInputStream
.is() && xGraphicProvider
.is() ) try
103 Sequence
< PropertyValue
> aMediaProperties
{ comphelper::makePropertyValue(
104 "InputStream", xInputStream
) };
105 Reference
< XGraphic
> xGraphic( xGraphicProvider
->queryGraphic( aMediaProperties
) );
109 rGraphic
= Graphic( xGraphic
);
115 TOOLS_WARN_EXCEPTION( "sd", "sd::ButtonsImpl::getGraphic()" );
120 bool ButtonsImpl::copyGraphic( const OUString
& rName
, const OUString
& rPath
)
122 Reference
< XInputStream
> xInput( getInputStream( rName
) );
123 if( xInput
.is() ) try
125 osl::File::remove( rPath
);
126 osl::File
aOutputFile( rPath
);
127 if( aOutputFile
.open( osl_File_OpenFlag_Write
|osl_File_OpenFlag_Create
) == osl::FileBase::E_None
)
129 Reference
< XOutputStream
> xOutput( new comphelper::OSLOutputStreamWrapper( aOutputFile
) );
130 comphelper::OStorageHelper::CopyInputToOutput( xInput
, xOutput
);
136 TOOLS_WARN_EXCEPTION( "sd", "sd::ButtonsImpl::copyGraphic()" );
147 int getCount() const;
149 bool getPreview( int nSet
, const std::vector
< OUString
>& rButtons
, Image
& rImage
);
150 bool exportButton( int nSet
, const OUString
& rPath
, const OUString
& rName
);
152 void scanForButtonSets( const OUString
& rPath
);
154 Reference
< XGraphicProvider
> const & getGraphicProvider();
156 std::vector
< std::shared_ptr
< ButtonsImpl
> > maButtons
;
157 Reference
< XGraphicProvider
> mxGraphicProvider
;
160 ButtonSetImpl::ButtonSetImpl()
162 static const char sSubPath
[] = "/wizard/web/buttons" ;
164 OUString sSharePath
= SvtPathOptions().GetConfigPath() +
166 scanForButtonSets( sSharePath
);
168 OUString sUserPath
= SvtPathOptions().GetUserConfigPath() +
170 scanForButtonSets( sUserPath
);
173 void ButtonSetImpl::scanForButtonSets( const OUString
& rPath
)
175 osl::Directory
aDirectory( rPath
);
176 if( aDirectory
.open() != osl::FileBase::E_None
)
179 osl::DirectoryItem aItem
;
180 while( aDirectory
.getNextItem( aItem
, 2211 ) == osl::FileBase::E_None
)
182 osl::FileStatus
aStatus( osl_FileStatus_Mask_FileName
|osl_FileStatus_Mask_FileURL
);
183 if( aItem
.getFileStatus( aStatus
) == osl::FileBase::E_None
)
185 OUString
sFileName( aStatus
.getFileName() );
186 if( sFileName
.endsWithIgnoreAsciiCase( ".zip" ) )
187 maButtons
.push_back( std::make_shared
< ButtonsImpl
>( aStatus
.getFileURL() ) );
192 int ButtonSetImpl::getCount() const
194 return maButtons
.size();
197 bool ButtonSetImpl::getPreview( int nSet
, const std::vector
< OUString
>& rButtons
, Image
& rImage
)
199 if( (nSet
>= 0) && (o3tl::make_unsigned(nSet
) < maButtons
.size()))
201 ButtonsImpl
& rSet
= *maButtons
[nSet
];
203 std::vector
< Graphic
> aGraphics
;
205 ScopedVclPtrInstance
< VirtualDevice
> pDev
;
206 pDev
->SetMapMode(MapMode(MapUnit::MapPixel
));
209 std::vector
< OUString
>::const_iterator
aIter( rButtons
.begin() );
210 while( aIter
!= rButtons
.end() )
213 if( !rSet
.getGraphic( getGraphicProvider(), (*aIter
++), aGraphic
) )
216 aGraphics
.push_back(aGraphic
);
218 Size
aGraphicSize( aGraphic
.GetSizePixel( pDev
) );
219 aSize
.AdjustWidth(aGraphicSize
.Width() );
221 if( aSize
.Height() < aGraphicSize
.Height() )
222 aSize
.setHeight( aGraphicSize
.Height() );
224 if( aIter
!= rButtons
.end() )
225 aSize
.AdjustWidth(3 );
228 pDev
->SetOutputSizePixel( aSize
);
232 for( const Graphic
& aGraphic
: aGraphics
)
234 aGraphic
.Draw(*pDev
, aPos
);
236 aPos
.AdjustX(aGraphic
.GetSizePixel().Width() + 3 );
239 rImage
= Image( pDev
->GetBitmapEx( Point(), aSize
) );
245 bool ButtonSetImpl::exportButton( int nSet
, const OUString
& rPath
, const OUString
& rName
)
247 if( (nSet
>= 0) && (o3tl::make_unsigned(nSet
) < maButtons
.size()))
249 ButtonsImpl
& rSet
= *maButtons
[nSet
];
251 return rSet
.copyGraphic( rName
, rPath
);
256 Reference
< XGraphicProvider
> const & ButtonSetImpl::getGraphicProvider()
258 if( !mxGraphicProvider
.is() )
260 Reference
< XComponentContext
> xComponentContext
= ::comphelper::getProcessComponentContext();
261 mxGraphicProvider
= GraphicProvider::create(xComponentContext
);
263 return mxGraphicProvider
;
266 ButtonSet::ButtonSet()
267 : mpImpl( new ButtonSetImpl() )
271 ButtonSet::~ButtonSet()
275 int ButtonSet::getCount() const
277 return mpImpl
->getCount();
280 bool ButtonSet::getPreview( int nSet
, const std::vector
< OUString
>& rButtons
, Image
& rImage
)
282 return mpImpl
->getPreview( nSet
, rButtons
, rImage
);
285 bool ButtonSet::exportButton( int nSet
, const OUString
& rPath
, const OUString
& rName
)
287 return mpImpl
->exportButton( nSet
, rPath
, rName
);
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */