]> Pileus Git - ~andy/gtk/blob - gtk/gtksearchenginequartz.c
5d3d5004e57c09f33c1450038d693d00aad4df67
[~andy/gtk] / gtk / gtksearchenginequartz.c
1 /*
2  * Copyright (C) 2007  Kristian Rietveld  <kris@gtk.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  */
18
19 #include <Cocoa/Cocoa.h>
20 #include <quartz/gdkquartz.h>
21
22 #include "gtksearchenginequartz.h"
23
24 /* This file is a mixture of an objective-C object and a GObject,
25  * so be careful to not confuse yourself.
26  */
27
28 #define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
29 #define QUARTZ_POOL_RELEASE [pool release]
30
31
32 /* Definition of objective-c object */
33 @interface ResultReceiver : NSObject
34 {
35   int submitted_hits;
36   GtkSearchEngine *engine;
37 }
38
39 - (void) setEngine:(GtkSearchEngine *)quartz_engine;
40
41 - (void) queryUpdate:(id)sender;
42 - (void) queryProgress:(id)sender;
43 - (void) queryFinished:(id)sender;
44
45 @end
46
47
48 /* Definition of GObject */
49 struct _GtkSearchEngineQuartzPrivate
50 {
51   GtkQuery *query;
52
53   ResultReceiver *receiver;
54   NSMetadataQuery *ns_query;
55
56   gboolean query_finished;
57 };
58
59 G_DEFINE_TYPE (GtkSearchEngineQuartz, _gtk_search_engine_quartz, GTK_TYPE_SEARCH_ENGINE);
60
61
62 /* Implementation of the objective-C object */
63 @implementation ResultReceiver
64
65 - (void) setEngine:(GtkSearchEngine *)quartz_engine
66 {
67   g_return_if_fail (GTK_IS_SEARCH_ENGINE (quartz_engine));
68
69   engine = quartz_engine;
70   submitted_hits = 0;
71 }
72
73 - (void) submitHits:(NSMetadataQuery *)ns_query
74 {
75   int i;
76   GList *hits = NULL;
77
78   /* Here we submit hits "submitted_hits" to "resultCount" */
79   for (i = submitted_hits; i < [ns_query resultCount]; i++)
80     {
81       id result = [ns_query resultAtIndex:i];
82       char *result_path;
83
84       result_path = g_strdup_printf ("file://%s", [[result valueForAttribute:@"kMDItemPath"] cString]);
85       hits = g_list_prepend (hits, result_path);
86     }
87
88   _gtk_search_engine_hits_added (engine, hits);
89   g_list_free (hits);
90
91   submitted_hits = [ns_query resultCount];
92
93   /* The beagle backend stops at 1000 hits, so guess we do so too here.
94    * It works pretty snappy on my MacBook, if we get rid of this limit
95    * we are almost definitely going to need some code to submit hits
96    * in batches.
97    */
98   if (submitted_hits > 1000)
99     [ns_query stopQuery];
100 }
101
102 - (void) queryUpdate:(id)sender
103 {
104   NSMetadataQuery *ns_query = [sender object];
105
106   [self submitHits:ns_query];
107 }
108
109 - (void) queryProgress:(id)sender
110 {
111   NSMetadataQuery *ns_query = [sender object];
112
113   [self submitHits:ns_query];
114 }
115
116 - (void) queryFinished:(id)sender
117 {
118   NSMetadataQuery *ns_query = [sender object];
119
120   [self submitHits:ns_query];
121
122   _gtk_search_engine_finished (engine);
123   submitted_hits = 0;
124 }
125
126 @end
127
128 /* Implementation of the GObject */
129
130 static void
131 gtk_search_engine_quartz_finalize (GObject *object)
132 {
133   GtkSearchEngineQuartz *quartz;
134
135   QUARTZ_POOL_ALLOC;
136
137   quartz = GTK_SEARCH_ENGINE_QUARTZ (object);
138
139   [[NSNotificationCenter defaultCenter] removeObserver:quartz->priv->receiver];
140
141   [quartz->priv->ns_query release];
142   [quartz->priv->receiver release];
143
144   QUARTZ_POOL_RELEASE;
145
146   if (quartz->priv->query)
147     {
148       g_object_unref (quartz->priv->query);
149       quartz->priv->query = NULL;
150     }
151
152   G_OBJECT_CLASS (_gtk_search_engine_quartz_parent_class)->finalize (object);
153 }
154
155 static void
156 gtk_search_engine_quartz_start (GtkSearchEngine *engine)
157 {
158   GtkSearchEngineQuartz *quartz;
159
160   QUARTZ_POOL_ALLOC;
161
162   quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
163
164   [quartz->priv->ns_query startQuery];
165
166   QUARTZ_POOL_RELEASE;
167 }
168
169 static void
170 gtk_search_engine_quartz_stop (GtkSearchEngine *engine)
171 {
172   GtkSearchEngineQuartz *quartz;
173
174   QUARTZ_POOL_ALLOC;
175
176   quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
177
178   [quartz->priv->ns_query stopQuery];
179
180   QUARTZ_POOL_RELEASE;
181 }
182
183 static gboolean
184 gtk_search_engine_quartz_is_indexed (GtkSearchEngine *engine)
185 {
186   return TRUE;
187 }
188
189 static void
190 gtk_search_engine_quartz_set_query (GtkSearchEngine *engine, 
191                                     GtkQuery        *query)
192 {
193   GtkSearchEngineQuartz *quartz;
194
195   QUARTZ_POOL_ALLOC;
196
197   quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
198
199   if (query)
200     g_object_ref (query);
201
202   if (quartz->priv->query)
203     g_object_unref (quartz->priv->query);
204
205   quartz->priv->query = query;
206
207   /* We create a query to look for ".*text.*" in the text contents of
208    * all indexed files.  (Should we also search for text in file and folder
209    * names?).
210    */
211   [quartz->priv->ns_query setPredicate:
212     [NSPredicate predicateWithFormat:
213       [NSString stringWithFormat:@"(kMDItemTextContent LIKE[cd] \"*%s*\")",
214                                  _gtk_query_get_text (query)]]];
215
216   QUARTZ_POOL_RELEASE;
217 }
218
219 static void
220 _gtk_search_engine_quartz_class_init (GtkSearchEngineQuartzClass *class)
221 {
222   GObjectClass *gobject_class;
223   GtkSearchEngineClass *engine_class;
224   
225   gobject_class = G_OBJECT_CLASS (class);
226   gobject_class->finalize = gtk_search_engine_quartz_finalize;
227   
228   engine_class = GTK_SEARCH_ENGINE_CLASS (class);
229   engine_class->set_query = gtk_search_engine_quartz_set_query;
230   engine_class->start = gtk_search_engine_quartz_start;
231   engine_class->stop = gtk_search_engine_quartz_stop;
232   engine_class->is_indexed = gtk_search_engine_quartz_is_indexed;
233
234   g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineQuartzPrivate));
235 }
236
237 static void
238 _gtk_search_engine_quartz_init (GtkSearchEngineQuartz *engine)
239 {
240   QUARTZ_POOL_ALLOC;
241
242   engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartzPrivate);
243
244   engine->priv->ns_query = [[NSMetadataQuery alloc] init];
245   engine->priv->receiver = [[ResultReceiver alloc] init];
246
247   [engine->priv->receiver setEngine:GTK_SEARCH_ENGINE (engine)];
248
249   [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
250                                         selector:@selector(queryUpdate:)
251                                         name:@"NSMetadataQueryDidUpdateNotification"
252                                         object:engine->priv->ns_query];
253   [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
254                                         selector:@selector(queryFinished:)
255                                         name:@"NSMetadataQueryDidFinishGatheringNotification"
256                                         object:engine->priv->ns_query];
257
258   [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
259                                         selector:@selector(queryProgress:)
260                                         name:@"NSMetadataQueryGatheringProgressNotification"
261                                         object:engine->priv->ns_query];
262
263   QUARTZ_POOL_RELEASE;
264 }
265
266 GtkSearchEngine *
267 _gtk_search_engine_quartz_new (void)
268 {
269 #ifdef GDK_WINDOWING_QUARTZ
270   return g_object_new (GTK_TYPE_SEARCH_ENGINE_QUARTZ, NULL);
271 #else
272   return NULL;
273 #endif
274 }