a3ff9e05d6ee30b52ae77135b90afaeee164dacc
[lxde/lxpanel.git] / src / gtk-run.c
1 /*
2 * gtk-run.c: Little application launcher
3 * Copyright (C) 2006 Hong Jen Yee (PCMan) pcman.tw(AT)gmail.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gtk/gtk.h>
25 #include <gdk/gdkx.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "misc.h"
31 #ifndef DISABLE_MENU
32 #include <menu-cache.h>
33 #endif
34 #include <libfm/fm-gtk.h>
35
36 static GtkWidget* win = NULL; /* the run dialog */
37 #ifndef DISABLE_MENU
38 static MenuCache* menu_cache = NULL;
39 static GSList* app_list = NULL; /* all known apps in menu cache */
40 static gpointer reload_notify_id = NULL;
41 #endif
42
43 typedef struct _ThreadData
44 {
45 gboolean cancel; /* is the loading cancelled */
46 GSList* files; /* all executable files found */
47 GtkEntry* entry;
48 }ThreadData;
49
50 static ThreadData* thread_data = NULL; /* thread data used to load availble programs in PATH */
51
52 #ifndef DISABLE_MENU
53 static MenuCacheApp* match_app_by_exec(const char* exec)
54 {
55 GSList* l;
56 MenuCacheApp* ret = NULL;
57 char* exec_path = g_find_program_in_path(exec);
58 const char* pexec;
59 int path_len, exec_len, len;
60
61 if( ! exec_path )
62 return NULL;
63
64 path_len = strlen(exec_path);
65 exec_len = strlen(exec);
66
67 for( l = app_list; l; l = l->next )
68 {
69 MenuCacheApp* app = MENU_CACHE_APP(l->data);
70 const char* app_exec = menu_cache_app_get_exec(app);
71 if ( ! app_exec)
72 continue;
73 #if 0 /* This is useless and incorrect. */
74 /* Dirty hacks to skip sudo programs. This can be a little bit buggy */
75 if( g_str_has_prefix(app_exec, "gksu") )
76 {
77 app_exec += 4;
78 if( app_exec[0] == '\0' ) /* "gksu" itself */
79 app_exec -= 4;
80 else if( app_exec[0] == ' ' ) /* "gksu something..." */
81 ++app_exec;
82 else if( g_str_has_prefix(app_exec, "do ") ) /* "gksudo something" */
83 app_exec += 3;
84 }
85 else if( g_str_has_prefix(app_exec, "kdesu ") ) /* kdesu */
86 app_exec += 6;
87 #endif
88
89 if( g_path_is_absolute(app_exec) )
90 {
91 pexec = exec_path;
92 len = path_len;
93 }
94 else
95 {
96 pexec = exec;
97 len = exec_len;
98 }
99
100 if( strncmp(app_exec, pexec, len) == 0 )
101 {
102 /* exact match has the highest priority */
103 if( app_exec[len] == '\0' )
104 {
105 ret = app;
106 break;
107 }
108 /* those matches the pattern: exe_name %F|%f|%U|%u have higher priority */
109 if( app_exec[len] == ' ' )
110 {
111 if( app_exec[len + 1] == '%' )
112 {
113 if( strchr( "FfUu", app_exec[len + 2] ) )
114 {
115 ret = app;
116 break;
117 }
118 }
119 ret = app;
120 }
121 }
122 }
123
124 /* if this is a symlink */
125 if( ! ret && g_file_test(exec_path, G_FILE_TEST_IS_SYMLINK) )
126 {
127 char target[512]; /* FIXME: is this enough? */
128 len = readlink( exec_path, target, sizeof(target) - 1);
129 if( len > 0 )
130 {
131 target[len] = '\0';
132 ret = match_app_by_exec(target);
133 if( ! ret )
134 {
135 /* FIXME: Actually, target could be relative paths.
136 * So, actually path resolution is needed here. */
137 char* basename = g_path_get_basename(target);
138 char* locate = g_find_program_in_path(basename);
139 if( locate && strcmp(locate, target) == 0 )
140 {
141 ret = match_app_by_exec(basename);
142 g_free(locate);
143 }
144 g_free(basename);
145 }
146 }
147 }
148
149 g_free(exec_path);
150 return ret;
151 }
152 #endif
153
154 static void setup_auto_complete_with_data(ThreadData* data)
155 {
156 GtkListStore* store;
157 GSList *l;
158 GtkEntryCompletion* comp = gtk_entry_completion_new();
159 gtk_entry_completion_set_minimum_key_length( comp, 2 );
160 gtk_entry_completion_set_inline_completion( comp, TRUE );
161 gtk_entry_completion_set_popup_set_width( comp, TRUE );
162 gtk_entry_completion_set_popup_single_match( comp, FALSE );
163 store = gtk_list_store_new( 1, G_TYPE_STRING );
164
165 for( l = data->files; l; l = l->next )
166 {
167 const char *name = (const char*)l->data;
168 GtkTreeIter it;
169 gtk_list_store_append( store, &it );
170 gtk_list_store_set( store, &it, 0, name, -1 );
171 }
172
173 gtk_entry_completion_set_model( comp, (GtkTreeModel*)store );
174 g_object_unref( store );
175 gtk_entry_completion_set_text_column( comp, 0 );
176 gtk_entry_set_completion( (GtkEntry*)data->entry, comp );
177
178 /* trigger entry completion */
179 gtk_entry_completion_complete(comp);
180 g_object_unref( comp );
181 }
182
183 static void thread_data_free(ThreadData* data)
184 {
185 g_slist_foreach(data->files, (GFunc)g_free, NULL);
186 g_slist_free(data->files);
187 g_slice_free(ThreadData, data);
188 }
189
190 static gboolean on_thread_finished(ThreadData* data)
191 {
192 /* don't setup entry completion if the thread is already cancelled. */
193 if( !data->cancel )
194 setup_auto_complete_with_data(thread_data);
195 thread_data_free(data);
196 thread_data = NULL; /* global thread_data pointer */
197 return FALSE;
198 }
199
200 static gpointer thread_func(ThreadData* data)
201 {
202 GSList *list = NULL;
203 gchar **dirname;
204 gchar **dirnames = g_strsplit( g_getenv("PATH"), ":", 0 );
205
206 for( dirname = dirnames; !thread_data->cancel && *dirname; ++dirname )
207 {
208 GDir *dir = g_dir_open( *dirname, 0, NULL );
209 const char *name;
210 if( ! dir )
211 continue;
212 while( !thread_data->cancel && (name = g_dir_read_name(dir)) )
213 {
214 char* filename = g_build_filename( *dirname, name, NULL );
215 if( g_file_test( filename, G_FILE_TEST_IS_EXECUTABLE ) )
216 {
217 if(thread_data->cancel)
218 break;
219 if( !g_slist_find_custom( list, name, (GCompareFunc)strcmp ) )
220 list = g_slist_prepend( list, g_strdup( name ) );
221 }
222 g_free( filename );
223 }
224 g_dir_close( dir );
225 }
226 g_strfreev( dirnames );
227
228 data->files = list;
229 /* install an idle handler to free associated data */
230 g_idle_add((GSourceFunc)on_thread_finished, data);
231 #if GLIB_CHECK_VERSION(2, 32, 0)
232 g_thread_unref(g_thread_self());
233 #endif
234
235 return NULL;
236 }
237
238 static void setup_auto_complete( GtkEntry* entry )
239 {
240 gboolean cache_is_available = FALSE;
241 /* FIXME: consider saving the list of commands as on-disk cache. */
242 if( cache_is_available )
243 {
244 /* load cached program list */
245 }
246 else
247 {
248 /* load in another working thread */
249 thread_data = g_slice_new0(ThreadData); /* the data will be freed in idle handler later. */
250 thread_data->entry = entry;
251 #if GLIB_CHECK_VERSION(2, 32, 0)
252 g_thread_new("gtk-run-autocomplete", (GThreadFunc)thread_func, thread_data);
253 /* we don't use loader_thread_id but Glib 2.32 crashes if we unref
254 GThread while it's in creation progress. It is a bug of GLib
255 certainly but as workaround we'll unref it in the thread itself */
256 #else
257 g_thread_create((GThreadFunc)thread_func, thread_data, FALSE, NULL);
258 #endif
259 }
260 }
261
262 #ifndef DISABLE_MENU
263 static void reload_apps(MenuCache* cache, gpointer user_data)
264 {
265 g_debug("reload apps!");
266 if(app_list)
267 {
268 g_slist_foreach(app_list, (GFunc)menu_cache_item_unref, NULL);
269 g_slist_free(app_list);
270 }
271 app_list = menu_cache_list_all_apps(cache);
272 }
273 #endif
274
275 static void on_response( GtkDialog* dlg, gint response, gpointer user_data )
276 {
277 GtkEntry* entry = (GtkEntry*)user_data;
278 if( G_LIKELY(response == GTK_RESPONSE_OK) )
279 {
280 if (!fm_launch_command_simple(GTK_WINDOW(dlg), NULL, 0, gtk_entry_get_text(entry), NULL))
281 {
282 g_signal_stop_emission_by_name( dlg, "response" );
283 return;
284 }
285 }
286
287 /* cancel running thread if needed */
288 if( thread_data ) /* the thread is still running */
289 thread_data->cancel = TRUE; /* cancel the thread */
290
291 gtk_widget_destroy( (GtkWidget*)dlg );
292 win = NULL;
293
294 #ifndef DISABLE_MENU
295 /* free app list */
296 g_slist_foreach(app_list, (GFunc)menu_cache_item_unref, NULL);
297 g_slist_free(app_list);
298 app_list = NULL;
299
300 /* free menu cache */
301 menu_cache_remove_reload_notify(menu_cache, reload_notify_id);
302 reload_notify_id = NULL;
303 menu_cache_unref(menu_cache);
304 menu_cache = NULL;
305 #endif
306 }
307
308 #ifndef DISABLE_MENU
309 static void on_entry_changed( GtkEntry* entry, GtkImage* img )
310 {
311 const char* str = gtk_entry_get_text(entry);
312 MenuCacheApp* app = NULL;
313 if( str && *str )
314 app = match_app_by_exec(str);
315
316 if( app )
317 {
318 int w, h;
319 const char *name = menu_cache_item_get_icon(MENU_CACHE_ITEM(app));
320 FmIcon * fm_icon;
321 GdkPixbuf* pix;
322
323 gtk_icon_size_lookup(GTK_ICON_SIZE_DIALOG, &w, &h);
324 fm_icon = fm_icon_from_name(name ? name : "application-x-executable");
325 pix = fm_pixbuf_from_icon_with_fallback(fm_icon, h, "application-x-executable");
326 g_object_unref(fm_icon);
327 gtk_image_set_from_pixbuf(img, pix);
328 g_object_unref(pix);
329 }
330 else
331 {
332 gtk_image_set_from_stock(img, GTK_STOCK_EXECUTE, GTK_ICON_SIZE_DIALOG);
333 }
334 }
335 #endif
336
337 static void activate_window(GtkWindow* toplevel_window)
338 {
339 /* Calling gtk_window_present() cannot support
340 * source indication. Use our own implementation.
341 * Without this, the window activated might be
342 * put in background by WM to avoid stealing
343 * of focus.
344 * See EWMH spec, source indication part:
345 * http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#sourceindication
346 */
347 GtkWidget* widget = GTK_WIDGET(toplevel_window);
348 GdkScreen* screen = gtk_widget_get_screen(widget);
349 if (gdk_x11_screen_supports_net_wm_hint (screen,
350 gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
351 {
352 GdkWindow* window = gtk_widget_get_window(widget);
353 GdkDisplay* display = gtk_widget_get_display(widget);
354 GdkWindow* root = gdk_screen_get_root_window(screen);
355
356 /* show the window first */
357 gtk_widget_show(widget);
358
359 /* then, activate it */
360 XClientMessageEvent xclient;
361 memset(&xclient, 0, sizeof (xclient));
362 xclient.type = ClientMessage;
363 xclient.window = GDK_WINDOW_XID(window);
364 xclient.message_type = gdk_x11_get_xatom_by_name_for_display(display,
365 "_NET_ACTIVE_WINDOW");
366 xclient.format = 32;
367 xclient.data.l[0] = 2; /* source indication: 2 represents direct user actions. */
368 xclient.data.l[1] = gtk_get_current_event_time();
369 xclient.data.l[2] = None; /* currently active window */
370 xclient.data.l[3] = 0;
371 xclient.data.l[4] = 0;
372
373 XSendEvent(GDK_DISPLAY_XDISPLAY(display), GDK_WINDOW_XID(root), False,
374 SubstructureRedirectMask|SubstructureNotifyMask,
375 (XEvent *)&xclient);
376 } /* if _NET_ACTIVE_WINDOW command is not supported */
377 else
378 gtk_window_present(toplevel_window);
379 }
380
381 void gtk_run()
382 {
383 GtkWidget *entry, *hbox, *img, *dlg_vbox;
384
385 if(!win)
386 {
387 win = gtk_dialog_new_with_buttons( _("Run"),
388 NULL,
389 GTK_DIALOG_NO_SEPARATOR,
390 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
391 GTK_STOCK_OK, GTK_RESPONSE_OK,
392 NULL );
393 gtk_dialog_set_alternative_button_order((GtkDialog*)win,
394 GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL, -1);
395 gtk_dialog_set_default_response( (GtkDialog*)win, GTK_RESPONSE_OK );
396 entry = gtk_entry_new();
397
398 gtk_entry_set_activates_default( (GtkEntry*)entry, TRUE );
399 dlg_vbox = gtk_dialog_get_content_area((GtkDialog*)win);
400 gtk_box_pack_start( (GtkBox*)dlg_vbox,
401 gtk_label_new(_("Enter the command you want to execute:")),
402 FALSE, FALSE, 8 );
403 hbox = gtk_hbox_new( FALSE, 2 );
404 img = gtk_image_new_from_stock( GTK_STOCK_EXECUTE, GTK_ICON_SIZE_DIALOG );
405 gtk_box_pack_start( (GtkBox*)hbox, img,
406 FALSE, FALSE, 4 );
407 gtk_box_pack_start( (GtkBox*)hbox, entry, TRUE, TRUE, 4 );
408 gtk_box_pack_start( (GtkBox*)dlg_vbox,
409 hbox, FALSE, FALSE, 8 );
410 g_signal_connect( win, "response", G_CALLBACK(on_response), entry );
411 gtk_window_set_position( (GtkWindow*)win, GTK_WIN_POS_CENTER );
412 gtk_window_set_default_size( (GtkWindow*)win, 360, -1 );
413 gtk_widget_show_all( win );
414
415 setup_auto_complete( (GtkEntry*)entry );
416 gtk_widget_show(win);
417
418 #ifndef DISABLE_MENU
419 g_signal_connect(entry ,"changed", G_CALLBACK(on_entry_changed), img);
420
421 /* get all apps */
422 #ifndef MENU_CACHE_CHECK_VERSION
423 #define MENU_CACHE_CHECK_VERSION(a,b,c) 0
424 #endif
425 #if MENU_CACHE_CHECK_VERSION(0, 6, 1)
426 menu_cache = menu_cache_lookup_sync(g_getenv("XDG_MENU_PREFIX") ? "applications.menu" : "lxde-applications.menu" );
427 if( menu_cache )
428 {
429 #else
430 /* SF bug #689: menu_cache_lookup_sync() was fail-prone before 0.6.1 */
431 menu_cache = menu_cache_lookup(g_getenv("XDG_MENU_PREFIX") ? "applications.menu" : "lxde-applications.menu" );
432 if( menu_cache )
433 {
434 menu_cache_reload(menu_cache);
435 #endif
436 app_list = menu_cache_list_all_apps(menu_cache);
437 reload_notify_id = menu_cache_add_reload_notify(menu_cache, reload_apps, NULL);
438 }
439 #endif
440 }
441
442 activate_window(GTK_WINDOW(win));
443 }
444
445
446 /* vim: set sw=4 et sts=4 ts=4 : */