~ubuntu-branches/ubuntu/karmic/gtk-gnutella/karmic

« back to all changes in this revision

Viewing changes to src/search_cb.c

  • Committer: Bazaar Package Importer
  • Author(s): Anand Kumria
  • Date: 2005-08-04 11:32:05 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050804113205-q746i4lgo3rtlegn
Tags: 0.95.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: search_cb.c,v 1.39 2004/01/15 23:51:48 wyldfire Exp $
3
 
 *
4
 
 * Copyright (c) 2001-2003, Raphael Manfredi, Richard Eckart
5
 
 *
6
 
 * GUI filtering functions.
7
 
 *
8
 
 *----------------------------------------------------------------------
9
 
 * This file is part of gtk-gnutella.
10
 
 *
11
 
 *  gtk-gnutella is free software; you can redistribute it and/or modify
12
 
 *  it under the terms of the GNU General Public License as published by
13
 
 *  the Free Software Foundation; either version 2 of the License, or
14
 
 *  (at your option) any later version.
15
 
 *
16
 
 *  gtk-gnutella is distributed in the hope that it will be useful,
17
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
 *  GNU General Public License for more details.
20
 
 *
21
 
 *  You should have received a copy of the GNU General Public License
22
 
 *  along with gtk-gnutella; if not, write to the Free Software
23
 
 *  Foundation, Inc.:
24
 
 *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 
 *----------------------------------------------------------------------
26
 
 */
27
 
 
28
 
#include "gui.h"
29
 
 
30
 
#ifdef USE_GTK1
31
 
 
32
 
#include <gdk/gdkkeysyms.h>
33
 
 
34
 
#include "gtkcolumnchooser.h"
35
 
#include "search_cb.h"
36
 
#include "search_gui.h"
37
 
#include "statusbar_gui.h"
38
 
#include "override.h"           /* Must be the last header included */
39
 
 
40
 
RCSID("$Id: search_cb.c,v 1.39 2004/01/15 23:51:48 wyldfire Exp $");
41
 
 
42
 
extern search_t *search_selected;
43
 
 
44
 
static gboolean in_autoselect = FALSE;
45
 
static gchar tmpstr[4096];
46
 
 
47
 
/***
48
 
 *** Private functions
49
 
 ***/
50
 
 
51
 
 
52
 
/* 
53
 
 *      refresh_popup
54
 
 *
55
 
 *      Activates/deactivates buttons and popups based on what is selected
56
 
 *
57
 
 */
58
 
static void refresh_popup(void)
59
 
{
60
 
        gboolean sensitive;
61
 
    search_t *search;
62
 
 
63
 
    search = search_gui_get_current_search();
64
 
 
65
 
        sensitive = search && 
66
 
        (NULL != (GTK_CLIST(search->ctree))->selection);
67
 
        gtk_widget_set_sensitive
68
 
        (lookup_widget(main_window, "button_search_download"), sensitive);
69
 
    gtk_widget_set_sensitive
70
 
        (lookup_widget(popup_search, "popup_search_drop_name"), sensitive);
71
 
    gtk_widget_set_sensitive
72
 
        (lookup_widget(popup_search, "popup_search_drop_sha1"), sensitive);
73
 
    gtk_widget_set_sensitive
74
 
        (lookup_widget(popup_search, "popup_search_drop_host"), sensitive);
75
 
    gtk_widget_set_sensitive(
76
 
        lookup_widget(popup_search, "popup_search_drop_name_global"), 
77
 
        sensitive);
78
 
    gtk_widget_set_sensitive(
79
 
        lookup_widget(popup_search, "popup_search_drop_sha1_global"), 
80
 
        sensitive);   
81
 
    gtk_widget_set_sensitive(
82
 
        lookup_widget(popup_search, "popup_search_drop_host_global"), 
83
 
        sensitive);   
84
 
    gtk_widget_set_sensitive(
85
 
        lookup_widget(popup_search, "popup_search_autodownload_name"), 
86
 
        sensitive);
87
 
    gtk_widget_set_sensitive(
88
 
        lookup_widget(popup_search, "popup_search_autodownload_sha1"), 
89
 
        sensitive);   
90
 
    gtk_widget_set_sensitive(
91
 
        lookup_widget(popup_search, "popup_search_new_from_selected"), 
92
 
        sensitive);   
93
 
 
94
 
    gtk_widget_set_sensitive(
95
 
        lookup_widget(popup_search, "popup_search_restart"), NULL != search);
96
 
    gtk_widget_set_sensitive(
97
 
        lookup_widget(popup_search, "popup_search_duplicate"), NULL != search);
98
 
 
99
 
    if (search) {
100
 
        gtk_widget_set_sensitive(
101
 
            lookup_widget(popup_search, "popup_search_stop"), 
102
 
                        !search_is_frozen(search->search_handle));
103
 
                gtk_widget_set_sensitive(
104
 
            lookup_widget(popup_search, "popup_search_resume"),
105
 
                        search_is_frozen(search->search_handle));
106
 
                if (search->passive)
107
 
            gtk_widget_set_sensitive(
108
 
                lookup_widget(popup_search, "popup_search_restart"), 
109
 
                FALSE);
110
 
    } else {
111
 
        gtk_widget_set_sensitive(
112
 
            lookup_widget(popup_search, "popup_search_stop"), FALSE);
113
 
            gtk_widget_set_sensitive(
114
 
            lookup_widget(popup_search, "popup_search_resume"), FALSE);
115
 
    }
116
 
}
117
 
 
118
 
 
119
 
/* 
120
 
 *      search_cb_autoselect
121
 
 *
122
 
 *      Autoselects all searches matching given node in given tree
123
 
 *      Uses the in_autosearch flag to prevent recursive autoselecting
124
 
 */
