gio/tests/file: skip the file monitor tests if using GPollFileMonitor
[platform/upstream/glib.git] / gio / gfilenamecompleter.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gfilenamecompleter.h"
25 #include "gfileenumerator.h"
26 #include "gfileattribute.h"
27 #include "gfile.h"
28 #include "gfileinfo.h"
29 #include "gcancellable.h"
30 #include <string.h>
31 #include "glibintl.h"
32
33
34 /**
35  * SECTION:gfilenamecompleter
36  * @short_description: Filename Completer
37  * @include: gio/gio.h
38  * 
39  * Completes partial file and directory names given a partial string by
40  * looking in the file system for clues. Can return a list of possible 
41  * completion strings for widget implementations.
42  * 
43  **/
44
45 enum {
46   GOT_COMPLETION_DATA,
47   LAST_SIGNAL
48 };
49
50 static guint signals[LAST_SIGNAL] = { 0 };
51
52 typedef struct {
53   GFilenameCompleter *completer;
54   GFileEnumerator *enumerator;
55   GCancellable *cancellable;
56   gboolean should_escape;
57   GFile *dir;
58   GList *basenames;
59   gboolean dirs_only;
60 } LoadBasenamesData;
61
62 struct _GFilenameCompleter {
63   GObject parent;
64
65   GFile *basenames_dir;
66   gboolean basenames_are_escaped;
67   gboolean dirs_only;
68   GList *basenames;
69
70   LoadBasenamesData *basename_loader;
71 };
72
73 G_DEFINE_TYPE (GFilenameCompleter, g_filename_completer, G_TYPE_OBJECT);
74
75 static void cancel_load_basenames (GFilenameCompleter *completer);
76
77 static void
78 g_filename_completer_finalize (GObject *object)
79 {
80   GFilenameCompleter *completer;
81
82   completer = G_FILENAME_COMPLETER (object);
83
84   cancel_load_basenames (completer);
85
86   if (completer->basenames_dir)
87     g_object_unref (completer->basenames_dir);
88
89   g_list_free_full (completer->basenames, g_free);
90
91   G_OBJECT_CLASS (g_filename_completer_parent_class)->finalize (object);
92 }
93
94 static void
95 g_filename_completer_class_init (GFilenameCompleterClass *klass)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98   
99   gobject_class->finalize = g_filename_completer_finalize;
100   /**
101    * GFilenameCompleter::got-completion-data:
102    * 
103    * Emitted when the file name completion information comes available.
104    **/
105   signals[GOT_COMPLETION_DATA] = g_signal_new (I_("got-completion-data"),
106                                           G_TYPE_FILENAME_COMPLETER,
107                                           G_SIGNAL_RUN_LAST,
108                                           G_STRUCT_OFFSET (GFilenameCompleterClass, got_completion_data),
109                                           NULL, NULL,
110                                           g_cclosure_marshal_VOID__VOID,
111                                           G_TYPE_NONE, 0);
112 }
113
114 static void
115 g_filename_completer_init (GFilenameCompleter *completer)
116 {
117 }
118
119 /**
120  * g_filename_completer_new:
121  * 
122  * Creates a new filename completer.
123  * 
124  * Returns: a #GFilenameCompleter.
125  **/
126 GFilenameCompleter *
127 g_filename_completer_new (void)
128 {
129   return g_object_new (G_TYPE_FILENAME_COMPLETER, NULL);
130 }
131
132 static char *
133 longest_common_prefix (char *a, char *b)
134 {
135   char *start;
136
137   start = a;
138
139   while (g_utf8_get_char (a) == g_utf8_get_char (b))
140     {
141       a = g_utf8_next_char (a);
142       b = g_utf8_next_char (b);
143     }
144
145   return g_strndup (start, a - start);
146 }
147
148 static void
149 load_basenames_data_free (LoadBasenamesData *data)
150 {
151   if (data->enumerator)
152     g_object_unref (data->enumerator);
153   
154   g_object_unref (data->cancellable);
155   g_object_unref (data->dir);
156   
157   g_list_free_full (data->basenames, g_free);
158   
159   g_free (data);
160 }
161
162 static void
163 got_more_files (GObject *source_object,
164                 GAsyncResult *res,
165                 gpointer user_data)
166 {
167   LoadBasenamesData *data = user_data;
168   GList *infos, *l;
169   GFileInfo *info;
170   const char *name;
171   gboolean append_slash;
172   char *t;
173   char *basename;
174
175   if (data->completer == NULL)
176     {
177       /* Was cancelled */
178       load_basenames_data_free (data);
179       return;
180     }
181
182   infos = g_file_enumerator_next_files_finish (data->enumerator, res, NULL);
183
184   for (l = infos; l != NULL; l = l->next)
185     {
186       info = l->data;
187
188       if (data->dirs_only &&
189           g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
190         {
191           g_object_unref (info);
192           continue;
193         }
194       
195       append_slash = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
196       name = g_file_info_get_name (info);
197       if (name == NULL)
198         {
199           g_object_unref (info);
200           continue;
201         }
202
203       
204       if (data->should_escape)
205         basename = g_uri_escape_string (name,
206                                         G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,
207                                         TRUE);
208       else
209         /* If not should_escape, must be a local filename, convert to utf8 */
210         basename = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
211       
212       if (basename)
213         {
214           if (append_slash)
215             {
216               t = basename;
217               basename = g_strconcat (basename, "/", NULL);
218               g_free (t);
219             }
220           
221           data->basenames = g_list_prepend (data->basenames, basename);
222         }
223       
224       g_object_unref (info);
225     }
226   
227   g_list_free (infos);
228   
229   if (infos)
230     {
231       /* Not last, get more files */
232       g_file_enumerator_next_files_async (data->enumerator,
233                                           100,
234                                           0,
235                                           data->cancellable,
236                                           got_more_files, data);
237     }
238   else
239     {
240       data->completer->basename_loader = NULL;
241       
242       if (data->completer->basenames_dir)
243         g_object_unref (data->completer->basenames_dir);
244       g_list_free_full (data->completer->basenames, g_free);
245       
246       data->completer->basenames_dir = g_object_ref (data->dir);
247       data->completer->basenames = data->basenames;
248       data->completer->basenames_are_escaped = data->should_escape;
249       data->basenames = NULL;
250       
251       g_file_enumerator_close_async (data->enumerator, 0, NULL, NULL, NULL);
252
253       g_signal_emit (data->completer, signals[GOT_COMPLETION_DATA], 0);
254       load_basenames_data_free (data);
255     }
256 }
257
258
259 static void
260 got_enum (GObject *source_object,
261           GAsyncResult *res,
262           gpointer user_data)
263 {
264   LoadBasenamesData *data = user_data;
265
266   if (data->completer == NULL)
267     {
268       /* Was cancelled */
269       load_basenames_data_free (data);
270       return;
271     }
272   
273   data->enumerator = g_file_enumerate_children_finish (G_FILE (source_object), res, NULL);
274   
275   if (data->enumerator == NULL)
276     {
277       data->completer->basename_loader = NULL;
278
279       if (data->completer->basenames_dir)
280         g_object_unref (data->completer->basenames_dir);
281       g_list_free_full (data->completer->basenames, g_free);
282
283       /* Mark uptodate with no basenames */
284       data->completer->basenames_dir = g_object_ref (data->dir);
285       data->completer->basenames = NULL;
286       data->completer->basenames_are_escaped = data->should_escape;
287       
288       load_basenames_data_free (data);
289       return;
290     }
291   
292   g_file_enumerator_next_files_async (data->enumerator,
293                                       100,
294                                       0,
295                                       data->cancellable,
296                                       got_more_files, data);
297 }
298
299 static void
300 schedule_load_basenames (GFilenameCompleter *completer,
301                          GFile *dir,
302                          gboolean should_escape)
303 {
304   LoadBasenamesData *data;
305
306   cancel_load_basenames (completer);
307
308   data = g_new0 (LoadBasenamesData, 1);
309   data->completer = completer;
310   data->cancellable = g_cancellable_new ();
311   data->dir = g_object_ref (dir);
312   data->should_escape = should_escape;
313   data->dirs_only = completer->dirs_only;
314
315   completer->basename_loader = data;
316   
317   g_file_enumerate_children_async (dir,
318                                    G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
319                                    0, 0,
320                                    data->cancellable,
321                                    got_enum, data);
322 }
323
324 static void
325 cancel_load_basenames (GFilenameCompleter *completer)
326 {
327   LoadBasenamesData *loader;
328   
329   if (completer->basename_loader)
330     {
331       loader = completer->basename_loader; 
332       loader->completer = NULL;
333       
334       g_cancellable_cancel (loader->cancellable);
335       
336       completer->basename_loader = NULL;
337     }
338 }
339
340
341 /* Returns a list of possible matches and the basename to use for it */
342 static GList *
343 init_completion (GFilenameCompleter *completer,
344                  const char *initial_text,
345                  char **basename_out)
346 {
347   gboolean should_escape;
348   GFile *file, *parent;
349   char *basename;
350   char *t;
351   int len;
352
353   *basename_out = NULL;
354   
355   should_escape = ! (g_path_is_absolute (initial_text) || *initial_text == '~');
356
357   len = strlen (initial_text);
358   
359   if (len > 0 &&
360       initial_text[len - 1] == '/')
361     return NULL;
362   
363   file = g_file_parse_name (initial_text);
364   parent = g_file_get_parent (file);
365   if (parent == NULL)
366     {
367       g_object_unref (file);
368       return NULL;
369     }
370
371   if (completer->basenames_dir == NULL ||
372       completer->basenames_are_escaped != should_escape ||
373       !g_file_equal (parent, completer->basenames_dir))
374     {
375       schedule_load_basenames (completer, parent, should_escape);
376       g_object_unref (file);
377       return NULL;
378     }
379   
380   basename = g_file_get_basename (file);
381   if (should_escape)
382     {
383       t = basename;
384       basename = g_uri_escape_string (basename, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
385       g_free (t);
386     }
387   else
388     {
389       t = basename;
390       basename = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
391       g_free (t);
392       
393       if (basename == NULL)
394         return NULL;
395     }
396
397   *basename_out = basename;
398
399   return completer->basenames;
400 }
401
402 /**
403  * g_filename_completer_get_completion_suffix:
404  * @completer: the filename completer.
405  * @initial_text: text to be completed.
406  *
407  * Obtains a completion for @initial_text from @completer.
408  *  
409  * Returns: a completed string, or %NULL if no completion exists. 
410  *     This string is not owned by GIO, so remember to g_free() it 
411  *     when finished.
412  **/
413 char *
414 g_filename_completer_get_completion_suffix (GFilenameCompleter *completer,
415                                             const char *initial_text)
416 {
417   GList *possible_matches, *l;
418   char *prefix;
419   char *suffix;
420   char *possible_match;
421   char *lcp;
422
423   g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
424   g_return_val_if_fail (initial_text != NULL, NULL);
425
426   possible_matches = init_completion (completer, initial_text, &prefix);
427
428   suffix = NULL;
429   
430   for (l = possible_matches; l != NULL; l = l->next)
431     {
432       possible_match = l->data;
433       
434       if (g_str_has_prefix (possible_match, prefix))
435         {
436           if (suffix == NULL)
437             suffix = g_strdup (possible_match + strlen (prefix));
438           else
439             {
440               lcp = longest_common_prefix (suffix,
441                                            possible_match + strlen (prefix));
442               g_free (suffix);
443               suffix = lcp;
444               
445               if (*suffix == 0)
446                 break;
447             }
448         }
449     }
450
451   g_free (prefix);
452   
453   return suffix;
454 }
455
456 /**
457  * g_filename_completer_get_completions:
458  * @completer: the filename completer.
459  * @initial_text: text to be completed.
460  * 
461  * Gets an array of completion strings for a given initial text.
462  * 
463  * Returns: (array zero-terminated=1) (transfer full): array of strings with possible completions for @initial_text.
464  * This array must be freed by g_strfreev() when finished. 
465  **/
466 char **
467 g_filename_completer_get_completions (GFilenameCompleter *completer,
468                                       const char         *initial_text)
469 {
470   GList *possible_matches, *l;
471   char *prefix;
472   char *possible_match;
473   GPtrArray *res;
474
475   g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
476   g_return_val_if_fail (initial_text != NULL, NULL);
477
478   possible_matches = init_completion (completer, initial_text, &prefix);
479
480   res = g_ptr_array_new ();
481   for (l = possible_matches; l != NULL; l = l->next)
482     {
483       possible_match = l->data;
484
485       if (g_str_has_prefix (possible_match, prefix))
486         g_ptr_array_add (res,
487                          g_strconcat (initial_text, possible_match + strlen (prefix), NULL));
488     }
489
490   g_free (prefix);
491
492   g_ptr_array_add (res, NULL);
493
494   return (char**)g_ptr_array_free (res, FALSE);
495 }
496
497 /**
498  * g_filename_completer_set_dirs_only:
499  * @completer: the filename completer.
500  * @dirs_only: a #gboolean.
501  * 
502  * If @dirs_only is %TRUE, @completer will only 
503  * complete directory names, and not file names.
504  **/
505 void
506 g_filename_completer_set_dirs_only (GFilenameCompleter *completer,
507                                     gboolean dirs_only)
508 {
509   g_return_if_fail (G_IS_FILENAME_COMPLETER (completer));
510
511   completer->dirs_only = dirs_only;
512 }