gtksearchenginesimple.c 8.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *
 * Based on nautilus-search-engine-simple.c
 */

23 24
#include <config.h>

25 26 27 28 29
/* these must be defined even when HAVE_GNU_FTW is not defined
 * because (really) old versions of GNU libc have ftw.h but do
 * export ftw() and friends only if _XOPEN_SOURCE and _GNU_SOURCE
 * are defined. see bug #444097.
 */
30 31
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE 
32 33 34 35

#ifdef HAVE_FTW_H
#include <ftw.h>
#endif
36 37 38 39 40 41 42

#include "gtksearchenginesimple.h"

#define XDG_PREFIX _gtk_xdg
#include "xdgmime/xdgmime.h"

#include <string.h>
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#include <glib/gstrfuncs.h>

#define BATCH_SIZE 500

typedef struct 
{
  GtkSearchEngineSimple *engine;
  
  gchar *path;
  GList *mime_types;
  gchar **words;
  GList *found_list;
  
  gint n_processed_files;
  GList *uri_hits;
  
  /* accessed on both threads: */
  volatile gboolean cancelled;
} SearchThreadData;


struct _GtkSearchEngineSimplePrivate 
{
  GtkQuery *query;
  
  SearchThreadData *active_search;
  
  gboolean query_finished;
};


G_DEFINE_TYPE (GtkSearchEngineSimple, _gtk_search_engine_simple, GTK_TYPE_SEARCH_ENGINE);

static void
finalize (GObject *object)
{
  GtkSearchEngineSimple *simple;
  
  simple = GTK_SEARCH_ENGINE_SIMPLE (object);
  
  if (simple->priv->query) 
    {
      g_object_unref (simple->priv->query);
      simple->priv->query = NULL;
    }
  
  G_OBJECT_CLASS (_gtk_search_engine_simple_parent_class)->finalize (object);
}

static SearchThreadData *
search_thread_data_new (GtkSearchEngineSimple *engine,
			GtkQuery              *query)
{
  SearchThreadData *data;
  char *text, *lower, *uri;
  
  data = g_new0 (SearchThreadData, 1);
  
  data->engine = engine;
  uri = _gtk_query_get_location (query);
  if (uri != NULL) 
    {
      data->path = g_filename_from_uri (uri, NULL, NULL);
      g_free (uri);
    }
  if (data->path == NULL)
    data->path = g_strdup (g_get_home_dir ());
	
  text = _gtk_query_get_text (query);
  lower = g_ascii_strdown (text, -1);
  data->words = g_strsplit (lower, " ", -1);
  g_free (text);
  g_free (lower);
  
  data->mime_types = _gtk_query_get_mime_types (query);
  
  return data;
}

static void 
search_thread_data_free (SearchThreadData *data)
{
  g_free (data->path);
  g_strfreev (data->words);
  g_list_foreach (data->mime_types, (GFunc)g_free, NULL);
  g_list_free (data->mime_types);
  g_free (data);
}

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

  data = user_data;
  
  if (!data->cancelled) 
    {
      _gtk_search_engine_finished (GTK_SEARCH_ENGINE (data->engine));
      data->engine->priv->active_search = NULL;
    }
  
  search_thread_data_free (data);
  
  return FALSE;
}

typedef struct 
{
  GList *uris;
  SearchThreadData *thread_data;
} SearchHits;


static gboolean
search_thread_add_hits_idle (gpointer user_data)
{
  SearchHits *hits;

  hits = user_data;

  if (!hits->thread_data->cancelled) 
    {
      _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (hits->thread_data->engine),
				    hits->uris);
    }

  g_list_foreach (hits->uris, (GFunc)g_free, NULL);
  g_list_free (hits->uris);
  g_free (hits);
	
  return FALSE;
}

static void
send_batch (SearchThreadData *data)
{
  SearchHits *hits;
  
  data->n_processed_files = 0;
  
  if (data->uri_hits) 
    {
      hits = g_new (SearchHits, 1);
      hits->uris = data->uri_hits;
      hits->thread_data = data;
      g_idle_add (search_thread_add_hits_idle, hits);
    }
  data->uri_hits = NULL;
}

static GStaticPrivate search_thread_data = G_STATIC_PRIVATE_INIT;

197
#ifdef HAVE_FTW_H
198 199 200 201 202 203 204 205 206
static int
search_visit_func (const char        *fpath,
		   const struct stat *sb,
		   int                typeflag,
		   struct FTW        *ftwbuf)
{
  SearchThreadData *data;
  gint i;
  const gchar *name; 
207
  gchar *lower_name, *mime_type;
208 209 210 211 212 213
  gchar *uri;
  gboolean hit;
  GList *l;
  gboolean is_hidden;
  
