'encoding UTF-8 Do not remove or change this line! '************************************************************************** ' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ' ' Copyright 2000, 2010 Oracle and/or its affiliates. ' ' OpenOffice.org - a multi-platform office productivity suite ' ' This file is part of OpenOffice.org. ' ' OpenOffice.org is free software: you can redistribute it and/or modify ' it under the terms of the GNU Lesser General Public License version 3 ' only, as published by the Free Software Foundation. ' ' OpenOffice.org is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU Lesser General Public License version 3 for more details ' (a copy is included in the LICENSE file that accompanied this code). ' ' You should have received a copy of the GNU Lesser General Public License ' version 3 along with OpenOffice.org. If not, see ' ' for a copy of the LGPLv3 License. ' '/****************************************************************************** '* '* owner : joerg.skottke@sun.com '* '* 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: '///
    '///+
  1. Name of the template to search for (string)
  2. '///
'///Returns: '///
    '///+
  1. Index of the Template in the containing folder (integer)
  2. '/// '///
'///Description: '/// end function '******************************************************************************* function hGetRefFilePath( cCategory as string, location as string) as string '///

Retrieve the location of the reference files for filename comparision

'///Input: '///
    '///+
  1. Category (string)
  2. '/// '///+
  3. Location (string)
  4. '/// '///
'///Returns: '///
    '///+
  1. Fully qualified path to workfile (string)
  2. '///
'///Description: '/// 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): '/// '///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: '///
    '///+
  1. Index of the folder to be selected on the categories-pane (integer)
  2. '///
      '///+
    • Must be > 0
    • '///
    '///+
  3. Turn printlog on for debugging purpose (boolean)
  4. '///
      '///+
    • TRUE : Be verbose
    • '///+
    • FALSE : Run silent
    • '///
    '///
'///Returns: '///
    '///+
  1. Number of items in the selected folder (integer)
  2. '///
      '///+
    • Must be > 0
    • '///
    '///
'///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: '///
    '///+
  1. Index of the folder to be selected on the categories-pane (integer)
  2. '///+
  3. Mode in which to open the template (integer)
  4. '///
      '///+
    • 0 = Do not open the object, just return its name
    • '///+
    • 1 = Open a new document based on the selected Template
    • '///+
    • 2 = edit the template
    • '///
    '///
'///Returns: '///
    '///+
  • The name of the selected item (string)
  • '///
'///Description: '///
    '///+
  • Verify function parameter iMode
  • if ( ( iMode < 0 ) or ( iMode > 2 ) ) then warnlog( CFN & "Invalid function parameter iMode: " & iMode ) hSelectDocumentObject() = "" exit function endif dim cTitle as string dim brc as boolean dim iObjectCount as integer Kontext "TemplateAndDocuments" '///+
  • Verify the function parameter iTitle > 0
  • if ( iTitle < 1 ) then warnlog( CFN & "Invalid function parameter iTitle: " & iTitle ) hSelectDocumentObject() = "" exit function endif '///+
  • Verify that the index is not too large
  • if ( iTitle > FileList.getItemCount() ) then warnlog( CFN & "Invalid function parameter iTitle: " & iTitle ) hSelectDocumentObject() = "" exit function endif '///+
  • 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
  • brc = hIsObjectAFolder( iObjectCount ) if ( brc ) then hSelectDocumentObject() = "Folder" exit function endif endif catch endcatch hFileWait() hHandleActivesOnLoad( 2, false ) hHandleInitialDialogs() end select '///+
  • 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