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