Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / appl / opengrf.cxx
blob4c001886d296800c96f0b09434e07289e4c20665
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>
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 ::cppu;
60 sal_uInt16 SvxOpenGrfErr2ResId( short err )
62 switch( err )
64 case GRFILTER_OPENERROR:
65 return RID_SVXSTR_GRFILTER_OPENERROR;
66 case GRFILTER_IOERROR:
67 return RID_SVXSTR_GRFILTER_IOERROR;
68 case GRFILTER_VERSIONERROR:
69 return RID_SVXSTR_GRFILTER_VERSIONERROR;
70 case GRFILTER_FILTERERROR:
71 return RID_SVXSTR_GRFILTER_FILTERERROR;
72 case GRFILTER_FORMATERROR:
73 default:
74 return RID_SVXSTR_GRFILTER_FORMATERROR;
79 struct SvxOpenGrf_Impl
81 SvxOpenGrf_Impl ();
83 sfx2::FileDialogHelper aFileDlg;
84 uno::Reference < XFilePickerControlAccess > xCtrlAcc;
88 SvxOpenGrf_Impl::SvxOpenGrf_Impl()
89 : aFileDlg(ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW,
90 SFXWB_GRAPHIC)
92 uno::Reference < XFilePicker > xFP = aFileDlg.GetFilePicker();
93 xCtrlAcc = uno::Reference < XFilePickerControlAccess >(xFP, UNO_QUERY);
97 SvxOpenGraphicDialog::SvxOpenGraphicDialog( const OUString& rTitle ) :
98 mpImpl( new SvxOpenGrf_Impl )
100 mpImpl->aFileDlg.SetTitle(rTitle);
104 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
109 short SvxOpenGraphicDialog::Execute()
111 sal_uInt16 nImpRet;
112 bool bQuitLoop(false);
114 while( !bQuitLoop &&
115 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
117 if( !GetPath().isEmpty() )
119 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
120 INetURLObject aObj( GetPath() );
122 // check whether we can load the graphic
123 OUString aCurFilter( GetCurrentFilter() );
124 sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
125 sal_uInt16 nRetFormat = 0;
126 sal_uInt16 nFound = USHRT_MAX;
128 // non-local?
129 if ( INetProtocol::File != aObj.GetProtocol() )
131 SfxMedium aMed( aObj.GetMainURL( INetURLObject::NO_DECODE ), StreamMode::READ );
132 aMed.Download();
133 SvStream* pStream = aMed.GetInStream();
135 if( pStream )
136 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream, nFormatNum, &nRetFormat );
137 else
138 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
140 if ( GRFILTER_OK != nImpRet )
142 if ( !pStream )
143 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
144 else
145 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::NO_DECODE ), *pStream,
146 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
149 else
151 if( (nImpRet=rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat )) != GRFILTER_OK )
152 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
155 if ( GRFILTER_OK == nImpRet )
156 nFound = nRetFormat;
158 // could not load?
159 if ( nFound == USHRT_MAX )
161 ScopedVclPtrInstance< WarningBox > aWarningBox(nullptr, WB_3DLOOK | WB_RETRY_CANCEL, SfxResId( SvxOpenGrfErr2ResId(nImpRet) ).toString());
162 bQuitLoop = aWarningBox->Execute() != RET_RETRY;
164 else
166 // setup appropriate filter (so next time, it will work)
167 if( rFilter.GetImportFormatCount() )
169 OUString aFormatName(rFilter.GetImportFormatName(nFound));
170 SetCurrentFilter(aFormatName);
173 return nImpRet;
178 // cancel
179 return -1;
183 void SvxOpenGraphicDialog::SetPath( const OUString& rPath )
185 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
188 void SvxOpenGraphicDialog::SetPath( const OUString& rPath, bool bLinkState )
190 SetPath(rPath);
191 AsLink(bLinkState);
195 void SvxOpenGraphicDialog::EnableLink( bool state )
197 if( mpImpl->xCtrlAcc.is() )
201 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
203 catch(const IllegalArgumentException&)
205 #ifdef DBG_UTIL
206 OSL_FAIL( "Cannot enable \"link\" checkbox" );
207 #endif
213 void SvxOpenGraphicDialog::AsLink(bool bState)
215 if( mpImpl->xCtrlAcc.is() )
219 Any aAny; aAny <<= bState;
220 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, aAny );
222 catch(const IllegalArgumentException&)
224 #ifdef DBG_UTIL
225 OSL_FAIL( "Cannot check \"link\" checkbox" );
226 #endif
232 bool SvxOpenGraphicDialog::IsAsLink() const
236 if( mpImpl->xCtrlAcc.is() )
238 Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
239 DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
240 return aVal.hasValue() && ( *static_cast<sal_Bool const *>(aVal.getValue()) );
243 catch(const IllegalArgumentException&)
245 #ifdef DBG_UTIL
246 OSL_FAIL( "Cannot access \"link\" checkbox" );
247 #endif
250 return false;
254 int SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
256 return mpImpl->aFileDlg.GetGraphic(rGraphic);
260 OUString SvxOpenGraphicDialog::GetPath() const
262 return mpImpl->aFileDlg.GetPath();
266 OUString SvxOpenGraphicDialog::GetCurrentFilter() const
268 return mpImpl->aFileDlg.GetCurrentFilter();
272 void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr)
274 mpImpl->aFileDlg.SetCurrentFilter(rStr);
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */