sync with sylpheed 0.6.5cvs8
[claws.git] / src / grouplistdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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   (gchar          *pattern,
74                                          gboolean        refresh);
75 static void grouplist_search            (void);
76 static void grouplist_clear             (void);
77 static void grouplist_recv_func         (SockInfo       *sock,
78                                          gint            count,
79                                          gint            read_bytes,
80                                          gpointer        data);
81
82 static void ok_clicked          (GtkWidget      *widget,
83                                  gpointer        data);
84 static void cancel_clicked      (GtkWidget      *widget,
85                                  gpointer        data);
86 static void refresh_clicked     (GtkWidget      *widget,
87                                  gpointer        data);
88 static void key_pressed         (GtkWidget      *widget,
89                                  GdkEventKey    *event,
90                                  gpointer        data);
91 static void ctree_selected      (GtkCTree       *ctree,
92                                  GtkCTreeNode   *node,
93                                  gint            column,
94                                  gpointer        data);
95 static void ctree_unselected    (GtkCTree       *ctree,
96                                  GtkCTreeNode   *node,
97                                  gint            column,
98                                  gpointer        data);
99 static void entry_activated     (GtkEditable    *editable);
100 static void search_clicked      (GtkWidget      *widget,
101                                  gpointer        data);
102
103 GSList *grouplist_dialog(Folder *folder)
104 {
105         GNode *node;
106         FolderItem *item;
107
108         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
109
110         if (!dialog)
111                 grouplist_dialog_create();
112
113         news_folder = folder;
114
115         gtk_widget_show(dialog);
116         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
117         manage_window_set_transient(GTK_WINDOW(dialog));
118         gtk_widget_grab_focus(ok_button);
119         gtk_widget_grab_focus(ctree);
120         GTK_EVENTS_FLUSH();
121
122         subscribed = NULL;
123         for (node = folder->node->children; node != NULL; node = node->next) {
124                 item = FOLDER_ITEM(node->data);
125                 subscribed = g_slist_append(subscribed, g_strdup(item->name));
126         }
127
128         grouplist_dialog_set_list(NULL, TRUE);
129
130         gtk_main();
131
132         manage_window_focus_out(dialog, NULL, NULL);
133         gtk_widget_hide(dialog);
134
135         if (!ack) {
136                 slist_free_strings(subscribed);
137                 g_slist_free(subscribed);
138                 subscribed = NULL;
139
140                 for (node = folder->node->children; node != NULL;
141                      node = node->next) {
142                         item = FOLDER_ITEM(node->data);
143                         subscribed = g_slist_append(subscribed,
144                                                     g_strdup(item->name));
145                 }
146         }
147
148         grouplist_clear();
149
150         return subscribed;
151 }
152
153 static void grouplist_dialog_create(void)
154 {
155         GtkWidget *vbox;
156         GtkWidget *hbox;
157         GtkWidget *msg_label;
158         GtkWidget *search_button;
159         GtkWidget *confirm_area;
160         GtkWidget *cancel_button;       
161         GtkWidget *refresh_button;      
162         GtkWidget *scrolledwin;
163         gchar *titles[3];
164         gint i;
165
166         dialog = gtk_dialog_new();
167         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
168         gtk_widget_set_usize(dialog,
169                              GROUPLIST_DIALOG_WIDTH, 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), _("Subscribe to newsgroup"));
174         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
175                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
176         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
177                            GTK_SIGNAL_FUNC(key_pressed), NULL);
178         gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
179                            GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
180         gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
181                            GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
182
183         gtk_widget_realize(dialog);
184
185         vbox = gtk_vbox_new(FALSE, 8);
186         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
187         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
188
189         hbox = gtk_hbox_new(FALSE, 0);
190         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
191
192         msg_label = gtk_label_new(_("Select newsgroups to subscribe."));
193         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
194
195         hbox = gtk_hbox_new(FALSE, 8);
196         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
197
198         msg_label = gtk_label_new(_("Find groups:"));
199         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
200
201         entry = gtk_entry_new();
202         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
203         gtk_signal_connect(GTK_OBJECT(entry), "activate",
204                            GTK_SIGNAL_FUNC(entry_activated), NULL);
205
206         search_button = gtk_button_new_with_label(_(" Search "));
207         gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
208
209         gtk_signal_connect(GTK_OBJECT(search_button), "clicked",
210                            GTK_SIGNAL_FUNC(search_clicked), NULL);
211
212         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
213         gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
214         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
215                                        GTK_POLICY_AUTOMATIC,
216                                        GTK_POLICY_AUTOMATIC);
217
218         titles[0] = _("Newsgroup name");
219         titles[1] = _("Messages");
220         titles[2] = _("Type");
221         ctree = gtk_ctree_new_with_titles(3, 0, titles);
222         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
223         gtk_clist_set_column_width
224                 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
225         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_MULTIPLE);
226         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
227         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
228                                      GTK_CTREE_EXPANDER_SQUARE);
229         for (i = 0; i < 3; i++)
230                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
231                                        GTK_CAN_FOCUS);
232         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
233                            GTK_SIGNAL_FUNC(ctree_selected), NULL);
234         gtk_signal_connect(GTK_OBJECT(ctree), "tree_unselect_row",
235                            GTK_SIGNAL_FUNC(ctree_unselected), NULL);
236
237         hbox = gtk_hbox_new(FALSE, 0);
238         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
239
240         status_label = gtk_label_new("");
241         gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
242
243         gtkut_button_set_create(&confirm_area,
244                                 &ok_button,      _("OK"),
245                                 &cancel_button,  _("Cancel"),
246                                 &refresh_button, _("Refresh"));
247         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
248                           confirm_area);
249         gtk_widget_grab_default(ok_button);
250
251         gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
252                            GTK_SIGNAL_FUNC(ok_clicked), NULL);
253         gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
254                            GTK_SIGNAL_FUNC(cancel_clicked), NULL);
255         gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
256                            GTK_SIGNAL_FUNC(refresh_clicked), NULL);
257
258         gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
259 }
260
261 static GHashTable *last_node_table;
262 static GHashTable *branch_node_table;
263
264 static void grouplist_hash_init(void)
265 {
266         last_node_table = g_hash_table_new(g_str_hash, g_str_equal);
267         branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
268 }
269
270 static void grouplist_hash_done(void)
271 {
272         hash_free_strings(branch_node_table);
273         hash_free_strings(last_node_table);
274
275         g_hash_table_destroy(branch_node_table);
276         g_hash_table_destroy(last_node_table);
277 }
278
279 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
280 {
281         return g_hash_table_lookup(branch_node_table, name);
282 }
283
284 static void grouplist_hash_set_branch_node(const gchar *name,
285                                            GtkCTreeNode *node)
286 {
287         g_hash_table_insert(branch_node_table, g_strdup(name), node);
288 }
289
290 static GtkCTreeNode *grouplist_hash_get_last_node(const gchar *name)
291 {
292         return g_hash_table_lookup(last_node_table, name);
293 }
294
295 static void grouplist_hash_set_last_node(const gchar *name,
296                                          GtkCTreeNode *node)
297 {
298         gchar *key;
299         gpointer value;
300
301         if (g_hash_table_lookup_extended(last_node_table, name,
302                                          (gpointer *)&key, &value)) {
303                 g_hash_table_remove(last_node_table, name);
304                 g_free(key);
305         }
306
307         g_hash_table_insert(last_node_table, g_strdup(name), node);
308 }
309
310 static gchar *grouplist_get_parent_name(const gchar *name)
311 {
312         gchar *p;
313
314         p = strrchr(name, '.');
315         if (!p)
316                 return g_strdup("");
317         return g_strndup(name, p - name);
318 }
319
320 static GtkCTreeNode *grouplist_create_parent(const gchar *name)
321 {
322         GtkCTreeNode *parent;
323         GtkCTreeNode *node;
324         gchar *cols[3];
325         gchar *parent_name;
326
327         if (*name == '\0') return NULL;
328         node = grouplist_hash_get_branch_node(name);
329         if (node != NULL) return node;
330
331         cols[0] = (gchar *)name;
332         cols[1] = cols[2] = "";
333
334         parent_name = grouplist_get_parent_name(name);
335         parent = grouplist_create_parent(parent_name);
336
337         node = grouplist_hash_get_last_node(parent_name);
338         node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
339                                      cols, 0, NULL, NULL, NULL, NULL,
340                                      FALSE, FALSE);
341         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
342         grouplist_hash_set_last_node(parent_name, node);
343         grouplist_hash_set_branch_node(name, node);
344
345         g_free(parent_name);
346
347         return node;
348 }
349
350 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo)
351 {
352         GtkCTreeNode *node;
353         GtkCTreeNode *parent;
354         gchar *name = (gchar *)ginfo->name;
355         gchar *parent_name;
356         gchar *count_str;
357         gchar *cols[3];
358         gint count;
359
360         count = ginfo->last - ginfo->first;
361         if (count < 0)
362                 count = 0;
363         count_str = itos(count);
364
365         cols[0] = ginfo->name;
366         cols[1] = count_str;
367         if (ginfo->type == 'y')
368                 cols[2] = "";
369         else if (ginfo->type == 'm')
370                 cols[2] = _("moderated");
371         else if (ginfo->type == 'n')
372                 cols[2] = _("readonly");
373         else
374                 cols[2] = _("unknown");
375
376         parent_name = grouplist_get_parent_name(name);
377         parent = grouplist_create_parent(parent_name);
378         node = grouplist_hash_get_branch_node(name);
379         if (node) {
380                 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
381                                         NULL, NULL, NULL, NULL, FALSE, FALSE);
382                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
383                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
384         } else {
385                 node = grouplist_hash_get_last_node(parent_name);
386                 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
387                                              cols, 0, NULL, NULL, NULL, NULL,
388                                              TRUE, FALSE);
389         }
390         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
391         if (node)
392                 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
393
394         grouplist_hash_set_last_node(parent_name, node);
395
396         g_free(parent_name);
397
398         return node;
399 }
400
401 static void grouplist_dialog_set_list(gchar *pattern, gboolean refresh)
402 {
403         GSList *cur;
404         GtkCTreeNode *node;
405
406         if (locked) return;
407         locked = TRUE;
408
409         if (!pattern || *pattern == '\0')
410                 pattern = "*";
411
412         if (refresh) {
413                 grouplist_clear();
414                 recv_set_ui_func(grouplist_recv_func, NULL);
415                 group_list = news_get_group_list(news_folder);
416                 group_list = g_slist_reverse(group_list);
417                 recv_set_ui_func(NULL, NULL);
418                 if (group_list == NULL) {
419                         alertpanel_error(_("Can't retrieve newsgroup list."));
420                         locked = FALSE;
421                         return;
422                 }
423         } else {
424                 gtk_signal_handler_block_by_func
425                         (GTK_OBJECT(ctree), GTK_SIGNAL_FUNC(ctree_unselected),
426                          NULL);
427                 gtk_clist_clear(GTK_CLIST(ctree));
428                 gtk_signal_handler_unblock_by_func
429                         (GTK_OBJECT(ctree), GTK_SIGNAL_FUNC(ctree_unselected),
430                          NULL);
431         }
432         gtk_entry_set_text(GTK_ENTRY(entry), pattern);
433
434         grouplist_hash_init();
435
436         gtk_clist_freeze(GTK_CLIST(ctree));
437
438         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
439                                          GTK_SIGNAL_FUNC(ctree_selected),
440                                          NULL);
441
442         for (cur = group_list; cur != NULL ; cur = cur->next) {
443                 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
444
445                 if (fnmatch(pattern, ginfo->name, 0) == 0) {
446                         node = grouplist_create_branch(ginfo);
447                         if (g_slist_find_custom(subscribed, ginfo->name,
448                                                 (GCompareFunc)g_strcasecmp)
449                             != NULL)
450                                 gtk_ctree_select(GTK_CTREE(ctree), node);
451                 }
452         }
453
454         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
455                                            GTK_SIGNAL_FUNC(ctree_selected),
456                                            NULL);
457
458         gtk_clist_thaw(GTK_CLIST(ctree));
459
460         grouplist_hash_done();
461
462         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
463
464         locked = FALSE;
465 }
466
467 static void grouplist_search(void)
468 {
469         gchar *str;
470
471         if (locked) return;
472
473         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
474         grouplist_dialog_set_list(str, FALSE);
475         g_free(str);
476 }
477
478 static void grouplist_clear(void)
479 {
480         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
481                                          GTK_SIGNAL_FUNC(ctree_unselected),
482                                          NULL);
483         gtk_clist_clear(GTK_CLIST(ctree));
484         gtk_entry_set_text(GTK_ENTRY(entry), "");
485         news_group_list_free(group_list);
486         group_list = NULL;
487         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
488                                            GTK_SIGNAL_FUNC(ctree_unselected),
489                                            NULL);
490 }
491
492 static void grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
493                                 gpointer data)
494 {
495         gchar buf[BUFFSIZE];
496
497         g_snprintf(buf, sizeof(buf),
498                    _("%d newsgroups received (%s read)"),
499                    count, to_human_readable(read_bytes));
500         gtk_label_set_text(GTK_LABEL(status_label), buf);
501         GTK_EVENTS_FLUSH();
502 }
503
504 static void ok_clicked(GtkWidget *widget, gpointer data)
505 {
506         ack = TRUE;
507         if (gtk_main_level() > 1)
508                 gtk_main_quit();
509 }
510
511 static void cancel_clicked(GtkWidget *widget, gpointer data)
512 {
513         ack = FALSE;
514         if (gtk_main_level() > 1)
515                 gtk_main_quit();
516 }
517
518 static void refresh_clicked(GtkWidget *widget, gpointer data)
519
520         gchar *str;
521
522         if (locked) return;
523
524         news_remove_group_list_cache(news_folder);
525
526         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
527         grouplist_dialog_set_list(str, TRUE);
528         g_free(str);
529 }
530
531 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
532 {
533         if (event && event->keyval == GDK_Escape)
534                 cancel_clicked(NULL, NULL);
535 }
536
537 static void ctree_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
538                            gpointer data)
539 {
540         NewsGroupInfo *ginfo;
541
542         ginfo = gtk_ctree_node_get_row_data(ctree, node);
543         if (!ginfo) return;
544
545         subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
546 }
547
548 static void ctree_unselected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
549                              gpointer data)
550 {
551         NewsGroupInfo *ginfo;
552         GSList *list;
553
554         ginfo = gtk_ctree_node_get_row_data(ctree, node);
555         if (!ginfo) return;
556
557         list = g_slist_find_custom(subscribed, ginfo->name,
558                                    (GCompareFunc)g_strcasecmp);
559         if (list) {
560                 g_free(list->data);
561                 subscribed = g_slist_remove(subscribed, list->data);
562         }
563 }
564
565 static void entry_activated(GtkEditable *editable)
566 {
567         grouplist_search();
568 }
569
570 static void search_clicked(GtkWidget *widget, gpointer data)
571 {
572         grouplist_search();
573 }