LibreOffice Module sfx2 (master) 1
docinsert.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sfx2/app.hxx>
21#include <sfx2/docinsert.hxx>
22#include <sfx2/docfile.hxx>
23#include <sfx2/fcontnr.hxx>
25#include <appopen.hxx>
26#include <openflag.hxx>
27#include <sfx2/passwd.hxx>
28
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 <utility>
44
45using namespace ::com::sun::star;
46using namespace ::com::sun::star::lang;
47using namespace ::com::sun::star::ui::dialogs;
48using namespace ::com::sun::star::uno;
49
50namespace
51{
52
53FileDialogFlags lcl_map_mode_to_flags(const sfx2::DocumentInserter::Mode mode)
54{
56 switch (mode)
57 {
60 break;
63 break;
66 break;
69 break;
70 }
71 return f;
72}
73
74}
75
76namespace sfx2 {
77
78DocumentInserter::DocumentInserter(weld::Window* pParent, OUString sFactory, const Mode mode)
79 : m_pParent ( pParent )
80 , m_sDocFactory (std::move( sFactory ))
81 , m_nDlgFlags ( lcl_map_mode_to_flags(mode) )
82 , m_nError ( ERRCODE_NONE )
83{
84}
85
87{
88}
89
91{
92 m_aDialogClosedLink = _rDialogClosedLink;
94 if ( !m_pFileDlg )
95 {
97 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
98 m_nDlgFlags, m_sDocFactory, SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_pParent ) );
99 }
101 m_pFileDlg->StartExecuteModal( LINK( this, DocumentInserter, DialogClosedHdl ) );
102}
103
104std::unique_ptr<SfxMedium> DocumentInserter::CreateMedium(char const*const pFallbackHack)
105{
106 std::unique_ptr<SfxMedium> pMedium;
107 if (!m_nError && m_xItemSet && !m_pURLList.empty())
108 {
109 DBG_ASSERT( m_pURLList.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
110 pMedium.reset(new SfxMedium(
112 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_xItemSet ));
113 pMedium->UseInteractionHandler( true );
114 std::optional<SfxFilterMatcher> pMatcher;
115 if ( !m_sDocFactory.isEmpty() )
116 pMatcher.emplace(m_sDocFactory);
117 else
118 pMatcher.emplace();
119
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)
124 {
125 pMatcher.emplace(OUString::createFromAscii(pFallbackHack));
126 nError = pMatcher->DetectFilter( *pMedium, pFilter );
127 }
128 if ( nError == ERRCODE_NONE && pFilter )
129 pMedium->SetFilter( pFilter );
130 else
131 pMedium.reset();
132
133 if ( pMedium && CheckPasswd_Impl( nullptr, pMedium.get() ) == ERRCODE_ABORT )
134 pMedium.reset();
135 }
136
137 return pMedium;
138}
139
141{
142 SfxMediumList aMediumList;
143 if (!m_nError && m_xItemSet && !m_pURLList.empty())
144 {
145 for (auto const& url : m_pURLList)
146 {
147 std::unique_ptr<SfxMedium> pMedium(new SfxMedium(
149 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_xItemSet ));
150
151 pMedium->UseInteractionHandler( true );
152
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();
160
161 if( pMedium && CheckPasswd_Impl( nullptr, pMedium.get() ) != ERRCODE_ABORT )
162 aMediumList.push_back( std::move(pMedium) );
163 }
164 }
165
166 return aMediumList;
167}
168
169static void impl_FillURLList( sfx2::FileDialogHelper const * _pFileDlg, std::vector<OUString>& _rpURLList )
170{
171 DBG_ASSERT( _pFileDlg, "DocumentInserter::fillURLList(): invalid file dialog" );
172
173 const Sequence < OUString > aPathSeq = _pFileDlg->GetSelectedFiles();
174
175 if ( aPathSeq.hasElements() )
176 {
177 _rpURLList.clear();
178
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);
183 });
184 }
185}
186
188{
189 DBG_ASSERT( m_pFileDlg, "DocumentInserter::DialogClosedHdl(): no file dialog" );
190
191 m_nError = m_pFileDlg->GetError();
192 if ( ERRCODE_NONE == m_nError )
193 impl_FillURLList( m_pFileDlg.get(), m_pURLList );
194
195 Reference < XFilePicker3 > xFP = m_pFileDlg->GetFilePicker();
196 Reference < XFilePickerControlAccess > xCtrlAccess( xFP, UNO_QUERY );
197 if ( xCtrlAccess.is() )
198 {
199 // always create a new itemset
200 m_xItemSet = std::make_shared<SfxAllItemSet>( SfxGetpApp()->GetPool() );
201
202 short nDlgType = m_pFileDlg->GetDialogType();
203 bool bHasPassword = (
204 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD == nDlgType
205 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS == nDlgType );
206
207 // check, whether or not we have to display a password box
208 if ( bHasPassword && m_pFileDlg->IsPasswordEnabled() )
209 {
210 try
211 {
212 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD, 0 );
213 bool bPassWord = false;
214 if ( ( aValue >>= bPassWord ) && bPassWord )
215 {
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 )
221 {
222 m_xItemSet->Put( SfxStringItem( SID_PASSWORD, aPasswordDlg.GetPassword() ) );
223 }
224 else
225 {
226 m_xItemSet.reset();
227 return;
228 }
229 }
230 }
231 catch( const IllegalArgumentException& ){}
232 }
233
234 if ( m_nDlgFlags & FileDialogFlags::Export )
235 {
236 try
237 {
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 ) );
242 }
243 catch( const IllegalArgumentException& )
244 {
245 TOOLS_WARN_EXCEPTION( "sfx.doc", "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
246 }
247 }
248
249
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
254 {
255 if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
256 {
257 try
258 {
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 ) );
263 }
264 catch( const IllegalArgumentException& )
265 {
266 TOOLS_WARN_EXCEPTION( "sfx.doc", "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
267 }
268 }
269 }
270
271 if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
272 {
273 try
274 {
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) ) );
281 }
282 catch( const IllegalArgumentException& ){}
283 }
284 }
285
286 m_sFilter = m_pFileDlg->GetRealFilter();
287
288 m_aDialogClosedLink.Call( m_pFileDlg.get() );
289}
290
291} // namespace sfx2
292
293/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
ErrCode CheckPasswd_Impl(SfxObjectShell *pDoc, SfxMedium *pFile)
Definition: appopen.cxx:172
const sal_uInt16 nVersion
Definition: childwin.cxx:43
ErrCode DetectFilter(SfxMedium &rMedium, std::shared_ptr< const SfxFilter > &) const
Definition: fltfnc.cxx:483
OUString GetPassword() const
Definition: passwd.hxx:91
virtual short run() override
Definition: passwd.cxx:213
void ShowExtras(SfxShowExtras nExtras)
Definition: passwd.hxx:119
FileDialogFlags const m_nDlgFlags
Definition: docinsert.hxx:48
std::shared_ptr< SfxItemSet > m_xItemSet
Definition: docinsert.hxx:53
Link< sfx2::FileDialogHelper *, void > m_aDialogClosedLink
Definition: docinsert.hxx:46
std::vector< OUString > m_pURLList
Definition: docinsert.hxx:54
void StartExecuteModal(const Link< sfx2::FileDialogHelper *, void > &_rDialogClosedLink)
Definition: docinsert.cxx:90
std::unique_ptr< sfx2::FileDialogHelper > m_pFileDlg
Definition: docinsert.hxx:52
SfxMediumList CreateMediumList()
Definition: docinsert.cxx:140
std::unique_ptr< SfxMedium > CreateMedium(char const *pFallbackHack=nullptr)
Definition: docinsert.cxx:104
weld::Window * m_pParent
Definition: docinsert.hxx:43
DocumentInserter(weld::Window *pParent, OUString aFactory, const Mode mode=Mode::Insert)
Definition: docinsert.cxx:78
css::uno::Sequence< OUString > GetSelectedFiles() const
Provides the selected files with full path information.
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
::std::vector< std::unique_ptr< SfxMedium > > SfxMediumList
Definition: docinsert.hxx:34
#define ERRCODE_ABORT
#define ERRCODE_NONE
FileDialogFlags
@ InsertMerge
Special insertion ("Compare" caption)
bool bReadOnly
OUString m_sFilter
IMPL_LINK_NOARG(SvDDELinkEditDialog, EditHdl_Impl, weld::Entry &, void)
Definition: impldde.cxx:91
static void impl_FillURLList(sfx2::FileDialogHelper const *_pFileDlg, std::vector< OUString > &_rpURLList)
Definition: docinsert.cxx:169
#define SFX_STREAM_READONLY
Definition: openflag.hxx:26
ConversionMode mode
RET_OK