1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"SF_Platform" script:
language=
"StarBasic" script:
moduleType=
"normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === Full documentation is available on https://help.libreoffice.org/ ===
6 REM =======================================================================================================================
11 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
12 ''' SF_Platform
13 ''' ===========
14 ''' Singleton class implementing the
"ScriptForge.Platform
" service
15 ''' Implemented as a usual Basic module
17 ''' A collection of properties about the execution environment:
18 ''' - HW platform
19 ''' - Operating System
20 ''' - current user
21 ''' - LibreOffice version
23 ''' Service invocation example:
24 ''' Dim platform As Variant
25 ''' platform = CreateScriptService(
"Platform
")
27 ''' Detailed user documentation:
28 ''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/
03/sf_platform.html?DbPAR=BASIC
29 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
31 REM ================================================================== EXCEPTIONS
33 REM ============================================================ MODULE CONSTANTS
35 REM ===================================================== CONSTRUCTOR/DESTRUCTOR
37 REM -----------------------------------------------------------------------------
38 Public Function Dispose() As Variant
40 End Function
' ScriptForge.SF_Array Explicit destructor
42 REM ================================================================== PROPERTIES
44 REM -----------------------------------------------------------------------------
45 Property Get Architecture() As String
46 ''' Returns the actual bit architecture
47 ''' Example:
48 ''' MsgBox platform.Architecture
' 64bit
49 Architecture = _PropertyGet(
"Architecture
")
50 End Property
' ScriptForge.SF_Platform.Architecture (get)
52 REM -----------------------------------------------------------------------------
53 Property Get ComputerName() As String
54 ''' Returns the computer
's network name
55 ''' Example:
56 ''' MsgBox platform.ComputerName
57 ComputerName = _PropertyGet(
"ComputerName
")
58 End Property
' ScriptForge.SF_Platform.ComputerName (get)
60 REM -----------------------------------------------------------------------------
61 Property Get CPUCount() As Integer
62 ''' Returns the number of Central Processor Units
63 ''' Example:
64 ''' MsgBox platform.CPUCount
' 4
65 CPUCount = _PropertyGet(
"CPUCount
")
66 End Property
' ScriptForge.SF_Platform.CPUCount (get)
68 REM -----------------------------------------------------------------------------
69 Property Get CurrentUser() As String
70 ''' Returns the name of logged in user
71 ''' Example:
72 ''' MsgBox platform.CurrentUser
73 CurrentUser = _PropertyGet(
"CurrentUser
")
74 End Property
' ScriptForge.SF_Platform.CurrentUser (get)
76 REM -----------------------------------------------------------------------------
77 Property Get Extensions() As Variant
78 ''' Returns the list of availableeExtensions as an unsorted array of unique strings
79 ''' To get the list sorted, use SF_Array.Sort()
80 ''' Example:
81 ''' myExtensionsList = platform.Extensions
82 Extensions = _PropertyGet(
"Extensions
")
83 End Property
' ScriptForge.SF_Platform.Extensions (get)
85 REM -----------------------------------------------------------------------------
86 Property Get FilterNames() As Variant
87 ''' Returns the list of available document import and export filter names as an unsorted array of unique strings
88 ''' To get the list sorted, use SF_Array.Sort()
89 ''' Example:
90 ''' myFilterNamesList = platform.FilterNames
91 FilterNames = _PropertyGet(
"FilterNames
")
92 End Property
' ScriptForge.SF_Platform.FilterNames (get)
94 REM -----------------------------------------------------------------------------
95 Property Get Fonts() As Variant
96 ''' Returns the list of available fonts as an unsorted array of unique strings
97 ''' To get the list sorted, use SF_Array.Sort()
98 ''' Example:
99 ''' myFontsList = platform.Fonts
100 Fonts = _PropertyGet(
"Fonts
")
101 End Property
' ScriptForge.SF_Platform.Fonts (get)
103 REM -----------------------------------------------------------------------------
104 Property Get FormatLocale() As String
105 ''' Returns the locale used for number and date formats, combining language-COUNTRY (la-CO)
106 ''' Example:
107 ''' MsgBox platform.FormatLocale
108 FormatLocale = _PropertyGet(
"FormatLocale
")
109 End Property
' ScriptForge.SF_Platform.FormatLocale (get)
111 REM -----------------------------------------------------------------------------
112 Property Get Locale() As String
113 ''' Returns the locale of the operating system, combining language-COUNTRY (la-CO)
114 ''' Example:
115 ''' MsgBox platform.Locale
116 Locale = _PropertyGet(
"Locale
")
117 End Property
' ScriptForge.SF_Platform.Locale (get)
119 REM -----------------------------------------------------------------------------
120 Property Get Machine() As String
121 ''' Returns the machine type like
'i386
' or
'x86_64
'
122 ''' Example:
123 ''' MsgBox platform.Machine
124 Machine = _PropertyGet(
"Machine
")
125 End Property
' ScriptForge.SF_Platform.Machine (get)
127 REM -----------------------------------------------------------------------------
128 Property Get ObjectType As String
129 ''' Only to enable object representation
130 ObjectType =
"SF_Platform
"
131 End Property
' ScriptForge.SF_Platform.ObjectType
133 REM -----------------------------------------------------------------------------
134 Property Get OfficeLocale() As String
135 ''' Returns the locale of the user interface, combining language-COUNTRY (la-CO)
136 ''' Example:
137 ''' MsgBox platform.OfficeLocale
138 OfficeLocale = _PropertyGet(
"OfficeLocale
")
139 End Property
' ScriptForge.SF_Platform.OfficeLocale (get)
141 REM -----------------------------------------------------------------------------
142 Property Get OfficeVersion() As String
143 ''' Returns the office software version in the form
'LibreOffice w.x.y.z (The Document Foundation)
'
144 ''' Example:
145 ''' MsgBox platform.OfficeVersion
146 OfficeVersion = _PropertyGet(
"OfficeVersion
")
147 End Property
' ScriptForge.SF_Platform.OfficeVersion (get)
149 REM -----------------------------------------------------------------------------
150 Property Get OSName() As String
151 ''' Returns the name of the operating system like
'Linux
' or
'Windows
'
152 ''' Example:
153 ''' MsgBox platform.OSName
154 OSName = _PropertyGet(
"OSName
")
155 End Property
' ScriptForge.SF_Platform.OSName (get)
157 REM -----------------------------------------------------------------------------
158 Property Get OSPlatform() As String
159 ''' Returns a single string identifying the underlying platform with as much useful and human-readable information as possible
160 ''' Example:
161 ''' MsgBox platform.OSPlatform
' Linux-
4.15.0-
117-generic-x86_64-with-Ubuntu-
18.04-bionic
162 OSPlatform = _PropertyGet(
"OSPlatform
")
163 End Property
' ScriptForge.SF_Platform.OSPlatform (get)
165 REM -----------------------------------------------------------------------------
166 Property Get OSRelease() As String
167 ''' Returns the operating system
's release
168 ''' Example:
169 ''' MsgBox platform.OSRelease
' 4.15.0-
117-generic
170 OSRelease = _PropertyGet(
"OSRelease
")
171 End Property
' ScriptForge.SF_Platform.OSRelease (get)
173 REM -----------------------------------------------------------------------------
174 Property Get OSVersion() As String
175 ''' Returns the name of the operating system build or version
176 ''' Example:
177 ''' MsgBox platform.OSVersion
' #
118-Ubuntu SMP Fri Sep
4 20:
02:
41 UTC
2020
178 OSVersion = _PropertyGet(
"OSVersion
")
179 End Property
' ScriptForge.SF_Platform.OSVersion (get)
181 REM -----------------------------------------------------------------------------
182 Property Get Printers() As Variant
183 ''' Returns the list of available printers type as a zero-based array
184 ''' The default printer is put in the
1st position in the list (index =
0)
185 ''' Example:
186 ''' MsgBox join(platform.Printers,
",
")
187 Printers = _PropertyGet(
"Printers
")
188 End Property
' ScriptForge.SF_Platform.Printers (get)
190 REM -----------------------------------------------------------------------------
191 Property Get Processor() As String
192 ''' Returns the (real) processor name, e.g.
'amdk6
'. Might return the same value as Machine
193 ''' Example:
194 ''' MsgBox platform.Processor
195 Processor = _PropertyGet(
"Processor
")
196 End Property
' ScriptForge.SF_Platform.Processor (get)
198 REM -----------------------------------------------------------------------------
199 Property Get PythonVersion() As String
200 ''' Returns the Python version as string
'Python major.minor.patchlevel
'
201 ''' Example:
202 ''' MsgBox platform.PythonVersion
' Python
3.7.7
203 PythonVersion = _PropertyGet(
"PythonVersion
")
204 End Property
' ScriptForge.SF_Platform.PythonVersion (get)
206 REM -----------------------------------------------------------------------------
207 Property Get ServiceName As String
208 ''' Internal use
209 ServiceName =
"ScriptForge.Platform
"
210 End Property
' ScriptForge.SF_Platform.ServiceName
212 REM -----------------------------------------------------------------------------
213 Property Get SystemLocale() As String
214 ''' Returns the locale of the operating system, combining language-COUNTRY (la-CO)
215 ''' Example:
216 ''' MsgBox platform.SystemLocale
217 SystemLocale = _PropertyGet(
"SystemLocale
")
218 End Property
' ScriptForge.SF_Platform.SystemLocale (get)
220 REM -----------------------------------------------------------------------------
221 Property Get UserData() As Variant
222 ''' Returns a dictionary of all Options + User Data values
223 ''' Example:
224 ''' dict = platform.UserData
225 UserData = _PropertyGet(
"UserData
")
226 End Property
' ScriptForge.SF_Platform.UserData (get)
228 REM ===================================================================== METHODS
230 REM -----------------------------------------------------------------------------
231 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
232 ''' Return the actual value of the given property
233 ''' Args:
234 ''' PropertyName: the name of the property as a string
235 ''' Returns:
236 ''' The actual value of the property
237 ''' If the property does not exist, returns Null
238 ''' Exceptions:
239 ''' ARGUMENTERROR The property does not exist
241 Const cstThisSub =
"Platform.GetProperty
"
242 Const cstSubArgs =
""
244 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
248 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
249 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
253 GetProperty = _PropertyGet(PropertyName)
256 SF_Utils._ExitFunction(cstThisSub)
260 End Function
' ScriptForge.SF_Platform.GetProperty
262 REM -----------------------------------------------------------------------------
263 Public Function Methods() As Variant
264 ''' Return the list of public methods of the Model service as an array
269 End Function
' ScriptForge.SF_Platform.Methods
271 REM -----------------------------------------------------------------------------
272 Public Function Properties() As Variant
273 ''' Return the list or properties of the Platform class as an array
275 Properties = Array( _
276 "Architecture
" _
277 ,
"ComputerName
" _
278 ,
"CPUCount
" _
279 ,
"CurrentUser
" _
280 ,
"Extensions
" _
281 ,
"FilterNames
" _
282 ,
"Fonts
" _
283 ,
"FormatLocale
" _
284 ,
"Locale
" _
285 ,
"Machine
" _
286 ,
"OfficeLocale
" _
287 ,
"OfficeVersion
" _
288 ,
"OSName
" _
289 ,
"OSPlatform
" _
290 ,
"OSRelease
" _
291 ,
"OSVersion
" _
292 ,
"Printers
" _
293 ,
"Processor
" _
294 ,
"PythonVersion
" _
295 ,
"SystemLocale
" _
296 ,
"UserData
" _
299 End Function
' ScriptForge.SF_Platform.Properties
301 REM =========================================================== PRIVATE FUNCTIONS
303 REM -----------------------------------------------------------------------------
304 Public Function _GetPrinters() as Variant
305 ''' Returns the list of available printers.
306 ''' The default printer is put in the
1st position (index =
0)
308 Dim oPrinterServer As Object
' com.sun.star.awt.PrinterServer
309 Dim vPrinters As Variant
' Array of printer names
310 Dim sDefaultPrinter As String
' The default printer
311 Dim lDefault As Long
' Initial position of the default printer in the list
313 On Local Error GoTo Catch
' Prevent any error
318 Set oPrinterServer = SF_Utils._GetUNOService(
"PrinterServer
")
320 vPrinters = .getPrinterNames()
321 sDefaultPrinter = .getDefaultPrinterName()
324 ' Put the default printer on top of the list
325 If Len(sDefaultPrinter)
> 0 Then
326 lDefault = SF_Array.IndexOf(vPrinters, sDefaultPrinter, CaseSensitive := True)
327 If lDefault
> 0 Then
' Invert
2 printers
328 vPrinters(lDefault) = vPrinters(
0)
329 vPrinters(
0) = sDefaultPrinter
334 _GetPrinters() = vPrinters()
338 End Function
' ScriptForge.SF_Platform._GetPrinters
340 REM -----------------------------------------------------------------------------
341 Public Function _GetProductName() as String
342 ''' Returns Office product and version numbers found in configuration registry
343 ''' Derived from the Tools library
345 Dim oProdNameAccess as Object
' configmgr.RootAccess
346 Dim sProdName as String
347 Dim sVersion as String
348 Dim sVendor As String
350 On Local Error GoTo Catch
' Prevent any error
351 _GetProductName =
""
354 Set oProdNameAccess = SF_Utils._GetRegistryKeyContent(
"org.openoffice.Setup/Product
")
356 sProdName = oProdNameAccess.ooName
357 sVersion = oProdNameAccess.ooSetupVersionAboutBox
358 sVendor = oProdNameAccess.ooVendor
360 _GetProductName = sProdName
& " " & sVersion
& " (
" & sVendor
& ")
"
366 End Function
' ScriptForge.SF_Platform._GetProductName
368 REM -----------------------------------------------------------------------------
369 Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
370 ''' Return the value of the named property
371 ''' Args:
372 ''' psProperty: the name of the property
374 Dim sOSName As String
' Operating system
375 Dim oLocale As Object
' com.sun.star.lang.Locale
376 Dim oPrinterServer As Object
' com.sun.star.awt.PrinterServer
377 Dim oToolkit As Object
' com.sun.star.awt.Toolkit
378 Dim oDevice As Object
' com.sun.star.awt.XDevice
379 Dim oFilterFactory As Object
' com.sun.star.document.FilterFactory
380 Dim oFontDescriptors As Variant
' Array of com.sun.star.awt.FontDescriptor
381 Dim sFonts As String
' Comma-separated list of fonts
382 Dim sFont As String
' A single font name
383 Dim vExtensionsList As Variant
' Array of extension descriptors
384 Dim sExtensions As String
' Comma separated list of extensions
385 Dim sExtension As String
' A single extension name
386 Dim vUserDataInternal As Variant
' The internal names of the supported user data items
387 Dim vUserDataExternal As Variant
' The external names of the supported user data items
388 Dim vUserData As Variant
' A SF_Dictionary instance linking user data external names and values
389 Dim vUserDataOptions As Variant
' configmgr.RootAccess
392 Const cstPyHelper =
"$
" & "_SF_Platform
"
393 Dim cstThisSub As String
394 Const cstSubArgs =
""
396 cstThisSub =
"Platform.get
" & psProperty
397 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
399 Select Case psProperty
400 Case
"Architecture
",
"ComputerName
",
"CPUCount
",
"CurrentUser
",
"Machine
" _
401 ,
"OSPlatform
",
"OSRelease
",
"OSVersion
",
"Processor
",
"PythonVersion
"
402 With ScriptForge.SF_Session
403 _PropertyGet = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper
& cstPyHelper, psProperty)
405 Case
"Extensions
"
406 Set vExtensionsList = SF_Utils._GetUnoService(
"PackageInformationProvider
").ExtensionList
407 sExtensions =
""
408 For i =
0 To UBound(vExtensionsList)
409 sExtensions = sExtensions
& ",
" & vExtensionsList(i)(
0)
411 If Len(sExtensions)
> 0 Then _PropertyGet = Split(Mid(sExtensions,
2),
",
") Else _PropertyGet = Array()
412 Case
"FilterNames
"
413 Set oFilterFactory = SF_Utils._GetUNOService(
"FilterFactory
")
414 _PropertyGet = oFilterFactory.getElementNames()
415 Case
"Fonts
"
416 Set oToolkit = SF_Utils._GetUnoService(
"Toolkit
")
417 Set oDevice = oToolkit.createScreenCompatibleDevice(
0,
0)
418 oFontDescriptors = oDevice.FontDescriptors()
419 sFonts =
",
"
420 ' Select only not yet registered fonts
421 For i =
0 To UBound(oFontDescriptors)
422 sFont = oFontDescriptors(i).Name
423 If InStr(
1, sFonts,
",
" & sFont
& ",
",
0) =
0 Then sFonts = sFonts
& sFont
& ",
" ' Case-sensitive comparison
425 ' Remove leading and trailing commas
426 If Len(sFonts)
> 1 Then _PropertyGet = Split(Mid(sFonts,
2, Len(sFonts) -
2),
",
") Else _PropertyGet = Array()
427 Case
"FormatLocale
"
428 Set oLocale = SF_Utils._GetUNOService(
"FormatLocale
")
429 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
430 Case
"OfficeLocale
"
431 Set oLocale = SF_Utils._GetUNOService(
"OfficeLocale
")
432 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
433 Case
"OfficeVersion
"
434 _PropertyGet = _GetProductName()
435 Case
"OSName
"
436 ' Calc INFO function preferred to Python script to avoid ScriptForge initialization risks when Python is not installed
437 sOSName = _SF_.OSName
438 If sOSName =
"" Then
439 sOSName = SF_Session.ExecuteCalcFunction(
"INFO
",
"system
")
441 Case
"WNT
" : sOSName =
"Windows
"
442 Case
"MACOSX
" : sOSName =
"macOS
"
443 Case
"LINUX
" : sOSName =
"Linux
"
444 Case
"SOLARIS
" : sOSName =
"Solaris
"
445 Case Else : sOSName = SF_String.Capitalize(sOSName)
448 _PropertyGet = sOSName
449 Case
"Printers
"
450 _PropertyGet = _GetPrinters()
451 Case
"SystemLocale
",
"Locale
"
452 Set oLocale = SF_Utils._GetUNOService(
"SystemLocale
")
453 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
454 Case
"UserData
"
455 vUserDataExternal = Array( _
456 "city
",
"company
",
"country
",
"email
",
"encryptionkey
",
"encrypttoself
",
"fax
" _
457 ,
"firstname
",
"homephone
",
"initials
",
"lastname
",
"officephone
",
"position
" _
458 ,
"postalcode
",
"signingkey
",
"state
",
"street
",
"title
" _
460 vUserDataInternal = Array( _
461 "l
",
"o
",
"c
",
"mail
",
"encryptionkey
",
"encrypttoself
",
"facsimiletelephonenumber
" _
462 ,
"givenname
",
"homephone
",
"initials
",
"sn
",
"telephonenumber
",
"position
" _
463 ,
"postalcode
",
"signingkey
",
"st
",
"street
",
"title
" _
465 ' Get the UserData page from the Options database
466 vUserDataOptions = SF_Utils._GetRegistryKeyContent(
"org.openoffice.UserProfile/Data
")
467 ' Create and feed an output dictionary with case-sensitive comparison of keys
468 vUserData = CreateScriptService(
"ScriptForge.Dictionary
", True)
469 For i =
0 To UBound(vUserDataExternal)
470 vUserData.Add(vUserDataExternal(i), vUserDataOptions.getByName(vUserDataInternal(i)))
472 _PropertyGet = vUserData
478 SF_Utils._ExitFunction(cstThisSub)
480 End Function
' ScriptForge.SF_Platform._PropertyGet
482 REM ============================================ END OF SCRIPTFORGE.SF_PLATFORM