Index table:name attribute of table:table of ods files. These store the sheet names.
[beagle.git] / chooser-fu / beaglequery.c
blobba4584590ed7cc684f361822703f6c1308c03a21
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 /*
5 * beaglequery.c
7 * Copyright (C) 2004 Novell, Inc.
9 */
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * USA.
28 #ifdef CONFIG_H
29 #include <config.h>
30 #endif
31 #include "beaglequery.h"
33 #define _XOPEN_SOURCE /* glibc2 needs this */
34 #include <time.h>
35 #include <string.h>
37 void
38 beagle_hit_free (BeagleHit *hit)
40 if (hit != NULL) {
41 g_free (hit->uri);
42 g_free (hit->type);
43 g_free (hit->mime_type);
44 g_free (hit->source);
45 g_free (hit);
49 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
51 static BeagleQueryResult *
52 beagle_query_result_new ()
54 BeagleQueryResult *bqr;
56 bqr = g_new0 (BeagleQueryResult, 1);
57 bqr->count = 0;
58 bqr->all = NULL;
59 bqr->by_uri = g_hash_table_new (g_str_hash, g_str_equal);
61 return bqr;
64 void
65 beagle_query_result_free (BeagleQueryResult *bqr)
67 if (bqr != NULL) {
68 g_hash_table_destroy (bqr->by_uri);
69 g_slist_foreach (bqr->all, (GFunc) beagle_hit_free, NULL);
70 g_slist_free (bqr->all);
74 static void
75 beagle_query_result_add (BeagleQueryResult *bqr, BeagleHit *hit)
77 g_return_if_fail (bqr != NULL);
78 g_return_if_fail (hit != NULL);
80 /* Throw out hits w/o URIs */
81 if (hit->uri == NULL) {
82 beagle_hit_free (hit);
83 return;
86 ++bqr->count;
87 bqr->all = g_slist_prepend (bqr->all, hit);
88 g_hash_table_insert (bqr->by_uri, beagle_hit_get_uri (hit), hit);
91 BeagleHit *
92 beagle_query_result_get_by_uri (BeagleQueryResult *bqr, const char *uri)
94 BeagleHit *hit;
96 g_return_val_if_fail (bqr != NULL, NULL);
97 g_return_val_if_fail (uri != NULL, NULL);
99 hit = g_hash_table_lookup (bqr->by_uri, uri);
101 return hit;
104 BeagleQueryResult *
105 beagle_query (const char *query_string)
107 BeagleQueryResult *bqr = NULL;
108 BeagleHit *current_hit = NULL;
109 char *cmdline = NULL;
110 char *query_output = NULL;
111 char *query_ptr;
113 g_return_val_if_fail (query_string != NULL, NULL);
115 /* An ugly hack: we run beagle-query and parse the results into BeagleHit
116 objects. */
118 cmdline = g_strdup_printf ("/opt/beagle/bin/beagle-query --verbose %s", query_string);
120 /* FIXME: We should fail gracefully and w/ a meaningful error message if
121 we can't find beagle-query, etc. */
122 if (! g_spawn_command_line_sync (cmdline, &query_output, NULL, NULL, NULL))
123 goto finished;
125 bqr = beagle_query_result_new ();
127 query_ptr = query_output;
128 while (query_ptr != NULL && *query_ptr) {
129 char *next = strchr (query_ptr, '\n');
130 if (next != NULL) {
131 *next = '\0';
132 ++next;
135 while (*query_ptr && isspace (*query_ptr))
136 ++query_ptr;
138 if (! strncmp (query_ptr, "Uri: ", 5)) {
139 if (current_hit != NULL)
140 beagle_query_result_add (bqr, current_hit);
141 current_hit = g_new0 (BeagleHit, 1);
142 current_hit->uri = g_strdup (query_ptr + 5);
143 } else if (! strncmp (query_ptr, "Type: ", 6)) {
144 current_hit->type = g_strdup (query_ptr + 6);
145 } else if (! strncmp (query_ptr, "MimeT: ", 7)) {
146 current_hit->mime_type = g_strdup (query_ptr + 7);
147 } else if (! strncmp (query_ptr, "Src: ", 5)) {
148 current_hit->source = g_strdup (query_ptr + 5);
149 } else if (! strncmp (query_ptr, "Score: ", 7)) {
150 current_hit->score = atof (query_ptr + 7);
151 } else if (! strncmp (query_ptr, "Time: ", 6)) {
152 struct tm tm;
154 /* strptime() does not init fields it does not touch ... */
155 memset (&tm, '0', sizeof (struct tm));
156 if (strptime (query_ptr + 6, "%m/%d/%Y %I:%M:%S %p", &tm))
157 current_hit->timestamp = mktime (&tm);
160 /* FIXME: We should also read in the properties */
162 query_ptr = next;
165 if (current_hit != NULL)
166 beagle_query_result_add (bqr, current_hit);
168 g_print ("Query '%s' yieled %d hits\n", query_string, beagle_query_result_get_count (bqr));
171 finished:
172 g_free (cmdline);
173 g_free (query_output);
175 return bqr;