xref: /trunk/main/sfx2/source/appl/opengrf.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 
31 #include <tools/urlobj.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <com/sun/star/uno/Reference.h>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
36 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
37 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
39 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
40 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
41 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
43 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
44 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
45 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
46 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
47 #include <svl/urihelper.hxx>
48 #include <unotools/ucbstreamhelper.hxx>
49 #include <svtools/transfer.hxx>
50 #include <sot/formats.hxx>
51 #include <vcl/msgbox.hxx>
52 #include <sfx2/filedlghelper.hxx>
53 #include <sfx2/docfile.hxx>
54 #include <unotools/pathoptions.hxx>
55 #include <sfx2/opengrf.hxx>
56 #include "app.hrc"
57 #include "sfx2/sfxresid.hxx"
58 
59 //-----------------------------------------------------------------------------
60 
61 using namespace ::com::sun::star;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::ui::dialogs;
64 using namespace ::com::sun::star::uno;
65 using namespace ::rtl;
66 using namespace ::cppu;
67 
68 
69 //-----------------------------------------------------------------------------
70 
71 sal_uInt16  SvxOpenGrfErr2ResId(    short   err     )
72 {
73     switch( err )
74     {
75         case GRFILTER_OPENERROR:
76             return RID_SVXSTR_GRFILTER_OPENERROR;
77         case GRFILTER_IOERROR:
78             return RID_SVXSTR_GRFILTER_IOERROR;
79         case GRFILTER_VERSIONERROR:
80             return RID_SVXSTR_GRFILTER_VERSIONERROR;
81         case GRFILTER_FILTERERROR:
82             return RID_SVXSTR_GRFILTER_FILTERERROR;
83         case GRFILTER_FORMATERROR:
84         default:
85             return RID_SVXSTR_GRFILTER_FORMATERROR;
86     }
87 }
88 
89 
90 struct SvxOpenGrf_Impl
91 {
92     SvxOpenGrf_Impl         ();
93 
94     sfx2::FileDialogHelper                  aFileDlg;
95     uno::Reference < XFilePickerControlAccess > xCtrlAcc;
96 };
97 
98 
99 SvxOpenGrf_Impl::SvxOpenGrf_Impl() :
100     aFileDlg(SFXWB_GRAPHIC)
101 {
102     uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
103     xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
104 }
105 
106 
107 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const String& rTitle ) :
108     mpImpl( new SvxOpenGrf_Impl )
109 {
110     mpImpl->aFileDlg.SetTitle(rTitle);
111 }
112 
113 
114 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
115 {
116 }
117 
118 
119 short SvxOpenGraphicDialog::Execute()
120 {
121     sal_uInt16  nImpRet;
122     sal_Bool    bQuitLoop(sal_False);
123 
124     while( bQuitLoop == sal_False &&
125            mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
126     {
127         if( GetPath().Len() )
128         {
129             GraphicFilter*  pFilter = GraphicFilter::GetGraphicFilter();
130             INetURLObject aObj( GetPath() );
131 
132             // check whether we can load the graphic
133             String  aCurFilter( GetCurrentFilter() );
134             sal_uInt16  nFormatNum = pFilter->GetImportFormatNumber( aCurFilter );
135             sal_uInt16  nRetFormat = 0;
136             sal_uInt16  nFound = USHRT_MAX;
137 
138             // non-local?
139             if ( INET_PROT_FILE != aObj.GetProtocol() )
140             {
141                 SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ, sal_True );
142                 aMed.DownLoad();
143                 SvStream* pStream = aMed.GetInStream();
144 
145                 if( pStream )
146                     nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
147                 else
148                     nImpRet = pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat );
149 
150                 if ( GRFILTER_OK != nImpRet )
151                 {
152                     if ( !pStream )
153                         nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
154                     else
155                         nImpRet = pFilter->CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
156                                                              GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
157                 }
158             }
159             else
160             {
161                 if( (nImpRet=pFilter->CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
162                     nImpRet = pFilter->CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
163             }
164 
165             if ( GRFILTER_OK == nImpRet )
166                 nFound = nRetFormat;
167 
168             // could not load?
169             if ( nFound == USHRT_MAX )
170             {
171                 WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, String( SfxResId( SvxOpenGrfErr2ResId(nImpRet) ) ) );
172                 bQuitLoop = aWarningBox.Execute()==RET_RETRY ? sal_False : sal_True;
173             }
174             else
175             {
176                 // setup appropriate filter (so next time, it will work)
177                 if( pFilter->GetImportFormatCount() )
178                 {
179                     String  aFormatName(pFilter->GetImportFormatName(nFound));
180                     SetCurrentFilter(aFormatName);
181                 }
182 
183                 return nImpRet;
184             }
185         }
186     }
187 
188     // cancel
189     return -1;
190 }
191 
192 
193 void SvxOpenGraphicDialog::SetPath( const String& rPath )
194 {
195     mpImpl->aFileDlg.SetDisplayDirectory(rPath);
196 }
197 
198 void SvxOpenGraphicDialog::SetPath( const String& rPath, sal_Bool bLinkState )
199 {
200     SetPath(rPath);
201     AsLink(bLinkState);
202 }
203 
204 
205 void SvxOpenGraphicDialog::EnableLink( sal_Bool  state  )
206 {
207     if( mpImpl->xCtrlAcc.is() )
208     {
209         try
210         {
211             mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
212         }
213         catch(IllegalArgumentException)
214         {
215 #ifdef DBG_UTIL
216             DBG_ERROR( "Cannot enable \"link\" checkbox" );
217 #endif
218         }
219     }
220 }
221 
222 
223 void SvxOpenGraphicDialog::AsLink(sal_Bool  bState)
224 {
225     if( mpImpl->xCtrlAcc.is() )
226     {
227         try
228         {
229             Any aAny; aAny <<= bState;
230             mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
231         }
232         catch(IllegalArgumentException)
233         {
234 #ifdef DBG_UTIL
235             DBG_ERROR( "Cannot check \"link\" checkbox" );
236 #endif
237         }
238     }
239 }
240 
241 
242 sal_Bool SvxOpenGraphicDialog::IsAsLink() const
243 {
244     try
245     {
246         if( mpImpl->xCtrlAcc.is() )
247         {
248             Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
249             DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
250             return aVal.hasValue() ? *(sal_Bool*) aVal.getValue() : sal_False;
251         }
252     }
253     catch(IllegalArgumentException)
254     {
255 #ifdef DBG_UTIL
256         DBG_ERROR( "Cannot access \"link\" checkbox" );
257 #endif
258     }
259 
260     return sal_False;
261 }
262 
263 
264 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
265 {
266     return mpImpl->aFileDlg.GetGraphic(rGraphic);
267 }
268 
269 
270 String SvxOpenGraphicDialog::GetPath() const
271 {
272     return mpImpl->aFileDlg.GetPath();
273 }
274 
275 
276 String SvxOpenGraphicDialog::GetCurrentFilter() const
277 {
278     return mpImpl->aFileDlg.GetCurrentFilter();
279 }
280 
281 
282 void SvxOpenGraphicDialog::SetCurrentFilter(const String&   rStr)
283 {
284     mpImpl->aFileDlg.SetCurrentFilter(rStr);
285 }
286 
287 void SvxOpenGraphicDialog::SetControlHelpIds( const sal_Int16* _pControlId, const char** _pHelpId )
288 {
289     mpImpl->aFileDlg.SetControlHelpIds( _pControlId, _pHelpId );
290 }
291 
292 
293