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