  data = (SearchThreadData*)g_static_private_get (&search_thread_data);
214

215
  if (data->cancelled)
216
#ifdef HAVE_GNU_FTW
217
    return FTW_STOP;
218 219 220
#else
    return 1;
#endif /* HAVE_GNU_FTW */
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

  name = strrchr (fpath, '/');
  if (name)
    name++;
  else
    name = fpath;

  is_hidden = *name == '.';
	
  hit = FALSE;
  
  if (!is_hidden) 
    {
      lower_name = g_ascii_strdown (name, -1);
      
      hit = TRUE;
      for (i = 0; data->words[i] != NULL; i++) 
	{
	  if (strstr (lower_name, data->words[i]) == NULL) 
	    {
	      hit = FALSE;
	      break;
	    }
	}
      g_free (lower_name);
    }

  if (hit && data->mime_types != NULL) 
    {
      hit = FALSE;
251
      mime_type = xdg_mime_get_mime_type_for_file (fpath, (struct stat *)sb);
252 253 254 255 256 257 258 259 260 261 262 263 264 265
      for (l = data->mime_types; l != NULL; l = l->next) 
	{
	  if (strcmp (mime_type, l->data) == 0) 
	    {
	      hit = TRUE;
	      break;
	    }
	}

      g_free (mime_type);
    }

  if (hit) 
    {
266
      uri = g_filename_to_uri (fpath, NULL, NULL);
267 268 269 270 271 272 273
      data->uri_hits = g_list_prepend (data->uri_hits, uri);
    }

  data->n_processed_files++;
  
  if (data->n_processed_files > BATCH_SIZE)
    send_batch (data);
274

275
#ifdef HAVE_GNU_FTW
276 277 278 279
  if (is_hidden)
    return FTW_SKIP_SUBTREE;
  else
    return FTW_CONTINUE;
280 281 282
#else
  return 0;
#endif /* HAVE_GNU_FTW */
283
}
284
#endif /* HAVE_FTW_H */
285 286 287 288

static gpointer 
search_thread_func (gpointer user_data)
{
289
#ifdef HAVE_FTW_H
290 291 292 293 294 295
  SearchThreadData *data;
  
  data = user_data;
  
  g_static_private_set (&search_thread_data, data, NULL);

296 297 298 299 300
  nftw (data->path, search_visit_func, 20,
#ifdef HAVE_GNU_FTW
        FTW_ACTIONRETVAL |
#endif
        FTW_PHYS);
301 302 303 304

  send_batch (data);
  
  g_idle_add (search_thread_done_idle, data);
305
#endif /* HAVE_FTW_H */
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  
  return NULL;
}

static void
gtk_search_engine_simple_start (GtkSearchEngine *engine)
{
  GtkSearchEngineSimple *simple;
  SearchThreadData *data;
  
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
  
  if (simple->priv->active_search != NULL)
    return;
  
  if (simple->priv->query == NULL)
    return;
	
  data = search_thread_data_new (simple, simple->priv->query);
  
  g_thread_create (search_thread_func, data, FALSE, NULL);
  
  simple->priv->active_search = data;
}

static void
gtk_search_engine_simple_stop (GtkSearchEngine *engine)
{
  GtkSearchEngineSimple *simple;
  
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
  
  if (simple->priv->active_search != NULL) 
    {
      simple->priv->active_search->cancelled = TRUE;
      simple->priv->active_search = NULL;
    }
}

static gboolean
gtk_search_engine_simple_is_indexed (GtkSearchEngine *engine)
{
  return FALSE;
}

static void
gtk_search_engine_simple_set_query (GtkSearchEngine *engine, 
				    GtkQuery        *query)
{
  GtkSearchEngineSimple *simple;
  
  simple = GTK_SEARCH_ENGINE_SIMPLE (engine);
  
  if (query)
    g_object_ref (query);

  if (simple->priv->query) 
    g_object_unref (simple->priv->query);

  simple->priv->query = query;
}

static void
_gtk_search_engine_simple_class_init (GtkSearchEngineSimpleClass *class)
{
  GObjectClass *gobject_class;
  GtkSearchEngineClass *engine_class;
  
  gobject_class = G_OBJECT_CLASS (class);
  gobject_class->finalize = finalize;
  
  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;
  engine_class->is_indexed = gtk_search_engine_simple_is_indexed;

  g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineSimplePrivate));
}

static void
_gtk_search_engine_simple_init (GtkSearchEngineSimple *engine)
{
  engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_SIMPLE, GtkSearchEngineSimplePrivate);
}

GtkSearchEngine *
_gtk_search_engine_simple_new (void)
{
395 396 397 398 399
#ifdef HAVE_FTW_H
  return g_object_new (GTK_TYPE_SEARCH_ENGINE_SIMPLE, NULL);
#else
  return NULL;
#endif
400
}