1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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"
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.
47 static guint signals[LAST_SIGNAL] = { 0 };
50 GFilenameCompleter *completer;
51 GFileEnumerator *enumerator;
52 GCancellable *cancellable;
53 gboolean should_escape;
59 struct _GFilenameCompleter {
63 gboolean basenames_are_escaped;
67 LoadBasenamesData *basename_loader;
70 G_DEFINE_TYPE (GFilenameCompleter, g_filename_completer, G_TYPE_OBJECT)
72 static void cancel_load_basenames (GFilenameCompleter *completer);
75 g_filename_completer_finalize (GObject *object)
77 GFilenameCompleter *completer;
79 completer = G_FILENAME_COMPLETER (object);
81 cancel_load_basenames (completer);
83 if (completer->basenames_dir)
84 g_object_unref (completer->basenames_dir);
86 g_list_free_full (completer->basenames, g_free);
88 G_OBJECT_CLASS (g_filename_completer_parent_class)->finalize (object);
92 g_filename_completer_class_init (GFilenameCompleterClass *klass)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = g_filename_completer_finalize;
98 * GFilenameCompleter::got-completion-data:
100 * Emitted when the file name completion information comes available.
102 signals[GOT_COMPLETION_DATA] = g_signal_new (I_("got-completion-data"),
103 G_TYPE_FILENAME_COMPLETER,
105 G_STRUCT_OFFSET (GFilenameCompleterClass, got_completion_data),
112 g_filename_completer_init (GFilenameCompleter *completer)
117 * g_filename_completer_new:
119 * Creates a new filename completer.
121 * Returns: a #GFilenameCompleter.
124 g_filename_completer_new (void)
126 return g_object_new (G_TYPE_FILENAME_COMPLETER, NULL);
130 longest_common_prefix (char *a, char *b)
136 while (g_utf8_get_char (a) == g_utf8_get_char (b))
138 a = g_utf8_next_char (a);
139 b = g_utf8_next_char (b);
142 return g_strndup (start, a - start);
146 load_basenames_data_free (LoadBasenamesData *data)
148 if (data->enumerator)
149 g_object_unref (data->enumerator);
151 g_object_unref (data->cancellable);
152 g_object_unref (data->dir);
154 g_list_free_full (data->basenames, g_free);
160 got_more_files (GObject *source_object,
164 LoadBasenamesData *data = user_data;
168 gboolean append_slash;
172 if (data->completer == NULL)
175 load_basenames_data_free (data);
179 infos = g_file_enumerator_next_files_finish (data->enumerator, res, NULL);
181 for (l = infos; l != NULL; l = l->next)
185 if (data->dirs_only &&
186 g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
188 g_object_unref (info);
192 append_slash = g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
193 name = g_file_info_get_name (info);
196 g_object_unref (info);
201 if (data->should_escape)
202 basename = g_uri_escape_string (name,
203 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH,
206 /* If not should_escape, must be a local filename, convert to utf8 */
207 basename = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
214 basename = g_strconcat (basename, "/", NULL);
218 data->basenames = g_list_prepend (data->basenames, basename);
221 g_object_unref (info);
228 /* Not last, get more files */
229 g_file_enumerator_next_files_async (data->enumerator,
233 got_more_files, data);
237 data->completer->basename_loader = NULL;
239 if (data->completer->basenames_dir)
240 g_object_unref (data->completer->basenames_dir);
241 g_list_free_full (data->completer->basenames, g_free);
243 data->completer->basenames_dir = g_object_ref (data->dir);
244 data->completer->basenames = data->basenames;
245 data->completer->basenames_are_escaped = data->should_escape;
246 data->basenames = NULL;
248 g_file_enumerator_close_async (data->enumerator, 0, NULL, NULL, NULL);
250 g_signal_emit (data->completer, signals[GOT_COMPLETION_DATA], 0);
251 load_basenames_data_free (data);
257 got_enum (GObject *source_object,
261 LoadBasenamesData *data = user_data;
263 if (data->completer == NULL)
266 load_basenames_data_free (data);
270 data->enumerator = g_file_enumerate_children_finish (G_FILE (source_object), res, NULL);
272 if (data->enumerator == NULL)
274 data->completer->basename_loader = NULL;
276 if (data->completer->basenames_dir)
277 g_object_unref (data->completer->basenames_dir);
278 g_list_free_full (data->completer->basenames, g_free);
280 /* Mark up-to-date with no basenames */
281 data->completer->basenames_dir = g_object_ref (data->dir);
282 data->completer->basenames = NULL;
283 data->completer->basenames_are_escaped = data->should_escape;
285 load_basenames_data_free (data);
289 g_file_enumerator_next_files_async (data->enumerator,
293 got_more_files, data);
297 schedule_load_basenames (GFilenameCompleter *completer,
299 gboolean should_escape)
301 LoadBasenamesData *data;
303 cancel_load_basenames (completer);
305 data = g_new0 (LoadBasenamesData, 1);
306 data->completer = completer;
307 data->cancellable = g_cancellable_new ();
308 data->dir = g_object_ref (dir);
309 data->should_escape = should_escape;
310 data->dirs_only = completer->dirs_only;
312 completer->basename_loader = data;
314 g_file_enumerate_children_async (dir,
315 G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
322 cancel_load_basenames (GFilenameCompleter *completer)
324 LoadBasenamesData *loader;
326 if (completer->basename_loader)
328 loader = completer->basename_loader;
329 loader->completer = NULL;
331 g_cancellable_cancel (loader->cancellable);
333 completer->basename_loader = NULL;
338 /* Returns a list of possible matches and the basename to use for it */
340 init_completion (GFilenameCompleter *completer,
341 const char *initial_text,
344 gboolean should_escape;
345 GFile *file, *parent;
350 *basename_out = NULL;
352 should_escape = ! (g_path_is_absolute (initial_text) || *initial_text == '~');
354 len = strlen (initial_text);
357 initial_text[len - 1] == '/')
360 file = g_file_parse_name (initial_text);
361 parent = g_file_get_parent (file);
364 g_object_unref (file);
368 if (completer->basenames_dir == NULL ||
369 completer->basenames_are_escaped != should_escape ||
370 !g_file_equal (parent, completer->basenames_dir))
372 schedule_load_basenames (completer, parent, should_escape);
373 g_object_unref (file);
377 basename = g_file_get_basename (file);
381 basename = g_uri_escape_string (basename, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, TRUE);
387 basename = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
390 if (basename == NULL)
394 *basename_out = basename;
396 return completer->basenames;
400 * g_filename_completer_get_completion_suffix:
401 * @completer: the filename completer.
402 * @initial_text: text to be completed.
404 * Obtains a completion for @initial_text from @completer.
406 * Returns: (nullable) (transfer full): a completed string, or %NULL if no
407 * completion exists. This string is not owned by GIO, so remember to g_free()
411 g_filename_completer_get_completion_suffix (GFilenameCompleter *completer,
412 const char *initial_text)
414 GList *possible_matches, *l;
417 char *possible_match;
420 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
421 g_return_val_if_fail (initial_text != NULL, NULL);
423 possible_matches = init_completion (completer, initial_text, &prefix);
427 for (l = possible_matches; l != NULL; l = l->next)
429 possible_match = l->data;
431 if (g_str_has_prefix (possible_match, prefix))
434 suffix = g_strdup (possible_match + strlen (prefix));
437 lcp = longest_common_prefix (suffix,
438 possible_match + strlen (prefix));
454 * g_filename_completer_get_completions:
455 * @completer: the filename completer.
456 * @initial_text: text to be completed.
458 * Gets an array of completion strings for a given initial text.
460 * Returns: (array zero-terminated=1) (transfer full): array of strings with possible completions for @initial_text.
461 * This array must be freed by g_strfreev() when finished.
464 g_filename_completer_get_completions (GFilenameCompleter *completer,
465 const char *initial_text)
467 GList *possible_matches, *l;
469 char *possible_match;
472 g_return_val_if_fail (G_IS_FILENAME_COMPLETER (completer), NULL);
473 g_return_val_if_fail (initial_text != NULL, NULL);
475 possible_matches = init_completion (completer, initial_text, &prefix);
477 res = g_ptr_array_new ();
478 for (l = possible_matches; l != NULL; l = l->next)
480 possible_match = l->data;
482 if (g_str_has_prefix (possible_match, prefix))
483 g_ptr_array_add (res,
484 g_strconcat (initial_text, possible_match + strlen (prefix), NULL));
489 g_ptr_array_add (res, NULL);
491 return (char**)g_ptr_array_free (res, FALSE);
495 * g_filename_completer_set_dirs_only:
496 * @completer: the filename completer.
497 * @dirs_only: a #gboolean.
499 * If @dirs_only is %TRUE, @completer will only
500 * complete directory names, and not file names.
503 g_filename_completer_set_dirs_only (GFilenameCompleter *completer,
506 g_return_if_fail (G_IS_FILENAME_COMPLETER (completer));
508 completer->dirs_only = dirs_only;