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