Rewrite simple search engine to use GFile and breadth order

https://bugzilla.gnome.org/show_bug.cgi?id=746916
This commit is contained in:
Руслан Ижбулатов 2015-04-30 17:46:27 +00:00
parent 4b50836442
commit 4acbcf9e97

View File

@ -21,9 +21,7 @@
#include "config.h" #include "config.h"
#ifdef HAVE_FTW_H #include <gio/gio.h>
#include <ftw.h>
#endif
#include <gdk/gdk.h> #include <gdk/gdk.h>
@ -106,7 +104,7 @@ search_thread_data_new (GtkSearchEngineSimple *engine,
data->path = g_strdup (g_get_home_dir ()); data->path = g_strdup (g_get_home_dir ());
text = _gtk_query_get_text (query); text = _gtk_query_get_text (query);
lower = g_ascii_strdown (text, -1); lower = g_utf8_casefold (text, -1);
data->words = g_strsplit (lower, " ", -1); data->words = g_strsplit (lower, " ", -1);
g_free (text); g_free (text);
g_free (lower); g_free (lower);
@ -114,7 +112,6 @@ search_thread_data_new (GtkSearchEngineSimple *engine,
return data; return data;
} }
#ifdef HAVE_FTW_H
static void static void
search_thread_data_free (SearchThreadData *data) search_thread_data_free (SearchThreadData *data)
{ {
@ -187,57 +184,143 @@ send_batch (SearchThreadData *data)
data->uri_hits = NULL; data->uri_hits = NULL;
} }
static GPrivate search_thread_data; typedef gboolean (VisitFunc) (const char *fpath, gpointer user_data);
static gboolean process_dir (GFile *dir, GList **new_root_dirs, VisitFunc func, gpointer user_data);
static GList * process_dirs (GList *root_dirs, VisitFunc func, gpointer user_data);
static void breadth_search (gchar *root, VisitFunc func, gpointer user_data);
static void
breadth_search (gchar *root, VisitFunc func, gpointer user_data)
{
GList *subdirs = NULL;
subdirs = g_list_prepend (subdirs, g_file_new_for_path (root));
while (subdirs)
subdirs = process_dirs (subdirs, func, user_data);
}
static GList *
process_dirs (GList *root_dirs, VisitFunc func, gpointer user_data)
{
SearchThreadData *data = (SearchThreadData*) user_data;
GList *new_root_dirs = NULL;
GList *root;
gboolean keep_going = TRUE;
for (root = root_dirs; root; root = g_list_next (root))
{
GFile *dir = (GFile *) root->data;
/* Even if cancelled or stopped, we keep looping to unref the dirs */
if (keep_going && !data->cancelled)
keep_going = process_dir (dir, &new_root_dirs, func, user_data);
g_object_unref (dir);
}
if (!keep_going || data->cancelled)
{
g_list_free_full (new_root_dirs, g_object_unref);
new_root_dirs = NULL;
}
g_list_free (root_dirs);
return g_list_reverse (new_root_dirs);
}
static gboolean
process_dir (GFile *dir, GList **new_root_dirs, VisitFunc func, gpointer user_data)
{
GFileEnumerator *direnum;
gchar *dirpath;
SearchThreadData *data = (SearchThreadData*) user_data;
direnum = g_file_enumerate_children (dir, G_FILE_ATTRIBUTE_STANDARD_NAME ","
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
NULL, NULL);
if (direnum == NULL || data->cancelled)
return FALSE;
dirpath = g_file_get_path (dir);
while (TRUE)
{
GFileInfo *info;
gchar *fullname;
const gchar *basename;
gboolean keep_going;
keep_going = g_file_enumerator_iterate (direnum, &info, NULL, NULL, NULL);
if (!keep_going || info == NULL || data->cancelled)
{
g_object_unref (direnum);
g_free (dirpath);
return keep_going;
}
if (g_file_info_get_is_hidden (info))
continue;
basename = g_file_info_get_name (info);
fullname = g_build_filename (dirpath, basename, NULL);
keep_going = func ((const char *) fullname, user_data);
g_free (fullname);
if (!keep_going)
{
g_object_unref (direnum);
g_free (dirpath);
return FALSE;
}
if (g_file_info_get_file_type (info) != G_FILE_TYPE_DIRECTORY)
continue;
*new_root_dirs = g_list_prepend (*new_root_dirs,
g_file_get_child (dir, basename));
}
}
static int static int
search_visit_func (const char *fpath, search_visit_func (const char *fpath, gpointer user_data)
const struct stat *sb,
int typeflag,
struct FTW *ftwbuf)
{ {
SearchThreadData *data; SearchThreadData *data;
gint i; gint i;
const gchar *name; gchar *display_basename;
gchar *lower_name; gchar *lower_name;
gchar *uri; gchar *uri;
gboolean hit; gboolean hit;
gboolean is_hidden;
data = (SearchThreadData*)g_private_get (&search_thread_data); data = (SearchThreadData*) user_data;
if (data->cancelled) if (data->cancelled)
#ifdef HAVE_GNU_FTW return FALSE;
return FTW_STOP;
#else
return 1;
#endif /* HAVE_GNU_FTW */
name = strrchr (fpath, '/'); display_basename = g_filename_display_basename (fpath);
if (name) lower_name = g_utf8_casefold (display_basename, -1);
name++; g_free (display_basename);
else
name = fpath;
is_hidden = *name == '.'; hit = TRUE;
for (i = 0; data->words[i] != NULL; i++)
hit = FALSE;
if (!is_hidden)
{ {
lower_name = g_ascii_strdown (name, -1); if (strstr (lower_name, data->words[i]) == NULL)
{
hit = TRUE; hit = FALSE;
for (i = 0; data->words[i] != NULL; i++) break;
{ }
if (strstr (lower_name, data->words[i]) == NULL)
{
hit = FALSE;
break;
}
}
g_free (lower_name);
} }
g_free (lower_name);
if (hit) if (hit)
{ {
uri = g_filename_to_uri (fpath, NULL, NULL); uri = g_filename_to_uri (fpath, NULL, NULL);
@ -249,39 +332,23 @@ search_visit_func (const char *fpath,
if (data->n_processed_files > BATCH_SIZE) if (data->n_processed_files > BATCH_SIZE)
send_batch (data); send_batch (data);
#ifdef HAVE_GNU_FTW return TRUE;
if (is_hidden)
return FTW_SKIP_SUBTREE;
else
return FTW_CONTINUE;
#else
return 0;
#endif /* HAVE_GNU_FTW */
} }
#endif /* HAVE_FTW_H */
static gpointer static gpointer
search_thread_func (gpointer user_data) search_thread_func (gpointer user_data)
{ {
#ifdef HAVE_FTW_H
guint id; guint id;
SearchThreadData *data; SearchThreadData *data;
data = user_data; data = user_data;
g_private_set (&search_thread_data, data); breadth_search (data->path, search_visit_func, data);
nftw (data->path, search_visit_func, 20,
#ifdef HAVE_GNU_FTW
FTW_ACTIONRETVAL |
#endif
FTW_PHYS);
send_batch (data); send_batch (data);
id = gdk_threads_add_idle (search_thread_done_idle, data); id = gdk_threads_add_idle (search_thread_done_idle, data);
g_source_set_name_by_id (id, "[gtk+] search_thread_done_idle"); g_source_set_name_by_id (id, "[gtk+] search_thread_done_idle");
#endif /* HAVE_FTW_H */
return NULL; return NULL;
} }
@ -362,9 +429,5 @@ _gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
GtkSearchEngine * GtkSearchEngine *
_gtk_search_engine_simple_new (void) _gtk_search_engine_simple_new (void)
{ {
#ifdef HAVE_FTW_H
return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL); return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);
#else
return NULL;
#endif
} }