inital gtk2 patch
[claws.git] / src / grouplistdialog.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 void ok_clicked          (GtkWidget      *widget,
83                                  gpointer        data);
84 static void cancel_clicked      (GtkWidget      *widget,
85                                  gpointer        data);
86 static void refresh_clicked     (GtkWidget      *widget,
87                                  gpointer        data);
88 static gboolean key_pressed     (GtkWidget      *widget,
89                                  GdkEventKey    *event,
90                                  gpointer        data);
91 static void ctree_selected      (GtkCTree       *ctree,
92                                  GtkCTreeNode   *node,
93                                  gint            column,
94                                  gpointer        data);
95 static void ctree_unselected    (GtkCTree       *ctree,
96                                  GtkCTreeNode   *node,
97                                  gint            column,
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_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
168         gtk_widget_set_size_request(dialog,
169                                     GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
170         gtk_container_set_border_width
171                 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
172         gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
173         gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
174         g_signal_connect(G_OBJECT(dialog), "delete_event",
175                          G_CALLBACK(cancel_clicked), NULL);
176         g_signal_connect(G_OBJECT(dialog), "key_press_event",
177                          G_CALLBACK(key_pressed), NULL);
178         MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
179
180         gtk_widget_realize(dialog);
181
182         vbox = gtk_vbox_new(FALSE, 8);
183         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
184         gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
185
186         hbox = gtk_hbox_new(FALSE, 0);
187         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
188
189         msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
190         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
191
192         hbox = gtk_hbox_new(FALSE, 8);
193         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
194
195         msg_label = gtk_label_new(_("Find groups:"));
196         gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
197
198         entry = gtk_entry_new();
199         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
200         g_signal_connect(G_OBJECT(entry), "activate",
201                          G_CALLBACK(entry_activated), NULL);
202
203         search_button = gtk_button_new_with_label(_(" Search "));
204         gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
205
206         g_signal_connect(G_OBJECT(search_button), "clicked",
207                          G_CALLBACK(search_clicked), NULL);
208
209         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
210         gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
211         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
212                                        GTK_POLICY_AUTOMATIC,
213                                        GTK_POLICY_AUTOMATIC);
214
215         titles[0] = _("Newsgroup name");
216         titles[1] = _("Messages");
217         titles[2] = _("Type");
218         ctree = gtk_ctree_new_with_titles(3, 0, titles);
219         gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
220         gtk_clist_set_column_width
221                 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
222         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_MULTIPLE);
223         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
224         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
225                                      GTK_CTREE_EXPANDER_SQUARE);
226         for (i = 0; i < 3; i++)
227                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
228                                        GTK_CAN_FOCUS);
229         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
230                          G_CALLBACK(ctree_selected), NULL);
231         g_signal_connect(G_OBJECT(ctree), "tree_unselect_row",
232                          G_CALLBACK(ctree_unselected), 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_button_set_create(&confirm_area,
241                                 &ok_button,      _("OK"),
242                                 &cancel_button,  _("Cancel"),
243                                 &refresh_button, _("Refresh"));
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_ctree_insert_node(GTK_CTREE(ctree), parent, node,
313                                      cols, 0, NULL, NULL, NULL, NULL,
314                                      FALSE, FALSE);
315         if (parent && fnmatch(pattern, parent_name, 0) != 0)
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_ctree_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_ctree_insert_node(GTK_CTREE(ctree), parent, node,
365                                              cols, 0, NULL, NULL, NULL, NULL,
366                                              TRUE, FALSE);
367                 if (parent && fnmatch(pattern, parent_name, 0) != 0)
368                         gtk_ctree_expand(GTK_CTREE(ctree), parent);
369         }
370         gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
371         if (node)
372                 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
373
374         g_free(parent_name);
375
376         return node;
377 }
378
379 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
380 {
381         GSList *cur;
382         GtkCTreeNode *node;
383
384         if (locked) return;
385         locked = TRUE;
386
387         if (!pattern || *pattern == '\0')
388                 pattern = "*";
389
390         if (refresh) {
391                 ack = TRUE;
392                 grouplist_clear();
393                 recv_set_ui_func(grouplist_recv_func, NULL);
394                 group_list = news_get_group_list(news_folder);
395                 group_list = g_slist_reverse(group_list);
396                 recv_set_ui_func(NULL, NULL);
397                 if (group_list == NULL && ack == TRUE) {
398                         alertpanel_error(_("Can't retrieve newsgroup list."));
399                         locked = FALSE;
400                         return;
401                 }
402         } else {
403                 g_signal_handlers_block_by_func
404                         (G_OBJECT(ctree), G_CALLBACK(ctree_unselected),
405                          NULL);
406                 gtk_clist_clear(GTK_CLIST(ctree));
407                 g_signal_handlers_unblock_by_func
408                         (G_OBJECT(ctree), G_CALLBACK(ctree_unselected),
409                          NULL);
410         }
411         gtk_entry_set_text(GTK_ENTRY(entry), pattern);
412
413         grouplist_hash_init();
414
415         gtk_clist_freeze(GTK_CLIST(ctree));
416
417         g_signal_handlers_block_by_func(G_OBJECT(ctree),
418                                                                         G_CALLBACK(ctree_selected),
419                                                                         NULL);
420
421         for (cur = group_list; cur != NULL ; cur = cur->next) {
422                 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
423
424                 if (fnmatch(pattern, ginfo->name, 0) == 0) {
425                         node = grouplist_create_branch(ginfo, pattern);
426                         if (g_slist_find_custom(subscribed, ginfo->name,
427                                                 (GCompareFunc)g_strcasecmp)
428                             != NULL)
429                                 gtk_ctree_select(GTK_CTREE(ctree), node);
430                 }
431         }
432
433         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
434                                           G_CALLBACK(ctree_selected),
435                                           NULL);
436
437         gtk_clist_thaw(GTK_CLIST(ctree));
438
439         grouplist_hash_done();
440
441         gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
442
443         locked = FALSE;
444 }
445
446 static void grouplist_search(void)
447 {
448         gchar *str;
449
450         if (locked) return;
451
452         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
453         grouplist_dialog_set_list(str, FALSE);
454         g_free(str);
455 }
456
457 static void grouplist_clear(void)
458 {
459         g_signal_handlers_block_by_func(G_OBJECT(ctree),
460                                         G_CALLBACK(ctree_unselected),
461                                         NULL);
462         gtk_clist_clear(GTK_CLIST(ctree));
463         gtk_entry_set_text(GTK_ENTRY(entry), "");
464         news_group_list_free(group_list);
465         group_list = NULL;
466         g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
467                                           G_CALLBACK(ctree_unselected),
468                                           NULL);
469 }
470
471 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
472                                     gpointer data)
473 {
474         gchar buf[BUFFSIZE];
475
476         g_snprintf(buf, sizeof(buf),
477                    _("%d newsgroups received (%s read)"),
478                    count, to_human_readable(read_bytes));
479         gtk_label_set_text(GTK_LABEL(status_label), buf);
480         GTK_EVENTS_FLUSH();
481         if (ack == FALSE)
482                 return FALSE;
483         else
484                 return TRUE;
485 }
486
487 static void ok_clicked(GtkWidget *widget, gpointer data)
488 {
489         ack = TRUE;
490         if (gtk_main_level() > 1)
491                 gtk_main_quit();
492 }
493
494 static void cancel_clicked(GtkWidget *widget, gpointer data)
495 {
496         ack = FALSE;
497         if (gtk_main_level() > 1)
498                 gtk_main_quit();
499 }
500
501 static void refresh_clicked(GtkWidget *widget, gpointer data)
502
503         gchar *str;
504
505         if (locked) return;
506
507         news_remove_group_list_cache(news_folder);
508
509         str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
510         grouplist_dialog_set_list(str, TRUE);
511         g_free(str);
512 }
513
514 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
515 {
516         if (event && event->keyval == GDK_Escape)
517                 cancel_clicked(NULL, NULL);
518         return FALSE;
519 }
520
521 static void ctree_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
522                            gpointer data)
523 {
524         NewsGroupInfo *ginfo;
525
526         ginfo = gtk_ctree_node_get_row_data(ctree, node);
527         if (!ginfo) return;
528
529         subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
530 }
531
532 static void ctree_unselected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
533                              gpointer data)
534 {
535         NewsGroupInfo *ginfo;
536         GSList *list;
537
538         ginfo = gtk_ctree_node_get_row_data(ctree, node);
539         if (!ginfo) return;
540
541         list = g_slist_find_custom(subscribed, ginfo->name,
542                                    (GCompareFunc)g_strcasecmp);
543         if (list) {
544                 g_free(list->data);
545                 subscribed = g_slist_remove(subscribed, list->data);
546         }
547 }
548
549 static void entry_activated(GtkEditable *editable)
550 {
551         grouplist_search();
552 }
553
554 static void search_clicked(GtkWidget *widget, gpointer data)
555 {
556         grouplist_search();
557 }