LibreOffice Module fpicker (master) 1
contentenumeration.cxx
Go to the documentation of this file.
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
21#include <svtools/imagemgr.hxx>
22
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>
32#include <vcl/svapp.hxx>
33#include <osl/mutex.hxx>
34#include <osl/diagnose.h>
36#include <tools/urlobj.hxx>
37
38namespace svt
39{
40
41
42#define ROW_TITLE 1
43#define ROW_SIZE 2
44#define ROW_DATE_MOD 3
45#define ROW_DATE_CREATE 4
46#define ROW_IS_FOLDER 5
47#define ROW_TARGET_URL 6
48#define ROW_IS_HIDDEN 7
49#define ROW_IS_VOLUME 8
50#define ROW_IS_REMOTE 9
51#define ROW_IS_REMOVABLE 10
52#define ROW_IS_FLOPPY 11
53#define ROW_IS_COMPACTDISC 12
54
55 using ::com::sun::star::uno::Reference;
56 using ::com::sun::star::uno::Sequence;
57 using ::com::sun::star::uno::Exception;
58 using ::com::sun::star::uno::UNO_QUERY;
59 using ::com::sun::star::util::DateTime;
60 using ::com::sun::star::sdbc::XResultSet;
61 using ::com::sun::star::sdbc::XRow;
62 using ::com::sun::star::ucb::XDynamicResultSet;
63 using ::com::sun::star::ucb::CommandAbortedException;
64 using ::com::sun::star::ucb::XContentAccess;
65 using ::com::sun::star::ucb::XCommandEnvironment;
66 using ::ucbhelper::INCLUDE_FOLDERS_AND_DOCUMENTS;
67
68
69 //= FileViewContentEnumerator
70
71
73 const Reference< XCommandEnvironment >& _rxCommandEnv,
74 ContentData& _rContentToFill, ::osl::Mutex& _rContentMutex )
75 :Thread ( "FileViewContentEnumerator" )
76 ,m_rContent ( _rContentToFill )
77 ,m_rContentMutex ( _rContentMutex )
78 ,m_xCommandEnv ( _rxCommandEnv )
79 ,m_pResultHandler ( nullptr )
80 ,m_bCancelled ( false )
81 {
82 }
83
84
86 {
87 }
88
89
91 {
92 std::unique_lock aGuard( m_aMutex );
93 m_bCancelled = true;
94 m_pResultHandler = nullptr;
96 m_aFolder.sURL.clear();
97 }
98
99
101 const FolderDescriptor& _rFolder,
102 const css::uno::Sequence< OUString >& rDenyList )
103 {
104 {
105 std::unique_lock aGuard( m_aMutex );
106 m_aFolder = _rFolder;
107 m_pResultHandler = nullptr;
108 m_rDenyList = rDenyList;
109 }
110 return enumerateFolderContent();
111 }
112
113
115 const FolderDescriptor& _rFolder, IEnumerationResultHandler* _pResultHandler )
116 {
117 std::unique_lock aGuard( m_aMutex );
118 m_aFolder = _rFolder;
119 m_pResultHandler = _pResultHandler;
120
121 OSL_ENSURE( m_aFolder.aContent.get().is() || !m_aFolder.sURL.isEmpty(),
122 "FileViewContentEnumerator::enumerateFolderContent: invalid folder descriptor!" );
123
124 launch();
125 //TODO: a protocol is missing how to join with the launched thread
126 // before exit(3), to ensure the thread is no longer relying on any
127 // infrastructure while that infrastructure is being shut down in
128 // atexit handlers
129 }
130
131
133 {
135 try
136 {
137
138 Reference< XResultSet > xResultSet;
139 Sequence< OUString > aProps{ "Title",
140 "Size",
141 "DateModified",
142 "DateCreated",
143 "IsFolder",
144 "TargetURL",
145 "IsHidden",
146 "IsVolume",
147 "IsRemote",
148 "IsRemoveable",
149 "IsFloppy",
150 "IsCompactDisc" };
151
152 Reference< XCommandEnvironment > xEnvironment;
153 try
154 {
155 FolderDescriptor aFolder;
156 {
157 std::unique_lock aGuard( m_aMutex );
158 aFolder = m_aFolder;
159 xEnvironment = m_xCommandEnv;
160 }
161 if ( !aFolder.aContent.get().is() )
162 {
163 aFolder.aContent = ::ucbhelper::Content( aFolder.sURL, xEnvironment, comphelper::getProcessComponentContext() );
164 {
165 std::unique_lock aGuard( m_aMutex );
166 m_aFolder.aContent = aFolder.aContent;
167 }
168 }
169
170 Reference< XDynamicResultSet > xDynResultSet = aFolder.aContent.createDynamicCursor( aProps, INCLUDE_FOLDERS_AND_DOCUMENTS );
171
172 if ( xDynResultSet.is() )
173 xResultSet = xDynResultSet->getStaticResultSet();
174 }
175 catch( CommandAbortedException& )
176 {
177 TOOLS_WARN_EXCEPTION( "svtools.contnr", "");
178 }
179 catch( Exception& )
180 {
181 }
182
183 if ( xResultSet.is() )
184 {
185 Reference< XRow > xRow( xResultSet, UNO_QUERY );
186 Reference< XContentAccess > xContentAccess( xResultSet, UNO_QUERY );
187
188 try
189 {
190 DateTime aDT;
191
192 bool bCancelled = false;
193 while ( !bCancelled && xResultSet->next() )
194 {
195 bool bIsHidden = xRow->getBoolean( ROW_IS_HIDDEN );
196 // don't show hidden files
197 if ( !bIsHidden || xRow->wasNull() )
198 {
199 aDT = xRow->getTimestamp( ROW_DATE_MOD );
200 bool bContainsDate = !xRow->wasNull();
201 if ( !bContainsDate )
202 {
203 aDT = xRow->getTimestamp( ROW_DATE_CREATE );
204 bContainsDate = !xRow->wasNull();
205 }
206
207 OUString aContentURL = xContentAccess->queryContentIdentifierString();
208 OUString aTargetURL = xRow->getString( ROW_TARGET_URL );
209 bool bHasTargetURL = !xRow->wasNull() && !aTargetURL.isEmpty();
210
211 OUString sRealURL = bHasTargetURL ? aTargetURL : aContentURL;
212
213 // check for restrictions
214 {
215 std::unique_lock aGuard( m_aMutex );
216 if ( /* m_rDenyList.hasElements() && */ URLOnDenyList ( sRealURL ) )
217 continue;
218 }
219
220 std::unique_ptr<SortingData_Impl> pData(new SortingData_Impl);
221 pData->maTargetURL = sRealURL;
222
223 pData->mbIsFolder = xRow->getBoolean( ROW_IS_FOLDER ) && !xRow->wasNull();
224 pData->mbIsVolume = xRow->getBoolean( ROW_IS_VOLUME ) && !xRow->wasNull();
225 pData->mbIsRemote = xRow->getBoolean( ROW_IS_REMOTE ) && !xRow->wasNull();
226 pData->mbIsRemoveable = xRow->getBoolean( ROW_IS_REMOVABLE ) && !xRow->wasNull();
227 pData->mbIsFloppy = xRow->getBoolean( ROW_IS_FLOPPY ) && !xRow->wasNull();
228 pData->mbIsCompactDisc = xRow->getBoolean( ROW_IS_COMPACTDISC ) && !xRow->wasNull();
229 pData->SetNewTitle( xRow->getString( ROW_TITLE ) );
230 pData->maSize = xRow->getLong( ROW_SIZE );
231
232 if ( bHasTargetURL &&
233 INetURLObject( aContentURL ).GetProtocol() == INetProtocol::VndSunStarHier )
234 {
236 try
237 {
238 aCnt.getPropertyValue("Size") >>= pData->maSize;
239 aCnt.getPropertyValue("DateModified") >>= aDT;
240 }
241 catch (...) {}
242 }
243
244 if ( bContainsDate )
245 {
246 pData->maModDate = ::DateTime( aDT );
247 }
248
249 if ( pData->mbIsFolder )
250 {
251 ::svtools::VolumeInfo aVolInfo( pData->mbIsVolume, pData->mbIsRemote,
252 pData->mbIsRemoveable, pData->mbIsFloppy,
253 pData->mbIsCompactDisc );
255 }
256 else
258 INetURLObject( pData->maTargetURL ) );
259
260 {
261 ::osl::MutexGuard aGuard( m_rContentMutex );
262 m_rContent.push_back( std::move(pData) );
263 }
264 }
265
266 {
267 std::unique_lock aGuard( m_aMutex );
268 bCancelled = m_bCancelled;
269 }
270 }
272 }
273 catch( Exception const & )
274 {
275 TOOLS_WARN_EXCEPTION( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception while enumerating");
276 }
277 }
278 }
279 catch( Exception const & )
280 {
281 TOOLS_WARN_EXCEPTION( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent" );
282 }
283
284 IEnumerationResultHandler* pHandler = nullptr;
285 {
286 std::unique_lock aGuard( m_aMutex );
287 pHandler = m_pResultHandler;
288 if ( m_bCancelled )
290 }
291
292 {
293 ::osl::MutexGuard aGuard( m_rContentMutex );
294 if ( eResult != EnumerationResult::SUCCESS )
295 // clear any "intermediate" and unfinished result
296 m_rContent.clear();
297 }
298
299 if ( pHandler )
300 pHandler->enumerationDone( eResult );
301 return eResult;
302 }
303
304
305 bool FileViewContentEnumerator::URLOnDenyList ( std::u16string_view sRealURL )
306 {
307 std::u16string_view entryName = sRealURL.substr( sRealURL.rfind( '/' ) + 1 );
308
309 return comphelper::findValue(m_rDenyList, entryName) != -1;
310 }
311
312
314 {
316 }
317
318
319} // namespace svt
320
321
322/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SVT_DLLPUBLIC OUString GetFolderDescription(const svtools::VolumeInfo &rInfo)
static SVT_DLLPUBLIC OUString GetFileDescription(const INetURLObject &rObject)
virtual ~FileViewContentEnumerator() override
css::uno::Reference< css::ucb::XCommandEnvironment > m_xCommandEnv
css::uno::Sequence< OUString > m_rDenyList
EnumerationResult enumerateFolderContentSync(const FolderDescriptor &_rFolder, const css::uno::Sequence< OUString > &rDenyList)
enumerates the content of a given folder synchronously
EnumerationResult enumerateFolderContent()
::std::vector< std::unique_ptr< SortingData_Impl > > ContentData
bool URLOnDenyList(std::u16string_view sRealURL)
void cancel()
cancels the running operation.
FileViewContentEnumerator(const css::uno::Reference< css::ucb::XCommandEnvironment > &_rxCommandEnv, ContentData &_rContentToFill, ::osl::Mutex &_rContentMutex)
constructs an enumerator instance
IEnumerationResultHandler * m_pResultHandler
virtual void enumerationDone(EnumerationResult _eResult)=0
css::uno::Any getPropertyValue(const OUString &rPropertyName)
css::uno::Reference< css::ucb::XContent > get() const
css::uno::Reference< css::ucb::XDynamicResultSet > createDynamicCursor(const css::uno::Sequence< OUString > &rPropertyNames, ResultSetInclude eMode=INCLUDE_FOLDERS_AND_DOCUMENTS)
Content_Impl & m_rContent
#define ROW_IS_FLOPPY
#define ROW_IS_COMPACTDISC
#define ROW_TARGET_URL
#define ROW_IS_FOLDER
#define ROW_TITLE
#define ROW_IS_VOLUME
#define ROW_DATE_MOD
#define ROW_SIZE
#define ROW_IS_HIDDEN
#define ROW_IS_REMOVABLE
#define ROW_IS_REMOTE
#define ROW_DATE_CREATE
#define TOOLS_WARN_EXCEPTION(area, stream)
std::unique_ptr< sal_Int32[]> pData
@ Exception
sal_Int32 findValue(const css::uno::Sequence< T1 > &_rList, const T2 &_rValue)
Reference< XComponentContext > getProcessComponentContext()
@ ERROR
the enumeration was successful
INCLUDE_FOLDERS_AND_DOCUMENTS
::ucbhelper::Content aContent
a content object describing the folder.
OUString sURL
the URL of a folder.
OUString aTargetURL