2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkmain.h>
30 #include <gtk/gtkwidget.h>
31 #include <gtk/gtkdialog.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkctree.h>
39 #include <gtk/gtkscrolledwindow.h>
40 #include <gtk/gtkbutton.h>
41 #include <gtk/gtkhbbox.h>
42 #include <gtk/gtkstock.h>
45 #include "grouplistdialog.h"
46 #include "manage_window.h"
51 #include "alertpanel.h"
55 #define GROUPLIST_DIALOG_WIDTH 450
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 (const gchar *pattern,
75 static void grouplist_search (void);
76 static void grouplist_clear (void);
77 static gboolean grouplist_recv_func (SockInfo *sock,
82 static gint window_deleted (GtkWidget *widget,
85 static void ok_clicked (GtkWidget *widget,
87 static void cancel_clicked (GtkWidget *widget,
89 static void refresh_clicked (GtkWidget *widget,
91 static gboolean key_pressed (GtkWidget *widget,
94 static gboolean button_press_cb (GtkCTree *ctree,
95 GdkEventButton *button,
97 static void entry_activated (GtkEditable *editable);
98 static void search_clicked (GtkWidget *widget,
101 GSList *grouplist_dialog(Folder *folder)
106 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
109 grouplist_dialog_create();
111 news_folder = folder;
113 gtk_widget_show(dialog);
114 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
115 manage_window_set_transient(GTK_WINDOW(dialog));
116 gtk_widget_grab_focus(ok_button);
117 gtk_widget_grab_focus(ctree);
121 for (node = folder->node->children; node != NULL; node = node->next) {
122 item = FOLDER_ITEM(node->data);
123 subscribed = g_slist_append(subscribed, g_strdup(item->path));
126 grouplist_dialog_set_list(NULL, TRUE);
130 manage_window_focus_out(dialog, NULL, NULL);
131 gtk_widget_hide(dialog);
134 slist_free_strings(subscribed);
135 g_slist_free(subscribed);
138 for (node = folder->node->children; node != NULL;
140 item = FOLDER_ITEM(node->data);
141 subscribed = g_slist_append(subscribed,
142 g_strdup(item->path));
151 static void grouplist_dialog_create(void)
155 GtkWidget *msg_label;
156 GtkWidget *search_button;
157 GtkWidget *confirm_area;
158 GtkWidget *cancel_button;
159 GtkWidget *refresh_button;
160 GtkWidget *scrolledwin;
164 dialog = gtk_dialog_new();
165 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
166 gtk_widget_set_size_request(dialog,
167 GROUPLIST_DIALOG_WIDTH,
168 GROUPLIST_DIALOG_HEIGHT);
169 gtk_container_set_border_width
170 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
171 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
172 gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
173 g_signal_connect(G_OBJECT(dialog), "delete_event",
174 G_CALLBACK(window_deleted), NULL);
175 g_signal_connect(G_OBJECT(dialog), "key_press_event",
176 G_CALLBACK(key_pressed), NULL);
177 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
179 gtk_widget_realize(dialog);
181 vbox = gtk_vbox_new(FALSE, 8);
182 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
183 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
185 hbox = gtk_hbox_new(FALSE, 0);
186 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
188 msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
189 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
191 hbox = gtk_hbox_new(FALSE, 8);
192 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
194 msg_label = gtk_label_new(_("Find groups:"));
195 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
197 entry = gtk_entry_new();
198 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
199 g_signal_connect(G_OBJECT(entry), "activate",
200 G_CALLBACK(entry_activated), NULL);
202 search_button = gtk_button_new_with_label(_(" Search "));
203 gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
205 g_signal_connect(G_OBJECT(search_button), "clicked",
206 G_CALLBACK(search_clicked), NULL);
208 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
209 gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
210 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
214 titles[0] = _("Newsgroup name");
215 titles[1] = _("Messages");
216 titles[2] = _("Type");
217 ctree = gtk_ctree_new_with_titles(3, 0, titles);
218 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
219 gtk_clist_set_column_width
220 (GTK_CLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
221 gtk_clist_set_column_auto_resize(GTK_CLIST(ctree), 0, TRUE);
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,
229 g_signal_connect(G_OBJECT(ctree), "button-press-event",
230 G_CALLBACK(button_press_cb), NULL);
232 hbox = gtk_hbox_new(FALSE, 0);
233 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
235 status_label = gtk_label_new("");
236 gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
238 gtkut_stock_button_set_create(&confirm_area,
239 &ok_button, GTK_STOCK_OK,
240 &cancel_button, GTK_STOCK_CANCEL,
241 &refresh_button, GTK_STOCK_REFRESH);
242 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
244 gtk_widget_grab_default(ok_button);
246 g_signal_connect(G_OBJECT(ok_button), "clicked",
247 G_CALLBACK(ok_clicked), NULL);
248 g_signal_connect(G_OBJECT(cancel_button), "clicked",
249 G_CALLBACK(cancel_clicked), NULL);
250 g_signal_connect(G_OBJECT(refresh_button), "clicked",
251 G_CALLBACK(refresh_clicked), NULL);
253 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
256 static GHashTable *branch_node_table;
258 static void grouplist_hash_init(void)
260 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
263 static void grouplist_hash_done(void)
265 hash_free_strings(branch_node_table);
266 g_hash_table_destroy(branch_node_table);
269 static GtkCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
271 return g_hash_table_lookup(branch_node_table, name);
274 static void grouplist_hash_set_branch_node(const gchar *name,
277 g_hash_table_insert(branch_node_table, g_strdup(name), node);
280 static gchar *grouplist_get_parent_name(const gchar *name)
284 p = strrchr(name, '.');
287 return g_strndup(name, p - name);
290 static GtkCTreeNode *grouplist_create_parent(const gchar *name,
291 const gchar *pattern)
293 GtkCTreeNode *parent;
298 if (*name == '\0') return NULL;
299 node = grouplist_hash_get_branch_node(name);
300 if (node != NULL) return node;
302 cols[0] = (gchar *)name;
303 cols[1] = cols[2] = "";
305 parent_name = grouplist_get_parent_name(name);
306 parent = grouplist_create_parent(parent_name, pattern);
308 node = parent ? GTK_CTREE_ROW(parent)->children
309 : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
310 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
311 cols, 0, NULL, NULL, NULL, NULL,
313 if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
314 gtk_ctree_expand(GTK_CTREE(ctree), parent);
315 gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, FALSE);
317 grouplist_hash_set_branch_node(name, node);
324 static GtkCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
325 const gchar *pattern)
328 GtkCTreeNode *parent;
329 gchar *name = (gchar *)ginfo->name;
335 count = ginfo->last - ginfo->first;
338 count_str = itos(count);
340 cols[0] = ginfo->name;
342 if (ginfo->type == 'y')
344 else if (ginfo->type == 'm')
345 cols[2] = _("moderated");
346 else if (ginfo->type == 'n')
347 cols[2] = _("readonly");
349 cols[2] = _("unknown");
351 parent_name = grouplist_get_parent_name(name);
352 parent = grouplist_create_parent(parent_name, pattern);
353 node = grouplist_hash_get_branch_node(name);
355 gtk_ctree_set_node_info(GTK_CTREE(ctree), node, cols[0], 0,
356 NULL, NULL, NULL, NULL, FALSE, FALSE);
357 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 1, cols[1]);
358 gtk_ctree_node_set_text(GTK_CTREE(ctree), node, 2, cols[2]);
360 node = parent ? GTK_CTREE_ROW(parent)->children
361 : GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
362 node = gtk_ctree_insert_node(GTK_CTREE(ctree), parent, node,
363 cols, 0, NULL, NULL, NULL, NULL,
366 g_pattern_match_simple(pattern, parent_name) == FALSE)
367 gtk_ctree_expand(GTK_CTREE(ctree), parent);
369 gtk_ctree_node_set_selectable(GTK_CTREE(ctree), node, TRUE);
371 gtk_ctree_node_set_row_data(GTK_CTREE(ctree), node, ginfo);
378 static void grouplist_expand_upwards(GtkCTree *ctree, const gchar *name) {
380 gchar *newname=g_malloc0(strlen(name));
382 for (ptr=name; *ptr; ptr++) {
384 gtk_ctree_expand(ctree,
385 grouplist_hash_get_branch_node(newname));
386 newname[ptr-name] = *ptr;
391 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
400 if (!pattern || *pattern == '\0')
406 recv_set_ui_func(grouplist_recv_func, NULL);
407 group_list = news_get_group_list(news_folder);
408 group_list = g_slist_reverse(group_list);
409 recv_set_ui_func(NULL, NULL);
410 if (group_list == NULL && ack == TRUE) {
411 alertpanel_error(_("Can't retrieve newsgroup list."));
416 gtk_clist_clear(GTK_CLIST(ctree));
418 gtk_entry_set_text(GTK_ENTRY(entry), pattern);
420 grouplist_hash_init();
422 gtk_clist_freeze(GTK_CLIST(ctree));
424 pspec = g_pattern_spec_new(pattern);
426 for (cur = group_list; cur != NULL ; cur = cur->next) {
427 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
429 if (g_pattern_match_string(pspec, ginfo->name)) {
430 node = grouplist_create_branch(ginfo, pattern);
431 if (g_slist_find_custom(subscribed, ginfo->name,
432 (GCompareFunc)g_ascii_strcasecmp)
434 gtk_ctree_select(GTK_CTREE(ctree), node);
437 for (cur = subscribed; cur; cur = g_slist_next(cur))
438 grouplist_expand_upwards(GTK_CTREE(ctree), (gchar *)cur->data);
440 g_pattern_spec_free(pspec);
442 gtk_clist_thaw(GTK_CLIST(ctree));
444 grouplist_hash_done();
446 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
451 static void grouplist_search(void)
457 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
458 grouplist_dialog_set_list(str, FALSE);
462 static void grouplist_clear(void)
464 gtk_clist_clear(GTK_CLIST(ctree));
465 gtk_entry_set_text(GTK_ENTRY(entry), "");
466 news_group_list_free(group_list);
470 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
475 g_snprintf(buf, sizeof(buf),
476 _("%d newsgroups received (%s read)"),
477 count, to_human_readable(read_bytes));
478 gtk_label_set_text(GTK_LABEL(status_label), buf);
486 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
489 if (gtk_main_level() > 1)
495 static void ok_clicked(GtkWidget *widget, gpointer data)
498 if (gtk_main_level() > 1)
502 static void cancel_clicked(GtkWidget *widget, gpointer data)
505 if (gtk_main_level() > 1)
509 static void refresh_clicked(GtkWidget *widget, gpointer data)
515 news_remove_group_list_cache(news_folder);
517 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
518 grouplist_dialog_set_list(str, TRUE);
522 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
524 if (event && event->keyval == GDK_Escape)
525 cancel_clicked(NULL, NULL);
529 /* clist/ctree clear old selection on click (gtk2 only)
530 * - intercept all button clicks (always return TRUE)
531 * - only allow left button single click
532 * - handle click on expander
533 * - update "subscribed" list and un-/select row
535 static gboolean button_press_cb(GtkCTree *ctree, GdkEventButton *button,
540 NewsGroupInfo *ginfo;
543 if (button->type != GDK_BUTTON_PRESS) return TRUE;
544 if (button->button != 1) return TRUE;
546 gtk_clist_get_selection_info(GTK_CLIST(ctree),
547 button->x, button->y, &row, &col);
548 node = gtk_ctree_node_nth(ctree, row);
549 if (!node) return TRUE;
551 if (gtk_ctree_is_hot_spot(ctree, button->x, button->y)) {
552 gtk_ctree_toggle_expansion(ctree, node);
556 ginfo = gtk_ctree_node_get_row_data(ctree, node);
557 if (!ginfo) return TRUE;
559 list = g_slist_find_custom(subscribed, ginfo->name,
560 (GCompareFunc)g_ascii_strcasecmp);
563 subscribed = g_slist_remove(subscribed, list->data);
564 gtk_clist_unselect_row(GTK_CLIST(ctree), row, 0);
566 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
567 gtk_clist_select_row(GTK_CLIST(ctree), row, 0);
573 static void entry_activated(GtkEditable *editable)
578 static void search_clicked(GtkWidget *widget, gpointer data)