~ubuntu-branches/ubuntu/vivid/gnome-panel/vivid

« back to all changes in this revision

Viewing changes to .pc/01_menus_rename.patch/gnome-panel/panel-run-dialog.c

  • Committer: Package Import Robot
  • Author(s): Alberts Muktupāvels
  • Date: 2014-03-17 09:00:22 UTC
  • Revision ID: package-import@ubuntu.com-20140317090022-5oba1ej59xo18nh0
Tags: 1:3.8.0-1ubuntu10
Update 01_menus_rename.patch to use separate menus file for
gnome-flashback session (lp: #1267787).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- c-basic-offset: 8; indent-tabs-mode: t -*-
 
2
 * panel-run-dialog.c:
 
3
 *
 
4
 * Copyright (C) 2003 Frank Worsley <fworsley@shaw.ca>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License as
 
8
 * published by the Free Software Foundation; either version 2 of the
 
9
 * License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
19
 * 02111-1307, USA.
 
20
 
 
21
 * Authors:
 
22
 *      Frank Worsley <fworsley@shaw.ca>
 
23
 *
 
24
 * Based on code by:
 
25
 *      Havoc Pennington <hp@pobox.com>
 
26
 *      George Lebl <jirka@5z.com>
 
27
 *      Mark McLoughlin <mark@skynet.ie>
 
28
 *      Tom Tromey (Copyright (C) 1998)
 
29
 */
 
30
 
 
31
#include <config.h>
 
32
 
 
33
#include "panel-run-dialog.h"
 
34
 
 
35
#include <string.h>
 
36
#include <dirent.h>
 
37
#include <errno.h>
 
38
#include <sys/types.h>
 
39
#include <unistd.h>
 
40
 
 
41
#include <glib/gi18n.h>
 
42
#include <gio/gio.h>
 
43
#include <gdk/gdkkeysyms.h>
 
44
#include <gmenu-tree.h>
 
45
 
 
46
#include <libpanel-util/panel-error.h>
 
47
#include <libpanel-util/panel-glib.h>
 
48
#include <libpanel-util/panel-gtk.h>
 
49
#include <libpanel-util/panel-keyfile.h>
 
50
#include <libpanel-util/panel-show.h>
 
51
#include <libpanel-util/panel-xdg.h>
 
52
 
 
53
#include "nothing.h"
 
54
#include "panel-util.h"
 
55
#include "panel-globals.h"
 
56
#include "panel-enums.h"
 
57
#include "panel-stock-icons.h"
 
58
#include "panel-multiscreen.h"
 
59
#include "menu.h"
 
60
#include "panel-lockdown.h"
 
61
#include "panel-xutils.h"
 
62
#include "panel-icon-names.h"
 
63
#include "panel-schemas.h"
 
64
 
 
65
typedef struct {
 
66
        GtkWidget        *run_dialog;
 
67
 
 
68
        GSettings        *run_settings;
 
69
 
 
70
        GtkWidget        *main_box;
 
71
 
 
72
        GtkWidget        *combobox;
 
73
        GtkWidget        *pixmap;
 
74
        GtkWidget        *run_button;
 
75
        GtkWidget        *file_button;
 
76
        GtkWidget        *list_expander;
 
77
        GtkWidget        *terminal_checkbox;
 
78
        GtkWidget        *program_label;
 
79
        GtkWidget        *program_list;
 
80
        
 
81
        long              changed_id;
 
82
 
 
83
        GtkListStore     *program_list_store;
 
84
 
 
85
        GHashTable       *dir_hash;
 
86
        GList            *possible_executables;
 
87
        GList            *completion_items;
 
88
        GCompletion      *completion;
 
89
        
 
90
        GSList           *add_icon_paths;
 
91
        int               add_items_idle_id;
 
92
        int               find_command_idle_id;
 
93
        gboolean          use_program_list;
 
94
        gboolean          completion_started;
 
95
        
 
96
        GIcon            *gicon;
 
97
        char             *desktop_path;
 
98
        char             *item_name;    
 
99
} PanelRunDialog;
 
100
 
 
101
enum {
 
102
        COLUMN_ICON,
 
103
        COLUMN_NAME,
 
104
        COLUMN_COMMENT,
 
105
        COLUMN_PATH,
 
106
        COLUMN_EXEC,
 
107
        COLUMN_VISIBLE,
 
108
        NUM_COLUMNS
 
109
};
 
110
 
 
111
static PanelRunDialog *static_dialog = NULL;
 
112
 
 
113
#define PANEL_RUN_MAX_HISTORY 20
 
114
 
 
115
static GtkTreeModel *
 
116
_panel_run_get_recent_programs_list (PanelRunDialog *dialog)
 
117
{
 
118
        GtkListStore  *list;
 
119
        char         **commands;
 
120
        int            i;
 
121
 
 
122
        list = gtk_list_store_new (1, G_TYPE_STRING);
 
123
 
 
124
        commands = g_settings_get_strv (dialog->run_settings,
 
125
                                        PANEL_RUN_HISTORY_KEY);
 
126
 
 
127
        for (i = 0; commands[i] != NULL; i++) {
 
128
                GtkTreeIter iter;
 
129
                gtk_list_store_prepend (list, &iter);
 
130
                gtk_list_store_set (list, &iter, 0, commands[i], -1);
 
131
        }
 
132
 
 
133
        g_strfreev (commands);
 
134
 
 
135
        return GTK_TREE_MODEL (list);
 
136
}
 
137
 
 
138
static void
 
139
_panel_run_save_recent_programs_list (PanelRunDialog *dialog,
 
140
                                      char           *last_command)
 
141
{
 
142
        char **commands;
 
143
        char **new_commands;
 
144
        int    i;
 
145
        int    size;
 
146
 
 
147
        commands = g_settings_get_strv (dialog->run_settings,
 
148
                                        PANEL_RUN_HISTORY_KEY);
 
149
 
 
150
        /* do not save the same command twice in a row */
 
151
        if (g_strcmp0 (commands[0], last_command) == 0)
 
152
                return;
 
153
 
 
154
        for (i = 0; commands[i] != NULL; i++);
 
155
        size = MIN (i + 1, PANEL_RUN_MAX_HISTORY);
 
156
 
 
157
        new_commands = g_new (char *, size + 1);
 
158
 
 
159
        new_commands[0] = last_command;
 
160
        new_commands[size] = NULL; /* last item */
 
161
 
 
162
        for (i = 1; i < size; i++)
 
163
                new_commands[i] = commands[i-1];
 
164
 
 
165
        g_settings_set_strv (dialog->run_settings,
 
166
                             PANEL_RUN_HISTORY_KEY,
 
167
                             (const char **) new_commands);
 
168
 
 
169
        g_free (new_commands); /* we don't own the strings */
 
170
        g_strfreev (commands);
 
171
}
 
172
 
 
173
static void
 
174
panel_run_dialog_destroy (PanelRunDialog *dialog)
 
175
{
 
176
        GList *l;
 
177
        
 
178
        dialog->changed_id = 0;
 
179
 
 
180
        g_object_unref (dialog->list_expander);
 
181
        
 
182
        g_slist_foreach (dialog->add_icon_paths, (GFunc) gtk_tree_path_free, NULL);
 
183
        g_slist_free (dialog->add_icon_paths);
 
184
        dialog->add_icon_paths = NULL;
 
185
 
 
186
        if (dialog->gicon)
 
187
                g_object_unref (dialog->gicon);
 
188
        dialog->gicon = NULL;
 
189
 
 
190
        g_free (dialog->desktop_path);
 
191
        dialog->desktop_path = NULL;
 
192
        g_free (dialog->item_name);
 
193
        dialog->item_name = NULL;
 
194
 
 
195
        if (dialog->add_items_idle_id)
 
196
                g_source_remove (dialog->add_items_idle_id);
 
197
        dialog->add_items_idle_id = 0;
 
198
 
 
199
        if (dialog->find_command_idle_id)
 
200
                g_source_remove (dialog->find_command_idle_id);
 
201
        dialog->find_command_idle_id = 0;
 
202
 
 
203
        if (dialog->dir_hash)
 
204
                g_hash_table_destroy (dialog->dir_hash);
 
205
        dialog->dir_hash = NULL;
 
206
 
 
207
        for (l = dialog->possible_executables; l; l = l->next)
 
208
                g_free (l->data);
 
209
        g_list_free (dialog->possible_executables);
 
210
        dialog->possible_executables = NULL;
 
211
        
 
212
        for (l = dialog->completion_items; l; l = l->next)
 
213
                g_free (l->data);
 
214
        g_list_free (dialog->completion_items);
 
215
        dialog->completion_items = NULL;
 
216
        
 
217
        if (dialog->completion)
 
218
                g_completion_free (dialog->completion);
 
219
        dialog->completion = NULL;
 
220
 
 
221
        if (dialog->run_settings)
 
222
                g_object_unref (dialog->run_settings);
 
223
        dialog->run_settings = NULL;
 
224
 
 
225
        g_free (dialog);
 
226
}
 
227
 
 
228
static const char *
 
229
panel_run_dialog_get_combo_text (PanelRunDialog *dialog)
 
230
{
 
231
        GtkWidget *entry;
 
232
 
 
233
        entry = gtk_bin_get_child (GTK_BIN (dialog->combobox));
 
234
 
 
235
        return gtk_entry_get_text (GTK_ENTRY (entry));
 
236
}
 
237
 
 
238
static void
 
239
panel_run_dialog_set_default_icon (PanelRunDialog *dialog, gboolean set_drag)
 
240
{
 
241
        gtk_image_set_from_icon_name (GTK_IMAGE (dialog->pixmap),
 
242
                                      PANEL_ICON_RUN,
 
243
                                      GTK_ICON_SIZE_DIALOG);
 
244
        
 
245
        if (set_drag)
 
246
                gtk_drag_source_set_icon_name (dialog->run_dialog,
 
247
                                               PANEL_ICON_LAUNCHER);
 
248
}
 
249
 
 
250
static void
 
251
panel_run_dialog_set_icon (PanelRunDialog *dialog,
 
252
                           GIcon          *gicon,
 
253
                           gboolean        force)
 
254
{
 
255
        if (!force && gicon && dialog->gicon &&
 
256
            gicon == dialog->gicon)
 
257
                return;
 
258
 
 
259
        if (dialog->gicon)
 
260
                g_object_unref (dialog->gicon);
 
261
        dialog->gicon = NULL;
 
262
                
 
263
        if (gicon) {
 
264
                dialog->gicon = g_object_ref (gicon);
 
265
                gtk_image_set_from_gicon (GTK_IMAGE (dialog->pixmap),
 
266
                                          gicon, GTK_ICON_SIZE_DIALOG);
 
267
                gtk_drag_source_set_icon_gicon (dialog->run_dialog, gicon);
 
268
        } else {
 
269
                panel_run_dialog_set_default_icon (dialog, TRUE);
 
270
        }
 
271
}
 
272
 
 
273
static gboolean
 
274
command_is_executable (const char   *command,
 
275
                       int          *argcp,
 
276
                       char       ***argvp)
 
277
{
 
278
        gboolean   result;
 
279
        char     **argv;
 
280
        char      *path;
 
281
        int        argc;
 
282
 
 
283
        result = g_shell_parse_argv (command, &argc, &argv, NULL);
 
284
 
 
285
        if (!result)
 
286
                return FALSE;
 
287
 
 
288
        path = g_find_program_in_path (argv[0]);
 
289
 
 
290
        if (!path) {
 
291
                g_strfreev (argv);
 
292
                return FALSE;
 
293
        }
 
294
 
 
295
        /* If we pass an absolute path to g_find_program it just returns
 
296
         * that absolute path without checking if it is executable. Also
 
297
         * make sure its a regular file so we don't try to launch
 
298
         * directories or device nodes.
 
299
         */
 
300
        if (!g_file_test (path, G_FILE_TEST_IS_EXECUTABLE) ||
 
301
            !g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
 
302
                g_free (path);
 
303
                g_strfreev (argv);
 
304
                return FALSE;
 
305
        }
 
306
 
 
307
        g_free (path);
 
308
 
 
309
        if (argcp)
 
310
                *argcp = argc;
 
311
        if (argvp)
 
312
                *argvp = argv;
 
313
 
 
314
        return TRUE;
 
315
}
 
316
 
 
317
/*
 
318
 * Set the DISPLAY variable, to be use by g_spawn_async.
 
319
 */
 
320
static void
 
321
set_environment (gpointer display)
 
322
{
 
323
  g_setenv ("DISPLAY", display, TRUE);
 
324
}
 
325
 
 
326
static void
 
327
dummy_child_watch (GPid         pid,
 
328
                   gint         status,
 
329
                   gpointer user_data)
 
330
{
 
331
        /* Nothing, this is just to ensure we don't double fork
 
332
         * and break pkexec:
 
333
         * https://bugzilla.gnome.org/show_bug.cgi?id=675789
 
334
         */
 
335
}
 
336
 
 
337
 
 
338
/**
 
339
 * panel_run_dialog_prepend_terminal_to_vector:
 
340
 * @argc: a pointer to the vector size
 
341
 * @argv: a pointer to the vector
 
342
 *
 
343
 * Description:  Prepends a terminal (either the one configured as default in
 
344
 * the user's GNOME setup, or one of the common xterm emulators) to the passed
 
345
 * in vector, modifying it in the process.  The vector should be allocated with
 
346
 * #g_malloc, as this will #g_free the original vector.  Also all elements must
 
347
 * have been allocated separately.  That is the standard glib/GNOME way of
 
348
 * doing vectors however.  If the integer that @argc points to is negative, the
 
349
 * size will first be computed.  Also note that passing in pointers to a vector
 
350
 * that is empty, will just create a new vector for you.
 
351
 **/
 
352
/* TODO: throw out this function if there ever is a proper GAppInfo port */
 
353
static void
 
354
panel_run_dialog_prepend_terminal_to_vector (int *argc, char ***argv)
 
355
{
 
356
        char **real_argv;
 
357
        int real_argc;
 
358
        int i, j;
 
359
        char **term_argv = NULL;
 
360
        int term_argc = 0;
 
361
        GSettings *settings;
 
362
 
 
363
        gchar *terminal = NULL;
 
364
 
 
365
        char **the_argv;
 
366
 
 
367
        g_return_if_fail (argc != NULL);
 
368
        g_return_if_fail (argv != NULL);
 
369
 
 
370
        /* sanity */
 
371
        if(*argv == NULL)
 
372
                *argc = 0;
 
373
 
 
374
        the_argv = *argv;
 
375
 
 
376
        /* compute size if not given */
 
377
        if (*argc < 0) {
 
378
                for (i = 0; the_argv[i] != NULL; i++)
 
379
                        ;
 
380
                *argc = i;
 
381
        }
 
382
 
 
383
        settings = g_settings_new ("org.gnome.desktop.default-applications.terminal");
 
384
        terminal = g_settings_get_string (settings, "exec");
 
385
 
 
386
        if (terminal) {
 
387
                gchar *command_line;
 
388
                gchar *exec_flag;
 
389
 
 
390
                exec_flag = g_settings_get_string (settings, "exec-arg");
 
391
 
 
392
                if (exec_flag == NULL)
 
393
                        command_line = g_strdup (terminal);
 
394
                else
 
395
                        command_line = g_strdup_printf ("%s %s", terminal,
 
396
                                                        exec_flag);
 
397
 
 
398
                g_shell_parse_argv (command_line,
 
399
                                    &term_argc,
 
400
                                    &term_argv,
 
401
                                    NULL /* error */);
 
402
 
 
403
                g_free (command_line);
 
404
                g_free (exec_flag);
 
405
                g_free (terminal);
 
406
        }
 
407
 
 
408
        g_object_unref (settings);
 
409
 
 
410
        if (term_argv == NULL) {
 
411
                char *check;
 
412
 
 
413
                term_argc = 2;
 
414
                term_argv = g_new0 (char *, 3);
 
415
 
 
416
                check = g_find_program_in_path ("gnome-terminal");
 
417
                if (check != NULL) {
 
418
                        term_argv[0] = check;
 
419
                        /* Note that gnome-terminal takes -x and
 
420
                         * as -e in gnome-terminal is broken we use that. */
 
421
                        term_argv[1] = g_strdup ("-x");
 
422
                } else {
 
423
                        if (check == NULL)
 
424
                                check = g_find_program_in_path ("nxterm");
 
425
                        if (check == NULL)
 
426
                                check = g_find_program_in_path ("color-xterm");
 
427
                        if (check == NULL)
 
428
                                check = g_find_program_in_path ("rxvt");
 
429
                        if (check == NULL)
 
430
                                check = g_find_program_in_path ("xterm");
 
431
                        if (check == NULL)
 
432
                                check = g_find_program_in_path ("dtterm");
 
433
                        if (check == NULL) {
 
434
                                g_warning (_("Cannot find a terminal, using "
 
435
                                             "xterm, even if it may not work"));
 
436
                                check = g_strdup ("xterm");
 
437
                        }
 
438
                        term_argv[0] = check;
 
439
                        term_argv[1] = g_strdup ("-e");
 
440
                }
 
441
        }
 
442
 
 
443
        real_argc = term_argc + *argc;
 
444
        real_argv = g_new (char *, real_argc + 1);
 
445
 
 
446
        for (i = 0; i < term_argc; i++)
 
447
                real_argv[i] = term_argv[i];
 
448
 
 
449
        for (j = 0; j < *argc; j++, i++)
 
450
                real_argv[i] = (char *)the_argv[j];
 
451
 
 
452
        real_argv[i] = NULL;
 
453
 
 
454
        g_free (*argv);
 
455
        *argv = real_argv;
 
456
        *argc = real_argc;
 
457
 
 
458
        /* we use g_free here as we sucked all the inner strings
 
459
         * out from it into real_argv */
 
460
        g_free (term_argv);
 
461
}
 
462
 
 
463
static gboolean
 
464
panel_run_dialog_launch_command (PanelRunDialog *dialog,
 
465
                                 const char     *command,
 
466
                                 const char     *locale_command)
 
467
{
 
468
        GdkScreen  *screen;
 
469
        gboolean    result;
 
470
        GError     *error = NULL;
 
471
        char      **argv;
 
472
        int         argc;
 
473
        char       *display;
 
474
        GPid        pid;
 
475
 
 
476
        if (!command_is_executable (locale_command, &argc, &argv))
 
477
                return FALSE;
 
478
 
 
479
        screen = gtk_window_get_screen (GTK_WINDOW (dialog->run_dialog));
 
480
 
 
481
        if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->terminal_checkbox)))
 
