LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / appl / opengrf.cxx
blob325c50422479eab970a3e2d8f42509266cbf1b48
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 TranslateId 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);
87 mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage);
90 SvxOpenGraphicDialog::SvxOpenGraphicDialog(const OUString& rTitle, weld::Window* pPreferredParent,
91 sal_Int16 nDialogType)
92 : mpImpl(new SvxOpenGrf_Impl(pPreferredParent, nDialogType))
94 mpImpl->aFileDlg.SetTitle(rTitle);
95 mpImpl->aFileDlg.SetContext(sfx2::FileDialogHelper::InsertImage);
98 SvxOpenGraphicDialog::~SvxOpenGraphicDialog()
102 ErrCode SvxOpenGraphicDialog::Execute()
104 ErrCode nImpRet;
105 bool bQuitLoop(false);
107 while( !bQuitLoop &&
108 mpImpl->aFileDlg.Execute() == ERRCODE_NONE )
110 if( !GetPath().isEmpty() )
112 GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
113 INetURLObject aObj( GetPath() );
115 // check whether we can load the graphic
116 OUString aCurFilter( GetCurrentFilter() );
117 sal_uInt16 nFormatNum = rFilter.GetImportFormatNumber( aCurFilter );
118 sal_uInt16 nRetFormat = 0;
119 sal_uInt16 nFound = USHRT_MAX;
121 // non-local?
122 if ( INetProtocol::File != aObj.GetProtocol() )
124 SfxMedium aMed( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), StreamMode::READ );
125 aMed.Download();
126 SvStream* pStream = aMed.GetInStream();
128 if( pStream )
129 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream, nFormatNum, &nRetFormat );
130 else
131 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
133 if ( ERRCODE_NONE != nImpRet )
135 if ( !pStream )
136 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
137 else
138 nImpRet = rFilter.CanImportGraphic( aObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ), *pStream,
139 GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
142 else
144 nImpRet = rFilter.CanImportGraphic( aObj, nFormatNum, &nRetFormat );
145 if( nImpRet != ERRCODE_NONE )
146 nImpRet = rFilter.CanImportGraphic( aObj, GRFILTER_FORMAT_DONTKNOW, &nRetFormat );
149 if ( ERRCODE_NONE == nImpRet )
150 nFound = nRetFormat;
152 // could not load?
153 if ( nFound == USHRT_MAX )
155 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(nullptr,
156 VclMessageType::Warning, VclButtonsType::NONE,
157 SfxResId(SvxOpenGrfErr2ResId(nImpRet))));
158 xWarn->add_button(GetStandardText(StandardButtonType::Retry), RET_RETRY);
159 xWarn->add_button(GetStandardText(StandardButtonType::Cancel), RET_CANCEL);
160 bQuitLoop = xWarn->run() != RET_RETRY;
162 else
164 if( rFilter.GetImportFormatCount() )
166 // store detected appropriate filter
167 OUString aFormatName(rFilter.GetImportFormatName(nFound));
168 SetDetectedFilter(aFormatName);
170 else
172 SetDetectedFilter(mpImpl->aFileDlg.GetCurrentFilter());
175 return nImpRet;
180 // cancel
181 return ErrCode(sal_uInt32(-1));
185 void SvxOpenGraphicDialog::SetPath( const OUString& rPath, bool bLinkState )
187 mpImpl->aFileDlg.SetDisplayDirectory(rPath);
188 AsLink(bLinkState);
192 void SvxOpenGraphicDialog::EnableLink( bool state )
194 if( !mpImpl->xCtrlAcc.is() )
195 return;
199 mpImpl->xCtrlAcc->enableControl( ExtendedFilePickerElementIds::CHECKBOX_LINK, state );
201 catch(const IllegalArgumentException&)
203 #ifdef DBG_UTIL
204 OSL_FAIL( "Cannot enable \"link\" checkbox" );
205 #endif
210 void SvxOpenGraphicDialog::AsLink(bool bState)
212 if( !mpImpl->xCtrlAcc.is() )
213 return;
217 mpImpl->xCtrlAcc->setValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0, Any(bState) );
219 catch(const IllegalArgumentException&)
221 #ifdef DBG_UTIL
222 OSL_FAIL( "Cannot check \"link\" checkbox" );
223 #endif
228 bool SvxOpenGraphicDialog::IsAsLink() const
232 if( mpImpl->xCtrlAcc.is() )
234 Any aVal = mpImpl->xCtrlAcc->getValue( ExtendedFilePickerElementIds::CHECKBOX_LINK, 0 );
235 DBG_ASSERT(aVal.hasValue(), "Value CBX_INSERT_AS_LINK not found");
236 return aVal.hasValue() && *o3tl::doAccess<bool>(aVal);
239 catch(const IllegalArgumentException&)
241 #ifdef DBG_UTIL
242 OSL_FAIL( "Cannot access \"link\" checkbox" );
243 #endif
246 return false;
249 ErrCode SvxOpenGraphicDialog::GetGraphic(Graphic& rGraphic) const
251 return mpImpl->aFileDlg.GetGraphic(rGraphic);
254 OUString SvxOpenGraphicDialog::GetPath() const
256 return mpImpl->aFileDlg.GetPath();
259 OUString SvxOpenGraphicDialog::GetCurrentFilter() const
261 return mpImpl->aFileDlg.GetCurrentFilter();
264 OUString const & SvxOpenGraphicDialog::GetDetectedFilter() const
266 return mpImpl->sDetectedFilter;
269 void SvxOpenGraphicDialog::SetCurrentFilter(const OUString& rStr)
271 mpImpl->aFileDlg.SetCurrentFilter(rStr);
274 void SvxOpenGraphicDialog::SetDetectedFilter(const OUString& rStr)
276 mpImpl->sDetectedFilter = rStr;
279 Reference<ui::dialogs::XFilePickerControlAccess> const & SvxOpenGraphicDialog::GetFilePickerControlAccess() const
281 return mpImpl->xCtrlAcc;
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */