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
188
189
190
191
/*
 * 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 <cdk/cdk.h>

#include "ctksearchenginemodel.h"
#include "ctkprivate.h"

#include <string.h>

#define BATCH_SIZE 500

struct _CtkSearchEngineModel
{
  CtkSearchEngine parent;

  CtkFileSystemModel *model;
  CtkQuery *query;

  gboolean query_finished;
  guint idle;
};

struct _CtkSearchEngineModelClass
{
  CtkSearchEngineClass parent_class;
};

G_DEFINE_TYPE (CtkSearchEngineModel, _ctk_search_engine_model, CTK_TYPE_SEARCH_ENGINE)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE is a macro then please configure it.

static void
ctk_search_engine_model_dispose (GObject *object)
{
  CtkSearchEngineModel *model = CTK_SEARCH_ENGINE_MODEL (object);

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

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

gboolean
info_matches_query (CtkQuery  *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 (!ctk_query_matches_string (query, display_name))
    return FALSE;

  return TRUE;
}

static gboolean
do_search (gpointer data)
{
  CtkSearchEngineModel *model = data;
  CtkTreeIter iter;
  GList *hits = NULL;
  gboolean got_results = FALSE;

  if (ctk_tree_model_get_iter_first (CTK_TREE_MODEL (model->model), &iter))
    {
      do
        {
          GFileInfo *info;

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

              file = _ctk_file_system_model_get_file (model->model, &iter);
              hit = g_new (CtkSearchHit, 1);
              hit->file = g_object_ref (file);
              hit->info = g_object_ref (info);
              hits = g_list_prepend (hits, hit);
            }
        }
      while (ctk_tree_model_iter_next (CTK_TREE_MODEL (model->model), &iter));

      if (hits)
        {
          _ctk_search_engine_hits_added (CTK_SEARCH_ENGINE (model), hits);
          g_list_free_full (hits, (GDestroyNotify)_ctk_search_hit_free);
          got_results = TRUE;
        }
    }

  model->idle = 0;

  _ctk_search_engine_finished (CTK_SEARCH_ENGINE (model), got_results);

  return G_SOURCE_REMOVE;
}

static void
ctk_search_engine_model_start (CtkSearchEngine *engine)
{
  CtkSearchEngineModel *model;

  model = CTK_SEARCH_ENGINE_MODEL (engine);

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

  model->idle = cdk_threads_add_idle (do_search, engine);
  g_source_set_name_by_id (model->idle, "[ctk+] ctk_search_engine_model_start");
}

static void
ctk_search_engine_model_stop (CtkSearchEngine *engine)
{
  CtkSearchEngineModel *model;

  model = CTK_SEARCH_ENGINE_MODEL (engine);

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

static void
ctk_search_engine_model_set_query (CtkSearchEngine *engine,
                                   CtkQuery        *query)
{
  CtkSearchEngineModel *model = CTK_SEARCH_ENGINE_MODEL (engine);

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

static void
_ctk_search_engine_model_class_init (CtkSearchEngineModelClass *class)
{
  GObjectClass *gobject_class;
  CtkSearchEngineClass *engine_class;

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

  engine_class = CTK_SEARCH_ENGINE_CLASS (class);
  engine_class->set_query = ctk_search_engine_model_set_query;
  engine_class->start = ctk_search_engine_model_start;
  engine_class->stop = ctk_search_engine_model_stop;
}

static void
_ctk_search_engine_model_init (CtkSearchEngineModel *engine G_GNUC_UNUSED)
{
}

CtkSearchEngine *
_ctk_search_engine_model_new (CtkFileSystemModel *model)
{
  CtkSearchEngineModel *engine;

  engine = CTK_SEARCH_ENGINE_MODEL (g_object_new (CTK_TYPE_SEARCH_ENGINE_MODEL, NULL));
  engine->model = g_object_ref (model);

  return CTK_SEARCH_ENGINE (engine);
}