1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26
27 #include <sfx2/app.hxx>
28 #include "sfx2/docinsert.hxx"
29 #include <sfx2/docfile.hxx>
30 #include <sfx2/fcontnr.hxx>
31 #include <sfx2/filedlghelper.hxx>
32 #include "openflag.hxx"
33 #include <sfx2/passwd.hxx>
34
35 #include <sfx2/sfxsids.hrc>
36 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
37 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
38 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
39 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
40 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
41 #include <com/sun/star/lang/IllegalArgumentException.hpp>
42 #include <tools/urlobj.hxx>
43 #include <vcl/msgbox.hxx>
44 #include <svl/itemset.hxx>
45 #include <svl/eitem.hxx>
46 #include <svl/intitem.hxx>
47 #include <svl/stritem.hxx>
48
49 #define _SVSTDARR_STRINGSDTOR
50 #include <svl/svstdarr.hxx>
51
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::ui::dialogs;
54 using namespace ::com::sun::star::uno;
55
56 // implemented in 'sfx2/source/appl/appopen.cxx'
57 extern sal_uInt32 CheckPasswd_Impl( SfxObjectShell* pDoc, SfxItemPool &rPool, SfxMedium* pFile );
58
59 // =======================================================================
60
61 namespace sfx2 {
62
63 // =======================================================================
64
65 // =======================================================================
66 // DocumentInserter
67 // =======================================================================
68
DocumentInserter(sal_Int64 _nFlags,const String & _rFactory,bool _bEnableMultiSelection)69 DocumentInserter::DocumentInserter(
70 sal_Int64 _nFlags, const String& _rFactory, bool _bEnableMultiSelection ) :
71
72 m_sDocFactory ( _rFactory )
73 , m_bMultiSelectionEnabled ( _bEnableMultiSelection )
74 , m_nDlgFlags ( _nFlags | SFXWB_INSERT | WB_3DLOOK )
75 , m_nError ( ERRCODE_NONE )
76 , m_pFileDlg ( NULL )
77 , m_pItemSet ( NULL )
78 , m_pURLList ( NULL )
79
80 {
81 }
82
~DocumentInserter()83 DocumentInserter::~DocumentInserter()
84 {
85 delete m_pFileDlg;
86 }
87
StartExecuteModal(const Link & _rDialogClosedLink)88 void DocumentInserter::StartExecuteModal( const Link& _rDialogClosedLink )
89 {
90 m_aDialogClosedLink = _rDialogClosedLink;
91 m_nError = ERRCODE_NONE;
92 DELETEZ( m_pURLList );
93 if ( !m_pFileDlg )
94 {
95 sal_Int64 nFlags = m_bMultiSelectionEnabled ? ( m_nDlgFlags | SFXWB_MULTISELECTION )
96 : m_nDlgFlags;
97 m_pFileDlg = new FileDialogHelper( nFlags, m_sDocFactory );
98 }
99 m_pFileDlg->StartExecuteModal( LINK( this, DocumentInserter, DialogClosedHdl ) );
100 }
101
CreateMedium()102 SfxMedium* DocumentInserter::CreateMedium()
103 {
104 SfxMedium* pMedium = NULL;
105 if ( !m_nError && m_pItemSet && m_pURLList && m_pURLList->Count() > 0 )
106 {
107 DBG_ASSERT( m_pURLList->Count() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
108 String sURL = *( m_pURLList->GetObject(0) );
109 pMedium = new SfxMedium(
110 sURL, SFX_STREAM_READONLY, sal_False,
111 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_pItemSet );
112 pMedium->UseInteractionHandler( sal_True );
113 SfxFilterMatcher* pMatcher = NULL;
114 if ( m_sDocFactory.Len() )
115 pMatcher = new SfxFilterMatcher( m_sDocFactory );
116 else
117 pMatcher = new SfxFilterMatcher();
118
119 const SfxFilter* pFilter = NULL;
120 sal_uInt32 nError = pMatcher->DetectFilter( *pMedium, &pFilter, sal_False );
121 if ( nError == ERRCODE_NONE && pFilter )
122 pMedium->SetFilter( pFilter );
123 else
124 DELETEZ( pMedium );
125
126 if ( pMedium && CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium ) == ERRCODE_ABORT )
127 pMedium = NULL;
128
129 DELETEZ( pMatcher );
130 }
131
132 return pMedium;
133 }
134
CreateMediumList()135 SfxMediumList* DocumentInserter::CreateMediumList()
136 {
137 SfxMediumList* pMediumList = new SfxMediumList;
138 if ( !m_nError && m_pItemSet && m_pURLList && m_pURLList->Count() > 0 )
139 {
140 sal_Int32 i = 0;
141 sal_Int32 nCount = m_pURLList->Count();
142 for ( ; i < nCount; ++i )
143 {
144 String sURL = *( m_pURLList->GetObject( static_cast< sal_uInt16 >(i) ) );
145 SfxMedium* pMedium = new SfxMedium(
146 sURL, SFX_STREAM_READONLY, sal_False,
147 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_pItemSet );
148
149 pMedium->UseInteractionHandler( sal_True );
150
151 SfxFilterMatcher aMatcher( m_sDocFactory );
152 const SfxFilter* pFilter = NULL;
153 sal_uInt32 nError = aMatcher.DetectFilter( *pMedium, &pFilter, sal_False );
154 if ( nError == ERRCODE_NONE && pFilter )
155 pMedium->SetFilter( pFilter );
156 else
157 DELETEZ( pMedium );
158
159 if( pMedium && CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium ) != ERRCODE_ABORT )
160 pMediumList->Insert( pMedium );
161 else
162 delete pMedium;
163 }
164 }
165
166 return pMediumList;
167 }
168
impl_FillURLList(sfx2::FileDialogHelper * _pFileDlg,SvStringsDtor * & _rpURLList)169 void impl_FillURLList( sfx2::FileDialogHelper* _pFileDlg, SvStringsDtor*& _rpURLList )
170 {
171 DBG_ASSERT( _pFileDlg, "DocumentInserter::fillURLList(): invalid file dialog" );
172 DBG_ASSERT( !_rpURLList, "DocumentInserter::fillURLList(): URLList already exists" );
173 Sequence < ::rtl::OUString > aPathSeq = _pFileDlg->GetSelectedFiles();
174
175 if ( aPathSeq.getLength() )
176 {
177 _rpURLList = new SvStringsDtor;
178
179 for ( sal_uInt16 i = 0; i < aPathSeq.getLength(); ++i )
180 {
181 INetURLObject aPathObj( aPathSeq[i] );
182 String* pURL = new String( aPathObj.GetMainURL( INetURLObject::NO_DECODE ) );
183 _rpURLList->Insert( pURL, _rpURLList->Count() );
184 }
185 }
186 }
187
IMPL_LINK(DocumentInserter,DialogClosedHdl,sfx2::FileDialogHelper *,EMPTYARG)188 IMPL_LINK( DocumentInserter, DialogClosedHdl, sfx2::FileDialogHelper*, EMPTYARG )
189 {
190 DBG_ASSERT( m_pFileDlg, "DocumentInserter::DialogClosedHdl(): no file dialog" );
191
192 m_nError = m_pFileDlg->GetError();
193 if ( ERRCODE_NONE == m_nError )
194 impl_FillURLList( m_pFileDlg, m_pURLList );
195
196 Reference < XFilePicker > xFP = m_pFileDlg->GetFilePicker();
197 Reference < XFilePickerControlAccess > xCtrlAccess( xFP, UNO_QUERY );
198 if ( xCtrlAccess.is() )
199 {
200 // always create a new itemset
201 m_pItemSet = new SfxAllItemSet( SFX_APP()->GetPool() );
202
203 short nDlgType = m_pFileDlg->GetDialogType();
204 bool bHasPassword = (
205 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD == nDlgType
206 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS == nDlgType );
207
208 // check, whether or not we have to display a password box
209 if ( bHasPassword && m_pFileDlg->IsPasswordEnabled() )
210 {
211 try
212 {
213 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD, 0 );
214 sal_Bool bPassWord = sal_False;
215 if ( ( aValue >>= bPassWord ) && bPassWord )
216 {
217 // ask for the password
218 SfxPasswordDialog aPasswordDlg( NULL );
219 aPasswordDlg.ShowExtras( SHOWEXTRAS_CONFIRM );
220 short nRet = aPasswordDlg.Execute();
221 if ( RET_OK == nRet )
222 {
223 String aPasswd = aPasswordDlg.GetPassword();
224 m_pItemSet->Put( SfxStringItem( SID_PASSWORD, aPasswd ) );
225 }
226 else
227 {
228 DELETEZ( m_pItemSet );
229 return 0;
230 }
231 }
232 }
233 catch( IllegalArgumentException ){}
234 }
235
236 if ( SFXWB_EXPORT == ( m_nDlgFlags & SFXWB_EXPORT ) )
237 {
238 try
239 {
240 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION, 0 );
241 sal_Bool bSelection = sal_False;
242 if ( aValue >>= bSelection )
243 m_pItemSet->Put( SfxBoolItem( SID_SELECTION, bSelection ) );
244 }
245 catch( IllegalArgumentException )
246 {
247 DBG_ERROR( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
248 }
249 }
250
251
252 // set the read-only flag. When inserting a file, this flag is always set
253 if ( SFXWB_INSERT == ( m_nDlgFlags & SFXWB_INSERT ) )
254 m_pItemSet->Put( SfxBoolItem( SID_DOC_READONLY, sal_True ) );
255 else
256 {
257 if ( ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType ) )
258 {
259 try
260 {
261 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY, 0 );
262 sal_Bool bReadOnly = sal_False;
263 if ( ( aValue >>= bReadOnly ) && bReadOnly )
264 m_pItemSet->Put( SfxBoolItem( SID_DOC_READONLY, bReadOnly ) );
265 }
266 catch( IllegalArgumentException )
267 {
268 DBG_ERROR( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
269 }
270 }
271 }
272
273 if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
274 {
275 try
276 {
277 Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION,
278 ControlActions::GET_SELECTED_ITEM_INDEX );
279 sal_Int32 nVersion = 0;
280 if ( ( aValue >>= nVersion ) && nVersion > 0 )
281 // open a special version; 0 == current version
282 m_pItemSet->Put( SfxInt16Item( SID_VERSION, (short)nVersion ) );
283 }
284 catch( IllegalArgumentException ){}
285 }
286 }
287
288 m_sFilter = m_pFileDlg->GetRealFilter();
289
290 if ( m_aDialogClosedLink.IsSet() )
291 m_aDialogClosedLink.Call( m_pFileDlg );
292
293 return 0;
294 }
295
296 // =======================================================================
297
298 } // namespace sfx2
299
300 // =======================================================================
301
302