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