summaryrefslogtreecommitdiffstats
path: root/kicker/kicker/plugins/beaglesearch.cpp
blob: f75f719ab384850091339eac8ac4b86aa164cc4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*****************************************************************

   Copyright (c) 2006 Debajyoti Bera <dbera.web@gmail.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

******************************************************************/

#include "beaglesearch.h"

#include <tqdatetime.h>
#include <tqmutex.h>
#include <tqstringlist.h>
#include <tqapplication.h>
#include <time.h>

void beagle_init ()
{
    g_type_init ();
}

// ---------------- Hit ---------------------------

Hit::Hit (BeagleHit *_hit) : processed (false)
{
    hit = beagle_hit_ref (_hit);
}

Hit::~Hit ()
{
    beagle_hit_unref (hit);
    if (! processed)
	return;
    TQDictIterator<TQStringList> it (property_map);
    for( ; it.current(); ++it )
        ((TQStringList *)it.current())->clear ();

}

void Hit::processProperties ()
{
    processed = true;
    GSList *prop_list = beagle_hit_get_all_properties (hit);
    GSList *it;
    property_map.setAutoDelete (true);
    for (it = prop_list; it; it = it->next) {
        BeagleProperty *property = (BeagleProperty *) it->data;
        TQString key = TQString::fromUtf8 (beagle_property_get_key (property));
        if (! property_map [key])
            property_map.insert (key, new TQStringList ());
        property_map [key]->append (TQString::fromUtf8 (beagle_property_get_value (property)));
    }
    g_slist_free (prop_list);
}

const TQString Hit::operator[] (TQString prop_name)
{
    if (! processed)
	processProperties ();

    TQStringList *prop_list = property_map [prop_name];
    if (! prop_list)
        return TQString::null;
    if (prop_list->count () != 1)
        return TQString::null;
    return (TQString)prop_list->first ();
}

// ---------------- BeagleSearch ------------------

BeagleSearchResult::BeagleSearchResult(int client_id)
  : client_id (client_id), total (0)
{
    hitlist = new TQPtrList<Hit>;
    hitlist->setAutoDelete (true);
}


BeagleSearchResult::~BeagleSearchResult()
{
    // everything is set to autodelete
}

void BeagleSearchResult::addHit(BeagleHit *_hit)
{
    Hit *hit = new Hit (_hit);
    hitlist->prepend (hit);
}

const TQPtrList<Hit> *BeagleSearchResult::getHits () const
{
    return hitlist;
}


static int total_hits;

static void print_feed_item_hit (BeagleHit *hit)
{
    const char *text;

    if (beagle_hit_get_one_property (hit, "dc:title", &text))
        g_print ("Blog: %s\n", text);
}

static void print_file_hit (BeagleHit *hit)
{
    g_print ("File: %s, (%s)\n", beagle_hit_get_uri (hit), beagle_hit_get_mime_type (hit));
}

static void print_other_hit (BeagleHit *hit)
{
    const char *text;

    g_print ("%s (%s)", beagle_hit_get_uri (hit),
             beagle_hit_get_source (hit));
    if (beagle_hit_get_one_property (hit, "dc:title", &text))
        g_print ("title = %s\n", text);
}

static void print_hit (BeagleHit *hit) 
{
    if (strcmp (beagle_hit_get_type (hit), "FeedItem") == 0) {
        print_feed_item_hit (hit);
    } 
    else if (strcmp (beagle_hit_get_type (hit), "File") == 0) {
        print_file_hit (hit);
    } else {
        print_other_hit (hit);
    }
}

// ---------------- BeagleSearchClient ------------------

void BeagleSearchClient::run ()
{
    kdDebug () << "Starting query ..." << endl;

    TQTime query_timer;
    query_timer.start ();
    
    g_signal_connect (query, "hits-added",
                      G_CALLBACK (hitsAddedSlot),
                      this);
    g_signal_connect (query, "finished",
                      G_CALLBACK (finishedSlot),
                      this);
    beagle_client_send_request_async (client,
                                      BEAGLE_REQUEST (query),
                                      NULL);
    g_main_loop_run (main_loop);
    kdDebug () << "Finished query ..." << endl;
    
    TQCustomEvent *ev;
    if (collate_results) {
	result->query_msec = query_timer.elapsed ();
    
	ev =  new TQCustomEvent (RESULTFOUND, result);
        TQApplication::postEvent (object, ev);
    }

    ev =  new TQCustomEvent (KILLME, this);
    TQApplication::postEvent (object, ev);

}

void BeagleSearchClient::stopClient ()
{
   if (finished ())
       return; // duh!
   kdDebug () << "Query thread " << id << " not yet finished ..." << endl;
   // get ready for suicide
   client_mutex->lock ();
   kill_me = true;
   g_signal_handlers_disconnect_by_func (
           query, 
           (void *)hitsAddedSlot,
           this);
   g_signal_handlers_disconnect_by_func (
           query,
           (void *)finishedSlot,
           this);
   g_main_loop_quit (main_loop);
   client_mutex->unlock ();
}