125
 
gint search_cb_autoselect(GtkCTree *ctree, GtkCTreeNode *node, 
126
 
        gboolean search_autoselect_ident)
127
 
{
128
 
        GtkCTreeNode *auto_node;
129
 
        GtkCTreeNode *parent, *child;
130
 
        GtkCTreeRow *row;
131
 
        record_t *rc, *rc2;
132
 
    guint32 fuzzy_threshold;
133
 
        gboolean child_selected, node_expanded;
134
 
        gint x = 0;
135
 
        
136
 
        if (in_autoselect)
137
 
                return 0;       /* Prevent recursive autoselect */
138
 
        
139
 
        in_autoselect = TRUE;
140
 
        gnet_prop_get_guint32(PROP_FUZZY_THRESHOLD, &fuzzy_threshold, 0, 1);
141
 
 
142
 
        /* 
143
 
     * Rows with NULL data can appear when inserting new rows
144
 
     * because the selection is resynced and the row data cannot
145
 
     * be set until insertion (and therefore also selection syncing
146
 
     * is done.
147
 
     *      --BLUE, 20/06/2002
148
 
     */
149
 
        rc = (record_t *) gtk_ctree_node_get_row_data(ctree, node);
150
 
        gtk_clist_freeze(GTK_CLIST(ctree));
151
 
        
152
 
    /* Search whole ctree for nodes to autoselect 
153
 
     */                 
154
 
        for (x = 1, auto_node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
155
 
                (NULL != auto_node) && (NULL != rc);
156
 
                auto_node = GTK_CTREE_NODE_NEXT (auto_node)) {                  
157
 
       
158
 
                if (NULL == auto_node)
159
 
                        continue;
160
 
                        
161
 
                rc2 = (record_t *) gtk_ctree_node_get_row_data(ctree, auto_node);
162
 
 
163
 
        /*
164
 
         * Skip the line we selected in the first place.
165
 
         */
166
 
        if (rc == rc2)
167
 
                continue;
168
 
 
169
 
        if (rc2 == NULL) {
170
 
                g_warning(" on_ctree_search_results_select_row: "
171
 
                                "detected row with NULL data, skipping.");
172
 
            continue;
173
 
                }
174
 
 
175
 
                parent = GTK_CTREE_NODE(auto_node);
176
 
                row = GTK_CTREE_ROW(parent);
177
 
                
178
 
                /* If auto_node is a child node, we skip it cause it will be handled 
179
 
                 * when we come to it's parent 
180
 
                 */
181
 
                if(NULL != row->parent)
182
 
                        continue;                       
183
 
                
184
 
                child = row->children;
185
 
                if (NULL != child) /* If the node has children */
186
 
                {
187
 
                        /* A header node.  We expand it and check all of the children 
188
 
                         * If one of the children get selected keep node expanded,
189
 
                         * if it was initially collapsed, collapse it again
190
 
                         */                             
191
 
                        child_selected = FALSE;
192
 
                        node_expanded = FALSE;
193
 
 
194
 
                        node_expanded = gtk_ctree_is_viewable(ctree, child);
195
 
                        gtk_ctree_expand(ctree, parent);
196
 
                        
197
 
                        for (; NULL != child; row = GTK_CTREE_ROW(child), 
198
 
                                child = row->sibling) {         
199
 
 
200
 
                                rc2 = gtk_ctree_node_get_row_data (ctree, child);
201
 
                    
202
 
                                if (rc == rc2)
203
 
                                continue;
204
 
        
205
 
                    if (search_autoselect_ident) {
206
 
                        if ((rc->size == rc2->size && rc->sha1 != NULL && 
207
 
                                                rc2->sha1 != NULL &&
208
 
                        memcmp(rc->sha1, rc2->sha1, SHA1_RAW_SIZE) == 0) 
209
 
                                                || ((rc->sha1 == NULL) && (rc2->size == rc->size) && 
210
 
                                                ((!search_autoselect_fuzzy && 
211
 
                                                !strcmp(rc2->name, rc->name)) || 
212
 
                                                (search_autoselect_fuzzy &&
213
 
(fuzzy_compare(rc2->name, rc->name) * 100 >= (fuzzy_threshold << FUZZY_SHIFT)))
214
 
                        ))) {
215
 
                                                        gtk_ctree_select(ctree, child);
216
 
                                                        child_selected = TRUE;                        
217
 
                                                        x++;
218
 
                    }
219
 
                } else {
220
 
                        if (((rc->sha1 != NULL && rc2->sha1 != NULL &&
221
 
                            memcmp(rc->sha1, rc2->sha1, SHA1_RAW_SIZE) == 0) || 
222
 
                        (rc2 && !strcmp(rc2->name, rc->name))) &&
223
 
                        (rc2->size >= rc->size)) {
224
 
                                                        
225
 
                                                gtk_ctree_select(ctree, child);
226
 
                                                child_selected = TRUE;                        
227
 
                            x++;
228
 
                        }
229
 
                                }
230
 
                        }                                       
231
 
                        if ((!child_selected) && (!node_expanded))
232
 
                                gtk_ctree_collapse(ctree, parent);
233
 
                }
234
 
 
235
 
                /* Reget rc2 in case we overwrote it while parsing the children */
236
 
                rc2 = (record_t *) gtk_ctree_node_get_row_data(ctree, auto_node);
237
 
 
238
 
        if (search_autoselect_ident) {
239
 
                        if ((
240
 
                /*
241
 
                                 * size check added to workaround buggy
242
 
                 * servents. -vidar, 2002-08-08
243
 
                                 */
244
 
                rc->size == rc2->size && 
245
 
                rc->sha1 != NULL && rc2->sha1 != NULL &&
246
 
                memcmp(rc->sha1, rc2->sha1, SHA1_RAW_SIZE) == 0
247
 
                        ) || (
248
 
                (rc->sha1 == NULL) && 
249
 
                (rc2->size == rc->size) && (
250
 
                (!search_autoselect_fuzzy &&
251
 
                                !strcmp(rc2->name, rc->name)) ||
252
 
                (search_autoselect_fuzzy &&
253
 
(fuzzy_compare(rc2->name, rc->name) * 100 >= (fuzzy_threshold << FUZZY_SHIFT)))
254
 
                )
255
 
                )) {
256
 
                
257
 
                                gtk_ctree_select(ctree, auto_node);
258
 
                x++;
259
 
                        }
260
 
                } else {
261
 
            
262
 
                        if (((rc->sha1 != NULL && rc2->sha1 != NULL &&
263
 
                memcmp(rc->sha1, rc2->sha1, SHA1_RAW_SIZE) == 0) || 
264
 
                (rc2 && !strcmp(rc2->name, rc->name))) &&
265
 
                (rc2->size >= rc->size)) {
266
 
                                
267
 
                                gtk_ctree_select(ctree, auto_node);
268
 
                x++;
269
 
            }
270
 
                }       
271
 
        } /* for */
272
 
        
273
 
        gtk_clist_thaw(GTK_CLIST(ctree));
274
 
        
275
 
        gtk_widget_queue_draw((GtkWidget *) ctree); /* Force redraw */
276
 
        in_autoselect = FALSE;
277
 
        return x;
278
 
}
279
 
 
280
 
 
281
 
 
282
 
 
283
 
