gb.desktop DesktopFile now supporting Actions

Ask about the individual Gambas components here.
Post Reply
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

gb.desktop DesktopFile now supporting Actions

Post by BruceSteers »

At last gb.desktop DesktopFile now has a way to edit the [Desktop Action <name>] properties of a .desktop file,

https://gitlab.com/gambas/gambas/-/comm ... 5a58c59031

Before you could only read the actions via the DesktopFile.AlternativeActions collection but you could not modify anything

Now there is a DesktopFile.DesktopActions property that allows you to edit/add/remove Actions.

The DesktopFile.DesktopActions property has the following ,,
Properties:
Actions As String[] , returns the list of [Desktop Action <name>] items, (different to DesktopFile.Actions)
Count , returns how many [Desktop Action <name>] items there are
Methods:
Exist(Keyname As String) , check an action exists
Add(Keyname As String, Name, As String, Exec As String)' add an action
Remove(Keyname As String) , delete an action.

Note: DesktopFile.Actions property sets what's in the Actions="" entry. This defines the menu order and what actions are visible.
DesktopFile.DesktopActions.Actions lists the [Desktop Action <name>] names and can be different to DesktopActions.Actions, it can contain more items and their order is irrelevant to the menu displayed.

The DesktopFile.DesktopActions property can be used like a collection to access an action via it's keyname.
Print hDesktopFile.DesktopAtcions["MyAction"].Exec


The DesktopFile.DesktopActions property is also enumerable
  For Each da As DesktopAction In hDesktopFile.DesktopActions
    Print da.Key, da.Name;; da.Exec
  Next


Each DesktopAction has the following..
Properties:
'' Get or set the keyName that will be it's Action name
Property Key As String

'' Get or set the Name (Text) that will display in the menu.
Property Name As String
Note, if your language is fr and Name[fr] property exists then that is used. (this is how DesktopFile.Name property works, I have done the same)

'' Get or set the command to run on selection
Property Exec As String

'' List all Property names for items in the Action including Name and Exec
Property Read Properties As String[]

Methods:
Delete() , remove this Action
Exist(PropertyName As String) As Boolean ' Check if a custom property name exists.

DesktopAction.class has also been set up to handle _unknown properties.
So you can set hDesktopFile.DesktopActions["MyAction"].CustomProperty = "Something"
CustomProperty can be any name and will save as that name in the Action
Ps.
You can also set hDesktopFile.DesktopActions["MyAction"]["CustomProperty"] = "Something"

check out the Main() method of main.module in gb.desktop to see some test code using the new DesktopActions
https://gitlab.com/gambas/gambas/-/blob ... ain.module

I submitted this DesktopActions merge 2 years ago but what i did was a bit bloated as i wanted to also support setting languages.
I cancelled that merge as i think I had over-bloated things and Benoit didn't like the look of it.
https://gitlab.com/gambas/gambas/-/merge_requests/239

I recently re-wrote it taking Benoits suggestions from the previous merge request and to just work exactly the same as DesktopFile.Name when it comes to language related stuff no bells or whistles.

I guess Benoit was happier with this merge request as he has now merged it to dev branch :)

PS. There is a new very useful hidden property for DesktopFile that is hDesktopFile._Data As Collection
hDesktopFile._Data points to the internal collection that DesktopFile.class uses when it loads a .desktop file.
You can read/modify everything with that property and any changes will save when the file is saved.
So if you want to make language setting stuff anything is possible as unlike before we now have access to the internal Collection.

Happy coding :)
If at first you don't succeed , try doing something differently.
BruceS
Post Reply