* src/prefs_folder_item.c
[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 void key_pressed         (GtkWidget      *widget,
92                                  GdkEventKey    *event,
93                                  gpointer        data);
94 static void ctree_selected      (GtkCTree       *ctree,
95                                  GtkCTreeNode   *node,
96                                  gint            column,
97                                  gpointer        data);
98 static void ctree_unselected    (GtkCTree       *ctree,
99                                  GtkCTreeNode   *node,
100                                  gint            column,
101                                  gpointer        data);
102 static void entry_activated     (GtkEditable    *editable);
103 static void search_clicked      (GtkWidget      *widget,
104                                  gpointer        data);
105
106 GSList *grouplist_dialog(Folder *folder)
107 {
108         GNode *node;
109         FolderItem *item;
110
111         if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
112
113         if (!dialog)
114                 grouplist_dialog_create();
115
116         news_folder = folder;
117
118         gtk_widget_show(dialog);
119         gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
120         manage_window_set_transient(GTK_WINDOW(dialog));
121         gtk_widget_grab_focus(ok_button);
122         gtk_widget_grab_focus(ctree);
123         GTK_EVENTS_FLUSH();
124
125         subscribed = NULL;
126         for (node = folder->node->children; node != NULL; node = node->next) {
127                 item = FOLDER_ITEM(node->data);
128                 subscribed = g_slist_append(subscribed, g_strdup(item->path));
129         }
130
131         grouplist_dialog_set_list(NULL, TRUE);
132
133         if (ack) gtk_main();
134
135         manage_window_focus_out(dialog, NULL, NULL);
136         gtk_widget_hide(dialog);
137
138         if (!ack) {
139                 slist_free_strings(subscribed);
140                 g_slist_free(subscribed);
141                 subscribed = NULL;
142
143                 for (node = folder->node->children; node != NULL;
144                      node = node->next) {
145                         item = FOLDER_ITEM(node->data);
146                         subscribed = g_slist_append(subscribed,
147                                                     g_strdup(item->path));
148                 }
149         }
150
151         grouplist_clear();
152
153         return subscribed;
154 }
155
156 static void grouplist_dialog_create(void)
157 {
158         GtkWidget *vbox;
159         GtkWidget *hbox;
160         GtkWidget *msg_label;
161         GtkWidget *search_button;
162         GtkWidget *confirm_area;
163         GtkWidget *cancel_button;       
164         GtkWidget *refresh_button;      
165         GtkWidget *scrolledwin;
166         gchar *titles[3];
167         gint i;
168
169         dialog = gtk_dialog_new();
170         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
171         gtk_widget_set_usize(dialog,
172                              GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
173         gtk_container_set_border_width
174                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
175         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
176         gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
177         gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
178                            GTK_SIGNAL_FUNC(window_deleted), NULL);
179         gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
180                            GTK_SIGNAL_FUNC(key_pressed), NULL);
181         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
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 for subscription:"));
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 *branch_node_table;
262
263 static void grouplist_hash_init(void)
264 {
265         branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
266 }
267
268 static void grouplist_hash_done(void)
269 {
270         hash_free_strings(branch_node_table);
271         g_hash_table_destroy(branch_node_table);
272 }
273
274 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
275 {
276         return g_hash_table_lookup(branch_node_table, name);
277 }
278
279 static void grouplist_hash_set_branch_node(const gchar *name,
280                                            GtkCTreeNode *node)
281 {
282         g_hash_table_insert(branch_node_table, g_strdup(name), node);
283 }
284
285 static gchar *grouplist_get_parent_name(const gchar *name)
286 {
287         gchar *p;
288
289         p = strrchr(name, '.');
290         if (!p)
291                 return g_strdup("");
292         return g_strndup(name, p - name);
293 }
294
295 static GtkCTreeNode *grouplist_create_parent(const gchar *name,
296                                              const gchar *pattern)
297 {
298         GtkCTreeNode *parent;
299         GtkCTreeNode *node;
300         gchar *cols[3];
301         gchar *parent_name;
302
303         if (*name == '\0') return NULL;
304         node = grouplist_hash_get_branch_node(name);
305         if (node != NULL) return node;
306
307         cols[0] = (gchar *)name;
308         cols[1] = cols[2] = "";
309
310         parent_name = grouplist_get_parent_name(name);
311         parent = grouplist_create_parent(parent_name, pattern);
312
313         node = parent ? GTK_CTREE_ROW(parent)->children
314                 : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
315         node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
316                                      cols, 0, NULL, NULL, NULL, NULL,
317                                      FALSE, FALSE);
318         if (parent && fnmatch(pattern, parent_name, 0) != 0)
319                 gtk_ctree_expand(GTK_CTREE(ctree), parent);
320         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
321
322         grouplist_hash_set_branch_node(name, node);
323
324         g_free(parent_name);
325
326         return node;
327 }
328
329 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
330                                              const gchar *pattern)
331 {
332         GtkCTreeNode *node;
333         GtkCTreeNode *parent;
334         gchar *name = (gchar *)ginfo->name;
335         gchar *parent_name;
336         gchar *count_str;
337         gchar *cols[3];
338         gint count;
339
340         count = ginfo->last - ginfo->first;
341         if (count < 0)
342                 count = 0;
343         count_str = itos(count);
344
345         cols[0] = ginfo->name;
346         cols[1] = count_str;
347         if (ginfo->type == 'y')
348                 cols[2] = "";
349         else if (ginfo->type == 'm')
350                 cols[2] = _("moderated");
351         else if (ginfo->type == 'n')
352                 cols[2] = _("readonly");
353         else
354                 cols[2] = _("unknown");
355
356         parent_name = grouplist_get_parent_name(name);
357         parent = grouplist_create_parent(parent_name, pattern);
358         node = grouplist_hash_get_branch_node(name);
359         if (node) {
360                 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
361                                         NULL, NULL, NULL, NULL, FALSE, FALSE);
362                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
363                 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
364         } else {
365                 node = parent ? GTK_CTREE_ROW(parent)->children
366                         : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
367                 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
368                                              cols, 0, NULL, NULL, NULL, NULL,
369                                              TRUE, FALSE);
370                 if (parent && fnmatch(pattern, parent_name, 0) != 0)
371                         gtk_ctree_expand(GTK_CTREE(ctree), parent);
372         }
373         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
374         if (node)
375                 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
376
377         g_free(parent_name);
378
379         return node;
380 }
381
382 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
383 {
384         GSList *cur;
385         GtkCTreeNode *node;
386
387         if (locked) return;
388         locked = TRUE;
389
390         if (!pattern || *pattern == '\0')
391                 pattern = "*";
392
393         if (refresh) {
394                 ack = TRUE;
395                 grouplist_clear();
396                 recv_set_ui_func(grouplist_recv_func, NULL);
397                 group_list = news_get_group_list(news_folder);
398                 group_list = g_slist_reverse(group_list);
399                 recv_set_ui_func(NULL, NULL);
400                 if (group_list == NULL && ack == TRUE) {
401                         alertpanel_error(_("Can't retrieve newsgroup list."));
402                         locked = FALSE;
403                         return;
404                 }
405         } else {
406                 gtk_signal_handler_block_by_func
407                         (GTK_OBJECT(ctree), GTK_SIGNAL_FUNC(ctree_unselected),
408                          NULL);
409                 gtk_clist_clear(GTK_CLIST(ctree));
410                 gtk_signal_handler_unblock_by_func
411                         (GTK_OBJECT(ctree), GTK_SIGNAL_FUNC(ctree_unselected),
412                          NULL);
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         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
421                                          GTK_SIGNAL_FUNC(ctree_selected),
422                                          NULL);
423
424         for (cur = group_list; cur != NULL ; cur = cur->next) {
425                 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
426
427                 if (fnmatch(pattern, ginfo->name, 0) == 0) {
428                         node = grouplist_create_branch(ginfo, pattern);
429                         if (g_slist_find_custom(subscribed, ginfo->name,
430                                                 (GCompareFunc)g_strcasecmp)
431                             != NULL)
432                                 gtk_ctree_select(GTK_CTREE(ctree), node);
433                 }
434         }
435
436         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
437                                            GTK_SIGNAL_FUNC(ctree_selected),
438                                            NULL);
439
440         gtk_clist_thaw(GTK_CLIST(ctree));
441
442         grouplist_hash_done();
443
444         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
445
446         locked = FALSE;
447 }
448
449 static void grouplist_search(void)
450 {
451         gchar *str;
452
453         if (locked) return;
454
455         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
456         grouplist_dialog_set_list(str, FALSE);
457         g_free(str);
458 }
459
460 static void grouplist_clear(void)
461 {
462         gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
463                                          GTK_SIGNAL_FUNC(ctree_unselected),
464                                          NULL);
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         gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
470                                            GTK_SIGNAL_FUNC(ctree_unselected),
471                                            NULL);
472 }
473
474 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
475                                     gpointer data)
476 {
477         gchar buf[BUFFSIZE];
478
479         g_snprintf(buf, sizeof(buf),
480                    _("%d newsgroups received (%s read)"),
481                    count, to_human_readable(read_bytes));
482         gtk_label_set_text(GTK_LABEL(status_label), buf);
483         GTK_EVENTS_FLUSH();
484         if (ack == FALSE)
485                 return FALSE;
486         else
487                 return TRUE;
488 }
489
490 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
491 {
492         ack = FALSE;
493         if (gtk_main_level() > 1)
494                 gtk_main_quit();
495
496         return TRUE;
497 }
498
499 static void ok_clicked(GtkWidget *widget, gpointer data)
500 {
501         ack = TRUE;
502         if (gtk_main_level() > 1)
503                 gtk_main_quit();
504 }
505
506 static void cancel_clicked(GtkWidget *widget, gpointer data)
507 {
508         ack = FALSE;
509         if (gtk_main_level() > 1)
510                 gtk_main_quit();
511 }
512
513 static void refresh_clicked(GtkWidget *widget, gpointer data)
514
515         gchar *str;
516
517         if (locked) return;
518
519         news_remove_group_list_cache(news_folder);
520
521         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
522         grouplist_dialog_set_list(str, TRUE);
523         g_free(str);
524 }
525
526 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
527 {
528         if (event && event->keyval == GDK_Escape)
529                 cancel_clicked(NULL, NULL);
530 }
531
532 static void ctree_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
533                            gpointer data)
534 {
535         NewsGroupInfo *ginfo;
536
537         ginfo = gtk_ctree_node_get_row_data(ctree, node);
538         if (!ginfo) return;
539
540         subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
541 }
542
543 static void ctree_unselected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
544                              gpointer data)
545 {
546         NewsGroupInfo *ginfo;
547         GSList *list;
548
549         ginfo = gtk_ctree_node_get_row_data(ctree, node);
550         if (!ginfo) return;
551
552         list = g_slist_find_custom(subscribed, ginfo->name,
553                                    (GCompareFunc)g_strcasecmp);
554         if (list) {
555                 g_free(list->data);
556                 subscribed = g_slist_remove(subscribed, list->data);
557         }
558 }
559
560 static void entry_activated(GtkEditable *editable)
561 {
562         grouplist_search();
563 }
564
565 static void search_clicked(GtkWidget *widget, gpointer data)
566 {
567         grouplist_search();
568 }