/***
284
 
 *** Glade callbacks
285
 
 ***/
286
 
void on_combo_entry_searches_activate
287
 
    (GtkEditable *editable, gpointer user_data)
288
 
{
289
 
    /* FIXME */
290
 
}
291
 
 
292
 
 
293
 
/* 
294
 
 *      on_search_popdown_switch
295
 
 */
296
 
void on_search_popdown_switch(GtkWidget *w, gpointer data)
297
 
{
298
 
        if (search_selected != NULL)
299
 
        search_gui_set_current_search(search_selected);
300
 
}
301
 
 
302
 
 
303
 
/* 
304
 
 *      on_search_notebook_switch
305
 
 *
306
 
 *      When the user switches notebook tabs, update the rest of GUI
307
 
 *
308
 
 *      This may be obsolete as we removed the tabbed interface --Emile 27/12/03
309
 
 */
310
 
void on_search_notebook_switch(GtkNotebook * notebook, GtkNotebookPage * page, 
311
 
        gint page_num, gpointer user_data)
312
 
{
313
 
        search_t *sch;
314
 
 
315
 
        sch = (search_t *) gtk_object_get_user_data(
316
 
                GTK_OBJECT(gtk_notebook_get_nth_page(notebook, page_num)));
317
 
 
318
 
        g_return_if_fail(sch);
319
 
 
320
 
    search_gui_set_current_search(sch);
321
 
}
322
 
 
323
 
 
324
 
/* 
325
 
 *      on_clist_search_select_row
326
 
 *
327
 
 *      Changes current search and updates GUI
328
 
 */
329
 
void on_clist_search_select_row(GtkCList * clist, gint row, gint column, 
330
 
        GdkEvent * event, gpointer user_data)
331
 
{
332
 
    gpointer sch;
333
 
 
334
 
    g_assert(clist != NULL);
335
 
 
336
 
    sch = gtk_clist_get_row_data(clist, row);
337
 
 
338
 
    if (sch == NULL)
339
 
        return;
340
 
 
341
 
    search_gui_set_current_search((search_t *)sch);
342
 
}
343
 
 
344
 
 
345
 
/* 
346
 
 *      on_search_selected
347
 
 */
348
 
void on_search_selected(GtkItem * i, gpointer data)
349
 
{
350
 
        search_selected = (search_t *) data;
351
 
}
352
 
 
353
 
 
354
 
/* 
355
 
 *      on_button_search_clicked
356
 
 *
357
 
 *      Create a search based on query entered
358
 
 *
359
 
 */
360
 
void on_button_search_clicked(GtkButton *button, gpointer user_data)
361
 
{
362
 
        gchar *e = STRTRACK(gtk_editable_get_chars
363
 
        (GTK_EDITABLE(lookup_widget(main_window, "entry_search")), 0, -1));
364
 
 
365
 
        /*
366
 
         * Even though we might not be on_the_net() yet, record the search.
367
 
         * There is a callback mechanism when a new node is connected, which
368
 
         * will launch the search there if it has not been sent already.
369
 
         *              --patch from Mark Schreiber, 10/01/2002
370
 
         */
371
 
 
372
 
    g_strstrip(e);
373
 
    if (*e) {
374
 
        filter_t *default_filter;
375
 
        search_t *search;
376
 
        gboolean res;
377
 
 
378
 
        /*
379
 
         * It's important gui_search_history_add is called before
380
 
         * new_search, otherwise the search entry will not be
381
 
         * cleared.
382
 
         *      --BLUE, 04/05/2002
383
 
         */
384
 
        gui_search_history_add(e);
385
 
 
386
 
        /*
387
 
         * We have to capture the selection here already, because
388
 
         * new_search will trigger a rebuild of the menu as a
389
 
         * side effect.
390
 
         */
391
 
        default_filter = (filter_t *)option_menu_get_selected_data
392
 
            (lookup_widget(main_window, "optionmenu_search_filter"));
393
 
 
394
 
                res = search_gui_new_search(e, 0, &search);
395
 
 
396
 
        /*
397
 
         * If we should set a default filter, we do that.
398
 
         */
399
 
        if (res && (default_filter != NULL)) {
400
 
            rule_t *rule = filter_new_jump_rule
401
 
                (default_filter, RULE_FLAG_ACTIVE);
402
 
            
403
 
            /*
404
 
             * Since we don't want to distrub the shadows and
405
 
             * do a "force commit" without the user having pressed
406
 
             * the "ok" button in the dialog, we add the rule
407
 
             * manually.
408
 
             */
409
 
            search->filter->ruleset = 
410
 
                g_list_append(search->filter->ruleset, rule);
411
 
            rule->target->refcount ++;
412
 
        }
413
 
        
414
 
        if (!res)
415
 
                gdk_beep();
416
 
    }
417
 
 
418
 
        G_FREE_NULL(e);
419
 
}
420
 
 
421
 
 
422
 
/* 
423
 
 *      on_entry_search_activate
424
 
 */
425
 
void on_entry_search_activate(GtkEditable * editable, gpointer user_data)
426
 
{
427
 
    /*
428
 
     * Delegate to: on_button_search_clicked.
429
 
     *      --BLUE, 30/04/2002
430
 
     */
431
 
 
432
 
        on_button_search_clicked(NULL, user_data);
433
 
}
434
 
 
435
 
 
436
 
/* 
437
 
 *      on_entry_search_changed
438
 
 *
439
 
 *      When a search string is entered, activate the search button
440
 
 *
441
 
 */
442
 
void on_entry_search_changed(GtkEditable * editable, gpointer user_data)
443
 
{
444
 
        gchar *e = STRTRACK(gtk_editable_get_chars(editable, 0, -1));
445
 
        g_strstrip(e);
446
 
        gtk_widget_set_sensitive
447
 
        (lookup_widget(main_window, "button_search"), *e != 0);
448
 
        G_FREE_NULL(e);
449
 
}
450
 
 
451
 
 
452
 
/* 
453
 
 *      on_button_search_clear_clicked
454
 
 *
455
 
 *      Clear search results, de-activate clear search button
456
 
 *
457
 
 */
458
 
void on_button_search_clear_clicked(GtkButton * button, gpointer user_data)
459
 
{
460
 
        gui_search_clear_results();
461
 
 
462
 
        gtk_widget_set_sensitive
463
 
        (lookup_widget(main_window, "button_search_clear"), FALSE);
464
 
}
465
 
 
466
 
 
467
 
/* 
468
 
 *      on_button_search_close_clicked
469
 
 */
470
 
void on_button_search_close_clicked(GtkButton * button, gpointer user_data)
471
 
{
472
 
    search_t *search;
473
 
 
474
 
    search = search_gui_get_current_search();
475
 
 
476
 
    if (search != NULL)
477
 
        search_gui_close_search(search);
478
 
}
479
 
 
480
 
 
481
 
/* 
482
 
 *      on_button_search_download_clicked
483
 
 */
484
 
void on_button_search_download_clicked(GtkButton * button, gpointer user_data)
485
 
{
486
 
    search_gui_download_files();
487
 
}
488
 
 
489
 
 
490
 
/* 
491
 
 *      on_button_search_collapse_clicked
492
 
 */
493
 
void on_button_search_collapse_all_clicked(GtkButton *button, gpointer user_data)
494
 
{
495
 
    search_gui_collapse_all();
496
 
}
497
 
 
498
 
 
499
 
/* 
500
 
 *      on_button_search_expand_clicked
501
 
 */
502
 
void on_button_search_expand_all_clicked(GtkButton *button, gpointer user_data)
503
 
{
504
 
    search_gui_expand_all();
505
 
}
506
 
 
507
 
 
508
 
/* 
509
 
 *      on_clist_search_results_key_press_event
510
 
 */
511
 
gboolean on_clist_search_results_key_press_event
512
 
    (GtkWidget *widget, GdkEventKey * event, gpointer user_data)
513
 
{
514
 
    g_assert(event != NULL);
515
 
 
516
 
    switch(event->keyval) {
517
 
    case GDK_Return:
518
 
        search_gui_download_files();
519
 
        return TRUE;
520
 
    default:
521
 
        return FALSE;
522
 
    };
523
 
}
524
 
 
525
 
 
526
 
/* 
527
 
 *      on_clist_search_results_button_press_event
528
 
 *
529
 
 *      Handles showing the popup in the event of right-clicks and downloading
530
 
 *      for double-clicks
531
 
 */
532
 
gboolean on_clist_search_results_button_press_event
533
 
    (GtkWidget *widget, GdkEventButton * event, gpointer user_data)
534
 
{
535
 
        gint row = 0;
536
 
        gint column = 0;
537
 
        static guint click_time = 0;
538
 
    search_t *search;
539
 
 
540
 
    search = search_gui_get_current_search();
541
 
 
542
 
        switch (event->button) {
543
 
        case 1:
544
 
        /* left click section */
545
 
                if (event->type == GDK_2BUTTON_PRESS) {
546
 
                        gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
547
 
                                "button_press_event");
548
 
                        return FALSE;
549
 
                }
550
 
                if (event->type == GDK_BUTTON_PRESS) {
551
 
                        if ((event->time - click_time) <= 250) {
552
 
                                /*
553
 
                                 * 2 clicks within 250 msec == doubleclick.
554
 
                                 * Surpress further events
555
 
                                 */
556
 
                                gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
557
 
                                        "button_press_event");
558
 
                                        
559
 
                                if (gtk_clist_get_selection_info(GTK_CLIST(widget), event->x,
560
 
                                        event->y, &row, &column)) {
561
 
 
562
 
                                        search_gui_download_files();
563
 
                    return TRUE;
564
 
                                }
565
 
                        } else {
566
 
                                click_time = event->time;
567
 
                                return FALSE;
568
 
                        }
569
 
                }
570
 
                return FALSE;
571
 
   
572
 
        case 3:
