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 <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>
30 #include <cppuhelper/exc_hlp.hxx>
31 #include <comphelper/anytostring.hxx>
32 #include <comphelper/processfactory.hxx>
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>
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
;
60 namespace drawing
= com::sun::star::drawing
;
62 void GraphicHelper::GetPreferedExtension( OUString
& rExtension
, const Graphic
& rGraphic
)
64 OUString aExtension
= "png";
65 switch( const_cast<Graphic
&>( rGraphic
).GetLink().GetType() )
67 case GFX_LINK_TYPE_NATIVE_GIF
:
70 case GFX_LINK_TYPE_NATIVE_TIF
:
73 case GFX_LINK_TYPE_NATIVE_WMF
:
76 case GFX_LINK_TYPE_NATIVE_MET
:
79 case GFX_LINK_TYPE_NATIVE_PCT
:
82 case GFX_LINK_TYPE_NATIVE_JPG
:
88 rExtension
= aExtension
;
91 OUString
GraphicHelper::ExportGraphic( const Graphic
& rGraphic
, const OUString
& rGraphicName
)
93 SvtPathOptions aPathOpt
;
94 OUString
sGraphicsPath( aPathOpt
.GetGraphicPath() );
96 FileDialogHelper
aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION
, 0 );
97 Reference
< XFilePicker
> xFilePicker
= aDialogHelper
.GetFilePicker();
100 aPath
.SetSmartURL( sGraphicsPath
);
102 // fish out the graphic's name
103 OUString aName
= rGraphicName
;
105 aDialogHelper
.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE
));
106 aDialogHelper
.SetDisplayDirectory( aPath
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
108 aURL
.SetSmartURL( aName
);
109 aDialogHelper
.SetFileName( aURL
.GetName() );
111 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
112 const sal_uInt16 nCount
= rGraphicFilter
.GetExportFormatCount();
114 OUString
aExtension( aURL
.GetExtension() );
115 if( aExtension
.isEmpty() )
117 GetPreferedExtension( aExtension
, rGraphic
);
120 aExtension
= aExtension
.toAsciiLowerCase();
121 sal_uInt16 nDefaultFilter
= USHRT_MAX
;
123 Reference
<XFilterManager
> xFilterManager(xFilePicker
, UNO_QUERY
);
125 for ( sal_uInt16 i
= 0; i
< nCount
; i
++ )
127 xFilterManager
->appendFilter( rGraphicFilter
.GetExportFormatName( i
), rGraphicFilter
.GetExportWildcard( i
) );
128 OUString aFormatShortName
= rGraphicFilter
.GetExportFormatShortName( i
);
129 if ( aFormatShortName
.equalsIgnoreAsciiCase( aExtension
) )
134 if ( USHRT_MAX
== nDefaultFilter
)
136 // "wrong" extension?
137 GetPreferedExtension( aExtension
, rGraphic
);
138 for ( sal_uInt16 i
= 0; i
< nCount
; ++i
)
139 if ( aExtension
== rGraphicFilter
.GetExportFormatShortName( i
).ToLowerAscii() )
146 if( USHRT_MAX
!= nDefaultFilter
)
148 xFilterManager
->setCurrentFilter( rGraphicFilter
.GetExportFormatName( nDefaultFilter
) ) ;
150 if( aDialogHelper
.Execute() == ERRCODE_NONE
)
152 OUString
sPath( xFilePicker
->getFiles().getConstArray()[0] );
153 // remember used path - please don't optimize away!
154 aPath
.SetSmartURL( sPath
);
155 sGraphicsPath
= aPath
.GetPath();
157 if( !rGraphicName
.isEmpty() &&
158 nDefaultFilter
== rGraphicFilter
.GetExportFormatNumber( xFilterManager
->getCurrentFilter()))
160 // try to save the original graphic
161 SfxMedium
aIn( rGraphicName
, STREAM_READ
| STREAM_NOCREATE
);
162 if( aIn
.GetInStream() && !aIn
.GetInStream()->GetError() )
164 SfxMedium
aOut( sPath
, STREAM_WRITE
| STREAM_SHARE_DENYNONE
);
165 if( aOut
.GetOutStream() && !aOut
.GetOutStream()->GetError())
167 *aOut
.GetOutStream() << *aIn
.GetInStream();
168 if ( 0 == aIn
.GetError() )
172 if ( 0 == aOut
.GetError() )
180 if ( !xFilterManager
->getCurrentFilter().isEmpty() && rGraphicFilter
.GetExportFormatCount() )
182 nFilter
= rGraphicFilter
.GetExportFormatNumber( xFilterManager
->getCurrentFilter() );
186 nFilter
= GRFILTER_FORMAT_DONTKNOW
;
188 OUString
aFilter( rGraphicFilter
.GetExportFormatShortName( nFilter
) );
190 String
aFilterString( aFilter
);
191 String
aPathString( sPath
);
192 XOutBitmap::WriteGraphic( rGraphic
, aPathString
, aFilterString
,
193 XOUTBMP_DONT_EXPAND_FILENAME
|
194 XOUTBMP_DONT_ADD_EXTENSION
|
195 XOUTBMP_USE_NATIVE_IF_POSSIBLE
);
202 void GraphicHelper::SaveShapeAsGraphic( const Reference
< drawing::XShape
>& xShape
)
206 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
207 Reference
< XPropertySet
> xShapeSet( xShape
, UNO_QUERY_THROW
);
211 SvtPathOptions aPathOpt
;
212 String
sGraphicPath( aPathOpt
.GetGraphicPath() );
214 FileDialogHelper
aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION
, 0 );
215 Reference
< XFilePicker
> xFilePicker
= aDialogHelper
.GetFilePicker();
217 aDialogHelper
.SetTitle( OUString( "Save as Picture" ) );
220 aPath
.SetSmartURL( sGraphicPath
);
221 xFilePicker
->setDisplayDirectory( aPath
.GetMainURL(INetURLObject::DECODE_TO_IURI
) );
223 // populate filter dialog filter list and select default filter to match graphic mime type
225 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
226 Reference
<XFilterManager
> xFilterManager( xFilePicker
, UNO_QUERY
);
227 OUString aDefaultFormatName
;
228 sal_uInt16 nCount
= rGraphicFilter
.GetExportFormatCount();
230 std::map
< OUString
, OUString
> aMimeTypeMap
;
232 for ( sal_uInt16 i
= 0; i
< nCount
; i
++ )
234 const OUString
aExportFormatName( rGraphicFilter
.GetExportFormatName( i
) );
235 const OUString
aFilterMimeType( rGraphicFilter
.GetExportFormatMediaType( i
) );
236 xFilterManager
->appendFilter( aExportFormatName
, rGraphicFilter
.GetExportWildcard( i
) );
237 aMimeTypeMap
[ aExportFormatName
] = aFilterMimeType
;
238 if( aMimeType
== aFilterMimeType
)
239 aDefaultFormatName
= aExportFormatName
;
242 if( aDefaultFormatName
.getLength() == 0 )
244 nCount
= rGraphicFilter
.GetImportFormatCount();
245 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
247 const OUString
aFilterMimeType( rGraphicFilter
.GetImportFormatMediaType( i
) );
248 if( aMimeType
== aFilterMimeType
)
250 aDefaultFormatName
= rGraphicFilter
.GetImportFormatName( i
);
251 xFilterManager
->appendFilter( aDefaultFormatName
, rGraphicFilter
.GetImportWildcard( i
) );
252 aMimeTypeMap
[ aDefaultFormatName
] = aFilterMimeType
;
258 if( aDefaultFormatName
.getLength() == 0 )
259 aDefaultFormatName
= OUString( "PNG - Portable Network Graphic" );
261 xFilterManager
->setCurrentFilter( aDefaultFormatName
);
265 if( aDialogHelper
.Execute() == ERRCODE_NONE
)
267 OUString
sPath( xFilePicker
->getFiles().getConstArray()[0] );
268 OUString
aExportMimeType( aMimeTypeMap
[xFilterManager
->getCurrentFilter()] );
270 Reference
< XInputStream
> xGraphStream
;
271 if( aMimeType
== aExportMimeType
)
273 xShapeSet
->getPropertyValue( OUString( "GraphicStream" ) ) >>= xGraphStream
;
276 if( xGraphStream
.is() )
278 Reference
<XSimpleFileAccess3
> xFileAccess
= SimpleFileAccess::create( xContext
);
279 xFileAccess
->writeFile( sPath
, xGraphStream
);
283 Reference
<css::drawing::XGraphicExportFilter
> xGraphicExporter
= css::drawing::GraphicExportFilter::create( xContext
);
285 Sequence
<PropertyValue
> aDescriptor( 2 );
286 aDescriptor
[0].Name
= OUString("MediaType");
287 aDescriptor
[0].Value
<<= aExportMimeType
;
288 aDescriptor
[1].Name
= OUString("URL");
289 aDescriptor
[1].Value
<<= sPath
;
291 Reference
< XComponent
> xSourceDocument
= Reference
< XComponent
>( xShape
, UNO_QUERY_THROW
);
292 if ( xSourceDocument
.is() )
294 xGraphicExporter
->setSourceDocument( xSourceDocument
);
295 xGraphicExporter
->filter( aDescriptor
);
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */