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 ===================================================================== METHODS
222 REM -----------------------------------------------------------------------------
223 Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
224 ''' Return the actual value of the given property
225 ''' Args:
226 ''' PropertyName: the name of the property as a string
227 ''' Returns:
228 ''' The actual value of the property
229 ''' If the property does not exist, returns Null
230 ''' Exceptions:
231 ''' ARGUMENTERROR The property does not exist
233 Const cstThisSub =
"Platform.GetProperty
"
234 Const cstSubArgs =
""
236 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
240 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
241 If Not SF_Utils._Validate(PropertyName,
"PropertyName
", V_STRING, Properties()) Then GoTo Catch
245 GetProperty = _PropertyGet(PropertyName)
248 SF_Utils._ExitFunction(cstThisSub)
252 End Function
' ScriptForge.SF_Platform.GetProperty
254 REM -----------------------------------------------------------------------------
255 Public Function Methods() As Variant
256 ''' Return the list of public methods of the Model service as an array
261 End Function
' ScriptForge.SF_Platform.Methods
263 REM -----------------------------------------------------------------------------
264 Public Function Properties() As Variant
265 ''' Return the list or properties of the Platform class as an array
267 Properties = Array( _
268 "Architecture
" _
269 ,
"ComputerName
" _
270 ,
"CPUCount
" _
271 ,
"CurrentUser
" _
272 ,
"Extensions
" _
273 ,
"FilterNames
" _
274 ,
"Fonts
" _
275 ,
"FormatLocale
" _
276 ,
"Locale
" _
277 ,
"Machine
" _
278 ,
"OfficeLocale
" _
279 ,
"OfficeVersion
" _
280 ,
"OSName
" _
281 ,
"OSPlatform
" _
282 ,
"OSRelease
" _
283 ,
"OSVersion
" _
284 ,
"Printers
" _
285 ,
"Processor
" _
286 ,
"PythonVersion
" _
287 ,
"SystemLocale
" _
290 End Function
' ScriptForge.SF_Platform.Properties
292 REM =========================================================== PRIVATE FUNCTIONS
294 REM -----------------------------------------------------------------------------
295 Public Function _GetPrinters() as Variant
296 ''' Returns the list of available printers.
297 ''' The default printer is put in the
1st position (index =
0)
299 Dim oPrinterServer As Object
' com.sun.star.awt.PrinterServer
300 Dim vPrinters As Variant
' Array of printer names
301 Dim sDefaultPrinter As String
' The default printer
302 Dim lDefault As Long
' Initial position of the default printer in the list
304 On Local Error GoTo Catch
' Prevent any error
309 Set oPrinterServer = SF_Utils._GetUNOService(
"PrinterServer
")
311 vPrinters = .getPrinterNames()
312 sDefaultPrinter = .getDefaultPrinterName()
315 ' Put the default printer on top of the list
316 If Len(sDefaultPrinter)
> 0 Then
317 lDefault = SF_Array.IndexOf(vPrinters, sDefaultPrinter, CaseSensitive := True)
318 If lDefault
> 0 Then
' Invert
2 printers
319 vPrinters(lDefault) = vPrinters(
0)
320 vPrinters(
0) = sDefaultPrinter
325 _GetPrinters() = vPrinters()
329 End Function
' ScriptForge.SF_Platform._GetPrinters
331 REM -----------------------------------------------------------------------------
332 Public Function _GetProductName() as String
333 ''' Returns Office product and version numbers found in configuration registry
334 ''' Derived from the Tools library
336 Dim oProdNameAccess as Object
' configmgr.RootAccess
337 Dim sProdName as String
338 Dim sVersion as String
339 Dim sVendor As String
341 On Local Error GoTo Catch
' Prevent any error
342 _GetProductName =
""
345 Set oProdNameAccess = SF_Utils._GetRegistryKeyContent(
"org.openoffice.Setup/Product
")
347 sProdName = oProdNameAccess.ooName
348 sVersion = oProdNameAccess.ooSetupVersionAboutBox
349 sVendor = oProdNameAccess.ooVendor
351 _GetProductName = sProdName
& " " & sVersion
& " (
" & sVendor
& ")
"
357 End Function
' ScriptForge.SF_Platform._GetProductName
359 REM -----------------------------------------------------------------------------
360 Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
361 ''' Return the value of the named property
362 ''' Args:
363 ''' psProperty: the name of the property
365 Dim sOSName As String
' Operating system
366 Dim oLocale As Object
' com.sun.star.lang.Locale
367 Dim oPrinterServer As Object
' com.sun.star.awt.PrinterServer
368 Dim oToolkit As Object
' com.sun.star.awt.Toolkit
369 Dim oDevice As Object
' com.sun.star.awt.XDevice
370 Dim oFilterFactory As Object
' com.sun.star.document.FilterFactory
371 Dim oFontDescriptors As Variant
' Array of com.sun.star.awt.FontDescriptor
372 Dim sFonts As String
' Comma-separated list of fonts
373 Dim sFont As String
' A single font name
374 Dim vExtensionsList As Variant
' Array of extension descriptors
375 Dim sExtensions As String
' Comma separated list of extensions
376 Dim sExtension As String
' A single extension name
379 Const cstPyHelper =
"$
" & "_SF_Platform
"
380 Dim cstThisSub As String
381 Const cstSubArgs =
""
383 cstThisSub =
"Platform.get
" & psProperty
384 SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
386 Select Case psProperty
387 Case
"Architecture
",
"ComputerName
",
"CPUCount
",
"CurrentUser
",
"Machine
" _
388 ,
"OSPlatform
",
"OSRelease
",
"OSVersion
",
"Processor
",
"PythonVersion
"
389 With ScriptForge.SF_Session
390 _PropertyGet = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper
& cstPyHelper, psProperty)
392 Case
"Extensions
"
393 Set vExtensionsList = SF_Utils._GetUnoService(
"PackageInformationProvider
").ExtensionList
394 sExtensions =
""
395 For i =
0 To UBound(vExtensionsList)
396 sExtensions = sExtensions
& ",
" & vExtensionsList(i)(
0)
398 If Len(sExtensions)
> 0 Then _PropertyGet = Split(Mid(sExtensions,
2),
",
") Else _PropertyGet = Array()
399 Case
"FilterNames
"
400 Set oFilterFactory = SF_Utils._GetUNOService(
"FilterFactory
")
401 _PropertyGet = oFilterFactory.getElementNames()
402 Case
"Fonts
"
403 Set oToolkit = SF_Utils._GetUnoService(
"Toolkit
")
404 Set oDevice = oToolkit.createScreenCompatibleDevice(
0,
0)
405 oFontDescriptors = oDevice.FontDescriptors()
406 sFonts =
",
"
407 ' Select only not yet registered fonts
408 For i =
0 To UBound(oFontDescriptors)
409 sFont = oFontDescriptors(i).Name
410 If InStr(
1, sFonts,
",
" & sFont
& ",
",
0) =
0 Then sFonts = sFonts
& sFont
& ",
" ' Case-sensitive comparison
412 ' Remove leading and trailing commas
413 If Len(sFonts)
> 1 Then _PropertyGet = Split(Mid(sFonts,
2, Len(sFonts) -
2),
",
") Else _PropertyGet = Array()
414 Case
"FormatLocale
"
415 Set oLocale = SF_Utils._GetUNOService(
"FormatLocale
")
416 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
417 Case
"OfficeLocale
"
418 Set oLocale = SF_Utils._GetUNOService(
"OfficeLocale
")
419 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
420 Case
"OfficeVersion
"
421 _PropertyGet = _GetProductName()
422 Case
"OSName
"
423 ' Calc INFO function preferred to Python script to avoid ScriptForge initialization risks when Python is not installed
424 sOSName = _SF_.OSName
425 If sOSName =
"" Then
426 sOSName = SF_Session.ExecuteCalcFunction(
"INFO
",
"system
")
428 Case
"WNT
" : sOSName =
"Windows
"
429 Case
"MACOSX
" : sOSName =
"macOS
"
430 Case
"LINUX
" : sOSName =
"Linux
"
431 Case
"SOLARIS
" : sOSName =
"Solaris
"
432 Case Else : sOSName = SF_String.Capitalize(sOSName)
435 _PropertyGet = sOSName
436 Case
"Printers
"
437 _PropertyGet = _GetPrinters()
438 Case
"SystemLocale
",
"Locale
"
439 Set oLocale = SF_Utils._GetUNOService(
"SystemLocale
")
440 _PropertyGet = oLocale.Language
& "-
" & oLocale.Country
446 SF_Utils._ExitFunction(cstThisSub)
448 End Function
' ScriptForge.SF_Platform._PropertyGet
450 REM ============================================ END OF SCRIPTFORGE.SF_PLATFORM