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/gtkclist.h>
38 #include <gtk/gtkctree.h>
39 #include <gtk/gtkscrolledwindow.h>
40 #include <gtk/gtkbutton.h>
41 #include <gtk/gtkhbbox.h>
46 #include "grouplistdialog.h"
47 #include "manage_window.h"
52 #include "alertpanel.h"
56 #define GROUPLIST_DIALOG_WIDTH 500
57 #define GROUPLIST_NAMES 250
58 #define GROUPLIST_DIALOG_HEIGHT 400
60 static GList * subscribed = NULL;
61 gboolean dont_unsubscribed = FALSE;
64 static gboolean locked;
66 static GtkWidget *dialog;
67 static GtkWidget *entry;
68 // static GtkWidget *clist;
69 static GtkWidget *groups_tree;
70 static GtkWidget *status_label;
71 static GtkWidget *ok_button;
72 static Folder *news_folder;
74 static void grouplist_dialog_create (void);
75 static void grouplist_dialog_set_list (gchar * pattern);
76 static void grouplist_clear (void);
77 static void grouplist_recv_func (SockInfo *sock,
82 static void ok_clicked (GtkWidget *widget,
84 static void cancel_clicked (GtkWidget *widget,
86 static void refresh_clicked (GtkWidget *widget,
88 static void key_pressed (GtkWidget *widget,
91 static void groups_tree_selected (GtkCTree *groups_tree,
94 GdkEventButton *event,
96 static void groups_tree_unselected (GtkCTree *groups_tree,
99 GdkEventButton *event,
101 static void entry_activated (GtkEditable *editable);
103 GList *grouplist_dialog(Folder *folder, GList * cur_subscriptions)
108 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
111 grouplist_dialog_create();
113 news_folder = folder;
115 gtk_widget_show(dialog);
116 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
117 manage_window_set_transient(GTK_WINDOW(dialog));
122 for(l = cur_subscriptions ; l != NULL ; l = l->next)
123 subscribed = g_list_append(subscribed, g_strdup((gchar *) l->data));
125 grouplist_dialog_set_list(NULL);
129 manage_window_focus_out(dialog, NULL, NULL);
130 gtk_widget_hide(dialog);
133 list_free_strings(subscribed);
134 g_list_free(subscribed);
138 for(l = cur_subscriptions ; l != NULL ; l = l->next)
139 subscribed = g_list_append(subscribed, g_strdup((gchar *) l->data));
144 debug_print("return string = %s\n", str ? str : "(none)");
148 static void grouplist_clear(void)
150 dont_unsubscribed = TRUE;
151 gtk_clist_clear(GTK_CLIST(groups_tree));
152 gtk_entry_set_text(GTK_ENTRY(entry), "");
153 dont_unsubscribed = FALSE;
156 static void grouplist_dialog_create(void)
160 GtkWidget *msg_label;
161 GtkWidget *confirm_area;
162 GtkWidget *cancel_button;
163 GtkWidget *refresh_button;
164 GtkWidget *scrolledwin;
165 gchar * col_names[3] = {
166 _("name"), _("count of messages"), _("type")
169 dialog = gtk_dialog_new();
170 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, TRUE, FALSE);
171 gtk_widget_set_usize(dialog,
172 GROUPLIST_DIALOG_WIDTH, GROUPLIST_DIALOG_HEIGHT);
173 gtk_container_set_border_width
174 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
175 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
176 gtk_window_set_title(GTK_WINDOW(dialog), _("Subscribe to newsgroup"));
177 gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
178 GTK_SIGNAL_FUNC(cancel_clicked), NULL);
179 gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
180 GTK_SIGNAL_FUNC(key_pressed), NULL);
181 gtk_signal_connect(GTK_OBJECT(dialog), "focus_in_event",
182 GTK_SIGNAL_FUNC(manage_window_focus_in), NULL);
183 gtk_signal_connect(GTK_OBJECT(dialog), "focus_out_event",
184 GTK_SIGNAL_FUNC(manage_window_focus_out), NULL);
186 gtk_widget_realize(dialog);
188 vbox = gtk_vbox_new(FALSE, 8);
189 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
190 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
192 hbox = gtk_hbox_new(FALSE, 0);
193 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
195 msg_label = gtk_label_new(_("Input subscribing newsgroup:"));
196 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
198 entry = gtk_entry_new();
199 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
200 gtk_signal_connect(GTK_OBJECT(entry), "activate",
201 GTK_SIGNAL_FUNC(entry_activated), NULL);
203 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
204 gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
205 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
206 GTK_POLICY_AUTOMATIC,
207 GTK_POLICY_AUTOMATIC);
209 groups_tree = gtk_ctree_new_with_titles(3, 0, col_names);
210 gtk_container_add(GTK_CONTAINER(scrolledwin), groups_tree);
211 gtk_clist_set_selection_mode(GTK_CLIST(groups_tree),
212 GTK_SELECTION_MULTIPLE);
214 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[0].button,
217 gtk_signal_connect(GTK_OBJECT(groups_tree), "tree_select_row",
218 GTK_SIGNAL_FUNC(groups_tree_selected), NULL);
219 gtk_signal_connect(GTK_OBJECT(groups_tree), "tree_unselect_row",
220 GTK_SIGNAL_FUNC(groups_tree_unselected), NULL);
222 hbox = gtk_hbox_new(FALSE, 0);
223 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
225 status_label = gtk_label_new("");
226 gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
228 gtkut_button_set_create(&confirm_area,
230 &cancel_button, _("Cancel"),
231 &refresh_button, _("Refresh"));
232 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
234 gtk_widget_grab_default(ok_button);
236 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
237 GTK_SIGNAL_FUNC(ok_clicked), NULL);
238 gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
239 GTK_SIGNAL_FUNC(cancel_clicked), NULL);
240 gtk_signal_connect(GTK_OBJECT(refresh_button), "clicked",
241 GTK_SIGNAL_FUNC(refresh_clicked), NULL);
243 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
246 static GHashTable * hash_last_node;
247 static GHashTable * hash_branch_node;
249 static void hash_news_init()
251 hash_last_node = g_hash_table_new(g_str_hash, g_str_equal);
252 hash_branch_node = g_hash_table_new(g_str_hash, g_str_equal);
255 static void free_key(gchar * key, void * value, void * user_data)
260 static void hash_news_done()
262 g_hash_table_foreach(hash_branch_node, free_key, NULL);
263 g_hash_table_foreach(hash_last_node, free_key, NULL);
265 g_hash_table_destroy(hash_branch_node);
266 g_hash_table_destroy(hash_last_node);
269 static GtkCTreeNode * hash_news_get_branch_node(gchar * name)
271 return g_hash_table_lookup(hash_branch_node, name);
274 static void hash_news_set_branch_node(gchar * name, GtkCTreeNode * node)
276 g_hash_table_insert(hash_branch_node, g_strdup(name), node);
279 static GtkCTreeNode * hash_news_get_last_node(gchar * name)
281 return g_hash_table_lookup(hash_last_node, name);
284 static void hash_news_set_last_node(gchar * name, GtkCTreeNode * node)
289 if (g_hash_table_lookup_extended(hash_last_node, name,
290 (void *) &key, &value)) {
291 g_hash_table_remove(hash_last_node, name);
295 g_hash_table_insert(hash_last_node, g_strdup(name), node);
298 static gchar * get_parent_name(gchar * name)
302 p = (gchar *) strrchr(name, '.');
306 return g_strndup(name, p - name);
309 static gchar * get_node_name(gchar * name)
313 p = (gchar *) strrchr(name, '.');
320 static GtkCTreeNode * create_parent(GtkCTree * groups_tree, gchar * name)
323 GtkCTreeNode * parent;
330 if (hash_news_get_branch_node(name) != NULL)
333 cols[0] = get_node_name(name);
337 parent_name = get_parent_name(name);
338 create_parent(groups_tree, parent_name);
340 parent = hash_news_get_branch_node(parent_name);
341 node = hash_news_get_last_node(parent_name);
342 node = gtk_ctree_insert_node(groups_tree,
347 gtk_ctree_node_set_selectable(groups_tree, node, FALSE);
348 hash_news_set_last_node(parent_name, node);
349 hash_news_set_branch_node(name, node);
354 static GtkCTreeNode * create_branch(GtkCTree * groups_tree, gchar * name,
355 struct NNTPGroupInfo * info)
360 GtkCTreeNode * parent;
366 count = info->last - info->first;
369 snprintf(count_str, 10, "%i", count);
371 cols[0] = get_node_name(info->name);
373 if (info->type == 'y')
375 else if (info->type == 'm')
376 cols[2] = "moderated";
377 else if (info->type == 'n')
378 cols[2] = "readonly";
382 parent_name = get_parent_name(name);
384 create_parent(groups_tree, parent_name);
386 parent = hash_news_get_branch_node(parent_name);
387 node = hash_news_get_last_node(parent_name);
388 node = gtk_ctree_insert_node(groups_tree,
393 gtk_ctree_node_set_selectable(groups_tree, node, TRUE);
394 hash_news_set_last_node(parent_name, node);
401 gtk_ctree_node_set_row_data(GTK_CTREE(groups_tree), node, info);
406 static void grouplist_dialog_set_list(gchar * pattern)
410 GSList * group_list = NULL;
421 recv_set_ui_func(grouplist_recv_func, NULL);
422 group_list = news_get_group_list(news_folder);
423 recv_set_ui_func(NULL, NULL);
424 if (group_list == NULL) {
425 alertpanel_error(_("Can't retrieve newsgroup list."));
430 dont_unsubscribed = TRUE;
434 gtk_clist_freeze(GTK_CLIST(groups_tree));
436 r_list = g_slist_copy(group_list);
437 r_list = g_slist_reverse(r_list);
439 for (cur = r_list; cur != NULL ; cur = cur->next) {
440 struct NNTPGroupInfo * info;
442 info = (struct NNTPGroupInfo *) cur->data;
444 if (fnmatch(pattern, info->name, 0) == 0) {
447 node = create_branch(GTK_CTREE(groups_tree),
450 l = g_list_find_custom(subscribed, info->name,
451 (GCompareFunc) g_strcasecmp);
454 gtk_ctree_select(GTK_CTREE(groups_tree), node);
458 g_slist_free(r_list);
462 node = gtk_ctree_node_nth(GTK_CTREE(groups_tree), 0);
463 gtk_ctree_node_moveto(GTK_CTREE(groups_tree), node, 0, 0, 0);
465 dont_unsubscribed = FALSE;
467 gtk_clist_set_column_width(GTK_CLIST(groups_tree), 0, GROUPLIST_NAMES);
468 gtk_clist_set_column_justification (GTK_CLIST(groups_tree), 1,
470 gtk_clist_set_column_auto_resize(GTK_CLIST(groups_tree), 1, TRUE);
471 gtk_clist_set_column_auto_resize(GTK_CLIST(groups_tree), 2, TRUE);
473 gtk_clist_thaw(GTK_CLIST(groups_tree));
475 gtk_widget_grab_focus(ok_button);
476 gtk_widget_grab_focus(groups_tree);
478 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
483 static void grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
488 g_snprintf(buf, sizeof(buf),
489 _("%d newsgroups received (%s read)"),
490 count, to_human_readable(read_bytes));
491 gtk_label_set_text(GTK_LABEL(status_label), buf);
495 static void ok_clicked(GtkWidget *widget, gpointer data)
498 gboolean update_list;
500 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
504 if (strchr(str, '*') != NULL)
508 grouplist_dialog_set_list(str);
514 if (gtk_main_level() > 1)
519 static void cancel_clicked(GtkWidget *widget, gpointer data)
522 if (gtk_main_level() > 1)
526 static void refresh_clicked(GtkWidget *widget, gpointer data)
532 news_cancel_group_list_cache(news_folder);
534 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
535 grouplist_dialog_set_list(str);
539 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
541 if (event && event->keyval == GDK_Escape)
542 cancel_clicked(NULL, NULL);
545 static void groups_tree_selected(GtkCTree *groups_tree, GtkCTreeNode * node,
547 GdkEventButton *event, gpointer user_data)
549 struct NNTPGroupInfo * group;
552 group = (struct NNTPGroupInfo *)
553 gtk_ctree_node_get_row_data(GTK_CTREE(groups_tree), node);
558 if (!dont_unsubscribed) {
559 subscribed = g_list_append(subscribed, g_strdup(group->name));
563 static void groups_tree_unselected(GtkCTree *groups_tree, GtkCTreeNode * node,
565 GdkEventButton *event, gpointer user_data)
567 struct NNTPGroupInfo * group;
570 group = (struct NNTPGroupInfo *)
571 gtk_ctree_node_get_row_data(GTK_CTREE(groups_tree), node);
576 if (!dont_unsubscribed) {
577 l = g_list_find_custom(subscribed, group->name,
578 (GCompareFunc) g_strcasecmp);
581 subscribed = g_list_remove(subscribed, l->data);
586 static void entry_activated(GtkEditable *editable)
589 gboolean update_list;
591 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
595 if (strchr(str, '*') != NULL)
599 grouplist_dialog_set_list(str);
604 ok_clicked(NULL, NULL);