d1850c73eff0573b09c2029c5a944e3f944df760
[claws.git] / src / grouplistdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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 <string.h>
42 #include <fnmatch.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, GROUPLIST_DIALOG_HEIGHT);
168         gtk_container_set_border_width
169                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
170         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
171         gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
172         g_signal_connect(G_OBJECT(dialog), "delete_event",
173                          G_CALLBACK(window_deleted), NULL);
174         g_signal_connect(G_OBJECT(dialog), "key_press_event",
175                          G_CALLBACK(key_pressed), NULL);
176         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
177
178         gtk_widget_realize(dialog);
179
180         vbox = gtk_vbox_new(FALSE, 8);
181         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
182         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
183
184         hbox = gtk_hbox_new(FALSE, 0);
185         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
186
187         msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
188         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
189
190         hbox = gtk_hbox_new(FALSE, 8);
191         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
192
193         msg_label = gtk_label_new(_("Find groups:"));
194         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
195
196         entry = gtk_entry_new();
197         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
198         g_signal_connect(G_OBJECT(entry), "activate",
199                          G_CALLBACK(entry_activated), NULL);
200
201         search_button = gtk_button_new_with_label(_(" Search "));
202         gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
203
204         g_signal_connect(G_OBJECT(search_button), "clicked",
205                          G_CALLBACK(search_clicked), NULL);
206
207         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
208         gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
209         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
210                                        GTK_POLICY_AUTOMATIC,
211                                        GTK_POLICY_AUTOMATIC);
212
213         titles[0] = _("Newsgroup name");
214         titles[1] = _("Messages");
215         titles[2] = _("Type");
216         ctree = gtk_ctree_new_with_titles(3, 0, titles);
217         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
218         gtk_clist_set_column_width
219                 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
220         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_MULTIPLE);
221         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
222         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
223                                      GTK_CTREE_EXPANDER_SQUARE);
224         for (i = 0; i < 3; i++)
225                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
226                                        GTK_CAN_FOCUS);
227         g_signal_connect(G_OBJECT(ctree), "button-press-event",
228                          G_CALLBACK(button_press_cb), NULL);
229
230         hbox = gtk_hbox_new(FALSE, 0);
231         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
232
233         status_label = gtk_label_new("");
234         gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
235
236         gtkut_button_set_create(&confirm_area,
237                                 &ok_button,      _("OK"),
238                                 &cancel_button,  _("Cancel"),
239                                 &refresh_button, _("Refresh"));
240         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
241                           confirm_area);
242         gtk_widget_grab_default(ok_button);
243
244         g_signal_connect(G_OBJECT(ok_button), "clicked",
245                          G_CALLBACK(ok_clicked), NULL);
246         g_signal_connect(G_OBJECT(cancel_button), "clicked",
247                          G_CALLBACK(cancel_clicked), NULL);
248         g_signal_connect(G_OBJECT(refresh_button), "clicked",
249                          G_CALLBACK(refresh_clicked), NULL);
250
251         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
252 }
253
254 static GHashTable *branch_node_table;
255
256 static void grouplist_hash_init(void)
257 {
258         branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
259 }
260
261 static void grouplist_hash_done(void)
262 {
263         hash_free_strings(branch_node_table);
264         g_hash_table_destroy(branch_node_table);
265 }
266
267 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
268 {
269         return g_hash_table_lookup(branch_node_table, name);
270 }
271
272 static void grouplist_hash_set_branch_node(const gchar *name,
273                                            GtkCTreeNode *node)
274 {
275         g_hash_table_insert(branch_node_table, g_strdup(name), node);
276 }
277
278 static gchar *grouplist_get_parent_name(const gchar *name)
279 {
280         gchar *p;
281
282         p = strrchr(name, '.');
283         if (!p)
284                 return g_strdup("");
285         return g_strndup(name, p - name);
286 }
287
288 static GtkCTreeNode *grouplist_create_parent(const gchar *name,
289                                              const gchar *pattern)
290 {
291         GtkCTreeNode *parent;
292         GtkCTreeNode *node;
293         gchar *cols[3];
294         gchar *parent_name;
295
296         if (*name == '\0') return NULL;
297         node = grouplist_hash_get_branch_node(name);
298         if (node != NULL) return node;
299
300         cols[0] = (gchar *)name;
301         cols[1] = cols[2] = "";
302
303         parent_name = grouplist_get_parent_name(name);
304         parent = grouplist_create_parent(parent_name, pattern);
305
306         node = parent ? GTK_CTREE_ROW(parent)->children
307                 : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
308         node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
309                                      cols, 0, NULL, NULL, NULL, NULL,
310                                      FALSE, FALSE);
311         if (parent && fnmatch(pattern, parent_name, 0) != 0)
312                 gtk_ctree_expand(GTK_CTREE(ctree), parent);
313         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
314
315         grouplist_hash_set_branch_node(name, node);
316
317         g_free(parent_name);
318
319         return node;
320 }
321
322 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
323                                              const gchar *pattern)
324 {
325         GtkCTreeNode *node;
326         GtkCTreeNode *parent;
327         gchar *name = (gchar *)ginfo->name;
328         gchar *parent_name;
329         gchar *count_str;
330         gchar *cols[3];
331         gint count;
332
333         count = ginfo->last - ginfo->first;
334         if (count < 0)
335                 count = 0;
336         count_str = itos(count);
337
338         cols[0] = ginfo->name;
339         cols[1] = count_str;
340         if (ginfo->type == 'y')
341                 cols[2] = "";
342         else if (ginfo->type == 'm')
343                 cols[2] = _("moderated");
344         else if (ginfo->type == 'n')
345                 cols[2] = _("readonly");
346         else
347                 cols[2] = _("unknown");
348
349         parent_name = grouplist_get_parent_name(name);
350         parent = grouplist_create_parent(parent_name, pattern);
351         node = grouplist_hash_get_branch_node(name);
352         if (node) {
353                 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
354                                         NULL, NULL, NULL, NULL, FALSE, FALSE);
355                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
356                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
357         } else {
358                 node = parent ? GTK_CTREE_ROW(parent)->children
359                         : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
360                 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
361                                              cols, 0, NULL, NULL, NULL, NULL,
362                                              TRUE, FALSE);
363                 if (parent && fnmatch(pattern, parent_name, 0) != 0)
364                         gtk_ctree_expand(GTK_CTREE(ctree), parent);
365         }
366         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
367         if (node)
368                 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
369
370         g_free(parent_name);
371
372         return node;
373 }
374
375 static void grouplist_expand_upwards(GtkCTree *ctree, const gchar *name) {
376         const gchar *ptr;
377         gchar *newname=g_malloc0(strlen(name));
378
379         for (ptr=name; *ptr; ptr++) {
380                 if (*ptr == '.')
381                         gtk_ctree_expand(ctree, 
382                                 grouplist_hash_get_branch_node(newname));
383                 newname[ptr-name] = *ptr;
384         }
385         g_free(newname);
386 }
387
388 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
389 {
390         GSList *cur;
391         GtkCTreeNode *node;
392
393         if (locked) return;
394         locked = TRUE;
395
396         if (!pattern || *pattern == '\0')
397                 pattern = "*";
398
399         if (refresh) {
400                 ack = TRUE;
401                 grouplist_clear();
402                 recv_set_ui_func(grouplist_recv_func, NULL);
403                 group_list = news_get_group_list(news_folder);
404                 group_list = g_slist_reverse(group_list);
405                 recv_set_ui_func(NULL, NULL);
406                 if (group_list == NULL && ack == TRUE) {
407                         alertpanel_error(_("Can't retrieve newsgroup list."));
408                         locked = FALSE;
409                         return;
410                 }
411         } else
412                 gtk_clist_clear(GTK_CLIST(ctree));
413
414         gtk_entry_set_text(GTK_ENTRY(entry), pattern);
415
416         grouplist_hash_init();
417
418         gtk_clist_freeze(GTK_CLIST(ctree));
419
420
421         for (cur = group_list; cur != NULL ; cur = cur->next) {
422                 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
423
424                 if (fnmatch(pattern, ginfo->name, 0) == 0) {
425                         node = grouplist_create_branch(ginfo, pattern);
426                         if (g_slist_find_custom(subscribed, ginfo->name,
427                                                 (GCompareFunc)g_ascii_strcasecmp)
428                                                 != NULL) {
429                                 gtk_ctree_select(GTK_CTREE(ctree), node);
430                         }
431                 }
432         }
433         for (cur = subscribed; cur; cur = g_slist_next(cur))
434                 grouplist_expand_upwards(GTK_CTREE(ctree), (gchar *)cur->data);
435
436         gtk_clist_thaw(GTK_CLIST(ctree));
437
438         grouplist_hash_done();
439
440         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
441
442         locked = FALSE;
443 }
444
445 static void grouplist_search(void)
446 {
447         gchar *str;
448
449         if (locked) return;
450
451         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
452         grouplist_dialog_set_list(str, FALSE);
453         g_free(str);
454 }
455
456 static void grouplist_clear(void)
457 {
458         gtk_clist_clear(GTK_CLIST(ctree));
459         gtk_entry_set_text(GTK_ENTRY(entry), "");
460         news_group_list_free(group_list);
461         group_list = NULL;
462 }
463
464 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
465                                     gpointer data)
466 {
467         gchar buf[BUFFSIZE];
468
469         g_snprintf(buf, sizeof(buf),
470                    _("%d newsgroups received (%s read)"),
471                    count, to_human_readable(read_bytes));
472         gtk_label_set_text(GTK_LABEL(status_label), buf);
473         GTK_EVENTS_FLUSH();
474         if (ack == FALSE)
475                 return FALSE;
476         else
477                 return TRUE;
478 }
479
480 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
481 {
482         ack = FALSE;
483         if (gtk_main_level() > 1)
484                 gtk_main_quit();
485
486         return TRUE;
487 }
488
489 static void ok_clicked(GtkWidget *widget, gpointer data)
490 {
491         ack = TRUE;
492         if (gtk_main_level() > 1)
493                 gtk_main_quit();
494 }
495
496 static void cancel_clicked(GtkWidget *widget, gpointer data)
497 {
498         ack = FALSE;
499         if (gtk_main_level() > 1)
500                 gtk_main_quit();
501 }
502
503 static void refresh_clicked(GtkWidget *widget, gpointer data)
504
505         gchar *str;
506
507         if (locked) return;
508
509         news_remove_group_list_cache(news_folder);
510
511         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
512         grouplist_dialog_set_list(str, TRUE);
513         g_free(str);
514 }
515
516 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
517 {
518         if (event && event->keyval == GDK_Escape)
519                 cancel_clicked(NULL, NULL);
520         return FALSE;
521 }
522
523 /* clist/ctree clear old selection on click (gtk2 only)
524  * - intercept all button clicks (always return TRUE)
525  * - only allow left button single click
526  * - handle click on expander
527  * - update "subscribed" list and un-/select row
528  */
529 static gboolean button_press_cb(GtkCTree *ctree, GdkEventButton *button,
530                                 gpointer data)
531 {
532         gint row, col;
533         GtkCTreeNode *node;
534         NewsGroupInfo *ginfo;
535         GSList *list;
536
537         if (button->type != GDK_BUTTON_PRESS) return TRUE;
538         if (button->button != 1) return TRUE;
539
540         gtk_clist_get_selection_info(GTK_CLIST(ctree), 
541                                      button->x, button->y, &row, &col);
542         node = gtk_ctree_node_nth(ctree, row);
543         if (!node) return TRUE;
544
545         if (gtk_ctree_is_hot_spot(ctree, button->x, button->y)) {
546                 gtk_ctree_toggle_expansion(ctree, node);
547                 return TRUE;
548         }
549
550         ginfo = gtk_ctree_node_get_row_data(ctree, node);
551         if (!ginfo) return TRUE;
552
553         list = g_slist_find_custom(subscribed, ginfo->name,
554                                    (GCompareFunc)g_ascii_strcasecmp);
555         if (list) {
556                 g_free(list->data);
557                 subscribed = g_slist_remove(subscribed, list->data);
558                 gtk_clist_unselect_row(GTK_CLIST(ctree), row, 0);
559         } else {
560                 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
561                 gtk_clist_select_row(GTK_CLIST(ctree), row, 0);
562         }
563
564         return TRUE;
565 }
566
567 static void entry_activated(GtkEditable *editable)
568 {
569         grouplist_search();
570 }
571
572 static void search_clicked(GtkWidget *widget, gpointer data)
573 {
574         grouplist_search();
575 }