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 "contentenumeration.hxx"
21 : #include <svtools/inettbc.hxx>
22 : #include <svtools/imagemgr.hxx>
23 :
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/sdbc/XResultSet.hpp>
26 : #include <com/sun/star/sdbc/XRow.hpp>
27 : #include <com/sun/star/ucb/XDynamicResultSet.hpp>
28 : #include <com/sun/star/ucb/XContentAccess.hpp>
29 : #include <com/sun/star/util/DateTime.hpp>
30 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 : #include <com/sun/star/document/DocumentProperties.hpp>
32 : #include <comphelper/processfactory.hxx>
33 : #include <tools/debug.hxx>
34 : #include <vcl/svapp.hxx>
35 : #include <osl/mutex.hxx>
36 :
37 : #include <memory>
38 :
39 :
40 : namespace svt
41 : {
42 :
43 :
44 : #define ROW_TITLE 1
45 : #define ROW_SIZE 2
46 : #define ROW_DATE_MOD 3
47 : #define ROW_DATE_CREATE 4
48 : #define ROW_IS_FOLDER 5
49 : #define ROW_TARGET_URL 6
50 : #define ROW_IS_HIDDEN 7
51 : #define ROW_IS_VOLUME 8
52 : #define ROW_IS_REMOTE 9
53 : #define ROW_IS_REMOVABLE 10
54 : #define ROW_IS_FLOPPY 11
55 : #define ROW_IS_COMPACTDISC 12
56 :
57 : #define CONVERT_DATETIME( aUnoDT, aToolsDT ) \
58 : aToolsDT = ::DateTime( Date( aUnoDT.Day, aUnoDT.Month, aUnoDT.Year ), \
59 : Time( aUnoDT.Hours, aUnoDT.Minutes, aUnoDT.Seconds, aUnoDT.NanoSeconds ) );
60 :
61 : using ::com::sun::star::uno::Reference;
62 : using ::com::sun::star::uno::Sequence;
63 : using ::com::sun::star::uno::Exception;
64 : using ::com::sun::star::uno::UNO_QUERY;
65 : using ::com::sun::star::uno::Any;
66 : using ::com::sun::star::util::DateTime;
67 : using ::com::sun::star::sdbc::XResultSet;
68 : using ::com::sun::star::sdbc::XRow;
69 : using ::com::sun::star::ucb::XDynamicResultSet;
70 : using ::com::sun::star::ucb::CommandAbortedException;
71 : using ::com::sun::star::ucb::XContentAccess;
72 : using ::com::sun::star::ucb::XCommandEnvironment;
73 : using ::com::sun::star::beans::XPropertySet;
74 : using ::com::sun::star::beans::PropertyValue;
75 : using ::com::sun::star::document::DocumentProperties;
76 : using ::ucbhelper::ResultSetInclude;
77 : using ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS;
78 :
79 :
80 : //= FileViewContentEnumerator
81 :
82 :
83 0 : FileViewContentEnumerator::FileViewContentEnumerator(
84 : const Reference< XCommandEnvironment >& _rxCommandEnv,
85 : ContentData& _rContentToFill, ::osl::Mutex& _rContentMutex,
86 : const IContentTitleTranslation* _pTranslator )
87 : :Thread ( "FileViewContentEnumerator" )
88 : ,m_rContent ( _rContentToFill )
89 : ,m_rContentMutex ( _rContentMutex )
90 : ,m_xCommandEnv ( _rxCommandEnv )
91 : ,m_pTranslator ( _pTranslator )
92 : ,m_pResultHandler ( NULL )
93 : ,m_bCancelled ( false )
94 0 : ,m_rBlackList ( ::com::sun::star::uno::Sequence< OUString >() )
95 : {
96 0 : }
97 :
98 :
99 0 : FileViewContentEnumerator::~FileViewContentEnumerator()
100 : {
101 0 : }
102 :
103 :
104 0 : void FileViewContentEnumerator::cancel()
105 : {
106 0 : ::osl::MutexGuard aGuard( m_aMutex );
107 0 : m_bCancelled = true;
108 0 : m_pResultHandler = NULL;
109 0 : m_pTranslator = NULL;
110 0 : m_aFolder.aContent = ::ucbhelper::Content();
111 0 : m_aFolder.sURL = "";
112 0 : }
113 :
114 :
115 0 : EnumerationResult FileViewContentEnumerator::enumerateFolderContentSync(
116 : const FolderDescriptor& _rFolder,
117 : const ::com::sun::star::uno::Sequence< OUString >& rBlackList )
118 : {
119 : {
120 0 : ::osl::MutexGuard aGuard( m_aMutex );
121 0 : m_aFolder = _rFolder;
122 0 : m_pResultHandler = NULL;
123 0 : m_rBlackList = rBlackList;
124 : }
125 0 : return enumerateFolderContent();
126 : }
127 :
128 :
129 0 : void FileViewContentEnumerator::enumerateFolderContent(
130 : const FolderDescriptor& _rFolder, IEnumerationResultHandler* _pResultHandler )
131 : {
132 0 : ::osl::MutexGuard aGuard( m_aMutex );
133 0 : m_aFolder = _rFolder;
134 0 : m_pResultHandler = _pResultHandler;
135 :
136 : OSL_ENSURE( m_aFolder.aContent.get().is() || !m_aFolder.sURL.isEmpty(),
137 : "FileViewContentEnumerator::enumerateFolderContent: invalid folder descriptor!" );
138 :
139 0 : launch();
140 : //TODO: a protocol is missing how to join with the launched thread
141 : // before exit(3), to ensure the thread is no longer relying on any
142 : // infrastructure while that infrastructure is being shut down in
143 : // atexit handlers
144 0 : }
145 :
146 :
147 0 : EnumerationResult FileViewContentEnumerator::enumerateFolderContent()
148 : {
149 0 : EnumerationResult eResult = ERROR;
150 : try
151 : {
152 :
153 0 : Reference< XResultSet > xResultSet;
154 0 : Sequence< OUString > aProps(12);
155 :
156 0 : aProps[0] = "Title";
157 0 : aProps[1] = "Size";
158 0 : aProps[2] = "DateModified";
159 0 : aProps[3] = "DateCreated";
160 0 : aProps[4] = "IsFolder";
161 0 : aProps[5] = "TargetURL";
162 0 : aProps[6] = "IsHidden";
163 0 : aProps[7] = "IsVolume";
164 0 : aProps[8] = "IsRemote";
165 0 : aProps[9] = "IsRemoveable";
166 0 : aProps[10] = "IsFloppy";
167 0 : aProps[11] = "IsCompactDisc";
168 :
169 0 : Reference< XCommandEnvironment > xEnvironment;
170 : try
171 : {
172 0 : FolderDescriptor aFolder;
173 : {
174 0 : ::osl::MutexGuard aGuard( m_aMutex );
175 0 : aFolder = m_aFolder;
176 0 : xEnvironment = m_xCommandEnv;
177 : }
178 0 : if ( !aFolder.aContent.get().is() )
179 : {
180 0 : aFolder.aContent = ::ucbhelper::Content( aFolder.sURL, xEnvironment, comphelper::getProcessComponentContext() );
181 : {
182 0 : ::osl::MutexGuard aGuard( m_aMutex );
183 0 : m_aFolder.aContent = aFolder.aContent;
184 : }
185 : }
186 :
187 0 : Reference< XDynamicResultSet > xDynResultSet;
188 0 : ResultSetInclude eInclude = INCLUDE_FOLDERS_AND_DOCUMENTS;
189 0 : xDynResultSet = aFolder.aContent.createDynamicCursor( aProps, eInclude );
190 :
191 0 : if ( xDynResultSet.is() )
192 0 : xResultSet = xDynResultSet->getStaticResultSet();
193 : }
194 0 : catch( CommandAbortedException& )
195 : {
196 : SAL_WARN( "svtools.contnr", "createCursor: CommandAbortedException" );
197 : }
198 0 : catch( Exception& )
199 : {
200 : }
201 :
202 0 : if ( xResultSet.is() )
203 : {
204 0 : Reference< XRow > xRow( xResultSet, UNO_QUERY );
205 0 : Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
206 :
207 : try
208 : {
209 : SortingData_Impl* pData;
210 0 : DateTime aDT;
211 :
212 0 : bool bCancelled = false;
213 0 : while ( !bCancelled && xResultSet->next() )
214 : {
215 0 : sal_Bool bIsHidden = xRow->getBoolean( ROW_IS_HIDDEN );
216 : // don't show hidden files
217 0 : if ( !bIsHidden || xRow->wasNull() )
218 : {
219 0 : pData = NULL;
220 :
221 0 : aDT = xRow->getTimestamp( ROW_DATE_MOD );
222 0 : sal_Bool bContainsDate = !xRow->wasNull();
223 0 : if ( !bContainsDate )
224 : {
225 0 : aDT = xRow->getTimestamp( ROW_DATE_CREATE );
226 0 : bContainsDate = !xRow->wasNull();
227 : }
228 :
229 0 : OUString aContentURL = xContentAccess->queryContentIdentifierString();
230 0 : OUString aTargetURL = xRow->getString( ROW_TARGET_URL );
231 0 : sal_Bool bHasTargetURL = !xRow->wasNull() && !aTargetURL.isEmpty();
232 :
233 0 : OUString sRealURL = bHasTargetURL ? aTargetURL : aContentURL;
234 :
235 : // check for restrictions
236 : {
237 0 : ::osl::MutexGuard aGuard( m_aMutex );
238 0 : if ( /* m_rBlackList.hasElements() && */ URLOnBlackList ( sRealURL ) )
239 0 : continue;
240 : }
241 :
242 0 : pData = new SortingData_Impl;
243 0 : pData->maTargetURL = sRealURL;
244 :
245 0 : pData->mbIsFolder = xRow->getBoolean( ROW_IS_FOLDER ) && !xRow->wasNull();
246 0 : pData->mbIsVolume = xRow->getBoolean( ROW_IS_VOLUME ) && !xRow->wasNull();
247 0 : pData->mbIsRemote = xRow->getBoolean( ROW_IS_REMOTE ) && !xRow->wasNull();
248 0 : pData->mbIsRemoveable = xRow->getBoolean( ROW_IS_REMOVABLE ) && !xRow->wasNull();
249 0 : pData->mbIsFloppy = xRow->getBoolean( ROW_IS_FLOPPY ) && !xRow->wasNull();
250 0 : pData->mbIsCompactDisc = xRow->getBoolean( ROW_IS_COMPACTDISC ) && !xRow->wasNull();
251 0 : pData->SetNewTitle( xRow->getString( ROW_TITLE ) );
252 0 : pData->maSize = xRow->getLong( ROW_SIZE );
253 :
254 0 : if ( bHasTargetURL &&
255 0 : INetURLObject( aContentURL ).GetProtocol() == INET_PROT_VND_SUN_STAR_HIER )
256 : {
257 0 : ::ucbhelper::Content aCnt( aTargetURL, xEnvironment, comphelper::getProcessComponentContext() );
258 : try
259 : {
260 0 : aCnt.getPropertyValue("Size") >>= pData->maSize;
261 0 : aCnt.getPropertyValue("DateModified") >>= aDT;
262 : }
263 0 : catch (...) {}
264 : }
265 :
266 0 : if ( bContainsDate )
267 : {
268 0 : CONVERT_DATETIME( aDT, pData->maModDate );
269 : }
270 :
271 0 : if ( pData->mbIsFolder )
272 : {
273 0 : SolarMutexGuard aGuard;
274 : ::svtools::VolumeInfo aVolInfo( pData->mbIsVolume, pData->mbIsRemote,
275 : pData->mbIsRemoveable, pData->mbIsFloppy,
276 0 : pData->mbIsCompactDisc );
277 0 : pData->maType = SvFileInformationManager::GetFolderDescription( aVolInfo );
278 : }
279 : else
280 0 : pData->maType = SvFileInformationManager::GetFileDescription(
281 0 : INetURLObject( pData->maTargetURL ) );
282 :
283 : // replace names on demand
284 : {
285 0 : ::osl::MutexGuard aGuard( m_aMutex );
286 0 : if( m_pTranslator )
287 : {
288 0 : OUString sNewTitle;
289 0 : sal_Bool bTranslated = sal_False;
290 :
291 0 : if ( pData->mbIsFolder )
292 0 : bTranslated = m_pTranslator->GetTranslation( pData->GetTitle(), sNewTitle );
293 : else
294 0 : bTranslated = implGetDocTitle( pData->maTargetURL, sNewTitle );
295 :
296 0 : if ( bTranslated )
297 0 : pData->ChangeTitle( sNewTitle );
298 0 : }
299 : }
300 :
301 : {
302 0 : ::osl::MutexGuard aGuard( m_rContentMutex );
303 0 : m_rContent.push_back( pData );
304 0 : }
305 : }
306 :
307 : {
308 0 : ::osl::MutexGuard aGuard( m_aMutex );
309 0 : bCancelled = m_bCancelled;
310 : }
311 : }
312 0 : eResult = SUCCESS;
313 : }
314 0 : catch( CommandAbortedException& )
315 : {
316 : SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException while enumerating!" );
317 : }
318 0 : catch( Exception& )
319 : {
320 : SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException while enumerating!" );
321 0 : }
322 0 : }
323 : }
324 0 : catch( CommandAbortedException& )
325 : {
326 : SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException!" );
327 : }
328 0 : catch( Exception& )
329 : {
330 : SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException!" );
331 : }
332 :
333 0 : IEnumerationResultHandler* pHandler = NULL;
334 : {
335 0 : ::osl::MutexGuard aGuard( m_aMutex );
336 0 : pHandler = m_pResultHandler;
337 0 : if ( m_bCancelled )
338 0 : return ERROR;
339 : }
340 :
341 : {
342 0 : ::osl::MutexGuard aGuard( m_rContentMutex );
343 0 : if ( eResult != SUCCESS )
344 : // clear any "intermediate" and unfinished result
345 0 : m_rContent.clear();
346 : }
347 :
348 0 : if ( pHandler )
349 0 : pHandler->enumerationDone( eResult );
350 0 : return eResult;
351 : }
352 :
353 :
354 :
355 0 : sal_Bool FileViewContentEnumerator::URLOnBlackList ( const OUString& sRealURL )
356 : {
357 0 : OUString entryName = sRealURL.copy( sRealURL.lastIndexOf( '/' ) + 1 );
358 :
359 0 : for (int i = 0; i < m_rBlackList.getLength() ; i++)
360 : {
361 0 : if ( entryName.equals( m_rBlackList[i] ) )
362 0 : return true;
363 : }
364 :
365 0 : return false;
366 : }
367 :
368 :
369 0 : sal_Bool FileViewContentEnumerator::implGetDocTitle( const OUString& _rTargetURL, OUString& _rRet ) const
370 : {
371 0 : sal_Bool bRet = sal_False;
372 :
373 : try
374 : {
375 0 : ::osl::MutexGuard aGuard( m_aMutex );
376 0 : if (!m_xDocProps.is())
377 : {
378 : m_xDocProps.set(DocumentProperties::create(
379 0 : ::comphelper::getProcessComponentContext()));
380 : }
381 :
382 : assert(m_xDocProps.is());
383 0 : if (!m_xDocProps.is())
384 0 : return sal_False;
385 :
386 0 : m_xDocProps->loadFromMedium(_rTargetURL, Sequence<PropertyValue>());
387 :
388 0 : OUString const sTitle(m_xDocProps->getTitle());
389 0 : if (!sTitle.isEmpty())
390 : {
391 0 : _rRet = sTitle;
392 0 : bRet = sal_True;
393 0 : }
394 : }
395 0 : catch ( const Exception& )
396 : {
397 : }
398 :
399 0 : return bRet;
400 : }
401 :
402 :
403 0 : void FileViewContentEnumerator::execute()
404 : {
405 0 : enumerateFolderContent();
406 0 : }
407 :
408 :
409 : } // namespace svt
410 :
411 :
412 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|