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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright (C) 2005 Red Hat, Inc
*
* Nautilus is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Nautilus 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*
* Author: Alexander Larsson <alexl@redhat.com>
*
*/
#define G_LOG_DOMAIN "nautilus-search"
#include <config.h>
#include "nautilus-search-engine-model.h"
#include "nautilus-directory.h"
#include "nautilus-directory-private.h"
#include "nautilus-file.h"
#include "nautilus-query.h"
#include "nautilus-search-hit.h"
#include "nautilus-search-provider.h"
#include "nautilus-ui-utilities.h"
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
struct _NautilusSearchEngineModel
{
NautilusSearchProvider parent_instance;
NautilusDirectory *directory;
guint finished_id;
};
G_DEFINE_FINAL_TYPE (NautilusSearchEngineModel,
nautilus_search_engine_model,
NAUTILUS_TYPE_SEARCH_PROVIDER)
static void
finalize (GObject *object)
{
NautilusSearchEngineModel *model;
model = NAUTILUS_SEARCH_ENGINE_MODEL (object);
if (model->finished_id != 0)
{
g_source_remove (model->finished_id);
model->finished_id = 0;
}
g_clear_object (&model->directory);
G_OBJECT_CLASS (nautilus_search_engine_model_parent_class)->finalize (object);
}
static gboolean
search_finished (NautilusSearchEngineModel *model)
{
model->finished_id = 0;
nautilus_search_provider_finished (NAUTILUS_SEARCH_PROVIDER (model));
return G_SOURCE_REMOVE;
}
static void
search_finished_idle (NautilusSearchEngineModel *model)
{
if (model->finished_id != 0)
{
return;
}
model->finished_id = g_idle_add ((GSourceFunc) search_finished, model);
}
static void
model_directory_ready_cb (NautilusDirectory *directory,
GList *list,
gpointer user_data)
{
NautilusSearchEngineModel *model = user_data;
gchar *uri;
GList *files, *l;
NautilusFile *file;
gdouble match;
gboolean found;
NautilusSearchHit *hit;
GDateTime *initial_date;
GDateTime *end_date;
GPtrArray *date_range;
NautilusQuery *query = nautilus_search_provider_get_query (model);
files = nautilus_directory_get_file_list (directory);
for (l = files; l != NULL; l = l->next)
{
g_autofree gchar *display_name = NULL;
g_autoptr (GDateTime) mtime = NULL;
g_autoptr (GDateTime) atime = NULL;
g_autoptr (GDateTime) ctime = NULL;
file = l->data;
match = nautilus_query_matches_string (query,
nautilus_file_get_display_name (file));
found = (match > -1);
if (!found)
{
continue;
}
const char *mime_type = nautilus_file_get_mime_type (file);
if (!nautilus_query_matches_mime_type (query, mime_type))
{
continue;
}
mtime = g_date_time_new_from_unix_local (nautilus_file_get_mtime (file));
atime = g_date_time_new_from_unix_local (nautilus_file_get_atime (file));
ctime = g_date_time_new_from_unix_local (nautilus_file_get_btime (file));
date_range = nautilus_query_get_date_range (query);
if (found && date_range != NULL)
{
NautilusSearchTimeType type;
GDateTime *target_date;
type = nautilus_query_get_search_type (query);
initial_date = g_ptr_array_index (date_range, 0);
end_date = g_ptr_array_index (date_range, 1);
switch (type)
{
case NAUTILUS_SEARCH_TIME_TYPE_LAST_ACCESS:
{
target_date = atime;
}
break;
case NAUTILUS_SEARCH_TIME_TYPE_LAST_MODIFIED:
{
target_date = mtime;
}
break;
case NAUTILUS_SEARCH_TIME_TYPE_CREATED:
{
target_date = ctime;
}
break;
default:
{
target_date = NULL;
}
}
found = nautilus_date_time_is_between_dates (target_date,
initial_date,
end_date);
g_ptr_array_unref (date_range);
}
if (found)
{
uri = nautilus_file_get_uri (file);
hit = nautilus_search_hit_new (uri);
nautilus_search_hit_set_fts_rank (hit, match);
nautilus_search_hit_set_modification_time (hit, mtime);
nautilus_search_hit_set_access_time (hit, atime);
nautilus_search_hit_set_creation_time (hit, ctime);
nautilus_search_provider_add_hit (model, hit);
g_free (uri);
}
}
nautilus_file_list_free (files);
search_finished (model);
}
static const char *
get_name (NautilusSearchProvider *provider)
{
return "model";
}
static gboolean
should_search (NautilusSearchProvider *provider,
NautilusQuery *query)
{
g_autoptr (GFile) location = nautilus_query_get_location (query);
g_autoptr (NautilusDirectory) directory = nautilus_directory_get (location);
return directory != NULL;
}
static void
start_search (NautilusSearchProvider *provider)
{
NautilusQuery *query = nautilus_search_provider_get_query (provider);
NautilusSearchEngineModel *model;
model = NAUTILUS_SEARCH_ENGINE_MODEL (provider);
g_autoptr (GFile) query_location = nautilus_query_get_location (query);
g_autoptr (NautilusDirectory) directory = nautilus_directory_get (query_location);
g_set_object (&model->directory, directory);
nautilus_directory_call_when_ready (model->directory,
NAUTILUS_ATTRIBUTE_INFO | NAUTILUS_ATTRIBUTE_FILE_LIST,
model_directory_ready_cb, model);
}
static void
search_engine_model_stop (NautilusSearchProvider *provider)
{
NautilusSearchEngineModel *self = NAUTILUS_SEARCH_ENGINE_MODEL (provider);
nautilus_directory_cancel_callback (self->directory,
model_directory_ready_cb, self);
search_finished_idle (self);
g_clear_object (&self->directory);
}
static void
nautilus_search_engine_model_class_init (NautilusSearchEngineModelClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
NautilusSearchProviderClass *search_provider_class = NAUTILUS_SEARCH_PROVIDER_CLASS (class);
gobject_class->finalize = finalize;
search_provider_class->get_name = get_name;
search_provider_class->should_search = should_search;
search_provider_class->start_search = start_search;
search_provider_class->stop_search = search_engine_model_stop;
}
static void
nautilus_search_engine_model_init (NautilusSearchEngineModel *engine)
{
}
NautilusSearchEngineModel *
nautilus_search_engine_model_new (void)
{
NautilusSearchEngineModel *engine;
engine = g_object_new (NAUTILUS_TYPE_SEARCH_ENGINE_MODEL, NULL);
return engine;
}
|