Line data Source code
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 :
20 : #ifndef INCLUDED_SD_SOURCE_UI_INC_TEMPLATESCANNER_HXX
21 : #define INCLUDED_SD_SOURCE_UI_INC_TEMPLATESCANNER_HXX
22 :
23 : #include "tools/AsynchronousTask.hxx"
24 : #include "sddllapi.h"
25 : #include <ucbhelper/content.hxx>
26 : #include "com/sun/star/uno/Reference.hxx"
27 :
28 : #include <vector>
29 : #include <boost/scoped_ptr.hpp>
30 : #include <boost/shared_ptr.hpp>
31 :
32 : namespace com { namespace sun { namespace star { namespace ucb {
33 : class XContent;
34 : class XCommandEnvironment;
35 : } } } }
36 :
37 : namespace com { namespace sun { namespace star { namespace sdbc {
38 : class XResultSet;
39 : } } } }
40 :
41 : namespace comphelper { namespace string {
42 : class NaturalStringSorter;
43 : } }
44 :
45 : namespace sd {
46 :
47 : /** Representation of a template or layout file.
48 : */
49 0 : class TemplateEntry
50 : {
51 : public:
52 0 : TemplateEntry (const OUString& rsTitle, const OUString& rsPath)
53 0 : : msTitle(rsTitle), msPath(rsPath) {}
54 :
55 : OUString msTitle;
56 : OUString msPath;
57 : };
58 :
59 :
60 :
61 :
62 : /** Functor that compares two TemplateEntries based on their titles
63 : */
64 0 : class TemplateEntryCompare
65 : {
66 : public:
67 : TemplateEntryCompare();
68 : bool operator()(TemplateEntry* pA, TemplateEntry* pB) const;
69 :
70 : private:
71 : ::boost::shared_ptr<comphelper::string::NaturalStringSorter> mpStringSorter;
72 : };
73 :
74 :
75 :
76 :
77 : /** Representation of a template or layout folder.
78 : */
79 0 : class TemplateDir
80 : {
81 : public:
82 0 : TemplateDir (const OUString& rsRegion, const OUString& rsUrl )
83 : : msRegion(rsRegion), msUrl(rsUrl), maEntries(),
84 0 : mbSortingEnabled(false), mpEntryCompare(NULL) {}
85 :
86 : OUString msRegion;
87 : OUString msUrl;
88 : ::std::vector<TemplateEntry*> maEntries;
89 :
90 : void EnableSorting(bool bSortingEnabled = true);
91 : void InsertEntry(TemplateEntry* pNewEntry);
92 :
93 : private:
94 : bool mbSortingEnabled;
95 : ::boost::scoped_ptr<TemplateEntryCompare> mpEntryCompare;
96 : };
97 :
98 :
99 :
100 :
101 : /** This class scans the template folders for impress templates. There are
102 : two ways to use this class.
103 : 1. The old and deprecated way is to call Scan() to scan all templates
104 : and collect the supported ones in a tree structure. This structure is
105 : returned by GetFolderList().
106 : 2. The new way implements the AsynchronousTask interface. Call
107 : RunNextStep() as long HasNextStep() returns <TRUE/>. After every step
108 : GetLastAddedEntry() returns the template that was scanned (and has a
109 : supported format) last. When a step does not add a new template then
110 : the value of the previous step is returned.
111 : */
112 : class SD_DLLPUBLIC TemplateScanner
113 : : public ::sd::tools::AsynchronousTask
114 : {
115 : public:
116 : /** Create a new template scanner and prepare but do not execute the scanning.
117 : */
118 : TemplateScanner (void);
119 :
120 : /** The destructor deletes any remaining entries of the local list of
121 : templates.
122 : */
123 : virtual ~TemplateScanner (void);
124 :
125 : /** Execute the actual scanning of templates. When this method
126 : terminates the result can be obtained by calling the
127 : <member>GetTemplateList</member> method.
128 : */
129 : void Scan (void);
130 :
131 : /** Return the list of template folders. It lies in the responsibility
132 : of the caller to take ownership of some or all entries and remove
133 : them from the returned list. All entries that remain until the
134 : destructor is called will be destroyed.
135 : */
136 : std::vector<TemplateDir*>& GetFolderList (void);
137 :
138 : /** Implementation of the AsynchronousTask interface method.
139 : */
140 : virtual void RunNextStep (void) SAL_OVERRIDE;
141 :
142 : /** Implementation of the AsynchronousTask interface method.
143 : */
144 : virtual bool HasNextStep (void) SAL_OVERRIDE;
145 :
146 : /** Return the TemplateDir object that was last added to
147 : mpTemplateDirectory.
148 : @return
149 : <NULL/> is returned either before the template scanning is
150 : started or after it has ended.
151 : */
152 : const TemplateEntry* GetLastAddedEntry (void) const;
153 :
154 : /** Set whether to sort the template entries inside the regions.
155 : */
156 0 : void EnableEntrySorting (bool isEntrySortingEnabled = true)
157 0 : {mbEntrySortingEnabled = isEntrySortingEnabled;}
158 :
159 : private:
160 : /** The current state determines which step will be executed next by
161 : RunNextStep().
162 : */
163 : enum State {
164 : INITIALIZE_SCANNING,
165 : INITIALIZE_FOLDER_SCANNING,
166 : GATHER_FOLDER_LIST,
167 : SCAN_FOLDER,
168 : INITIALIZE_ENTRY_SCAN,
169 : SCAN_ENTRY,
170 : DONE,
171 : ERROR
172 : };
173 : State meState;
174 :
175 : ::ucbhelper::Content maFolderContent;
176 : TemplateDir* mpTemplateDirectory;
177 :
178 : /** The data structure that is to be filled with information about the
179 : template files.
180 : */
181 : std::vector<TemplateDir*> maFolderList;
182 :
183 : /** Whether the template entries have to be sorted.
184 : */
185 : bool mbEntrySortingEnabled;
186 :
187 : /** This member points into the maFolderList to the member that was most
188 : recently added.
189 : */
190 : TemplateEntry* mpLastAddedEntry;
191 :
192 : /** The folders that are collected by GatherFolderList().
193 : */
194 : class FolderDescriptorList;
195 : ::boost::scoped_ptr<FolderDescriptorList> mpFolderDescriptors;
196 :
197 : /** Set of state variables used by the methods
198 : InitializeFolderScanning(), GatherFolderList(), ScanFolder(),
199 : InitializeEntryScanning(), and ScanEntry().
200 : */
201 : com::sun::star::uno::Reference<com::sun::star::ucb::XContent> mxTemplateRoot;
202 : com::sun::star::uno::Reference<com::sun::star::ucb::XCommandEnvironment> mxFolderEnvironment;
203 : com::sun::star::uno::Reference<com::sun::star::ucb::XCommandEnvironment> mxEntryEnvironment;
204 : com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> mxFolderResultSet;
205 : com::sun::star::uno::Reference<com::sun::star::sdbc::XResultSet> mxEntryResultSet;
206 :
207 : /** Obtain the root folder of the template folder hierarchy. The result
208 : is stored in mxTemplateRoot for later use.
209 : */
210 : State GetTemplateRoot (void);
211 :
212 : /** Initialize the scanning of folders. This is called exactly once.
213 : @return
214 : Returns one of the two states ERROR or GATHER_FOLDER_LIST.
215 : */
216 : State InitializeFolderScanning (void);
217 :
218 : /** Collect all available top-level folders in an ordered list which can
219 : then be processed by ScanFolder().
220 : @return
221 : Returns one of the two states ERROR or SCAN_FOLDER.
222 : */
223 : State GatherFolderList (void);
224 :
225 : /** From the list of top-level folders collected by GatherFolderList()
226 : the one with highest priority is processed.
227 : @return
228 : Returns one of the states ERROR, DONE, or INITILIZE_ENTRY_SCAN.
229 : */
230 : State ScanFolder (void);
231 :
232 : /** Initialize the scanning of entries of a top-level folder.
233 : @return
234 : Returns one of the states ERROR or SCAN_ENTRY.
235 : */
236 : State InitializeEntryScanning (void);
237 :
238 : /** Scan one entry. When this entry matches the recognized template
239 : types it is appended to the result set.
240 : @return
241 : Returns one of the states ERROR, SCAN_ENTRY, or SCAN_FOLDER.
242 : */
243 : State ScanEntry (void);
244 : };
245 :
246 : } // end of namespace sd
247 :
248 : #endif
249 :
250 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|