~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to GMenu/src/applet-run-dialog.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Adapted from the Gnome-panel for Cairo-Dock by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
 
 
27
******************************************************************************/
 
28
 
 
29
#include "applet-struct.h"
 
30
#include "applet-run-dialog.h"
 
31
 
 
32
#define __USE_BSD 1
 
33
#include <string.h>
 
34
#include <dirent.h>
 
35
#include <errno.h>
 
36
#include <sys/types.h>
 
37
#include <unistd.h>
 
38
#include <sys/stat.h>
 
39
 
 
40
#include <gdk/gdkkeysyms.h>
 
41
 
 
42
 
 
43
static GList *
 
44
fill_files_from (const char *dirname,
 
45
                 const char *dirprefix,
 
46
                 char        prefix,
 
47
                 GList      *existing_items)
 
48
{
 
49
        GList         *list;
 
50
        DIR           *dir;
 
51
        struct dirent *dent;
 
52
        
 
53
        list = NULL;
 
54
        dir = opendir (dirname);
 
55
        
 
56
        if (!dir)
 
57
                return list;
 
58
        
 
59
        while ((dent = readdir (dir))) {
 
60
                char       *file;
 
61
                char       *item;
 
62
                const char *suffix;
 
63
                
 
64
                if (!dent->d_name ||
 
65
                    dent->d_name [0] != prefix)
 
66
                        continue;
 
67
 
 
68
                file = g_build_filename (dirname, dent->d_name, NULL);
 
69
                
 
70
                suffix = NULL;
 
71
                if (
 
72
                /* don't use g_file_test at first so we don't stat() */
 
73
                    dent->d_type == DT_DIR ||
 
74
                    (dent->d_type == DT_LNK &&
 
75
                     g_file_test (file, G_FILE_TEST_IS_DIR))
 
76
                        //g_file_test (file, G_FILE_TEST_IS_DIR)
 
77
                   )
 
78
                        suffix = "/";
 
79
                
 
80
                g_free (file);
 
81
                
 
82
                item = g_build_filename (dirprefix, dent->d_name, suffix, NULL);
 
83
                
 
84
                list = g_list_prepend (list, item);
 
85
        }
 
86
 
 
87
        closedir (dir);
 
88
        
 
89
        return list;
 
90
}       
 
91
 
 
92
static GList *
 
93
fill_possible_executables (void)
 
94
{
 
95
        GList         *list;
 
96
        const char    *path;
 
97
        char         **pathv;
 
98
        int            i;
 
99
        
 
100
        list = NULL;
 
101
        path = g_getenv ("PATH");
 
102
 
 
103
        if (!path || !path [0])
 
104
                return list;
 
105
 
 
106
        pathv = g_strsplit (path, ":", 0);
 
107
        
 
108
        for (i = 0; pathv [i]; i++) {
 
109
                const char *file;
 
110
                char       *filename;
 
111
                GDir       *dir;
 
112
 
 
113
                dir = g_dir_open (pathv [i], 0, NULL);
 
114
 
 
115
                if (!dir)
 
116
                        continue;
 
117
 
 
118
                while ((file = g_dir_read_name (dir))) {
 
119
                        filename = g_build_filename (pathv [i], file, NULL);
 
120
                        list = g_list_prepend (list, filename);
 
121
                }
 
122
 
 
123
                g_dir_close (dir);
 
124
        }
 
125
        
 
126
        g_strfreev (pathv);
 
127
        
 
128
        return list;
 
129
}
 
130
 
 
131
static GList *
 
132
fill_executables (GList *possible_executables,
 
133
                  GList *existing_items,
 
134
                  char   prefix)
 
135
{
 
136
        GList *list;
 
137
        GList *l;
 
138
        
 
139
        list = NULL;    
 
140
        
 
141
        for (l = possible_executables; l; l = l->next) {
 
142
                const char *filename;
 
143
                char       *basename;
 
144
                        
 
145
                filename = l->data;
 
146
                basename = g_path_get_basename (filename);
 
147
                        
 
148
                if (basename [0] == prefix &&
 
149
                    g_file_test (filename, G_FILE_TEST_IS_REGULAR) &&
 
150
                    g_file_test (filename, G_FILE_TEST_IS_EXECUTABLE)) {
 
151
                            
 
152
                        if (g_list_find_custom (existing_items, basename,
 
153
                                                (GCompareFunc) strcmp)) {
 
154
                                g_free (basename);
 
155
                                return NULL;
 
156
                        }
 
157
 
 
158
                        list = g_list_prepend (list, basename);
 
159
                 } else {
 
160
                        g_free (basename);
 
161
                 }
 
162
        }
 
163
        
 
164
        return list;
 
165
}
 
