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
|
/*
* Copyright (C) 2007 Kristian Rietveld <kris@gtk.org>
*
* 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/>.
*/
#include <Cocoa/Cocoa.h>
#include <quartz/gdkquartz.h>
#include "gtksearchenginequartz.h"
/* This file is a mixture of an objective-C object and a GObject,
* so be careful to not confuse yourself.
*/
#define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
#define QUARTZ_POOL_RELEASE [pool release]
/* Definition of objective-c object */
@interface ResultReceiver : NSObject
{
int submitted_hits;
GtkSearchEngine *engine;
}
- (void) setEngine:(GtkSearchEngine *)quartz_engine;
- (void) queryUpdate:(id)sender;
- (void) queryProgress:(id)sender;
- (void) queryFinished:(id)sender;
@end
/* Definition of GObject */
struct _GtkSearchEngineQuartzPrivate
{
GtkQuery *query;
ResultReceiver *receiver;
NSMetadataQuery *ns_query;
gboolean query_finished;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkSearchEngineQuartz, _gtk_search_engine_quartz, GTK_TYPE_SEARCH_ENGINE)
/* Implementation of the objective-C object */
@implementation ResultReceiver
- (void) setEngine:(GtkSearchEngine *)quartz_engine
{
g_return_if_fail (GTK_IS_SEARCH_ENGINE (quartz_engine));
engine = quartz_engine;
submitted_hits = 0;
}
- (void) submitHits:(NSMetadataQuery *)ns_query
{
int i;
GList *hits = NULL;
/* Here we submit hits "submitted_hits" to "resultCount" */
for (i = submitted_hits; i < [ns_query resultCount]; i++)
{
id result = [ns_query resultAtIndex:i];
char *result_path;
result_path = g_strdup_printf ("file://%s", [[result valueForAttribute:@"kMDItemPath"] cString]);
hits = g_list_prepend (hits, result_path);
}
_gtk_search_engine_hits_added (engine, hits);
g_list_free (hits);
submitted_hits = [ns_query resultCount];
/* The beagle backend stops at 1000 hits, so guess we do so too here.
* It works pretty snappy on my MacBook, if we get rid of this limit
* we are almost definitely going to need some code to submit hits
* in batches.
*/
if (submitted_hits > 1000)
[ns_query stopQuery];
}
- (void) queryUpdate:(id)sender
{
NSMetadataQuery *ns_query = [sender object];
[self submitHits:ns_query];
}
- (void) queryProgress:(id)sender
{
NSMetadataQuery *ns_query = [sender object];
[self submitHits:ns_query];
}
- (void) queryFinished:(id)sender
{
NSMetadataQuery *ns_query = [sender object];
[self submitHits:ns_query];
_gtk_search_engine_finished (engine);
submitted_hits = 0;
}
@end
/* Implementation of the GObject */
static void
gtk_search_engine_quartz_finalize (GObject *object)
{
GtkSearchEngineQuartz *quartz;
QUARTZ_POOL_ALLOC;
quartz = GTK_SEARCH_ENGINE_QUARTZ (object);
[[NSNotificationCenter defaultCenter] removeObserver:quartz->priv->receiver];
[quartz->priv->ns_query release];
[quartz->priv->receiver release];
QUARTZ_POOL_RELEASE;
if (quartz->priv->query)
{
g_object_unref (quartz->priv->query);
quartz->priv->query = NULL;
}
G_OBJECT_CLASS (_gtk_search_engine_quartz_parent_class)->finalize (object);
}
static void
gtk_search_engine_quartz_start (GtkSearchEngine *engine)
{
GtkSearchEngineQuartz *quartz;
QUARTZ_POOL_ALLOC;
quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
[quartz->priv->ns_query startQuery];
QUARTZ_POOL_RELEASE;
}
static void
gtk_search_engine_quartz_stop (GtkSearchEngine *engine)
{
GtkSearchEngineQuartz *quartz;
QUARTZ_POOL_ALLOC;
quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
[quartz->priv->ns_query stopQuery];
QUARTZ_POOL_RELEASE;
}
static void
gtk_search_engine_quartz_set_query (GtkSearchEngine *engine,
GtkQuery *query)
{
GtkSearchEngineQuartz *quartz;
QUARTZ_POOL_ALLOC;
quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
if (query)
g_object_ref (query);
if (quartz->priv->query)
g_object_unref (quartz->priv->query);
quartz->priv->query = query;
/* We create a query to look for ".*text.*" in the text contents of
* all indexed files. (Should we also search for text in file and folder
* names?).
*/
[quartz->priv->ns_query setPredicate:
[NSPredicate predicateWithFormat:
[NSString stringWithFormat:@"(kMDItemTextContent LIKE[cd] \"*%s*\")",
gtk_query_get_text (query)]]];
QUARTZ_POOL_RELEASE;
}
static void
_gtk_search_engine_quartz_class_init (GtkSearchEngineQuartzClass *class)
{
GObjectClass *gobject_class;
GtkSearchEngineClass *engine_class;
gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = gtk_search_engine_quartz_finalize;
engine_class = GTK_SEARCH_ENGINE_CLASS (class);
engine_class->set_query = gtk_search_engine_quartz_set_query;
engine_class->start = gtk_search_engine_quartz_start;
engine_class->stop = gtk_search_engine_quartz_stop;
}
static void
_gtk_search_engine_quartz_init (GtkSearchEngineQuartz *engine)
{
QUARTZ_POOL_ALLOC;
engine->priv = _gtk_search_engine_quartz_get_instance_private (engine);
engine->priv->ns_query = [[NSMetadataQuery alloc] init];
engine->priv->receiver = [[ResultReceiver alloc] init];
[engine->priv->receiver setEngine:GTK_SEARCH_ENGINE (engine)];
[[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
selector:@selector(queryUpdate:)
name:@"NSMetadataQueryDidUpdateNotification"
object:engine->priv->ns_query];
[[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
selector:@selector(queryFinished:)
name:@"NSMetadataQueryDidFinishGatheringNotification"
object:engine->priv->ns_query];
[[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
selector:@selector(queryProgress:)
name:@"NSMetadataQueryGatheringProgressNotification"
object:engine->priv->ns_query];
QUARTZ_POOL_RELEASE;
}
GtkSearchEngine *
_gtk_search_engine_quartz_new (void)
{
#ifdef GDK_WINDOWING_QUARTZ
return g_object_new (GTK_TYPE_SEARCH_ENGINE_QUARTZ, NULL);
#else
return NULL;
#endif
}
|