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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* Copyright (C) 2012 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/>.
*
*/
#include <config.h>
#include <string.h>
#include <gio/gio.h>
#include "nautilus-search-hit.h"
#include "nautilus-query.h"
#define DEBUG_FLAG NAUTILUS_DEBUG_SEARCH_HIT
#include "nautilus-debug.h"
struct NautilusSearchHitDetails
{
char *uri;
GDateTime *modification_time;
GDateTime *access_time;
gdouble fts_rank;
gdouble relevance;
};
enum {
PROP_URI = 1,
PROP_RELEVANCE,
PROP_MODIFICATION_TIME,
PROP_ACCESS_TIME,
PROP_FTS_RANK,
NUM_PROPERTIES
};
G_DEFINE_TYPE (NautilusSearchHit, nautilus_search_hit, G_TYPE_OBJECT)
void
nautilus_search_hit_compute_scores (NautilusSearchHit *hit,
NautilusQuery *query)
{
GDateTime *now;
GFile *query_location;
GFile *hit_location;
GTimeSpan m_diff = G_MAXINT64;
GTimeSpan a_diff = G_MAXINT64;
GTimeSpan t_diff = G_MAXINT64;
gdouble recent_bonus = 0.0;
gdouble proximity_bonus = 0.0;
gdouble match_bonus = 0.0;
query_location = nautilus_query_get_location (query);
hit_location = g_file_new_for_uri (hit->details->uri);
if (g_file_has_prefix (hit_location, query_location)) {
GFile *parent, *location;
guint dir_count = 0;
parent = g_file_get_parent (hit_location);
while (!g_file_equal (parent, query_location)) {
dir_count++;
location = parent;
parent = g_file_get_parent (location);
g_object_unref (location);
}
g_object_unref (parent);
if (dir_count < 10) {
proximity_bonus = 10000.0 - 1000.0 * dir_count;
}
}
g_object_unref (hit_location);
now = g_date_time_new_now_local ();
if (hit->details->modification_time != NULL)
m_diff = g_date_time_difference (now, hit->details->modification_time);
if (hit->details->access_time != NULL)
a_diff = g_date_time_difference (now, hit->details->access_time);
m_diff /= G_TIME_SPAN_DAY;
a_diff /= G_TIME_SPAN_DAY;
t_diff = MIN (m_diff, a_diff);
if (t_diff > 90) {
recent_bonus = 0.0;
} else if (t_diff > 30) {
recent_bonus = 10.0;
} else if (t_diff > 14) {
recent_bonus = 30.0;
} else if (t_diff > 7) {
recent_bonus = 50.0;
} else if (t_diff > 1) {
recent_bonus = 70.0;
} else {
recent_bonus = 100.0;
}
if (hit->details->fts_rank > 0) {
match_bonus = MIN (500, 10.0 * hit->details->fts_rank);
} else {
match_bonus = 0.0;
}
hit->details->relevance = recent_bonus + proximity_bonus + match_bonus;
DEBUG ("Hit %s computed relevance %.2f (%.2f + %.2f + %.2f)", hit->details->uri, hit->details->relevance,
proximity_bonus, recent_bonus, match_bonus);
g_date_time_unref (now);
g_object_unref (query_location);
}
const char *
nautilus_search_hit_get_uri (NautilusSearchHit *hit)
{
return hit->details->uri;
}
gdouble
nautilus_search_hit_get_relevance (NautilusSearchHit *hit)
{
return hit->details->relevance;
}
static void
nautilus_search_hit_set_uri (NautilusSearchHit *hit,
const char *uri)
{
g_free (hit->details->uri);
hit->details->uri = g_strdup (uri);
}
void
nautilus_search_hit_set_fts_rank (NautilusSearchHit *hit,
gdouble rank)
{
hit->details->fts_rank = rank;
}
void
nautilus_search_hit_set_modification_time (NautilusSearchHit *hit,
GDateTime *date)
{
if (hit->details->modification_time != NULL)
g_date_time_unref (hit->details->modification_time);
if (date != NULL)
hit->details->modification_time = g_date_time_ref (date);
else
hit->details->modification_time = NULL;
}
void
nautilus_search_hit_set_access_time (NautilusSearchHit *hit,
GDateTime *date)
{
if (hit->details->access_time != NULL)
g_date_time_unref (hit->details->access_time);
if (date != NULL)
hit->details->access_time = g_date_time_ref (date);
else
hit->details->access_time = NULL;
}
static void
nautilus_search_hit_set_property (GObject *object,
guint arg_id,
const GValue *value,
GParamSpec *pspec)
{
NautilusSearchHit *hit;
hit = NAUTILUS_SEARCH_HIT (object);
switch (arg_id) {
case PROP_RELEVANCE:
hit->details->relevance = g_value_get_double (value);
break;
case PROP_FTS_RANK:
nautilus_search_hit_set_fts_rank (hit, g_value_get_double (value));
break;
case PROP_URI:
nautilus_search_hit_set_uri (hit, g_value_get_string (value));
break;
case PROP_MODIFICATION_TIME:
nautilus_search_hit_set_modification_time (hit, g_value_get_boxed (value));
break;
case PROP_ACCESS_TIME:
nautilus_search_hit_set_access_time (hit, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, pspec);
break;
}
}
static void
nautilus_search_hit_get_property (GObject *object,
guint arg_id,
GValue *value,
GParamSpec *pspec)
{
NautilusSearchHit *hit;
hit = NAUTILUS_SEARCH_HIT (object);
switch (arg_id) {
case PROP_RELEVANCE:
g_value_set_double (value, hit->details->relevance);
break;
case PROP_FTS_RANK:
g_value_set_double (value, hit->details->fts_rank);
break;
case PROP_URI:
g_value_set_string (value, hit->details->uri);
break;
case PROP_MODIFICATION_TIME:
g_value_set_boxed (value, hit->details->modification_time);
break;
case PROP_ACCESS_TIME:
g_value_set_boxed (value, hit->details->access_time);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, pspec);
break;
}
}
static void
nautilus_search_hit_finalize (GObject *object)
{
NautilusSearchHit *hit = NAUTILUS_SEARCH_HIT (object);
g_free (hit->details->uri);
if (hit->details->access_time != NULL) {
g_date_time_unref (hit->details->access_time);
}
if (hit->details->modification_time != NULL) {
g_date_time_unref (hit->details->modification_time);
}
G_OBJECT_CLASS (nautilus_search_hit_parent_class)->finalize (object);
}
static void
nautilus_search_hit_class_init (NautilusSearchHitClass *class)
{
GObjectClass *object_class;
object_class = (GObjectClass *) class;
object_class->finalize = nautilus_search_hit_finalize;
object_class->get_property = nautilus_search_hit_get_property;
object_class->set_property = nautilus_search_hit_set_property;
g_object_class_install_property (object_class,
PROP_URI,
g_param_spec_string ("uri",
"URI",
"URI",
NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_READABLE));
g_object_class_install_property (object_class,
PROP_MODIFICATION_TIME,
g_param_spec_boxed ("modification-time",
"Modification time",
"Modification time",
G_TYPE_DATE_TIME,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_ACCESS_TIME,
g_param_spec_boxed ("access-time",
"acess time",
"access time",
G_TYPE_DATE_TIME,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (object_class,
PROP_RELEVANCE,
g_param_spec_double ("relevance",
NULL,
NULL,
-G_MAXDOUBLE, G_MAXDOUBLE,
0,
G_PARAM_READWRITE));
g_object_class_install_property (object_class,
PROP_FTS_RANK,
g_param_spec_double ("fts-rank",
NULL,
NULL,
-G_MAXDOUBLE, G_MAXDOUBLE,
0,
G_PARAM_READWRITE));
g_type_class_add_private (class, sizeof (NautilusSearchHitDetails));
}
static void
nautilus_search_hit_init (NautilusSearchHit *hit)
{
hit->details = G_TYPE_INSTANCE_GET_PRIVATE (hit,
NAUTILUS_TYPE_SEARCH_HIT,
NautilusSearchHitDetails);
}
NautilusSearchHit *
nautilus_search_hit_new (const char *uri)
{
NautilusSearchHit *hit;
hit = g_object_new (NAUTILUS_TYPE_SEARCH_HIT,
"uri", uri,
NULL);
return hit;
}
|