166
 
 
167
static void cd_menu_run_dialog_update_completion (CairoDockModuleInstance *myApplet,
 
168
        const char *text)
 
169
{
 
170
        GList *list;
 
171
        GList *executables;
 
172
        char   prefix;
 
173
        char  *buf;
 
174
        char  *dirname;
 
175
        char  *dirprefix;
 
176
        char  *key;
 
177
 
 
178
        g_assert (text != NULL && *text != '\0' && !g_ascii_isspace (*text));
 
179
 
 
180
        list = NULL;
 
181
        executables = NULL;
 
182
 
 
183
        if (!myData.completion) {
 
184
                myData.completion = g_completion_new (NULL);
 
185
                myData.possible_executables = fill_possible_executables ();
 
186
                myData.dir_hash = g_hash_table_new_full (g_str_hash,
 
187
                        g_str_equal,
 
188
                        g_free, NULL);
 
189
        }
 
190
        
 
191
        buf = g_path_get_basename (text);
 
192
        prefix = buf[0];
 
193
        g_free (buf);
 
194
        if (prefix == '/' || prefix == '.')
 
195
                return;
 
196
 
 
197
        if (text [0] == '/') {
 
198
                /* complete against absolute path */
 
199
                dirname = g_path_get_dirname (text);
 
200
                dirprefix = g_strdup (dirname);
 
201
        } else {
 
202
                /* complete against relative path and executable name */
 
203
                if (!strchr (text, '/')) {
 
204
                        executables = fill_executables (myData.possible_executables,
 
205
                                                        myData.completion_items,
 
206
                                                        text [0]);
 
207
                        dirprefix = g_strdup ("");
 
208
                } else {
 
209
                        dirprefix = g_path_get_dirname (text);
 
210
                }
 
211
 
 
212
                dirname = g_build_filename (g_get_home_dir (), dirprefix, NULL);
 
213
        }
 
214
 
 
215
        key = g_strdup_printf ("%s%c%c", dirprefix, G_DIR_SEPARATOR, prefix);
 
216
 
 
217
        if (!g_hash_table_lookup (myData.dir_hash, key)) {
 
218
                g_hash_table_insert (myData.dir_hash, key, myApplet);
 
219
 
 
220
                list = fill_files_from (dirname, dirprefix, prefix,
 
221
                                        myData.completion_items);
 
222
        } else {
 
223
                g_free (key);
 
224
        }
 
225
 
 
226
        list = g_list_concat (list, executables);
 
227
 
 
228
        g_free (dirname);
 
229
        g_free (dirprefix);
 
230
 
 
231
        if (list == NULL)
 
232
                return;
 
233
                
 
234
        g_completion_add_items (myData.completion, list);
 
235
                
 
236
        myData.completion_items = g_list_concat (myData.completion_items, list);
 
237
}
 
238
 
 
239
static gboolean _entry_event (GtkEditable *entry,
 
240
        GdkEventKey *event,
 
241
        CairoDockModuleInstance *myApplet)
 
