FilterHtml.cs: Add ExtractText(string) command to extract text out of large html...
[beagle.git] / libbeagle / examples / beagle-search.c
blobd50858455b3a1395a2f5277c139582c03e2ae79a
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;
50 hits = beagle_hits_added_response_get_hits (response);
52 nr_hits = g_slist_length (hits);
53 total_hits += nr_hits;
54 g_print ("Found hits (%d):\n", nr_hits);
55 g_print ("-------------------------------------------\n");
56 for (l = hits, i = 1; l; l = l->next, ++i) {
57 g_print ("[%d] ", i);
59 print_hit (BEAGLE_HIT (l->data));
61 g_print ("\n");
63 g_print ("-------------------------------------------\n\n\n");
66 static void
67 finished_cb (BeagleQuery *query,
68 BeagleFinishedResponse *response,
69 GMainLoop *main_loop)
71 g_main_loop_quit (main_loop);
74 int
75 main (int argc, char **argv)
77 BeagleClient *client;
78 BeagleQuery *query;
79 GMainLoop *main_loop;
80 gint i;
82 if (argc < 2) {
83 g_print ("Usage %s \"query string\"\n", argv[0]);
84 exit (1);
87 g_type_init ();
89 total_hits = 0;
91 client = beagle_client_new (NULL);
93 main_loop = g_main_loop_new (NULL, FALSE);
95 query = beagle_query_new ();
97 for (i = 1; i < argc; ++i) {
98 beagle_query_add_text (query, argv[i]);
101 g_signal_connect (query, "hits-added",
102 G_CALLBACK (hits_added_cb),
103 client);
105 g_signal_connect (query, "finished",
106 G_CALLBACK (finished_cb),
107 main_loop);
109 beagle_client_send_request_async (client, BEAGLE_REQUEST (query),
110 NULL);
112 g_main_loop_run (main_loop);
113 g_object_unref (query);
114 g_object_unref (client);
115 g_main_loop_unref (main_loop);
117 g_print ("Found a total of %d hits\n", total_hits);
119 return 0;