gtksearchenginesimple.c 9.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2005 Red Hat, Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *
 * Based on nautilus-search-engine-simple.c
 */

22
#include "config.h"
23

24
#include <gio/gio.h>
25

Emmanuele Bassi's avatar
Emmanuele Bassi committed
26 27
#include <gdk/gdk.h>

28
#include "gtksearchenginesimple.h"
29
#include "gtkfilesystem.h"
Matthias Clasen's avatar
Matthias Clasen committed
30
#include "gtkprivate.h"
31 32

#include <string.h>
33

34 35
#define BATCH_SIZE 500

Matthias Clasen's avatar
Matthias Clasen committed
36
typedef struct
37 38
{
  GtkSearchEngineSimple *engine;
39
  GCancellable *cancellable;
Matthias Clasen's avatar
Matthias Clasen committed
40

41
  GQueue *directories;
Matthias Clasen's avatar
Matthias Clasen committed
42

43
  gint n_processed_files;
44
  GList *hits;
Matthias Clasen's avatar
Matthias Clasen committed
45

46
  GtkQuery *query;
47
  gboolean recursive;
48 49 50
} SearchThreadData;


51
struct _GtkSearchEngineSimple
52
{
53 54
  GtkSearchEngine parent;

55
  GtkQuery *query;
Matthias Clasen's avatar
Matthias Clasen committed
56

57
  SearchThreadData *active_search;
Matthias Clasen's avatar
Matthias Clasen committed
58

59
  gboolean query_finished;
60 61 62

  GtkSearchEngineSimpleIsIndexed is_indexed_callback;
  gpointer                       is_indexed_data;
63
  GDestroyNotify                 is_indexed_data_destroy;
64 65
};

66 67 68 69
struct _GtkSearchEngineSimpleClass
{
  GtkSearchEngineClass parent_class;
};
70

71
G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE)
72 73

static void
Emmanuele Bassi's avatar
Emmanuele Bassi committed
74
gtk_search_engine_simple_dispose (GObject *object)
75
{
76
  GtkSearchEngineSimple *simple = GTK_SEARCH_ENGINE_SIMPLE (object);
Matthias Clasen's avatar
Matthias Clasen committed
77

78
  g_clear_object (&simple->query);
Matthias Clasen's avatar
Matthias Clasen committed
79

80
  if (simple->active_search)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
81
    {
82 83
      g_cancellable_cancel (simple->active_search->cancellable);
      simple->active_search = NULL;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
84 85
    }

86
  _gtk_search_engine_simple_set_indexed_cb (simple, NULL, NULL, NULL);
87

Emmanuele Bassi's avatar
Emmanuele Bassi committed
88
  G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->dispose (object);
89 90
}

91 92 93 94
static void
queue_if_local (SearchThreadData *data,
                GFile            *file)
{
95 96
  if (file &&
      !_gtk_file_consider_as_remote (file) &&
97
      !g_file_has_uri_scheme (file, "recent"))
98 99 100
    g_queue_push_tail (data->directories, g_object_ref (file));
}

101 102 103 104 105
static SearchThreadData *
search_thread_data_new (GtkSearchEngineSimple *engine,
			GtkQuery              *query)
{
  SearchThreadData *data;
Matthias Clasen's avatar
Matthias Clasen committed
106

107
  data = g_new0 (SearchThreadData, 1);
Matthias Clasen's avatar
Matthias Clasen committed
108

109
  data->engine = g_object_ref (engine);
110 111
  data->directories = g_queue_new ();
  data->query = g_object_ref (query);
112
  data->recursive = _gtk_search_engine_get_recursive (GTK_SEARCH_ENGINE (engine));
113
  queue_if_local (data, gtk_query_get_location (query));
Matthias Clasen's avatar
Matthias Clasen committed
114

115
  data->cancellable = g_cancellable_new ();
Matthias Clasen's avatar
Matthias Clasen committed
116

117 118 119
  return data;
}

Matthias Clasen's avatar
Matthias Clasen committed
120
static void
121 122
search_thread_data_free (SearchThreadData *data)
{
123 124 125 126
  g_queue_foreach (data->directories, (GFunc)g_object_unref, NULL);
  g_queue_free (data->directories);
  g_object_unref (data->cancellable);
  g_object_unref (data->query);
127
  g_object_unref (data->engine);
128

129 130 131 132 133 134 135 136 137
  g_free (data);
}

