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