2 * Copyright (C) 2007 Kristian Rietveld <kris@gtk.org>
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.
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.
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,
19 #include <Cocoa/Cocoa.h>
20 #include <quartz/gdkquartz.h>
22 #include "gtksearchenginequartz.h"
24 /* This file is a mixture of an objective-C object and a GObject,
25 * so be careful to not confuse yourself.
28 #define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
29 #define QUARTZ_POOL_RELEASE [pool release]
32 /* Definition of objective-c object */
33 @interface ResultReceiver : NSObject
36 GtkSearchEngine *engine;
39 - (void) setEngine:(GtkSearchEngine *)quartz_engine;
41 - (void) queryUpdate:(id)sender;
42 - (void) queryProgress:(id)sender;
43 - (void) queryFinished:(id)sender;
48 /* Definition of GObject */
49 struct _GtkSearchEngineQuartzPrivate
53 ResultReceiver *receiver;
54 NSMetadataQuery *ns_query;
56 gboolean query_finished;
59 G_DEFINE_TYPE (GtkSearchEngineQuartz, _gtk_search_engine_quartz, GTK_TYPE_SEARCH_ENGINE);
62 /* Implementation of the objective-C object */
63 @implementation ResultReceiver
65 - (void) setEngine:(GtkSearchEngine *)quartz_engine
67 g_return_if_fail (GTK_IS_SEARCH_ENGINE (quartz_engine));
69 engine = quartz_engine;
73 - (void) submitHits:(NSMetadataQuery *)ns_query
78 /* Here we submit hits "submitted_hits" to "resultCount" */
79 for (i = submitted_hits; i < [ns_query resultCount]; i++)
81 id result = [ns_query resultAtIndex:i];
84 result_path = g_strdup_printf ("file://%s", [[result valueForAttribute:@"kMDItemPath"] cString]);
85 hits = g_list_prepend (hits, result_path);
88 _gtk_search_engine_hits_added (engine, hits);
91 submitted_hits = [ns_query resultCount];
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
98 if (submitted_hits > 1000)
102 - (void) queryUpdate:(id)sender
104 NSMetadataQuery *ns_query = [sender object];
106 [self submitHits:ns_query];
109 - (void) queryProgress:(id)sender
111 NSMetadataQuery *ns_query = [sender object];
113 [self submitHits:ns_query];
116 - (void) queryFinished:(id)sender
118 NSMetadataQuery *ns_query = [sender object];
120 [self submitHits:ns_query];
122 _gtk_search_engine_finished (engine);
128 /* Implementation of the GObject */
131 gtk_search_engine_quartz_finalize (GObject *object)
133 GtkSearchEngineQuartz *quartz;
137 quartz = GTK_SEARCH_ENGINE_QUARTZ (object);
139 [[NSNotificationCenter defaultCenter] removeObserver:quartz->priv->receiver];
141 [quartz->priv->ns_query release];
142 [quartz->priv->receiver release];
146 if (quartz->priv->query)
148 g_object_unref (quartz->priv->query);
149 quartz->priv->query = NULL;
152 G_OBJECT_CLASS (_gtk_search_engine_quartz_parent_class)->finalize (object);
156 gtk_search_engine_quartz_start (GtkSearchEngine *engine)
158 GtkSearchEngineQuartz *quartz;
162 quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
164 [quartz->priv->ns_query startQuery];
170 gtk_search_engine_quartz_stop (GtkSearchEngine *engine)
172 GtkSearchEngineQuartz *quartz;
176 quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
178 [quartz->priv->ns_query stopQuery];
184 gtk_search_engine_quartz_is_indexed (GtkSearchEngine *engine)
190 gtk_search_engine_quartz_set_query (GtkSearchEngine *engine,
193 GtkSearchEngineQuartz *quartz;
197 quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
200 g_object_ref (query);
202 if (quartz->priv->query)
203 g_object_unref (quartz->priv->query);
205 quartz->priv->query = query;
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
211 [quartz->priv->ns_query setPredicate:
212 [NSPredicate predicateWithFormat:
213 [NSString stringWithFormat:@"(kMDItemTextContent LIKE[cd] \"*%s*\")",
214 _gtk_query_get_text (query)]]];
220 _gtk_search_engine_quartz_class_init (GtkSearchEngineQuartzClass *class)
222 GObjectClass *gobject_class;
223 GtkSearchEngineClass *engine_class;
225 gobject_class = G_OBJECT_CLASS (class);
226 gobject_class->finalize = gtk_search_engine_quartz_finalize;
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;
234 g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineQuartzPrivate));
238 _gtk_search_engine_quartz_init (GtkSearchEngineQuartz *engine)
242 engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartzPrivate);
244 engine->priv->ns_query = [[NSMetadataQuery alloc] init];
245 engine->priv->receiver = [[ResultReceiver alloc] init];
247 [engine->priv->receiver setEngine:GTK_SEARCH_ENGINE (engine)];
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];
258 [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
259 selector:@selector(queryProgress:)
260 name:@"NSMetadataQueryGatheringProgressNotification"
261 object:engine->priv->ns_query];
267 _gtk_search_engine_quartz_new (void)
269 #ifdef GDK_WINDOWING_QUARTZ
270 return g_object_new (GTK_TYPE_SEARCH_ENGINE_QUARTZ, NULL);