573
 
        /* right click section (popup menu) */
574
 
        refresh_popup();
575
 
        {
576
 
            gboolean search_results_show_tabs;
577
 
    
578
 
            gui_prop_get_boolean(
579
 
                PROP_SEARCH_RESULTS_SHOW_TABS,
580
 
                &search_results_show_tabs, 0, 1);
581
 
 
582
 
            gm_snprintf(tmpstr, sizeof(tmpstr), (search_results_show_tabs) ? 
583
 
                "Show search list" : "Show tabs");
584
 
        }
585
 
 
586
 
        gtk_label_set(GTK_LABEL((GTK_MENU_ITEM
587
 
            (lookup_widget(popup_search, "popup_search_toggle_tabs"))
588
 
                ->item.bin.child)), tmpstr);
589
 
                gtk_menu_popup(GTK_MENU(popup_search), NULL, NULL, NULL, NULL, 
590
 
                     event->button, event->time);
591
 
                return TRUE;
592
 
 
593
 
        default:
594
 
                break;
595
 
        }
596
 
 
597
 
        return FALSE;
598
 
}
599
 
 
600
 
 
601
 
/* 
602
 
 *      on_button_search_filter_clicked
603
 
 */
604
 
void on_button_search_filter_clicked(
605
 
    GtkButton *button, gpointer user_data)
606
 
{
607
 
        filter_open_dialog();
608
 
}
609
 
 
610
 
 
611
 
/* 
612
 
 *      on_clist_search_results_click_column
613
 
 *
614
 
 *      Sort search according to selected column 
615
 
 *
616
 
 */
617
 
void on_clist_search_results_click_column(
618
 
    GtkCList *clist, gint column, gpointer user_data)
619
 
{
620
 
    search_t *search;
621
 
 
622
 
    g_assert(clist != NULL);
623
 
 
624
 
    search = search_gui_get_current_search();
625
 
 
626
 
        if (search == NULL)
627
 
                return;
628
 
 
629
 
    /* rotate or initialize search order */
630
 
        if (column == search->sort_col) {
631
 
        switch (search->sort_order) {
632
 
        case SORT_ASC:
633
 
            search->sort_order = SORT_DESC;
634
 
                break;
635
 
        case SORT_DESC:
636
 
            search->sort_order = SORT_NONE;
637
 
            break;
638
 
        case SORT_NONE:
639
 
            search->sort_order = SORT_ASC;
640
 
        }
641
 
        } else {
642
 
                search->sort_col = column;
643
 
                search->sort_order = SORT_ASC;
644
 
        }
645
 
        
646
 
        search_gui_sort_column(search, column); /* Sort column, draw arrow */
647
 
}
648
 
 
649
 
 
650
 
/*
651
 
 *      on_clist_search_results_select_row:
652
 
 *
653
 
 *      This function is called when the user selects a row in the
654
 
 *      search results pane. Autoselection takes place here.
655
 
 */
656
 
void on_ctree_search_results_select_row(GtkCTree *ctree,
657
 
        GList *node, gint column, gpointer user_data)
658
 
{
659
 
    gboolean search_autoselect;
660
 
    gboolean search_autoselect_ident;
661
 
    gboolean search_autoselect_fuzzy;
662
 
    search_t *sch;
663
 
        record_t *rc;
664
 
        gint x;
665
 
 
666
 
        if (TRUE == in_autoselect)
667
 
                return;
668
 
        
669
 
        if (NULL == node)
670
 
                return;
671
 
        
672
 
    sch = search_gui_get_current_search();
673
 
        rc = (record_t *) gtk_ctree_node_get_row_data(ctree, GTK_CTREE_NODE(node));
674
 
        
675
 
    gui_prop_get_boolean(
676
 
        PROP_SEARCH_AUTOSELECT, 
677
 
        &search_autoselect, 0, 1);
678
 
 
679
 
    gui_prop_get_boolean(
680
 
        PROP_SEARCH_AUTOSELECT_IDENT, 
681
 
        &search_autoselect_ident, 0, 1);
682
 
 
683
 
    gui_prop_get_boolean(
684
 
        PROP_SEARCH_AUTOSELECT_FUZZY, 
685
 
        &search_autoselect_fuzzy, 0, 1);
686
 
 
687
 
 
688
 
    refresh_popup();
689
 
 
690
 
    /* 
691
 
     * check if config setting select all is on and only autoselect if
692
 
     * only one item is selected (no way to merge two autoselections)
693
 
     */
694
 
        if (search_autoselect && 
695
 
        (GTK_CLIST(ctree)->selection != NULL) &&
696
 
        (GTK_CLIST(ctree)->selection->next == NULL)) {
697
 
 
698
 
                x =     search_cb_autoselect(ctree, GTK_CTREE_NODE(node), 
699
 
                        search_autoselect_ident); 
700
 
   
701
 
        if (x > 1) {
702
 
            statusbar_gui_message(15, 
703
 
                "%d auto selected %s",
704
 
                x, (rc->sha1 != NULL) ? 
705
 
                    "by urn:sha1 and filename" : "by filename");
706
 
        } else if (x == 1) {
707
 
            statusbar_gui_message(15, "none auto selected");
708
 
        }
709
 
        } 
710
 
 
711
 
 
712
 
        /* The following code will select all the children of a parent, if that
713
 
         * parent is selected. This isn't necessary because when a node is closed
714
 
         * it's children are already selected.  This would only be useful if 
715
 
         * someone opened a node and then clicked on the parent... in which case
716
 
         * they likely wouldn't want to select all the children.  Regardless,
717
 
         * we may want this later --- Emile
718
 
         */
719
 
 
720
 
#if 0
721
 
        /* If a parent node is selected, select all children */
722
 
        if (GTK_CLIST(ctree)->selection != NULL) {
723
 
 
724
 
                rc = gtk_ctree_node_get_row_data(ctree, GTK_CTREE_NODE(node));
725
 
                if (NULL != rc->sha1) {
726
 
 
727
 
                        key = atom_sha1_get(rc->sha1);
728
 
                        parent = find_parent_with_sha1(sch->parents, key);
729
 
                
730
 
                        if (NULL != parent) {
731
 
                        
732
 
                                parent_row = GTK_CTREE_ROW(parent);
733
 
                                                                
734
 
                                /* A parent exists with that sha1, is it the selected node? */
735
 
                                if ((parent == GTK_CTREE_NODE(node)) 
736
 
                                        && (NULL != parent_row->children)) {
737
 
                                        gtk_ctree_select_recursive(ctree, GTK_CTREE_NODE(node));                                
738
 
                                }
739
 
                        }
740
 
                        
741
 
                        atom_sha1_free(key);
742
 
                }       
743
 
        }
744
 
#endif
745
 
}
746
 
 
747
 
 
748
 
/* 
749
 
 *      on_clist_search_results_unselect_row
750
 
 */
751
 
void on_ctree_search_results_unselect_row(
752
 
    GtkCTree *ctree, GList *node, gint column, gpointer user_data)
753
 
{
754
 
    refresh_popup();
755
 
}
756
 
 
757
 
 
758
 
/* 
759
 
 *      on_ctree_search_results_resize_column
760
 
 */
761
 
void on_ctree_search_results_resize_column(
762
 
    GtkCList * clist, gint column, gint width, gpointer user_data)
763
 
{
764
 
    guint32 buf = width;
765
 
 
766
 
    /* remember the width for storing it to the config file later */
767
 
    gui_prop_set_guint32(PROP_SEARCH_RESULTS_COL_WIDTHS, &buf, column, 1);
768
 
}
769
 
 
770
 
 
771
 
/* 
772
 
 *      on_button_search_passive_clicked
773
 
 *
774
 
 *      Please add comment
775
 
 *
776
 
 */
777
 
void on_button_search_passive_clicked(
778
 
    GtkButton *button, gpointer user_data)
779
 
{
780
 
    filter_t *default_filter;
781
 
        search_t *search;
782
 
 
783
 
    /*
784
 
     * We have to capture the selection here already, because
785
 
     * new_search will trigger a rebuild of the menu as a
786
 
     * side effect.
787
 
     */
788
 
    default_filter = (filter_t *)
789
 
        option_menu_get_selected_data
790
 
            (lookup_widget(main_window, "optionmenu_search_filter"));
791
 
 
792
 
        search_gui_new_search("Passive", SEARCH_PASSIVE, &search);
793
 
 
794
 
    /*
795
 
     * If we should set a default filter, we do that.
796
 
     */
797
 
    if (default_filter != NULL) {
798
 
        rule_t *rule = filter_new_jump_rule
799
 
            (default_filter, RULE_FLAG_ACTIVE);
800
 
            
801
 
        /*
802
 
         * Since we don't want to distrub the shadows and
803
 
         * do a "force commit" without the user having pressed
804
 
         * the "ok" button in the dialog, we add the rule
805
 
         * manually.
806
 
         */
807
 
        search->filter->ruleset = 
808
 
            g_list_append(search->filter->ruleset, rule);
809
 
        rule->target->refcount ++;
810
 
    }
811
 
}
812
 
 
813
 
 
814
 
 
815
 
/***
816
 
 *** Search results popup
817
 
 ***/
818
 
 
819
 
 
820
 
/* 
821
 
 *      search_cb_collect_ctree_data
822
 
 *
823
 
 *      Given a GList of GtkCTreeNodes, return a new list pointing to the row data 
824
 
 *      List will have to be freed later on.
825
 
 */
826
 
GList *search_cb_collect_ctree_data(GtkCTree *ctree, GList *node_list)
827
 
{
828
 
        GList *data_list = NULL;
829
 
        record_t *rc;
830
 
        
831
 
        for(; node_list != NULL; node_list = g_list_next(node_list)) {
832
 
        
833
 
                if(node_list->data != NULL) {
834
 
                        rc = gtk_ctree_node_get_row_data(ctree, node_list->data);
835
 
                        data_list = g_list_append(data_list, rc);
836
 
                }
837
 
        }
838
 
        
839
 
        data_list = g_list_first(data_list);
840
 
        return data_list;
841
 
}
842
 
 
843
 
 
844
 
 
845
 
/* 
846
 
 *      on_popup_search_drop_name_activate
847
 
 *
848
 
 *      For all selected results, create a filter based on name
849
 
 */
850
 
void on_popup_search_drop_name_activate(GtkMenuItem *menuitem, 
851
 
        gpointer user_data)
852
 
{
853
 
    GList *node_list, *data_list = NULL;
854
 
    search_t *search;
855
 
 
856
 
    search = search_gui_get_current_search();
857
 
    g_assert(search != NULL);
858
 
 
859
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
860
 
        
861
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
862
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
863
 
        
864
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_name_rule, 
865
 
                search->filter);
866
 
 
867
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
868
 
        g_list_free(data_list);
869
 
        g_list_free(node_list);
870
 
}
871
 
 
872
 
 
873
 
/* 
874
 
 *      on_popup_search_drop_sha1_activate
875
 
 *
876
 
 *      For all selected results, create a filter based on sha1
877
 
 */
878
 
void on_popup_search_drop_sha1_activate(GtkMenuItem *menuitem, 
879
 
        gpointer user_data)
880
 
{
881
 
    GList *node_list, *data_list = NULL;
882
 
    search_t *search;
883
 
 
884
 
    search = search_gui_get_current_search();
885
 
    g_assert(search != NULL);
886
 
 
887
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
888
 
        
889
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
890
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
891
 
        
892
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_sha1_rule, 
893
 
                search->filter);
894
 
 
895
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
896
 
        g_list_free(data_list);
897
 
        g_list_free(node_list);
898
 
}
899
 
 
900
 
 
901
 
/* 
902
 
 *      on_popup_search_drop_host_activate
903
 
 *
904
 
 *      For all selected results, create a filter based on host
905
 
 */
906
 
void on_popup_search_drop_host_activate(GtkMenuItem *menuitem, 
907
 
        gpointer user_data)
908
 
{
909
 
    GList *node_list, *data_list = NULL;
910
 
    search_t *search;
911
 
 
912
 
    search = search_gui_get_current_search();
913
 
    g_assert(search != NULL);
914
 
 
915
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
916
 
        
917
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
918
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
919
 
        
920
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_host_rule, 
921
 
                search->filter);
922
 
 
923
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
924
 
        g_list_free(data_list);
925
 
        g_list_free(node_list);
926
 
}
927
 
 
928
 
 
929
 
/* 
930
 
 *      on_popup_search_drop_name_global_activate
931
 
 *
932
 
 *      For all selected results, create a global filter based on name
933
 
 */
934
 
void on_popup_search_drop_name_global_activate(GtkMenuItem *menuitem, 
935
 
        gpointer user_data)
936
 
{
937
 
    GList *node_list, *data_list = NULL;
938
 
    search_t *search;
939
 
 
940
 
    search = search_gui_get_current_search();
941
 
    g_assert(search != NULL);
942
 
 
943
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
944
 
        
945
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
946
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
947
 
        
948
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_name_rule, 
949
 
                filter_get_global_pre());
950
 
 
951
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
952
 
        g_list_free(data_list);
953
 
        g_list_free(node_list);
954
 
}
955
 
 
956
 
 
957
 
/* 
958
 
 *      on_popup_search_drop_sha1_global_activate
959
 
 *
960
 
 *      For all selected results, create a global filter based on sha1
961
 
 *
962
 
 */
963
 
void on_popup_search_drop_sha1_global_activate(GtkMenuItem *menuitem, 
964
 
        gpointer user_data)
965
 
{
966
 
    GList *node_list, *data_list = NULL;
967
 
    search_t *search;
968
 
 
969
 
    search = search_gui_get_current_search();
970
 
    g_assert(search != NULL);
971
 
 
972
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
973
 
        
974
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
975
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
976
 
        
977
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_sha1_rule, 
978
 
                filter_get_global_pre());
979
 
 
980
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
981
 
        g_list_free(data_list);
982
 
        g_list_free(node_list);
983
 
}
984
 
 
985
 
 
986
 
/* 
987
 
 *      on_popup_search_drop_host_global_activate
988
 
 *
989
 
 *      For all selected results, create a global filter based on host
990
 
 *
991
 
 */
992
 
void on_popup_search_drop_host_global_activate(GtkMenuItem *menuitem, 
993
 
        gpointer user_data)
994
 
{
995
 
    GList *node_list, *data_list = NULL;
996
 
    search_t *search;
997
 
 
998
 
    search = search_gui_get_current_search();
999
 
    g_assert(search != NULL);
1000
 
 
1001
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
1002
 
        
1003
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
1004
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
1005
 
        
1006
 
    g_list_foreach(data_list, (GFunc) filter_add_drop_host_rule, 
1007
 
                filter_get_global_pre());
1008
 
 
1009
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
1010
 
        g_list_free(data_list);
1011
 
        g_list_free(node_list);
1012
 
}
1013
 
 
1014
 
 
1015
 
/* 
1016
 
 *      on_popup_search_autodownload_name_activate
1017
 
 *
1018
 
 *      Please add comment
1019
 
 *
1020
 
 */
1021
 
void on_popup_search_autodownload_name_activate(GtkMenuItem *menuitem, 
1022
 
        gpointer user_data)
1023
 
{
1024
 
    GList *node_list, *data_list = NULL;
1025
 
    search_t *search;
1026
 
 
1027
 
    search = search_gui_get_current_search();
1028
 
    g_assert(search != NULL);
1029
 
 
1030
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
1031
 
        
1032
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
1033
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
1034
 
        
1035
 
    g_list_foreach(data_list, (GFunc) filter_add_download_name_rule, 
1036
 
                search->filter);
1037
 
 
1038
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
1039
 
        g_list_free(data_list);
1040
 
        g_list_free(node_list);
1041
 
}
1042
 
 
1043
 
 
1044
 
/* 
1045
 
 *      on_popup_search_autodownload_sha1_activate
1046
 
 *
1047
 
 *      Please add comment
1048
 
 *
1049
 
 */
1050
 
void on_popup_search_autodownload_sha1_activate(GtkMenuItem *menuitem, 
1051
 
        gpointer user_data)
1052
 
{
1053
 
    GList *node_list, *data_list = NULL;
1054
 
    search_t *search;
1055
 
 
1056
 
    search = search_gui_get_current_search();
1057
 
    g_assert(search != NULL);
1058
 
 
1059
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
1060
 
        
1061
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
1062
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
1063
 
        
1064
 
    g_list_foreach(data_list, (GFunc) filter_add_download_sha1_rule, 
1065
 
                search->filter);
1066
 
 
1067
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
1068
 
        g_list_free(data_list);
1069
 
        g_list_free(node_list);
1070
 
}
1071
 
 
1072
 
 
1073
 
/* 
1074
 
 *      on_popup_search_new_from_selected_activate
1075
 
 *
1076
 
 *      Please add comment
1077
 
 *
1078
 
 */
1079
 
void on_popup_search_new_from_selected_activate(GtkMenuItem *menuitem, 
1080
 
        gpointer user_data)
