summaryrefslogtreecommitdiffstats
path: root/preferred-apps.patch
blob: 8f755d9d5657185c18eebcfb7bf91b2def58336b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
diff -up gnome-panel-2.25.3/gnome-panel/launcher.c.preferred-apps gnome-panel-2.25.3/gnome-panel/launcher.c
--- gnome-panel-2.25.3/gnome-panel/launcher.c.preferred-apps	2009-01-16 14:15:17.000000000 -0500
+++ gnome-panel-2.25.3/gnome-panel/launcher.c	2009-01-16 14:24:06.000000000 -0500
@@ -21,6 +21,7 @@
 #include <glib/gi18n.h>
 #include <gio/gio.h>
 #include <gdk/gdkx.h>
+#include <gmenu-tree.h>
 
 #include <libpanel-util/panel-error.h>
 #include <libpanel-util/panel-glib.h>
@@ -44,6 +45,7 @@
 #include "panel-compatibility.h"
 #include "panel-ditem-editor.h"
 #include "panel-icon-names.h"
+#include "panel-run-dialog.h"
 
 static GdkScreen *
 launcher_get_screen (Launcher *launcher)
@@ -1215,3 +1217,189 @@ panel_launcher_set_dnd_enabled (Launcher
 	} else
 		gtk_drag_source_unset (launcher->button);
 }
+
+static gchar *
+find_desktop_file_from_exec (const gchar *exec)
+{
+	GSList *all_applications, *l;
+	gchar *path = NULL;
+	gchar **tokens, **tokens2;
+	gint i, match = 0;
+
+	/* FIXME no need to construct a humongous list here */
+	all_applications = get_all_applications ();
+
+	for (l = all_applications; l; l = l->next) {
+		GMenuTreeEntry *entry = l->data;
+		const char     *entry_exec;
+
+		entry_exec = gmenu_tree_entry_get_exec (entry);
+
+		if (strcmp (exec, entry_exec) == 0) {
+			path = gmenu_tree_entry_get_desktop_file_path (entry);
+			break;
+		}
+
+		tokens = g_strsplit (exec, " ", -1);
+		tokens2 = g_strsplit (entry_exec, " ", -1);
+
+		for (i = 0; tokens[i] && tokens2[i]; i++) {
+			if (strcmp (tokens[i], tokens2[i]) != 0)
+				break;
+		}
+		if (i > match) {
+			match = i;
+			path = gmenu_tree_entry_get_desktop_file_path (entry);
+		}
+
+		g_strfreev (tokens);
+		g_strfreev (tokens2);
+	}
+
+	path = g_strdup (path);
+	g_slist_free (all_applications);
+
+	return path;
+}
+
+static void
+update_preferred_app (const gchar *filename,
+		      const gchar *key,
+		      const gchar *exec)
+{
+	gchar *location;
+	GKeyFile *key_file;
+	GError *error = NULL;
+	gchar *data;
+	gsize len;
+	gboolean needs_terminal;
+
+	location = find_desktop_file_from_exec (exec);
+	key_file = g_key_file_new ();
+	if (!panel_key_file_load_from_uri (key_file, location,
+				   	   G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS,
+					   NULL)) {
+		/* FIXME would be much better if preferred apps were backed by desktop files */
+		g_key_file_set_string (key_file, "Desktop Entry", "Version", "1.0");
+		g_key_file_set_string (key_file, "Desktop Entry", "Encoding", "UTF-8");
+		g_key_file_set_string (key_file, "Desktop Entry", "Type", "Application");
+		g_key_file_set_string (key_file, "Desktop Entry", "Exec", exec);
+		if (strstr (key, "http")) {
+			g_key_file_set_string (key_file, "Desktop Entry", "Name", "Preferred Web Browser");
+			g_key_file_set_string (key_file, "Desktop Entry", "GenericName", "Web Browser");
+			g_key_file_set_string (key_file, "Desktop Entry", "Comment", "Browse the Web");
+			g_key_file_set_string (key_file, "Desktop Entry", "Icon", "redhat-web-browser.png");
+			if (strstr (exec, "%s")) {
+				gchar *exec2, *s;
+
+				exec2 = g_strdup (exec);
+				s = strstr (exec2, "%s");
+				s[1] = 'u';
+
+				g_key_file_set_string (key_file, "Desktop Entry", "Exec", exec2);
+				g_free (exec2);
+				
+			}
+		}
+		else if (strstr (key, "mailto")) {
+			g_key_file_set_string (key_file, "Desktop Entry", "Name", "Preferred Mail Reader");
+			g_key_file_set_string (key_file, "Desktop Entry", "GenericName", "Mail Reader");
+			g_key_file_set_string (key_file, "Desktop Entry", "Comment", "Send email");
+			g_key_file_set_string (key_file, "Desktop Entry", "Icon", "redhat-email.png");
+		}
+		if (g_str_has_suffix (key, "command")) {
+			int len;
+			char *key2;
+
+			len = strlen (key);
+			key2 = g_new (char, len - strlen ("command") + strlen ("needs_terminal") + 1);
+			strncpy (key2, key,  len - strlen ("command"));
+			strcpy (key2 + len - strlen ("command"), "needs_terminal");
+			needs_terminal = gconf_client_get_bool (panel_gconf_get_client (),
+								key2,
+								NULL);
+			g_free (key2);
+		}
+		else
+			needs_terminal = FALSE;
+		g_key_file_set_boolean (key_file, "Desktop Entry", "Terminal", needs_terminal);
+	}
+
+	g_free (location);
+
+	g_key_file_set_boolean (key_file, "Desktop Entry", "X-Panel-Monitor", TRUE);
+	g_key_file_set_boolean (key_file, "Desktop Entry", "NoDisplay", TRUE);
+
+	data = g_key_file_to_data (key_file, &len, &error);
+	if (error) {
+		g_printerr (_("Failed to convert data for '%s': %s"), 
+			    filename, error->message);
+		g_error_free (error);
+		g_key_file_free (key_file);
+
+		return; 
+	}
+	if (!g_file_set_contents (filename, data, len, &error)) {
+		g_printerr (_("Failed to save '%s': %s"), 
+			    filename, error->message);
+		g_error_free (error);
+	}
+
+	g_key_file_free (key_file);
+	g_free (data);
+}
+
+static void
+preferred_app_changed (GConfClient *client,
+		       gint         notify_id,
+		       GConfEntry  *entry,
+		       const gchar  *filename)
+{
+	update_preferred_app (filename, 
+	gconf_entry_get_key (entry), 
+	gconf_value_get_string (entry->value));
+}
+
+void
+panel_preferred_apps_init (void)
+{
+	GConfClient *client;
+        gchar *dirname, *filename, *exec;
+	gint i;
+
+	const gchar *keys[] = {
+		"/desktop/gnome/url-handlers/http/command",
+		"/desktop/gnome/url-handlers/mailto/command", 
+		NULL };
+	const gchar *files[] = {
+		"preferred-web-browser.desktop",
+		"preferred-mail-reader.desktop",
+		NULL }; 
+
+	client = panel_gconf_get_client ();
+
+	gconf_client_add_dir (client,
+			      "/desktop/gnome/url-handlers",
+			      GCONF_CLIENT_PRELOAD_NONE,
+			      NULL);
+
+	for (i = 0; keys[i]; i++) {
+                dirname = g_build_filename (g_get_user_data_dir (), 
+					    "applications", NULL);
+		filename = g_build_filename (dirname, files[i], NULL);
+		if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
+                        
+                        if (!g_file_test (dirname, G_FILE_TEST_EXISTS))
+                            g_mkdir_with_parents (dirname, 0755);
+
+			exec = gconf_client_get_string (client, keys[i], NULL);
+			update_preferred_app (filename, keys[i], exec);
+			g_free (exec);
+		}
+                g_free (dirname);
+
+		gconf_client_notify_add (client, keys[i],
+					 (GConfClientNotifyFunc) preferred_app_changed,
+					 filename, g_free, NULL);
+	}
+}
diff -up gnome-panel-2.25.3/gnome-panel/launcher.h.preferred-apps gnome-panel-2.25.3/gnome-panel/launcher.h
--- gnome-panel-2.25.3/gnome-panel/launcher.h.preferred-apps	2009-01-16 14:15:17.000000000 -0500
+++ gnome-panel-2.25.3/gnome-panel/launcher.h	2009-01-16 14:15:17.000000000 -0500
@@ -14,6 +14,8 @@
 #include "applet.h"
 #include "panel-widget.h"
 
