r3247: Experimental extended attributes support for setting MIME types
[rox-filer.git] / ROX-Filer / src / appmenu.c
blobd83cfc471b826ff52524179367b79b1de5e805b1
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* appmenu.c - handles application-specific menus read from XMLwrapper.xml */
24 /* XXX: This handles all File menu extensions. Needs renaming! */
26 #include "config.h"
28 #include <gtk/gtk.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
34 #include <libxml/parser.h>
36 #include "global.h"
38 #include "choices.h"
39 #include "fscache.h"
40 #include "gui_support.h"
41 #include "support.h"
42 #include "menu.h"
43 #include "filer.h"
44 #include "appmenu.h"
45 #include "dir.h"
46 #include "type.h"
47 #include "appinfo.h"
48 #include "xml.h"
49 #include "run.h"
50 #include "diritem.h"
51 #include "action.h"
52 #include "options.h"
54 /* Static prototypes */
55 static void apprun_menu(GtkWidget *item, gpointer data);
56 static GtkWidget *create_menu_item(xmlNode *node);
57 static void show_app_help(GtkWidget *item, gpointer data);
58 static void build_app_menu(const char *app_dir, DirItem *app_item);
59 static void mnt_eject(GtkWidget *item, gpointer data);
61 /* There can only be one menu open at a time... we store: */
62 static GtkWidget *current_menu = NULL; /* The GtkMenu */
63 static guchar *current_app_path = NULL; /* The path of the application */
64 static GList *current_items = NULL; /* The GtkMenuItems we added directly
65 * to it --- not submenu items.
67 /****************************************************************
68 * EXTERNAL INTERFACE *
69 ****************************************************************/
71 /* Removes all appmenu menu items */
72 void appmenu_remove(void)
74 GList *next;
76 if (!current_menu)
77 return;
79 for (next = current_items; next; next = next->next)
80 gtk_widget_destroy((GtkWidget *) next->data);
82 null_g_free(&current_app_path);
83 current_menu = NULL;
85 g_list_free(current_items);
86 current_items = NULL;
89 /* Add AppMenu entries to 'menu', if appropriate.
90 * This function modifies the menu stored in "menu".
91 * 'app_dir' is the pathname of the application directory, and 'item'
92 * is the corresponding DirItem.
93 * Call appmenu_remove() to undo the effect.
95 void appmenu_add(const gchar *app_dir, DirItem *app_item, GtkWidget *menu)
97 GList *next;
98 GtkWidget *sep;
100 g_return_if_fail(menu != NULL);
102 /* Should have called appmenu_remove() already... */
103 g_return_if_fail(current_menu == NULL);
104 g_return_if_fail(current_items == NULL);
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)
125 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu),
126 GTK_WIDGET(next->data));
128 current_menu = menu;
129 current_app_path = g_strdup(app_dir);
133 /****************************************************************
134 * INTERNAL FUNCTIONS *
135 ****************************************************************/
137 /* Create a new menu and return it */
138 static GtkWidget *appmenu_add_submenu(xmlNode *subm_node)
140 xmlNode *node;
141 GtkWidget *sub_menu;
143 /* Create the new submenu */
144 sub_menu = gtk_menu_new();
146 /* Add the menu entries */
147 for (node = subm_node->xmlChildrenNode; node; node = node->next)
149 GtkWidget *item;
151 item = create_menu_item(node);
152 if (item)
153 gtk_menu_shell_append(GTK_MENU_SHELL(sub_menu), item);
156 return sub_menu;
159 /* Create and return a menu item */
160 static GtkWidget *create_menu_item(xmlNode *node)
162 GtkWidget *item;
163 xmlNode *label_node;
164 guchar *label, *option = NULL;
165 gboolean is_submenu;
167 if (node->type != XML_ELEMENT_NODE)
168 return NULL;
170 if (strcmp(node->name, "Item") == 0)
172 is_submenu = FALSE;
173 option = xmlGetProp(node, "option");
175 else if (strcmp(node->name, "AppMenu") == 0)
176 is_submenu = TRUE;
177 else
178 return NULL;
180 /* Create the item */
181 label_node = get_subnode(node, NULL, "Label");
182 if (label_node)
184 label = xmlNodeListGetString(label_node->doc,
185 label_node->xmlChildrenNode, 1);
187 else
189 label = xmlGetProp(node, "label");
190 if (!label)
191 label = g_strdup(_("<missing label>"));
193 item = gtk_menu_item_new_with_label(label);
195 gtk_widget_set_accel_path(item, NULL, NULL); /* XXX */
197 g_free(label);
199 if (is_submenu)
201 /* Add submenu items */
203 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
204 appmenu_add_submenu(node));
206 else
208 /* Set up callback */
210 if (option)
212 g_object_set_data_full(G_OBJECT(item), "option",
213 g_strdup(option),
214 g_free);
215 g_free(option);
218 g_signal_connect(item, "activate", G_CALLBACK(apprun_menu),
219 NULL);
222 gtk_widget_show(item);
224 return item;
227 /* Send to current_app_path (though not actually an app) */
228 static void send_to(GtkWidget *item, const char *app)
230 GList *file_list;
232 g_return_if_fail(current_app_path != NULL);
234 file_list = g_list_prepend(NULL, current_app_path);
235 run_with_files(app, file_list);
236 g_list_free(file_list);
239 /* Function called to execute an AppMenu item */
240 static void apprun_menu(GtkWidget *item, gpointer data)
242 guchar *option;
243 gchar *argv[3];
245 g_return_if_fail(current_app_path != NULL);
247 option = g_object_get_data(G_OBJECT(item), "option");
249 argv[0] = g_strconcat(current_app_path, "/AppRun", NULL);
250 argv[1] = option; /* (may be NULL) */
251 argv[2] = NULL;
253 rox_spawn(NULL, (const gchar **) argv);
255 g_free(argv[0]);
258 static void show_app_help(GtkWidget *item, gpointer data)
260 g_return_if_fail(current_app_path != NULL);
262 show_help_files(current_app_path);
265 static void mnt_eject(GtkWidget *item, gpointer data)
267 GList *dirs;
269 g_return_if_fail(current_app_path != NULL);
270 dirs = g_list_prepend(NULL, current_app_path);
271 action_eject(dirs);
272 g_list_free(dirs);
275 static void customise_type(GtkWidget *item, MIME_type *type)
277 char *leaf;
278 char *path;
280 leaf = g_strconcat(".", type->media_type, "_", type->subtype, NULL);
281 path = choices_find_path_save(leaf, "SendTo", TRUE);
282 g_free(leaf);
284 mkdir(path, 0755);
285 filer_opendir(path, NULL, NULL);
286 g_free(path);
288 info_message(_("Symlink any programs you want into this directory. "
289 "They will appear in the menu for all items of this "
290 "type (%s/%s)."), type->media_type, type->subtype);
293 static void build_menu_for_type(MIME_type *type)
295 GPtrArray *names;
296 char *path;
297 int i;
298 char *leaf;
299 GtkWidget *item;
300 DirItem *ditem;
302 leaf = g_strconcat(".", type->media_type, "_", type->subtype, NULL);
303 path = choices_find_path_load(leaf, "SendTo");
305 if (!path)
306 goto out;
308 names = list_dir(path);
310 ditem = diritem_new("");
312 for (i = 0; i < names->len; i++)
314 char *leaf = names->pdata[i];
315 char *full_path;
317 full_path = g_build_filename(path, leaf, NULL);
318 diritem_restat(full_path, ditem, NULL);
320 item = make_send_to_item(ditem, leaf, MIS_SMALL);
321 current_items = g_list_prepend(current_items, item);
322 gtk_widget_show(item);
323 g_signal_connect_data(item, "activate", G_CALLBACK(send_to),
324 full_path, (GClosureNotify) g_free, 0);
327 g_ptr_array_free(names, TRUE);
329 g_free(path);
331 out:
332 item = gtk_menu_item_new_with_label(_("Customise Menu..."));
333 current_items = g_list_prepend(current_items, item);
334 g_signal_connect(item, "activate", G_CALLBACK(customise_type), type);
335 gtk_widget_show(item);
337 g_free(leaf);
340 /* Adds to current_items */
341 static void build_app_menu(const char *app_dir, DirItem *app_item)
343 XMLwrapper *ai = NULL;
344 xmlNode *node;
345 GtkWidget *item;
347 ai = appinfo_get(app_dir, app_item);
348 if (ai)
350 node = xml_get_section(ai, NULL, "AppMenu");
351 if (node)
352 node = node->xmlChildrenNode;
354 else
356 if (app_item->flags & ITEM_FLAG_APPDIR)
357 node = NULL;
358 else
360 /* Not an application AND no AppInfo */
361 build_menu_for_type(app_item->mime_type);
362 return;
366 /* Add the menu entries */
367 for (; node; node = node->next)
369 item = create_menu_item(node);
371 if (item)
372 current_items = g_list_prepend(current_items, item);
375 item = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, NULL);
376 gtk_widget_show(item);
377 current_items = g_list_prepend(current_items, item);
378 g_signal_connect(item, "activate", G_CALLBACK(show_app_help), NULL);
379 gtk_label_set_text(GTK_LABEL(GTK_BIN(item)->child), _("Help"));
381 if (ai)
382 g_object_unref(ai);