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 <tools/debug.hxx>
34 #include <vcl/svapp.hxx>
35 #include <osl/mutex.hxx>
39 //........................................................................
42 //........................................................................
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
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 ) );
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
;
79 //====================================================================
80 //= FileViewContentEnumerator
81 //====================================================================
82 //--------------------------------------------------------------------
83 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_bCancelled ( false )
93 ,m_rBlackList ( ::com::sun::star::uno::Sequence
< OUString
>() )
97 //--------------------------------------------------------------------
98 FileViewContentEnumerator::~FileViewContentEnumerator()
102 //--------------------------------------------------------------------
103 void FileViewContentEnumerator::cancel()
105 ::osl::MutexGuard
aGuard( m_aMutex
);
107 m_pResultHandler
= NULL
;
108 m_pTranslator
= NULL
;
109 m_aFolder
.aContent
= ::ucbhelper::Content();
113 //--------------------------------------------------------------------
114 EnumerationResult
FileViewContentEnumerator::enumerateFolderContentSync(
115 const FolderDescriptor
& _rFolder
,
116 const ::com::sun::star::uno::Sequence
< OUString
>& rBlackList
)
119 ::osl::MutexGuard
aGuard( m_aMutex
);
120 m_aFolder
= _rFolder
;
121 m_pResultHandler
= NULL
;
122 m_rBlackList
= rBlackList
;
124 return enumerateFolderContent();
127 //--------------------------------------------------------------------
128 void FileViewContentEnumerator::enumerateFolderContent(
129 const FolderDescriptor
& _rFolder
, IEnumerationResultHandler
* _pResultHandler
)
131 ::osl::MutexGuard
aGuard( m_aMutex
);
132 m_aFolder
= _rFolder
;
133 m_pResultHandler
= _pResultHandler
;
135 OSL_ENSURE( m_aFolder
.aContent
.get().is() || !m_aFolder
.sURL
.isEmpty(),
136 "FileViewContentEnumerator::enumerateFolderContent: invalid folder descriptor!" );
139 //TODO: a protocol is missing how to join with the launched thread
140 // before exit(3), to ensure the thread is no longer relying on any
141 // infrastructure while that infrastructure is being shut down in
145 //--------------------------------------------------------------------
146 EnumerationResult
FileViewContentEnumerator::enumerateFolderContent()
148 EnumerationResult eResult
= ERROR
;
152 Reference
< XResultSet
> xResultSet
;
153 Sequence
< OUString
> aProps(12);
157 aProps
[2] = "DateModified";
158 aProps
[3] = "DateCreated";
159 aProps
[4] = "IsFolder";
160 aProps
[5] = "TargetURL";
161 aProps
[6] = "IsHidden";
162 aProps
[7] = "IsVolume";
163 aProps
[8] = "IsRemote";
164 aProps
[9] = "IsRemoveable";
165 aProps
[10] = "IsFloppy";
166 aProps
[11] = "IsCompactDisc";
168 Reference
< XCommandEnvironment
> xEnvironment
;
171 FolderDescriptor aFolder
;
173 ::osl::MutexGuard
aGuard( m_aMutex
);
175 xEnvironment
= m_xCommandEnv
;
177 if ( !aFolder
.aContent
.get().is() )
179 aFolder
.aContent
= ::ucbhelper::Content( aFolder
.sURL
, xEnvironment
, comphelper::getProcessComponentContext() );
181 ::osl::MutexGuard
aGuard( m_aMutex
);
182 m_aFolder
.aContent
= aFolder
.aContent
;
186 Reference
< XDynamicResultSet
> xDynResultSet
;
187 ResultSetInclude eInclude
= INCLUDE_FOLDERS_AND_DOCUMENTS
;
188 xDynResultSet
= aFolder
.aContent
.createDynamicCursor( aProps
, eInclude
);
190 if ( xDynResultSet
.is() )
191 xResultSet
= xDynResultSet
->getStaticResultSet();
193 catch( CommandAbortedException
& )
195 SAL_WARN( "svtools.contnr", "createCursor: CommandAbortedException" );
201 if ( xResultSet
.is() )
203 Reference
< XRow
> xRow( xResultSet
, UNO_QUERY
);
204 Reference
< XContentAccess
> xContentAccess( xResultSet
, UNO_QUERY
);
208 SortingData_Impl
* pData
;
211 bool bCancelled
= false;
212 while ( !bCancelled
&& xResultSet
->next() )
214 sal_Bool bIsHidden
= xRow
->getBoolean( ROW_IS_HIDDEN
);
215 // don't show hidden files
216 if ( !bIsHidden
|| xRow
->wasNull() )
220 aDT
= xRow
->getTimestamp( ROW_DATE_MOD
);
221 sal_Bool bContainsDate
= !xRow
->wasNull();
222 if ( !bContainsDate
)
224 aDT
= xRow
->getTimestamp( ROW_DATE_CREATE
);
225 bContainsDate
= !xRow
->wasNull();
228 OUString aContentURL
= xContentAccess
->queryContentIdentifierString();
229 OUString aTargetURL
= xRow
->getString( ROW_TARGET_URL
);
230 sal_Bool bHasTargetURL
= !xRow
->wasNull() && !aTargetURL
.isEmpty();
232 OUString sRealURL
= bHasTargetURL
? aTargetURL
: aContentURL
;
234 // check for restrictions
236 ::osl::MutexGuard
aGuard( m_aMutex
);
237 if ( /* m_rBlackList.hasElements() && */ URLOnBlackList ( sRealURL
) )
241 pData
= new SortingData_Impl
;
242 pData
->maTargetURL
= sRealURL
;
244 pData
->mbIsFolder
= xRow
->getBoolean( ROW_IS_FOLDER
) && !xRow
->wasNull();
245 pData
->mbIsVolume
= xRow
->getBoolean( ROW_IS_VOLUME
) && !xRow
->wasNull();
246 pData
->mbIsRemote
= xRow
->getBoolean( ROW_IS_REMOTE
) && !xRow
->wasNull();
247 pData
->mbIsRemoveable
= xRow
->getBoolean( ROW_IS_REMOVABLE
) && !xRow
->wasNull();
248 pData
->mbIsFloppy
= xRow
->getBoolean( ROW_IS_FLOPPY
) && !xRow
->wasNull();
249 pData
->mbIsCompactDisc
= xRow
->getBoolean( ROW_IS_COMPACTDISC
) && !xRow
->wasNull();
250 pData
->SetNewTitle( xRow
->getString( ROW_TITLE
) );
251 pData
->maSize
= xRow
->getLong( ROW_SIZE
);
253 if ( bHasTargetURL
&&
254 INetURLObject( aContentURL
).GetProtocol() == INET_PROT_VND_SUN_STAR_HIER
)
256 ::ucbhelper::Content
aCnt( aTargetURL
, xEnvironment
, comphelper::getProcessComponentContext() );
259 aCnt
.getPropertyValue("Size") >>= pData
->maSize
;
260 aCnt
.getPropertyValue("DateModified") >>= aDT
;
267 CONVERT_DATETIME( aDT
, pData
->maModDate
);
270 if ( pData
->mbIsFolder
)
272 SolarMutexGuard aGuard
;
273 ::svtools::VolumeInfo
aVolInfo( pData
->mbIsVolume
, pData
->mbIsRemote
,
274 pData
->mbIsRemoveable
, pData
->mbIsFloppy
,
275 pData
->mbIsCompactDisc
);
276 pData
->maType
= SvFileInformationManager::GetFolderDescription( aVolInfo
);
279 pData
->maType
= SvFileInformationManager::GetFileDescription(
280 INetURLObject( pData
->maTargetURL
) );
282 // replace names on demand
284 ::osl::MutexGuard
aGuard( m_aMutex
);
288 sal_Bool bTranslated
= sal_False
;
290 if ( pData
->mbIsFolder
)
291 bTranslated
= m_pTranslator
->GetTranslation( pData
->GetTitle(), sNewTitle
);
293 bTranslated
= implGetDocTitle( pData
->maTargetURL
, sNewTitle
);
296 pData
->ChangeTitle( sNewTitle
);
301 ::osl::MutexGuard
aGuard( m_rContentMutex
);
302 m_rContent
.push_back( pData
);
307 ::osl::MutexGuard
aGuard( m_aMutex
);
308 bCancelled
= m_bCancelled
;
313 catch( CommandAbortedException
& )
315 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException while enumerating!" );
319 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException while enumerating!" );
323 catch( CommandAbortedException
& )
325 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an CommandAbortedException!" );
329 SAL_WARN( "svtools.contnr", "FileViewContentEnumerator::enumerateFolderContent: caught an exception other than CommandAbortedException!" );
332 IEnumerationResultHandler
* pHandler
= NULL
;
334 ::osl::MutexGuard
aGuard( m_aMutex
);
335 pHandler
= m_pResultHandler
;
341 ::osl::MutexGuard
aGuard( m_rContentMutex
);
342 if ( eResult
!= SUCCESS
)
343 // clear any "intermediate" and unfinished result
348 pHandler
->enumerationDone( eResult
);
352 //--------------------------------------------------------------------
354 sal_Bool
FileViewContentEnumerator::URLOnBlackList ( const OUString
& sRealURL
)
356 OUString entryName
= sRealURL
.copy( sRealURL
.lastIndexOf( '/' ) + 1 );
358 for (int i
= 0; i
< m_rBlackList
.getLength() ; i
++)
360 if ( entryName
.equals( m_rBlackList
[i
] ) )
367 //--------------------------------------------------------------------
368 sal_Bool
FileViewContentEnumerator::implGetDocTitle( const OUString
& _rTargetURL
, OUString
& _rRet
) const
370 sal_Bool bRet
= sal_False
;
374 ::osl::MutexGuard
aGuard( m_aMutex
);
375 if (!m_xDocProps
.is())
377 m_xDocProps
.set(DocumentProperties::create(
378 ::comphelper::getProcessComponentContext()));
381 assert(m_xDocProps
.is());
382 if (!m_xDocProps
.is())
385 m_xDocProps
->loadFromMedium(_rTargetURL
, Sequence
<PropertyValue
>());
387 OUString
const sTitle(m_xDocProps
->getTitle());
388 if (!sTitle
.isEmpty())
394 catch ( const Exception
& )
401 //--------------------------------------------------------------------
402 void FileViewContentEnumerator::execute()
404 enumerateFolderContent();
407 //........................................................................
409 //........................................................................
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */