1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
7 * Copyright (C) 2004 Novell, Inc.
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
31 #include "beaglequery.h"
33 #define _XOPEN_SOURCE /* glibc2 needs this */
38 beagle_hit_free (BeagleHit
*hit
)
43 g_free (hit
->mime_type
);
49 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
51 static BeagleQueryResult
*
52 beagle_query_result_new ()
54 BeagleQueryResult
*bqr
;
56 bqr
= g_new0 (BeagleQueryResult
, 1);
59 bqr
->by_uri
= g_hash_table_new (g_str_hash
, g_str_equal
);
65 beagle_query_result_free (BeagleQueryResult
*bqr
)
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
);
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
);
87 bqr
->all
= g_slist_prepend (bqr
->all
, hit
);
88 g_hash_table_insert (bqr
->by_uri
, beagle_hit_get_uri (hit
), hit
);
92 beagle_query_result_get_by_uri (BeagleQueryResult
*bqr
, const char *uri
)
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
);
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
;
113 g_return_val_if_fail (query_string
!= NULL
, NULL
);
115 /* An ugly hack: we run beagle-query and parse the results into BeagleHit
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
))
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');
135 while (*query_ptr
&& isspace (*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)) {
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 */
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
));
173 g_free (query_output
);