void BeagleSearchClient::hitsAddedSlot (BeagleQuery *query,
                                        BeagleHitsAddedResponse *response,
                                        BeagleSearchClient *bsclient)
{
    GSList *hits, *l;
    gint    i;
    gint    nr_hits;

    // check if we are supposed to be killed
    bsclient->client_mutex->lock ();
    if (bsclient->kill_me) {
        kdDebug () << "Suicide time before processing" << endl;
	bsclient->client_mutex->unlock ();
	return;
    }
    bsclient->client_mutex->unlock ();

    hits = beagle_hits_added_response_get_hits (response);

    nr_hits = g_slist_length (hits);
    total_hits += nr_hits;
    g_print ("Found hits (%d) at %ld:\n", nr_hits, time (NULL));

    BeagleSearchResult *search_result;
    if (! bsclient->collate_results)
        search_result = new BeagleSearchResult (bsclient->id);
    else
        search_result = bsclient->result;
    search_result->total += nr_hits;

    for (l = hits, i = 1; l; l = l->next, ++i) {
        //g_print ("[%d] ", i);
        //print_hit (BEAGLE_HIT (l->data));
        //g_print ("\n");

        search_result->addHit(BEAGLE_HIT (l->data));//hit);
    }
    g_print ("[%ld] hits adding finished \n", time (NULL));

    // check if we are supposed to be killed
    bsclient->client_mutex->lock ();
    if (bsclient->kill_me) {
        kdDebug () << "Suicide time before sending ..." << endl;
	bsclient->client_mutex->unlock ();
	if (! bsclient->collate_results)
	    delete search_result;
	return;
    }
    bsclient->client_mutex->unlock ();

    // time to send back results, if user asked so
    if (bsclient->collate_results)
        return;
    TQCustomEvent *ev =  new TQCustomEvent (RESULTFOUND, search_result);
    g_print ("[%ld] event notified \n", time (NULL));
    TQApplication::postEvent (bsclient->object, ev);
}

void BeagleSearchClient::finishedSlot (BeagleQuery *query,
                                       BeagleFinishedResponse *response,
                                       BeagleSearchClient *bsclient)
{
    // check if we are supposed to be killed
    bsclient->client_mutex->lock ();
    bool should_kill = bsclient->kill_me;
    TQObject* receiver = bsclient->object;
    bsclient->client_mutex->unlock ();

    if (should_kill)
	return;

    g_main_loop_quit (bsclient->main_loop);

    if (bsclient->collate_results)
        return; // if we are collating, everything will be send from a central place
    if (receiver) {
        TQCustomEvent *ev =  new TQCustomEvent (SEARCHOVER, bsclient);
        g_print ("[%ld] query finish notified \n", time (NULL));
        TQApplication::postEvent (receiver, ev);
    }
}

// ----------------- BeagleUtil -------------------

BeagleQuery *
BeagleUtil::createQueryFromString (TQString query_str,
                                    TQStringList &sources_menu,
                                    TQStringList &types_menu,
				    int max_hits_per_source)
{
    BeagleQuery *beagle_query = beagle_query_new ();
    beagle_query_set_max_hits (beagle_query, max_hits_per_source); // this is per source!

    kdDebug () << "Creating query from \"" << query_str << "\"" << endl;
    for ( TQStringList::Iterator it = sources_menu.begin(); it != sources_menu.end(); ++it )
        beagle_query_add_source (beagle_query, g_strdup ((*it).utf8 ()));

    for ( TQStringList::Iterator it = types_menu.begin(); it != types_menu.end(); ++it )
        beagle_query_add_hit_type (beagle_query, g_strdup ((*it).utf8 ()));

    TQStringList query_terms;
    TQString start_date, end_date;
    TQStringList words = TQStringList::split (' ', query_str, false);
    for ( TQStringList::Iterator it = words.begin(); it != words.end(); ++it ) {
        TQStringList key_value_pair = TQStringList::split ('=', *it, false);
        if (key_value_pair.count () == 1)
            query_terms += *it;
        else if (key_value_pair.count () == 2) {
            TQString key = key_value_pair [0].lower ();
            TQString value = key_value_pair [1];
            if (key == "mime")
                beagle_query_add_mime_type (beagle_query, g_strdup (value.utf8 ()));
            else if (key == "type")
                beagle_query_add_hit_type (beagle_query, g_strdup (value.utf8 ()));
            else if (key == "source")
                beagle_query_add_source (beagle_query, g_strdup (value.utf8 ()));
            else if (key == "start")
                start_date = value;
            else if (key == "end")
                end_date = value;
            else
                query_terms += *it;
        } else
            query_terms += *it;
    }

    beagle_query_add_text (beagle_query, g_strdup (query_terms.join (" ").utf8 ()));
    kdDebug () << "Adding query text:" << query_terms.join (" ").utf8 () << endl;

    if (start_date.isNull () && end_date.isNull ())
        return beagle_query;

    //kdDebug () << "Handling dates ..." << endl;
    BeagleQueryPartDate * date_part = beagle_query_part_date_new ();
    if (! start_date.isNull ())
        beagle_query_part_date_set_start_date (date_part, timestringToBeagleTimestamp (start_date));
    if (! end_date.isNull ())
        beagle_query_part_date_set_end_date (date_part, timestringToBeagleTimestamp (end_date));
    beagle_query_add_part (beagle_query, BEAGLE_QUERY_PART (date_part));

    return beagle_query;
}

// timestring format allowed YYYYmmDD
BeagleTimestamp *
BeagleUtil::timestringToBeagleTimestamp(TQString timestring)
{
    //kdDebug () << "datetime string:" << timestring << endl;
    // FIXME: error check timestring format
    if (timestring.isNull () || timestring.stripWhiteSpace () == "" || timestring.length() != 8 )
        return beagle_timestamp_new_from_unix_time (TQDateTime::tqcurrentDateTime ().toTime_t ());
    //TQDateTime dt = TQDateTime::fromString (timestring, Qt::ISODate);
    struct tm tm_time;
    time_t timet_time;
    time (&timet_time);
    localtime_r (&timet_time, &tm_time);
    strptime (timestring.ascii(), "%Y%m%d", &tm_time);
    tm_time.tm_sec = tm_time.tm_min = tm_time.tm_hour = 0;
    //kdDebug() << asctime (&tm_time) << endl;
    timet_time = mktime (&tm_time);
    return beagle_timestamp_new_from_unix_time (timet_time);
}