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