1081
 
{
1082
 
    GList *node_list, *data_list = NULL;
1083
 
    search_t *search;
1084
 
 
1085
 
    search = search_gui_get_current_search();
1086
 
    g_assert(search != NULL);
1087
 
 
1088
 
    gtk_clist_freeze(GTK_CLIST(search->ctree));
1089
 
        
1090
 
        node_list = g_list_copy(GTK_CLIST(search->ctree)->selection);
1091
 
        data_list = search_cb_collect_ctree_data(search->ctree, node_list);
1092
 
        
1093
 
    g_list_foreach(data_list, (GFunc) gui_add_targetted_search, 
1094
 
                search->filter);
1095
 
 
1096
 
    gtk_clist_thaw(GTK_CLIST(search->ctree));
1097
 
        g_list_free(data_list);
1098
 
        g_list_free(node_list);
1099
 
}
1100
 
 
1101
 
 
1102
 
/* 
1103
 
 *      on_popup_search_edit_filter_activate
1104
 
 */
1105
 
void on_popup_search_edit_filter_activate(GtkMenuItem * menuitem, 
1106
 
        gpointer user_data)
1107
 
{
1108
 
    filter_open_dialog();
1109
 
}
1110
 
 
1111
 
 
1112
 
/* 
1113
 
 *      on_popup_search_duplicate_activate
1114
 
 *
1115
 
 *      Create a new search identical to the current search
1116
 
 *      Note: Doesn't duplicate filters or passive searches yet
1117
 
 */
1118
 
void on_popup_search_duplicate_activate(GtkMenuItem * menuitem, 
1119
 
        gpointer user_data)
1120
 
{
1121
 
    search_t *search;
1122
 
    guint32 timeout;
1123
 
 
1124
 
    gnet_prop_get_guint32_val(PROP_SEARCH_REISSUE_TIMEOUT, &timeout);
1125
 
 
1126
 
    search = search_gui_get_current_search();
1127
 
    /* FIXME: should also duplicate filters! */
1128
 
    /* FIXME: should call search_duplicate which has to be written. */
1129
 
    /* FIXME: should properly duplicate passive searches. */
1130
 
        if (search)
1131
 
                search_gui_new_search_full(search->query, 0,
1132
 
                        timeout, search->sort_col, search->sort_order,
1133
 
                        search->enabled ? SEARCH_ENABLED : 0, NULL);
1134
 
}
1135
 
 
1136
 
 
1137
 
/* 
1138
 
 *      on_popup_search_restart_activate
1139
 
 */
1140
 
void on_popup_search_restart_activate(GtkMenuItem *menuitem, gpointer user_data)
1141
 
{
1142
 
    search_t *search;
1143
 
 
1144
 
    search = search_gui_get_current_search();
1145
 
        if (search)
1146
 
                search_gui_restart_search(search);
1147
 
}
1148
 
 
1149
 
 
1150
 
/* 
1151
 
 *      on_popup_search_resume_activate
1152
 
 */
1153
 
void on_popup_search_resume_activate(GtkMenuItem * menuitem, gpointer user_data)
1154
 
{
1155
 
    search_t *search;
1156
 
 
1157
 
    search = search_gui_get_current_search();
1158
 
        if (search)
1159
 
                gui_search_set_enabled(search, TRUE);
1160
 
}
1161
 
 
1162
 
 
1163
 
/* 
1164
 
 *      on_popup_search_stop_activate
1165
 
 *
1166
 
 *      Stop current search
1167
 
 */
1168
 
void on_popup_search_stop_activate
1169
 
    (GtkMenuItem *menuitem, gpointer user_data)
1170
 
{
1171
 
    search_t *search;
1172
 
 
1173
 
    search = search_gui_get_current_search();
1174
 
        if (search)
1175
 
                gui_search_set_enabled(search, FALSE);
1176
 
}
1177
 
 
1178
 
 
1179
 
/* 
1180
 
 *      on_popup_search_config_cols_activate
1181
 
 *
1182
 
 *      Please add comment
1183
 
 *
1184
 
 */
1185
 
void on_popup_search_config_cols_activate(GtkMenuItem * menuitem,
1186
 
        gpointer user_data)
1187
 
{
1188
 
    search_t *search;
1189
 
 
1190
 
    search = search_gui_get_current_search();
1191
 
    g_return_if_fail(search != NULL);
1192
 
    g_assert(search->ctree != NULL);
1193
 
 
1194
 
    {
1195
 
        GtkWidget * cc;
1196
 
 
1197
 
        /* FIXME: needs to work also in Gtk2 or be replaced. */
1198
 
        cc = gtk_column_chooser_new(GTK_WIDGET(search->ctree));
1199
 
        gtk_menu_popup(GTK_MENU(cc), NULL, NULL, NULL, NULL, 1, 0);
1200
 
 
1201
 
        /* GtkColumnChooser takes care of cleaning up itself */
1202
 
    }
1203
 
}
1204
 
 
1205
 
 
1206
 
/* 
1207
 
 *      on_popup_search_expand_all_activate
1208
 
 */
1209
 
void on_popup_search_expand_all_activate(GtkMenuItem *menuitem, 
1210
 
        gpointer user_data)
1211
 
{
1212
 
    search_gui_expand_all();
1213
 
 
1214
 
}
1215
 
 
1216
 
 
1217
 
/* 
1218
 
 *      on_popup_search_collapse_all_activate
1219
 
 */
1220
 
void on_popup_search_collapse_all_activate(GtkMenuItem *menuitem,
1221
 
        gpointer user_data)
1222
 
{
1223
 
    search_gui_collapse_all();
1224
 
 
1225
 
}
1226
 
 
1227
 
#endif  /* USE_GTK1 */