1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
42 #include <osl/diagnose.h>
44 using namespace ::com::sun::star
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::ui::dialogs
;
47 using namespace ::com::sun::star::uno
;
52 FileDialogFlags
lcl_map_mode_to_flags(const sfx2::DocumentInserter::Mode mode
)
54 FileDialogFlags f
{FileDialogFlags::NONE
};
57 case sfx2::DocumentInserter::Mode::Insert
:
58 f
= FileDialogFlags::Insert
;
60 case sfx2::DocumentInserter::Mode::InsertMulti
:
61 f
= FileDialogFlags::Insert
|FileDialogFlags::MultiSelection
;
63 case sfx2::DocumentInserter::Mode::Compare
:
64 f
= FileDialogFlags::InsertCompare
;
66 case sfx2::DocumentInserter::Mode::Merge
:
67 f
= FileDialogFlags::InsertMerge
;
77 DocumentInserter::DocumentInserter(weld::Window
* pParent
, const OUString
& rFactory
, const Mode mode
)
78 : m_pParent ( pParent
)
79 , m_sDocFactory ( rFactory
)
80 , m_nDlgFlags ( lcl_map_mode_to_flags(mode
) )
81 , m_nError ( ERRCODE_NONE
)
85 DocumentInserter::~DocumentInserter()
89 void DocumentInserter::StartExecuteModal( const Link
<sfx2::FileDialogHelper
*,void>& _rDialogClosedLink
)
91 m_aDialogClosedLink
= _rDialogClosedLink
;
92 m_nError
= ERRCODE_NONE
;
95 m_pFileDlg
.reset( new FileDialogHelper(
96 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
97 m_nDlgFlags
, m_sDocFactory
, SfxFilterFlags::NONE
, SfxFilterFlags::NONE
, m_pParent
) );
99 m_pFileDlg
->StartExecuteModal( LINK( this, DocumentInserter
, DialogClosedHdl
) );
102 std::unique_ptr
<SfxMedium
> DocumentInserter::CreateMedium(char const*const pFallbackHack
)
104 std::unique_ptr
<SfxMedium
> pMedium
;
105 if (!m_nError
&& m_xItemSet
&& !m_pURLList
.empty())
107 DBG_ASSERT( m_pURLList
.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
108 pMedium
.reset(new SfxMedium(
109 m_pURLList
[0], SFX_STREAM_READONLY
,
110 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_xItemSet
));
111 pMedium
->UseInteractionHandler( true );
112 std::unique_ptr
<SfxFilterMatcher
> pMatcher
;
113 if ( !m_sDocFactory
.isEmpty() )
114 pMatcher
.reset(new SfxFilterMatcher(m_sDocFactory
));
116 pMatcher
.reset(new SfxFilterMatcher());
118 std::shared_ptr
<const SfxFilter
> pFilter
;
119 ErrCode nError
= pMatcher
->DetectFilter( *pMedium
, pFilter
);
120 // tdf#101813 hack: check again if it's a global document
121 if (ERRCODE_NONE
!= nError
&& pFallbackHack
)
123 pMatcher
.reset(new SfxFilterMatcher(OUString::createFromAscii(pFallbackHack
)));
124 nError
= pMatcher
->DetectFilter( *pMedium
, pFilter
);
126 if ( nError
== ERRCODE_NONE
&& pFilter
)
127 pMedium
->SetFilter( pFilter
);
131 if ( pMedium
&& CheckPasswd_Impl( nullptr, pMedium
.get() ) == ERRCODE_ABORT
)
138 SfxMediumList
DocumentInserter::CreateMediumList()
140 SfxMediumList aMediumList
;
141 if (!m_nError
&& m_xItemSet
&& !m_pURLList
.empty())
143 for (auto const& url
: m_pURLList
)
145 std::unique_ptr
<SfxMedium
> pMedium(new SfxMedium(
146 url
, SFX_STREAM_READONLY
,
147 SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_xItemSet
));
149 pMedium
->UseInteractionHandler( true );
151 SfxFilterMatcher
aMatcher( m_sDocFactory
);
152 std::shared_ptr
<const SfxFilter
> pFilter
;
153 ErrCode nError
= aMatcher
.DetectFilter( *pMedium
, pFilter
);
154 if ( nError
== ERRCODE_NONE
&& pFilter
)
155 pMedium
->SetFilter( pFilter
);
159 if( pMedium
&& CheckPasswd_Impl( nullptr, pMedium
.get() ) != ERRCODE_ABORT
)
160 aMediumList
.push_back( std::move(pMedium
) );
167 static void impl_FillURLList( sfx2::FileDialogHelper
const * _pFileDlg
, std::vector
<OUString
>& _rpURLList
)
169 DBG_ASSERT( _pFileDlg
, "DocumentInserter::fillURLList(): invalid file dialog" );
171 Sequence
< OUString
> aPathSeq
= _pFileDlg
->GetSelectedFiles();
173 if ( aPathSeq
.hasElements() )
177 std::transform(aPathSeq
.begin(), aPathSeq
.end(), std::back_inserter(_rpURLList
),
178 [](const OUString
& rPath
) -> OUString
{
179 INetURLObject
aPathObj( rPath
);
180 return aPathObj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
);
185 IMPL_LINK_NOARG(DocumentInserter
, DialogClosedHdl
, sfx2::FileDialogHelper
*, void)
187 DBG_ASSERT( m_pFileDlg
, "DocumentInserter::DialogClosedHdl(): no file dialog" );
189 m_nError
= m_pFileDlg
->GetError();
190 if ( ERRCODE_NONE
== m_nError
)
191 impl_FillURLList( m_pFileDlg
.get(), m_pURLList
);
193 Reference
< XFilePicker3
> xFP
= m_pFileDlg
->GetFilePicker();
194 Reference
< XFilePickerControlAccess
> xCtrlAccess( xFP
, UNO_QUERY
);
195 if ( xCtrlAccess
.is() )
197 // always create a new itemset
198 m_xItemSet
= std::make_shared
<SfxAllItemSet
>( SfxGetpApp()->GetPool() );
200 short nDlgType
= m_pFileDlg
->GetDialogType();
201 bool bHasPassword
= (
202 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD
== nDlgType
203 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS
== nDlgType
);
205 // check, whether or not we have to display a password box
206 if ( bHasPassword
&& m_pFileDlg
->IsPasswordEnabled() )
210 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD
, 0 );
211 bool bPassWord
= false;
212 if ( ( aValue
>>= bPassWord
) && bPassWord
)
214 // ask for the password
215 SfxPasswordDialog
aPasswordDlg(m_pParent
);
216 aPasswordDlg
.ShowExtras( SfxShowExtras::CONFIRM
);
217 short nRet
= aPasswordDlg
.run();
218 if ( RET_OK
== nRet
)
220 m_xItemSet
->Put( SfxStringItem( SID_PASSWORD
, aPasswordDlg
.GetPassword() ) );
229 catch( const IllegalArgumentException
& ){}
232 if ( m_nDlgFlags
& FileDialogFlags::Export
)
236 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, 0 );
237 bool bSelection
= false;
238 if ( aValue
>>= bSelection
)
239 m_xItemSet
->Put( SfxBoolItem( SID_SELECTION
, bSelection
) );
241 catch( const IllegalArgumentException
& )
243 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
248 // set the read-only flag. When inserting a file, this flag is always set
249 if ( m_nDlgFlags
& FileDialogFlags::Insert
)
250 m_xItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, true ) );
253 if ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
)
257 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY
, 0 );
258 bool bReadOnly
= false;
259 if ( ( aValue
>>= bReadOnly
) && bReadOnly
)
260 m_xItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
262 catch( const IllegalArgumentException
& )
264 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
269 if ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
)
273 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION
,
274 ControlActions::GET_SELECTED_ITEM_INDEX
);
275 sal_Int32 nVersion
= 0;
276 if ( ( aValue
>>= nVersion
) && nVersion
> 0 )
277 // open a special version; 0 == current version
278 m_xItemSet
->Put( SfxInt16Item( SID_VERSION
, static_cast<short>(nVersion
) ) );
280 catch( const IllegalArgumentException
& ){}
284 m_sFilter
= m_pFileDlg
->GetRealFilter();
286 m_aDialogClosedLink
.Call( m_pFileDlg
.get() );
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */