LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / doc / docinsert.cxx
blob04fbea3cce7ca8a5ac14d2f534b403a8fb4e52ef
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 .
20 #include <sfx2/app.hxx>
21 #include <sfx2/docinsert.hxx>
22 #include <sfx2/docfile.hxx>
23 #include <sfx2/fcontnr.hxx>
24 #include <sfx2/filedlghelper.hxx>
25 #include <appopen.hxx>
26 #include <openflag.hxx>
27 #include <sfx2/passwd.hxx>
29 #include <sfx2/sfxsids.hrc>
30 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
31 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
32 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 #include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
35 #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 #include <tools/urlobj.hxx>
37 #include <svl/itemset.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/intitem.hxx>
40 #include <svl/stritem.hxx>
41 #include <memory>
42 #include <osl/diagnose.h>
43 #include <tools/diagnose_ex.h>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::ui::dialogs;
48 using namespace ::com::sun::star::uno;
50 namespace
53 FileDialogFlags lcl_map_mode_to_flags(const sfx2::DocumentInserter::Mode mode)
55 FileDialogFlags f {FileDialogFlags::NONE};
56 switch (mode)
58 case sfx2::DocumentInserter::Mode::Insert:
59 f = FileDialogFlags::Insert;
60 break;
61 case sfx2::DocumentInserter::Mode::InsertMulti:
62 f = FileDialogFlags::Insert|FileDialogFlags::MultiSelection;
63 break;
64 case sfx2::DocumentInserter::Mode::Compare:
65 f = FileDialogFlags::InsertCompare;
66 break;
67 case sfx2::DocumentInserter::Mode::Merge:
68 f = FileDialogFlags::InsertMerge;
69 break;
71 return f;
76 namespace sfx2 {
78 DocumentInserter::DocumentInserter(weld::Window* pParent, const OUString& rFactory, const Mode mode)
79 : m_pParent ( pParent )
80 , m_sDocFactory ( rFactory )
81 , m_nDlgFlags ( lcl_map_mode_to_flags(mode) )
82 , m_nError ( ERRCODE_NONE )
86 DocumentInserter::~DocumentInserter()
90 void DocumentInserter::StartExecuteModal( const Link<sfx2::FileDialogHelper*,void>& _rDialogClosedLink )
92 m_aDialogClosedLink = _rDialogClosedLink;
93 m_nError = ERRCODE_NONE;
94 if ( !m_pFileDlg )
96 m_pFileDlg.reset( new FileDialogHelper(
97 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
98 m_nDlgFlags, m_sDocFactory, SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_pParent ) );
100 m_pFileDlg->SetContext(FileDialogHelper::InsertDoc);
101 m_pFileDlg->StartExecuteModal( LINK( this, DocumentInserter, DialogClosedHdl ) );
104 std::unique_ptr<SfxMedium> DocumentInserter::CreateMedium(char const*const pFallbackHack)
106 std::unique_ptr<SfxMedium> pMedium;
107 if (!m_nError && m_xItemSet && !m_pURLList.empty())
109 DBG_ASSERT( m_pURLList.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
110 pMedium.reset(new SfxMedium(
111 m_pURLList[0], SFX_STREAM_READONLY,
112 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_xItemSet ));
113 pMedium->UseInteractionHandler( true );
114 std::unique_ptr<SfxFilterMatcher> pMatcher;
115 if ( !m_sDocFactory.isEmpty() )
116 pMatcher.reset(new SfxFilterMatcher(m_sDocFactory));
117 else
118 pMatcher.reset(new SfxFilterMatcher());
120 std::shared_ptr<const SfxFilter> pFilter;
121 ErrCode nError = pMatcher->DetectFilter( *pMedium, pFilter );
122 // tdf#101813 hack: check again if it's a global document
123 if (ERRCODE_NONE != nError && pFallbackHack)
125 pMatcher.reset(new SfxFilterMatcher(OUString::createFromAscii(pFallbackHack)));
126 nError = pMatcher->DetectFilter( *pMedium, pFilter );
128 if ( nError == ERRCODE_NONE && pFilter )
129 pMedium->SetFilter( pFilter );
130 else
131 pMedium.reset();
133 if ( pMedium && CheckPasswd_Impl( nullptr, pMedium.get() ) == ERRCODE_ABORT )
134 pMedium.reset();
137 return pMedium;
140 SfxMediumList DocumentInserter::CreateMediumList()
142 SfxMediumList aMediumList;
143 if (!m_nError && m_xItemSet && !m_pURLList.empty())
145 for (auto const& url : m_pURLList)
147 std::unique_ptr<SfxMedium> pMedium(new SfxMedium(
148 url, SFX_STREAM_READONLY,
149 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_xItemSet ));
151 pMedium->UseInteractionHandler( true );
153 SfxFilterMatcher aMatcher( m_sDocFactory );
154 std::shared_ptr<const SfxFilter> pFilter;
155 ErrCode nError = aMatcher.DetectFilter( *pMedium, pFilter );
156 if ( nError == ERRCODE_NONE && pFilter )
157 pMedium->SetFilter( pFilter );
158 else
159 pMedium.reset();
161 if( pMedium && CheckPasswd_Impl( nullptr, pMedium.get() ) != ERRCODE_ABORT )
162 aMediumList.push_back( std::move(pMedium) );
166 return aMediumList;
169 static void impl_FillURLList( sfx2::FileDialogHelper const * _pFileDlg, std::vector<OUString>& _rpURLList )
171 DBG_ASSERT( _pFileDlg, "DocumentInserter::fillURLList(): invalid file dialog" );
173 const Sequence < OUString > aPathSeq = _pFileDlg->GetSelectedFiles();
175 if ( aPathSeq.hasElements() )
177 _rpURLList.clear();
179 std::transform(aPathSeq.begin(), aPathSeq.end(), std::back_inserter(_rpURLList),
180 [](const OUString& rPath) -> OUString {
181 INetURLObject aPathObj( rPath );
182 return aPathObj.GetMainURL(INetURLObject::DecodeMechanism::NONE);
187 IMPL_LINK_NOARG(DocumentInserter, DialogClosedHdl, sfx2::FileDialogHelper*, void)
189 DBG_ASSERT( m_pFileDlg, "DocumentInserter::DialogClosedHdl(): no file dialog" );
191 m_nError = m_pFileDlg->GetError();
192 if ( ERRCODE_NONE == m_nError )
193 impl_FillURLList( m_pFileDlg.get(), m_pURLList );
195 Reference < XFilePicker3 > xFP = m_pFileDlg->GetFilePicker();
196 Reference < XFilePickerControlAccess > xCtrlAccess( xFP, UNO_QUERY );
197 if ( xCtrlAccess.is() )
199 // always create a new itemset
200 m_xItemSet = std::make_shared<SfxAllItemSet>( SfxGetpApp()->GetPool() );
202 short nDlgType = m_pFileDlg->GetDialogType();
203 bool bHasPassword = (
204 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD == nDlgType
205 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS == nDlgType );
207 // check, whether or not we have to display a password box
208 if ( bHasPassword && m_pFileDlg->IsPasswordEnabled() )
212 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD, 0 );
213 bool bPassWord = false;
214 if ( ( aValue >>= bPassWord ) && bPassWord )
216 // ask for the password
217 SfxPasswordDialog aPasswordDlg(m_pParent);
218 aPasswordDlg.ShowExtras( SfxShowExtras::CONFIRM );
219 short nRet = aPasswordDlg.run();
220 if ( RET_OK == nRet )
222 m_xItemSet->Put( SfxStringItem( SID_PASSWORD, aPasswordDlg.GetPassword() ) );
224 else
226 m_xItemSet.reset();
227 return;
231 catch( const IllegalArgumentException& ){}
234 if ( m_nDlgFlags & FileDialogFlags::Export )
238 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION, 0 );
239 bool bSelection = false;
240 if ( aValue >>= bSelection )
241 m_xItemSet->Put( SfxBoolItem( SID_SELECTION, bSelection ) );
243 catch( const IllegalArgumentException& )
245 TOOLS_WARN_EXCEPTION( "sfx.doc", "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
250 // set the read-only flag. When inserting a file, this flag is always set
251 if ( m_nDlgFlags & FileDialogFlags::Insert )
252 m_xItemSet->Put( SfxBoolItem( SID_DOC_READONLY, true ) );
253 else
255 if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
259 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY, 0 );
260 bool bReadOnly = false;
261 if ( ( aValue >>= bReadOnly ) && bReadOnly )
262 m_xItemSet->Put( SfxBoolItem( SID_DOC_READONLY, bReadOnly ) );
264 catch( const IllegalArgumentException& )
266 TOOLS_WARN_EXCEPTION( "sfx.doc", "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
271 if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
275 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION,
276 ControlActions::GET_SELECTED_ITEM_INDEX );
277 sal_Int32 nVersion = 0;
278 if ( ( aValue >>= nVersion ) && nVersion > 0 )
279 // open a special version; 0 == current version
280 m_xItemSet->Put( SfxInt16Item( SID_VERSION, static_cast<short>(nVersion) ) );
282 catch( const IllegalArgumentException& ){}
286 m_sFilter = m_pFileDlg->GetRealFilter();
288 m_aDialogClosedLink.Call( m_pFileDlg.get() );
291 } // namespace sfx2
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */