bump product version to 4.2.0.1
[LibreOffice.git] / sfx2 / source / appl / opengrf.cxx
blobd39383b4af727e3c50bc8b2996cd42f2b7d732c5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <tools/urlobj.hxx>
22 #include <cppuhelper/implbase1.hxx>
23 #include <com/sun/star/uno/Reference.h>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
26 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
27 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
28 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
29 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
30 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
31 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
32 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
33 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
35 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
36 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
37 #include <svl/urihelper.hxx>
38 #include <unotools/ucbstreamhelper.hxx>
39 #include <svtools/transfer.hxx>
40 #include <sot/formats.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <sfx2/filedlghelper.hxx>
43 #include <sfx2/docfile.hxx>
44 #include <unotools/pathoptions.hxx>
45 #include <sfx2/opengrf.hxx>
46 #include "app.hrc"
47 #include <sfx2/sfxresid.hxx>
49 //-----------------------------------------------------------------------------
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::ui::dialogs;
54 using namespace ::com::sun::star::uno;
55 using namespace ::rtl;
56 using namespace ::cppu;
59 //-----------------------------------------------------------------------------
61 sal_uInt16 SvxOpenGrfErr2ResId( short err )
63 switch( err )
65 case GRFILTER_OPENERROR:
66 return RID_SVXSTR_GRFILTER_OPENERROR;
67 case GRFILTER_IOERROR:
68 return RID_SVXSTR_GRFILTER_IOERROR;
69 case GRFILTER_VERSIONERROR:
70 return RID_SVXSTR_GRFILTER_VERSIONERROR;
71 case GRFILTER_FILTERERROR:
72 return RID_SVXSTR_GRFILTER_FILTERERROR;
73 case GRFILTER_FORMATERROR:
74 default:
75 return RID_SVXSTR_GRFILTER_FORMATERROR;
80 struct SvxOpenGrf_Impl
82 SvxOpenGrf_Impl ();
84 sfx2::FileDialogHelper aFileDlg;
85 uno::Reference < XFilePickerControlAccess > xCtrlAcc;
89 SvxOpenGrf_Impl::SvxOpenGrf_Impl()
90 : aFileDlg(ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW,
91 SFXWB_GRAPHIC)
93 uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
94 xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
98 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const OUString& rTitle ) :
99 mpImpl( new SvxOpenGrf_Impl )
101 mpImpl->aFileDlg.SetTitle(rTitle);
105 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
110 short SvxOpenGraphicDialog::Execute()
112 sal_uInt16 nImpRet;
113 sal_Bool bQuitLoop(sal_False);
115 while( bQuitLoop == sal_False &&
116 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
118 if( !GetPath().isEmpty() )
120 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
121 INetURLObject aObj( GetPath() );
123 // check whether we can load the graphic
124 OUString aCurFilter( GetCurrentFilter() );
125 sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
126 sal_uInt16 nRetFormat = 0;
127 sal_uInt16 nFound = USHRT_MAX;
129 // non-local?
130 if ( INET_PROT_FILE != aObj.GetProtocol() )
132 SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), STREAM_READ );
133 aMed.DownLoad();
134 SvStream* pStream = aMed.GetInStream();
136 if( pStream )
137 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
138 else
139 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
141 if ( GRFILTER_OK != nImpRet )
143 if ( !pStream )
144 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
145 else
146 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
147 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
150 else
152 if( (nImpRet=rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
153 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
156 if ( GRFILTER_OK == nImpRet )
157 nFound = nRetFormat;
159 // could not load?
160 if ( nFound == USHRT_MAX )
162 WarningBox aWarningBox( NULL, WB_3DLOOK | WB_RETRY_CANCEL, SfxResId( SvxOpenGrfErr2ResId(nImpRet) ).toString() );
163 bQuitLoop = aWarningBox.Execute()==RET_RETRY ? sal_False : sal_True;
165 else
167 // setup appropriate filter (so next time, it will work)
168 if( rFilter.GetImportFormatCount() )
170 OUString aFormatName(rFilter.GetImportFormatName(nFound));
171 SetCurrentFilter(aFormatName);
174 return nImpRet;
179 // cancel
180 return -1;
184 void SvxOpenGraphicDialog::SetPath( const OUString& rPath )
186 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
189 void SvxOpenGraphicDialog::SetPath( const OUString& rPath, sal_Bool bLinkState )
191 SetPath(rPath);
192 AsLink(bLinkState);
196 void SvxOpenGraphicDialog::EnableLink( sal_Bool state )
198 if( mpImpl->xCtrlAcc.is() )
202 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
204 catch(const IllegalArgumentException&)
206 #ifdef DBG_UTIL
207 OSL_FAIL( "Cannot enable \"link\" checkbox" );
208 #endif
214 void SvxOpenGraphicDialog::AsLink(sal_Bool bState)
216 if( mpImpl->xCtrlAcc.is() )
220 Any aAny; aAny <<= bState;
221 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
223 catch(const IllegalArgumentException&)
225 #ifdef DBG_UTIL
226 OSL_FAIL( "Cannot check \"link\" checkbox" );
227 #endif
233 sal_Bool SvxOpenGraphicDialog::IsAsLink() const
237 if( mpImpl->xCtrlAcc.is() )
239 Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
240 DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
241 return aVal.hasValue() ? *(sal_Bool*) aVal.getValue() : sal_False;
244 catch(const IllegalArgumentException&)
246 #ifdef DBG_UTIL
247 OSL_FAIL( "Cannot access \"link\" checkbox" );
248 #endif
251 return sal_False;
255 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
257 return mpImpl->aFileDlg.GetGraphic(rGraphic);
261 OUString SvxOpenGraphicDialog::GetPath() const
263 return mpImpl->aFileDlg.GetPath();
267 OUString SvxOpenGraphicDialog::GetCurrentFilter() const
269 return mpImpl->aFileDlg.GetCurrentFilter();
273 void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr)
275 mpImpl->aFileDlg.SetCurrentFilter(rStr);
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */