2006-12-16 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / libbeagle / examples / beagle-search.c
bloba8d0e178ba4a22334a2144104f70244051087106
1 #include <stdlib.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <beagle/beagle.h>
6 static int total_hits;
8 static void
9 print_feed_item_hit (BeagleHit *hit)
11 const char *text;
13 if (beagle_hit_get_one_property (hit, "dc:title", &text))
14 g_print ("Blog: %s\n", text);
17 static void
18 print_file_hit (BeagleHit *hit)
20 g_print ("File: %s\n", beagle_hit_get_uri (hit));
23 static void
24 print_other_hit (BeagleHit *hit)
26 g_print ("%s (%s)", beagle_hit_get_uri (hit),
27 beagle_hit_get_source (hit));
30 static void
31 print_hit (BeagleHit *hit)
33 if (strcmp (beagle_hit_get_type (hit), "FeedItem") == 0) {
34 print_feed_item_hit (hit);
36 else if (strcmp (beagle_hit_get_type (hit), "File") == 0) {
37 print_file_hit (hit);
38 } else {
39 print_other_hit (hit);
43 static void
44 hits_added_cb (BeagleQuery *query, BeagleHitsAddedResponse *response)
46 GSList *hits, *l;
47 gint i;
48 gint nr_hits;
49 gint total_matches;
51 hits = beagle_hits_added_response_get_hits (response);
52 total_matches = beagle_hits_added_response_get_num_matches (response);
54 nr_hits = g_slist_length (hits);
55 total_hits += nr_hits;
56 g_print ("Found hits (%d) out of total %d matches:\n", nr_hits, total_matches);
57 g_print ("-------------------------------------------\n");
58 for (l = hits, i = 1; l; l = l->next, ++i) {
59 g_print ("[%d] ", i);
61 print_hit (BEAGLE_HIT (l->data));
63 g_print ("\n");
65 g_print ("-------------------------------------------\n\n\n");
68 static void
69 finished_cb (BeagleQuery *query,
70 BeagleFinishedResponse *response,
71 GMainLoop *main_loop)
73 g_main_loop_quit (main_loop);
76 static void
77 indexing_status_cb (BeagleInformationalMessagesRequest *request,
78 BeagleIndexingStatusResponse *response,
79 gpointer user_data)
81 g_print ("Daemon is indexing: %s\n", beagle_indexing_status_response_is_indexing (response) ? "YES" : "NO");
84 int
85 main (int argc, char **argv)
87 BeagleClient *client;
88 BeagleInformationalMessagesRequest *info_req;
89 BeagleQuery *query;
90 GMainLoop *main_loop;
91 gint i;
93 if (argc < 2) {
94 g_print ("Usage %s \"query string\"\n", argv[0]);
95 exit (1);
98 g_type_init ();
100 total_hits = 0;
102 client = beagle_client_new (NULL);
104 if (client == NULL) {
105 g_warning ("Unable to establish a connection to the beagle daemon");
106 return 1;
109 main_loop = g_main_loop_new (NULL, FALSE);
111 info_req = beagle_informational_messages_request_new ();
112 g_signal_connect (info_req, "indexing-status",
113 G_CALLBACK (indexing_status_cb),
114 NULL);
115 beagle_client_send_request_async (client, BEAGLE_REQUEST (info_req),
116 NULL);
118 query = beagle_query_new ();
120 for (i = 1; i < argc; ++i) {
121 beagle_query_add_text (query, argv[i]);
124 g_signal_connect (query, "hits-added",
125 G_CALLBACK (hits_added_cb),
126 client);
128 g_signal_connect (query, "finished",
129 G_CALLBACK (finished_cb),
130 main_loop);
132 beagle_client_send_request_async (client, BEAGLE_REQUEST (query),
133 NULL);
135 g_main_loop_run (main_loop);
137 g_object_unref (info_req);
138 g_object_unref (query);
139 g_object_unref (client);
140 g_main_loop_unref (main_loop);
142 g_print ("Found a total of %d hits\n", total_hits);
144 return 0;