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