483 lines
No EOL
23 KiB
XML
483 lines
No EOL
23 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
|
|
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_Platform" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
|
|
REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
|
|
REM === Full documentation is available on https://help.libreoffice.org/ ===
|
|
REM =======================================================================================================================
|
|
|
|
Option Compatible
|
|
Option Explicit
|
|
|
|
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|
|
''' SF_Platform
|
|
''' ===========
|
|
''' Singleton class implementing the "ScriptForge.Platform" service
|
|
''' Implemented as a usual Basic module
|
|
'''
|
|
''' A collection of properties about the execution environment:
|
|
''' - HW platform
|
|
''' - Operating System
|
|
''' - current user
|
|
''' - LibreOffice version
|
|
'''
|
|
''' Service invocation example:
|
|
''' Dim platform As Variant
|
|
''' platform = CreateScriptService("Platform")
|
|
'''
|
|
''' Detailed user documentation:
|
|
''' https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_platform.html?DbPAR=BASIC
|
|
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
|
|
|
|
REM ================================================================== EXCEPTIONS
|
|
|
|
REM ============================================================ MODULE CONSTANTS
|
|
|
|
REM ===================================================== CONSTRUCTOR/DESTRUCTOR
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function Dispose() As Variant
|
|
Set Dispose = Nothing
|
|
End Function ' ScriptForge.SF_Array Explicit destructor
|
|
|
|
REM ================================================================== PROPERTIES
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Architecture() As String
|
|
''' Returns the actual bit architecture
|
|
''' Example:
|
|
''' MsgBox platform.Architecture ' 64bit
|
|
Architecture = _PropertyGet("Architecture")
|
|
End Property ' ScriptForge.SF_Platform.Architecture (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get ComputerName() As String
|
|
''' Returns the computer's network name
|
|
''' Example:
|
|
''' MsgBox platform.ComputerName
|
|
ComputerName = _PropertyGet("ComputerName")
|
|
End Property ' ScriptForge.SF_Platform.ComputerName (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get CPUCount() As Integer
|
|
''' Returns the number of Central Processor Units
|
|
''' Example:
|
|
''' MsgBox platform.CPUCount ' 4
|
|
CPUCount = _PropertyGet("CPUCount")
|
|
End Property ' ScriptForge.SF_Platform.CPUCount (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get CurrentUser() As String
|
|
''' Returns the name of logged in user
|
|
''' Example:
|
|
''' MsgBox platform.CurrentUser
|
|
CurrentUser = _PropertyGet("CurrentUser")
|
|
End Property ' ScriptForge.SF_Platform.CurrentUser (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Extensions() As Variant
|
|
''' Returns the list of availableeExtensions as an unsorted array of unique strings
|
|
''' To get the list sorted, use SF_Array.Sort()
|
|
''' Example:
|
|
''' myExtensionsList = platform.Extensions
|
|
Extensions = _PropertyGet("Extensions")
|
|
End Property ' ScriptForge.SF_Platform.Extensions (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get FilterNames() As Variant
|
|
''' Returns the list of available document import and export filter names as an unsorted array of unique strings
|
|
''' To get the list sorted, use SF_Array.Sort()
|
|
''' Example:
|
|
''' myFilterNamesList = platform.FilterNames
|
|
FilterNames = _PropertyGet("FilterNames")
|
|
End Property ' ScriptForge.SF_Platform.FilterNames (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Fonts() As Variant
|
|
''' Returns the list of available fonts as an unsorted array of unique strings
|
|
''' To get the list sorted, use SF_Array.Sort()
|
|
''' Example:
|
|
''' myFontsList = platform.Fonts
|
|
Fonts = _PropertyGet("Fonts")
|
|
End Property ' ScriptForge.SF_Platform.Fonts (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get FormatLocale() As String
|
|
''' Returns the locale used for number and date formats, combining language-COUNTRY (la-CO)
|
|
''' Example:
|
|
''' MsgBox platform.FormatLocale
|
|
FormatLocale = _PropertyGet("FormatLocale")
|
|
End Property ' ScriptForge.SF_Platform.FormatLocale (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Locale() As String
|
|
''' Returns the locale of the operating system, combining language-COUNTRY (la-CO)
|
|
''' Example:
|
|
''' MsgBox platform.Locale
|
|
Locale = _PropertyGet("Locale")
|
|
End Property ' ScriptForge.SF_Platform.Locale (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Machine() As String
|
|
''' Returns the machine type like 'i386' or 'x86_64'
|
|
''' Example:
|
|
''' MsgBox platform.Machine
|
|
Machine = _PropertyGet("Machine")
|
|
End Property ' ScriptForge.SF_Platform.Machine (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get ObjectType As String
|
|
''' Only to enable object representation
|
|
ObjectType = "SF_Platform"
|
|
End Property ' ScriptForge.SF_Platform.ObjectType
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OfficeLocale() As String
|
|
''' Returns the locale of the user interface, combining language-COUNTRY (la-CO)
|
|
''' Example:
|
|
''' MsgBox platform.OfficeLocale
|
|
OfficeLocale = _PropertyGet("OfficeLocale")
|
|
End Property ' ScriptForge.SF_Platform.OfficeLocale (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OfficeVersion() As String
|
|
''' Returns the office software version in the form 'LibreOffice w.x.y.z (The Document Foundation)'
|
|
''' Example:
|
|
''' MsgBox platform.OfficeVersion
|
|
OfficeVersion = _PropertyGet("OfficeVersion")
|
|
End Property ' ScriptForge.SF_Platform.OfficeVersion (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OSName() As String
|
|
''' Returns the name of the operating system like 'Linux' or 'Windows'
|
|
''' Example:
|
|
''' MsgBox platform.OSName
|
|
OSName = _PropertyGet("OSName")
|
|
End Property ' ScriptForge.SF_Platform.OSName (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OSPlatform() As String
|
|
''' Returns a single string identifying the underlying platform with as much useful and human-readable information as possible
|
|
''' Example:
|
|
''' MsgBox platform.OSPlatform ' Linux-4.15.0-117-generic-x86_64-with-Ubuntu-18.04-bionic
|
|
OSPlatform = _PropertyGet("OSPlatform")
|
|
End Property ' ScriptForge.SF_Platform.OSPlatform (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OSRelease() As String
|
|
''' Returns the operating system's release
|
|
''' Example:
|
|
''' MsgBox platform.OSRelease ' 4.15.0-117-generic
|
|
OSRelease = _PropertyGet("OSRelease")
|
|
End Property ' ScriptForge.SF_Platform.OSRelease (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get OSVersion() As String
|
|
''' Returns the name of the operating system build or version
|
|
''' Example:
|
|
''' MsgBox platform.OSVersion ' #118-Ubuntu SMP Fri Sep 4 20:02:41 UTC 2020
|
|
OSVersion = _PropertyGet("OSVersion")
|
|
End Property ' ScriptForge.SF_Platform.OSVersion (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Printers() As Variant
|
|
''' Returns the list of available printers type as a zero-based array
|
|
''' The default printer is put in the 1st position in the list (index = 0)
|
|
''' Example:
|
|
''' MsgBox join(platform.Printers, ",")
|
|
Printers = _PropertyGet("Printers")
|
|
End Property ' ScriptForge.SF_Platform.Printers (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get Processor() As String
|
|
''' Returns the (real) processor name, e.g. 'amdk6'. Might return the same value as Machine
|
|
''' Example:
|
|
''' MsgBox platform.Processor
|
|
Processor = _PropertyGet("Processor")
|
|
End Property ' ScriptForge.SF_Platform.Processor (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get PythonVersion() As String
|
|
''' Returns the Python version as string 'Python major.minor.patchlevel'
|
|
''' Example:
|
|
''' MsgBox platform.PythonVersion ' Python 3.7.7
|
|
PythonVersion = _PropertyGet("PythonVersion")
|
|
End Property ' ScriptForge.SF_Platform.PythonVersion (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get ServiceName As String
|
|
''' Internal use
|
|
ServiceName = "ScriptForge.Platform"
|
|
End Property ' ScriptForge.SF_Platform.ServiceName
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get SystemLocale() As String
|
|
''' Returns the locale of the operating system, combining language-COUNTRY (la-CO)
|
|
''' Example:
|
|
''' MsgBox platform.SystemLocale
|
|
SystemLocale = _PropertyGet("SystemLocale")
|
|
End Property ' ScriptForge.SF_Platform.SystemLocale (get)
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Property Get UserData() As Variant
|
|
''' Returns a dictionary of all Options + User Data values
|
|
''' Example:
|
|
''' dict = platform.UserData
|
|
UserData = _PropertyGet("UserData")
|
|
End Property ' ScriptForge.SF_Platform.UserData (get)
|
|
|
|
REM ===================================================================== METHODS
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
|
|
''' Return the actual value of the given property
|
|
''' Args:
|
|
''' PropertyName: the name of the property as a string
|
|
''' Returns:
|
|
''' The actual value of the property
|
|
''' If the property does not exist, returns Null
|
|
''' Exceptions:
|
|
''' ARGUMENTERROR The property does not exist
|
|
|
|
Const cstThisSub = "Platform.GetProperty"
|
|
Const cstSubArgs = ""
|
|
|
|
If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
|
|
GetProperty = Null
|
|
|
|
Check:
|
|
If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
|
|
If Not SF_Utils._Validate(PropertyName, "PropertyName", V_STRING, Properties()) Then GoTo Catch
|
|
End If
|
|
|
|
Try:
|
|
GetProperty = _PropertyGet(PropertyName)
|
|
|
|
Finally:
|
|
SF_Utils._ExitFunction(cstThisSub)
|
|
Exit Function
|
|
Catch:
|
|
GoTo Finally
|
|
End Function ' ScriptForge.SF_Platform.GetProperty
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function Methods() As Variant
|
|
''' Return the list of public methods of the Model service as an array
|
|
|
|
Methods = Array( _
|
|
)
|
|
|
|
End Function ' ScriptForge.SF_Platform.Methods
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function Properties() As Variant
|
|
''' Return the list or properties of the Platform class as an array
|
|
|
|
Properties = Array( _
|
|
"Architecture" _
|
|
, "ComputerName" _
|
|
, "CPUCount" _
|
|
, "CurrentUser" _
|
|
, "Extensions" _
|
|
, "FilterNames" _
|
|
, "Fonts" _
|
|
, "FormatLocale" _
|
|
, "Locale" _
|
|
, "Machine" _
|
|
, "OfficeLocale" _
|
|
, "OfficeVersion" _
|
|
, "OSName" _
|
|
, "OSPlatform" _
|
|
, "OSRelease" _
|
|
, "OSVersion" _
|
|
, "Printers" _
|
|
, "Processor" _
|
|
, "PythonVersion" _
|
|
, "SystemLocale" _
|
|
, "UserData" _
|
|
)
|
|
|
|
End Function ' ScriptForge.SF_Platform.Properties
|
|
|
|
REM =========================================================== PRIVATE FUNCTIONS
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function _GetPrinters() as Variant
|
|
''' Returns the list of available printers.
|
|
''' The default printer is put in the 1st position (index = 0)
|
|
|
|
Dim oPrinterServer As Object ' com.sun.star.awt.PrinterServer
|
|
Dim vPrinters As Variant ' Array of printer names
|
|
Dim sDefaultPrinter As String ' The default printer
|
|
Dim lDefault As Long ' Initial position of the default printer in the list
|
|
|
|
On Local Error GoTo Catch ' Prevent any error
|
|
vPrinters = Array()
|
|
|
|
Try:
|
|
' Find printers
|
|
Set oPrinterServer = SF_Utils._GetUNOService("PrinterServer")
|
|
With oPrinterServer
|
|
vPrinters = .getPrinterNames()
|
|
sDefaultPrinter = .getDefaultPrinterName()
|
|
End With
|
|
|
|
' Put the default printer on top of the list
|
|
If Len(sDefaultPrinter) > 0 Then
|
|
lDefault = SF_Array.IndexOf(vPrinters, sDefaultPrinter, CaseSensitive := True)
|
|
If lDefault > 0 Then ' Invert 2 printers
|
|
vPrinters(lDefault) = vPrinters(0)
|
|
vPrinters(0) = sDefaultPrinter
|
|
End If
|
|
End If
|
|
|
|
Finally:
|
|
_GetPrinters() = vPrinters()
|
|
Exit Function
|
|
Catch:
|
|
GoTo Finally
|
|
End Function ' ScriptForge.SF_Platform._GetPrinters
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Public Function _GetProductName() as String
|
|
''' Returns Office product and version numbers found in configuration registry
|
|
''' Derived from the Tools library
|
|
|
|
Dim oProdNameAccess as Object ' configmgr.RootAccess
|
|
Dim sProdName as String
|
|
Dim sVersion as String
|
|
Dim sVendor As String
|
|
|
|
On Local Error GoTo Catch ' Prevent any error
|
|
_GetProductName = ""
|
|
|
|
Try:
|
|
Set oProdNameAccess = SF_Utils._GetRegistryKeyContent("org.openoffice.Setup/Product")
|
|
|
|
sProdName = oProdNameAccess.ooName
|
|
sVersion = oProdNameAccess.ooSetupVersionAboutBox
|
|
sVendor = oProdNameAccess.ooVendor
|
|
|
|
_GetProductName = sProdName & " " & sVersion & " (" & sVendor & ")"
|
|
|
|
Finally:
|
|
Exit Function
|
|
Catch:
|
|
GoTo Finally
|
|
End Function ' ScriptForge.SF_Platform._GetProductName
|
|
|
|
REM -----------------------------------------------------------------------------
|
|
Private Function _PropertyGet(Optional ByVal psProperty As String) As Variant
|
|
''' Return the value of the named property
|
|
''' Args:
|
|
''' psProperty: the name of the property
|
|
|
|
Dim sOSName As String ' Operating system
|
|
Dim oLocale As Object ' com.sun.star.lang.Locale
|
|
Dim oPrinterServer As Object ' com.sun.star.awt.PrinterServer
|
|
Dim oToolkit As Object ' com.sun.star.awt.Toolkit
|
|
Dim oDevice As Object ' com.sun.star.awt.XDevice
|
|
Dim oFilterFactory As Object ' com.sun.star.document.FilterFactory
|
|
Dim oFontDescriptors As Variant ' Array of com.sun.star.awt.FontDescriptor
|
|
Dim sFonts As String ' Comma-separated list of fonts
|
|
Dim sFont As String ' A single font name
|
|
Dim vExtensionsList As Variant ' Array of extension descriptors
|
|
Dim sExtensions As String ' Comma separated list of extensions
|
|
Dim sExtension As String ' A single extension name
|
|
Dim vUserDataInternal As Variant' The internal names of the supported user data items
|
|
Dim vUserDataExternal As Variant' The external names of the supported user data items
|
|
Dim vUserData As Variant ' A SF_Dictionary instance linking user data external names and values
|
|
Dim vUserDataOptions As Variant ' configmgr.RootAccess
|
|
Dim i As Long
|
|
|
|
Const cstPyHelper = "$" & "_SF_Platform"
|
|
Dim cstThisSub As String
|
|
Const cstSubArgs = ""
|
|
|
|
cstThisSub = "Platform.get" & psProperty
|
|
SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
|
|
|
|
Select Case psProperty
|
|
Case "Architecture", "ComputerName", "CPUCount", "CurrentUser", "Machine" _
|
|
, "OSPlatform", "OSRelease", "OSVersion", "Processor", "PythonVersion"
|
|
With ScriptForge.SF_Session
|
|
_PropertyGet = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper & cstPyHelper, psProperty)
|
|
End With
|
|
Case "Extensions"
|
|
Set vExtensionsList = SF_Utils._GetUnoService("PackageInformationProvider").ExtensionList
|
|
sExtensions = ""
|
|
For i = 0 To UBound(vExtensionsList)
|
|
sExtensions = sExtensions & "," & vExtensionsList(i)(0)
|
|
Next i
|
|
If Len(sExtensions) > 0 Then _PropertyGet = Split(Mid(sExtensions, 2), ",") Else _PropertyGet = Array()
|
|
Case "FilterNames"
|
|
Set oFilterFactory = SF_Utils._GetUNOService("FilterFactory")
|
|
_PropertyGet = oFilterFactory.getElementNames()
|
|
Case "Fonts"
|
|
Set oToolkit = SF_Utils._GetUnoService("Toolkit")
|
|
Set oDevice = oToolkit.createScreenCompatibleDevice(0, 0)
|
|
oFontDescriptors = oDevice.FontDescriptors()
|
|
sFonts = ","
|
|
' Select only not yet registered fonts
|
|
For i = 0 To UBound(oFontDescriptors)
|
|
sFont = oFontDescriptors(i).Name
|
|
If InStr(1, sFonts, "," & sFont & ",", 0) = 0 Then sFonts = sFonts & sFont & "," ' Case-sensitive comparison
|
|
Next i
|
|
' Remove leading and trailing commas
|
|
If Len(sFonts) > 1 Then _PropertyGet = Split(Mid(sFonts, 2, Len(sFonts) - 2), ",") Else _PropertyGet = Array()
|
|
Case "FormatLocale"
|
|
Set oLocale = SF_Utils._GetUNOService("FormatLocale")
|
|
_PropertyGet = oLocale.Language & "-" & oLocale.Country
|
|
Case "OfficeLocale"
|
|
Set oLocale = SF_Utils._GetUNOService("OfficeLocale")
|
|
_PropertyGet = oLocale.Language & "-" & oLocale.Country
|
|
Case "OfficeVersion"
|
|
_PropertyGet = _GetProductName()
|
|
Case "OSName"
|
|
' Calc INFO function preferred to Python script to avoid ScriptForge initialization risks when Python is not installed
|
|
sOSName = _SF_.OSName
|
|
If sOSName = "" Then
|
|
sOSName = SF_Session.ExecuteCalcFunction("INFO", "system")
|
|
Select Case sOSName
|
|
Case "WNT" : sOSName = "Windows"
|
|
Case "MACOSX" : sOSName = "macOS"
|
|
Case "LINUX" : sOSName = "Linux"
|
|
Case "SOLARIS" : sOSName = "Solaris"
|
|
Case Else : sOSName = SF_String.Capitalize(sOSName)
|
|
End Select
|
|
EndIf
|
|
_PropertyGet = sOSName
|
|
Case "Printers"
|
|
_PropertyGet = _GetPrinters()
|
|
Case "SystemLocale", "Locale"
|
|
Set oLocale = SF_Utils._GetUNOService("SystemLocale")
|
|
_PropertyGet = oLocale.Language & "-" & oLocale.Country
|
|
Case "UserData"
|
|
vUserDataExternal = Array( _
|
|
"city", "company", "country", "email", "encryptionkey", "encrypttoself", "fax" _
|
|
, "firstname", "homephone", "initials", "lastname", "officephone", "position" _
|
|
, "postalcode", "signingkey", "state", "street", "title" _
|
|
)
|
|
vUserDataInternal = Array( _
|
|
"l", "o", "c", "mail", "encryptionkey", "encrypttoself", "facsimiletelephonenumber" _
|
|
, "givenname", "homephone", "initials", "sn", "telephonenumber", "position" _
|
|
, "postalcode", "signingkey", "st", "street", "title" _
|
|
)
|
|
' Get the UserData page from the Options database
|
|
vUserDataOptions = SF_Utils._GetRegistryKeyContent("org.openoffice.UserProfile/Data")
|
|
' Create and feed an output dictionary with case-sensitive comparison of keys
|
|
vUserData = CreateScriptService("ScriptForge.Dictionary", True)
|
|
For i = 0 To UBound(vUserDataExternal)
|
|
vUserData.Add(vUserDataExternal(i), vUserDataOptions.getByName(vUserDataInternal(i)))
|
|
Next i
|
|
_PropertyGet = vUserData
|
|
Case Else
|
|
_PropertyGet = Null
|
|
End Select
|
|
|
|
Finally:
|
|
SF_Utils._ExitFunction(cstThisSub)
|
|
Exit Function
|
|
End Function ' ScriptForge.SF_Platform._PropertyGet
|
|
|
|
REM ============================================ END OF SCRIPTFORGE.SF_PLATFORM
|
|
</script:module> |