482
                panel_run_dialog_prepend_terminal_to_vector (&argc, &argv);
 
483
 
 
484
        display = gdk_screen_make_display_name (screen);
 
485
 
 
486
        result = g_spawn_async (NULL, /* working directory */
 
487
                                argv,
 
488
                                NULL, /* envp */
 
489
                                G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
 
490
                                set_environment,
 
491
                                display,
 
492
                                &pid,
 
493
                                &error);
 
494
 
 
495
        if (!result) {
 
496
                char *primary;
 
497
 
 
498
                primary = g_markup_printf_escaped (_("Could not run command '%s'"),
 
499
                                                   command);
 
500
                panel_error_dialog (GTK_WINDOW (dialog->run_dialog), NULL,
 
501
                                    "cannot_spawn_command", TRUE,
 
502
                                    primary, error->message);
 
503
                g_free (primary);
 
504
 
 
505
                g_error_free (error);
 
506
        } else {
 
507
                g_child_watch_add (pid, dummy_child_watch, NULL);
 
508
        }
 
509
 
 
510
        g_free (display);
 
511
        g_strfreev (argv);
 
512
 
 
513
        return result;
 
514
}
 
515
 
 
516
static void
 
517
panel_run_dialog_execute (PanelRunDialog *dialog)
 
518
{
 
519
        GError   *error;
 
520
        gboolean  result;
 
521
        char     *command;
 
522
        char     *disk;
 
523
        char     *scheme;       
 
524
        
 
525
        command = g_strdup (panel_run_dialog_get_combo_text (dialog));
 
526
        command = g_strchug (command);
 
527
 
 
528
        if (!command || !command [0]) {
 
529
                g_free (command);
 
530
                return;
 
531
        }
 
532
        
 
533
        /* evil eggies, do not translate! */
 
534
        if (!strcmp (command, "free the fish")) {
 
535
                start_screen_check ();
 
536
 
 
537
                g_free (command);
 
538
                gtk_widget_destroy (dialog->run_dialog);
 
539
                return;
 
540
        }
 
541
                
 
542
        error = NULL;
 
543
        disk = g_locale_from_utf8 (command, -1, NULL, NULL, &error);
 
544
 
 
545
        if (!disk || error) {
 
546
                char *primary;
 
547
 
 
548
                primary = g_strdup_printf (_("Could not convert '%s' from UTF-8"),
 
549
                                           command);
 
550
                panel_error_dialog (GTK_WINDOW (dialog->run_dialog), NULL,
 
551
                                    "cannot_convert_command_from_utf8", TRUE,
 
552
                                    primary, error->message);
 
553
                g_free (primary);
 
554
 
 
555
                g_error_free (error);
 
556
                return;
 
557
        }
 
558
 
 
559
        result = FALSE;
 
560
        
 
561
        scheme = g_uri_parse_scheme (disk);
 
562
        /* if it's an absolute path or not a URI, it's possibly an executable,
 
563
         * so try it before displaying it */
 
564
        if (g_path_is_absolute (disk) || !scheme)
 
565
                result = panel_run_dialog_launch_command (dialog, command, disk);
 
566
        
 
567
        if (!result) {
 
568
                GFile     *file;
 
569
                char      *uri;
 
570
                GdkScreen *screen;
 
571
                
 
572
                file = panel_util_get_file_optional_homedir (command);
 
573
                uri = g_file_get_uri (file);
 
574
                g_object_unref (file);
 
575
 
 
576
                screen = gtk_window_get_screen (GTK_WINDOW (dialog->run_dialog));
 
577
                result = panel_show_uri (screen, uri,
 
578
                                         gtk_get_current_event_time (), NULL);
 
579
 
 
580
                g_free (uri);
 
581
        }
 
582
                
 
583
        if (result) {
 
584
                /* only save working commands in history */
 
585
                _panel_run_save_recent_programs_list (dialog, command);
 
586
                
 
587
                /* only close the dialog if we successfully showed or launched
 
588
                 * something */
 
589
                gtk_widget_destroy (dialog->run_dialog);
 
590
        }
 
591
 
 
592
        g_free (command);
 
593
        g_free (disk);
 
594
        g_free (scheme);
 
595
}
 
596
 
 
597
static void
 
598
panel_run_dialog_response (PanelRunDialog *dialog,
 
599
                           int             response,
 
600
                           GtkWidget      *run_dialog)
 
601
{
 
602
 
 
603
        dialog->completion_started = FALSE;
 
604
 
 
605
        switch (response) {
 
606
        case GTK_RESPONSE_OK:
 
607
                panel_run_dialog_execute (dialog);
 
608
                break;
 
609
        case GTK_RESPONSE_CANCEL:
 
610
                gtk_widget_destroy (dialog->run_dialog);
 
611
                break;
 
612
        default:
 
613
                break;
 
614
        }
 
615
}
 
616
 
 
617
/* only quote the string if really needed */
 
618
static char *
 
619
quote_string (const char *s)
 
620
{
 
621
        const char *p;
 
622
        
 
623
        for (p = s; *p != '\0'; p++) {
 
624
                if ((*p >= 'a' && *p <= 'z') ||
 
625
                    (*p >= 'A' && *p <= 'Z') ||
 
626
                    (*p >= '0' && *p <= '9') ||
 
627
                    strchr ("-_./=:", *p) != NULL)
 
628
                        ;
 
629
                else
 
630
                        return g_shell_quote (s);
 
631
        }
 
632
        
 
633
        return g_strdup (s);
 
634
}
 
635
 
 
636
static void
 
637
panel_run_dialog_append_file_utf8 (PanelRunDialog *dialog,
 
638
                                   const char     *file)
 
639
{
 
640
        const char *text;
 
641
        char       *quoted, *temp;
 
642
        GtkWidget  *entry;
 
643
        
 
644
        /* Don't allow filenames beginning with '-' */
 
645
        if (!file || !file[0] || file[0] == '-')
 
646
                return;
 
647
        
 
648
        quoted = quote_string (file);
 
649
        entry = gtk_bin_get_child (GTK_BIN (dialog->combobox));
 
650
        text = gtk_entry_get_text (GTK_ENTRY (entry));
 
651
        if (text && text [0]) {
 
652
                temp = g_strconcat (text, " ", quoted, NULL);
 
653
                gtk_entry_set_text (GTK_ENTRY (entry), temp);
 
654
                g_free (temp);
 
655
        } else
 
656
                gtk_entry_set_text (GTK_ENTRY (entry), quoted);
 
657
        
 
658
        g_free (quoted);
 
659
}
 
660
 
 
661
static void
 
662
panel_run_dialog_append_file (PanelRunDialog *dialog,
 
663
                              const char *file)
 
664
{
 
665
        char *utf8_file;
 
666
        
 
667
        if (!file)
 
668
                return;
 
669
        
 
670
        utf8_file = g_filename_to_utf8 (file, -1, NULL, NULL, NULL);
 
671
        
 
672
        if (utf8_file)
 
673
                panel_run_dialog_append_file_utf8 (dialog, utf8_file);
 
674
        
 
675
        g_free (utf8_file);
 
676
}
 
677
 
 
678
static gboolean
 
679
fuzzy_command_match (const char *cmd1,
 
680
                     const char *cmd2,
 
681
                     gboolean   *fuzzy)
 
682
{
 
683
        char **tokens;
 
684
        char  *word1, *word2;
 
685
 
 
686
        g_return_val_if_fail (cmd1 && cmd2, TRUE);
 
687
 
 
688
        *fuzzy = FALSE;
 
689
 
 
690
        if (!strcmp (cmd1, cmd2))
 
691
                return TRUE;
 
692
 
 
693
        /* find basename of exec from desktop item.
 
694
           strip of all arguments after the initial command */
 
695
        tokens = g_strsplit (cmd1, " ", -1);
 
696
        if (!tokens || !tokens [0]) {
 
697
                g_strfreev (tokens);
 
698
                return FALSE;
 
699
        }
 
700
 
 
701
        word1 = g_path_get_basename (tokens [0]);
 
702
        g_strfreev (tokens);
 
703
 
 
704
        /* same for the user command */
 
705
        tokens = g_strsplit (cmd2, " ", -1);
 
706
        word2 = g_path_get_basename (tokens [0]);
 
707
        if (!tokens || !tokens [0]) {
 
708
                g_free (word1);
 
709
                g_strfreev (tokens);
 
710
                return FALSE;
 
711
        }
 
712
 
 
713
        g_strfreev (tokens);
 
714
 
 
715
        if (!strcmp (word1, word2)) {
 
716
                g_free (word1);
 
717
                g_free (word2);
 
718
                *fuzzy = TRUE;
 
719
                return TRUE;
 
720
        }
 
721
 
 
722
        g_free (word1);
 
723
        g_free (word2);
 
724
 
 
725
        return FALSE;
 
726
}
 
727
 
 
728
static gboolean
 
729
panel_run_dialog_make_all_list_visible (GtkTreeModel *model,
 
730
                                        GtkTreePath  *path,
 
731
                                        GtkTreeIter  *iter,
 
732
                                        gpointer      data)
 
733
{
 
734
        gtk_list_store_set (GTK_LIST_STORE (model), iter,
 
735
                            COLUMN_VISIBLE, TRUE,
 
736
                            -1);
 
737
 
 
738
        return FALSE;
 
739
}
 
740
 
 
741
static gboolean
 
742
panel_run_dialog_find_command_idle (PanelRunDialog *dialog)
 
743
{
 
744
        GtkTreeIter   iter;
 
745
        GtkTreeModel *model;
 
746
        GtkTreePath  *path;
 
747
        char         *text;
 
748
        GIcon        *found_icon;
 
749
        char         *found_name;
 
750
        gboolean      fuzzy;
 
751
        
 
752
        model = GTK_TREE_MODEL (dialog->program_list_store);
 
753
        path = gtk_tree_path_new_first ();
 
754
        
 
755
        if (!path || !gtk_tree_model_get_iter (model, &iter, path)) {
 
756
                if (path)
 
757
                        gtk_tree_path_free (path);
 
758
                
 
759
                panel_run_dialog_set_icon (dialog, NULL, FALSE);
 
760
        
 
761
                dialog->find_command_idle_id = 0;
 
762
                return FALSE;
 
763
        }
 
764
 
 
765
        text = g_strdup (panel_run_dialog_get_combo_text (dialog));
 
766
        found_icon = NULL;
 
767
        found_name = NULL;
 
768
        fuzzy = FALSE;
 
769
 
 
770
        do {
 
771
                char *exec = NULL;
 
772
                char *name = NULL;
 
773
                char *comment = NULL;
 
774
                GIcon *icon = NULL;
 
775
 
 
776
                gtk_tree_model_get (model, &iter,
 
777
                                    COLUMN_EXEC,    &exec,
 
778
                                    COLUMN_ICON,    &icon,
 
779
                                    COLUMN_NAME,    &name,
 
780
                                    COLUMN_COMMENT, &comment,
 
781
                                    -1);
 
782
 
 
783
                if (!fuzzy && exec && icon &&
 
784
                    fuzzy_command_match (text, exec, &fuzzy)) {
 
785
                        if (found_icon)
 
786
                                g_object_unref (found_icon);
 
787
                        g_free (found_name);
 
788
                        
 
789
                        found_icon = g_object_ref (icon);
 
790
                        found_name = g_strdup (name);
 
791
                        
 
792
                        gtk_list_store_set (dialog->program_list_store,
 
793
                                            &iter,
 
794
                                            COLUMN_VISIBLE, TRUE,
 
795
                                            -1);
 
796
                } else if (panel_g_utf8_strstrcase (exec, text) != NULL ||
 
797
                           panel_g_utf8_strstrcase (name, text) != NULL ||
 
798
                           panel_g_utf8_strstrcase (comment, text) != NULL) {
 
799
                        gtk_list_store_set (dialog->program_list_store,
 
800
                                            &iter,
 
801
                                            COLUMN_VISIBLE, TRUE,
 
802
                                            -1);
 
803
                } else {
 
804
                        gtk_list_store_set (dialog->program_list_store,
 
805
                                            &iter,
 
806
                                            COLUMN_VISIBLE, FALSE,
 
807
                                            -1);
 
808
                }
 
809
 
 
810
                g_free (exec);
 
811
                g_object_unref (icon);
 
812
                g_free (name);
 
813
                g_free (comment);
 
814
        
 
815
        } while (gtk_tree_model_iter_next (model, &iter));
 
816
 
 
817
        if (gtk_tree_model_get_iter (gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->program_list)),
 
818
                                     &iter, path))
 
819
                gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dialog->program_list),
 
820
                                              path, NULL, FALSE, 0, 0);
 
821
 
 
822
        gtk_tree_path_free (path);
 
823
 
 
824
        panel_run_dialog_set_icon (dialog, found_icon, FALSE);
 
825
        //FIXME update dialog->program_label
 
826
 
 
827
        if (found_icon)
 
828
                g_object_unref (found_icon);
 
829
        g_free (text);
 
830
        
 
831
        g_free (dialog->item_name);
 
832
        dialog->item_name = found_name;
 
833
        
 
834
        dialog->find_command_idle_id = 0;
 
835
        return FALSE;
 
836
}
 
837
 
 
838
static int
 
839
compare_applications (GMenuTreeEntry *a,
 
840
                      GMenuTreeEntry *b)
 
841
{
 
842
        return g_utf8_collate (g_app_info_get_display_name ((GAppInfo*)(gmenu_tree_entry_get_app_info (a))),
 
843
                               g_app_info_get_display_name ((GAppInfo*)(gmenu_tree_entry_get_app_info (b))));
 
844
}
 
845
 
 
846
static GSList *get_all_applications_from_dir (GMenuTreeDirectory *directory,
 
847
                                              GSList            *list);
 
848
 
 
849
static GSList *
 
850
get_all_applications_from_alias (GMenuTreeAlias *alias,
 
851
                                 GSList         *list)
 
852
{
 
853
        switch (gmenu_tree_alias_get_aliased_item_type (alias)) {
 
854
        case GMENU_TREE_ITEM_ENTRY:
 
855
                /* pass on the reference */
 
856
                list = g_slist_append (list, gmenu_tree_alias_get_aliased_entry (alias));
 
857
                break;
 
858
 
 
859
        case GMENU_TREE_ITEM_DIRECTORY: {
 
860
                GMenuTreeDirectory *directory = gmenu_tree_alias_get_aliased_directory (alias);
 
861
                list = get_all_applications_from_dir (directory, list);
 
862
                gmenu_tree_item_unref (directory);
 
863
                break;
 
864
        }
 
865
 
 
866
        default:
 
867
                break;
 
868
        }
 
869
 
 
870
        return list;
 
871
}
 
872
 
 
873
static GSList *
 
874
get_all_applications_from_dir (GMenuTreeDirectory *directory,
 
875
                               GSList             *list)
 
876
{
 
877
        GMenuTreeIter *iter;
 
878
        GMenuTreeItemType next_type;
 
879
 
 
880
        iter = gmenu_tree_directory_iter (directory);
 
881
 
 
882
        while ((next_type = gmenu_tree_iter_next (iter)) != GMENU_TREE_ITEM_INVALID) {
 
883
                switch (next_type) {
 
884
                case GMENU_TREE_ITEM_ENTRY:
 
885
                        list = g_slist_append (list, gmenu_tree_iter_get_entry (iter));
 
886
                        break;
 
887
 
 
888
                case GMENU_TREE_ITEM_DIRECTORY: {
 
889
                        GMenuTreeDirectory *dir = gmenu_tree_iter_get_directory (iter);
 
890
                        list = get_all_applications_from_dir (dir, list);
 
891
                        gmenu_tree_item_unref (dir);
 
892
                        break;
 
893
                }
 
894
 
 
895
                case GMENU_TREE_ITEM_ALIAS: {
 
896
                        GMenuTreeAlias *alias = gmenu_tree_iter_get_alias (iter);
 
897
                        list = get_all_applications_from_alias (alias, list);
 
898
                        gmenu_tree_item_unref (alias);
 
899
                        break;
 
900
                }
 
901
 
 
902
                default:
 
903
                        break;
 
904
                }
 
905
        }
 
906
 
 
907
        gmenu_tree_iter_unref (iter);
 
908
 
 
909
        return list;
 
910
}
 
911
 
 
912
static GSList *
 
913
get_all_applications (void)
 
914
{
 
915
        GMenuTree          *tree;
 
916
        GMenuTreeDirectory *root;
 
917
        GSList             *retval;
 
918
 
 
919
        tree = gmenu_tree_new ("applications.menu", GMENU_TREE_FLAGS_SORT_DISPLAY_NAME);
 
920
 
 
921
        if (!gmenu_tree_load_sync (tree, NULL))
 
922
                return NULL;
 
923
 
 
924
        root = gmenu_tree_get_root_directory (tree);
 
925
 
 
926
        retval = get_all_applications_from_dir (root, NULL);
 
927
 
 
928
        gmenu_tree_item_unref (root);
 
929
        g_object_unref (tree);
 
930
 
 
931
        retval = g_slist_sort (retval,
 
932
                               (GCompareFunc) compare_applications);
 
933
 
 
934
        return retval;
 
935
}
 
936
 
 
937
static gboolean
 
938
panel_run_dialog_add_items_idle (PanelRunDialog *dialog)
 
939
{
 
940
        GtkCellRenderer   *renderer;
 
941
        GtkTreeViewColumn *column;
 
942
        GtkTreeModel      *model_filter;
 
943
        GSList            *all_applications;
 
944
        GSList            *l;
 
945
        GSList            *next;
 
946
        const char        *prev_name;
 
947
 
 
948
        /* create list store */
 
949
        dialog->program_list_store = gtk_list_store_new (NUM_COLUMNS,
 
950
                                                         G_TYPE_ICON,
 
951
                                                         G_TYPE_STRING,
 
952
                                                         G_TYPE_STRING,
 
953
                                                         G_TYPE_STRING,
 
954
                                                         G_TYPE_STRING,
 
955
                                                         G_TYPE_BOOLEAN);
 
956
 
 
957
        all_applications = get_all_applications ();
 
958
        
 
959
        /* Strip duplicates */
 
960
        prev_name = NULL;
 
961
        for (l = all_applications; l; l = next) {
 
962
                GMenuTreeEntry  *entry = l->data;
 
963
                const char      *entry_name;
 
964
                GDesktopAppInfo *app_info;
 
965
 
 
966
 
 
967
                next = l->next;
 
968
                app_info = gmenu_tree_entry_get_app_info (entry);
 
969
 
 
970
                entry_name = g_app_info_get_display_name (G_APP_INFO (app_info));
 
971
                if (prev_name && entry_name && strcmp (entry_name, prev_name) == 0) {
 
972
                        gmenu_tree_item_unref (entry);
 
973
 
 
974
                        all_applications = g_slist_delete_link (all_applications, l);
 
975
                } else {
 
976
                        prev_name = entry_name;
 
977
                }
 
978
        }
 
979
 
 
980
        for (l = all_applications; l; l = l->next) {
 
981
                GMenuTreeEntry *entry = l->data;
 
982
                GtkTreeIter     iter;
 
983
                GtkTreePath    *path;
 
984
                GAppInfo       *app_info;
 
985
 
 
986
                app_info = G_APP_INFO (gmenu_tree_entry_get_app_info (entry));
 
987
 
 
988
                gtk_list_store_append (dialog->program_list_store, &iter);
 
989
                gtk_list_store_set (dialog->program_list_store, &iter,
 
990
                                    COLUMN_ICON,    g_app_info_get_icon (app_info),
 
991
                                    COLUMN_NAME,    g_app_info_get_display_name (app_info),
 
992
                                    COLUMN_COMMENT, g_app_info_get_description (app_info),
 
993
                                    COLUMN_EXEC,    g_app_info_get_executable (app_info),
 
994
                                    COLUMN_PATH,    gmenu_tree_entry_get_desktop_file_path (entry),
 
995
                                    COLUMN_VISIBLE, TRUE,
 
996
                                    -1);
 
997
 
 
998
                path = gtk_tree_model_get_path (GTK_TREE_MODEL (dialog->program_list_store), &iter);
 
999
                if (path != NULL)
 
1000
                        dialog->add_icon_paths = g_slist_prepend (dialog->add_icon_paths, path);
 
1001
 
 
1002
                gmenu_tree_item_unref (entry);
 
1003
        }
 
1004
        g_slist_free (all_applications);
 
1005
 
 
1006
        model_filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (dialog->program_list_store),
 
1007
                                                  NULL);
 
1008
        gtk_tree_model_filter_set_visible_column (GTK_TREE_MODEL_FILTER (model_filter),
 
1009
                                                  COLUMN_VISIBLE);
 
1010
 
 
1011
        gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->program_list), 
 
1012
                                 model_filter);
 
1013
        //FIXME use the same search than the fuzzy one?
 
1014
        gtk_tree_view_set_search_column (GTK_TREE_VIEW (dialog->program_list),
 
1015
                                         COLUMN_NAME);
 
1016
 
 
1017
        renderer = gtk_cell_renderer_pixbuf_new ();
 
1018
        column = gtk_tree_view_column_new ();
 
1019
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
 
1020
        gtk_tree_view_column_set_attributes (column, renderer,
 
1021
                                             "gicon", COLUMN_ICON,
 
1022
                                             NULL);
 
1023
        
 
1024
        renderer = gtk_cell_renderer_text_new ();
 
1025
        gtk_tree_view_column_pack_start (column, renderer, TRUE);
 
1026
        gtk_tree_view_column_set_attributes (column, renderer,
 
1027
                                             "text", COLUMN_NAME,
 
1028
                                             NULL);
 
1029
                                                  
 
1030
        gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->program_list), column);
 
1031
 
 
1032
        dialog->add_icon_paths = g_slist_reverse (dialog->add_icon_paths);
 
1033
 
 
1034
        dialog->add_items_idle_id = 0;                                   
 
1035
        return FALSE;
 
1036
}
 
1037
 
 
1038
static char *
 
1039
remove_parameters (const char *exec)
 
1040
{
 
1041
        GString *str;
 
1042
        char    *retval, *p;
 
1043
 
 
1044
        str = g_string_new (exec);
 
1045
 
 
1046
        while ((p = strstr (str->str, "%"))) {
 
1047
                switch (p [1]) {
 
1048
                case '%':
 
1049
                        g_string_erase (str, p - str->str, 1);
 
1050
                        break;
 
1051
                case 'U':
 
1052
                case 'F':
 
1053
                case 'N':
 
1054
                case 'D':
 
1055
                case 'f':
 
1056
                case 'u':
 
1057
                case 'd':
 
1058
                case 'n':
 
1059
                case 'm':
 
1060
                case 'i':
 
1061
                case 'c':
 
1062
                case 'k':
 
1063
                case 'v':
 
1064
                        g_string_erase (str, p - str->str, 2);
 
1065
                        break;
 
1066
                default:
 
1067
                        break;
 
1068
                }
 
1069
        }
 
1070
 
 
1071
        retval = str->str;
 
1072
        g_string_free (str, FALSE);
 
1073
 
 
1074
        return retval;
 
1075
}
 
1076
 
 
1077
static void
 
1078
program_list_selection_changed (GtkTreeSelection *selection,
 
1079
                                PanelRunDialog   *dialog)
 
1080
{
 
1081
        GtkTreeModel *filter_model;
 
1082
        GtkTreeModel *child_model;
 
1083
        GtkTreeIter   iter;
 
1084
        GtkTreeIter   filter_iter;
 
1085
        char         *temp;
 
1086
        char         *path, *stripped;
 
1087
        gboolean      terminal;
 
1088
        GKeyFile     *key_file;
 
1089
        GtkWidget    *entry;
 
1090
 
 
1091
        if (!gtk_tree_selection_get_selected (selection, &filter_model,
 
1092
                                              &filter_iter))
 
1093
                return;
 
1094
 
 
1095
        gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (filter_model),
 
1096
                                                          &iter, &filter_iter);
 
1097
 
 
1098
        path = NULL;
 
1099
        child_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter_model));
 
1100
        gtk_tree_model_get (child_model, &iter,
 
1101
                            COLUMN_PATH, &path,
 
1102
                            -1);
 
1103
                                  
 
1104
        if (!path)
 
1105
                return;
 
1106
 
 
1107
        key_file = g_key_file_new ();
 
1108
 
 
1109
        if (!g_key_file_load_from_file (key_file, path,
 
1110
                                        G_KEY_FILE_NONE, NULL)) {
 
1111
                g_key_file_free (key_file);
 
1112
                g_free (path);
 
1113
                return;
 
1114
        }
 
1115
 
 
1116
        dialog->use_program_list = TRUE;
 
1117
        if (dialog->desktop_path)
 
1118
                g_free (dialog->desktop_path);
 
1119
        dialog->desktop_path = g_strdup (path);
 
1120
        if (dialog->item_name)
 
1121
                g_free (dialog->item_name);
 
1122
        dialog->item_name = NULL;
 
1123
 
 
1124
        /* Order is important here. We have to set the text first so that the
 
1125
         * drag source is enabled, otherwise the drag icon can't be set by
 
1126
         * panel_run_dialog_set_icon.
 
1127
         */
 
1128
        entry = gtk_bin_get_child (GTK_BIN (dialog->combobox));
 
1129
        temp = panel_key_file_get_string (key_file, "Exec");
 
1130
        if (temp) {
 
1131
                stripped = remove_parameters (temp);
 
1132
                gtk_entry_set_text (GTK_ENTRY (entry), stripped);
 
1133
                g_free (stripped);
 
1134
        } else {
 
1135
                temp = panel_key_file_get_string (key_file, "URL");
 
1136
                gtk_entry_set_text (GTK_ENTRY (entry), sure_string (temp));
 
1137
        }
 
1138
        g_free (temp);
 
1139
 
 
1140
        temp = panel_key_file_get_locale_string (key_file, "Icon");
 
1141
        if (!PANEL_GLIB_STR_EMPTY (temp)) {
 
1142
                GIcon *gicon;
 
1143
 
 
1144
                stripped = panel_xdg_icon_remove_extension (temp);
 
1145
                gicon = g_themed_icon_new (stripped);
 
1146
                panel_run_dialog_set_icon (dialog, gicon, FALSE);
 
1147
                g_object_unref (gicon);
 
1148
                g_free (stripped);
 
1149
        } else {
 
1150
                panel_run_dialog_set_icon (dialog, NULL, FALSE);
 
1151
        }
 
1152
        g_free (temp);
 
1153
        
 
1154
        temp = panel_key_file_get_locale_string (key_file, "Comment");
 
1155
        //FIXME: if sure_string () == "", we should display "Will run..." as in entry_changed()
 
1156
        gtk_label_set_text (GTK_LABEL (dialog->program_label),
 
1157
                            sure_string (temp));
 
1158
        g_free (temp);
 
1159
 
 
1160
        terminal = panel_key_file_get_boolean (key_file, "Terminal", FALSE);
 
1161
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->terminal_checkbox),
 
1162
                                      terminal);
 
1163
 
 
1164
        g_key_file_free (key_file);
 
1165
 
 
1166
        g_free (path);
 
1167
}
 
1168
 
 
1169
static void
 
1170
program_list_selection_activated (GtkTreeView       *view,
 
1171
                                  GtkTreePath       *path,
 
1172
                                  GtkTreeViewColumn *column,
 
1173
                                  PanelRunDialog    *dialog)
 
1174
{
 
1175
        GtkTreeSelection *selection;
 
1176
 
 
1177
        /* update the entry with the info from the selection */
 
1178
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->program_list)); 
 
1179
        program_list_selection_changed (selection, dialog);
 
1180
        
 
1181
        /* now launch the command */
 
1182
        gtk_dialog_response (GTK_DIALOG (dialog->run_dialog), GTK_RESPONSE_OK);
 
1183
}
 
1184
 
 
1185
 
 
1186
static void
 
1187
panel_run_dialog_setup_program_list (PanelRunDialog *dialog,
 
1188
                                     GtkBuilder     *gui)
 
1189
{
 
1190
        GtkTreeSelection *selection;
 
1191
        
 
1192
        dialog->program_list = PANEL_GTK_BUILDER_GET (gui, "program_list");
 
1193
        dialog->program_label = PANEL_GTK_BUILDER_GET (gui, "program_label");
 
1194
        dialog->main_box = PANEL_GTK_BUILDER_GET (gui, "main_box");
 
1195
        
 
1196
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->program_list));
 
1197
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
 
1198
 
 
1199
        g_signal_connect (selection, "changed",
 
1200
                          G_CALLBACK (program_list_selection_changed),
 
1201
                          dialog);
 
1202
 
 
1203
        g_signal_connect (dialog->program_list, "row-activated",
 
1204
                          G_CALLBACK (program_list_selection_activated),
 
1205
                          dialog);
 
1206
}
 
1207
 
 
1208
static void
 
1209
panel_run_dialog_setup_list_expander (PanelRunDialog *dialog,
 
1210
                                      GtkBuilder     *gui)
 
1211
{
 
1212
        dialog->list_expander = PANEL_GTK_BUILDER_GET (gui, "list_expander");
 
1213
 
 
1214
        /* Ref the expander so it doesn't get destroyed when it is
 
1215
         * removed from the visible area of the dialog box. */
 
1216
        g_object_ref (dialog->list_expander);
 
1217
 
 
1218
        g_settings_bind (dialog->run_settings,
 
1219
                         PANEL_RUN_SHOW_LIST_KEY,
 
1220
                         dialog->list_expander,
 
1221
                         "expanded",
 
1222
                         G_SETTINGS_BIND_DEFAULT);
 
1223
}
 
1224
 
 
1225
static void
 
1226
panel_run_dialog_update_program_list (GSettings      *settings,
 
1227
                                      char           *key,
 
1228
                                      PanelRunDialog *dialog)
 
1229
{
 
1230
        gboolean   enabled;
 
1231
        gboolean   shown;
 
1232
        GtkWidget *parent;
 
1233
 
 
1234
        enabled = g_settings_get_boolean (dialog->run_settings,
 
1235
                                          PANEL_RUN_ENABLE_LIST_KEY);
 
1236
 
 
1237
        parent = gtk_widget_get_parent (dialog->list_expander);
 
1238
 
 
1239
        if (enabled) {
 
1240
                if (dialog->program_list_store == NULL) {
 
1241
                        /* start loading the list of applications */
 
1242
                        dialog->add_items_idle_id =
 
1243
                                g_idle_add_full (G_PRIORITY_LOW,
 
1244
                                                 (GSourceFunc) panel_run_dialog_add_items_idle,
 
1245
                                                 dialog, NULL);
 
1246
                }
 
1247
 
 
1248
                if (!parent)
 
1249
                        gtk_box_pack_end (GTK_BOX (dialog->main_box),
 
1250
                                          dialog->list_expander,
 
1251
                                          TRUE, TRUE, 0);
 
1252
        } else {
 
1253
                if (parent)
 
1254
                        gtk_container_remove (GTK_CONTAINER (parent),
 
1255
                                              dialog->list_expander);
 
1256
        }
 
1257
 
 
1258
        shown = g_settings_get_boolean (dialog->run_settings,
 
1259
                                        PANEL_RUN_SHOW_LIST_KEY);
 
1260
 
 
1261
        if (enabled && shown) {
 
1262
                gtk_window_resize (GTK_WINDOW (dialog->run_dialog), 100, 300);
 
1263
                gtk_window_set_resizable (GTK_WINDOW (dialog->run_dialog), TRUE);
 
1264
                gtk_widget_grab_focus (dialog->program_list);
 
1265
        } else {
 
1266
                gtk_window_set_resizable (GTK_WINDOW (dialog->run_dialog), FALSE);
 
1267
                gtk_widget_grab_focus (dialog->combobox);
 
1268
        }
 
1269
}
 
1270
 
 
1271
static void
 
1272
file_button_browse_response (GtkWidget      *chooser,
 
1273
                             gint            response,
 
1274
                             PanelRunDialog *dialog)
 
1275
{
 
1276
        char *file;
 
1277
        
 
1278
        if (response == GTK_RESPONSE_OK) {
 
1279
                file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
 
1280
                panel_run_dialog_append_file (dialog, file);
 
1281
                g_free (file);
 
1282
        }
 
1283
 
 
1284
        gtk_widget_destroy (chooser);
 
1285
 
 
1286
        gtk_widget_grab_focus (dialog->combobox);
 
1287
}
 
1288
 
 
1289
static void
 
1290
file_button_clicked (GtkButton      *button,
 
1291
                     PanelRunDialog *dialog)
 
1292
{
 
1293
        GtkWidget *chooser;
 
1294
 
 
1295
        chooser = gtk_file_chooser_dialog_new (_("Choose a file to append to the command..."),
 
1296
                                               GTK_WINDOW (dialog->run_dialog),
 
1297
                                               GTK_FILE_CHOOSER_ACTION_OPEN,
 
1298
                                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
1299
                                               GTK_STOCK_OK, GTK_RESPONSE_OK,
 
1300
                                               NULL);
 
1301
        
 
1302
        gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
 
1303
                                             g_get_home_dir ());
 
1304
        
 
1305
        gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_OK);
 
1306
        gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser), TRUE);
 
1307
 
 
1308
        g_signal_connect (chooser, "response",
 
1309
                          G_CALLBACK (file_button_browse_response), dialog);
 
1310
 
 
1311
        gtk_window_present (GTK_WINDOW (chooser));
 
1312
}
 
1313
 
 
1314
static void
 
1315
panel_run_dialog_setup_file_button (PanelRunDialog *dialog,
 
1316
                                    GtkBuilder     *gui)
 
1317
{
 
1318
        dialog->file_button = PANEL_GTK_BUILDER_GET (gui, "file_button");
 
1319
                
 
1320
        g_signal_connect (dialog->file_button, "clicked",
 
1321
                          G_CALLBACK (file_button_clicked),
 
1322
                          dialog);
 
1323
}
 
1324
 
 
1325
static GList *
 
1326
fill_files_from (const char *dirname,
 
1327
                 const char *dirprefix,
 
1328
                 char        prefix,
 
1329
                 GList      *existing_items)
 
1330
{
 
1331
        GList         *list;
 
1332
        DIR           *dir;
 
1333
        struct dirent *dent;
 
1334
        
 
1335
        list = NULL;
 
1336
        dir = opendir (dirname);
 
1337
        
 
1338
        if (!dir)
 
1339
                return list;
 
1340
        
 
1341
        while ((dent = readdir (dir))) {
 
1342
                char       *file;
 
1343
                char       *item;
 
1344
                const char *suffix;
 
1345
                
 
1346
                if (!dent->d_name ||
 
1347
                    dent->d_name [0] != prefix)
 
1348
                        continue;
 
1349
 
 
1350
                file = g_build_filename (dirname, dent->d_name, NULL);
 
1351
                
 
1352
                suffix = NULL;
 
1353
                if (
 
1354
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
 
1355
                /* don't use g_file_test at first so we don't stat() */
 
1356
                    dent->d_type == DT_DIR ||
 
1357
                    (dent->d_type == DT_LNK &&
 
1358
                     g_file_test (file, G_FILE_TEST_IS_DIR))
 
1359
#else
 
1360
                    g_file_test (file, G_FILE_TEST_IS_DIR)
 
1361
#endif
 
1362
                   )
 
1363
                        suffix = "/";
 
1364
                
 
1365
                g_free (file);
 
1366
                
 
1367
                item = g_build_filename (dirprefix, dent->d_name, suffix, NULL);
 
1368
                
 
1369
                list = g_list_prepend (list, item);
 
1370
        }
 
1371
 
 
1372
        closedir (dir);
 
1373
        
 
1374
        return list;
 
1375
}       
 
1376
 
 
1377
static GList *
 
1378
fill_possible_executables (void)
 
1379
{
 
1380
        GList         *list;
 
1381
        const char    *path;
 
1382
        char         **pathv;
 
1383
        int            i;
 
1384
        
 
1385
        list = NULL;
 
1386
        path = g_getenv ("PATH");
 
1387
 
 
1388
        if (!path || !path [0])
 
1389
                return list;
 
1390
 
 
1391
        pathv = g_strsplit (path, ":", 0);
 
1392
        
 
1393
        for (i = 0; pathv [i]; i++) {
 
1394
                const char *file;
 
1395
                char       *filename;
 
1396
                GDir       *dir;
 
1397
 
 
1398
                dir = g_dir_open (pathv [i], 0, NULL);
 
1399
 
 
1400
                if (!dir)
 
1401
                        continue;
 
1402
 
 
1403
                while ((file = g_dir_read_name (dir))) {
 
1404
                        filename = g_build_filename (pathv [i], file, NULL);
 
1405
                        list = g_list_prepend (list, filename);
 
1406
                }
 
1407
 
 
1408
                g_dir_close (dir);
 
1409
        }
 
1410
        
 
1411
        g_strfreev (pathv);
 
1412
        
 
1413
        return list;
 
1414
}
 
1415
 
 
1416
static GList *
 
1417
fill_executables (GList *possible_executables,
 
1418
                  GList *existing_items,
 
1419
                  char   prefix)
 
1420
{
 
1421
        GList *list;
 
1422
        GList *l;
 
1423
        
 
1424
        list = NULL;    
 
1425
        
 
1426
        for (l = possible_executables; l; l = l->next) {
 
1427
                const char *filename;
 
1428
                char       *basename;
 
1429
                        
 
1430
                filename = l->data;
 
1431
                basename = g_path_get_basename (filename);
 
1432
                        
 
1433
                if (basename [0] == prefix &&
 
1434
                    g_file_test (filename, G_FILE_TEST_IS_REGULAR) &&
 
1435
                    g_file_test (filename, G_FILE_TEST_IS_EXECUTABLE)) {
 
1436
                            
 
1437
                        if (g_list_find_custom (existing_items, basename,
 
1438
                                                (GCompareFunc) strcmp)) {
 
1439
                                g_free (basename);
 
1440
                                return NULL;
 
1441
                        }
 
1442
 
 
1443
                        list = g_list_prepend (list, basename);
 
1444
                 } else {
 
1445
                        g_free (basename);
 
1446
                 }
 
1447
        }
 
1448
        
 
1449
        return list;
 
1450
}
 
1451
 
 
1452
static void
 
1453
panel_run_dialog_update_completion (PanelRunDialog *dialog,
 
1454
                                    const char     *text)
 
1455
{
 
1456
        GList *list;
 
1457
        GList *executables;
 
1458
        char   prefix;
 
1459
        char  *buf;
 
1460
        char  *dirname;
 
1461
        char  *dirprefix;
 
1462
        char  *key;
 
1463
 
 
1464
        g_assert (text != NULL && *text != '\0' && !g_ascii_isspace (*text));
 
1465
 
 
1466
        list = NULL;
 
1467
        executables = NULL;
 
1468
 
 
1469
        if (!dialog->completion) {
 
1470
                dialog->completion = g_completion_new (NULL);
 
1471
                dialog->possible_executables = fill_possible_executables ();
 
1472
                dialog->dir_hash = g_hash_table_new_full (g_str_hash,
 
1473
                                                          g_str_equal,
 
1474
                                                          g_free, NULL);
 
1475
        }
 
1476
        
 
1477
        buf = g_path_get_basename (text);
 
1478
        prefix = buf[0];
 
1479
        g_free (buf);
 
1480
        if (prefix == '/' || prefix == '.')
 
1481
                return;
 
1482
 
 
1483
        if (text [0] == '/') {
 
1484
                /* complete against absolute path */
 
1485
                dirname = g_path_get_dirname (text);
 
1486
                dirprefix = g_strdup (dirname);
 
1487
        } else {
 
1488
                /* complete against relative path and executable name */
 
1489
                if (!strchr (text, '/')) {
 
1490
                        executables = fill_executables (dialog->possible_executables,
 
1491
                                                        dialog->completion_items,
 
1492
                                                        text [0]);
 
1493
                        dirprefix = g_strdup ("");
 
1494
                } else {
 
1495
                        dirprefix = g_path_get_dirname (text);
 
1496
                }
 
1497
 
 
1498
                dirname = g_build_filename (g_get_home_dir (), dirprefix, NULL);
 
1499
        }
 
1500
 
 
1501
        key = g_strdup_printf ("%s%c%c", dirprefix, G_DIR_SEPARATOR, prefix);
 
1502
 
 
1503
        if (!g_hash_table_lookup (dialog->dir_hash, key)) {
 
1504
                g_hash_table_insert (dialog->dir_hash, key, dialog);
 
1505
 
 
1506
                list = fill_files_from (dirname, dirprefix, prefix,
 
1507
                                        dialog->completion_items);
 
1508
        } else {
 
1509
                g_free (key);
 
1510
        }
 
1511
 
 
1512
        list = g_list_concat (list, executables);
 
1513
 
 
1514
        g_free (dirname);
 
1515
        g_free (dirprefix);
 
1516
 
 
1517
        if (list == NULL)
 
1518
                return;
 
1519
                
 
1520
        g_completion_add_items (dialog->completion, list);
 
1521
                
 
1522
        dialog->completion_items = g_list_concat (dialog->completion_items,
 
1523
                                                  list);        
 
1524
}
 
1525
 
 
1526
static gboolean
 
1527
entry_event (GtkEditable    *entry,
 
1528
             GdkEventKey    *event,
 
1529
             PanelRunDialog *dialog)
 
1530
{
 
1531
        GtkTreeSelection *selection;
 
1532
        char             *prefix;
 
1533
        char             *nospace_prefix;
 
1534
        char             *nprefix;
 
1535
        char             *temp;
 
1536
        int               pos, tmp;
 
1537
 
 
1538
        if (event->type != GDK_KEY_PRESS)
 
1539
                return FALSE;
 
1540
 
 
1541
        /* if user typed something we're not using the list anymore */
 
1542
        dialog->use_program_list = FALSE;
 
1543
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->program_list));
 
1544
        gtk_tree_selection_unselect_all (selection);
 
1545
 
 
1546
        if (!g_settings_get_boolean (dialog->run_settings,
 
1547
                                     PANEL_RUN_ENABLE_COMPLETION_KEY))
 
1548
                return FALSE;
 
1549
 
 
1550
        /* tab completion */
 
