bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / core / graphichelper.cxx
blob96f6788885a178a3bbd4719fad83aa93b404fb3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
29 #include <cppuhelper/exc_hlp.hxx>
30 #include <comphelper/anytostring.hxx>
31 #include <comphelper/processfactory.hxx>
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>
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;
57 using namespace sfx2;
59 namespace drawing = com::sun::star::drawing;
61 void GraphicHelper::GetPreferredExtension( OUString& rExtension, const Graphic& rGraphic )
63 OUString aExtension = "png";
64 switch( rGraphic.GetLink().GetType() )
66 case GFX_LINK_TYPE_NATIVE_GIF:
67 aExtension = "gif";
68 break;
69 case GFX_LINK_TYPE_NATIVE_TIF:
70 aExtension = "tif";
71 break;
72 case GFX_LINK_TYPE_NATIVE_WMF:
73 aExtension = "wmf";
74 break;
75 case GFX_LINK_TYPE_NATIVE_MET:
76 aExtension = "met";
77 break;
78 case GFX_LINK_TYPE_NATIVE_PCT:
79 aExtension = "pct";
80 break;
81 case GFX_LINK_TYPE_NATIVE_JPG:
82 aExtension = "jpg";
83 break;
84 case GFX_LINK_TYPE_NATIVE_BMP:
85 aExtension = "bmp";
86 break;
87 case GFX_LINK_TYPE_NATIVE_SVG:
88 aExtension = "svg";
89 break;
90 default:
91 break;
93 rExtension = aExtension;
96 OUString GraphicHelper::ExportGraphic( const Graphic& rGraphic, const OUString& rGraphicName )
98 SvtPathOptions aPathOpt;
99 OUString sGraphicsPath( aPathOpt.GetGraphicPath() );
101 FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
102 Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
104 INetURLObject aPath;
105 aPath.SetSmartURL( sGraphicsPath );
107 // fish out the graphic's name
108 OUString aName = rGraphicName;
110 aDialogHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE));
111 aDialogHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
112 INetURLObject aURL;
113 aURL.SetSmartURL( aName );
114 aDialogHelper.SetFileName( aURL.GetName() );
116 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
117 const sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
119 OUString aExtension( aURL.GetExtension() );
120 if( aExtension.isEmpty() )
122 GetPreferredExtension( aExtension, rGraphic );
125 aExtension = aExtension.toAsciiLowerCase();
126 sal_uInt16 nDefaultFilter = USHRT_MAX;
128 Reference<XFilterManager> xFilterManager(xFilePicker, UNO_QUERY);
130 for ( sal_uInt16 i = 0; i < nCount; i++ )
132 xFilterManager->appendFilter( rGraphicFilter.GetExportFormatName( i ), rGraphicFilter.GetExportWildcard( i ) );
133 OUString aFormatShortName = rGraphicFilter.GetExportFormatShortName( i );
134 if ( aFormatShortName.equalsIgnoreAsciiCase( aExtension ) )
136 nDefaultFilter = i;
139 if ( USHRT_MAX == nDefaultFilter )
141 // "wrong" extension?
142 GetPreferredExtension( aExtension, rGraphic );
143 for ( sal_uInt16 i = 0; i < nCount; ++i )
144 if ( aExtension == rGraphicFilter.GetExportFormatShortName( i ).toAsciiLowerCase() )
146 nDefaultFilter = i;
147 break;
151 if( USHRT_MAX != nDefaultFilter )
153 xFilterManager->setCurrentFilter( rGraphicFilter.GetExportFormatName( nDefaultFilter ) ) ;
155 if( aDialogHelper.Execute() == ERRCODE_NONE )
157 OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
158 // remember used path - please don't optimize away!
159 aPath.SetSmartURL( sPath);
160 sGraphicsPath = aPath.GetPath();
162 if( !rGraphicName.isEmpty() &&
163 nDefaultFilter == rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter()))
165 // try to save the original graphic
166 SfxMedium aIn( rGraphicName, StreamMode::READ | StreamMode::NOCREATE );
167 if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
169 SfxMedium aOut( sPath, StreamMode::WRITE | StreamMode::SHARE_DENYNONE);
170 if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
172 aOut.GetOutStream()->WriteStream( *aIn.GetInStream() );
173 if ( 0 == aIn.GetError() )
175 aOut.Close();
176 aOut.Commit();
177 if ( 0 == aOut.GetError() )
178 return sPath;
184 sal_uInt16 nFilter;
185 if ( !xFilterManager->getCurrentFilter().isEmpty() && rGraphicFilter.GetExportFormatCount() )
187 nFilter = rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter() );
189 else
191 nFilter = GRFILTER_FORMAT_DONTKNOW;
193 OUString aFilter( rGraphicFilter.GetExportFormatShortName( nFilter ) );
195 XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
196 XOUTBMP_DONT_EXPAND_FILENAME |
197 XOUTBMP_DONT_ADD_EXTENSION |
198 XOUTBMP_USE_NATIVE_IF_POSSIBLE );
199 return sPath;
202 return OUString();
205 void GraphicHelper::SaveShapeAsGraphic( const Reference< drawing::XShape >& xShape )
209 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
210 Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW );
212 SvtPathOptions aPathOpt;
213 OUString sGraphicPath( aPathOpt.GetGraphicPath() );
215 FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
216 Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker();
218 aDialogHelper.SetTitle( "Save as Image" );
220 INetURLObject aPath;
221 aPath.SetSmartURL( sGraphicPath );
222 xFilePicker->setDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
224 // populate filter dialog filter list and select default filter to match graphic mime type
226 GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
227 Reference<XFilterManager> xFilterManager( xFilePicker, UNO_QUERY );
228 const OUString aDefaultMimeType("image/png");
229 OUString aDefaultFormatName;
230 sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount();
232 std::map< OUString, OUString > aMimeTypeMap;
234 for ( sal_uInt16 i = 0; i < nCount; i++ )
236 const OUString aExportFormatName( rGraphicFilter.GetExportFormatName( i ) );
237 const OUString aFilterMimeType( rGraphicFilter.GetExportFormatMediaType( i ) );
238 xFilterManager->appendFilter( aExportFormatName, rGraphicFilter.GetExportWildcard( i ) );
239 aMimeTypeMap[ aExportFormatName ] = aFilterMimeType;
240 if( aDefaultMimeType == aFilterMimeType )
241 aDefaultFormatName = aExportFormatName;
244 if( !aDefaultFormatName.isEmpty() )
245 xFilterManager->setCurrentFilter( aDefaultFormatName );
247 // execute dialog
249 if( aDialogHelper.Execute() == ERRCODE_NONE )
251 OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
252 OUString aExportMimeType( aMimeTypeMap[xFilterManager->getCurrentFilter()] );
254 Reference< XInputStream > xGraphStream;
256 if( xGraphStream.is() )
258 Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create( xContext );
259 xFileAccess->writeFile( sPath, xGraphStream );
261 else
263 Reference<css::drawing::XGraphicExportFilter> xGraphicExporter = css::drawing::GraphicExportFilter::create( xContext );
265 Sequence<PropertyValue> aDescriptor( 2 );
266 aDescriptor[0].Name = "MediaType";
267 aDescriptor[0].Value <<= aExportMimeType;
268 aDescriptor[1].Name = "URL";
269 aDescriptor[1].Value <<= sPath;
271 Reference< XComponent > xSourceDocument = Reference< XComponent >( xShape, UNO_QUERY_THROW );
272 if ( xSourceDocument.is() )
274 xGraphicExporter->setSourceDocument( xSourceDocument );
275 xGraphicExporter->filter( aDescriptor );
280 catch( Exception& )
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */