DesktopFile.FromMime() now follows the freedesktop search path specifications.

[GB.DESKTOP]
* BUG: DesktopFile.FromMime() now follows the freedesktop search path specifications.
This commit is contained in:
gambas 2018-08-13 11:39:09 +02:00
parent ffa3cd0875
commit bc2c0490cf
4 changed files with 142 additions and 71 deletions

View file

@ -2,8 +2,9 @@
Count=0
[DebugWindow]
Count=1
DebugWindow[1]="aMInfo"
Count=2
DebugWindow[1]="aDir"
DebugWindow[2]="aResult"
[DebugWindow/$aDataDir]
Geometry=[848,380,224,280]
@ -109,6 +110,9 @@ Geometry=[1285,145,224,280]
[DebugWindow/aCmd]
Geometry=[772,380,376,280]
[DebugWindow/aDir]
Geometry=[988,259,536,631]
[DebugWindow/aMInfo]
Geometry=[1422,141,256,320]
@ -118,6 +122,9 @@ Geometry=[1159,276,256,320]
[DebugWindow/aMInfo[1]]
Geometry=[1192,617,256,320]
[DebugWindow/aResult]
Geometry=[1599,277,256,320]
[DebugWindow/hInfo]
Geometry=[832,362,256,320]

View file

@ -22,7 +22,6 @@ Static Property Read RuntimeDir As String
Static Property Read NetworkAvailable As Boolean
Static Property Read RemoteDisplay As Boolean
Static Private $sDesktop As String
Static Private $cDesktop As Collection
Static Private $cExt As Collection
Static Private $aImgExt As String[]
@ -189,62 +188,6 @@ Catch
End
Static Private Sub GetDesktop() As String
If Not $sDesktop Then
If Application.Env["KDE_FULL_SESSION"] Then
If Application.Env["KDE_SESSION_VERSION"] = "4" Then
$sDesktop = "KDE4"
Else If Application.Env["KDE_SESSION_VERSION"] = "5" Then
$sDesktop = "KDE5"
Else
$sDesktop = "KDE"
Endif
Else
Select Case UCase(Application.Env["XDG_CURRENT_DESKTOP"])
Case "LXDE"
$sDesktop = "LXDE"
Case "UNITY"
$sDesktop = "UNITY"
Default
$sDesktop = UCase(Application.Env["XDG_CURRENT_DESKTOP"])
If $sDesktop Begins "X-" Then $sDesktop = Mid$($sDesktop, 3)
End Select
Endif
If Not $sDesktop Then
If Application.Env["GNOME_DESKTOP_SESSION_ID"] Then
$sDesktop = "GNOME"
Else If Application.Env["MATE_DESKTOP_SESSION_ID"] Or If Application.Env["MATECORBA_SOCKETDIR"] Then
$sDesktop = "MATE"
Else If Application.Env["E_BIN_DIR"] And If Application.Env["E_LIB_DIR"] Then
$sDesktop = "ENLIGHTENMENT"
Else If Application.Env["WMAKER_BIN_NAME"] Then
$sDesktop = "WINDOWMAKER"
Else If Application.Env["DESKTOP_SESSION"] = "LXDE" Then
$sDesktop = "LXDE"
Else If UCase(Env["DESKTOP_SESSION"]) = "XCFE" Then
$sDesktop = "XFCE"
Else If UCase(Env["XDG_MENU_PREFIX"]) Begins "XCFE" Then
$sDesktop = "XFCE"
Else If InStr(Env["XDG_DATA_DIR"], "/xfce") Then
$sDesktop = "XFCE"
Endif
Endif
Endif
Return $sDesktop
End
Static Public Sub OpenTerminal(Optional {Dir} As String) ', Optional Command As String)
Dim sExec As String
@ -255,7 +198,7 @@ Static Public Sub OpenTerminal(Optional {Dir} As String) ', Optional Command As
Dim sProg As String
Dim sDesktop As String
sDesktop = GetDesktop()
sDesktop = Main.GetDesktop()
Select Case sDesktop
@ -444,7 +387,7 @@ Static Public Sub RunAsRoot(Command As String)
Dim sExec As String
Select Case GetDesktop()
Select Case Main.GetDesktop()
Case "KDE", "KDE4", "KDE5"
sExec = "kdesudo"

View file

@ -475,8 +475,9 @@ Static Public Sub FromMime(MimeType As String) As DesktopFile[]
Dim sList As String
Dim aMime As String[]
Dim sMime As String
Dim sDir As String
Dim sFound As String
Dim aPath As String[]
Dim sPath As String
hProgList = $cMimeCache[MimeType]
@ -491,15 +492,20 @@ Static Public Sub FromMime(MimeType As String) As DesktopFile[]
sList = ""
'Print "["; sMime; "]"
For Each sDir In Main.GetDataDir()
sFound = FindInMimeInfoCache(sDir &/ "applications/mimeapps.list", sMime)
aPath = Main.MakeSearchPath("mimeapps.list", ["$XDG_CONFIG_HOME/$desktop-mimeapps.list", "$XDG_CONFIG_HOME/mimeapps.list", "$XDG_CONFIG_DIRS/$desktop-mimeapps.list", "$XDG_CONFIG_DIRS/mimeapps.list", "$XDG_DATA_DIRS/applications/$desktop-mimeapps.list", "$XDG_DATA_DIRS/applications/mimeapps.list"])
For Each sPath In aPath
sFound = FindInMimeInfoCache(sPath, sMime)
If sFound Then sList &= ";" & sFound
Next
If Not sList Then
For Each sDir In Main.GetDataDir()
sFound = FindInMimeInfoCache(sDir &/ "applications/mimeinfo.cache", sMime)
aPath = Main.MakeSearchPath("mimeinfo.cache", ["$XDG_CONFIG_HOME/mimeinfo.cache", "$XDG_CONFIG_DIRS/mimeinfo.cache", "$XDG_DATA_DIRS/applications/mimeinfo.cache"])
For Each sPath In aPath
sFound = FindInMimeInfoCache(sPath, sMime)
If sFound Then sList &= ";" & sFound
Next
Endif

