2005-01-29 [paul] 1.0.0cvs23.2
[claws.git] / src / grouplistdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtkmain.h>
29 #include <gtk/gtkwidget.h>
30 #include <gtk/gtkdialog.h>
31 #include <gtk/gtkwindow.h>
32 #include <gtk/gtksignal.h>
33 #include <gtk/gtkvbox.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkctree.h>
38 #include <gtk/gtkscrolledwindow.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkhbbox.h>
41 #include <gtk/gtkstock.h>
42 #include <string.h>
43
44 #include "intl.h"
45 #include "grouplistdialog.h"
46 #include "manage_window.h"
47 #include "gtkutils.h"
48 #include "utils.h"
49 #include "news.h"
50 #include "folder.h"
51 #include "alertpanel.h"
52 #include "recv.h"
53 #include "socket.h"
54
55 #define GROUPLIST_DIALOG_WIDTH          450
56 #define GROUPLIST_DIALOG_HEIGHT         400
57 #define GROUPLIST_COL_NAME_WIDTH        250
58
59 static gboolean ack;
60 static gboolean locked;
61
62 static GtkWidget *dialog;
63 static GtkWidget *entry;
64 static GtkWidget *ctree;
65 static GtkWidget *status_label;
66 static GtkWidget *ok_button;
67 static GSList *group_list;
68 static Folder *news_folder;
69
70 static GSList *subscribed;
71
72 static void grouplist_dialog_create     (void);
73 static void grouplist_dialog_set_list   (const gchar    *pattern,
74                                          gboolean        refresh);
75 static void grouplist_search            (void);
76 static void grouplist_clear             (void);
77 static gboolean grouplist_recv_func     (SockInfo       *sock,
78                                          gint            count,
79                                          gint            read_bytes,
80                                          gpointer        data);
81
82 static gint window_deleted      (GtkWidget      *widget,
83                                  GdkEventAny    *event,
84                                  gpointer        data);
85 static void ok_clicked          (GtkWidget      *widget,
86                                  gpointer        data);
87 static void cancel_clicked      (GtkWidget      *widget,
88                                  gpointer        data);
89 static void refresh_clicked     (GtkWidget      *widget,
90                                  gpointer        data);
91 static gboolean key_pressed     (GtkWidget      *widget,
92                                  GdkEventKey    *event,
93                                  gpointer        data);
94 static gboolean button_press_cb (GtkCTree       *ctree,
95                                  GdkEventButton *button,
96                                  gpointer        data);
97 static void entry_activated     (GtkEditable    *editable);
98 static void search_clicked      (GtkWidget      *widget,
99                                  gpointer        data);
100
101 GSList *grouplist_dialog(Folder *folder)
102 {
103         GNode *node;
104         FolderItem *item;
105
106         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
107
108         if (!dialog)
109                 grouplist_dialog_create();
110
111         news_folder = folder;
112
113         gtk_widget_show(dialog);
114         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
115         manage_window_set_transient(GTK_WINDOW(dialog));
116         gtk_widget_grab_focus(ok_button);
117         gtk_widget_grab_focus(ctree);
118         GTK_EVENTS_FLUSH();
119
120         subscribed = NULL;
121         for (node = folder->node->children; node != NULL; node = node->next) {
122                 item = FOLDER_ITEM(node->data);
123                 subscribed = g_slist_append(subscribed, g_strdup(item->path));
124         }
125
126         grouplist_dialog_set_list(NULL, TRUE);
127
128         if (ack) gtk_main();
129
130         manage_window_focus_out(dialog, NULL, NULL);
131         gtk_widget_hide(dialog);
132
133         if (!ack) {
134                 slist_free_strings(subscribed);
135                 g_slist_free(subscribed);
136                 subscribed = NULL;
137
138                 for (node = folder->node->children; node != NULL;
139                      node = node->next) {
140                         item = FOLDER_ITEM(node->data);
141                         subscribed = g_slist_append(subscribed,
142                                                     g_strdup(item->path));
143                 }
144         }
145
146         grouplist_clear();
147
148         return subscribed;
149 }
150
151 static void grouplist_dialog_create(void)
152 {
153         GtkWidget *vbox;
154         GtkWidget *hbox;
155         GtkWidget *msg_label;
156         GtkWidget *search_button;
157         GtkWidget *confirm_area;
158         GtkWidget *cancel_button;       
159         GtkWidget *refresh_button;      
160         GtkWidget *scrolledwin;
161         gchar *titles[3];
162         gint i;
163
164         dialog = gtk_dialog_new();
165         gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
166         gtk_widget_set_size_request(dialog,
167                                     GROUPLIST_DIALOG_WIDTH,
168                                     GROUPLIST_DIALOG_HEIGHT);
169         gtk_container_set_border_width
170                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
171         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
172         gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
173         g_signal_connect(G_OBJECT(dialog), "delete_event",
174                          G_CALLBACK(window_deleted), NULL);
175         g_signal_connect(G_OBJECT(dialog), "key_press_event",
176                          G_CALLBACK(key_pressed), NULL);
177         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
178
179         gtk_widget_realize(dialog);
180
181         vbox = gtk_vbox_new(FALSE, 8);
182         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
183         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
184
185         hbox = gtk_hbox_new(FALSE, 0);
186         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
187
188         msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
189         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
190
191         hbox = gtk_hbox_new(FALSE, 8);
192         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
193
194         msg_label = gtk_label_new(_("Find groups:"));
195         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
196
197         entry = gtk_entry_new();
198         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
199         g_signal_connect(G_OBJECT(entry), "activate",
200                          G_CALLBACK(entry_activated), NULL);
201
202         search_button = gtk_button_new_with_label(_(" Search "));
203         gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
204
205         g_signal_connect(G_OBJECT(search_button), "clicked",
206                          G_CALLBACK(search_clicked), NULL);
207
208         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
209         gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
210         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
211                                        GTK_POLICY_AUTOMATIC,
212                                        GTK_POLICY_AUTOMATIC);
213
214         titles[0] = _("Newsgroup name");
215         titles[1] = _("Messages");
216         titles[2] = _("Type");
217         ctree = gtk_ctree_new_with_titles(3, 0, titles);
218         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
219         gtk_clist_set_column_width
220                 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
221         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_MULTIPLE);
222         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
223         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
224                                      GTK_CTREE_EXPANDER_SQUARE);
225         for (i = 0; i < 3; i++)
226                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
227                                        GTK_CAN_FOCUS);
228         g_signal_connect(G_OBJECT(ctree), "button-press-event",
229                          G_CALLBACK(button_press_cb), NULL);
230
231         hbox = gtk_hbox_new(FALSE, 0);
232         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
233
234         status_label = gtk_label_new("");
235         gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
236
237         gtkut_stock_button_set_create(&confirm_area,
238                                       &ok_button, GTK_STOCK_OK,
239                                       &cancel_button, GTK_STOCK_CANCEL,
240                                       &refresh_button, GTK_STOCK_REFRESH);
241         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
242                           confirm_area);
243         gtk_widget_grab_default(ok_button);
244
245         g_signal_connect(G_OBJECT(ok_button), "clicked",
246                          G_CALLBACK(ok_clicked), NULL);
247         g_signal_connect(G_OBJECT(cancel_button), "clicked",
248                          G_CALLBACK(cancel_clicked), NULL);
249         g_signal_connect(G_OBJECT(refresh_button), "clicked",
250                          G_CALLBACK(refresh_clicked), NULL);
251
252         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
253 }
254
255 static GHashTable *branch_node_table;
256
257 static void grouplist_hash_init(void)
258 {
259         branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
260 }
261
262 static void grouplist_hash_done(void)
263 {
264         hash_free_strings(branch_node_table);
265         g_hash_table_destroy(branch_node_table);
266 }
267
268 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
269 {
270         return g_hash_table_lookup(branch_node_table, name);
271 }
272
273 static void grouplist_hash_set_branch_node(const gchar *name,
274                                            GtkCTreeNode *node)
275 {
276         g_hash_table_insert(branch_node_table, g_strdup(name), node);
277 }
278
279 static gchar *grouplist_get_parent_name(const gchar *name)
280 {
281         gchar *p;
282
283         p = strrchr(name, '.');
284         if (!p)
285                 return g_strdup("");
286         return g_strndup(name, p - name);
287 }
288
289 static GtkCTreeNode *grouplist_create_parent(const gchar *name,
290                                              const gchar *pattern)
291 {
292         GtkCTreeNode *parent;
293         GtkCTreeNode *node;
294         gchar *cols[3];
295         gchar *parent_name;
296
297         if (*name == '\0') return NULL;
298         node = grouplist_hash_get_branch_node(name);
299         if (node != NULL) return node;
300
301         cols[0] = (gchar *)name;
302         cols[1] = cols[2] = "";
303
304         parent_name = grouplist_get_parent_name(name);
305         parent = grouplist_create_parent(parent_name, pattern);
306
307         node = parent ? GTK_CTREE_ROW(parent)->children
308                 : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
309         node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
310                                      cols, 0, NULL, NULL, NULL, NULL,
311                                      FALSE, FALSE);
312         if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
313                 gtk_ctree_expand(GTK_CTREE(ctree), parent);
314         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
315
316         grouplist_hash_set_branch_node(name, node);
317
318         g_free(parent_name);
319
320         return node;
321 }
322
323 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
324                                              const gchar *pattern)
325 {
326         GtkCTreeNode *node;
327         GtkCTreeNode *parent;
328         gchar *name = (gchar *)ginfo->name;
329         gchar *parent_name;
330         gchar *count_str;
331         gchar *cols[3];
332         gint count;
333
334         count = ginfo->last - ginfo->first;
335         if (count < 0)
336                 count = 0;
337         count_str = itos(count);
338
339         cols[0] = ginfo->name;
340         cols[1] = count_str;
341         if (ginfo->type == 'y')
342                 cols[2] = "";
343         else if (ginfo->type == 'm')
344                 cols[2] = _("moderated");
345         else if (ginfo->type == 'n')
346                 cols[2] = _("readonly");
347         else
348                 cols[2] = _("unknown");
349
350         parent_name = grouplist_get_parent_name(name);
351         parent = grouplist_create_parent(parent_name, pattern);
352         node = grouplist_hash_get_branch_node(name);
353         if (node) {
354                 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
355                                         NULL, NULL, NULL, NULL, FALSE, FALSE);
356                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
357                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
358         } else {
359                 node = parent ? GTK_CTREE_ROW(parent)->children
360                         : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
361                 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
362                                              cols, 0, NULL, NULL, NULL, NULL,
363                                              TRUE, FALSE);
364                 if (parent &&
365                     g_pattern_match_simple(pattern, parent_name) == FALSE)
366                         gtk_ctree_expand(GTK_CTREE(ctree), parent);
367         }
368         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
369         if (node)
370                 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
371
372         g_free(parent_name);
373
374         return node;
375 }
376
377 static void grouplist_expand_upwards(GtkCTree *ctree, const gchar *name) {
378         const gchar *ptr;
379         gchar *newname=g_malloc0(strlen(name));
380
381         for (ptr=name; *ptr; ptr++) {
382                 if (*ptr == '.')
383                         gtk_ctree_expand(ctree, 
384                                 grouplist_hash_get_branch_node(newname));
385                 newname[ptr-name] = *ptr;
386         }
387         g_free(newname);
388 }
389
390 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
391 {
392         GSList *cur;
393         GtkCTreeNode *node;
394         GPatternSpec *pspec;
395
396         if (locked) return;
397         locked = TRUE;
398
399         if (!pattern || *pattern == '\0')
400                 pattern = "*";
401
402         if (refresh) {
403                 ack = TRUE;
404                 grouplist_clear();
405                 recv_set_ui_func(grouplist_recv_func, NULL);
406                 group_list = news_get_group_list(news_folder);
407                 group_list = g_slist_reverse(group_list);
408                 recv_set_ui_func(NULL, NULL);
409                 if (group_list == NULL && ack == TRUE) {
410                         alertpanel_error(_("Can't retrieve newsgroup list."));
411                         locked = FALSE;
412                         return;
413                 }
414         } else
415                 gtk_clist_clear(GTK_CLIST(ctree));
416
417         gtk_entry_set_text(GTK_ENTRY(entry), pattern);
418
419         grouplist_hash_init();
420
421         gtk_clist_freeze(GTK_CLIST(ctree));
422
423         pspec = g_pattern_spec_new(pattern);
424
425         for (cur = group_list; cur != NULL ; cur = cur->next) {
426                 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
427
428                 if (g_pattern_match_string(pspec, ginfo->name)) {
429                         node = grouplist_create_branch(ginfo, pattern);
430                         if (g_slist_find_custom(subscribed, ginfo->name,
431                                                 (GCompareFunc)g_strcasecmp)
432                             != NULL)
433                                 gtk_ctree_select(GTK_CTREE(ctree), node);
434                 }
435         }
436         for (cur = subscribed; cur; cur = g_slist_next(cur))
437                 grouplist_expand_upwards(GTK_CTREE(ctree), (gchar *)cur->data);
438
439         g_pattern_spec_free(pspec);
440
441         gtk_clist_thaw(GTK_CLIST(ctree));
442
443         grouplist_hash_done();
444
445         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
446
447         locked = FALSE;
448 }
449
450 static void grouplist_search(void)
451 {
452         gchar *str;
453
454         if (locked) return;
455
456         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
457         grouplist_dialog_set_list(str, FALSE);
458         g_free(str);
459 }
460
461 static void grouplist_clear(void)
462 {
463         gtk_clist_clear(GTK_CLIST(ctree));
464         gtk_entry_set_text(GTK_ENTRY(entry), "");
465         news_group_list_free(group_list);
466         group_list = NULL;
467 }
468
469 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
470                                     gpointer data)
471 {
472         gchar buf[BUFFSIZE];
473
474         g_snprintf(buf, sizeof(buf),
475                    _("%d newsgroups received (%s read)"),
476                    count, to_human_readable(read_bytes));
477         gtk_label_set_text(GTK_LABEL(status_label), buf);
478         GTK_EVENTS_FLUSH();
479         if (ack == FALSE)
480                 return FALSE;
481         else
482                 return TRUE;
483 }
484
485 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
486 {
487         ack = FALSE;
488         if (gtk_main_level() > 1)
489                 gtk_main_quit();
490
491         return TRUE;
492 }
493
494 static void ok_clicked(GtkWidget *widget, gpointer data)
495 {
496         ack = TRUE;
497         if (gtk_main_level() > 1)
498                 gtk_main_quit();
499 }
500
501 static void cancel_clicked(GtkWidget *widget, gpointer data)
502 {
503         ack = FALSE;
504         if (gtk_main_level() > 1)
505                 gtk_main_quit();
506 }
507
508 static void refresh_clicked(GtkWidget *widget, gpointer data)
509
510         gchar *str;
511
512         if (locked) return;
513
514         news_remove_group_list_cache(news_folder);
515
516         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
517         grouplist_dialog_set_list(str, TRUE);
518         g_free(str);
519 }
520
521 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
522 {
523         if (event && event->keyval == GDK_Escape)
524                 cancel_clicked(NULL, NULL);
525         return FALSE;
526 }
527
528 /* clist/ctree clear old selection on click (gtk2 only)
529  * - intercept all button clicks (always return TRUE)
530  * - only allow left button single click
531  * - handle click on expander
532  * - update "subscribed" list and un-/select row
533  */
534 static gboolean button_press_cb(GtkCTree *ctree, GdkEventButton *button,
535                                 gpointer data)
536 {
537         gint row, col;
538         GtkCTreeNode *node;
539         NewsGroupInfo *ginfo;
540         GSList *list;
541
542         if (button->type != GDK_BUTTON_PRESS) return TRUE;
543         if (button->button != 1) return TRUE;
544
545         gtk_clist_get_selection_info(GTK_CLIST(ctree), 
546                                      button->x, button->y, &row, &col);
547         node = gtk_ctree_node_nth(ctree, row);
548         if (!node) return TRUE;
549
550         if (gtk_ctree_is_hot_spot(ctree, button->x, button->y)) {
551                 gtk_ctree_toggle_expansion(ctree, node);
552                 return TRUE;
553         }
554
555         ginfo = gtk_ctree_node_get_row_data(ctree, node);
556         if (!ginfo) return TRUE;
557
558         list = g_slist_find_custom(subscribed, ginfo->name,
559                                    (GCompareFunc)g_ascii_strcasecmp);
560         if (list) {
561                 g_free(list->data);
562                 subscribed = g_slist_remove(subscribed, list->data);
563                 gtk_clist_unselect_row(GTK_CLIST(ctree), row, 0);
564         } else {
565                 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
566                 gtk_clist_select_row(GTK_CLIST(ctree), row, 0);
567         }
568
569         return TRUE;
570 }
571
572 static void entry_activated(GtkEditable *editable)
573 {
574         grouplist_search();
575 }
576
577 static void search_clicked(GtkWidget *widget, gpointer data)
578 {
579         grouplist_search();
580 }