[Gambas-user] Get the default file manager

Bruce Steers bsteers4 at gmail.com
Sun Feb 18 19:18:55 CET 2024


On Sun, 18 Feb 2024 at 15:44, Benoît Minisini <
benoit.minisini at gambas-basic.org> wrote:

> Le 18/02/2024 à 13:28, Bruce Steers a écrit :
> >
> > In the attached gb.desktop DesktopFile.class I have added a DefaultOnly
> > option to DesktopFile.FromMime(Type As String, Optional DefaultOnly As
> > Boolean)
> > And the same option to the Private  FindInMimeInfoCache function
> >
> > If you make DefaultOnly True it only returns the "Default Applications"
> > found and skips all the "Associated Applications" by first moving the
> > position to the [Default Applications] slot
> >
> > So i can now simply use...
> >
> > DesktopFile.FromMime("inode/directory", True)[0].Run(User.home)
> >
> > Seems to work okay I only get one result and yes its caja :)
> >
> > Maybe Benoit could add something similar to only return the Default
> > application not all associated apps?
> >
> > To make this work Martin just remove gb.desktop from your program
> > components and add the attached folder to your .src folder
> >
> > Respects
> > BruceS
> >
>
> Instead of adding a boolean argument, can't you just make the default
> application always the first application of the returned array?
>
> --
> Benoît Minisini.
>

Yes i think so.
I removed last changes and re did it..

It first looks for the mime in [Default Applications] slot and notes the
result
My initial way was flawed in a few ways, i could get the default but it
would omit others..
I worked around it like this
first seek the default like i did.
Then If the default only has one item (sometimes it returns many) then it
is found in the list and moved to the top
So i kept the DefaultOnly argument for the private FindInMimeInfoCache
method but removed it from the public FromMime

       For Each sPath In aPath
        sDefault = FindInMimeInfoCache(sPath, sMime, True)
        sFound = FindInMimeInfoCache(sPath, sMime)
        If sFound Then
          aList.Insert(Split(sFound, ";", Null, True))
          ' move default to top of list
          If Not InStr(sDefault, ";") Then
           If aList.Exist(sDefault) Then
aList.Add(aList.Extract(aList.Find(sDefault), 1)[0], 0) Else
aList.Add(sDefault, 0)
           Endif
        Endif
      Next

Results from the last commit ...

Print DesktopFile.FromMime("inode/directory")[0].Path
Print DesktopFile.FromMime("text/plain")[0].Path
Print DesktopFile.FromMime("text/html")[0].Path
Print
Dim aDF As DesktopFile[] = DesktopFile.FromMime("inode/directory")
For Each hDF As DesktopFile In aDF
  Print hDF.Path
Next

Gives the following results...

/usr/share/applications/caja-folder-handler.desktop
/home/bonus/.local/share/applications/userapp-scripted-G2CT70.desktop
/usr/share/applications/firefox.desktop

/home/bonus/.local/share/applications/userapp-scripted-G2CT70.desktop
/usr/share/applications/pluma.desktop
/usr/share/applications/emacs.desktop
/usr/share/applications/featherpad.desktop
/home/bonus/.local/share/applications/jedit.desktop
/usr/share/applications/libreoffice-writer.desktop
/usr/share/applications/okularApplication_txt.desktop
/usr/share/applications/org.gnome.gedit.desktop
/usr/share/applications/org.kde.kate.desktop
/usr/share/applications/org.kde.kwrite.desktop
/usr/share/applications/org.xfce.mousepad.desktop

And that looks right to me.

Note: i also needed to remove the caching...
$cMimeCache[sMime] = hProgList
as it gets cached to a Collection then the order of items gets jumbled!
So it would work the first time then the second time be wrong :(

Ps. I also fixed a bug where DesktopMime.GetApplications is still using
FindMime so reports a depreciated error
Pps. probably it's not the best solution and you have a better / simpler
way?

Respects
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20240218/a54c072c/attachment-0001.htm>


More information about the User mailing list