'encoding UTF-8 Do not remove or change this line!
'**************************************************************
'
' Licensed to the Apache Software Foundation (ASF) under one
' or more contributor license agreements. See the NOTICE file
' distributed with this work for additional information
' regarding copyright ownership. The ASF licenses this file
' to you under the Apache License, Version 2.0 (the
' "License"); you may not use this file except in compliance
' with the License. You may obtain a copy of the License at
'
' http://www.apache.org/licenses/LICENSE-2.0
'
' Unless required by applicable law or agreed to in writing,
' software distributed under the License is distributed on an
' "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
' KIND, either express or implied. See the License for the
' specific language governing permissions and limitations
' under the License.
'
'**************************************************************
'*
'* short description : Helper functions to ease usage of templates
'*
'\******************************************************************************
function hFindTemplate( sTemplateName as string ) as integer
'///
Find a template by name in FileNewFromTemplate
'///Starting point: Templates and Documents dialog
'///Input:
'///
'///+- Name of the template to search for (string)
'///
'///Returns:
'///
'///+- Index of the Template in the containing folder (integer)
'///
'///+- 1 ... n : Index of the template (Position in folder)
'///+- 0 : No template found by given name
'///
'///
'///Description:
'///
const CFN = "hFindTemplate::"
dim brc as boolean
brc = false
dim irc as integer
irc = 0
dim iObjectFolder as integer
dim iObjectFolders as integer
dim iItemCount as integer
dim iCurrentItem as integer
dim cCurrentItem as string
'///+- select the templates from the category list
hSelectCategory( "TEMPLATES" )
'///+- run through every item in the list to find the template.
' NOTE: If the name of the template is not unique, the function will find
' the first occurrence
' NOTE: As we do not know the name of "My Templates" (it is localized) we
' need to search all folders..
iObjectFolders = FileList.getItemCount()
'///
for iObjectFolder = 1 to iObjectFolders
'///+- Select the (next) folder
hSelectFileFolder( iObjectFolder , true )
'///+- Retrieve the number of items within the folder
iItemCount = FileList.getItemCount()
'///+- For each item in the folder do:
'///
for iCurrentItem = 1 to iItemCount
'///+- Select the (next) item
FileList.select( iCurrentItem )
'///+- Get the name of the item
cCurrentItem = FileList.getSelText()
'///+- If this is the item we are searching for, exit
if ( cCurrentItem = sTemplateName ) then
irc = iCurrentItem : if ( irc = 0 ) then irc = 1 ' strange hack
brc = true
exit for
endif
next iCurrentItem
'///
'///+- Exit the outer loop
if ( brc ) then
exit for
endif
'///+- Click "Up one level"
UpOneLevel.click()
next iObjectFolder
'///
if ( brc ) then
printlog( CFN & "Template found: " & cCurrentItem )
else
printlog( CFN & "Template could not be found." )
endif
'///+- Return the index of the requested template
hFindTemplate() = irc
'///
end function
'*******************************************************************************
function hSelectCategory( cCategory as string ) as boolean
'///Select a category from the left pane of the templates dialog
'///Requires: Templates and Documets dialog must be open
'///Input: Category (string):
'///
'///+- NEWDOCUMENTS to select New Documents
'///+- TEMPLATES to select Templates
'///+- MYDOCUMENTS to select My Documents
'///+- SAMPLES to select Samples
'///
'///Returns: Alwas TRUE, no errorhandling
Kontext "TemplateAndDocuments"
if ( TemplateAndDocuments.exists( 2 ) ) then
Wait( 500 )
Category.typeKeys( "" )
wait( 500 )
if ( UCASE( cCategory ) = "NEWDOCUMENTS" ) then
' do nothing, the selection should be on this item
elseif ( UCASE( cCategory ) = "TEMPLATES" ) then
Category.typeKeys( "" )
elseif ( UCASE( cCategory ) = "MYDOCUMENTS" ) then
Category.typeKeys( "" )
Category.typeKeys( "" )
elseif ( UCASE( cCategory ) = "SAMPLES" ) then
Category.typeKeys( "" )
Category.typeKeys( "" )
Category.typeKeys( "" )
endif
hSelectCategory() = true
else
warnlog( "TemplateAndDocuments dialog did not open" )
endif
end function
'***************************************************************************
function hSelectFileFolder( iFolder as integer , bVerbose as boolean ) as integer
'///Select a folder on the templates dialog's right pane by index
'///Requires: Templates and Documents dialog must be open
'///Input:
'///
'///+- Index of the folder to be selected on the categories-pane (integer)
'///
'///+- Turn printlog on for debugging purpose (boolean)
'///
'///+- TRUE : Be verbose
'///+- FALSE : Run silent
'///
'///
'///Returns:
'///
'///+- Number of items in the selected folder (integer)
'///
'///
'///Description:
'///
dim iItems as integer
dim cFolder as string
Kontext "TemplateAndDocuments"
'///+- Select the entry with the given index
FileList.select( iFolder )
'///+- Retrieve the name of the selected object
cFolder = FileList.getText()
'///+- Press return
FileList.typeKeys( "" )
wait( 500 )
'///+- Get the number of items in the current folder
iItems = FileList.getItemCount()
'///+- Print a comment to the log if specified
if ( bVerbose ) then
printlog( " * " & cFolder & " contains " & iItems & " items." )
endif
'///+- Return the number of items
hSelectFileFolder() = iItems
'///
end function
'*******************************************************************************
function hGetFileFolderName( iFolder as integer ) as string
'///Get the name of the currently selected folder on templates dialog
'///Requires: Templates and Documents dialog must be open
'///Input: Index of the desired folder
'///Returns: Name of the selected folder
Kontext "TemplateAndDocuments"
FileList.select( iFolder )
WaitSlot()
hGetFileFolderName() = FileList.getText()
WaitSlot()
end function
'*******************************************************************************
function hSelectDocumentObject( iTitle as integer , iMode as integer ) as string
const CFN = "hSelectDocumentObject::"
'///Open or edit sample/template from the templates dialog
'///Requires: Templates and Documents dialog must be open
'///Input:
'///
'///+- Index of the folder to be selected on the categories-pane (integer)
'///
'///+- Valid positive index
'///
'///+- Mode in which to open the template (integer)
'///
'///+- 0 = Do not open the object, just return its name
'///+- 1 = Open a new document based on the selected Template
'///+- 2 = edit the template (unsupported)
'///
'///
'///Returns:
'///
'///+- The name of the selected item (string)
'///
'///Description:
'///
dim cTitle as string
dim brc as boolean
dim iObjectCount as integer
Kontext "TemplateAndDocuments"
'///+- Get the title of the selected object
cTitle = hGetFileFolderName( iTitle )
'///+- Count the number of objects in the list
iObjectCount = FileList.getItemCount()
printlog( CFN & "Title: " & cTitle )
select case iMode
case 0 ' do not load the document, return the title and exit the function
hSelectDocumentObject() = cTitle
exit function
case 1 ' open new document based on the template
WaitSlot()
kontext "TemplateAndDocuments"
FileList.typeKeys( "" )
WaitSlot( 5000 )
try
kontext "TemplateAndDocuments"
if ( TemplateAndDocuments.exists() ) then
'///+- If yes: Try to determine if it is a new folder
if ( hIsObjectAFolder( iObjectCount ) ) then
hSelectDocumentObject() = "Folder"
exit function
endif
endif
catch
endcatch
case 2 : warnlog( "Unsupported option: Edit template" )
end select
hFileWait()
hHandleActivesOnLoad( 2, false )
brc = hHandleInitialDialogs()
'///+- If all initial dialogs were handled correctly, return the title
if ( brc ) then
hSelectDocumentObject() = cTitle
else
hSelectDocumentObject() = ""
endif
'///
end function
'*******************************************************************************
function hIsTemplateDialogClosed() as boolean
const CFN = "hIsTemplateDialogClosed::"
'///Test whether the Templates and Documents dialog is closed after executing an object
'///Requires: Templates and Documents dialog must be open
'///Returns:
'///
'///+- TRUE if the Templates and Documents dialog cannot be found
'///+- FALSE if the selected object was a foder (Templates and Documents still open
'///
dim iTry as integer : iTry = 0
if ( WaitSlot( 2000 ) = WSFinished ) then
kontext "TemplateAndDocuments"
try
if ( TemplateAndDocuments.exists() ) then
printlog( CFN & "Dialog still open. Maybe we opened a folder" )
hIsTemplateDialogClosed() = false
exit function
else
printlog( CFN & "Regular object. Dialog is closed" )
hIsTemplateDialogClosed() = true
exit function
endif
catch
warnlog( "Failure to query Templates and Documents dialog" )
hIsTemplateDialogClosed() = true
endcatch
else
warnlog( "Slot not finished within 2000 msec" )
hIsTemplateDialogClosed() = true
endif
end function
'*******************************************************************************
function hIsObjectAFolder( iObjects as integer ) as boolean
'///Test whether the "Chapters" folder has been selected
'///E.g. the Chapters-folder belongs to a master document and must be skipped.
'///+ To didentify this folder, the number of items is checked (here: 4) which
'///+ should be unique (all other folders have more items)
'///Requires: Templates and Documents dialog must be open
'///Input. Number of objects in the folder (integer)
'///Returns: TRUE if number of items matches iObjects (boolean)
'///Description:
'///
' NOTE: This function should only handle one folder called "Chapters"
' below "Text Documents". We do not want to load the chapters
' separately, they are a part of a Master-Document and will be
' loaded at another time.
' To find out whether we are in a new folder or not, the number
' of objects in the parent folder is compared to the number in the
' current. This is a hack with a good probability to work.
const CFN = "hIsObjectAFolder::"
dim iCurrentObjects as integer
if ( iObjects < 1 ) then
warnlog( CFN & "Invalid Objectcount passed to function: " & iObjects )
warnlog( CFN & "Defaulting to 1 -> outcome is undefined" )
iObjects = 1
endif
kontext "TemplateAndDocuments"
'///+- Compare the objectcount. If different, this is another folder
'///+- If the number is unchanged, we have an unknown error -> crash
iCurrentObjects = Filelist.getItemCount()
if ( iCurrentObjects <> iObjects ) then
printlog( CFN & "Object appears to be a folder with " & iCurrentObjects & " items" )
hIsObjectAFolder() = true
else
warnlog( CFN & "Unknown error: Objectcount is unchanged" )
hIsObjectAFolder() = false
endif
'///
end function
'*******************************************************************************
function hDeleteUserTemplates() as integer
' Recommendation: Use outside of testcase
'///Delete all files located in the user templates directory
'///Uses hDeleteFile tzo remove all files below gOfficePath\user\template
'///Input: Nothing
'///Returns: Number of deleted objects
'///Description:
'///
const CFN = "hDeleteUserTemplates::"
dim iFileCount as integer
dim aFileList( 200 ) as string
dim sPath as string
dim iCounter as integer
'///+- Set the path to the user-templates (default location)
sPath = convertpath( gOfficePath & "user\template" )
'///+- Load the list of files into an array
iFileCount = GetFileList( sPath, "*.*", aFileList() )
printlog( CFN & "Found " & iFileCount & " file(s)." )
'///+- Delete each file and print the result to the log
for iCounter = 1 to iFileCount
hDeleteFile( aFileList( iCounter ) )
next iCounter
'///+- Return the number of files that was found in the templates directory
hDeleteUserTemplates() = iFileCount
'///
end function