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