Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / appl / opengrf.cxx
blobfd547986d0da2be2c25e424f95834df882b17016
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/debug.hxx>
22 #include <tools/urlobj.hxx>
23 #include <com/sun/star/uno/Reference.h>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
26 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
27 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
28 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
29 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
30 #include <o3tl/any.hxx>
31 #include <vcl/stdtext.hxx>
32 #include <vcl/graphicfilter.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/weld.hxx>
35 #include <sfx2/filedlghelper.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <sfx2/opengrf.hxx>
38 #include <sfx2/strings.hrc>
39 #include <sfx2/sfxresid.hxx>
40 #include <osl/diagnose.h>
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::ui::dialogs;
46 using namespace ::com::sun::star::uno;
47 using namespace ::cppu;
49 static const char* SvxOpenGrfErr2ResId( ErrCode err )
51 if (err == ERRCODE_GRFILTER_OPENERROR)
52 return RID_SVXSTR_GRFILTER_OPENERROR;
53 else if (err == ERRCODE_GRFILTER_IOERROR)
54 return RID_SVXSTR_GRFILTER_IOERROR;
55 else if (err == ERRCODE_GRFILTER_VERSIONERROR)
56 return RID_SVXSTR_GRFILTER_VERSIONERROR;
57 else if (err == ERRCODE_GRFILTER_FILTERERROR)
58 return RID_SVXSTR_GRFILTER_FILTERERROR;
59 else
60 return RID_SVXSTR_GRFILTER_FORMATERROR;
63 struct SvxOpenGrf_Impl
65 SvxOpenGrf_Impl(weld::Window* pPreferredParent,
66 sal_Int16 nDialogType);
68 sfx2::FileDialogHelper aFileDlg;
69 OUString sDetectedFilter;
70 uno::Reference < XFilePickerControlAccess > xCtrlAcc;
74 SvxOpenGrf_Impl::SvxOpenGrf_Impl(weld::Window* pPreferredParent,
75 sal_Int16 nDialogType)
76 : aFileDlg(nDialogType, FileDialogFlags::Graphic, pPreferredParent)
78 uno::Reference < XFilePicker3 > xFP = aFileDlg.GetFilePicker();
79 xCtrlAcc.set(xFP, UNO_QUERY);
83 SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent)
84 : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW))
86 mpImpl->aFileDlg.SetTitle(rTitle);
89 SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent,
90 sal_Int16 nDialogType)
91 : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, nDialogType))
93 mpImpl->aFileDlg.SetTitle(rTitle);
96 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
100 ErrCode SvxOpenGraphicDialog::Execute()
102 ErrCode nImpRet;
103 bool bQuitLoop(false);
105 while( !bQuitLoop &&
106 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
108 if( !GetPath().isEmpty() )
110 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
111 INetURLObject aObj( GetPath() );
113 // check whether we can load the graphic
114 OUString aCurFilter( GetCurrentFilter() );
115 sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
116 sal_uInt16 nRetFormat = 0;
117 sal_uInt16 nFound = USHRT_MAX;
119 // non-local?
120 if ( INetProtocol::File != aObj.GetProtocol() )
122 SfxMedium aMed( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ );
123 aMed.Download();
124 SvStream* pStream = aMed.GetInStream();
126 if( pStream )
127 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream, nFormatNum, &nRetFormat );
128 else
129 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
131 if ( ERRCODE_NONE != nImpRet )
133 if ( !pStream )
134 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
135 else
136 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream,
137 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
140 else
142 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
143 if( nImpRet != ERRCODE_NONE )
144 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
147 if ( ERRCODE_NONE == nImpRet )
148 nFound = nRetFormat;
150 // could not load?
151 if ( nFound == USHRT_MAX )
153 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(nullptr,
154 VclMessageType::Warning, VclButtonsType::NONE,
155 SfxResId(SvxOpenGrfErr2ResId(nImpRet))));
156 xWarn->add_button(GetStandardText(StandardButtonType::Retry), RET_RETRY);
157 xWarn->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
158 bQuitLoop = xWarn->run() != RET_RETRY;
160 else
162 if( rFilter.GetImportFormatCount() )
164 // store detected appropriate filter
165 OUString aFormatName(rFilter.GetImportFormatName(nFound));
166 SetDetectedFilter(aFormatName);
168 else
170 SetDetectedFilter(mpImpl->aFileDlg.GetCurrentFilter());
173 return nImpRet;
178 // cancel
179 return ErrCode(sal_uInt32(-1));
183 void SvxOpenGraphicDialog::SetPath( const OUString& rPath, bool bLinkState )
185 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
186 AsLink(bLinkState);
190 void SvxOpenGraphicDialog::EnableLink( bool state )
192 if( !mpImpl->xCtrlAcc.is() )
193 return;
197 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
199 catch(const IllegalArgumentException&)
201 #ifdef DBG_UTIL
202 OSL_FAIL( "Cannot enable \"link\" checkbox" );
203 #endif
208 void SvxOpenGraphicDialog::AsLink(bool bState)
210 if( !mpImpl->xCtrlAcc.is() )
211 return;
215 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, Any(bState) );
217 catch(const IllegalArgumentException&)
219 #ifdef DBG_UTIL
220 OSL_FAIL( "Cannot check \"link\" checkbox" );
221 #endif
226 bool SvxOpenGraphicDialog::IsAsLink() const
230 if( mpImpl->xCtrlAcc.is() )
232 Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
233 DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
234 return aVal.hasValue() && *o3tl::doAccess<bool>(aVal);
237 catch(const IllegalArgumentException&)
239 #ifdef DBG_UTIL
240 OSL_FAIL( "Cannot access \"link\" checkbox" );
241 #endif
244 return false;
247 ErrCode SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
249 return mpImpl->aFileDlg.GetGraphic(rGraphic);
252 OUString SvxOpenGraphicDialog::GetPath() const
254 return mpImpl->aFileDlg.GetPath();
257 OUString SvxOpenGraphicDialog::GetCurrentFilter() const
259 return mpImpl->aFileDlg.GetCurrentFilter();
262 OUString const & SvxOpenGraphicDialog::GetDetectedFilter() const
264 return mpImpl->sDetectedFilter;
267 void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr)
269 mpImpl->aFileDlg.SetCurrentFilter(rStr);
272 void SvxOpenGraphicDialog::SetDetectedFilter(const OUString& rStr)
274 mpImpl->sDetectedFilter = rStr;
277 Reference<ui::dialogs::XFilePickerControlAccess> const & SvxOpenGraphicDialog::GetFilePickerControlAccess() const
279 return mpImpl->xCtrlAcc;
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */