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