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 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: t_filetools.inc,v $
13 '* last change: $Author: rt $ $Date: 2008-09-04 09:17:26 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : joerg.skottke@sun.com
36 '* short description : Functions that work with files like deleting, loading...
38 '\******************************************************************************
40 private const VERBOSE = false
42 function hHandleActivesOnLoad( iTries as integer , iAdd as integer ) as boolean
44 '///<h3>Handle any dialogs that might pop up when loading a file</h3>
45 '///<i>Beware: This function gives only limited control over the dialogs closed,
46 '///+ it just closes anything it can.</i><br>
47 '///<i>Please refer to the inline documentation for further details</i><br>
50 '///+<li>Number of dialogs to be closed (integer)</li>
52 '///+<li>Number of dialogs not limited though more than 3 is not useful</li>
54 '///+<li>Additional dialogs (integer).</li>
56 '///+<li>Number of dialogs to handle in case of unexpected behavior. If unsure set it to 2</li>
61 '///+<li>Errorcondition (boolean)</li>
63 '///+<li>TRUE: The expected number of dialogs were closed</li>
64 '///+<li>FALSE: On any other condition</li>
67 '///<u>Description</u>:
69 '///+<li>OK to create a new document based on a template</li>
70 '///+<li>YES to update links</li>
71 '///+<li>YES to execute macros</li>
74 use "global/tools/includes/optional/t_stringtools.inc"
76 const CFN = "hHandleActivesOnLoad::"
79 dim iActives as integer
80 dim cMessage as string
83 printlog( CFN & "Enter" )
85 ' not good but apparently required - else hDestroyDocument will handle the
86 ' dialogs and warn about them.
88 ' think positive: preset the returnvalue with "true"
89 hHandleActivesOnLoad() = true
91 for iTry = 1 to iTries + iAdd
93 ' handle three possible dialogs:
94 ' 1. The warning that the user should create a new document based
95 ' on this Sample/Template (the document is write protected)
96 ' 2. The question to update links to the internet. Note: If you
97 ' use proxies, they must be set correctly otherwise the result
99 ' 3. Some weird usage information dialog which is an active with just
102 ' Although this works quite well, there is a flaw that we will not
103 ' get information about an "active" dialog that cannot be closed by
104 ' .yes() or .ok(). If this happens, we probably run into an error
107 if ( active.exists( 1 ) ) then
109 iActives = iActives + 1
110 cMessage = hRemoveLineBreaks( active.getText() )
111 printlog( CFN & "MSG (" & iActives & "): " & cMessage )
112 printlog( CFN & "Ressource type is: " & Active.getRT() )
113 printlog( CFN & "Buttoncount is: " & Active.getButtonCount() )
116 ' ok to create a new document
119 printlog( CFN & "MSG (" & iActives & "): closed with Cancel" )
122 printlog( CFN & "Missed - this dialog has no OK button (create new document)" )
126 if ( not bSkip ) then
127 if ( Active.getRT() = 304 ) then
128 if ( Active.getButtonCount() = 1 ) then
130 printlog( CFN & "MSG (" & iActives & "): closed with OK" )
138 if ( not bSkip ) then
142 printlog( CFN & "MSG (" & iActives & "): closed with NO" )
144 printlog( CFN & "Missed - this dialog has no NO button (update links)" )
150 qaerrorlog( "Problem while trying to handle messageboxes" )
151 hHandleActivesOnLoad() = false
156 ' now see how many dialogs were allowed and how many have been closed
157 ' this does not change the return value of the function
158 if ( iActives > iTries ) then
159 printlog( CFN & "Exit: The test closed more dialogs than expected" )
160 hHandleActivesOnLoad() = false
162 printlog( CFN & "Exit" )
167 '*******************************************************************************
169 function hHandleInitialDialogs() as integer
171 const CFN = "hHandleInitialDialogs::"
173 '///<h3>Handle dialogs while opening samples/templates etc.</h3>
174 '///<i>The returnvalue is of limited use</i><br>
177 '///+<li>Nothing</li>
181 '///+<li>Id of the dialog (integer)</li>
183 '///+<li>0: No dialog was found</li>
184 '///+<li>1: UseOfThisTemplate</li>
185 '///+<li>2: StarOfficeCalendar1</li>
186 '///+<li>3: NewsletterLayout</li>
187 '///+<li>4: PortfolioCurrency</li>
188 '///+<li>5: StarOfficeCalendar2</li>
191 '///<u>Description</u>:
195 ' NOTE: The ID of the dialog will be returned but is of limited use
196 ' Function returns 0 if no dialogs have been encountered
198 dim incident as integer
201 printlog( CFN & "Enter" )
204 '///+<li>Test for UseOfThisTemplate-dialog</li>
205 Kontext "UseOfThisTemplate"
206 if ( UseOfThisTemplate.Exists() ) then
207 printlog( CFN & "Closing UseOfThisTemplate-dialog" )
212 '///+<li>Test for StarOfficeCalendar1-dialog</li>
213 Kontext "StarOfficeCalendar1"
214 if ( StarOfficeCalendar1.Exists() ) then
215 printlog( CFN & "Closing StarOfficeCalendar1-dialog" )
220 '///+<li>Test for StarOfficeCalendar2-dialog (This should be idential to StarofficeCalendar1)</li>
221 Kontext "StarOfficeCalendar2"
222 if ( StarOfficeCalendar2.Exists() ) then
223 printlog( CFN & "Closing StarOfficeCalendar2-dialog" )
228 '///+<li>Test for NewsletterLayout-dialog</li>
229 Kontext "NewsletterLayout"
230 if ( NewsletterLayout.Exists() ) then
231 printlog( CFN & "Closing NewsletterLayout-dialog" )
236 '///+<li>Test for PortfolioCurrency-dialog</li>
237 Kontext "PortfolioCurrency"
238 if ( PortfolioCurrency.Exists() ) then
239 printlog( CFN & "Closing PortfolioCurrency-dialog" )
246 hHandleInitialDialogs() = incident
247 printlog( CFN & "Exit" )
253 '*******************************************************************************
255 function hGetWorkPath() as string
257 '///<h3>Retrieve the users work directory</h3>
258 '///<i>Uses: Call to UNO service</i><br>
259 '///<i>Errorhandling: Fixes "$(user)"-type path (uses fallback)</i><br>
260 '///<i>Errorhandling: Handles broken UNO connection (uses fallback)</i><br>
263 '///+<li>Nothing</li>
267 '///+<li>Path to local workdir (string)</li>
269 '///+<li>Includes trailing slash/backslash</li>
272 '///<u>Description</u>:
276 dim sDefaultPath as string
277 sDefaultPath = convertpath( gOfficePath & "user/work" )
278 dim oOfficeConnect as object
279 dim oOfficeConfig as object
280 dim bPathIsFromAPI as boolean
282 const CFN = "hGetWorkPath::"
284 '///+<li>Create an UNO service and ask it for the location of "Work"</li>
286 oOfficeConnect = hGetUnoService( true )
287 oOfficeConfig = oOfficeConnect.createInstance( "com.sun.star.util.PathSettings" )
288 sPath = convertFromURL( oOfficeConfig.Work )
289 bPathIsFromAPI = true
291 '///+<li>Handle broken UNO connection</li>
292 printlog( CFN & "Could not access service, connection broken?" )
294 bPathIsFromAPI = false
297 '///+<li>If the path differs from the default do a warnlog</li>
298 if ( sPath <> sDefaultPath ) then
299 printlog( CFN & "Default path is: " & sDefaultPath )
300 printlog( CFN & "Workpath is not defaultpath: " & sPath )
301 printlog( CFN & "Overriding UNO path" )
302 sPath = convertpath( gOfficePath & "user\work" )
303 bPathIsFromAPI = false
306 '///+<li>Apply fallback in case of broken connection or invalid path</li>
307 if ( instr( sPath , "$(user)" ) > 0 ) then
308 printlog( CFN & "sPath has $(user)-type string, using fallback" )
310 bPathIsFromAPI = false
313 '///+<li>Add trailing pathseparator, do convertpath</li>
315 sPath = convertpath( sPath )
317 '///+<li>Print info to the log and return the path</li>
318 if ( bPathIsFromAPI ) then
319 printlog( CFN & "(From API): " & sPath )
321 qaerrorlog( CFN & "(Junked API-Path): " & sPath )
323 hGetWorkPath() = sPath
328 '*******************************************************************************
330 function hGetFileSizeAsLong( cFileName as string ) as long
332 '///<h3>Get the size (long) of a file</h3>
335 '///+<li>Filename (string)</li>
339 '///+<li>Size of file (long)</li>
341 '///<u>Description</u>:
344 const CFN = "hGetFileSizeAsLong::"
346 ' This function returns the size of a specified file. If it is a directory,
347 ' the size will default to -1 (A filesize can never be <0 - hopefully)
349 dim iAttrib as integer
351 '///+<li>Verify that the given file exists</li>
352 if ( app.dir( cFileName ) <> "" ) then
355 iAttrib = getattr( cFileName )
357 '///+<li>Return the size for a file or -1 for a directory</li>
358 if ( iAttrib <> 16 and iAttrib <> 17 ) then
359 hGetFileSizeAsLong() = filelen( cFileName )
361 hGetFileSizeAsLong() = -1
364 qaerrorlog( CFN & "File was not handled correctly: " & cFileName )
365 hGetFileSizeAsLong() = 0
370 '///+<li>Return a size of 0 if the file was not found</li>
371 hGetFileSizeAsLong() = 0
372 warnlog( CFN & "File does not exist: " & cFileName )
380 '*******************************************************************************
382 function hGetWorkFile( cFileName as string ) as string
384 '///<h3>Returns a fully qualified filename to a workfile</h3>
387 '///+<li>Filename without path (string)</li>
391 '///+<li>Filename including path to user work directory (string)</li>
393 '///<u>Description</u>:
396 '///+<li>Concatenate workpath and filename, convertpath</li>
397 hGetWorkFile() = convertpath( hGetWorkPath() & cFileName )