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