2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
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>
45 #include "grouplistdialog.h"
46 #include "manage_window.h"
51 #include "alertpanel.h"
55 #define GROUPLIST_DIALOG_WIDTH 480
56 #define GROUPLIST_DIALOG_HEIGHT 400
57 #define GROUPLIST_COL_NAME_WIDTH 250
60 static gboolean locked;
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;
70 static GSList *subscribed;
72 static void grouplist_dialog_create (void);
73 static void grouplist_dialog_set_list (gchar *pattern);
74 static void grouplist_clear (void);
75 static void grouplist_recv_func (SockInfo *sock,
80 static void ok_clicked (GtkWidget *widget,
82 static void cancel_clicked (GtkWidget *widget,
84 static void refresh_clicked (GtkWidget *widget,
86 static void key_pressed (GtkWidget *widget,
89 static void ctree_selected (GtkCTree *ctree,
93 static void ctree_unselected (GtkCTree *ctree,
97 static void entry_activated (GtkEditable *editable);
99 GSList *grouplist_dialog(Folder *folder)
104 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
107 grouplist_dialog_create();
109 news_folder = folder;
111 gtk_widget_show(dialog);
112 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
113 manage_window_set_transient(GTK_WINDOW(dialog));
114 gtk_widget_grab_focus(ok_button);
115 gtk_widget_grab_focus(ctree);
119 for (node = folder->node->children; node != NULL; node = node->next) {
120 item = FOLDER_ITEM(node->data);
121 subscribed = g_slist_append(subscribed, g_strdup(item->name));
124 grouplist_dialog_set_list(NULL);
128 manage_window_focus_out(dialog, NULL, NULL);
129 gtk_widget_hide(dialog);
132 slist_free_strings(subscribed);
133 g_slist_free(subscribed);
136 for (node = folder->node->children; node != NULL;
138 item = FOLDER_ITEM(node->data);
139 subscribed = g_slist_append(subscribed,
140 g_strdup(item->name));
149 static void grouplist_dialog_create(void)
153 GtkWidget *msg_label;
154 GtkWidget *confirm_area;
155 GtkWidget *cancel_button;
156 GtkWidget *refresh_button;
157 GtkWidget *scrolledwin;
161 dialog = gtk_dialog_new();
162 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
163 gtk_widget_set_usize(dialog,
164 GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
165 gtk_container_set_border_width
166 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
167 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
168 gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe to newsgroup"));
169 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
170 GTK_SIGNAL_FUNC(cancel_clicked), NULL);
171 gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
172 GTK_SIGNAL_FUNC(key_pressed), NULL);
173 gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
174 GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
175 gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
176 GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
178 gtk_widget_realize(dialog);
180 vbox = gtk_vbox_new(FALSE, 8);
181 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
182 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
184 hbox = gtk_hbox_new(FALSE, 0);
185 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
187 msg_label = gtk_label_new(_("Input subscribing newsgroup:"));
188 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
190 entry = gtk_entry_new();
191 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
192 gtk_signal_connect(GTK_OBJECT(entry), "activate",
193 GTK_SIGNAL_FUNC(entry_activated), NULL);
195 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
196 gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
197 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
198 GTK_POLICY_AUTOMATIC,
199 GTK_POLICY_AUTOMATIC);
201 titles[0] = _("Newsgroup name");
202 titles[1] = _("Messages");
203 titles[2] = _("Type");
204 ctree = gtk_ctree_new_with_titles(3, 0, titles);
205 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
206 gtk_clist_set_column_width
207 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
208 gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_MULTIPLE);
209 gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
210 gtk_ctree_set_expander_style(GTK_CTREE(ctree),
211 GTK_CTREE_EXPANDER_SQUARE);
212 for (i = 0; i < 3; i++)
213 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
215 gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
216 GTK_SIGNAL_FUNC(ctree_selected), NULL);
217 gtk_signal_connect(GTK_OBJECT(ctree), "tree_unselect_row",
218 GTK_SIGNAL_FUNC(ctree_unselected), NULL);
220 hbox = gtk_hbox_new(FALSE, 0);
221 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
223 status_label = gtk_label_new("");
224 gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
226 gtkut_button_set_create(&confirm_area,
228 &cancel_button, _("Cancel"),
229 &refresh_button, _("Refresh"));
230 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
232 gtk_widget_grab_default(ok_button);
234 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
235 GTK_SIGNAL_FUNC(ok_clicked), NULL);
236 gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
237 GTK_SIGNAL_FUNC(cancel_clicked), NULL);
238 gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
239 GTK_SIGNAL_FUNC(refresh_clicked), NULL);
241 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
244 static GHashTable *last_node_table;
245 static GHashTable *branch_node_table;
247 static void grouplist_hash_init(void)
249 last_node_table = g_hash_table_new(g_str_hash, g_str_equal);
250 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
253 static void grouplist_hash_done(void)
255 hash_free_strings(branch_node_table);
256 hash_free_strings(last_node_table);
258 g_hash_table_destroy(branch_node_table);
259 g_hash_table_destroy(last_node_table);
262 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
264 return g_hash_table_lookup(branch_node_table, name);
267 static void grouplist_hash_set_branch_node(const gchar *name,
270 g_hash_table_insert(branch_node_table, g_strdup(name), node);
273 static GtkCTreeNode *grouplist_hash_get_last_node(const gchar *name)
275 return g_hash_table_lookup(last_node_table, name);
278 static void grouplist_hash_set_last_node(const gchar *name,
284 if (g_hash_table_lookup_extended(last_node_table, name,
285 (gpointer *)&key, &value)) {
286 g_hash_table_remove(last_node_table, name);
290 g_hash_table_insert(last_node_table, g_strdup(name), node);
293 static gchar *grouplist_get_parent_name(const gchar *name)
297 p = strrchr(name, '.');
300 return g_strndup(name, p - name);
303 static GtkCTreeNode *grouplist_create_parent(const gchar *name)
305 GtkCTreeNode *parent;
310 if (*name == '\0') return NULL;
311 node = grouplist_hash_get_branch_node(name);
312 if (node != NULL) return node;
314 cols[0] = (gchar *)name;
315 cols[1] = cols[2] = "";
317 parent_name = grouplist_get_parent_name(name);
318 parent = grouplist_create_parent(parent_name);
320 node = grouplist_hash_get_last_node(parent_name);
321 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
322 cols, 0, NULL, NULL, NULL, NULL,
324 gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
325 grouplist_hash_set_last_node(parent_name, node);
326 grouplist_hash_set_branch_node(name, node);
333 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo)
336 GtkCTreeNode *parent;
337 gchar *name = (gchar *)ginfo->name;
343 count = ginfo->last - ginfo->first;
346 count_str = itos(count);
348 cols[0] = ginfo->name;
350 if (ginfo->type == 'y')
352 else if (ginfo->type == 'm')
353 cols[2] = _("moderated");
354 else if (ginfo->type == 'n')
355 cols[2] = _("readonly");
357 cols[2] = _("unknown");
359 parent_name = grouplist_get_parent_name(name);
360 parent = grouplist_create_parent(parent_name);
361 node = grouplist_hash_get_branch_node(name);
363 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
364 NULL, NULL, NULL, NULL, FALSE, FALSE);
365 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
366 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
368 node = grouplist_hash_get_last_node(parent_name);
369 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
370 cols, 0, NULL, NULL, NULL, NULL,
373 gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
375 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
377 grouplist_hash_set_last_node(parent_name, node);
384 static void grouplist_dialog_set_list(gchar *pattern)
392 if (!pattern || *pattern == '\0')
397 recv_set_ui_func(grouplist_recv_func, NULL);
398 group_list = news_get_group_list(news_folder);
399 group_list = g_slist_reverse(group_list);
400 recv_set_ui_func(NULL, NULL);
401 if (group_list == NULL) {
402 alertpanel_error(_("Can't retrieve newsgroup list."));
407 grouplist_hash_init();
409 gtk_clist_freeze(GTK_CLIST(ctree));
411 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
412 GTK_SIGNAL_FUNC(ctree_selected),
415 for (cur = group_list; cur != NULL ; cur = cur->next) {
416 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
418 if (fnmatch(pattern, ginfo->name, 0) == 0) {
419 node = grouplist_create_branch(ginfo);
420 if (g_slist_find_custom(subscribed, ginfo->name,
421 (GCompareFunc)g_strcasecmp)
423 gtk_ctree_select(GTK_CTREE(ctree), node);
427 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
428 GTK_SIGNAL_FUNC(ctree_selected),
431 gtk_clist_thaw(GTK_CLIST(ctree));
433 grouplist_hash_done();
435 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
440 static void grouplist_clear(void)
442 gtk_signal_handler_block_by_func(GTK_OBJECT(ctree),
443 GTK_SIGNAL_FUNC(ctree_unselected),
445 gtk_clist_clear(GTK_CLIST(ctree));
446 gtk_entry_set_text(GTK_ENTRY(entry), "");
447 news_group_list_free(group_list);
449 gtk_signal_handler_unblock_by_func(GTK_OBJECT(ctree),
450 GTK_SIGNAL_FUNC(ctree_unselected),
454 static void grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
459 g_snprintf(buf, sizeof(buf),
460 _("%d newsgroups received (%s read)"),
461 count, to_human_readable(read_bytes));
462 gtk_label_set_text(GTK_LABEL(status_label), buf);
466 static void ok_clicked(GtkWidget *widget, gpointer data)
470 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
472 if (strchr(str, '*') != NULL)
473 grouplist_dialog_set_list(str);
476 if (gtk_main_level() > 1)
483 static void cancel_clicked(GtkWidget *widget, gpointer data)
486 if (gtk_main_level() > 1)
490 static void refresh_clicked(GtkWidget *widget, gpointer data)
496 news_remove_group_list_cache(news_folder);
498 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
499 grouplist_dialog_set_list(str);
503 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
505 if (event && event->keyval == GDK_Escape)
506 cancel_clicked(NULL, NULL);
509 static void ctree_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
512 NewsGroupInfo *ginfo;
514 ginfo = gtk_ctree_node_get_row_data(ctree, node);
517 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
520 static void ctree_unselected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
523 NewsGroupInfo *ginfo;
526 ginfo = gtk_ctree_node_get_row_data(ctree, node);
529 list = g_slist_find_custom(subscribed, ginfo->name,
530 (GCompareFunc)g_strcasecmp);
533 subscribed = g_slist_remove(subscribed, list->data);
537 static void entry_activated(GtkEditable *editable)
540 gboolean update_list;
542 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
546 if (strchr(str, '*') != NULL)
550 grouplist_dialog_set_list(str);
555 ok_clicked(NULL, NULL);