View file

@ -9,6 +9,8 @@ Private $cTool As New Collection
'Private _NET_CLIENT_LIST As Integer
Private $aDataDir As String[]
Private $sDesktop As String
Private $cSearchPath As New Collection
Public Sub RunXdgUtil(sProg As String, aArg As String[], Optional bWait As Boolean)
@ -120,6 +122,62 @@ Private Function SearchPathVariable(sFileConfig As String, sDirectory As String)
End
Public Sub GetDesktop() As String
If Not $sDesktop Then
If Application.Env["KDE_FULL_SESSION"] Then
If Application.Env["KDE_SESSION_VERSION"] = "4" Then
$sDesktop = "KDE4"
Else If Application.Env["KDE_SESSION_VERSION"] = "5" Then
$sDesktop = "KDE5"
Else
$sDesktop = "KDE"
Endif
Else
Select Case UCase(Application.Env["XDG_CURRENT_DESKTOP"])
Case "LXDE"
$sDesktop = "LXDE"
Case "UNITY"
$sDesktop = "UNITY"
Default
$sDesktop = UCase(Application.Env["XDG_CURRENT_DESKTOP"])
If $sDesktop Begins "X-" Then $sDesktop = Mid$($sDesktop, 3)
End Select
Endif
If Not $sDesktop Then
If Application.Env["GNOME_DESKTOP_SESSION_ID"] Then
$sDesktop = "GNOME"
Else If Application.Env["MATE_DESKTOP_SESSION_ID"] Or If Application.Env["MATECORBA_SOCKETDIR"] Then
$sDesktop = "MATE"
Else If Application.Env["E_BIN_DIR"] And If Application.Env["E_LIB_DIR"] Then
$sDesktop = "ENLIGHTENMENT"
Else If Application.Env["WMAKER_BIN_NAME"] Then
$sDesktop = "WINDOWMAKER"
Else If Application.Env["DESKTOP_SESSION"] = "LXDE" Then
$sDesktop = "LXDE"
Else If UCase(Env["DESKTOP_SESSION"]) = "XCFE" Then
$sDesktop = "XFCE"
Else If UCase(Env["XDG_MENU_PREFIX"]) Begins "XCFE" Then
$sDesktop = "XFCE"
Else If InStr(Env["XDG_DATA_DIR"], "/xfce") Then
$sDesktop = "XFCE"
Endif
Endif
Endif
Return $sDesktop
End
Public Sub GetDesktopPath(Optional sType As String = "DESKTOP") As String
Dim sDir, sXdgConfigHome, sXdgConfigDirs, sFileConfig As String
@ -159,10 +217,7 @@ End
Public Sub Main()
Dim hInfo As DesktopMime
hInfo = DesktopMime.FromFile("/bin/ls")
Print hInfo.Pattern
DesktopFile.FromMime("application/vnd.oasis.opendocument.text")
End
@ -183,3 +238,63 @@ Public Sub X11_ConfigureNotify((Window) As Integer, X As Integer, Y As Integer,
End
Public Sub GetDesktopVar(sVar As String) As String[]
Dim sValue As String
sValue = Env[sVar]
If sValue Then Return Split(sValue, ":")
If Not sValue Then
Select Case sVar
Case "XDG_CONFIG_HOME"
Return [User.Home &/ ".config"]
Case "XDG_CONFIG_DIRS"
Return ["/etc/xdg"]
Case "XDG_DATA_HOME"
Return [User.Home &/ ".local/share"]
Case "XDG_DATA_DIRS"
Return ["/usr/local/share", "/usr/share"]
End Select
Endif
End
Public Sub MakeSearchPath(sKey As String, aList As String[]) As String[]
Dim iPos As Integer
Dim sDir As String
Dim aDir As String[]
Dim sVar As String
Dim aResult As New String[]
If $cSearchPath.Exist(sKey) Then Return $cSearchPath[sKey]
aDir = aList.Copy()
While aDir.Count
sDir = aDir[0]
aDir.Remove(0)
If sDir Begins "$" Then
iPos = InStr(sDir, "/")
If iPos = 0 Then iPos = Len(sDir) + 1
For Each sVar In GetDesktopVar(Mid$(sDir, 2, iPos - 2))
aDir.Add(sVar &/ Mid$(sDir, iPos + 1))
Next
Continue
Endif
sDir = Replace(sDir, "$desktop", LCase(GetDesktop()))
If Exist(sDir) Then aResult.Add(sDir)
Wend
$cSearchPath[sKey] = aResult
Return aResult
End