1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <sfx2/app.hxx>
30 #include "sfx2/docinsert.hxx"
31 #include <sfx2/docfile.hxx>
32 #include <sfx2/fcontnr.hxx>
33 #include <sfx2/filedlghelper.hxx>
34 #include "openflag.hxx"
35 #include <sfx2/passwd.hxx>
37 #include <sfx2/sfxsids.hrc>
38 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
39 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
40 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
41 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
42 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
43 #include <com/sun/star/lang/IllegalArgumentException.hpp>
44 #include <tools/urlobj.hxx>
45 #include <vcl/msgbox.hxx>
46 #include <svl/itemset.hxx>
47 #include <svl/eitem.hxx>
48 #include <svl/intitem.hxx>
49 #include <svl/stritem.hxx>
50 #include <svl/svstdarr.hxx>
52 using namespace ::com::sun::star
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::com::sun::star::ui::dialogs
;
55 using namespace ::com::sun::star::uno
;
57 // implemented in 'sfx2/source/appl/appopen.cxx'
58 extern sal_uInt32
CheckPasswd_Impl( SfxObjectShell
* pDoc
, SfxItemPool
&rPool
, SfxMedium
* pFile
);
62 DocumentInserter::DocumentInserter(
63 const String
& rFactory
, bool const bEnableMultiSelection
) :
65 m_sDocFactory ( rFactory
)
66 , m_nDlgFlags ( (bEnableMultiSelection
)
67 ? (SFXWB_INSERT
|SFXWB_MULTISELECTION
)
69 , m_nError ( ERRCODE_NONE
)
75 DocumentInserter::~DocumentInserter()
80 void DocumentInserter::StartExecuteModal( const Link
& _rDialogClosedLink
)
82 m_aDialogClosedLink
= _rDialogClosedLink
;
83 m_nError
= ERRCODE_NONE
;
86 m_pFileDlg
= new FileDialogHelper(
87 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
,
88 m_nDlgFlags
, m_sDocFactory
);
90 m_pFileDlg
->StartExecuteModal( LINK( this, DocumentInserter
, DialogClosedHdl
) );
93 SfxMedium
* DocumentInserter::CreateMedium()
95 SfxMedium
* pMedium
= NULL
;
96 if (!m_nError
&& m_pItemSet
&& !m_pURLList
.empty())
98 DBG_ASSERT( m_pURLList
.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
99 String
sURL(m_pURLList
[0]);
100 pMedium
= new SfxMedium(
101 sURL
, SFX_STREAM_READONLY
,
102 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
103 pMedium
->UseInteractionHandler( sal_True
);
104 SfxFilterMatcher
* pMatcher
= NULL
;
105 if ( m_sDocFactory
.Len() )
106 pMatcher
= new SfxFilterMatcher( m_sDocFactory
);
108 pMatcher
= new SfxFilterMatcher();
110 const SfxFilter
* pFilter
= NULL
;
111 sal_uInt32 nError
= pMatcher
->DetectFilter( *pMedium
, &pFilter
, sal_False
);
112 if ( nError
== ERRCODE_NONE
&& pFilter
)
113 pMedium
->SetFilter( pFilter
);
117 if ( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) == ERRCODE_ABORT
)
126 SfxMediumList
* DocumentInserter::CreateMediumList()
128 SfxMediumList
* pMediumList
= new SfxMediumList
;
129 if (!m_nError
&& m_pItemSet
&& !m_pURLList
.empty())
131 for(std::vector
<rtl::OUString
>::const_iterator i
= m_pURLList
.begin(); i
!= m_pURLList
.end(); ++i
)
133 SfxMedium
* pMedium
= new SfxMedium(
134 *i
, SFX_STREAM_READONLY
,
135 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
137 pMedium
->UseInteractionHandler( sal_True
);
139 SfxFilterMatcher
aMatcher( m_sDocFactory
);
140 const SfxFilter
* pFilter
= NULL
;
141 sal_uInt32 nError
= aMatcher
.DetectFilter( *pMedium
, &pFilter
, sal_False
);
142 if ( nError
== ERRCODE_NONE
&& pFilter
)
143 pMedium
->SetFilter( pFilter
);
147 if( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) != ERRCODE_ABORT
)
148 pMediumList
->push_back( pMedium
);
157 void impl_FillURLList( sfx2::FileDialogHelper
* _pFileDlg
, std::vector
<rtl::OUString
>& _rpURLList
)
159 DBG_ASSERT( _pFileDlg
, "DocumentInserter::fillURLList(): invalid file dialog" );
161 Sequence
< ::rtl::OUString
> aPathSeq
= _pFileDlg
->GetSelectedFiles();
163 if ( aPathSeq
.getLength() )
167 for ( sal_uInt16 i
= 0; i
< aPathSeq
.getLength(); ++i
)
169 INetURLObject
aPathObj( aPathSeq
[i
] );
170 _rpURLList
.push_back(aPathObj
.GetMainURL(INetURLObject::NO_DECODE
));
175 IMPL_LINK_NOARG(DocumentInserter
, DialogClosedHdl
)
177 DBG_ASSERT( m_pFileDlg
, "DocumentInserter::DialogClosedHdl(): no file dialog" );
179 m_nError
= m_pFileDlg
->GetError();
180 if ( ERRCODE_NONE
== m_nError
)
181 impl_FillURLList( m_pFileDlg
, m_pURLList
);
183 Reference
< XFilePicker
> xFP
= m_pFileDlg
->GetFilePicker();
184 Reference
< XFilePickerControlAccess
> xCtrlAccess( xFP
, UNO_QUERY
);
185 if ( xCtrlAccess
.is() )
187 // always create a new itemset
188 m_pItemSet
= new SfxAllItemSet( SFX_APP()->GetPool() );
190 short nDlgType
= m_pFileDlg
->GetDialogType();
191 bool bHasPassword
= (
192 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD
== nDlgType
193 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS
== nDlgType
);
195 // check, whether or not we have to display a password box
196 if ( bHasPassword
&& m_pFileDlg
->IsPasswordEnabled() )
200 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD
, 0 );
201 sal_Bool bPassWord
= sal_False
;
202 if ( ( aValue
>>= bPassWord
) && bPassWord
)
204 // ask for the password
205 SfxPasswordDialog
aPasswordDlg( NULL
);
206 aPasswordDlg
.ShowExtras( SHOWEXTRAS_CONFIRM
);
207 short nRet
= aPasswordDlg
.Execute();
208 if ( RET_OK
== nRet
)
210 String aPasswd
= aPasswordDlg
.GetPassword();
211 m_pItemSet
->Put( SfxStringItem( SID_PASSWORD
, aPasswd
) );
215 DELETEZ( m_pItemSet
);
220 catch( const IllegalArgumentException
& ){}
223 if ( SFXWB_EXPORT
== ( m_nDlgFlags
& SFXWB_EXPORT
) )
227 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, 0 );
228 sal_Bool bSelection
= sal_False
;
229 if ( aValue
>>= bSelection
)
230 m_pItemSet
->Put( SfxBoolItem( SID_SELECTION
, bSelection
) );
232 catch( const IllegalArgumentException
& )
234 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
239 // set the read-only flag. When inserting a file, this flag is always set
240 if ( SFXWB_INSERT
== ( m_nDlgFlags
& SFXWB_INSERT
) )
241 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, sal_True
) );
244 if ( ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
) )
248 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY
, 0 );
249 sal_Bool bReadOnly
= sal_False
;
250 if ( ( aValue
>>= bReadOnly
) && bReadOnly
)
251 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
253 catch( const IllegalArgumentException
& )
255 OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
260 if ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
)
264 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION
,
265 ControlActions::GET_SELECTED_ITEM_INDEX
);
266 sal_Int32 nVersion
= 0;
267 if ( ( aValue
>>= nVersion
) && nVersion
> 0 )
268 // open a special version; 0 == current version
269 m_pItemSet
->Put( SfxInt16Item( SID_VERSION
, (short)nVersion
) );
271 catch( const IllegalArgumentException
& ){}
275 m_sFilter
= m_pFileDlg
->GetRealFilter();
277 if ( m_aDialogClosedLink
.IsSet() )
278 m_aDialogClosedLink
.Call( m_pFileDlg
);
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */