~muktupavels/gnome-panel/fix-for-lp-1310929-and-lp-1222339

« back to all changes in this revision

Viewing changes to .pc/git-build-with-gnome-desktop38-part1.patch/gnome-panel/panel-run-dialog.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-06-19 11:49:35 UTC
  • mfrom: (2.1.20 sid)
  • Revision ID: package-import@ubuntu.com-20130619114935-e912mx6nwxecoaib
Tags: 1:3.6.2-0ubuntu7
* Sync with Debian svn (LP: #1185873). Remaining changes:
  - Use epoch
  - Add apport hook
  - debian/control:
    + Recommend indicator-complete
    + Don't depend on nautilus 3.8 yet
  - debian/patches/14_revert_timedate_change.patch:
    + Dropped, we're using timedated
  - debian/patches/40_unset_menuproxy.patch:
    + Make sure gnome-panel and the applets don't pick up menu proxies.
  - debian/patches/41_classic_layout.patch:
    + Change the defaults to be more "classic Ubuntu": Show indicators
      & show desktop, drop GNOME clock & notification area
  - debian/patches/85_disable_shutdown_on_ltsp.patch:
    + Suppress the shutdown option in the panel if LTSP_CLIENT is set.
* debian/patches/50_ubuntu_sessions.patch:
  - Merge patch from gnome-session to split sessions into GNOME Flashback
    (No effects) using Metacity and GNOME Flashback using Compiz
* debian/patches/51_dont_require_nautilus38.patch:
  - Run Flashback without Nautilus 3.8 since it's not in Saucy yet

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