Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <unotools/pathoptions.hxx>
21 : #include <vcl/graphicfilter.hxx>
22 : #include <sfx2/docfile.hxx>
23 : #include <sfx2/filedlghelper.hxx>
24 : #include <svx/xoutbmp.hxx>
25 : #include <svx/dialmgr.hxx>
26 : #include <svx/graphichelper.hxx>
27 : #include <svx/dialogs.hrc>
28 :
29 : #include <cppuhelper/exc_hlp.hxx>
30 : #include <comphelper/anytostring.hxx>
31 : #include <comphelper/processfactory.hxx>
32 :
33 : #include <com/sun/star/beans/PropertyValues.hpp>
34 : #include <com/sun/star/beans/PropertyValue.hpp>
35 : #include <com/sun/star/document/XExporter.hpp>
36 : #include <com/sun/star/document/XFilter.hpp>
37 : #include <com/sun/star/drawing/GraphicExportFilter.hpp>
38 : #include <com/sun/star/graphic/XGraphicProvider.hpp>
39 : #include <com/sun/star/graphic/GraphicType.hpp>
40 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 : #include <com/sun/star/lang/XComponent.hpp>
42 : #include <com/sun/star/io/XInputStream.hpp>
43 : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
44 : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
45 : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
46 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
47 :
48 : using namespace css::uno;
49 : using namespace css::lang;
50 : using namespace css::graphic;
51 : using namespace css::ucb;
52 : using namespace css::beans;
53 : using namespace css::io;
54 : using namespace css::document;
55 : using namespace css::ui::dialogs;
56 :
57 : using namespace sfx2;
58 :
59 : namespace drawing = com::sun::star::drawing;
60 :
61 0 : void GraphicHelper::GetPreferredExtension( OUString& rExtension, const Graphic& rGraphic )
62 : {
63 0 : OUString aExtension = "png";
64 0 : switch( const_cast<Graphic&>( rGraphic ).GetLink().GetType() )
65 : {
66 : case GFX_LINK_TYPE_NATIVE_GIF:
67 0 : aExtension = "gif";
68 0 : break;
69 : case GFX_LINK_TYPE_NATIVE_TIF:
70 0 : aExtension = "tif";
71 0 : break;
72 : case GFX_LINK_TYPE_NATIVE_WMF:
73 0 : aExtension = "wmf";
74 0 : break;
75 : case GFX_LINK_TYPE_NATIVE_MET:
76 0 : aExtension = "met";
77 0 : break;
78 : case GFX_LINK_TYPE_NATIVE_PCT:
79 0 : aExtension = "pct";
80 0 : break;
81 : case GFX_LINK_TYPE_NATIVE_JPG:
82 0 : aExtension = "jpg";
83 0 : break;
84 : case GFX_LINK_TYPE_NATIVE_BMP:
85 0 : aExtension = "bmp";
86 0 : break;
87 : case GFX_LINK_TYPE_NATIVE_SVG:
88 0 : aExtension = "svg";
89 0 : break;
90 : default:
91 0 : break;
92 : }
93 0 : rExtension = aExtension;
94 0 : }
95 :
96 0 : OUString GraphicHelper::ExportGraphic( const Graphic& rGraphic, const OUString& rGraphicName )
97 : {
98 0 : SvtPathOptions aPathOpt;
99 0 : OUString sGraphicsPath( aPathOpt.GetGraphicPath() );
100 :
101 0 : FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
102 0 : Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
103 :
104 0 : INetURLObject aPath;
105 0 : aPath.SetSmartURL( sGraphicsPath );
106 :
107 : // fish out the graphic's name
108 0 : OUString aName = rGraphicName;
109 :
110 0 : aDialogHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE));
111 0 : aDialogHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
112 0 : INetURLObject aURL;
113 0 : aURL.SetSmartURL( aName );
114 0 : aDialogHelper.SetFileName( aURL.GetName() );
115 :
116 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
117 0 : const sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
118 :
119 0 : OUString aExtension( aURL.GetExtension() );
120 0 : if( aExtension.isEmpty() )
121 : {
122 0 : GetPreferredExtension( aExtension, rGraphic );
123 : }
124 :
125 0 : aExtension = aExtension.toAsciiLowerCase();
126 0 : sal_uInt16 nDefaultFilter = USHRT_MAX;
127 :
128 0 : Reference<XFilterManager> xFilterManager(xFilePicker, UNO_QUERY);
129 :
130 0 : for ( sal_uInt16 i = 0; i < nCount; i++ )
131 : {
132 0 : xFilterManager->appendFilter( rGraphicFilter.GetExportFormatName( i ), rGraphicFilter.GetExportWildcard( i ) );
133 0 : OUString aFormatShortName = rGraphicFilter.GetExportFormatShortName( i );
134 0 : if ( aFormatShortName.equalsIgnoreAsciiCase( aExtension ) )
135 : {
136 0 : nDefaultFilter = i;
137 : }
138 0 : }
139 0 : if ( USHRT_MAX == nDefaultFilter )
140 : {
141 : // "wrong" extension?
142 0 : GetPreferredExtension( aExtension, rGraphic );
143 0 : for ( sal_uInt16 i = 0; i < nCount; ++i )
144 0 : if ( aExtension == rGraphicFilter.GetExportFormatShortName( i ).toAsciiLowerCase() )
145 : {
146 0 : nDefaultFilter = i;
147 0 : break;
148 : }
149 : }
150 :
151 0 : if( USHRT_MAX != nDefaultFilter )
152 : {
153 0 : xFilterManager->setCurrentFilter( rGraphicFilter.GetExportFormatName( nDefaultFilter ) ) ;
154 :
155 0 : if( aDialogHelper.Execute() == ERRCODE_NONE )
156 : {
157 0 : OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
158 : // remember used path - please don't optimize away!
159 0 : aPath.SetSmartURL( sPath);
160 0 : sGraphicsPath = aPath.GetPath();
161 :
162 0 : if( !rGraphicName.isEmpty() &&
163 0 : nDefaultFilter == rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter()))
164 : {
165 : // try to save the original graphic
166 0 : SfxMedium aIn( rGraphicName, STREAM_READ | STREAM_NOCREATE );
167 0 : if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
168 : {
169 0 : SfxMedium aOut( sPath, STREAM_WRITE | STREAM_SHARE_DENYNONE);
170 0 : if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
171 : {
172 0 : aOut.GetOutStream()->WriteStream( *aIn.GetInStream() );
173 0 : if ( 0 == aIn.GetError() )
174 : {
175 0 : aOut.Close();
176 0 : aOut.Commit();
177 0 : if ( 0 == aOut.GetError() )
178 0 : return sPath;
179 : }
180 0 : }
181 0 : }
182 : }
183 :
184 : sal_uInt16 nFilter;
185 0 : if ( !xFilterManager->getCurrentFilter().isEmpty() && rGraphicFilter.GetExportFormatCount() )
186 : {
187 0 : nFilter = rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter() );
188 : }
189 : else
190 : {
191 0 : nFilter = GRFILTER_FORMAT_DONTKNOW;
192 : }
193 0 : OUString aFilter( rGraphicFilter.GetExportFormatShortName( nFilter ) );
194 :
195 : XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
196 : XOUTBMP_DONT_EXPAND_FILENAME |
197 : XOUTBMP_DONT_ADD_EXTENSION |
198 0 : XOUTBMP_USE_NATIVE_IF_POSSIBLE );
199 0 : return sPath;
200 : }
201 : }
202 0 : return OUString();
203 : }
204 :
205 0 : void GraphicHelper::SaveShapeAsGraphic( const Reference< drawing::XShape >& xShape )
206 : {
207 : try
208 : {
209 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
210 0 : Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW );
211 :
212 0 : OUString aMimeType;
213 :
214 0 : SvtPathOptions aPathOpt;
215 0 : OUString sGraphicPath( aPathOpt.GetGraphicPath() );
216 :
217 0 : FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
218 0 : Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
219 :
220 0 : aDialogHelper.SetTitle( "Save as Picture" );
221 :
222 0 : INetURLObject aPath;
223 0 : aPath.SetSmartURL( sGraphicPath );
224 0 : xFilePicker->setDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
225 :
226 : // populate filter dialog filter list and select default filter to match graphic mime type
227 :
228 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
229 0 : Reference<XFilterManager> xFilterManager( xFilePicker, UNO_QUERY );
230 0 : OUString aDefaultFormatName;
231 0 : sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
232 :
233 0 : std::map< OUString, OUString > aMimeTypeMap;
234 :
235 0 : for ( sal_uInt16 i = 0; i < nCount; i++ )
236 : {
237 0 : const OUString aExportFormatName( rGraphicFilter.GetExportFormatName( i ) );
238 0 : const OUString aFilterMimeType( rGraphicFilter.GetExportFormatMediaType( i ) );
239 0 : xFilterManager->appendFilter( aExportFormatName, rGraphicFilter.GetExportWildcard( i ) );
240 0 : aMimeTypeMap[ aExportFormatName ] = aFilterMimeType;
241 0 : if( aMimeType == aFilterMimeType )
242 0 : aDefaultFormatName = aExportFormatName;
243 0 : }
244 :
245 0 : if( aDefaultFormatName.getLength() == 0 )
246 : {
247 0 : nCount = rGraphicFilter.GetImportFormatCount();
248 0 : for( sal_uInt16 i = 0; i < nCount; i++ )
249 : {
250 0 : const OUString aFilterMimeType( rGraphicFilter.GetImportFormatMediaType( i ) );
251 0 : if( aMimeType == aFilterMimeType )
252 : {
253 0 : aDefaultFormatName = rGraphicFilter.GetImportFormatName( i );
254 0 : xFilterManager->appendFilter( aDefaultFormatName, rGraphicFilter.GetImportWildcard( i ) );
255 0 : aMimeTypeMap[ aDefaultFormatName ] = aFilterMimeType;
256 0 : break;
257 : }
258 0 : }
259 : }
260 :
261 0 : if( aDefaultFormatName.getLength() == 0 )
262 0 : aDefaultFormatName = "PNG - Portable Network Graphic";
263 :
264 0 : xFilterManager->setCurrentFilter( aDefaultFormatName );
265 :
266 : // execute dialog
267 :
268 0 : if( aDialogHelper.Execute() == ERRCODE_NONE )
269 : {
270 0 : OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
271 0 : OUString aExportMimeType( aMimeTypeMap[xFilterManager->getCurrentFilter()] );
272 :
273 0 : Reference< XInputStream > xGraphStream;
274 0 : if( aMimeType == aExportMimeType )
275 : {
276 0 : xShapeSet->getPropertyValue("GraphicStream") >>= xGraphStream;
277 : }
278 :
279 0 : if( xGraphStream.is() )
280 : {
281 0 : Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create( xContext );
282 0 : xFileAccess->writeFile( sPath, xGraphStream );
283 : }
284 : else
285 : {
286 0 : Reference<css::drawing::XGraphicExportFilter> xGraphicExporter = css::drawing::GraphicExportFilter::create( xContext );
287 :
288 0 : Sequence<PropertyValue> aDescriptor( 2 );
289 0 : aDescriptor[0].Name = "MediaType";
290 0 : aDescriptor[0].Value <<= aExportMimeType;
291 0 : aDescriptor[1].Name = "URL";
292 0 : aDescriptor[1].Value <<= sPath;
293 :
294 0 : Reference< XComponent > xSourceDocument = Reference< XComponent >( xShape, UNO_QUERY_THROW );
295 0 : if ( xSourceDocument.is() )
296 : {
297 0 : xGraphicExporter->setSourceDocument( xSourceDocument );
298 0 : xGraphicExporter->filter( aDescriptor );
299 0 : }
300 0 : }
301 0 : }
302 : }
303 0 : catch( Exception& )
304 : {
305 : }
306 0 : }
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|