1551
        if (event->keyval == GDK_KEY_Tab) {
 
1552
                gtk_editable_get_selection_bounds (entry, &pos, &tmp);
 
1553
 
 
1554
                if (dialog->completion_started &&
 
1555
                    pos != tmp &&
 
1556
                    pos != 1 &&
 
1557
                    tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
1558
                        gtk_editable_select_region (entry, 0, 0);               
 
1559
                        gtk_editable_set_position (entry, -1);
 
1560
                        
 
1561
                        return TRUE;
 
1562
                }
 
1563
        } else if (event->length > 0) {
 
1564
                           
 
1565
                gtk_editable_get_selection_bounds (entry, &pos, &tmp);
 
1566
 
 
1567
                if (dialog->completion_started &&
 
1568
                    pos != tmp &&
 
1569
                    pos != 0 &&
 
1570
                    tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
1571
                        temp = gtk_editable_get_chars (entry, 0, pos);
 
1572
                        prefix = g_strconcat (temp, event->string, NULL);
 
1573
                        g_free (temp);
 
1574
                } else if (pos == tmp &&
 
1575
                           tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
1576
                        prefix = g_strconcat (gtk_entry_get_text (GTK_ENTRY (entry)),
 
1577
                                              event->string, NULL);
 
1578
                } else {
 
1579
                        return FALSE;
 
1580
                }
 
1581
                
 
1582
                nospace_prefix = prefix;
 
1583
                while (*nospace_prefix != '\0' &&
 
1584
                       g_ascii_isspace (*nospace_prefix))
 
1585
                        nospace_prefix++;
 
1586
                if (*nospace_prefix == '\0')
 
1587
                        return FALSE;
 
1588
 
 
1589
                panel_run_dialog_update_completion (dialog, nospace_prefix);
 
1590
                
 
1591
                if (!dialog->completion) {
 
1592
                        g_free (prefix);
 
1593
                        return FALSE;
 
1594
                }
 
1595
                
 
1596
                pos = strlen (prefix);
 
1597
                nprefix = NULL;
 
1598
 
 
1599
                g_completion_complete_utf8 (dialog->completion, nospace_prefix,
 
1600
                                            &nprefix);
 
1601
 
 
1602
                if (nprefix) {
 
1603
                        int insertpos;
 
1604
                        insertpos = 0;
 
1605
 
 
1606
                        temp = g_strndup (prefix, nospace_prefix - prefix);
 
1607
                        g_free (prefix);
 
1608
 
 
1609
                        prefix = g_strconcat (temp, nprefix, NULL);
 
1610
 
 
1611
                        g_signal_handler_block (dialog->combobox,
 
1612
                                                dialog->changed_id);
 
1613
                        gtk_editable_delete_text (entry, 0, -1);
 
1614
                        g_signal_handler_unblock (dialog->combobox,
 
1615
                                                  dialog->changed_id);
 
1616
 
 
1617
                        gtk_editable_insert_text (entry,
 
1618
                                                  prefix, strlen (prefix),
 
1619
                                                  &insertpos);
 
1620
 
 
1621
                        gtk_editable_set_position (entry, pos);
 
1622
                        gtk_editable_select_region (entry, pos, -1);
 
1623
                        
 
1624
                        dialog->completion_started = TRUE;
 
1625
 
 
1626
                        g_free (temp);
 
1627
                        g_free (nprefix);
 
1628
                        g_free (prefix);
 
1629
                        
 
1630
                        return TRUE;
 
1631
                }
 
1632
                
 
1633
                g_free (prefix);
 
1634
        }
 
1635
        
 
1636
        return FALSE;
 
1637
}
 
1638
 
 
1639
static void
 
1640
combobox_changed (GtkComboBox    *combobox,
 
1641
                  PanelRunDialog *dialog)
 
1642
{
 
1643
        gboolean  program_list_enabled;
 
1644
        char     *text;
 
1645
        char     *start;
 
1646
        char     *msg;
 
1647
 
 
1648
        program_list_enabled = g_settings_get_boolean (dialog->run_settings,
 
1649
                                                       PANEL_RUN_ENABLE_LIST_KEY);
 
1650
 
 
1651
        text = g_strdup (panel_run_dialog_get_combo_text (dialog));
 
1652
 
 
1653
        start = text;
 
1654
        while (*start != '\0' && g_ascii_isspace (*start))
 
1655
                start++;
 
1656
 
 
1657
        /* update item name to use for dnd */
 
1658
        if (!dialog->use_program_list) {
 
1659
                if (dialog->desktop_path) {
 
1660
                        g_free (dialog->desktop_path);
 
1661
                        dialog->desktop_path = NULL;
 
1662
                }
 
1663
                if (dialog->item_name) {
 
1664
                        g_free (dialog->item_name);
 
1665
                        dialog->item_name = NULL;
 
1666
                }
 
1667
        }
 
1668
 
 
1669
        /* desensitize run button if no text entered */
 
1670
        if (!start || !start [0]) {
 
1671
                g_free (text);
 
1672
 
 
1673
                gtk_widget_set_sensitive (dialog->run_button, FALSE);
 
1674
                gtk_drag_source_unset (dialog->run_dialog);
 
1675
 
 
1676
                if (program_list_enabled)
 
1677
                        gtk_label_set_text (GTK_LABEL (dialog->program_label),
 
1678
                                            _("Select an application to view its description."));
 
1679
 
 
1680
                panel_run_dialog_set_default_icon (dialog, FALSE);
 
1681
 
 
1682
                if (dialog->find_command_idle_id) {
 
1683
                        g_source_remove (dialog->find_command_idle_id);
 
1684
                        dialog->find_command_idle_id = 0;
 
1685
                }
 
1686
 
 
1687
                if (program_list_enabled) {
 
1688
                        GtkTreeIter  iter;
 
1689
                        GtkTreePath *path;
 
1690
 
 
1691
                        gtk_tree_model_foreach (GTK_TREE_MODEL (dialog->program_list_store),
 
1692
                                                panel_run_dialog_make_all_list_visible,
 
1693
                                                NULL);
 
1694
 
 
1695
                        path = gtk_tree_path_new_first ();
 
1696
                        if (gtk_tree_model_get_iter (gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->program_list)),
 
1697
                                                     &iter, path))
 
1698
                                gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dialog->program_list),
 
1699
                                                              path, NULL,
 
1700
                                                              FALSE, 0, 0);
 
1701
                        gtk_tree_path_free (path);
 
1702
                }
 
1703
 
 
1704
                return;
 
1705
        }
 
1706
 
 
1707
        gtk_widget_set_sensitive (dialog->run_button, TRUE);
 
1708
        gtk_drag_source_set (dialog->run_dialog,
 
1709
                             GDK_BUTTON1_MASK,
 
1710
                             NULL, 0,
 
1711
                             GDK_ACTION_COPY);
 
1712
        gtk_drag_source_add_uri_targets (dialog->run_dialog);
 
1713
 
 
1714
        if (program_list_enabled &&
 
1715
            !dialog->use_program_list) {
 
1716
                msg = g_strdup_printf (_("Will run command: '%s'"),
 
1717
                                       start);
 
1718
                gtk_label_set_text (GTK_LABEL (dialog->program_label), msg);
 
1719
                g_free (msg);
 
1720
        }
 
1721
        
 
1722
        /* look up icon for the command */
 
1723
        if (program_list_enabled &&
 
1724
            !dialog->use_program_list &&
 
1725
            !dialog->find_command_idle_id)
 
1726
                dialog->find_command_idle_id =
 
1727
                        g_idle_add_full (G_PRIORITY_LOW,
 
1728
                                         (GSourceFunc) panel_run_dialog_find_command_idle,
 
1729
                                         dialog, NULL);
 
1730
 
 
1731
        g_free (text);
 
1732
}
 
1733
 
 
1734
static void
 
1735
entry_drag_data_received (GtkEditable      *entry,
 
1736
                          GdkDragContext   *context,
 
1737
                          gint              x,
 
1738
                          gint              y,
 
1739
                          GtkSelectionData *selection_data,
 
1740
                          guint             info,
 
1741
                          guint32           time,
 
1742
                          PanelRunDialog   *dialog)
 
1743
{
 
1744
        char **uris;
 
1745
        char  *file;
 
1746
        int    i;
 
1747
 
 
1748
        if (gtk_selection_data_get_format (selection_data) != 8 || gtk_selection_data_get_length (selection_data) == 0) {
 
1749
                g_warning (_("URI list dropped on run dialog had wrong format (%d) or length (%d)\n"),
 
1750
                           gtk_selection_data_get_format (selection_data),
 
1751
                           gtk_selection_data_get_length (selection_data));
 
1752
                return;
 
1753
        }
 
1754
 
 
1755
        uris = g_uri_list_extract_uris ((const char *)gtk_selection_data_get_data (selection_data));
 
1756
 
 
1757
        if (!uris) {
 
1758
                gtk_drag_finish (context, FALSE, FALSE, time);
 
1759
                return;
 
1760
        }
 
1761
 
 
1762
        for (i = 0; uris [i]; i++) {
 
1763
                if (!uris [i] || !uris [i][0])
 
1764
                        continue;
 
1765
                
 
1766
                file = g_filename_from_uri (uris [i], NULL, NULL);
 
1767
 
 
1768
                /* FIXME: I assume the file is in utf8 encoding if coming from a URI? */
 
1769
                if (file) {
 
1770
                        panel_run_dialog_append_file_utf8 (dialog, file);
 
1771
                        g_free (file);
 
1772
                } else
 
1773
                        panel_run_dialog_append_file_utf8 (dialog, uris [i]);
 
1774
        }
 
1775
 
 
1776
        g_strfreev (uris);
 
1777
        gtk_drag_finish (context, TRUE, FALSE, time);
 
1778
}
 
1779
 
 
1780
static void
 
1781
panel_run_dialog_setup_entry (PanelRunDialog *dialog,
 
1782
                              GtkBuilder     *gui)
 
1783
{
 
1784
        GdkScreen             *screen;
 
1785
        int                    width_request;
 
1786
        GtkWidget             *entry;
 
1787
        
 
1788
        dialog->combobox = PANEL_GTK_BUILDER_GET (gui, "comboboxentry");
 
1789
 
 
1790
        entry = gtk_bin_get_child (GTK_BIN (dialog->combobox));
 
1791
        gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
 
1792
 
 
1793
        gtk_combo_box_set_model (GTK_COMBO_BOX (dialog->combobox),
 
1794
                                 _panel_run_get_recent_programs_list (dialog));
 
1795
        gtk_combo_box_set_entry_text_column
 
1796
                (GTK_COMBO_BOX (dialog->combobox), 0);
 
1797
 
 
1798
        screen = gtk_window_get_screen (GTK_WINDOW (dialog->run_dialog));
 
1799
 
 
1800
        /* 1/4 the width of the first monitor should be a good value */
 
1801
        width_request = panel_multiscreen_width (screen, 0) / 4;
 
1802
        g_object_set (G_OBJECT (dialog->combobox),
 
1803
                      "width_request", width_request,
 
1804
                      NULL);
 
1805
 
 
1806
        g_signal_connect (entry, "key-press-event",
 
1807
                          G_CALLBACK (entry_event), dialog);
 
1808
                          
 
1809
        dialog->changed_id = g_signal_connect (dialog->combobox, "changed",
 
1810
                                               G_CALLBACK (combobox_changed),
 
1811
                                               dialog);
 
1812
 
 
1813
        gtk_drag_dest_unset (dialog->combobox);
 
1814
        
 
1815
        gtk_drag_dest_set (dialog->combobox,
 
1816
                           GTK_DEST_DEFAULT_MOTION|GTK_DEST_DEFAULT_HIGHLIGHT,
 
1817
                           NULL, 0,
 
1818
                           GDK_ACTION_COPY);
 
1819
        gtk_drag_dest_add_uri_targets (dialog->combobox);
 
1820
 
 
1821
        g_signal_connect (dialog->combobox, "drag_data_received",
 
1822
                          G_CALLBACK (entry_drag_data_received), dialog);
 
1823
}
 