static gboolean
search_thread_done_idle (gpointer user_data)
{
  SearchThreadData *data;

  data = user_data;
Matthias Clasen's avatar
Matthias Clasen committed
138

139
  if (!g_cancellable_is_cancelled (data->cancellable))
Emmanuele Bassi's avatar
Emmanuele Bassi committed
140
    _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
Matthias Clasen's avatar
Matthias Clasen committed
141

142
  data->engine->active_search = NULL;
143
  search_thread_data_free (data);
Matthias Clasen's avatar
Matthias Clasen committed
144

145 146 147
  return FALSE;
}

Matthias Clasen's avatar
Matthias Clasen committed
148
typedef struct
149
{
150
  GList *hits;
151
  SearchThreadData *thread_data;
152
} Batch;
153 154 155 156

static gboolean
search_thread_add_hits_idle (gpointer user_data)
{
157
  Batch *batch = user_data;
158

159 160
  if (!g_cancellable_is_cancelled (batch->thread_data->cancellable))
    _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (batch->thread_data->engine), batch->hits);
161

162 163
  g_list_free_full (batch->hits, (GDestroyNotify)_gtk_search_hit_free);
  g_free (batch);
Matthias Clasen's avatar
Matthias Clasen committed
164

165 166 167 168 169 170
  return FALSE;
}

static void
send_batch (SearchThreadData *data)
{
171
  Batch *batch;
Matthias Clasen's avatar
Matthias Clasen committed
172

173
  data->n_processed_files = 0;
Matthias Clasen's avatar
Matthias Clasen committed
174

175
  if (data->hits)
176
    {
177 178
      guint id;

179 180 181
      batch = g_new (Batch, 1);
      batch->hits = data->hits;
      batch->thread_data = data;
Matthias Clasen's avatar
Matthias Clasen committed
182

183
      id = gdk_threads_add_idle (search_thread_add_hits_idle, batch);
184
      g_source_set_name_by_id (id, "[gtk+] search_thread_add_hits_idle");
185
    }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
186

187
  data->hits = NULL;
188 189
}

190 191 192 193
static gboolean
is_indexed (GtkSearchEngineSimple *engine,
            GFile                 *location)
{
194
  if (engine->is_indexed_callback)
195
    {
196
      if (engine->is_indexed_callback (location, engine->is_indexed_data))
197 198 199 200 201 202 203 204 205 206 207 208
        {
          gchar *uri = g_file_get_uri (location);
          g_debug ("Simple search engine: Skipping indexed location: %s\n", uri);
          g_free (uri);

          return TRUE;
        }
    }

  return FALSE;
}

209
static void
210
visit_directory (GFile *dir, SearchThreadData *data)
211
{
212 213 214 215 216 217 218 219 220
  GFileEnumerator *enumerator;
  GFileInfo *info;
  GFile *child;
  const gchar *display_name;

  enumerator = g_file_enumerate_children (dir,
                                          G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                          G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
                                          G_FILE_ATTRIBUTE_STANDARD_TYPE ","
221 222 223 224
                                          G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
                                          G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP ","
                                          G_FILE_ATTRIBUTE_STANDARD_SIZE ","
                                          G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
225
                                          G_FILE_ATTRIBUTE_STANDARD_TARGET_URI ","
226
                                          G_FILE_ATTRIBUTE_TIME_MODIFIED ","
227
                                          G_FILE_ATTRIBUTE_TIME_ACCESS ","
228
                                          G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME ","
229
                                          G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH ","
230
                                          G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE,
231 232 233 234
                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                          data->cancellable, NULL);
  if (enumerator == NULL)
    return;
235

236
  while (g_file_enumerator_iterate (enumerator, &info, &child, data->cancellable, NULL))
237
    {
238 239
      if (info == NULL)
        break;
240

241 242 243
      display_name = g_file_info_get_display_name (info);
      if (display_name == NULL)
        continue;
244 245 246 247

      if (g_file_info_get_is_hidden (info))
        continue;

248
      if (gtk_query_matches_string (data->query, display_name))
249 250 251 252
        {
          GtkSearchHit *hit;

          hit = g_new (GtkSearchHit, 1);
253
          hit->file = g_object_ref (child);
254 255 256
          hit->info = g_object_ref (info);
          data->hits = g_list_prepend (data->hits, hit);
        }
257

258 259 260
      data->n_processed_files++;
      if (data->n_processed_files > BATCH_SIZE)
        send_batch (data);
261

262 263 264
      if (data->recursive &&
          g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY &&
          !is_indexed (data->engine, child))
265
        queue_if_local (data, child);
266
    }
267

268
  g_object_unref (enumerator);
269 270
}

