Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / opengrf.cxx
blobcfc00a72a314f298680c85a3ef15e7a380887070
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <tools/urlobj.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 #include <com/sun/star/uno/Reference.h>
33 #include <com/sun/star/lang/XInitialization.hpp>
34 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
35 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
36 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
37 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
38 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
39 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
40 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
41 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
43 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
44 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
45 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
46 #include <svl/urihelper.hxx>
47 #include <unotools/ucbstreamhelper.hxx>
48 #include <svtools/transfer.hxx>
49 #include <sot/formats.hxx>
50 #include <vcl/msgbox.hxx>
51 #include <sfx2/filedlghelper.hxx>
52 #include <sfx2/docfile.hxx>
53 #include <unotools/pathoptions.hxx>
54 #include <sfx2/opengrf.hxx>
55 #include "app.hrc"
56 #include "sfx2/sfxresid.hxx"
58 //-----------------------------------------------------------------------------
60 using namespace ::com::sun::star;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::ui::dialogs;
63 using namespace ::com::sun::star::uno;
64 using namespace ::rtl;
65 using namespace ::cppu;
68 //-----------------------------------------------------------------------------
70 sal_uInt16 SvxOpenGrfErr2ResId( short err )
72 switch( err )
74 case GRFILTER_OPENERROR:
75 return RID_SVXSTR_GRFILTER_OPENERROR;
76 case GRFILTER_IOERROR:
77 return RID_SVXSTR_GRFILTER_IOERROR;
78 case GRFILTER_VERSIONERROR:
79 return RID_SVXSTR_GRFILTER_VERSIONERROR;
80 case GRFILTER_FILTERERROR:
81 return RID_SVXSTR_GRFILTER_FILTERERROR;
82 case GRFILTER_FORMATERROR:
83 default:
84 return RID_SVXSTR_GRFILTER_FORMATERROR;
89 struct SvxOpenGrf_Impl
91 SvxOpenGrf_Impl ();
93 sfx2::FileDialogHelper aFileDlg;
94 uno::Reference < XFilePickerControlAccess > xCtrlAcc;
98 SvxOpenGrf_Impl::SvxOpenGrf_Impl()
99 : aFileDlg(ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW,
100 SFXWB_GRAPHIC)
102 uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
103 xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
107 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const String& rTitle ) :
108 mpImpl( new SvxOpenGrf_Impl )
110 mpImpl->aFileDlg.SetTitle(rTitle);
114 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
119 short SvxOpenGraphicDialog::Execute()
121 sal_uInt16 nImpRet;
122 sal_Bool bQuitLoop(sal_False);
124 while( bQuitLoop == sal_False &&
125 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
127 if( GetPath().Len() )
129 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
130 INetURLObject aObj( GetPath() );
132 // check whether we can load the graphic
133 String aCurFilter( GetCurrentFilter() );
134 sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
135 sal_uInt16 nRetFormat = 0;
136 sal_uInt16 nFound = USHRT_MAX;
138 // non-local?
139 if ( INET_PROT_FILE != aObj.GetProtocol() )
141 SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
142 aMed.DownLoad();
143 SvStream* pStream = aMed.GetInStream();
145 if( pStream )
146 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
147 else
148 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
150 if ( GRFILTER_OK != nImpRet )
152 if ( !pStream )
153 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
154 else
155 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
156 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
159 else
161 if( (nImpRet=rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
162 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
165 if ( GRFILTER_OK == nImpRet )
166 nFound = nRetFormat;
168 // could not load?
169 if ( nFound == USHRT_MAX )
171 WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, String( SfxResId( SvxOpenGrfErr2ResId(nImpRet) ) ) );
172 bQuitLoop = aWarningBox.Execute()==RET_RETRY ? sal_False : sal_True;
174 else
176 // setup appropriate filter (so next time, it will work)
177 if( rFilter.GetImportFormatCount() )
179 String aFormatName(rFilter.GetImportFormatName(nFound));
180 SetCurrentFilter(aFormatName);
183 return nImpRet;
188 // cancel
189 return -1;
193 void SvxOpenGraphicDialog::SetPath( const String& rPath )
195 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
198 void SvxOpenGraphicDialog::SetPath( const String& rPath, sal_Bool bLinkState )
200 SetPath(rPath);
201 AsLink(bLinkState);
205 void SvxOpenGraphicDialog::EnableLink( sal_Bool state )
207 if( mpImpl->xCtrlAcc.is() )
211 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
213 catch(const IllegalArgumentException&)
215 #ifdef DBG_UTIL
216 OSL_FAIL( "Cannot enable \"link\" checkbox" );
217 #endif
223 void SvxOpenGraphicDialog::AsLink(sal_Bool bState)
225 if( mpImpl->xCtrlAcc.is() )
229 Any aAny; aAny <<= bState;
230 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
232 catch(const IllegalArgumentException&)
234 #ifdef DBG_UTIL
235 OSL_FAIL( "Cannot check \"link\" checkbox" );
236 #endif
242 sal_Bool SvxOpenGraphicDialog::IsAsLink() const
246 if( mpImpl->xCtrlAcc.is() )
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;
253 catch(const IllegalArgumentException&)
255 #ifdef DBG_UTIL
256 OSL_FAIL( "Cannot access \"link\" checkbox" );
257 #endif
260 return sal_False;
264 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
266 return mpImpl->aFileDlg.GetGraphic(rGraphic);
270 String SvxOpenGraphicDialog::GetPath() const
272 return mpImpl->aFileDlg.GetPath();
276 String SvxOpenGraphicDialog::GetCurrentFilter() const
278 return mpImpl->aFileDlg.GetCurrentFilter();
282 void SvxOpenGraphicDialog::SetCurrentFilter(const String& rStr)
284 mpImpl->aFileDlg.SetCurrentFilter(rStr);
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */