[Gambas-user] Get the default file manager

System64 Development 64xcode at gmail.com
Sun Feb 18 22:18:08 CET 2024


El 18/2/24 a las 16:39, Benoît Minisini escribió:
> If you read the 'xdg-mime' source code, you will notice that it 
> implements three methods for defining the default application of 
> specific mimetype: one for KDE, one for LXQT, and one for the other 
> desktops.

I rewrite the function to obtain te apps by 2 filters.

Note: The part to load the mimeapps.list is not tested in desktops 
different to XFCE, perhaps we can made some survey to get some info 
about paths in diferents distros/desktops

Calling the function

o = System.DeskApp2(["inode/directory"], ["FileManager"])


'' This function list all the .desktop applications in the directory 
/usr/share/applications which are asociated to al the mimetypes in the 
frirst argument and optionally is possible filter by category.

Static Public Function DeskApp2(aMimeType As String[], Optional 
aCategories As String[]) As DesktopFile[]

   Dim oTemp As DesktopFile[]
   Dim oDsk As New DesktopFile[]
   Dim oDsk2 As New DesktopFile[]
   Dim dsk As DesktopFile
   Dim sDef As String
   Dim ad As New String[]
   Dim sMime As String
   Dim sApps As String
   Dim xapp As DesktopFile
   Dim mk As String
   Dim sKat As String
   Dim sl As String
   Dim sApp As String

   If aMimeType.Count Then
     For Each mk In aMimeType
       oTemp = DesktopFile.FromMime(mk)
       For Each dsk In oTemp
         If aCategories.Count Then
           For Each sKat In aCategories
             If dsk.Categories.Exist(sKat) Then
               oDsk.Add(dsk)
               Break
             Endif
           Next
         Else
         Endif
       Next
     Next
   Endif

   Select Desktop.Type
     Case "KDE"
       sDef = User.Home &/ ".config/kde-mimeapps.list"
     Case "LXQT"
       sDef = User.Home &/ ".config/lxqt-mimeapps.list"

     Case "GNOME", "MATE", "ENLIGHTENMENT", "WINDOWMAKER", "XFCE"
       sDef = User.Home &/ ".config/mimeapps.list"

   End Select

   If Exist(sDef) Then
     Select Stat(sDef).Type
       Case gb.File, gb.Link
         ad = Split(File.Load(sDef), "\n")
         If ad.Count > 0 Then
           For Each sl In ad
             If InStr(sl, "=") > 0 Then

               sMime = Split(sl, "=")[0]
               sApps = Split(sl, "=")[1]

               If InStr(sApps, ";") Then
                 For Each sApp In Split(sApps, ";")
                   If sApp <> "" Then
                     For Each xapp In oDsk
                       If File.Name(xapp.Path) = sApp Then
                         oDsk2.Add(xapp)
                       Endif
                     Next
                   Endif
                 Next
               Endif
             Endif
           Next
         Endif
     End Select
     For Each xapp In oDsk
       If Not oDsk2.Exist(xapp) Then
         oDsk2.Add(xapp)
       Endif
     Next

   Else
     oDsk2 = oDsk
   Endif

   Return oDsk2

End

Best regards

Martin.



More information about the User mailing list