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