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