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 <svx/graphichelper.hxx>
22 : #include <unotools/pathoptions.hxx>
23 : #include <svtools/filter.hxx>
24 : #include <sfx2/docfile.hxx>
25 : #include <svx/xoutbmp.hxx>
26 : #include <sfx2/filedlghelper.hxx>
27 : #include <svx/dialmgr.hxx>
28 : #include <svx/dialogs.hrc>
29 :
30 : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
31 : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
32 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 :
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::ui::dialogs;
36 : using namespace ::sfx2;
37 :
38 0 : void GraphicHelper::GetPreferedExtension( String &rExtension, const Graphic &rGraphic )
39 : {
40 : // propose the "best" filter using the native-info, if applicable
41 0 : const sal_Char* pExtension = "png";
42 0 : switch( const_cast<Graphic&>( rGraphic ).GetLink().GetType() )
43 : {
44 : case GFX_LINK_TYPE_NATIVE_GIF:
45 0 : pExtension = "gif";
46 0 : break;
47 : case GFX_LINK_TYPE_NATIVE_TIF:
48 0 : pExtension = "tif";
49 0 : break;
50 : case GFX_LINK_TYPE_NATIVE_WMF:
51 0 : pExtension = "wmf";
52 0 : break;
53 : case GFX_LINK_TYPE_NATIVE_MET:
54 0 : pExtension = "met";
55 0 : break;
56 : case GFX_LINK_TYPE_NATIVE_PCT:
57 0 : pExtension = "pct";
58 0 : break;
59 : case GFX_LINK_TYPE_NATIVE_JPG:
60 0 : pExtension = "jpg";
61 0 : break;
62 : default:
63 0 : break;
64 : }
65 0 : rExtension.AssignAscii( pExtension );
66 0 : }
67 :
68 0 : String GraphicHelper::ExportGraphic( const Graphic &rGraphic, const String &rGrfName )
69 : {
70 0 : SvtPathOptions aPathOpt;
71 0 : String sGrfPath( aPathOpt.GetGraphicPath() );
72 :
73 0 : FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
74 0 : Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
75 :
76 0 : INetURLObject aPath;
77 0 : aPath.SetSmartURL( sGrfPath );
78 :
79 : // fish out the graphic's name
80 0 : String aName = rGrfName;
81 :
82 0 : aDlgHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE));
83 0 : aDlgHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
84 0 : INetURLObject aURL;
85 0 : aURL.SetSmartURL( aName );
86 0 : aDlgHelper.SetFileName( aURL.GetName() );
87 :
88 0 : GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
89 0 : const sal_uInt16 nCount = rGF.GetExportFormatCount();
90 :
91 0 : String aExt( aURL.GetExtension() );
92 0 : if( !aExt.Len() )
93 0 : GetPreferedExtension( aExt, rGraphic );
94 :
95 0 : aExt.ToLowerAscii();
96 0 : sal_uInt16 nDfltFilter = USHRT_MAX;
97 :
98 0 : Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
99 :
100 0 : for ( sal_uInt16 i = 0; i < nCount; i++ )
101 : {
102 0 : xFltMgr->appendFilter( rGF.GetExportFormatName( i ), rGF.GetExportWildcard( i ) );
103 0 : if ( COMPARE_EQUAL == aExt.CompareIgnoreCaseToAscii(rGF.GetExportFormatShortName( i ).ToLowerAscii() ))
104 0 : nDfltFilter = i;
105 : }
106 0 : if ( USHRT_MAX == nDfltFilter )
107 : {
108 : // "wrong" extension?
109 0 : GetPreferedExtension( aExt, rGraphic );
110 0 : for ( sal_uInt16 i = 0; i < nCount; ++i )
111 0 : if ( aExt == rGF.GetExportFormatShortName( i ).ToLowerAscii() )
112 : {
113 0 : nDfltFilter = i;
114 0 : break;
115 : }
116 : }
117 :
118 0 : if( USHRT_MAX != nDfltFilter )
119 : {
120 0 : xFltMgr->setCurrentFilter( rGF.GetExportFormatName( nDfltFilter ) ) ;
121 :
122 0 : if( aDlgHelper.Execute() == ERRCODE_NONE )
123 : {
124 0 : String sPath( xFP->getFiles().getConstArray()[0] );
125 : // remember used path - please don't optimize away!
126 0 : aPath.SetSmartURL( sPath);
127 0 : sGrfPath = aPath.GetPath();
128 :
129 0 : if( rGrfName.Len() &&
130 0 : nDfltFilter == rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter()))
131 : {
132 : // try to save the original graphic
133 0 : SfxMedium aIn( rGrfName, STREAM_READ | STREAM_NOCREATE );
134 0 : if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
135 : {
136 0 : SfxMedium aOut( sPath, STREAM_WRITE | STREAM_SHARE_DENYNONE);
137 0 : if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
138 : {
139 0 : *aOut.GetOutStream() << *aIn.GetInStream();
140 0 : if ( 0 == aIn.GetError() )
141 : {
142 0 : aOut.Close();
143 0 : aOut.Commit();
144 0 : if ( 0 == aOut.GetError() )
145 0 : return sPath;
146 : }
147 0 : }
148 0 : }
149 : }
150 :
151 : sal_uInt16 nFilter;
152 0 : if ( !xFltMgr->getCurrentFilter().isEmpty() && rGF.GetExportFormatCount() )
153 0 : nFilter = rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter() );
154 : else
155 0 : nFilter = GRFILTER_FORMAT_DONTKNOW;
156 0 : String aFilter( rGF.GetExportFormatShortName( nFilter ) );
157 : XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
158 : XOUTBMP_DONT_EXPAND_FILENAME|
159 : XOUTBMP_DONT_ADD_EXTENSION|
160 0 : XOUTBMP_USE_NATIVE_IF_POSSIBLE );
161 0 : return sPath;
162 : }
163 : }
164 0 : return String();
165 : }
166 :
167 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|