1824
 
 
1825
static char *
 
1826
panel_run_dialog_create_desktop_file (PanelRunDialog *dialog)
 
1827
{
 
1828
        GKeyFile *key_file;
 
1829
        gboolean  exec = FALSE;
 
1830
        char     *text;
 
1831
        char     *name;
 
1832
        char     *icon;
 
1833
        char     *disk;
 
1834
        char     *scheme;
 
1835
        char     *save_uri;
 
1836
 
 
1837
        text = g_strdup (panel_run_dialog_get_combo_text (dialog));
 
1838
        
 
1839
        if (!text || !text [0]) {
 
1840
                g_free (text);
 
1841
                return NULL;
 
1842
        }
 
1843
                
 
1844
        key_file = panel_key_file_new_desktop ();
 
1845
        disk = g_locale_from_utf8 (text, -1, NULL, NULL, NULL);
 
1846
 
 
1847
        scheme = g_uri_parse_scheme (disk);
 
1848
        /* if it's an absolute path or not a URI, it's possibly an executable */
 
1849
        if (g_path_is_absolute (disk) || !scheme)
 
1850
                exec = command_is_executable (disk, NULL, NULL);
 
1851
        g_free (scheme);
 
1852
                
 
1853
        if (exec) {
 
1854
                panel_key_file_set_string (key_file, "Type", "Application");
 
1855
                panel_key_file_set_string (key_file, "Exec", text);
 
1856
                name = g_strdup (text);
 
1857
        } else {
 
1858
                GFile *file;
 
1859
                char  *uri;
 
1860
 
 
1861
                file = panel_util_get_file_optional_homedir (disk);
 
1862
                uri = g_file_get_uri (file);
 
1863
                g_object_unref (file);
 
1864
 
 
1865
                panel_key_file_set_string (key_file, "Type", "Link");
 
1866
                panel_key_file_set_string (key_file, "URL", uri);
 
1867
                name = uri;
 
1868
        }
 
1869
 
 
1870
        g_free (disk);
 
1871
 
 
1872
        panel_key_file_set_locale_string (key_file, "Name",
 
1873
                                          (dialog->item_name) ?
 
1874
                                          dialog->item_name : text);
 
1875
 
 
1876
        panel_key_file_set_boolean (key_file, "Terminal",
 
1877
                                    gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->terminal_checkbox)));
 
1878
 
 
1879
        icon = NULL;
 
1880
        if (dialog->gicon)
 
1881
                icon = panel_util_get_icon_name_from_g_icon (dialog->gicon);
 
1882
        if (icon != NULL) {
 
1883
                panel_key_file_set_locale_string (key_file, "Icon",
 
1884
                                                  icon);
 
1885
                g_free (icon);
 
1886
        } else
 
1887
                panel_key_file_set_locale_string (key_file, "Icon",
 
1888
                                                  PANEL_ICON_LAUNCHER);
 
1889
 
 
1890
        save_uri = panel_make_unique_desktop_uri (g_get_tmp_dir (), name);
 
1891
        disk = g_filename_from_uri (save_uri, NULL, NULL);
 
1892
 
 
1893
        if (!disk || !panel_key_file_to_file (key_file, disk, NULL)) {
 
1894
                g_free (save_uri);
 
1895
                save_uri = NULL;
 
1896
        }
 
1897
 
 
1898
        g_key_file_free (key_file);
 
1899
        g_free (disk);
 
1900
        g_free (name);
 
1901
        g_free (text);
 
1902
 
 
1903
        return save_uri;
 
1904
}
 
1905
 
 
1906
static void
 
1907
pixmap_drag_data_get (GtkWidget          *run_dialog,
 
1908
                      GdkDragContext     *context,
 
1909
                      GtkSelectionData   *selection_data,
 
1910
                      guint               info,
 
1911
                      guint               time,
 
1912
                      PanelRunDialog     *dialog)
 
1913
{
 
1914
        char *uri;
 
1915
 
 
1916
        if (dialog->use_program_list && dialog->desktop_path)
 
1917
                uri = g_filename_to_uri (dialog->desktop_path, NULL, NULL);
 
1918
        else
 
1919
                uri = panel_run_dialog_create_desktop_file (dialog);
 
1920
 
 
1921
        if (uri) {
 
1922
                gtk_selection_data_set (selection_data,
 
1923
                                        gtk_selection_data_get_target (selection_data), 8,
 
1924
                                        (unsigned char *) uri, strlen (uri));
 
1925
                g_free (uri);
 
1926
        }
 
1927
}
 
1928
 
 
1929
static void
 
1930
panel_run_dialog_setup_pixmap (PanelRunDialog *dialog,
 
1931
                               GtkBuilder     *gui)
 
1932
{
 
1933
        dialog->pixmap = PANEL_GTK_BUILDER_GET (gui, "icon_pixmap");
 
1934
        
 
1935
        g_signal_connect (dialog->run_dialog, "drag_data_get",
 
1936
                          G_CALLBACK (pixmap_drag_data_get),
 
1937
                          dialog);
 
1938
}
 
1939
 
 
1940
static PanelRunDialog *
 
1941
panel_run_dialog_new (GdkScreen  *screen,
 
1942
                      GtkBuilder *gui,
 
1943
                      guint32    activate_time)
 
1944
{
 
1945
        PanelRunDialog *dialog;
 
1946
 
 
1947
        dialog = g_new0 (PanelRunDialog, 1);
 
1948
 
 
1949
        dialog->run_dialog = PANEL_GTK_BUILDER_GET (gui, "panel_run_dialog");
 
1950
 
 
1951
        dialog->run_settings = g_settings_new (PANEL_RUN_SCHEMA);
 
1952
        
 
1953
        g_signal_connect_swapped (dialog->run_dialog, "response",
 
1954
                                  G_CALLBACK (panel_run_dialog_response), dialog);
 
1955
                                  
 
1956
        g_signal_connect_swapped (dialog->run_dialog, "destroy",
 
1957
                                  G_CALLBACK (panel_run_dialog_destroy), dialog);
 
1958
 
 
1959
        dialog->run_button = PANEL_GTK_BUILDER_GET (gui, "run_button");
 
1960
        dialog->terminal_checkbox = PANEL_GTK_BUILDER_GET (gui, "terminal_checkbox");
 
1961
        
 
1962
        panel_run_dialog_setup_pixmap        (dialog, gui);
 
1963
        panel_run_dialog_setup_entry         (dialog, gui);
 
1964
        panel_run_dialog_setup_file_button   (dialog, gui);
 
1965
        panel_run_dialog_setup_program_list  (dialog, gui);
 
1966
        panel_run_dialog_setup_list_expander (dialog, gui);
 
1967
 
 
1968
        gtk_window_set_icon_name (GTK_WINDOW (dialog->run_dialog),
 
1969
                                  PANEL_ICON_RUN);
 
1970
        panel_run_dialog_set_default_icon (dialog, FALSE);
 
1971
 
 
1972
        g_signal_connect (dialog->run_settings, "changed::"PANEL_RUN_ENABLE_LIST_KEY,
 
1973
                          G_CALLBACK (panel_run_dialog_update_program_list), dialog);
 
1974
        g_signal_connect (dialog->run_settings, "changed::"PANEL_RUN_SHOW_LIST_KEY,
 
1975
                          G_CALLBACK (panel_run_dialog_update_program_list), dialog);
 
1976
 
 
1977
        panel_run_dialog_update_program_list (dialog->run_settings, NULL, dialog);
 
1978
 
 
1979
        gtk_widget_set_sensitive (dialog->run_button, FALSE);
 
1980
        
 
1981
        gtk_dialog_set_default_response (GTK_DIALOG (dialog->run_dialog),
 
1982
                                         GTK_RESPONSE_OK);
 
1983
 
 
1984
        gtk_window_set_screen (GTK_WINDOW (dialog->run_dialog), screen);
 
1985
 
 
1986
        gtk_widget_grab_focus (dialog->combobox);
 
1987
        gtk_widget_realize (dialog->run_dialog);
 
1988
        gdk_x11_window_set_user_time (gtk_widget_get_window (dialog->run_dialog),
 
1989
                                      activate_time);
 
1990
        gtk_widget_show (dialog->run_dialog);
 
1991
        
 
1992
        return dialog;
 
1993
}
 
1994
 
 
1995
static void
 
1996
panel_run_dialog_static_dialog_destroyed (PanelRunDialog *dialog)
 
1997
{
 
1998
        /* just reset the static dialog to NULL for next time */
 
1999
        static_dialog = NULL;
 
2000
}
 
2001
 
 
2002
void
 
2003
panel_run_dialog_present (GdkScreen *screen,
 
2004
                          guint32    activate_time)
 
2005
{
 
2006
        GtkBuilder *gui;
 
2007
 
 
2008
        if (panel_lockdown_get_disable_command_line_s ())
 
2009
                return;
 
2010
 
 
2011
        if (static_dialog) {
 
2012
                gtk_window_set_screen (GTK_WINDOW (static_dialog->run_dialog), screen);
 
2013
                gtk_window_present_with_time (GTK_WINDOW (static_dialog->run_dialog),
 
2014
                                              activate_time);
 
2015
                gtk_widget_grab_focus (static_dialog->combobox);
 
2016
                return;
 
2017
        }
 
2018
 
 
2019
        gui = gtk_builder_new ();
 
2020
        gtk_builder_set_translation_domain (gui, GETTEXT_PACKAGE);
 
2021
        gtk_builder_add_from_resource (gui,
 
2022
                                       PANEL_RESOURCE_PATH "panel-run-dialog.ui",
 
2023
                                       NULL);
 
2024
 
 
2025
        static_dialog = panel_run_dialog_new (screen, gui, activate_time);
 
2026
 
 
2027
        g_signal_connect_swapped (static_dialog->run_dialog, "destroy",
 
2028
                                  G_CALLBACK (panel_run_dialog_static_dialog_destroyed),
 
2029
                                  static_dialog);
 
2030
 
 
2031
        g_object_unref (gui);
 
2032
}