Initialize variable
[glib.git] / gio / gfilenamecompleter.c
blob2550fececd9acfa605c1779015b5dab520bb568a
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include "gfilenamecompleter.h"
23 #include "gfileenumerator.h"
24 #include "gfileattribute.h"
25 #include "gfile.h"
26 #include "gfileinfo.h"
27 #include "gcancellable.h"
28 #include <string.h>
29 #include "glibintl.h"
32 /**
33 * SECTION:gfilenamecompleter
34 * @short_description: Filename Completer
35 * @include: gio/gio.h
37 * Completes partial file and directory names given a partial string by
38 * looking in the file system for clues. Can return a list of possible
39 * completion strings for widget implementations.
41 **/
43 enum {
44 GOT_COMPLETION_DATA,
45 LAST_SIGNAL
48 static guint signals[LAST_SIGNAL] = { 0 };
50 typedef struct {
51 GFilenameCompleter *completer;
52 GFileEnumerator *enumerator;
53 GCancellable *cancellable;
54 gboolean should_escape;
55 GFile *dir;
56 GList *basenames;
57 gboolean dirs_only;
58 } LoadBasenamesData;
60 struct _GFilenameCompleter {
61 GObject parent;
63 GFile *basenames_dir;
64 gboolean basenames_are_escaped;
65 gboolean dirs_only;
66 GList *basenames;
68 LoadBasenamesData *basename_loader;
71 G_DEFINE_TYPE (GFilenameCompleter, g_filename_completer, G_TYPE_OBJECT)
73 static void cancel_load_basenames (GFilenameCompleter *completer);
75 static void
76 g_filename_completer_finalize (GObject *object)
78 GFilenameCompleter *completer;
80 completer = G_FILENAME_COMPLETER (object);
82 cancel_load_basenames (completer);
84 if (completer->basenames_dir)
85 g_object_unref (completer->basenames_dir);
87 g_list_free_full (completer->basenames, g_free);
89 G_OBJECT_CLASS (g_filename_completer_parent_class)->finalize (object);
92 static void
93 g_filename_completer_class_init (GFilenameCompleterClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->finalize = g_filename_completer_finalize;
98 /**
99 * GFilenameCompleter::got-completion-data:
101 * Emitted when the file name completion information comes available.
103 signals[GOT_COMPLETION_DATA] = g_signal_new (I_("got-completion-data"),
104 G_TYPE_FILENAME_COMPLETER,
105 G_SIGNAL_RUN_LAST,
106 G_STRUCT_OFFSET (GFilenameCompleterClass, got_completion_data),
107 NULL, NULL,
108 g_cclosure_marshal_VOID__VOID,
109 G_TYPE_NONE, 0);
112 static void
113 g_filename_completer_init (GFilenameCompleter *completer)
118 * g_filename_completer_new:
120 * Creates a new filename completer.
122 * Returns: a #GFilenameCompleter.
124 GFilenameCompleter *
125 g_filename_completer_new (void)
127 return g_object_new (G_TYPE_FILENAME_COMPLETER, NULL);
130 static char *
131 longest_common_prefix (char *a, char *b)
133 char *start;
135 start = a;
137 while (g_utf8_get_char (a) == g_utf8_get_char (b))
139 a = g_utf8_next_char (a);
140 b = g_utf8_next_char (b);
143 return g_strndup (start, a - start);
146 static void
147 load_basenames_data_free (LoadBasenamesData *data)
149 if (data->enumerator)
150 g_object_unref (data->enumerator);
152 g_object_unref (data->cancellable);
153 g_object_unref (data->dir);
155 g_list_free_full (data->basenames, g_free);
157 g_free (data);
160 static void
161 got_more_files (GObject *source_object,
162 GAsyncResult *res,
163 gpointer user_data)
165 LoadBasenamesData *data = user_data;
166 GList *infos, *l;
167 GFileInfo *info;
168 const char *name;
169 gboolean append_slash;
170 char *t;
171 char *basename;
173 if (data->completer == NULL)
175 /* Was cancelled */
176 load_basenames_data_free (data);
177 return;
180 infos = g_file_enumerator_next_files_finish (data->enumerator, res, NULL);
182 for (l = infos; l != NULL; l = l->next)
184 info = l->data;
186 if (data->dirs_only &&
187 g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
189 g_object_unref (info);
190 continue;
193 append_slash = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
194 name = g_file_info_get_name (info);
195 if (name == NULL)
197 g_object_unref (info);
198 continue;
202 if (data->should_escape)
203 basename = g_uri_escape_string (name,
204 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,
205 TRUE);
206 else
207 /* If not should_escape, must be a local filename, convert to utf8 */
208 basename = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
210 if (basename)
212 if (append_slash)
214 t = basename;
215 basename = g_strconcat (basename, "/", NULL);
216 g_free (t);
219 data->basenames = g_list_prepend (data->basenames, basename);
222 g_object_unref (info);
225 g_list_free (infos);
227 if (infos)
229 /* Not last, get more files */
230 g_file_enumerator_next_files_async (data->enumerator,
231 100,
233 data->cancellable,
234 got_more_files, data);
236 else
238 data->completer->basename_loader = NULL;
240 if (data->completer->basenames_dir)
241 g_object_unref (data->completer->basenames_dir);
242 g_list_free_full (data->completer->basenames, g_free);
244 data->completer->basenames_dir = g_object_ref (data->dir);
245 data->completer->basenames = data->basenames;
246 data->completer->basenames_are_escaped = data->should_escape;
247 data->basenames = NULL;
249 g_file_enumerator_close_async (data->enumerator, 0, NULL, NULL, NULL);
251 g_signal_emit (data->completer, signals[GOT_COMPLETION_DATA], 0);
252 load_basenames_data_free (data);
257 static void
258 got_enum (GObject *source_object,
259 GAsyncResult *res,
260 gpointer user_data)
262 LoadBasenamesData *data = user_data;
264 if (data->completer == NULL)
266 /* Was cancelled */
267 load_basenames_data_free (data);
268 return;
271 data->enumerator = g_file_enumerate_children_finish (G_FILE (source_object), res, NULL);
273 if (data->enumerator == NULL)
275 data->completer->basename_loader = NULL;
277 if (data->completer->basenames_dir)
278 g_object_unref (data->completer->basenames_dir);
279 g_list_free_full (data->completer->basenames, g_free);
281 /* Mark uptodate with no basenames */
282 data->completer->basenames_dir = g_object_ref (data->dir);
283 data->completer->basenames = NULL;
284 data->completer->basenames_are_escaped = data->should_escape;
286 load_basenames_data_free (data);
287 return;
290 g_file_enumerator_next_files_async (data->enumerator,
291 100,
293 data->cancellable,
294 got_more_files, data);
297 static void
298 schedule_load_basenames (GFilenameCompleter *completer,
299 GFile *dir,
300 gboolean should_escape)
302 LoadBasenamesData *data;
304 cancel_load_basenames (completer);
306 data = g_new0 (LoadBasenamesData, 1);
307 data->completer = completer;
308 data->cancellable = g_cancellable_new ();
309 data->dir = g_object_ref (dir);
310 data->should_escape = should_escape;
311 data->dirs_only = completer->dirs_only;
313 completer->basename_loader = data;
315 g_file_enumerate_children_async (dir,
316 G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
317 0, 0,
318 data->cancellable,
319 got_enum, data);
322 static void
323 cancel_load_basenames (GFilenameCompleter *completer)
325 LoadBasenamesData *loader;
327 if (completer->basename_loader)
329 loader = completer->basename_loader;
330 loader->completer = NULL;
332 g_cancellable_cancel (loader->cancellable);
334 completer->basename_loader = NULL;
339 /* Returns a list of possible matches and the basename to use for it */
340 static GList *
341 init_completion (GFilenameCompleter *completer,
342 const char *initial_text,
343 char **basename_out)
345 gboolean should_escape;
346 GFile *file, *parent;
347 char *basename;
348 char *t;
349 int len;
351 *basename_out = NULL;
353 should_escape = ! (g_path_is_absolute (initial_text) || *initial_text == '~');
355 len = strlen (initial_text);
357 if (len > 0 &&
358 initial_text[len - 1] == '/')
359 return NULL;
361 file = g_file_parse_name (initial_text);
362 parent = g_file_get_parent (file);
363 if (parent == NULL)
365 g_object_unref (file);
366 return NULL;
369 if (completer->basenames_dir == NULL ||
370 completer->basenames_are_escaped != should_escape ||
371 !g_file_equal (parent, completer->basenames_dir))
373 schedule_load_basenames (completer, parent, should_escape);
374 g_object_unref (file);
375 return NULL;
378 basename = g_file_get_basename (file);
379 if (should_escape)
381 t = basename;
382 basename = g_uri_escape_string (basename, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
383 g_free (t);
385 else
387 t = basename;
388 basename = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
389 g_free (t);
391 if (basename == NULL)
392 return NULL;
395 *basename_out = basename;
397 return completer->basenames;
401 * g_filename_completer_get_completion_suffix:
402 * @completer: the filename completer.
403 * @initial_text: text to be completed.
405 * Obtains a completion for @initial_text from @completer.
407 * Returns: a completed string, or %NULL if no completion exists.
408 * This string is not owned by GIO, so remember to g_free() it
409 * when finished.
411 char *
412 g_filename_completer_get_completion_suffix (GFilenameCompleter *completer,
413 const char *initial_text)
415 GList *possible_matches, *l;
416 char *prefix;
417 char *suffix;
418 char *possible_match;
419 char *lcp;
421 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
422 g_return_val_if_fail (initial_text != NULL, NULL);
424 possible_matches = init_completion (completer, initial_text, &prefix);
426 suffix = NULL;
428 for (l = possible_matches; l != NULL; l = l->next)
430 possible_match = l->data;
432 if (g_str_has_prefix (possible_match, prefix))
434 if (suffix == NULL)
435 suffix = g_strdup (possible_match + strlen (prefix));
436 else
438 lcp = longest_common_prefix (suffix,
439 possible_match + strlen (prefix));
440 g_free (suffix);
441 suffix = lcp;
443 if (*suffix == 0)
444 break;
449 g_free (prefix);
451 return suffix;
455 * g_filename_completer_get_completions:
456 * @completer: the filename completer.
457 * @initial_text: text to be completed.
459 * Gets an array of completion strings for a given initial text.
461 * Returns: (array zero-terminated=1) (transfer full): array of strings with possible completions for @initial_text.
462 * This array must be freed by g_strfreev() when finished.
464 char **
465 g_filename_completer_get_completions (GFilenameCompleter *completer,
466 const char *initial_text)
468 GList *possible_matches, *l;
469 char *prefix;
470 char *possible_match;
471 GPtrArray *res;
473 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
474 g_return_val_if_fail (initial_text != NULL, NULL);
476 possible_matches = init_completion (completer, initial_text, &prefix);
478 res = g_ptr_array_new ();
479 for (l = possible_matches; l != NULL; l = l->next)
481 possible_match = l->data;
483 if (g_str_has_prefix (possible_match, prefix))
484 g_ptr_array_add (res,
485 g_strconcat (initial_text, possible_match + strlen (prefix), NULL));
488 g_free (prefix);
490 g_ptr_array_add (res, NULL);
492 return (char**)g_ptr_array_free (res, FALSE);
496 * g_filename_completer_set_dirs_only:
497 * @completer: the filename completer.
498 * @dirs_only: a #gboolean.
500 * If @dirs_only is %TRUE, @completer will only
501 * complete directory names, and not file names.
503 void
504 g_filename_completer_set_dirs_only (GFilenameCompleter *completer,
505 gboolean dirs_only)
507 g_return_if_fail (G_IS_FILENAME_COMPLETER (completer));
509 completer->dirs_only = dirs_only;