+#include <gio/gio.h>
+
 G_BEGIN_DECLS
 
 typedef struct {
@@ -71,6 +73,7 @@ void            launcher_properties_dest
 void            panel_launcher_set_dnd_enabled  (Launcher *launcher,
 						 gboolean  dnd_enabled);
 
+void            panel_preferred_apps_init       (void);
 
 G_END_DECLS
 
diff -up gnome-panel-2.25.3/gnome-panel/main.c.preferred-apps gnome-panel-2.25.3/gnome-panel/main.c
--- gnome-panel-2.25.3/gnome-panel/main.c.preferred-apps	2008-12-09 07:32:37.000000000 -0500
+++ gnome-panel-2.25.3/gnome-panel/main.c	2009-01-16 14:15:17.000000000 -0500
@@ -27,6 +27,7 @@
 #include "panel-action-protocol.h"
 #include "panel-lockdown.h"
 #include "panel-icon-names.h"
+#include "launcher.h"
 #include "xstuff.h"
 
 #include "nothing.cP"
@@ -86,6 +87,7 @@ main (int argc, char **argv)
 			      GCONF_CLIENT_PRELOAD_NONE,
 			      NULL);
 
+	panel_preferred_apps_init ();
 	panel_global_config_load ();
 	panel_lockdown_init ();
 	panel_profile_load ();
diff -up gnome-panel-2.25.3/gnome-panel/panel-run-dialog.c.preferred-apps gnome-panel-2.25.3/gnome-panel/panel-run-dialog.c
--- gnome-panel-2.25.3/gnome-panel/panel-run-dialog.c.preferred-apps	2008-12-11 12:03:03.000000000 -0500
+++ gnome-panel-2.25.3/gnome-panel/panel-run-dialog.c	2009-01-16 14:15:17.000000000 -0500
@@ -789,7 +789,7 @@ get_all_applications_from_dir (GMenuTree
 	return list;
 }
 
-static GSList *
+GSList *
 get_all_applications (void)
 {
 	GMenuTree          *tree;
diff -up gnome-panel-2.25.3/gnome-panel/panel-run-dialog.h.preferred-apps gnome-panel-2.25.3/gnome-panel/panel-run-dialog.h
--- gnome-panel-2.25.3/gnome-panel/panel-run-dialog.h.preferred-apps	2008-11-08 19:27:01.000000000 -0500
+++ gnome-panel-2.25.3/gnome-panel/panel-run-dialog.h	2009-01-16 14:15:17.000000000 -0500
@@ -32,6 +32,8 @@ G_BEGIN_DECLS
 void panel_run_dialog_present           (GdkScreen  *screen,
 					 guint32    activate_time);
 
+GSList *get_all_applications           (void);
+
 G_END_DECLS
 
 #endif /* __PANEL_RUN_DIALOG_H__ */