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 "appopen.hxx"
26 : #include "openflag.hxx"
27 : #include <sfx2/passwd.hxx>
28 :
29 : #include <sfx2/sfxsids.hrc>
30 : #include <com/sun/star/ui/dialogs/ControlActions.hpp>
31 : #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
32 : #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
33 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
34 : #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
35 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
36 : #include <tools/urlobj.hxx>
37 : #include <vcl/msgbox.hxx>
38 : #include <svl/itemset.hxx>
39 : #include <svl/eitem.hxx>
40 : #include <svl/intitem.hxx>
41 : #include <svl/stritem.hxx>
42 :
43 : using namespace ::com::sun::star;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::ui::dialogs;
46 : using namespace ::com::sun::star::uno;
47 :
48 : namespace sfx2 {
49 :
50 0 : DocumentInserter::DocumentInserter(
51 : const OUString& rFactory, bool const bEnableMultiSelection) :
52 :
53 : m_sDocFactory ( rFactory )
54 : , m_nDlgFlags ( (bEnableMultiSelection)
55 : ? (SFXWB_INSERT|SFXWB_MULTISELECTION)
56 : : SFXWB_INSERT )
57 : , m_nError ( ERRCODE_NONE )
58 : , m_pFileDlg ( NULL )
59 0 : , m_pItemSet ( NULL )
60 : {
61 0 : }
62 :
63 0 : DocumentInserter::~DocumentInserter()
64 : {
65 0 : delete m_pFileDlg;
66 0 : }
67 :
68 0 : void DocumentInserter::StartExecuteModal( const Link<>& _rDialogClosedLink )
69 : {
70 0 : m_aDialogClosedLink = _rDialogClosedLink;
71 0 : m_nError = ERRCODE_NONE;
72 0 : if ( !m_pFileDlg )
73 : {
74 : m_pFileDlg = new FileDialogHelper(
75 : ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
76 0 : m_nDlgFlags, m_sDocFactory );
77 : }
78 0 : m_pFileDlg->StartExecuteModal( LINK( this, DocumentInserter, DialogClosedHdl ) );
79 0 : }
80 :
81 0 : SfxMedium* DocumentInserter::CreateMedium()
82 : {
83 0 : SfxMedium* pMedium = NULL;
84 0 : if (!m_nError && m_pItemSet && !m_pURLList.empty())
85 : {
86 : DBG_ASSERT( m_pURLList.size() == 1, "DocumentInserter::CreateMedium(): invalid URL list count" );
87 0 : OUString sURL(m_pURLList[0]);
88 : pMedium = new SfxMedium(
89 0 : sURL, SFX_STREAM_READONLY,
90 0 : SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_pItemSet );
91 0 : pMedium->UseInteractionHandler( true );
92 0 : SfxFilterMatcher* pMatcher = NULL;
93 0 : if ( !m_sDocFactory.isEmpty() )
94 0 : pMatcher = new SfxFilterMatcher( m_sDocFactory );
95 : else
96 0 : pMatcher = new SfxFilterMatcher();
97 :
98 0 : const SfxFilter* pFilter = NULL;
99 0 : sal_uInt32 nError = pMatcher->DetectFilter( *pMedium, &pFilter, false );
100 0 : if ( nError == ERRCODE_NONE && pFilter )
101 0 : pMedium->SetFilter( pFilter );
102 : else
103 0 : DELETEZ( pMedium );
104 :
105 0 : if ( pMedium && CheckPasswd_Impl( 0, SfxGetpApp()->GetPool(), pMedium ) == ERRCODE_ABORT )
106 0 : pMedium = NULL;
107 :
108 0 : DELETEZ( pMatcher );
109 : }
110 :
111 0 : return pMedium;
112 : }
113 :
114 0 : SfxMediumList* DocumentInserter::CreateMediumList()
115 : {
116 0 : SfxMediumList* pMediumList = new SfxMediumList;
117 0 : if (!m_nError && m_pItemSet && !m_pURLList.empty())
118 : {
119 0 : for(std::vector<OUString>::const_iterator i = m_pURLList.begin(); i != m_pURLList.end(); ++i)
120 : {
121 : SfxMedium* pMedium = new SfxMedium(
122 0 : *i, SFX_STREAM_READONLY,
123 0 : SfxGetpApp()->GetFilterMatcher().GetFilter4FilterName( m_sFilter ), m_pItemSet );
124 :
125 0 : pMedium->UseInteractionHandler( true );
126 :
127 0 : SfxFilterMatcher aMatcher( m_sDocFactory );
128 0 : const SfxFilter* pFilter = NULL;
129 0 : sal_uInt32 nError = aMatcher.DetectFilter( *pMedium, &pFilter, false );
130 0 : if ( nError == ERRCODE_NONE && pFilter )
131 0 : pMedium->SetFilter( pFilter );
132 : else
133 0 : DELETEZ( pMedium );
134 :
135 0 : if( pMedium && CheckPasswd_Impl( 0, SfxGetpApp()->GetPool(), pMedium ) != ERRCODE_ABORT )
136 0 : pMediumList->push_back( pMedium );
137 : else
138 0 : delete pMedium;
139 0 : }
140 : }
141 :
142 0 : return pMediumList;
143 : }
144 :
145 0 : void impl_FillURLList( sfx2::FileDialogHelper* _pFileDlg, std::vector<OUString>& _rpURLList )
146 : {
147 : DBG_ASSERT( _pFileDlg, "DocumentInserter::fillURLList(): invalid file dialog" );
148 :
149 0 : Sequence < OUString > aPathSeq = _pFileDlg->GetSelectedFiles();
150 :
151 0 : if ( aPathSeq.getLength() )
152 : {
153 0 : _rpURLList.clear();
154 :
155 0 : for ( sal_Int32 i = 0; i < aPathSeq.getLength(); ++i )
156 : {
157 0 : INetURLObject aPathObj( aPathSeq[i] );
158 0 : _rpURLList.push_back(aPathObj.GetMainURL(INetURLObject::NO_DECODE));
159 0 : }
160 0 : }
161 0 : }
162 :
163 0 : IMPL_LINK_NOARG(DocumentInserter, DialogClosedHdl)
164 : {
165 : DBG_ASSERT( m_pFileDlg, "DocumentInserter::DialogClosedHdl(): no file dialog" );
166 :
167 0 : m_nError = m_pFileDlg->GetError();
168 0 : if ( ERRCODE_NONE == m_nError )
169 0 : impl_FillURLList( m_pFileDlg, m_pURLList );
170 :
171 0 : Reference < XFilePicker > xFP = m_pFileDlg->GetFilePicker();
172 0 : Reference < XFilePickerControlAccess > xCtrlAccess( xFP, UNO_QUERY );
173 0 : if ( xCtrlAccess.is() )
174 : {
175 : // always create a new itemset
176 0 : m_pItemSet = new SfxAllItemSet( SfxGetpApp()->GetPool() );
177 :
178 0 : short nDlgType = m_pFileDlg->GetDialogType();
179 : bool bHasPassword = (
180 : TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD == nDlgType
181 0 : || TemplateDescription::FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS == nDlgType );
182 :
183 : // check, whether or not we have to display a password box
184 0 : if ( bHasPassword && m_pFileDlg->IsPasswordEnabled() )
185 : {
186 : try
187 : {
188 0 : Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_PASSWORD, 0 );
189 0 : bool bPassWord = false;
190 0 : if ( ( aValue >>= bPassWord ) && bPassWord )
191 : {
192 : // ask for the password
193 0 : ScopedVclPtrInstance< SfxPasswordDialog > aPasswordDlg(nullptr);
194 0 : aPasswordDlg->ShowExtras( SfxShowExtras::CONFIRM );
195 0 : short nRet = aPasswordDlg->Execute();
196 0 : if ( RET_OK == nRet )
197 : {
198 0 : OUString aPasswd = aPasswordDlg->GetPassword();
199 0 : m_pItemSet->Put( SfxStringItem( SID_PASSWORD, aPasswd ) );
200 : }
201 : else
202 : {
203 0 : DELETEZ( m_pItemSet );
204 0 : return 0;
205 0 : }
206 0 : }
207 : }
208 0 : catch( const IllegalArgumentException& ){}
209 : }
210 :
211 0 : if ( SFXWB_EXPORT == ( m_nDlgFlags & SFXWB_EXPORT ) )
212 : {
213 : try
214 : {
215 0 : Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_SELECTION, 0 );
216 0 : bool bSelection = false;
217 0 : if ( aValue >>= bSelection )
218 0 : m_pItemSet->Put( SfxBoolItem( SID_SELECTION, bSelection ) );
219 : }
220 0 : catch( const IllegalArgumentException& )
221 : {
222 : OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
223 : }
224 : }
225 :
226 :
227 : // set the read-only flag. When inserting a file, this flag is always set
228 0 : if ( SFXWB_INSERT == ( m_nDlgFlags & SFXWB_INSERT ) )
229 0 : m_pItemSet->Put( SfxBoolItem( SID_DOC_READONLY, true ) );
230 : else
231 : {
232 0 : if ( ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType ) )
233 : {
234 : try
235 : {
236 0 : Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::CHECKBOX_READONLY, 0 );
237 0 : bool bReadOnly = false;
238 0 : if ( ( aValue >>= bReadOnly ) && bReadOnly )
239 0 : m_pItemSet->Put( SfxBoolItem( SID_DOC_READONLY, bReadOnly ) );
240 : }
241 0 : catch( const IllegalArgumentException& )
242 : {
243 : OSL_FAIL( "FileDialogHelper_Impl::execute: caught an IllegalArgumentException!" );
244 : }
245 : }
246 : }
247 :
248 0 : if ( TemplateDescription::FILEOPEN_READONLY_VERSION == nDlgType )
249 : {
250 : try
251 : {
252 0 : Any aValue = xCtrlAccess->getValue( ExtendedFilePickerElementIds::LISTBOX_VERSION,
253 0 : ControlActions::GET_SELECTED_ITEM_INDEX );
254 0 : sal_Int32 nVersion = 0;
255 0 : if ( ( aValue >>= nVersion ) && nVersion > 0 )
256 : // open a special version; 0 == current version
257 0 : m_pItemSet->Put( SfxInt16Item( SID_VERSION, (short)nVersion ) );
258 : }
259 0 : catch( const IllegalArgumentException& ){}
260 : }
261 : }
262 :
263 0 : m_sFilter = m_pFileDlg->GetRealFilter();
264 :
265 0 : if ( m_aDialogClosedLink.IsSet() )
266 0 : m_aDialogClosedLink.Call( m_pFileDlg );
267 :
268 0 : return 0;
269 : }
270 :
271 648 : } // namespace sfx2
272 :
273 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|