summaryrefslogtreecommitdiff
path: root/gtk/gtksearchenginemodel.c
blob: 2ff615246d69ef7e0eb272576a85de83fc3b5062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
/*
 * Copyright (C) 2015 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Matthias Clasen <mclasen@redhat.com>
 */

#include "config.h"

#include <gio/gio.h>

#include <gdk/gdk.h>

#include "gtksearchenginemodel.h"
#include "gtkprivate.h"

#include <string.h>

#define BATCH_SIZE 500

struct _GtkSearchEngineModel
{
  GtkSearchEngine parent;

  GtkFileSystemModel *model;
  GtkQuery *query;

  gboolean query_finished;
  guint idle;
};

struct _GtkSearchEngineModelClass
{
  GtkSearchEngineClass parent_class;
};

G_DEFINE_TYPE (GtkSearchEngineModel, _gtk_search_engine_model, GTK_TYPE_SEARCH_ENGINE)

static void
gtk_search_engine_model_dispose (GObject *object)
{
  GtkSearchEngineModel *model = GTK_SEARCH_ENGINE_MODEL (object);

  g_clear_object (&model->query);
  g_clear_object (&model->model);

  G_OBJECT_CLASS (_gtk_search_engine_model_parent_class)->dispose (object);
}

static gboolean
info_matches_query (GtkQuery  *query,
                    GFileInfo *info)
{
  const gchar *display_name;

  display_name = g_file_info_get_display_name (info);
  if (display_name == NULL)
    return FALSE;

  if (g_file_info_get_is_hidden (info))
    return FALSE;

  if (!gtk_query_matches_string (query, display_name))
    return FALSE;

  return TRUE;
}

static gboolean
do_search (gpointer data)
{
  GtkSearchEngineModel *model = data;
  GtkTreeIter iter;
  GList *hits = NULL;

  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model->model), &iter))
    {
      do
        {
          GFileInfo *info;

          info = _gtk_file_system_model_get_info (model->model, &iter);
          if (info_matches_query (model->query, info))
            {
              GFile *file;
              GtkSearchHit *hit;

              file = _gtk_file_system_model_get_file (model->model, &iter);
              hit = g_new (GtkSearchHit, 1);
              hit->file = g_object_ref (file);
              hit->info = g_object_ref (info);
              hits = g_list_prepend (hits, hit);
            }
        }
      while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model->model), &iter));

      if (hits)
        {
          _gtk_search_engine_hits_added (GTK_SEARCH_ENGINE (model), hits);
          g_list_free_full (hits, (GDestroyNotify)_gtk_search_hit_free);
        }
    }

  model->idle = 0;

  return G_SOURCE_REMOVE;
}

static void
gtk_search_engine_model_start (GtkSearchEngine *engine)
{
  GtkSearchEngineModel *model;

  model = GTK_SEARCH_ENGINE_MODEL (engine);

  if (model->query == NULL)
    return;

  model->idle = g_idle_add (do_search, engine);
  g_source_set_name_by_id (model->idle, "[gtk+] gtk_search_engine_model_start");
}

static void
gtk_search_engine_model_stop (GtkSearchEngine *engine)
{
  GtkSearchEngineModel *model;

  model = GTK_SEARCH_ENGINE_MODEL (engine);

  if (model->idle != 0)
    {
      g_source_remove (model->idle);
      model->idle = 0;
    }
}

static void
gtk_search_engine_model_set_query (GtkSearchEngine *engine,
                                   GtkQuery        *query)
{
  GtkSearchEngineModel *model = GTK_SEARCH_ENGINE_MODEL (engine);

  g_set_object (&model->query, query);
}

static void
_gtk_search_engine_model_class_init (GtkSearchEngineModelClass *class)
{
  GObjectClass *gobject_class;
  GtkSearchEngineClass *engine_class;

  gobject_class = G_OBJECT_CLASS (class);
  gobject_class->dispose = gtk_search_engine_model_dispose;

  engine_class = GTK_SEARCH_ENGINE_CLASS (class);
  engine_class->set_query = gtk_search_engine_model_set_query;
  engine_class->start = gtk_search_engine_model_start;
  engine_class->stop = gtk_search_engine_model_stop;
}

static void
_gtk_search_engine_model_init (GtkSearchEngineModel *engine)
{
}

GtkSearchEngine *
_gtk_search_engine_model_new (GtkFileSystemModel *model)
{
  GtkSearchEngineModel *engine;

  engine = GTK_SEARCH_ENGINE_MODEL (g_object_new (GTK_TYPE_SEARCH_ENGINE_MODEL, NULL));
  engine->model = g_object_ref (model);

  return GTK_SEARCH_ENGINE (engine);
}