1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 ' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 ' Copyright 2000, 2010 Oracle and/or its affiliates.
7 ' OpenOffice.org - a multi-platform office productivity suite
9 ' This file is part of OpenOffice.org.
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.
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).
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.
26 '/******************************************************************************
28 '* owner : gregor.hartmann@oracle.com
30 '* short description : Helper functions to ease usage of templates
32 '\******************************************************************************
34 function hFindTemplate( sTemplateName as string ) as integer
36 '///<H3>Find a template by name in FileNewFromTemplate</H3>
37 '///<i>Starting point: Templates and Documents dialog</i><br>
40 '///+<li> Name of the template to search for (string)</li>
44 '///+<li> Index of the Template in the containing folder (integer)</li>
46 '///+<li>1 ... n : Index of the template (Position in folder)</li>
47 '///+<li>0 : No template found by given name</li>
50 '///<u>Description</u>:
52 const CFN = "hFindTemplate::"
59 dim iObjectFolder as integer
60 dim iObjectFolders as integer
62 dim iItemCount as integer
63 dim iCurrentItem as integer
64 dim cCurrentItem as string
66 '///+<li>select the templates from the category list</li>
67 hSelectCategory( "TEMPLATES" )
69 '///+<li>run through every item in the list to find the template.</li>
70 ' NOTE: If the name of the template is not unique, the function will find
71 ' the first occurrence
72 ' NOTE: As we do not know the name of "My Templates" (it is localized) we
73 ' need to search all folders..
74 iObjectFolders = FileList.getItemCount()
77 for iObjectFolder = 1 to iObjectFolders
79 '///+<li>Select the (next) folder</li>
80 hSelectFileFolder( iObjectFolder , true )
82 '///+<li>Retrieve the number of items within the folder</li>
83 iItemCount = FileList.getItemCount()
85 '///+<li>For each item in the folder do:</li>
87 for iCurrentItem = 1 to iItemCount
89 '///+<li>Select the (next) item</li>
90 FileList.select( iCurrentItem )
92 '///+<li>Get the name of the item</li>
93 cCurrentItem = FileList.getSelText()
95 '///+<li>If this is the item we are searching for, exit</li>
96 if ( cCurrentItem = sTemplateName ) then
97 irc = iCurrentItem : if ( irc = 0 ) then irc = 1 ' strange hack
105 '///+<li>Exit the outer loop</li>
110 '///+<li>Click "Up one level"</li>
117 printlog( CFN & "Template found: " & cCurrentItem )
119 printlog( CFN & "Template could not be found." )
122 '///+<li>Return the index of the requested template</li>
123 hFindTemplate() = irc
128 '*******************************************************************************
130 function hSelectCategory( cCategory as string ) as boolean
132 '///<h3>Select a category from the left pane of the templates dialog</h3>
133 '///<i>Requires: Templates and Documets dialog must be open</i><br>
134 '///<u>Input</u>: Category (string):
136 '///+<li>NEWDOCUMENTS to select New Documents</li>
137 '///+<li>TEMPLATES to select Templates</li>
138 '///+<li>MYDOCUMENTS to select My Documents</li>
139 '///+<li>SAMPLES to select Samples</li>
141 '///<u>Returns</u>: Alwas TRUE, no errorhandling
143 Kontext "TemplateAndDocuments"
144 if ( TemplateAndDocuments.exists( 2 ) ) then
146 Category.typeKeys( "<HOME>" )
149 if ( UCASE( cCategory ) = "NEWDOCUMENTS" ) then
150 ' do nothing, the selection should be on this item
152 elseif ( UCASE( cCategory ) = "TEMPLATES" ) then
153 Category.typeKeys( "<DOWN>" )
155 elseif ( UCASE( cCategory ) = "MYDOCUMENTS" ) then
156 Category.typeKeys( "<DOWN>" )
157 Category.typeKeys( "<DOWN>" )
159 elseif ( UCASE( cCategory ) = "SAMPLES" ) then
160 Category.typeKeys( "<DOWN>" )
161 Category.typeKeys( "<DOWN>" )
162 Category.typeKeys( "<DOWN>" )
165 hSelectCategory() = true
167 warnlog( "TemplateAndDocuments dialog did not open" )
172 '***************************************************************************
174 function hSelectFileFolder( iFolder as integer , bVerbose as boolean ) as integer
176 '///<h3>Select a folder on the templates dialog's right pane by index</h3>
177 '///<i>Requires: Templates and Documents dialog must be open</i><br>
180 '///+<li>Index of the folder to be selected on the categories-pane (integer)</li>
182 '///+<li>Must be > 0</li>
184 '///+<li>Turn printlog on for debugging purpose (boolean)</li>
186 '///+<li>TRUE : Be verbose</li>
187 '///+<li>FALSE : Run silent</li>
192 '///+<li>Number of items in the selected folder (integer)</li>
194 '///+<li>Must be > 0</li>
198 '///<u>Description</u>:
200 dim iItems as integer
201 dim cFolder as string
203 Kontext "TemplateAndDocuments"
205 '///+<li>Select the entry with the given index</li>
206 FileList.select( iFolder )
208 '///+<li>Retrieve the name of the selected object</li>
209 cFolder = FileList.getText()
211 '///+<li>Press return</li>
212 FileList.typeKeys( "<return>" )
215 '///+<li>Get the number of items in the current folder</li>
216 iItems = FileList.getItemCount()
218 '///+<li>Print a comment to the log if specified</li>
220 printlog( " * " & cFolder & " contains " & iItems & " items." )
223 '///+<li>Return the number of items</li>
224 hSelectFileFolder() = iItems
229 '*******************************************************************************
231 function hGetFileFolderName( iFolder as integer ) as string
233 '///<h3>Get the name of the currently selected folder on templates dialog</h3>
234 '///<i>Requires: Templates and Documents dialog must be open</i><br>
235 '///<u>Input</u>: Index of the desired folder<br>
236 '///<u>Returns</u>: Name of the selected folder
238 Kontext "TemplateAndDocuments"
239 FileList.select( iFolder )
241 hGetFileFolderName() = FileList.getText()
246 '*******************************************************************************
248 function hSelectDocumentObject( iTitle as integer , iMode as integer ) as string
250 const CFN = "hSelectDocumentObject::"
252 '///<h3>Open or edit sample/template from the templates dialog</h3>
253 '///<i>Requires: Templates and Documents dialog must be open</i><br>
256 '///+<li>Index of the folder to be selected on the categories-pane (integer)</li>
258 '///+<li>Valid positive index</li>
260 '///+<li>Mode in which to open the template (integer)</li>
262 '///+<li>0 = Do not open the object, just return its name</li>
263 '///+<li>1 = Open a new document based on the selected Template</li>
264 '///+<li>2 = edit the template (unsupported)</li>
269 '///+<li>The name of the selected item (string)</li>
271 '///<u>Description</u>:
276 dim iObjectCount as integer
278 Kontext "TemplateAndDocuments"
280 '///+<li>Get the title of the selected object</li>
281 cTitle = hGetFileFolderName( iTitle )
283 '///+<li>Count the number of objects in the list</li>
284 iObjectCount = FileList.getItemCount()
285 printlog( CFN & "Title: " & cTitle )
288 case 0 ' do not load the document, return the title and exit the function
289 hSelectDocumentObject() = cTitle
291 case 1 ' open new document based on the template
293 kontext "TemplateAndDocuments"
294 FileList.typeKeys( "<return>" )
298 kontext "TemplateAndDocuments"
299 if ( TemplateAndDocuments.exists() ) then
300 '///+<li>If yes: Try to determine if it is a new folder</li>
301 if ( hIsObjectAFolder( iObjectCount ) ) then
302 hSelectDocumentObject() = "Folder"
310 case 2 : warnlog( "Unsupported option: Edit template" )
315 hHandleActivesOnLoad( 2, false )
316 brc = hHandleInitialDialogs()
318 '///+<li>If all initial dialogs were handled correctly, return the title</li>
320 hSelectDocumentObject() = cTitle
322 hSelectDocumentObject() = ""
328 '*******************************************************************************
330 function hIsTemplateDialogClosed() as boolean
332 const CFN = "hIsTemplateDialogClosed::"
333 '///<h3>Test whether the Templates and Documents dialog is closed after executing an object</h3>
334 '///<i>Requires: Templates and Documents dialog must be open</i><br>
337 '///+<li>TRUE if the Templates and Documents dialog cannot be found</li>
338 '///+<li>FALSE if the selected object was a foder (Templates and Documents still open</li>
341 dim iTry as integer : iTry = 0
344 if ( WaitSlot( 2000 ) = WSFinished ) then
345 kontext "TemplateAndDocuments"
347 if ( TemplateAndDocuments.exists() ) then
348 printlog( CFN & "Dialog still open. Maybe we opened a folder" )
349 hIsTemplateDialogClosed() = false
352 printlog( CFN & "Regular object. Dialog is closed" )
353 hIsTemplateDialogClosed() = true
357 warnlog( "Failure to query Templates and Documents dialog" )
358 hIsTemplateDialogClosed() = true
361 warnlog( "Slot not finished within 2000 msec" )
362 hIsTemplateDialogClosed() = true
367 '*******************************************************************************
369 function hIsObjectAFolder( iObjects as integer ) as boolean
371 '///<h3>Test whether the "Chapters" folder has been selected</h3>
372 '///<i>E.g. the Chapters-folder belongs to a master document and must be skipped.
373 '///+ To didentify this folder, the number of items is checked (here: 4) which
374 '///+ should be unique (all other folders have more items)</i><br>
375 '///<i>Requires: Templates and Documents dialog must be open</i><br>
376 '///<u>Input</u>. Number of objects in the folder (integer)<br>
377 '///<u>Returns</u>: TRUE if number of items matches iObjects (boolean)<br>
378 '///<u>Description</u>:
381 ' NOTE: This function should only handle one folder called "Chapters"
382 ' below "Text Documents". We do not want to load the chapters
383 ' separately, they are a part of a Master-Document and will be
384 ' loaded at another time.
385 ' To find out whether we are in a new folder or not, the number
386 ' of objects in the parent folder is compared to the number in the
387 ' current. This is a hack with a good probability to work.
389 const CFN = "hIsObjectAFolder::"
390 dim iCurrentObjects as integer
392 if ( iObjects < 1 ) then
393 warnlog( CFN & "Invalid Objectcount passed to function: " & iObjects )
394 warnlog( CFN & "Defaulting to 1 -> outcome is undefined" )
398 kontext "TemplateAndDocuments"
399 '///+<li>Compare the objectcount. If different, this is another folder</li>
400 '///+<li>If the number is unchanged, we have an unknown error -> crash</li>
401 iCurrentObjects = Filelist.getItemCount()
403 if ( iCurrentObjects <> iObjects ) then
404 printlog( CFN & "Object appears to be a folder with " & iCurrentObjects & " items" )
405 hIsObjectAFolder() = true
407 warnlog( CFN & "Unknown error: Objectcount is unchanged" )
408 hIsObjectAFolder() = false
415 '*******************************************************************************
417 function hDeleteUserTemplates() as integer
419 ' Recommendation: Use outside of testcase
421 '///<h3>Delete all files located in the user templates directory</h3>
422 '///<i>Uses hDeleteFile tzo remove all files below gOfficePath\user\template</i><br>
423 '///<u>Input</u>: Nothing<br>
424 '///<u>Returns</u>: Number of deleted objects<br>
425 '///<u>Description</u>:
428 const CFN = "hDeleteUserTemplates::"
430 dim iFileCount as integer
431 dim aFileList( 200 ) as string
433 dim iCounter as integer
435 '///+<li>Set the path to the user-templates (default location)</li>
436 sPath = convertpath( gOfficePath & "user\template" )
438 '///+<li>Load the list of files into an array</li>
439 iFileCount = GetFileList( sPath, "*.*", aFileList() )
440 printlog( CFN & "Found " & iFileCount & " file(s)." )
442 '///+<li>Delete each file and print the result to the log</li>
443 for iCounter = 1 to iFileCount
445 hDeleteFile( aFileList( iCounter ) )
449 '///+<li>Return the number of files that was found in the templates directory</li>
450 hDeleteUserTemplates() = iFileCount