r4567: Get ATK headers through Zero Install.
[rox-filer/translations.git] / ROX-Filer / src / appmenu.c
blob1c749641e38221903d387e4bfa05ffbaa531db21
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* appmenu.c - handles application-specific menus read from XMLwrapper.xml */
22 /* XXX: This handles all File menu extensions. Needs renaming! */
24 #include "config.h"
26 #include <gtk/gtk.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <ctype.h>
32 #include <libxml/parser.h>
34 #include "global.h"
36 #include "choices.h"
37 #include "fscache.h"
38 #include "gui_support.h"
39 #include "support.h"
40 #include "menu.h"
41 #include "filer.h"
42 #include "appmenu.h"
43 #include "dir.h"
44 #include "type.h"
45 #include "appinfo.h"
46 #include "xml.h"
47 #include "run.h"
48 #include "diritem.h"
49 #include "action.h"
50 #include "options.h"
52 /* Static prototypes */
53 static void apprun_menu(GtkWidget *item, gpointer data);
54 static GtkWidget *create_menu_item(xmlNode *node);
55 static void show_app_help(GtkWidget *item, gpointer data);
56 static void build_app_menu(const char *app_dir, DirItem *app_item);
57 static void mnt_eject(GtkWidget *item, gpointer data);
59 /* There can only be one menu open at a time... we store: */
60 static GtkWidget *current_menu = NULL; /* The GtkMenu */
61 static guchar *current_app_path = NULL; /* The path of the application */
62 static GList *current_items = NULL; /* The GtkMenuItems we added directly
63 * to it --- not submenu items.
65 /****************************************************************
66 * EXTERNAL INTERFACE *
67 ****************************************************************/
69 /* Removes all appmenu menu items */
70 void appmenu_remove(void)
72 GList *next;
74 if (!current_menu)
75 return;
77 for (next = current_items; next; next = next->next)
78 gtk_widget_destroy((GtkWidget *) next->data);
80 null_g_free(&current_app_path);
81 current_menu = NULL;
83 g_list_free(current_items);
84 current_items = NULL;
87 /* Add AppMenu entries to 'menu', if appropriate.
88 * This function modifies the menu stored in "menu".
89 * 'app_dir' is the pathname of the application directory, and 'item'
90 * is the corresponding DirItem.
91 * Returns number of entries added.
92 * Call appmenu_remove() to undo the effect.
94 int appmenu_add(const gchar *app_dir, DirItem *app_item, GtkWidget *menu)
96 GList *next;
97 GtkWidget *sep;
98 int nadded = 0;
100 g_return_val_if_fail(menu != NULL, 0);
102 /* Should have called appmenu_remove() already... */
103 g_return_val_if_fail(current_menu == NULL, 0);
104 g_return_val_if_fail(current_items == NULL, 0);
106 build_app_menu(app_dir, app_item);
108 if (app_item->flags & ITEM_FLAG_MOUNT_POINT)
110 GtkWidget *item;
111 item = gtk_menu_item_new_with_label(_("Eject"));
112 gtk_widget_show(item);
113 current_items = g_list_prepend(current_items, item);
114 g_signal_connect(item, "activate", G_CALLBACK(mnt_eject), NULL);
117 if (current_items)
119 sep = gtk_menu_item_new();
120 current_items = g_list_prepend(current_items, sep);
121 gtk_widget_show(sep);
124 for (next = current_items; next; next = next->next)
126 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu),
127 GTK_WIDGET(next->data));
128 nadded++;
131 current_menu = menu;
132 current_app_path = g_strdup(app_dir);
134 return nadded;
138 /****************************************************************
139 * INTERNAL FUNCTIONS *
140 ****************************************************************/
142 /* Create a new menu and return it */
143 static GtkWidget *appmenu_add_submenu(xmlNode *subm_node)
145 xmlNode *node;
146 GtkWidget *sub_menu;
148 /* Create the new submenu */
149 sub_menu = gtk_menu_new();
151 /* Add the menu entries */
152 for (node = subm_node->xmlChildrenNode; node; node = node->next)
154 GtkWidget *item;
156 item = create_menu_item(node);
157 if (item)
158 gtk_menu_shell_append(GTK_MENU_SHELL(sub_menu), item);
161 return sub_menu;
164 /* Create and return a menu item */
165 static GtkWidget *create_menu_item(xmlNode *node)
167 GtkWidget *item;
168 xmlNode *label_node;
169 guchar *label, *option = NULL;
170 gboolean is_submenu;
172 if (node->type != XML_ELEMENT_NODE)
173 return NULL;
175 if (strcmp(node->name, "Item") == 0)
177 is_submenu = FALSE;
178 option = xmlGetProp(node, "option");
180 else if (strcmp(node->name, "AppMenu") == 0)
181 is_submenu = TRUE;
182 else
183 return NULL;
185 /* Create the item */
186 label_node = get_subnode(node, NULL, "Label");
187 if (label_node)
189 label = xmlNodeListGetString(label_node->doc,
190 label_node->xmlChildrenNode, 1);
192 else
194 label = xmlGetProp(node, "label");
195 if (!label)
196 label = g_strdup(_("<missing label>"));
198 item = gtk_menu_item_new_with_label(label);
200 gtk_widget_set_accel_path(item, NULL, NULL); /* XXX */
202 g_free(label);
204 if (is_submenu)
206 /* Add submenu items */
208 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
209 appmenu_add_submenu(node));
211 else
213 /* Set up callback */
215 if (option)
217 g_object_set_data_full(G_OBJECT(item), "option",
218 g_strdup(option),
219 g_free);
220 g_free(option);
223 g_signal_connect(item, "activate", G_CALLBACK(apprun_menu),
224 NULL);
227 gtk_widget_show(item);
229 return item;
232 /* Send to current_app_path (though not actually an app) */
233 static void send_to(GtkWidget *item, const char *app)
235 GList *file_list;
237 g_return_if_fail(current_app_path != NULL);
239 file_list = g_list_prepend(NULL, current_app_path);
240 run_with_files(app, file_list);
241 g_list_free(file_list);
244 /* Function called to execute an AppMenu item */
245 static void apprun_menu(GtkWidget *item, gpointer data)
247 guchar *option;
248 gchar *argv[3];
250 g_return_if_fail(current_app_path != NULL);
252 option = g_object_get_data(G_OBJECT(item), "option");
254 argv[0] = g_strconcat(current_app_path, "/AppRun", NULL);
255 argv[1] = option; /* (may be NULL) */
256 argv[2] = NULL;
258 rox_spawn(NULL, (const gchar **) argv);
260 g_free(argv[0]);
263 static void show_app_help(GtkWidget *item, gpointer data)
265 g_return_if_fail(current_app_path != NULL);
267 show_help_files(current_app_path);
270 static void mnt_eject(GtkWidget *item, gpointer data)
272 GList *dirs;
274 g_return_if_fail(current_app_path != NULL);
275 dirs = g_list_prepend(NULL, current_app_path);
276 action_eject(dirs);
277 g_list_free(dirs);
280 static void customise_type(GtkWidget *item, MIME_type *type)
282 char *leaf;
283 char *path;
285 leaf = g_strconcat(".", type->media_type, "_", type->subtype, NULL);
286 path = choices_find_xdg_path_save(leaf, "SendTo", SITE, TRUE);
287 g_free(leaf);
289 mkdir(path, 0755);
290 filer_opendir(path, NULL, NULL);
291 g_free(path);
293 info_message(_("Symlink any programs you want into this directory. "
294 "They will appear in the menu for all items of this "
295 "type (%s/%s)."), type->media_type, type->subtype);
298 static void build_menu_for_type(MIME_type *type)
300 GPtrArray *names;
301 char *path;
302 int i;
303 char *leaf;
304 GtkWidget *item;
305 DirItem *ditem;
307 leaf = g_strconcat(".", type->media_type, "_", type->subtype, NULL);
308 path = choices_find_xdg_path_load(leaf, "SendTo", SITE);
310 if (!path)
311 goto out;
313 names = list_dir(path);
315 ditem = diritem_new("");
317 for (i = 0; i < names->len; i++)
319 char *leaf = names->pdata[i];
320 char *full_path;
322 full_path = g_build_filename(path, leaf, NULL);
323 diritem_restat(full_path, ditem, NULL);
325 item = make_send_to_item(ditem, leaf, MIS_SMALL);
326 current_items = g_list_prepend(current_items, item);
327 gtk_widget_show(item);
328 g_signal_connect_data(item, "activate", G_CALLBACK(send_to),
329 full_path, (GClosureNotify) g_free, 0);
332 g_ptr_array_free(names, TRUE);
334 g_free(path);
336 out:
337 item = gtk_menu_item_new_with_label(_("Customise Menu..."));
338 current_items = g_list_prepend(current_items, item);
339 g_signal_connect(item, "activate", G_CALLBACK(customise_type), type);
340 gtk_widget_show(item);
342 g_free(leaf);
345 static inline gboolean is_dir(const char *dir)
347 struct stat info;
348 return stat(dir, &info) == 0 && S_ISDIR(info.st_mode);
351 /* Adds to current_items */
352 static void build_app_menu(const char *app_dir, DirItem *app_item)
354 XMLwrapper *ai = NULL;
355 xmlNode *node;
356 GtkWidget *item;
357 char *help_dir;
359 ai = appinfo_get(app_dir, app_item);
360 if (ai)
362 node = xml_get_section(ai, NULL, "AppMenu");
363 if (node)
364 node = node->xmlChildrenNode;
366 else
368 if (app_item->flags & ITEM_FLAG_APPDIR)
369 node = NULL;
370 else
372 /* Not an application AND no AppInfo */
373 build_menu_for_type(app_item->mime_type);
374 return;
378 /* Add the menu entries */
379 for (; node; node = node->next)
381 item = create_menu_item(node);
383 if (item)
384 current_items = g_list_prepend(current_items, item);
387 help_dir = g_build_filename(app_dir, "Help", NULL);
388 if (is_dir(help_dir))
390 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
391 gtk_widget_show(item);
392 current_items = g_list_prepend(current_items, item);
393 g_signal_connect(item, "activate", G_CALLBACK(show_app_help), NULL);
394 gtk_label_set_text(GTK_LABEL(GTK_BIN(item)->child), _("Help"));
396 g_free(help_dir);
398 if (ai)
399 g_object_unref(ai);