242
{
 
243
        GtkTreeSelection *selection;
 
244
        char             *prefix;
 
245
        char             *nospace_prefix;
 
246
        char             *nprefix;
 
247
        char             *temp;
 
248
        int               pos, tmp;
 
249
 
 
250
        if (event->type != GDK_KEY_PRESS)
 
251
                return FALSE;
 
252
 
 
253
        /* tab completion */
 
254
        if (event->keyval == GDK_Tab) {
 
255
                gtk_editable_get_selection_bounds (entry, &pos, &tmp);
 
256
 
 
257
                if (myData.completion_started &&
 
258
                    pos != tmp &&
 
259
                    pos != 1 &&
 
260
                    tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
261
                        gtk_editable_select_region (entry, 0, 0);               
 
262
                        gtk_editable_set_position (entry, -1);
 
263
                        
 
264
                        return TRUE;
 
265
                }
 
266
        } else if (event->length > 0) {
 
267
                           
 
268
                gtk_editable_get_selection_bounds (entry, &pos, &tmp);
 
269
 
 
270
                if (myData.completion_started &&
 
271
                    pos != tmp &&
 
272
                    pos != 0 &&
 
273
                    tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
274
                        temp = gtk_editable_get_chars (entry, 0, pos);
 
275
                        prefix = g_strconcat (temp, event->string, NULL);
 
276
                        g_free (temp);
 
277
                } else if (pos == tmp &&
 
278
                           tmp == strlen (gtk_entry_get_text (GTK_ENTRY (entry)))) {
 
279
                        prefix = g_strconcat (gtk_entry_get_text (GTK_ENTRY (entry)),
 
280
                                              event->string, NULL);
 
281
                } else {
 
282
                        return FALSE;
 
283
                }
 
284
                
 
285
                nospace_prefix = prefix;
 
286
                while (g_ascii_isspace (*nospace_prefix))
 
287
                        nospace_prefix ++;
 
288
                if (*nospace_prefix == '\0')
 
289
                        return FALSE;
 
290
 
 
291
                cd_menu_run_dialog_update_completion (myApplet, nospace_prefix);
 
292
                
 
293
                if (!myData.completion) {
 
294
                        g_free (prefix);
 
295
                        return FALSE;
 
296
                }
 
297
                
 
298
                pos = strlen (prefix);
 
299
                nprefix = NULL;
 
300
 
 
301
                g_completion_complete_utf8 (myData.completion,
 
302
                        nospace_prefix,
 
303
                        &nprefix);
 
304
 
 
305
                if (nprefix) {
 
306
                        int insertpos;
 
307
                        insertpos = 0;
 
308
 
 
309
                        temp = g_strndup (prefix, nospace_prefix - prefix);
 
310
                        g_free (prefix);
 
311
 
 
312
                        prefix = g_strconcat (temp, nprefix, NULL);
 
313
 
 
314
                        gtk_editable_delete_text (entry, 0, -1);
 
315
 
 
316
                        gtk_editable_insert_text (entry,
 
317
                                                  prefix, strlen (prefix),
 
318
                                                  &insertpos);
 
319
 
 
320
                        gtk_editable_set_position (entry, pos);
 
321
                        gtk_editable_select_region (entry, pos, -1);
 
322
                        
 
323
                        myData.completion_started = TRUE;
 
324
 
 
325
                        g_free (temp);
 
326
                        g_free (nprefix);
 
327
                        g_free (prefix);
 
328
                        
 
329
                        return TRUE;
 
330
                }
 
331
                
 
332
                g_free (prefix);
 
333
        }
 
334
        
 
335
        return FALSE;
 
336
}
 
337
 
 
338
 
 
339
static void _cd_menu_on_quick_launch (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
 
340
{
 
341
        if (iClickedButton == 0 || iClickedButton == -1)  // ok ou entree.
 
342
        {
 
343
                const gchar *cCommand = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
 
344
                if (cCommand != NULL && *cCommand != '0')
 
345
                        cairo_dock_launch_command (cCommand);
 
346
        }
 
347
        else
 
348
        {
 
349
                gtk_entry_set_text (GTK_ENTRY (pInteractiveWidget), "");
 
350
        }
 
351
        cairo_dock_dialog_reference (myData.pQuickLaunchDialog);
 
352
        cairo_dock_hide_dialog (myData.pQuickLaunchDialog);
 
353
}
 
354
CairoDialog *cd_menu_create_quick_launch_dialog (CairoDockModuleInstance *myApplet)
 
355
{
 
356
        CairoDialog *pDialog = cairo_dock_show_dialog_with_entry (D_("Enter a command to launch :"),
 
357
                myIcon, myContainer,
 
358
                MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
 
359
                NULL,
 
360
                (CairoDockActionOnAnswerFunc) _cd_menu_on_quick_launch, NULL, NULL);
 
361
        
 
362
        GtkWidget *pEntry = pDialog->pInteractiveWidget;
 
363
        g_signal_connect (pEntry, "key-press-event",
 
364
                G_CALLBACK (_entry_event),
 
365
                myApplet);
 
366
        return pDialog;
 
367
}