"Fossies" - the Fresh Open Source Software Archive

Member "gambas-3.16.3/comp/src/gb.desktop/.src/Main.module" (7 Sep 2021, 7775 Bytes) of package /linux/misc/gambas-3.16.3.tar.bz2:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "Main.module": 3.16.2_vs_3.16.3.

    1 ' Gambas module file
    2 
    3 Class X11
    4 
    5 Public Result As String
    6 
    7 Private $cTool As New Collection
    8 
    9 'Private _NET_CLIENT_LIST As Integer
   10 
   11 Private $aDataDir As String[]
   12 Private $sDesktop As String
   13 Private $cSearchPath As New Collection
   14 
   15 Public Sub RunXdgUtil(sProg As String, aArg As String[], Optional bWait As Boolean) 
   16   
   17   Dim sCmd As String
   18   Dim sDir As String
   19   Dim aCmd As String[]
   20   Dim bAgain As Boolean
   21   'Dim hProcess As Process
   22   
   23   sCmd = $cTool[sProg]
   24   
   25   If Not sCmd Then 
   26     'Shell "which " & sProg & " 2>&1" To sCmd
   27     'If Process.LastValue Then 
   28       sDir = Temp$("xdg-utils")
   29       sDir = File.Dir(sDir) &/ File.BaseName(sDir)
   30       Try Mkdir sDir
   31       
   32     TRY_AGAIN:
   33     
   34       sCmd = sDir &/ sProg
   35       Copy "xdg-utils" &/ sProg To sCmd
   36       Shell "chmod a+x " & Shell$(sCmd) Wait
   37       
   38       If Process.LastValue Then
   39         If bAgain Then Error.Raise("Unable to install xdg tool: " & sProg)
   40         sDir = "~/tmp/xdg-utils"
   41         Try Mkdir "~/tmp"
   42         bAgain = True
   43         Goto TRY_AGAIN
   44       Endif
   45       
   46     'Endif
   47     sCmd = Trim(sCmd)
   48     $cTool[sProg] = sCmd
   49   Endif
   50   
   51   aCmd = [sCmd]
   52   aCmd.Insert(aArg)
   53   
   54   If bWait Then
   55   
   56     Exec aCmd To Result 
   57     
   58     Select Case Process.LastValue  
   59       Case 1
   60         Error.Raise("Syntax error")
   61       Case 2
   62         Error.Raise("File does not exist")
   63       Case 3
   64         Error.Raise("A required tool could not be found")
   65       Case 4
   66         Error.Raise("The action has failed")
   67       Case 5
   68         Error.Raise("File access forbidden")
   69     End Select 
   70     
   71   Else
   72   
   73     Exec aCmd
   74     
   75   Endif
   76   
   77 End
   78 
   79 Private Function SearchPathConfig(sXdgConfigHome As String, sXdgConfigDirs As String) As String
   80   
   81   Dim sDir As String
   82   
   83   For Each sDir In Split(sXdgConfigHome, ":")
   84     If Exist(sDir &/ "user-dirs.dirs") Then ' Devrait exister
   85       Return sDir &/ "user-dirs.dirs"
   86     Endif
   87   Next
   88 
   89   For Each sDir In Split(sXdgConfigDirs, ":")
   90     If Exist(sDir &/ "user-dirs.defaults") Then ' Sinon l'un
   91       Return sDir &/ "user-dirs.defaults"
   92     Else If Exist(sDir &/ "user-dirs.dirs") Then ' Ou l'autre
   93       Return sDir &/ "user-dirs.dirs"
   94     Endif
   95   Next
   96   
   97 End
   98 
   99 Private Function SearchPathVariable(sFileConfig As String, sDirectory As String) As String
  100 
  101   Dim hFile As File
  102   Dim sLine As String
  103   Dim aLine As String[]
  104   Dim sAltDirectory As String
  105   Dim bFullPath As Boolean
  106   
  107   sAltDirectory = "XDG_" & sDirectory & "_DIR"
  108   
  109   hFile = Open sFileConfig For Input
  110   While Not Eof(hFile)
  111     Line Input #hFile, sLine
  112     sLine = Trim(sLine)
  113     If Not sLine Then Continue
  114     If Left(sLine) = "#" Then Continue
  115     If sLine Begins sAltDirectory Then 
  116       sDirectory = sAltDirectory
  117       bFullPath = True
  118       Break
  119     Endif
  120     If sLine Begins sDirectory Then Break
  121     sLine = ""
  122   Wend
  123   
  124   aLine = Scan(sLine, sDirectory & "=*")
  125   If aLine.Count Then
  126     sLine = aLine[0]
  127     If Left(sLine) = Chr$(34) Then sLine = Mid$(sLine, 2, -1)
  128     If sLine Begins "$HOME/" Then sLine = Replace(sLine, "$HOME", System.User.Home)
  129     If Not bFullPath Then sLine = User.Home &/ sLine
  130     Return sLine
  131   Endif
  132 
  133 End
  134 
  135 Public Sub GetDesktop() As String
  136 
  137   If Not $sDesktop Then
  138 
  139     If Application.Env["KDE_FULL_SESSION"] Then
  140       
  141       If Application.Env["KDE_SESSION_VERSION"] = "4" Then
  142         $sDesktop = "KDE4"
  143       Else If Application.Env["KDE_SESSION_VERSION"] = "5" Then
  144         $sDesktop = "KDE5"
  145       Else
  146         $sDesktop = "KDE"
  147       Endif
  148 
  149     Else
  150       
  151       Select Case UCase(Application.Env["XDG_CURRENT_DESKTOP"])
  152         Case "LXDE"
  153           $sDesktop = "LXDE"
  154         Case "UNITY"
  155           $sDesktop = "UNITY"
  156         Default
  157           $sDesktop = UCase(Application.Env["XDG_CURRENT_DESKTOP"])
  158           If $sDesktop Begins "X-" Then $sDesktop = Mid$($sDesktop, 3)
  159       End Select
  160       
  161     Endif
  162     
  163     If Not $sDesktop Then
  164     
  165       If Application.Env["GNOME_DESKTOP_SESSION_ID"] Then
  166         $sDesktop = "GNOME"
  167       Else If Application.Env["MATE_DESKTOP_SESSION_ID"] Or If Application.Env["MATECORBA_SOCKETDIR"] Then
  168         $sDesktop = "MATE"
  169       Else If Application.Env["E_BIN_DIR"] And If Application.Env["E_LIB_DIR"] Then
  170         $sDesktop = "ENLIGHTENMENT"
  171       Else If Application.Env["WMAKER_BIN_NAME"] Then
  172         $sDesktop = "WINDOWMAKER"
  173       Else If Application.Env["DESKTOP_SESSION"] = "LXDE" Then
  174         $sDesktop = "LXDE"
  175       Else If UCase(Env["DESKTOP_SESSION"]) = "XCFE" Then
  176         $sDesktop = "XFCE"
  177       Else If UCase(Env["XDG_MENU_PREFIX"]) Begins "XCFE" Then
  178         $sDesktop = "XFCE"
  179       Else If InStr(Env["XDG_DATA_DIR"], "/xfce") Then
  180         $sDesktop = "XFCE"
  181       Endif
  182       
  183     Endif
  184 
  185   Endif
  186 
  187   Return $sDesktop
  188 
  189 End
  190 
  191 Public Sub GetDesktopPath(Optional sType As String = "DESKTOP") As String
  192 
  193   Dim sDir, sXdgConfigHome, sXdgConfigDirs, sFileConfig As String
  194   
  195   sXdgConfigHome = Application.Env["XDG_CONFIG_HOME"] ' $XDG_CONFIG_HOME définie ?
  196   If Not sXdgConfigHome Then sXdgConfigHome = User.Home & "/.config" ' Sinon val. par défaut
  197   
  198   sXdgConfigDirs = Application.Env["XDG_CONFIG_DIRS"] ' $XDG_CONFIG_DIRS définie ?
  199   If Not sXdgConfigDirs Then sXdgConfigDirs = "/etc/xdg" ' Sinon val. par défaut
  200   
  201   sFileConfig = SearchPathConfig(sXdgConfigHome, sXdgConfigDirs)
  202   If sFileConfig Then
  203     sDir = SearchPathVariable(sFileConfig, UCase(sType)) 
  204   Else 
  205     sDir = System.User.Home &/ "Desktop"
  206   Endif
  207   
  208   Return sDir
  209   
  210 End
  211 
  212 Public Sub GetDataDir() As String[]
  213   
  214   Dim sDir As String
  215   
  216   If Not $aDataDir Then
  217     $aDataDir = [Desktop.DataDir] 'Split(Desktop.DataDir, ":", "", True)
  218     sDir = Env["XDG_DATA_DIRS"]
  219     If Not sDir Then sDir = "/usr/local/share:/usr/share"
  220     $aDataDir.Insert(Split(sDir, ":", "", True))
  221   Endif
  222   
  223   Return $aDataDir
  224   
  225 End
  226 
  227 
  228 Public Sub Main()
  229 
  230   'Dim hMime As DesktopMime
  231   
  232   'DesktopFile.FromMime("application/vnd.oasis.opendocument.text")
  233   ' hMime = DesktopMime.FromFile("/bin/ls")
  234   ' Print hMime.Type
  235   
  236   Print Desktop.Path
  237   
  238 End
  239 
  240 
  241 
  242 ' Public Sub X11_PropertyNotify((Window) As Integer, Atom As Integer)
  243 '   
  244 '   'Debug Window;; X11.GetAtomName(Atom)
  245 '   
  246 '   If Window = X11.RootWindow And If Atom = _NET_CLIENT_LIST Then
  247 '   Endif
  248 '   
  249 ' End
  250 
  251 Public Sub X11_ConfigureNotify((Window) As Integer, X As Integer, Y As Integer, W As Integer, H As Integer)
  252   
  253   Debug Window;; X;; Y;; W;; H
  254   
  255 End
  256 
  257 Public Sub GetDesktopVar(sVar As String) As String[]
  258 
  259   Dim sValue As String
  260   
  261   sValue = Env[sVar]
  262   If sValue Then Return Split(sValue, ":")
  263   
  264   If Not sValue Then
  265     Select Case sVar
  266       Case "XDG_CONFIG_HOME"
  267         Return [User.Home &/ ".config"]
  268       Case "XDG_CONFIG_DIRS"
  269         Return ["/etc/xdg"]
  270       Case "XDG_DATA_HOME"
  271         Return [User.Home &/ ".local/share"]
  272       Case "XDG_DATA_DIRS"
  273         Return ["/usr/local/share", "/usr/share"]
  274     End Select
  275   Endif
  276   
  277 End
  278 
  279 
  280 Public Sub MakeSearchPath(sKey As String, aList As String[]) As String[]
  281 
  282   Dim iPos As Integer
  283   Dim sDir As String
  284   Dim aDir As String[]
  285   Dim sVar As String
  286   Dim aResult As New String[]
  287   
  288   If $cSearchPath.Exist(sKey) Then Return $cSearchPath[sKey]
  289   
  290   aDir = aList.Copy()
  291   
  292   While aDir.Count
  293     
  294     sDir = aDir[0]
  295     aDir.Remove(0)
  296     
  297     If sDir Begins "$" Then
  298       
  299       iPos = InStr(sDir, "/")
  300       If iPos = 0 Then iPos = Len(sDir) + 1
  301       For Each sVar In GetDesktopVar(Mid$(sDir, 2, iPos - 2))
  302         aDir.Add(sVar &/ Mid$(sDir, iPos + 1))
  303       Next
  304       Continue
  305     
  306     Endif
  307     
  308     sDir = Replace(sDir, "$desktop", LCase(GetDesktop()))
  309     If Exist(sDir) Then aResult.Add(sDir)
  310     
  311   Wend
  312   
  313   $cSearchPath[sKey] = aResult
  314   Return aResult
  315   
  316 End
  317 
  318 Public Sub NotSupported()
  319 
  320   If Desktop.Platform Then
  321     Error.Raise("Not supported on " & Desktop.Platform)
  322   Else
  323     Error.Raise("Not desktop detected")
  324   Endif
  325 
  326 End
  327