1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: docinsert.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
34 #include <sfx2/app.hxx>
35 #include "docinsert.hxx"
36 #include <sfx2/docfile.hxx>
37 #include <sfx2/fcontnr.hxx>
38 #include <sfx2/filedlghelper.hxx>
39 #include "openflag.hxx"
40 #include <sfx2/passwd.hxx>
42 #include <sfx2/sfxsids.hrc>
43 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
44 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
45 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
46 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
47 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
48 #include <com/sun/star/lang/IllegalArgumentException.hpp>
49 #include <tools/urlobj.hxx>
50 #include <vcl/msgbox.hxx>
51 #include <svtools/itemset.hxx>
52 #include <svtools/eitem.hxx>
53 #include <svtools/intitem.hxx>
54 #include <svtools/stritem.hxx>
56 #define _SVSTDARR_STRINGSDTOR
57 #include <svtools/svstdarr.hxx>
59 using namespace ::com::sun::star::lang
;
60 using namespace ::com::sun::star::ui::dialogs
;
61 using namespace ::com::sun::star::uno
;
63 // implemented in 'sfx2/source/appl/appopen.cxx'
64 extern sal_uInt32
CheckPasswd_Impl( SfxObjectShell
* pDoc
, SfxItemPool
&rPool
, SfxMedium
* pFile
);
66 // =======================================================================
70 // =======================================================================
72 // =======================================================================
74 // =======================================================================
76 DocumentInserter::DocumentInserter(
77 sal_Int64 _nFlags
, const String
& _rFactory
, bool _bEnableMultiSelection
) :
79 m_sDocFactory ( _rFactory
)
80 , m_bMultiSelectionEnabled ( _bEnableMultiSelection
)
81 , m_nDlgFlags ( _nFlags
| SFXWB_INSERT
| WB_3DLOOK
)
82 , m_nError ( ERRCODE_NONE
)
90 DocumentInserter::~DocumentInserter()
95 void DocumentInserter::StartExecuteModal( const Link
& _rDialogClosedLink
)
97 m_aDialogClosedLink
= _rDialogClosedLink
;
98 m_nError
= ERRCODE_NONE
;
99 DELETEZ( m_pURLList
);
102 sal_Int64 nFlags
= m_bMultiSelectionEnabled
? ( m_nDlgFlags
| SFXWB_MULTISELECTION
)
104 m_pFileDlg
= new FileDialogHelper( nFlags
, m_sDocFactory
);
106 m_pFileDlg
->StartExecuteModal( LINK( this, DocumentInserter
, DialogClosedHdl
) );
109 SfxMedium
* DocumentInserter::CreateMedium()
111 SfxMedium
* pMedium
= NULL
;
112 if ( !m_nError
&& m_pItemSet
&& m_pURLList
&& m_pURLList
->Count() > 0 )
114 DBG_ASSERT( m_pURLList
->Count() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
115 String sURL
= *( m_pURLList
->GetObject(0) );
116 pMedium
= new SfxMedium(
117 sURL
, SFX_STREAM_READONLY
, FALSE
,
118 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
119 pMedium
->UseInteractionHandler( TRUE
);
120 SfxFilterMatcher
* pMatcher
= NULL
;
121 if ( m_sDocFactory
.Len() )
122 pMatcher
= new SfxFilterMatcher( m_sDocFactory
);
124 pMatcher
= new SfxFilterMatcher();
126 const SfxFilter
* pFilter
= NULL
;
127 sal_uInt32 nError
= pMatcher
->DetectFilter( *pMedium
, &pFilter
, FALSE
);
128 if ( nError
== ERRCODE_NONE
&& pFilter
)
129 pMedium
->SetFilter( pFilter
);
133 if ( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) == ERRCODE_ABORT
)
142 SfxMediumList
* DocumentInserter::CreateMediumList()
144 SfxMediumList
* pMediumList
= new SfxMediumList
;
145 if ( !m_nError
&& m_pItemSet
&& m_pURLList
&& m_pURLList
->Count() > 0 )
148 sal_Int32 nCount
= m_pURLList
->Count();
149 for ( ; i
< nCount
; ++i
)
151 String sURL
= *( m_pURLList
->GetObject( static_cast< USHORT
>(i
) ) );
152 SfxMedium
* pMedium
= new SfxMedium(
153 sURL
, SFX_STREAM_READONLY
, FALSE
,
154 SFX_APP()->GetFilterMatcher().GetFilter4FilterName( m_sFilter
), m_pItemSet
);
156 pMedium
->UseInteractionHandler( TRUE
);
158 SfxFilterMatcher
aMatcher( m_sDocFactory
);
159 const SfxFilter
* pFilter
= NULL
;
160 sal_uInt32 nError
= aMatcher
.DetectFilter( *pMedium
, &pFilter
, FALSE
);
161 if ( nError
== ERRCODE_NONE
&& pFilter
)
162 pMedium
->SetFilter( pFilter
);
166 if( pMedium
&& CheckPasswd_Impl( 0, SFX_APP()->GetPool(), pMedium
) != ERRCODE_ABORT
)
167 pMediumList
->Insert( pMedium
);
176 void impl_FillURLList( sfx2::FileDialogHelper
* _pFileDlg
, SvStringsDtor
*& _rpURLList
)
178 DBG_ASSERT( _pFileDlg
, "DocumentInserter::fillURLList(): invalid file dialog" );
179 DBG_ASSERT( !_rpURLList
, "DocumentInserter::fillURLList(): URLList already exists" );
180 Sequence
< ::rtl::OUString
> aPathSeq
= _pFileDlg
->GetSelectedFiles();
182 if ( aPathSeq
.getLength() )
184 _rpURLList
= new SvStringsDtor
;
186 for ( USHORT i
= 0; i
< aPathSeq
.getLength(); ++i
)
188 INetURLObject
aPathObj( aPathSeq
[i
] );
189 String
* pURL
= new String( aPathObj
.GetMainURL( INetURLObject::NO_DECODE
) );
190 _rpURLList
->Insert( pURL
, _rpURLList
->Count() );
195 IMPL_LINK( DocumentInserter
, DialogClosedHdl
, sfx2::FileDialogHelper
*, EMPTYARG
)
197 DBG_ASSERT( m_pFileDlg
, "DocumentInserter::DialogClosedHdl(): no file dialog" );
199 m_nError
= m_pFileDlg
->GetError();
200 if ( ERRCODE_NONE
== m_nError
)
201 impl_FillURLList( m_pFileDlg
, m_pURLList
);
203 Reference
< XFilePicker
> xFP
= m_pFileDlg
->GetFilePicker();
204 Reference
< XFilePickerControlAccess
> xCtrlAccess( xFP
, UNO_QUERY
);
205 if ( xCtrlAccess
.is() )
207 // always create a new itemset
208 m_pItemSet
= new SfxAllItemSet( SFX_APP()->GetPool() );
210 short nDlgType
= m_pFileDlg
->GetDialogType();
211 bool bHasPassword
= (
212 TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD
== nDlgType
213 || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS
== nDlgType
);
215 // check, wether or not we have to display a password box
216 if ( bHasPassword
&& m_pFileDlg
->IsPasswordEnabled() )
220 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD
, 0 );
221 sal_Bool bPassWord
= sal_False
;
222 if ( ( aValue
>>= bPassWord
) && bPassWord
)
224 // ask for the password
225 SfxPasswordDialog
aPasswordDlg( NULL
);
226 aPasswordDlg
.ShowExtras( SHOWEXTRAS_CONFIRM
);
227 short nRet
= aPasswordDlg
.Execute();
228 if ( RET_OK
== nRet
)
230 String aPasswd
= aPasswordDlg
.GetPassword();
231 m_pItemSet
->Put( SfxStringItem( SID_PASSWORD
, aPasswd
) );
235 DELETEZ( m_pItemSet
);
240 catch( IllegalArgumentException
){}
243 if ( SFXWB_EXPORT
== ( m_nDlgFlags
& SFXWB_EXPORT
) )
247 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION
, 0 );
248 sal_Bool bSelection
= sal_False
;
249 if ( aValue
>>= bSelection
)
250 m_pItemSet
->Put( SfxBoolItem( SID_SELECTION
, bSelection
) );
252 catch( IllegalArgumentException
)
254 DBG_ERROR( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
259 // set the read-only flag. When inserting a file, this flag is always set
260 if ( SFXWB_INSERT
== ( m_nDlgFlags
& SFXWB_INSERT
) )
261 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, sal_True
) );
264 if ( ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
) )
268 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY
, 0 );
269 sal_Bool bReadOnly
= sal_False
;
270 if ( ( aValue
>>= bReadOnly
) && bReadOnly
)
271 m_pItemSet
->Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
273 catch( IllegalArgumentException
)
275 DBG_ERROR( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
280 if ( TemplateDescription::FILEOPEN_READONLY_VERSION
== nDlgType
)
284 Any aValue
= xCtrlAccess
->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION
,
285 ControlActions::GET_SELECTED_ITEM_INDEX
);
286 sal_Int32 nVersion
= 0;
287 if ( ( aValue
>>= nVersion
) && nVersion
> 0 )
288 // open a special version; 0 == current version
289 m_pItemSet
->Put( SfxInt16Item( SID_VERSION
, (short)nVersion
) );
291 catch( IllegalArgumentException
){}
295 m_sFilter
= m_pFileDlg
->GetRealFilter();
297 if ( m_aDialogClosedLink
.IsSet() )
298 m_aDialogClosedLink
.Call( m_pFileDlg
);
303 // =======================================================================
307 // =======================================================================