Matthias Clasen's avatar
Matthias Clasen committed
271
static gpointer
272 273 274
search_thread_func (gpointer user_data)
{
  SearchThreadData *data;
275 276
  GFile *dir;
  guint id;
Matthias Clasen's avatar
Matthias Clasen committed
277

278
  data = user_data;
Matthias Clasen's avatar
Matthias Clasen committed
279

280 281 282 283 284 285
  while (!g_cancellable_is_cancelled (data->cancellable) &&
         (dir = g_queue_pop_head (data->directories)) != NULL)
    {
      visit_directory (dir, data);
      g_object_unref (dir);
    }
286

287 288
  if (!g_cancellable_is_cancelled (data->cancellable))
    send_batch (data);
Matthias Clasen's avatar
Matthias Clasen committed
289

290 291
  id = gdk_threads_add_idle (search_thread_done_idle, data);
  g_source_set_name_by_id (id, "[gtk+] search_thread_done_idle");
Matthias Clasen's avatar
Matthias Clasen committed
292

293 294 295 296 297 298 299 300
  return NULL;
}

static void
gtk_search_engine_simple_start (GtkSearchEngine *engine)
{
  GtkSearchEngineSimple *simple;
  SearchThreadData *data;
Matthias Clasen's avatar
Matthias Clasen committed
301

302
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
Matthias Clasen's avatar
Matthias Clasen committed
303

304
  if (simple->active_search != NULL)
305
    return;
Matthias Clasen's avatar
Matthias Clasen committed
306

307
  if (simple->query == NULL)
308
    return;
Matthias Clasen's avatar
Matthias Clasen committed
309

310
  data = search_thread_data_new (simple, simple->query);
Matthias Clasen's avatar
Matthias Clasen committed
311

312
  g_thread_unref (g_thread_new ("file-search", search_thread_func, data));
Matthias Clasen's avatar
Matthias Clasen committed
313

314
  simple->active_search = data;
315 316 317 318 319 320
}

static void
gtk_search_engine_simple_stop (GtkSearchEngine *engine)
{
  GtkSearchEngineSimple *simple;
Matthias Clasen's avatar
Matthias Clasen committed
321

322
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
Matthias Clasen's avatar
Matthias Clasen committed
323

324
  if (simple->active_search != NULL)
325
    {
326 327
      g_cancellable_cancel (simple->active_search->cancellable);
      simple->active_search = NULL;
328 329 330 331
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
332
gtk_search_engine_simple_set_query (GtkSearchEngine *engine,
333 334 335
				    GtkQuery        *query)
{
  GtkSearchEngineSimple *simple;
Matthias Clasen's avatar
Matthias Clasen committed
336

337
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
Matthias Clasen's avatar
Matthias Clasen committed
338

339 340 341
  if (query)
    g_object_ref (query);

342 343
  if (simple->query)
    g_object_unref (simple->query);
344

345
  simple->query = query;
346 347 348 349 350 351 352
}

static void
_gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
{
  GObjectClass *gobject_class;
  GtkSearchEngineClass *engine_class;
Matthias Clasen's avatar
Matthias Clasen committed
353

354
  gobject_class = G_OBJECT_CLASS (class);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
355
  gobject_class->dispose = gtk_search_engine_simple_dispose;
Matthias Clasen's avatar
Matthias Clasen committed
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370
  engine_class = GTK_SEARCH_ENGINE_CLASS (class);
  engine_class->set_query = gtk_search_engine_simple_set_query;
  engine_class->start = gtk_search_engine_simple_start;
  engine_class->stop = gtk_search_engine_simple_stop;
}

static void
_gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
{
}

GtkSearchEngine *
_gtk_search_engine_simple_new (void)
{
371
  return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);
372
}
373 374 375 376

void
_gtk_search_engine_simple_set_indexed_cb (GtkSearchEngineSimple          *engine,
                                          GtkSearchEngineSimpleIsIndexed  callback,
377 378
                                          gpointer                        data,
                                          GDestroyNotify                  destroy)
379
{
380 381
  if (engine->is_indexed_data_destroy)
    engine->is_indexed_data_destroy (engine->is_indexed_data);
382

383 384 385
  engine->is_indexed_callback = callback;
  engine->is_indexed_data = data;
  engine->is_indexed_data_destroy = destroy;
386
}