2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
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.
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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
32 #include "grouplistdialog.h"
33 #include "mainwindow.h"
34 #include "manage_window.h"
39 #include "alertpanel.h"
43 #define GROUPLIST_DIALOG_WIDTH 450
44 #define GROUPLIST_DIALOG_HEIGHT 400
45 #define GROUPLIST_COL_NAME_WIDTH 250
48 static gboolean locked;
50 static GtkWidget *dialog;
51 static GtkWidget *entry;
52 static GtkWidget *ctree;
53 static GtkWidget *status_label;
54 static GtkWidget *ok_button;
55 static GSList *group_list;
56 static Folder *news_folder;
58 static GSList *subscribed;
60 static void grouplist_dialog_create (void);
61 static void grouplist_dialog_set_list (const gchar *pattern,
63 static void grouplist_search (void);
64 static void grouplist_clear (void);
65 static gboolean grouplist_recv_func (SockInfo *sock,
70 static gint window_deleted (GtkWidget *widget,
73 static void ok_clicked (GtkWidget *widget,
75 static void cancel_clicked (GtkWidget *widget,
77 static void refresh_clicked (GtkWidget *widget,
79 static gboolean key_pressed (GtkWidget *widget,
82 static gboolean button_press_cb (GtkCMCTree *ctree,
83 GdkEventButton *button,
85 static void entry_activated (GtkEditable *editable);
86 static void search_clicked (GtkWidget *widget,
89 GSList *grouplist_dialog(Folder *folder)
94 if (dialog && GTK_WIDGET_VISIBLE(dialog)) return NULL;
97 grouplist_dialog_create();
101 gtk_widget_show(dialog);
102 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
103 manage_window_set_transient(GTK_WINDOW(dialog));
104 gtk_widget_grab_focus(ok_button);
105 gtk_widget_grab_focus(ctree);
109 for (node = folder->node->children; node != NULL; node = node->next) {
110 item = FOLDER_ITEM(node->data);
111 subscribed = g_slist_append(subscribed, g_strdup(item->path));
114 grouplist_dialog_set_list(NULL, TRUE);
118 manage_window_focus_out(dialog, NULL, NULL);
119 gtk_widget_hide(dialog);
122 slist_free_strings(subscribed);
123 g_slist_free(subscribed);
126 for (node = folder->node->children; node != NULL;
128 item = FOLDER_ITEM(node->data);
129 subscribed = g_slist_append(subscribed,
130 g_strdup(item->path));
139 static void grouplist_dialog_create(void)
143 GtkWidget *msg_label;
144 GtkWidget *search_button;
145 GtkWidget *confirm_area;
146 GtkWidget *cancel_button;
147 GtkWidget *refresh_button;
148 GtkWidget *scrolledwin;
152 dialog = gtk_dialog_new();
153 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
154 gtk_widget_set_size_request(dialog,
155 GROUPLIST_DIALOG_WIDTH,
156 GROUPLIST_DIALOG_HEIGHT);
157 gtk_container_set_border_width
158 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
159 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
160 gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
161 g_signal_connect(G_OBJECT(dialog), "delete_event",
162 G_CALLBACK(window_deleted), NULL);
163 g_signal_connect(G_OBJECT(dialog), "key_press_event",
164 G_CALLBACK(key_pressed), NULL);
165 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
167 gtk_widget_realize(dialog);
169 vbox = gtk_vbox_new(FALSE, 8);
170 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
171 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
173 hbox = gtk_hbox_new(FALSE, 0);
174 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
176 msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
177 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
179 hbox = gtk_hbox_new(FALSE, 8);
180 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
182 msg_label = gtk_label_new(_("Find groups:"));
183 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
185 entry = gtk_entry_new();
186 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
187 g_signal_connect(G_OBJECT(entry), "activate",
188 G_CALLBACK(entry_activated), NULL);
190 search_button = gtk_button_new_with_label(_(" Search "));
191 gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
193 g_signal_connect(G_OBJECT(search_button), "clicked",
194 G_CALLBACK(search_clicked), NULL);
196 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
197 gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
198 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
202 titles[0] = _("Newsgroup name");
203 titles[1] = _("Messages");
204 titles[2] = _("Type");
205 ctree = gtk_cmctree_new_with_titles(3, 0, titles);
206 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
207 gtk_cmclist_set_column_width
208 (GTK_CMCLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
209 gtk_cmclist_set_column_auto_resize(GTK_CMCLIST(ctree), 0, TRUE);
210 gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_MULTIPLE);
211 gtk_cmctree_set_line_style(GTK_CMCTREE(ctree), GTK_CMCTREE_LINES_DOTTED);
212 gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
213 GTK_CMCTREE_EXPANDER_SQUARE);
214 for (i = 0; i < 3; i++)
215 GTK_WIDGET_UNSET_FLAGS(GTK_CMCLIST(ctree)->column[i].button,
217 g_signal_connect(G_OBJECT(ctree), "button-press-event",
218 G_CALLBACK(button_press_cb), 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_stock_button_set_create(&confirm_area,
227 &refresh_button, GTK_STOCK_REFRESH,
228 &cancel_button, GTK_STOCK_CANCEL,
229 &ok_button, GTK_STOCK_OK);
230 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
232 gtk_widget_grab_default(ok_button);
234 g_signal_connect(G_OBJECT(ok_button), "clicked",
235 G_CALLBACK(ok_clicked), NULL);
236 g_signal_connect(G_OBJECT(cancel_button), "clicked",
237 G_CALLBACK(cancel_clicked), NULL);
238 g_signal_connect(G_OBJECT(refresh_button), "clicked",
239 G_CALLBACK(refresh_clicked), NULL);
241 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
244 static GHashTable *branch_node_table;
246 static void grouplist_hash_init(void)
248 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
251 static void grouplist_hash_done(void)
253 hash_free_strings(branch_node_table);
254 g_hash_table_destroy(branch_node_table);
257 static GtkCMCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
259 return g_hash_table_lookup(branch_node_table, name);
262 static void grouplist_hash_set_branch_node(const gchar *name,
263 GtkCMCTreeNode *node)
265 g_hash_table_insert(branch_node_table, g_strdup(name), node);
268 static gchar *grouplist_get_parent_name(const gchar *name)
272 p = strrchr(name, '.');
275 return g_strndup(name, p - name);
278 static GtkCMCTreeNode *grouplist_create_parent(const gchar *name,
279 const gchar *pattern)
281 GtkCMCTreeNode *parent;
282 GtkCMCTreeNode *node;
286 if (*name == '\0') return NULL;
287 node = grouplist_hash_get_branch_node(name);
288 if (node != NULL) return node;
290 cols[0] = (gchar *)name;
291 cols[1] = cols[2] = "";
293 parent_name = grouplist_get_parent_name(name);
294 parent = grouplist_create_parent(parent_name, pattern);
296 node = parent ? GTK_CMCTREE_ROW(parent)->children
297 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
298 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
301 if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
302 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
303 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, FALSE);
305 grouplist_hash_set_branch_node(name, node);
312 static GtkCMCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
313 const gchar *pattern)
315 GtkCMCTreeNode *node;
316 GtkCMCTreeNode *parent;
317 gchar *name = (gchar *)ginfo->name;
323 count = ginfo->last - ginfo->first;
326 count_str = itos(count);
328 cols[0] = ginfo->name;
330 if (ginfo->type == 'y')
332 else if (ginfo->type == 'm')
333 cols[2] = _("moderated");
334 else if (ginfo->type == 'n')
335 cols[2] = _("readonly");
337 cols[2] = _("unknown");
339 parent_name = grouplist_get_parent_name(name);
340 parent = grouplist_create_parent(parent_name, pattern);
341 node = grouplist_hash_get_branch_node(name);
343 gtk_sctree_set_node_info(GTK_CMCTREE(ctree), node, cols[0], 0,
344 NULL, NULL, FALSE, FALSE);
345 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 1, cols[1]);
346 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 2, cols[2]);
348 node = parent ? GTK_CMCTREE_ROW(parent)->children
349 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
350 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
354 g_pattern_match_simple(pattern, parent_name) == FALSE)
355 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
357 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, TRUE);
359 gtk_cmctree_node_set_row_data(GTK_CMCTREE(ctree), node, ginfo);
366 static void grouplist_expand_upwards(GtkCMCTree *ctree, const gchar *name) {
368 gchar *newname=g_malloc0(strlen(name));
370 for (ptr=name; *ptr; ptr++) {
372 gtk_cmctree_expand(ctree,
373 grouplist_hash_get_branch_node(newname));
374 newname[ptr-name] = *ptr;
379 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
381 static GdkCursor *watch_cursor = NULL;
383 GtkCMCTreeNode *node;
389 if (!pattern || *pattern == '\0')
393 watch_cursor = gdk_cursor_new(GDK_WATCH);
394 gdk_window_set_cursor(dialog->window, watch_cursor);
395 main_window_cursor_wait(mainwindow_get_mainwindow());
401 recv_set_ui_func(grouplist_recv_func, NULL);
402 group_list = news_get_group_list(news_folder);
403 group_list = g_slist_reverse(group_list);
404 recv_set_ui_func(NULL, NULL);
405 if (group_list == NULL && ack == TRUE) {
406 alertpanel_error(_("Can't retrieve newsgroup list."));
408 gdk_window_set_cursor(dialog->window, NULL);
409 main_window_cursor_normal(mainwindow_get_mainwindow());
413 gtk_cmclist_clear(GTK_CMCLIST(ctree));
415 gtk_entry_set_text(GTK_ENTRY(entry), pattern);
417 grouplist_hash_init();
419 gtk_cmclist_freeze(GTK_CMCLIST(ctree));
421 pspec = g_pattern_spec_new(pattern);
423 for (cur = group_list; cur != NULL ; cur = cur->next) {
424 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
426 if (g_pattern_match_string(pspec, ginfo->name)) {
427 node = grouplist_create_branch(ginfo, pattern);
428 if (g_slist_find_custom(subscribed, ginfo->name,
429 (GCompareFunc)g_ascii_strcasecmp)
431 gtk_cmctree_select(GTK_CMCTREE(ctree), node);
434 for (cur = subscribed; cur; cur = g_slist_next(cur))
435 grouplist_expand_upwards(GTK_CMCTREE(ctree), (gchar *)cur->data);
437 g_pattern_spec_free(pspec);
439 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
441 grouplist_hash_done();
443 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
445 gdk_window_set_cursor(dialog->window, NULL);
446 main_window_cursor_normal(mainwindow_get_mainwindow());
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_cmclist_clear(GTK_CMCLIST(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((goffset)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(GtkCMCTree *ctree, GdkEventButton *button,
539 GtkCMCTreeNode *node;
540 NewsGroupInfo *ginfo;
543 if (button->type != GDK_BUTTON_PRESS) return TRUE;
544 if (button->button != 1) return TRUE;
546 gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree),
547 button->x, button->y, &row, &col);
548 node = gtk_cmctree_node_nth(ctree, row);
549 if (!node) return TRUE;
551 if (gtk_cmctree_is_hot_spot(ctree, button->x, button->y)) {
552 gtk_cmctree_toggle_expansion(ctree, node);
556 ginfo = gtk_cmctree_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_cmclist_unselect_row(GTK_CMCLIST(ctree), row, 0);
566 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
567 gtk_cmclist_select_row(GTK_CMCLIST(ctree), row, 0);
573 static void entry_activated(GtkEditable *editable)
578 static void search_clicked(GtkWidget *widget, gpointer data)