1 /* GIO - GLib Input, Output and Streaming Library
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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gfilenamecompleter.h"
25 #include "gfileenumerator.h"
26 #include "gfileattribute.h"
28 #include "gfileinfo.h"
29 #include "gcancellable.h"
35 * SECTION:gfilenamecompleter
36 * @short_description: Filename Completer
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.
50 static guint signals[LAST_SIGNAL] = { 0 };
53 GFilenameCompleter *completer;
54 GFileEnumerator *enumerator;
55 GCancellable *cancellable;
56 gboolean should_escape;
62 struct _GFilenameCompleter {
66 gboolean basenames_are_escaped;
70 LoadBasenamesData *basename_loader;
73 G_DEFINE_TYPE (GFilenameCompleter, g_filename_completer, G_TYPE_OBJECT);
75 static void cancel_load_basenames (GFilenameCompleter *completer);
78 g_filename_completer_finalize (GObject *object)
80 GFilenameCompleter *completer;
82 completer = G_FILENAME_COMPLETER (object);
84 cancel_load_basenames (completer);
86 if (completer->basenames_dir)
87 g_object_unref (completer->basenames_dir);
89 g_list_free_full (completer->basenames, g_free);
91 G_OBJECT_CLASS (g_filename_completer_parent_class)->finalize (object);
95 g_filename_completer_class_init (GFilenameCompleterClass *klass)
97 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 gobject_class->finalize = g_filename_completer_finalize;
101 * GFilenameCompleter::got-completion-data:
103 * Emitted when the file name completion information comes available.
105 signals[GOT_COMPLETION_DATA] = g_signal_new (I_("got-completion-data"),
106 G_TYPE_FILENAME_COMPLETER,
108 G_STRUCT_OFFSET (GFilenameCompleterClass, got_completion_data),
110 g_cclosure_marshal_VOID__VOID,
115 g_filename_completer_init (GFilenameCompleter *completer)
120 * g_filename_completer_new:
122 * Creates a new filename completer.
124 * Returns: a #GFilenameCompleter.
127 g_filename_completer_new (void)
129 return g_object_new (G_TYPE_FILENAME_COMPLETER, NULL);
133 longest_common_prefix (char *a, char *b)
139 while (g_utf8_get_char (a) == g_utf8_get_char (b))
141 a = g_utf8_next_char (a);
142 b = g_utf8_next_char (b);
145 return g_strndup (start, a - start);
149 load_basenames_data_free (LoadBasenamesData *data)
151 if (data->enumerator)
152 g_object_unref (data->enumerator);
154 g_object_unref (data->cancellable);
155 g_object_unref (data->dir);
157 g_list_free_full (data->basenames, g_free);
163 got_more_files (GObject *source_object,
167 LoadBasenamesData *data = user_data;
171 gboolean append_slash;
175 if (data->completer == NULL)
178 load_basenames_data_free (data);
182 infos = g_file_enumerator_next_files_finish (data->enumerator, res, NULL);
184 for (l = infos; l != NULL; l = l->next)
188 if (data->dirs_only &&
189 g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
191 g_object_unref (info);
195 append_slash = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
196 name = g_file_info_get_name (info);
199 g_object_unref (info);
204 if (data->should_escape)
205 basename = g_uri_escape_string (name,
206 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,
209 /* If not should_escape, must be a local filename, convert to utf8 */
210 basename = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
217 basename = g_strconcat (basename, "/", NULL);
221 data->basenames = g_list_prepend (data->basenames, basename);
224 g_object_unref (info);
231 /* Not last, get more files */
232 g_file_enumerator_next_files_async (data->enumerator,
236 got_more_files, data);
240 data->completer->basename_loader = NULL;
242 if (data->completer->basenames_dir)
243 g_object_unref (data->completer->basenames_dir);
244 g_list_free_full (data->completer->basenames, g_free);
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;
251 g_file_enumerator_close_async (data->enumerator, 0, NULL, NULL, NULL);
253 g_signal_emit (data->completer, signals[GOT_COMPLETION_DATA], 0);
254 load_basenames_data_free (data);
260 got_enum (GObject *source_object,
264 LoadBasenamesData *data = user_data;
266 if (data->completer == NULL)
269 load_basenames_data_free (data);
273 data->enumerator = g_file_enumerate_children_finish (G_FILE (source_object), res, NULL);
275 if (data->enumerator == NULL)
277 data->completer->basename_loader = NULL;
279 if (data->completer->basenames_dir)
280 g_object_unref (data->completer->basenames_dir);
281 g_list_free_full (data->completer->basenames, g_free);
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;
288 load_basenames_data_free (data);
292 g_file_enumerator_next_files_async (data->enumerator,
296 got_more_files, data);
300 schedule_load_basenames (GFilenameCompleter *completer,
302 gboolean should_escape)
304 LoadBasenamesData *data;
306 cancel_load_basenames (completer);
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;
315 completer->basename_loader = data;
317 g_file_enumerate_children_async (dir,
318 G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
325 cancel_load_basenames (GFilenameCompleter *completer)
327 LoadBasenamesData *loader;
329 if (completer->basename_loader)
331 loader = completer->basename_loader;
332 loader->completer = NULL;
334 g_cancellable_cancel (loader->cancellable);
336 completer->basename_loader = NULL;
341 /* Returns a list of possible matches and the basename to use for it */
343 init_completion (GFilenameCompleter *completer,
344 const char *initial_text,
347 gboolean should_escape;
348 GFile *file, *parent;
353 *basename_out = NULL;
355 should_escape = ! (g_path_is_absolute (initial_text) || *initial_text == '~');
357 len = strlen (initial_text);
360 initial_text[len - 1] == '/')
363 file = g_file_parse_name (initial_text);
364 parent = g_file_get_parent (file);
367 g_object_unref (file);
371 if (completer->basenames_dir == NULL ||
372 completer->basenames_are_escaped != should_escape ||
373 !g_file_equal (parent, completer->basenames_dir))
375 schedule_load_basenames (completer, parent, should_escape);
376 g_object_unref (file);
380 basename = g_file_get_basename (file);
384 basename = g_uri_escape_string (basename, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
390 basename = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
393 if (basename == NULL)
397 *basename_out = basename;
399 return completer->basenames;
403 * g_filename_completer_get_completion_suffix:
404 * @completer: the filename completer.
405 * @initial_text: text to be completed.
407 * Obtains a completion for @initial_text from @completer.
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
414 g_filename_completer_get_completion_suffix (GFilenameCompleter *completer,
415 const char *initial_text)
417 GList *possible_matches, *l;
420 char *possible_match;
423 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
424 g_return_val_if_fail (initial_text != NULL, NULL);
426 possible_matches = init_completion (completer, initial_text, &prefix);
430 for (l = possible_matches; l != NULL; l = l->next)
432 possible_match = l->data;
434 if (g_str_has_prefix (possible_match, prefix))
437 suffix = g_strdup (possible_match + strlen (prefix));
440 lcp = longest_common_prefix (suffix,
441 possible_match + strlen (prefix));
457 * g_filename_completer_get_completions:
458 * @completer: the filename completer.
459 * @initial_text: text to be completed.
461 * Gets an array of completion strings for a given initial text.
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.
467 g_filename_completer_get_completions (GFilenameCompleter *completer,
468 const char *initial_text)
470 GList *possible_matches, *l;
472 char *possible_match;
475 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
476 g_return_val_if_fail (initial_text != NULL, NULL);
478 possible_matches = init_completion (completer, initial_text, &prefix);
480 res = g_ptr_array_new ();
481 for (l = possible_matches; l != NULL; l = l->next)
483 possible_match = l->data;
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));
492 g_ptr_array_add (res, NULL);
494 return (char**)g_ptr_array_free (res, FALSE);
498 * g_filename_completer_set_dirs_only:
499 * @completer: the filename completer.
500 * @dirs_only: a #gboolean.
502 * If @dirs_only is %TRUE, @completer will only
503 * complete directory names, and not file names.
506 g_filename_completer_set_dirs_only (GFilenameCompleter *completer,
509 g_return_if_fail (G_IS_FILENAME_COMPLETER (completer));
511 completer->dirs_only = dirs_only;