1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SVTOOLS_SOURCE_CONTNR_CONTENTENUMERATION_HXX
29 #define SVTOOLS_SOURCE_CONTNR_CONTENTENUMERATION_HXX
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
33 #include <com/sun/star/document/XStandaloneDocumentInfo.hpp>
34 /** === end UNO includes === **/
35 #include <osl/thread.hxx>
36 #include <rtl/ref.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <rtl/ustring.hxx>
39 #include <tools/datetime.hxx>
40 #include <vcl/image.hxx>
41 
42 class IUrlFilter;
43 //........................................................................
44 namespace svt
45 {
46 //........................................................................
47 
48     //====================================================================
49     //= SortingData_Impl
50     //====================================================================
51     struct SortingData_Impl
52     {
53     private:
54         ::rtl::OUString maFilename;     // only filename in upper case - for compare purposes
55         ::rtl::OUString maTitle;        //  -> be carefull when changing maTitle to update maFilename only when new
56         ::rtl::OUString maLowerTitle;
57 
58 
59     public:
60         ::rtl::OUString maType;
61         ::rtl::OUString maTargetURL;
62         ::rtl::OUString maImageURL;
63         ::rtl::OUString maDisplayText;
64         DateTime        maModDate;
65         Image           maImage;
66         sal_Int64       maSize;
67         sal_Bool        mbIsFolder;
68         sal_Bool        mbIsVolume;
69         sal_Bool        mbIsRemote;
70         sal_Bool        mbIsRemoveable;
71         sal_Bool        mbIsFloppy;
72         sal_Bool        mbIsCompactDisc;
73 
74         inline                          SortingData_Impl();
75         inline const ::rtl::OUString&   GetTitle() const;
76         inline const ::rtl::OUString&   GetLowerTitle() const;
77         inline const ::rtl::OUString&   GetFileName() const;
78         inline void                     SetNewTitle( const ::rtl::OUString& rNewTitle );        // new maTitle is set -> maFilename is set to same!
79         inline void                     ChangeTitle( const ::rtl::OUString& rChangedTitle );    // maTitle is changed, maFilename is unchanged!
80 
81     private:
82         inline void                     SetTitles( const ::rtl::OUString& rNewTitle );
83     };
84 
85     inline SortingData_Impl::SortingData_Impl() :
86         maSize          ( 0 ),
87         mbIsFolder      ( sal_False ),
88         mbIsVolume      ( sal_False ),
89         mbIsRemote      ( sal_False ),
90         mbIsRemoveable  ( sal_False ),
91         mbIsFloppy      ( sal_False ),
92         mbIsCompactDisc ( sal_False )
93     {
94     }
95 
96     inline const ::rtl::OUString& SortingData_Impl::GetTitle() const
97     {
98         return maTitle;
99     }
100 
101     inline const ::rtl::OUString& SortingData_Impl::GetLowerTitle() const
102     {
103         return maLowerTitle;
104     }
105 
106     inline const ::rtl::OUString& SortingData_Impl::GetFileName() const
107     {
108         return maFilename;
109     }
110 
111     inline void SortingData_Impl::SetNewTitle( const ::rtl::OUString& rNewTitle )
112     {
113         SetTitles( rNewTitle );
114         maFilename = rNewTitle.toAsciiUpperCase();
115     }
116 
117     inline void SortingData_Impl::ChangeTitle( const ::rtl::OUString& rChangedTitle )
118     {
119         SetTitles( rChangedTitle );
120     }
121 
122     inline void SortingData_Impl::SetTitles( const ::rtl::OUString& rNewTitle )
123     {
124         maTitle = rNewTitle;
125         maLowerTitle = rNewTitle.toAsciiLowerCase();
126     }
127 
128     //====================================================================
129     //= IContentTitleTranslation
130     //====================================================================
131     class IContentTitleTranslation
132     {
133     public:
134         virtual sal_Bool    GetTranslation( const ::rtl::OUString& _rOriginalName, ::rtl::OUString& _rTranslatedName ) const = 0;
135     };
136 
137     //====================================================================
138     //= EnumerationResult
139     //====================================================================
140     enum EnumerationResult
141     {
142         SUCCESS,    /// the enumration was successfull
143         ERROR,      /// the enumration was unsuccessfull
144         RUNNING     /// the enumeration is still running, and the maximum wait time has passed
145     };
146 
147     //====================================================================
148     //= FolderDescriptor
149     //====================================================================
150     struct FolderDescriptor
151     {
152         /** a content object describing the folder. Can be <NULL/>, in this case <member>sURL</member>
153             is relevant.
154         */
155         ::ucbhelper::Content  aContent;
156         /** the URL of a folder. Will be ignored if <member>aContent</member> is not <NULL/>.
157         */
158         String          sURL;
159 
160         FolderDescriptor() { }
161 
162         FolderDescriptor( const ::ucbhelper::Content& _rContent )
163             :aContent( _rContent )
164         {
165         }
166 
167         FolderDescriptor( const String& _rURL )
168             :sURL( _rURL )
169         {
170         }
171     };
172 
173     //====================================================================
174     //= IEnumerationResultHandler
175     //====================================================================
176     class IEnumerationResultHandler
177     {
178     public:
179         virtual void        enumerationDone( EnumerationResult _eResult ) = 0;
180     };
181 
182     //====================================================================
183     //= FileViewContentEnumerator
184     //====================================================================
185     class FileViewContentEnumerator
186             :public  ::rtl::IReference
187             ,private ::osl::Thread
188     {
189     public:
190         typedef ::std::vector< SortingData_Impl* >  ContentData;
191 
192     private:
193         ContentData&                    m_rContent;
194         ::osl::Mutex&                   m_rContentMutex;
195 
196         oslInterlockedCount             m_refCount;
197         mutable ::osl::Mutex            m_aMutex;
198 
199         FolderDescriptor                m_aFolder;
200         ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >
201                                         m_xCommandEnv;
202         const IUrlFilter*               m_pFilter;
203         const IContentTitleTranslation* m_pTranslator;
204         IEnumerationResultHandler*      m_pResultHandler;
205         bool                            m_bCancelled;
206 
207         mutable ::com::sun::star::uno::Reference< ::com::sun::star::document::XStandaloneDocumentInfo >
208                                         m_xDocInfo;
209 
210 		::com::sun::star::uno::Sequence< ::rtl::OUString > m_rBlackList;
211 
212 		sal_Bool URLOnBlackList ( const ::rtl::OUString& sRealURL );
213 
214     public:
215         /** constructs an enumerator instance
216 
217             @param _rContentToFill
218                 the structure which is to be filled with the found content
219             @param _rContentMutex
220                 the mutex which protects the access to <arg>_rContentToFill</arg>
221             @param _pTranslator
222                 an instance which should be used to translate content titles. May be <NULL/>
223         */
224         FileViewContentEnumerator(
225             const ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >& _rxCommandEnv,
226             ContentData& _rContentToFill,
227             ::osl::Mutex& _rContentMutex,
228             const IContentTitleTranslation* _pTranslator
229         );
230 
231         /** enumerates the content of a given folder
232 
233             @param _rFolder
234                 the folder whose content is to be enumerated
235             @param _pFilter
236                 a filter to apply to the found contents
237             @param _pResultHandler
238                 an instance which should handle the results of the enumeration
239         */
240         void    enumerateFolderContent(
241                     const FolderDescriptor& _rFolder,
242                     const IUrlFilter* _pFilter,
243                     IEnumerationResultHandler* _pResultHandler
244                 );
245 
246         /** enumerates the content of a given folder synchronously
247         */
248         EnumerationResult   enumerateFolderContentSync(
249                     const FolderDescriptor& _rFolder,
250                     const IUrlFilter* _pFilter,
251 					const ::com::sun::star::uno::Sequence< ::rtl::OUString >& rBlackList = ::com::sun::star::uno::Sequence< ::rtl::OUString >()
252                 );
253 
254         /** cancels the running operation.
255 
256             Note that "cancel" may mean that the operation is running, but its result
257             is simply disregarded later on.
258         */
259         void    cancel();
260 
261         // IReference overridables
262 	    virtual oslInterlockedCount SAL_CALL acquire();
263 	    virtual oslInterlockedCount SAL_CALL release();
264 
265         using Thread::operator new;
266         using Thread::operator delete;
267 
268     protected:
269         ~FileViewContentEnumerator();
270 
271     private:
272         EnumerationResult enumerateFolderContent();
273 
274         // Thread overridables
275         virtual void SAL_CALL run();
276         virtual void SAL_CALL onTerminated();
277 
278     private:
279         sal_Bool implGetDocTitle( const ::rtl::OUString& _rTargetURL, ::rtl::OUString& _rRet ) const;
280     };
281 
282 //........................................................................
283 } // namespace svt
284 //........................................................................
285 
286 #endif // SVTOOLS_SOURCE_CONTNR_CONTENTENUMERATION_HXX
287 
288