1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "contentenumeration.hxx"
21 #include <svtools/inettbc.hxx>
22 #include <svtools/imagemgr.hxx>
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>
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
53 using ::com::sun::star::uno::Reference
;
54 using ::com::sun::star::uno::Sequence
;
55 using ::com::sun::star::uno::Exception
;
56 using ::com::sun::star::uno::UNO_QUERY
;
57 using ::com::sun::star::uno::Any
;
58 using ::com::sun::star::util::DateTime
;
59 using ::com::sun::star::sdbc::XResultSet
;
60 using ::com::sun::star::sdbc::XRow
;
61 using ::com::sun::star::ucb::XDynamicResultSet
;
62 using ::com::sun::star::ucb::CommandAbortedException
;
63 using ::com::sun::star::ucb::XContentAccess
;
64 using ::com::sun::star::ucb::XCommandEnvironment
;
65 using ::com::sun::star::beans::XPropertySet
;
66 using ::com::sun::star::beans::PropertyValue
;
67 using ::com::sun::star::document::DocumentProperties
;
68 using ::ucbhelper::ResultSetInclude
;
69 using ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS
;
72 //= FileViewContentEnumerator
75 FileViewContentEnumerator::FileViewContentEnumerator(
76 const Reference
< XCommandEnvironment
>& _rxCommandEnv
,
77 ContentData
& _rContentToFill
, ::osl::Mutex
& _rContentMutex
,
78 const IContentTitleTranslation
* _pTranslator
)
79 :Thread ( "FileViewContentEnumerator" )
80 ,m_rContent ( _rContentToFill
)
81 ,m_rContentMutex ( _rContentMutex
)
82 ,m_xCommandEnv ( _rxCommandEnv
)
83 ,m_pTranslator ( _pTranslator
)
84 ,m_pResultHandler ( NULL
)
85 ,m_bCancelled ( false )
86 ,m_rBlackList ( ::com::sun::star::uno::Sequence
< OUString
>() )
91 FileViewContentEnumerator::~FileViewContentEnumerator()
96 void FileViewContentEnumerator::cancel()
98 ::osl::MutexGuard
aGuard( m_aMutex
);
100 m_pResultHandler
= NULL
;
101 m_pTranslator
= NULL
;
102 m_aFolder
.aContent
= ::ucbhelper::Content();
103 m_aFolder
.sURL
.clear();
107 EnumerationResult
FileViewContentEnumerator::enumerateFolderContentSync(
108 const FolderDescriptor
& _rFolder
,
109 const ::com::sun::star::uno::Sequence
< OUString
>& rBlackList
)
112 ::osl::MutexGuard
aGuard( m_aMutex
);
113 m_aFolder
= _rFolder
;
114 m_pResultHandler
= NULL
;
115 m_rBlackList
= rBlackList
;
117 return enumerateFolderContent();
121 void FileViewContentEnumerator::enumerateFolderContent(
122 const FolderDescriptor
& _rFolder
, IEnumerationResultHandler
* _pResultHandler
)
124 ::osl::MutexGuard
aGuard( m_aMutex
);
125 m_aFolder
= _rFolder
;
126 m_pResultHandler
= _pResultHandler
;
128 OSL_ENSURE( m_aFolder
.aContent
.get().is() || !m_aFolder
.sURL
.isEmpty(),
129 "FileViewContentEnumerator::enumerateFolderContent: invalid folder descriptor!" );
132 //TODO: a protocol is missing how to join with the launched thread
133 // before exit(3), to ensure the thread is no longer relying on any
134 // infrastructure while that infrastructure is being shut down in
139 EnumerationResult
FileViewContentEnumerator::enumerateFolderContent()
141 EnumerationResult eResult
= ERROR
;
145 Reference
< XResultSet
> xResultSet
;
146 Sequence
< OUString
> aProps(12);
150 aProps
[2] = "DateModified";
151 aProps
[3] = "DateCreated";
152 aProps
[4] = "IsFolder";
153 aProps
[5] = "TargetURL";
154 aProps
[6] = "IsHidden";
155 aProps
[7] = "IsVolume";
156 aProps
[8] = "IsRemote";
157 aProps
[9] = "IsRemoveable";
158 aProps
[10] = "IsFloppy";
159 aProps
[11] = "IsCompactDisc";
161 Reference
< XCommandEnvironment
> xEnvironment
;
164 FolderDescriptor aFolder
;
166 ::osl::MutexGuard
aGuard( m_aMutex
);
168 xEnvironment
= m_xCommandEnv
;
170 if ( !aFolder
.aContent
.get().is() )
172 aFolder
.aContent
= ::ucbhelper::Content( aFolder
.sURL
, xEnvironment
, comphelper::getProcessComponentContext() );
174 ::osl::MutexGuard
aGuard( m_aMutex
);
175 m_aFolder
.aContent
= aFolder
.aContent
;
179 Reference
< XDynamicResultSet
> xDynResultSet
;
180 ResultSetInclude eInclude
= INCLUDE_FOLDERS_AND_DOCUMENTS
;
181 xDynResultSet
= aFolder
.aContent
.createDynamicCursor( aProps
, eInclude
);
183 if ( xDynResultSet
.is() )
184 xResultSet
= xDynResultSet
->getStaticResultSet();
186 catch( CommandAbortedException
& )
188 SAL_WARN( "svtools.contnr", "createCursor: CommandAbortedException" );
194 if ( xResultSet
.is() )
196 Reference
< XRow
> xRow( xResultSet
, UNO_QUERY
);
197 Reference
< XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
201 SortingData_Impl
* pData
;
204 bool bCancelled
= false;
205 while ( !bCancelled
&& xResultSet
->next() )
207 bool bIsHidden
= xRow
->getBoolean( ROW_IS_HIDDEN
);
208 // don't show hidden files
209 if ( !bIsHidden
|| xRow
->wasNull() )
213 aDT
= xRow
->getTimestamp( ROW_DATE_MOD
);
214 bool bContainsDate
= !xRow
->wasNull();
215 if ( !bContainsDate
)
217 aDT
= xRow
->getTimestamp( ROW_DATE_CREATE
);
218 bContainsDate
= !xRow
->wasNull();
221 OUString aContentURL
= xContentAccess
->queryContentIdentifierString();
222 OUString aTargetURL
= xRow
->getString( ROW_TARGET_URL
);
223 bool bHasTargetURL
= !xRow
->wasNull() && !aTargetURL
.isEmpty();
225 OUString sRealURL
= bHasTargetURL
? aTargetURL
: aContentURL
;
227 // check for restrictions
229 ::osl::MutexGuard
aGuard( m_aMutex
);
230 if ( /* m_rBlackList.hasElements() && */ URLOnBlackList ( sRealURL
) )
234 pData
= new SortingData_Impl
;
235 pData
->maTargetURL
= sRealURL
;
237 pData
->mbIsFolder
= xRow
->getBoolean( ROW_IS_FOLDER
) && !xRow
->wasNull();
238 pData
->mbIsVolume
= xRow
->getBoolean( ROW_IS_VOLUME
) && !xRow
->wasNull();
239 pData
->mbIsRemote
= xRow
->getBoolean( ROW_IS_REMOTE
) && !xRow
->wasNull();
240 pData
->mbIsRemoveable
= xRow
->getBoolean( ROW_IS_REMOVABLE
) && !xRow
->wasNull();
241 pData
->mbIsFloppy
= xRow
->getBoolean( ROW_IS_FLOPPY
) && !xRow
->wasNull();
242 pData
->mbIsCompactDisc
= xRow
->getBoolean( ROW_IS_COMPACTDISC
) && !xRow
->wasNull();
243 pData
->SetNewTitle( xRow
->getString( ROW_TITLE
) );
244 pData
->maSize
= xRow
->getLong( ROW_SIZE
);
246 if ( bHasTargetURL
&&
247 INetURLObject( aContentURL
).GetProtocol() == INetProtocol::VndSunStarHier
)
249 ::ucbhelper::Content
aCnt( aTargetURL
, xEnvironment
, comphelper::getProcessComponentContext() );
252 aCnt
.getPropertyValue("Size") >>= pData
->maSize
;
253 aCnt
.getPropertyValue("DateModified") >>= aDT
;
260 pData
->maModDate
= ::DateTime( aDT
);
263 if ( pData
->mbIsFolder
)
265 SolarMutexGuard aGuard
;
266 ::svtools::VolumeInfo
aVolInfo( pData
->mbIsVolume
, pData
->mbIsRemote
,
267 pData
->mbIsRemoveable
, pData
->mbIsFloppy
,
268 pData
->mbIsCompactDisc
);
269 pData
->maType
= SvFileInformationManager::GetFolderDescription( aVolInfo
);
272 pData
->maType
= SvFileInformationManager::GetFileDescription(
273 INetURLObject( pData
->maTargetURL
) );
275 // replace names on demand
277 ::osl::MutexGuard
aGuard( m_aMutex
);
281 bool bTranslated
= false;
283 if ( pData
->mbIsFolder
)
284 bTranslated
= m_pTranslator
->GetTranslation( pData
->GetTitle(), sNewTitle
);
286 bTranslated
= implGetDocTitle( pData
->maTargetURL
, sNewTitle
);
289 pData
->ChangeTitle( sNewTitle
);
294 ::osl::MutexGuard
aGuard( m_rContentMutex
);
295 m_rContent
.push_back( pData
);
300 ::osl::MutexGuard
aGuard( m_aMutex
);
301 bCancelled
= m_bCancelled
;
306 catch( CommandAbortedException
& )
308 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException while enumerating!" );
312 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException while enumerating!" );
316 catch( CommandAbortedException
& )
318 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException!" );
322 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException!" );
325 IEnumerationResultHandler
* pHandler
= NULL
;
327 ::osl::MutexGuard
aGuard( m_aMutex
);
328 pHandler
= m_pResultHandler
;
334 ::osl::MutexGuard
aGuard( m_rContentMutex
);
335 if ( eResult
!= SUCCESS
)
336 // clear any "intermediate" and unfinished result
341 pHandler
->enumerationDone( eResult
);
347 bool FileViewContentEnumerator::URLOnBlackList ( const OUString
& sRealURL
)
349 OUString entryName
= sRealURL
.copy( sRealURL
.lastIndexOf( '/' ) + 1 );
351 for (int i
= 0; i
< m_rBlackList
.getLength() ; i
++)
353 if ( entryName
.equals( m_rBlackList
[i
] ) )
361 bool FileViewContentEnumerator::implGetDocTitle( const OUString
& _rTargetURL
, OUString
& _rRet
) const
367 ::osl::MutexGuard
aGuard( m_aMutex
);
368 if (!m_xDocProps
.is())
370 m_xDocProps
.set(DocumentProperties::create(
371 ::comphelper::getProcessComponentContext()));
374 assert(m_xDocProps
.is());
375 if (!m_xDocProps
.is())
378 m_xDocProps
->loadFromMedium(_rTargetURL
, Sequence
<PropertyValue
>());
380 OUString
const sTitle(m_xDocProps
->getTitle());
381 if (!sTitle
.isEmpty())
387 catch ( const Exception
& )
395 void FileViewContentEnumerator::execute()
397 enumerateFolderContent();
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */