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 "openflag.hxx"
26 #include <sfx2/passwd.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
30 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
31 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
32 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <tools/urlobj.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <svl/itemset.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/intitem.hxx>
40 #include <svl/stritem.hxx>
42 using namespace ::com::sun::star
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::ui::dialogs
;
45 using namespace ::com::sun::star::uno
;
47 // implemented in 'sfx2/source/appl/appopen.cxx'
48 extern sal_uInt32
CheckPasswd_Impl( SfxObjectShell
* pDoc
, SfxItemPool
&rPool
, SfxMedium
* pFile
);
52 DocumentInserter::DocumentInserter(
53 const String
& rFactory
, bool const bEnableMultiSelection
) :
55 m_sDocFactory ( rFactory
)
56 , m_nDlgFlags ( (bEnableMultiSelection
)
57 ? (SFXWB_INSERT
|SFXWB_MULTISELECTION
)
59 , m_nError ( ERRCODE_NONE
)
65 DocumentInserter::~DocumentInserter()
70 void DocumentInserter::StartExecuteModal( const Link
& _rDialogClosedLink
)
72 m_aDialogClosedLink
= _rDialogClosedLink
;
73 m_nError
= ERRCODE_NONE
;
76 m_pFileDlg
= new FileDialogHelper(
77 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
78 m_nDlgFlags
, m_sDocFactory
);
80 m_pFileDlg
->StartExecuteModal( LINK( this, DocumentInserter
, DialogClosedHdl
) );
83 SfxMedium
* DocumentInserter::CreateMedium()
85 SfxMedium
* pMedium
= NULL
;
86 if (!m_nError
&& m_pItemSet
&& !m_pURLList
.empty())
88 DBG_ASSERT( m_pURLList
.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
89 String
sURL(m_pURLList
[0]);
90 pMedium
= new SfxMedium(
91 sURL
, SFX_STREAM_READONLY
,
92 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
93 pMedium
->UseInteractionHandler( sal_True
);
94 SfxFilterMatcher
* pMatcher
= NULL
;
95 if ( m_sDocFactory
.Len() )
96 pMatcher
= new SfxFilterMatcher( m_sDocFactory
);
98 pMatcher
= new SfxFilterMatcher();
100 const SfxFilter
* pFilter
= NULL
;
101 sal_uInt32 nError
= pMatcher
->DetectFilter( *pMedium
, &pFilter
, sal_False
);
102 if ( nError
== ERRCODE_NONE
&& pFilter
)
103 pMedium
->SetFilter( pFilter
);
107 if ( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) == ERRCODE_ABORT
)
116 SfxMediumList
* DocumentInserter::CreateMediumList()
118 SfxMediumList
* pMediumList
= new SfxMediumList
;
119 if (!m_nError
&& m_pItemSet
&& !m_pURLList
.empty())
121 for(std::vector
<OUString
>::const_iterator i
= m_pURLList
.begin(); i
!= m_pURLList
.end(); ++i
)
123 SfxMedium
* pMedium
= new SfxMedium(
124 *i
, SFX_STREAM_READONLY
,
125 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
127 pMedium
->UseInteractionHandler( sal_True
);
129 SfxFilterMatcher
aMatcher( m_sDocFactory
);
130 const SfxFilter
* pFilter
= NULL
;
131 sal_uInt32 nError
= aMatcher
.DetectFilter( *pMedium
, &pFilter
, sal_False
);
132 if ( nError
== ERRCODE_NONE
&& pFilter
)
133 pMedium
->SetFilter( pFilter
);
137 if( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) != ERRCODE_ABORT
)
138 pMediumList
->push_back( pMedium
);
147 void impl_FillURLList( sfx2::FileDialogHelper
* _pFileDlg
, std::vector
<OUString
>& _rpURLList
)
149 DBG_ASSERT( _pFileDlg
, "DocumentInserter::fillURLList(): invalid file dialog" );
151 Sequence
< OUString
> aPathSeq
= _pFileDlg
->GetSelectedFiles();
153 if ( aPathSeq
.getLength() )
157 for ( sal_uInt16 i
= 0; i
< aPathSeq
.getLength(); ++i
)
159 INetURLObject
aPathObj( aPathSeq
[i
] );
160 _rpURLList
.push_back(aPathObj
.GetMainURL(INetURLObject::NO_DECODE
));
165 IMPL_LINK_NOARG(DocumentInserter
, DialogClosedHdl
)
167 DBG_ASSERT( m_pFileDlg
, "DocumentInserter::DialogClosedHdl(): no file dialog" );
169 m_nError
= m_pFileDlg
->GetError();
170 if ( ERRCODE_NONE
== m_nError
)
171 impl_FillURLList( m_pFileDlg
, m_pURLList
);
173 Reference
< XFilePicker
> xFP
= m_pFileDlg
->GetFilePicker();
174 Reference
< XFilePickerControlAccess
> xCtrlAccess( xFP
, UNO_QUERY
);
175 if ( xCtrlAccess
.is() )
177 // always create a new itemset
178 m_pItemSet
= new SfxAllItemSet( SFX_APP()->GetPool() );
180 short nDlgType
= m_pFileDlg
->GetDialogType();
181 bool bHasPassword
= (
182 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD
== nDlgType
183 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS
== nDlgType
);
185 // check, whether or not we have to display a password box
186 if ( bHasPassword
&& m_pFileDlg
->IsPasswordEnabled() )
190 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD
, 0 );
191 sal_Bool bPassWord
= sal_False
;
192 if ( ( aValue
>>= bPassWord
) && bPassWord
)
194 // ask for the password
195 SfxPasswordDialog
aPasswordDlg( NULL
);
196 aPasswordDlg
.ShowExtras( SHOWEXTRAS_CONFIRM
);
197 short nRet
= aPasswordDlg
.Execute();
198 if ( RET_OK
== nRet
)
200 String aPasswd
= aPasswordDlg
.GetPassword();
201 m_pItemSet
->Put( SfxStringItem( SID_PASSWORD
, aPasswd
) );
205 DELETEZ( m_pItemSet
);
210 catch( const IllegalArgumentException
& ){}
213 if ( SFXWB_EXPORT
== ( m_nDlgFlags
& SFXWB_EXPORT
) )
217 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, 0 );
218 sal_Bool bSelection
= sal_False
;
219 if ( aValue
>>= bSelection
)
220 m_pItemSet
->Put( SfxBoolItem( SID_SELECTION
, bSelection
) );
222 catch( const IllegalArgumentException
& )
224 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
229 // set the read-only flag. When inserting a file, this flag is always set
230 if ( SFXWB_INSERT
== ( m_nDlgFlags
& SFXWB_INSERT
) )
231 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, sal_True
) );
234 if ( ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
) )
238 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY
, 0 );
239 sal_Bool bReadOnly
= sal_False
;
240 if ( ( aValue
>>= bReadOnly
) && bReadOnly
)
241 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
243 catch( const IllegalArgumentException
& )
245 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
250 if ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
)
254 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION
,
255 ControlActions::GET_SELECTED_ITEM_INDEX
);
256 sal_Int32 nVersion
= 0;
257 if ( ( aValue
>>= nVersion
) && nVersion
> 0 )
258 // open a special version; 0 == current version
259 m_pItemSet
->Put( SfxInt16Item( SID_VERSION
, (short)nVersion
) );
261 catch( const IllegalArgumentException
& ){}
265 m_sFilter
= m_pFileDlg
->GetRealFilter();
267 if ( m_aDialogClosedLink
.IsSet() )
268 m_aDialogClosedLink
.Call( m_pFileDlg
);
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */