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