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 <TemplateScanner.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/documentconstants.hxx>
25 #include <sfx2/doctempl.hxx>
26 #include <com/sun/star/frame/DocumentTemplates.hpp>
27 #include <com/sun/star/frame/XDocumentTemplates.hpp>
28 #include <com/sun/star/ucb/XContentAccess.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdbc/XRow.hpp>
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
39 const char TITLE
[] = "Title";
41 class FolderDescriptor
46 const OUString
& rsContentIdentifier
,
47 const Reference
<css::ucb::XCommandEnvironment
>& rxFolderEnvironment
)
48 : mnPriority(nPriority
),
49 msContentIdentifier(rsContentIdentifier
),
50 mxFolderEnvironment(rxFolderEnvironment
)
53 OUString
const msContentIdentifier
;
54 // Reference<sdbc::XResultSet> mxFolderResultSet;
55 Reference
<css::ucb::XCommandEnvironment
> mxFolderEnvironment
;
60 bool operator() (const FolderDescriptor
& r1
, const FolderDescriptor
& r2
) const
61 { return r1
.mnPriority
< r2
.mnPriority
; }
65 /** Use a heuristic based on the URL of a top-level template folder to
66 assign a priority that is used to sort the folders.
68 int Classify (const OUString
&, const OUString
& rsURL
)
74 else if (rsURL
.indexOf("presnt")>=0)
78 else if (rsURL
.indexOf("layout")>=0)
82 else if (rsURL
.indexOf("educate")>=0)
86 else if (rsURL
.indexOf("finance")>=0)
92 // All other folders are taken for user supplied and have the
100 } // end of anonymous namespace
105 class TemplateScanner::FolderDescriptorList
106 : public ::std::multiset
<FolderDescriptor
,FolderDescriptor::Comparator
>
110 TemplateScanner::TemplateScanner()
111 : meState(INITIALIZE_SCANNING
),
113 mpFolderDescriptors(new FolderDescriptorList
),
115 mxFolderEnvironment(),
116 mxEntryEnvironment(),
123 TemplateScanner::~TemplateScanner()
127 TemplateScanner::State
TemplateScanner::GetTemplateRoot()
129 Reference
< XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
130 Reference
<frame::XDocumentTemplates
> xTemplates
= frame::DocumentTemplates::create(xContext
);
131 mxTemplateRoot
= xTemplates
->getContent();
133 return INITIALIZE_FOLDER_SCANNING
;
136 TemplateScanner::State
TemplateScanner::InitializeEntryScanning()
138 State
eNextState (SCAN_ENTRY
);
140 if (maFolderContent
.isFolder())
142 mxEntryEnvironment
.clear();
144 // We are interested only in three properties: the entry's name,
145 // its URL, and its content type.
146 Sequence
<OUString
> aProps (3);
148 aProps
[1] = "TargetURL";
149 aProps
[2] = "TypeDescription";
151 // Create a cursor to iterate over the templates in this folders.
152 mxEntryResultSet
.set( maFolderContent
.createCursor(aProps
, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY
));
160 TemplateScanner::State
TemplateScanner::ScanEntry()
162 State
eNextState (ERROR
);
164 Reference
<css::ucb::XContentAccess
> xContentAccess (mxEntryResultSet
, UNO_QUERY
);
165 Reference
<css::sdbc::XRow
> xRow (mxEntryResultSet
, UNO_QUERY
);
167 if (xContentAccess
.is() && xRow
.is() && mxEntryResultSet
.is())
169 if (mxEntryResultSet
->next())
171 OUString
sTitle (xRow
->getString (1));
172 OUString
sTargetURL (xRow
->getString (2));
173 OUString
sContentType (xRow
->getString (3));
175 OUString aId
= xContentAccess
->queryContentIdentifierString();
176 ::ucbhelper::Content aContent
= ::ucbhelper::Content (aId
, mxEntryEnvironment
, comphelper::getProcessComponentContext());
177 if (aContent
.isDocument ())
179 // Check whether the entry is an impress template. If so
180 // add a new entry to the resulting list (which is created
181 // first if necessary).
182 // These strings are used to find impress templates in the tree of
183 // template files. Should probably be determined dynamically.
184 if ( (sContentType
== MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
)
185 || (sContentType
== MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
)
186 || (sContentType
== "application/vnd.stardivision.impress")
187 || (sContentType
== MIMETYPE_VND_SUN_XML_IMPRESS_ASCII
)
188 // The following id comes from the bugdoc in #i2764#.
189 || (sContentType
== "Impress 2.0"))
191 OUString sLocalisedTitle
= SfxDocumentTemplates::ConvertResourceString(sTitle
);
192 mpTemplateEntries
.push_back(std::make_unique
<TemplateEntry
>(sLocalisedTitle
, sTargetURL
));
196 // Continue scanning entries.
197 eNextState
= SCAN_ENTRY
;
201 // Continue with scanning the next folder.
202 eNextState
= SCAN_FOLDER
;
209 TemplateScanner::State
TemplateScanner::InitializeFolderScanning()
211 State
eNextState (ERROR
);
213 mxFolderResultSet
.clear();
217 // Create content for template folders.
218 mxFolderEnvironment
.clear();
219 ::ucbhelper::Content
aTemplateDir (mxTemplateRoot
, mxFolderEnvironment
, comphelper::getProcessComponentContext());
221 // Define the list of properties we are interested in.
222 Sequence
<OUString
> aProps (2);
224 aProps
[1] = "TargetDirURL";
226 // Create an cursor to iterate over the template folders.
227 mxFolderResultSet
.set( aTemplateDir
.createCursor(aProps
, ::ucbhelper::INCLUDE_FOLDERS_ONLY
));
228 if (mxFolderResultSet
.is())
229 eNextState
= GATHER_FOLDER_LIST
;
231 catch (css::uno::Exception
&)
239 TemplateScanner::State
TemplateScanner::GatherFolderList()
241 State
eNextState (ERROR
);
243 Reference
<css::ucb::XContentAccess
> xContentAccess (mxFolderResultSet
, UNO_QUERY
);
244 if (xContentAccess
.is() && mxFolderResultSet
.is())
246 while (mxFolderResultSet
->next())
248 Reference
<sdbc::XRow
> xRow (mxFolderResultSet
, UNO_QUERY
);
251 OUString
sTitle (xRow
->getString (1));
252 OUString
sTargetDir (xRow
->getString (2));
253 OUString aId
= xContentAccess
->queryContentIdentifierString();
255 mpFolderDescriptors
->insert(
257 Classify(sTitle
,sTargetDir
),
259 mxFolderEnvironment
));
263 eNextState
= SCAN_FOLDER
;
269 TemplateScanner::State
TemplateScanner::ScanFolder()
271 State
eNextState (ERROR
);
273 if (!mpFolderDescriptors
->empty())
275 FolderDescriptor
aDescriptor (*mpFolderDescriptors
->begin());
276 mpFolderDescriptors
->erase(mpFolderDescriptors
->begin());
278 OUString
aId (aDescriptor
.msContentIdentifier
);
280 maFolderContent
= ::ucbhelper::Content (aId
, aDescriptor
.mxFolderEnvironment
, comphelper::getProcessComponentContext());
281 if (maFolderContent
.isFolder())
283 // Scan the folder and insert it into the list of template
285 // Continue with scanning all entries in the folder.
286 mpTemplateEntries
.clear();
287 eNextState
= INITIALIZE_ENTRY_SCAN
;
298 void TemplateScanner::RunNextStep()
302 case INITIALIZE_SCANNING
:
303 meState
= GetTemplateRoot();
306 case INITIALIZE_FOLDER_SCANNING
:
307 meState
= InitializeFolderScanning();
311 meState
= ScanFolder();
314 case GATHER_FOLDER_LIST
:
315 meState
= GatherFolderList();
318 case INITIALIZE_ENTRY_SCAN
:
319 meState
= InitializeEntryScanning();
323 meState
= ScanEntry();
333 mxTemplateRoot
.clear();
334 mxFolderEnvironment
.clear();
335 mxEntryEnvironment
.clear();
336 mxFolderResultSet
.clear();
337 mxEntryResultSet
.clear();
344 bool TemplateScanner::HasNextStep()
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */