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/imagemgr.hxx>
23 #include <com/sun/star/sdbc/XResultSet.hpp>
24 #include <com/sun/star/sdbc/XRow.hpp>
25 #include <com/sun/star/ucb/CommandAbortedException.hpp>
26 #include <com/sun/star/ucb/XDynamicResultSet.hpp>
27 #include <com/sun/star/ucb/XContentAccess.hpp>
28 #include <com/sun/star/util/DateTime.hpp>
29 #include <com/sun/star/document/DocumentProperties.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <comphelper/sequence.hxx>
32 #include <vcl/svapp.hxx>
33 #include <osl/mutex.hxx>
34 #include <sal/log.hxx>
35 #include <osl/diagnose.h>
36 #include <tools/diagnose_ex.h>
37 #include <tools/urlobj.hxx>
45 #define ROW_DATE_MOD 3
46 #define ROW_DATE_CREATE 4
47 #define ROW_IS_FOLDER 5
48 #define ROW_TARGET_URL 6
49 #define ROW_IS_HIDDEN 7
50 #define ROW_IS_VOLUME 8
51 #define ROW_IS_REMOTE 9
52 #define ROW_IS_REMOVABLE 10
53 #define ROW_IS_FLOPPY 11
54 #define ROW_IS_COMPACTDISC 12
56 using ::com::sun::star::uno::Reference
;
57 using ::com::sun::star::uno::Sequence
;
58 using ::com::sun::star::uno::Exception
;
59 using ::com::sun::star::uno::UNO_QUERY
;
60 using ::com::sun::star::util::DateTime
;
61 using ::com::sun::star::sdbc::XResultSet
;
62 using ::com::sun::star::sdbc::XRow
;
63 using ::com::sun::star::ucb::XDynamicResultSet
;
64 using ::com::sun::star::ucb::CommandAbortedException
;
65 using ::com::sun::star::ucb::XContentAccess
;
66 using ::com::sun::star::ucb::XCommandEnvironment
;
67 using ::com::sun::star::beans::PropertyValue
;
68 using ::com::sun::star::document::DocumentProperties
;
69 using ::ucbhelper::ResultSetInclude
;
70 using ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS
;
73 //= FileViewContentEnumerator
76 FileViewContentEnumerator::FileViewContentEnumerator(
77 const Reference
< XCommandEnvironment
>& _rxCommandEnv
,
78 ContentData
& _rContentToFill
, ::osl::Mutex
& _rContentMutex
)
79 :Thread ( "FileViewContentEnumerator" )
80 ,m_rContent ( _rContentToFill
)
81 ,m_rContentMutex ( _rContentMutex
)
82 ,m_xCommandEnv ( _rxCommandEnv
)
83 ,m_pResultHandler ( nullptr )
84 ,m_bCancelled ( false )
85 ,m_rBlackList ( css::uno::Sequence
< OUString
>() )
90 FileViewContentEnumerator::~FileViewContentEnumerator()
95 void FileViewContentEnumerator::cancel()
97 ::osl::MutexGuard
aGuard( m_aMutex
);
99 m_pResultHandler
= nullptr;
100 m_aFolder
.aContent
= ::ucbhelper::Content();
101 m_aFolder
.sURL
.clear();
105 EnumerationResult
FileViewContentEnumerator::enumerateFolderContentSync(
106 const FolderDescriptor
& _rFolder
,
107 const css::uno::Sequence
< OUString
>& rBlackList
)
110 ::osl::MutexGuard
aGuard( m_aMutex
);
111 m_aFolder
= _rFolder
;
112 m_pResultHandler
= nullptr;
113 m_rBlackList
= rBlackList
;
115 return enumerateFolderContent();
119 void FileViewContentEnumerator::enumerateFolderContent(
120 const FolderDescriptor
& _rFolder
, IEnumerationResultHandler
* _pResultHandler
)
122 ::osl::MutexGuard
aGuard( m_aMutex
);
123 m_aFolder
= _rFolder
;
124 m_pResultHandler
= _pResultHandler
;
126 OSL_ENSURE( m_aFolder
.aContent
.get().is() || !m_aFolder
.sURL
.isEmpty(),
127 "FileViewContentEnumerator::enumerateFolderContent: invalid folder descriptor!" );
130 //TODO: a protocol is missing how to join with the launched thread
131 // before exit(3), to ensure the thread is no longer relying on any
132 // infrastructure while that infrastructure is being shut down in
137 EnumerationResult
FileViewContentEnumerator::enumerateFolderContent()
139 EnumerationResult eResult
= EnumerationResult::ERROR
;
143 Reference
< XResultSet
> xResultSet
;
144 Sequence
< OUString
> aProps(12);
148 aProps
[2] = "DateModified";
149 aProps
[3] = "DateCreated";
150 aProps
[4] = "IsFolder";
151 aProps
[5] = "TargetURL";
152 aProps
[6] = "IsHidden";
153 aProps
[7] = "IsVolume";
154 aProps
[8] = "IsRemote";
155 aProps
[9] = "IsRemoveable";
156 aProps
[10] = "IsFloppy";
157 aProps
[11] = "IsCompactDisc";
159 Reference
< XCommandEnvironment
> xEnvironment
;
162 FolderDescriptor aFolder
;
164 ::osl::MutexGuard
aGuard( m_aMutex
);
166 xEnvironment
= m_xCommandEnv
;
168 if ( !aFolder
.aContent
.get().is() )
170 aFolder
.aContent
= ::ucbhelper::Content( aFolder
.sURL
, xEnvironment
, comphelper::getProcessComponentContext() );
172 ::osl::MutexGuard
aGuard( m_aMutex
);
173 m_aFolder
.aContent
= aFolder
.aContent
;
177 Reference
< XDynamicResultSet
> xDynResultSet
= aFolder
.aContent
.createDynamicCursor( aProps
, INCLUDE_FOLDERS_AND_DOCUMENTS
);
179 if ( xDynResultSet
.is() )
180 xResultSet
= xDynResultSet
->getStaticResultSet();
182 catch( CommandAbortedException
& )
184 SAL_WARN( "svtools.contnr", "createCursor: CommandAbortedException" );
190 if ( xResultSet
.is() )
192 Reference
< XRow
> xRow( xResultSet
, UNO_QUERY
);
193 Reference
< XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
199 bool bCancelled
= false;
200 while ( !bCancelled
&& xResultSet
->next() )
202 bool bIsHidden
= xRow
->getBoolean( ROW_IS_HIDDEN
);
203 // don't show hidden files
204 if ( !bIsHidden
|| xRow
->wasNull() )
206 aDT
= xRow
->getTimestamp( ROW_DATE_MOD
);
207 bool bContainsDate
= !xRow
->wasNull();
208 if ( !bContainsDate
)
210 aDT
= xRow
->getTimestamp( ROW_DATE_CREATE
);
211 bContainsDate
= !xRow
->wasNull();
214 OUString aContentURL
= xContentAccess
->queryContentIdentifierString();
215 OUString aTargetURL
= xRow
->getString( ROW_TARGET_URL
);
216 bool bHasTargetURL
= !xRow
->wasNull() && !aTargetURL
.isEmpty();
218 OUString sRealURL
= bHasTargetURL
? aTargetURL
: aContentURL
;
220 // check for restrictions
222 ::osl::MutexGuard
aGuard( m_aMutex
);
223 if ( /* m_rBlackList.hasElements() && */ URLOnBlackList ( sRealURL
) )
227 std::unique_ptr
<SortingData_Impl
> pData(new SortingData_Impl
);
228 pData
->maTargetURL
= sRealURL
;
230 pData
->mbIsFolder
= xRow
->getBoolean( ROW_IS_FOLDER
) && !xRow
->wasNull();
231 pData
->mbIsVolume
= xRow
->getBoolean( ROW_IS_VOLUME
) && !xRow
->wasNull();
232 pData
->mbIsRemote
= xRow
->getBoolean( ROW_IS_REMOTE
) && !xRow
->wasNull();
233 pData
->mbIsRemoveable
= xRow
->getBoolean( ROW_IS_REMOVABLE
) && !xRow
->wasNull();
234 pData
->mbIsFloppy
= xRow
->getBoolean( ROW_IS_FLOPPY
) && !xRow
->wasNull();
235 pData
->mbIsCompactDisc
= xRow
->getBoolean( ROW_IS_COMPACTDISC
) && !xRow
->wasNull();
236 pData
->SetNewTitle( xRow
->getString( ROW_TITLE
) );
237 pData
->maSize
= xRow
->getLong( ROW_SIZE
);
239 if ( bHasTargetURL
&&
240 INetURLObject( aContentURL
).GetProtocol() == INetProtocol::VndSunStarHier
)
242 ::ucbhelper::Content
aCnt( aTargetURL
, xEnvironment
, comphelper::getProcessComponentContext() );
245 aCnt
.getPropertyValue("Size") >>= pData
->maSize
;
246 aCnt
.getPropertyValue("DateModified") >>= aDT
;
253 pData
->maModDate
= ::DateTime( aDT
);
256 if ( pData
->mbIsFolder
)
258 SolarMutexGuard aGuard
;
259 ::svtools::VolumeInfo
aVolInfo( pData
->mbIsVolume
, pData
->mbIsRemote
,
260 pData
->mbIsRemoveable
, pData
->mbIsFloppy
,
261 pData
->mbIsCompactDisc
);
262 pData
->maType
= SvFileInformationManager::GetFolderDescription( aVolInfo
);
265 pData
->maType
= SvFileInformationManager::GetFileDescription(
266 INetURLObject( pData
->maTargetURL
) );
269 ::osl::MutexGuard
aGuard( m_rContentMutex
);
270 m_rContent
.push_back( std::move(pData
) );
275 ::osl::MutexGuard
aGuard( m_aMutex
);
276 bCancelled
= m_bCancelled
;
279 eResult
= EnumerationResult::SUCCESS
;
281 catch( Exception
const & )
283 TOOLS_WARN_EXCEPTION( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception while enumerating");
287 catch( Exception
const & )
289 TOOLS_WARN_EXCEPTION( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent" );
292 IEnumerationResultHandler
* pHandler
= nullptr;
294 ::osl::MutexGuard
aGuard( m_aMutex
);
295 pHandler
= m_pResultHandler
;
297 return EnumerationResult::ERROR
;
301 ::osl::MutexGuard
aGuard( m_rContentMutex
);
302 if ( eResult
!= EnumerationResult::SUCCESS
)
303 // clear any "intermediate" and unfinished result
308 pHandler
->enumerationDone( eResult
);
313 bool FileViewContentEnumerator::URLOnBlackList ( const OUString
& sRealURL
)
315 OUString entryName
= sRealURL
.copy( sRealURL
.lastIndexOf( '/' ) + 1 );
317 return comphelper::findValue(m_rBlackList
, entryName
) != -1;
321 void FileViewContentEnumerator::execute()
323 enumerateFolderContent();
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */