views:

424

answers:

5

How can I, in Java or using some other programming language, add a new program group in the applications menu in both KDE and Gnome?

I am testing with Ubuntu and Kubuntu 8. Putting a simple .menu file in ~/.config/menus/applications-merged worked in Kubuntu, but the same procedure does nothing in Ubuntu.

The content of my file is as follows:

<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" "http://www.freedesktop.org/standards/menu-spec/1.0/menu.dtd"&gt;
<Menu>
<Menu>
  <Name>My Program Group</Name>
  <Include>
   <Filename>shortcut.desktop</Filename>
  </Include>
</Menu>
</Menu>

Note that the .desktop file is correctly placed in ~/.local/share/applications.

Ps: The original question did not specify I wanted a solution in a programmatic way.

A: 

In Gnome use System -> Settings -> Menu then just choose New Menu or New Entry.

fly.floh
I want to do that from inside a program, not manually.
Morgaelyn
+1  A: 

Maybe xdg-desktop-menu does that? See man xdg-desktop-menu or http://manpages.ubuntu.com/manpages/hardy/en/man1/xdg-desktop-menu.html .

oliver
Rather than copying myself these files, it is best to use xdg-desktop-menu to do the work.
Morgaelyn
+1  A: 

Thanks, oliver. I used xdg-desktop-menu and then analyzed its output. The correct menu file needs to explicitly name the outer menu (Applications), as follows:

<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" 
    "http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd"&gt;
<Menu>
    <Name>Applications</Name>
<Menu>
    <Name>My Program Group</Name>
    <Include>
        <Filename>shortcut.desktop</Filename>
    </Include>
</Menu>
</Menu>

This worked fine in Kubuntu, Ubuntu and Fedora Core 9. Couldn't make it work on openSUSE, though.

Morgaelyn
+1  A: 

I recommend you look into freedesktop.org standards that cover this. Up to date list is available here:

http://www.freedesktop.org/wiki/Specifications/menu-spec

The latest one is currently 1.0:

http://standards.freedesktop.org/menu-spec/1.0/

FreeDesktop.org standards are followed by Gnome, KDE and XFCE, so it should work on any distribution.

Milan Babuškov
I've taken a look at the standard, but each desktop seems to implement the standard slightly differently. For example, I found out that in openSUSE the .directory file is mandatory or else the program group does not shows up, and in the other distributions it is not needed.
Morgaelyn
A: 

Not sure what you meant exactly with "in openSUSE the .directory file is mandatory or else the program group does not shows up"; generally I suppose you have to call xdg-desktop-menu twice (once for the program group and once for the program itself), and so you have to supply two different .directory files as well. If the program group is empty, it makes sense that the desktop hides it.

(But maybe I completely misunderstood you here :-) and I've never used xdg-desktop-menu myself anyway).

oliver
I didn't want to use xdg-desktop-menu. I was trying to create the files in the correct folders myself and in openSUSE you have to have the directory file, but in Ubuntu and Kubuntu it isn't missed.But now I think using xdg-desktop-menu directly is the best solution.
Morgaelyn