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"
42 #include "prefs_common.h"
44 #define GROUPLIST_DIALOG_WIDTH 450
45 #define GROUPLIST_DIALOG_HEIGHT 400
46 #define GROUPLIST_COL_NAME_WIDTH 250
49 static gboolean locked;
51 static GtkWidget *dialog;
52 static GtkWidget *entry;
53 static GtkWidget *ctree;
54 static GtkWidget *status_label;
55 static GtkWidget *ok_button;
56 static GSList *group_list;
57 static Folder *news_folder;
59 static GSList *subscribed;
61 static void grouplist_dialog_create (void);
62 static void grouplist_dialog_set_list (const gchar *pattern,
64 static void grouplist_search (void);
65 static void grouplist_clear (void);
66 static gboolean grouplist_recv_func (SockInfo *sock,
70 static void grouplist_size_allocate (GtkWidget *widget,
71 GtkAllocation *allocation);
73 static gint window_deleted (GtkWidget *widget,
76 static void ok_clicked (GtkWidget *widget,
78 static void cancel_clicked (GtkWidget *widget,
80 static void refresh_clicked (GtkWidget *widget,
82 static gboolean key_pressed (GtkWidget *widget,
85 static gboolean button_press_cb (GtkCMCTree *ctree,
86 GdkEventButton *button,
88 static void entry_activated (GtkEditable *editable);
89 static void search_clicked (GtkWidget *widget,
92 GSList *grouplist_dialog(Folder *folder)
97 if (dialog && gtkut_widget_get_visible(dialog)) return NULL;
100 grouplist_dialog_create();
102 news_folder = folder;
104 gtk_widget_show(dialog);
105 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
106 manage_window_set_transient(GTK_WINDOW(dialog));
107 gtk_widget_grab_focus(ok_button);
108 gtk_widget_grab_focus(ctree);
112 for (node = folder->node->children; node != NULL; node = node->next) {
113 item = FOLDER_ITEM(node->data);
114 subscribed = g_slist_append(subscribed, g_strdup(item->path));
117 grouplist_dialog_set_list(NULL, TRUE);
121 manage_window_focus_out(dialog, NULL, NULL);
122 gtk_widget_hide(dialog);
125 slist_free_strings(subscribed);
126 g_slist_free(subscribed);
129 for (node = folder->node->children; node != NULL;
131 item = FOLDER_ITEM(node->data);
132 subscribed = g_slist_append(subscribed,
133 g_strdup(item->path));
142 static void grouplist_dialog_create(void)
146 GtkWidget *msg_label;
147 GtkWidget *search_button;
148 GtkWidget *confirm_area;
149 GtkWidget *cancel_button;
150 GtkWidget *refresh_button;
151 GtkWidget *scrolledwin;
152 static GdkGeometry geometry;
156 dialog = gtk_dialog_new();
157 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
158 gtk_container_set_border_width
159 (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
160 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
161 gtk_window_set_title(GTK_WINDOW(dialog), _("Newsgroup subscription"));
162 g_signal_connect(G_OBJECT(dialog), "delete_event",
163 G_CALLBACK(window_deleted), NULL);
164 g_signal_connect(G_OBJECT(dialog), "key_press_event",
165 G_CALLBACK(key_pressed), NULL);
166 g_signal_connect(G_OBJECT(dialog), "size_allocate",
167 G_CALLBACK(grouplist_size_allocate), NULL);
168 MANAGE_WINDOW_SIGNALS_CONNECT(dialog);
170 vbox = gtk_vbox_new(FALSE, 8);
171 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
172 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
174 hbox = gtk_hbox_new(FALSE, 0);
175 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
177 msg_label = gtk_label_new(_("Select newsgroups for subscription:"));
178 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
180 hbox = gtk_hbox_new(FALSE, 8);
181 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
183 msg_label = gtk_label_new(_("Find groups:"));
184 gtk_box_pack_start(GTK_BOX(hbox), msg_label, FALSE, FALSE, 0);
186 entry = gtk_entry_new();
187 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
188 g_signal_connect(G_OBJECT(entry), "activate",
189 G_CALLBACK(entry_activated), NULL);
191 search_button = gtk_button_new_with_label(_(" Search "));
192 gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
194 g_signal_connect(G_OBJECT(search_button), "clicked",
195 G_CALLBACK(search_clicked), NULL);
197 scrolledwin = gtk_scrolled_window_new(NULL, NULL);
198 gtk_box_pack_start(GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0);
199 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolledwin),
200 GTK_POLICY_AUTOMATIC,
201 GTK_POLICY_AUTOMATIC);
203 titles[0] = _("Newsgroup name");
204 titles[1] = _("Messages");
205 titles[2] = _("Type");
206 ctree = gtk_sctree_new_with_titles(3, 0, titles);
207 gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
208 gtk_cmclist_set_column_width
209 (GTK_CMCLIST(ctree), 0, GROUPLIST_COL_NAME_WIDTH);
210 gtk_cmclist_set_column_auto_resize(GTK_CMCLIST(ctree), 0, TRUE);
211 gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_MULTIPLE);
213 if (prefs_common.enable_dotted_lines) {
214 gtk_cmctree_set_line_style(GTK_CMCTREE(ctree), GTK_CMCTREE_LINES_DOTTED);
215 gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
216 GTK_CMCTREE_EXPANDER_SQUARE);
218 gtk_cmctree_set_line_style(GTK_CMCTREE(ctree), GTK_CMCTREE_LINES_NONE);
219 gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
220 GTK_CMCTREE_EXPANDER_TRIANGLE);
223 for (i = 0; i < 3; i++)
224 GTK_WIDGET_UNSET_FLAGS(GTK_CMCLIST(ctree)->column[i].button,
226 g_signal_connect(G_OBJECT(ctree), "button-press-event",
227 G_CALLBACK(button_press_cb), NULL);
229 hbox = gtk_hbox_new(FALSE, 0);
230 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
232 status_label = gtk_label_new("");
233 gtk_box_pack_start(GTK_BOX(hbox), status_label, FALSE, FALSE, 0);
235 gtkut_stock_button_set_create(&confirm_area,
236 &refresh_button, GTK_STOCK_REFRESH,
237 &cancel_button, GTK_STOCK_CANCEL,
238 &ok_button, GTK_STOCK_OK);
239 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
241 gtk_widget_grab_default(ok_button);
243 g_signal_connect(G_OBJECT(ok_button), "clicked",
244 G_CALLBACK(ok_clicked), NULL);
245 g_signal_connect(G_OBJECT(cancel_button), "clicked",
246 G_CALLBACK(cancel_clicked), NULL);
247 g_signal_connect(G_OBJECT(refresh_button), "clicked",
248 G_CALLBACK(refresh_clicked), NULL);
250 if (!geometry.min_width) {
251 geometry.min_width = GROUPLIST_DIALOG_WIDTH;
252 geometry.min_height = GROUPLIST_DIALOG_HEIGHT;
255 gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
257 gtk_window_set_default_size(GTK_WINDOW(dialog),
258 prefs_common.news_subscribe_width,
259 prefs_common.news_subscribe_height);
261 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
264 static GHashTable *branch_node_table;
266 static void grouplist_hash_init(void)
268 branch_node_table = g_hash_table_new(g_str_hash, g_str_equal);
271 static void grouplist_hash_done(void)
273 hash_free_strings(branch_node_table);
274 g_hash_table_destroy(branch_node_table);
277 static GtkCMCTreeNode *grouplist_hash_get_branch_node(const gchar *name)
279 return g_hash_table_lookup(branch_node_table, name);
282 static void grouplist_hash_set_branch_node(const gchar *name,
283 GtkCMCTreeNode *node)
285 g_hash_table_insert(branch_node_table, g_strdup(name), node);
288 static gchar *grouplist_get_parent_name(const gchar *name)
292 p = strrchr(name, '.');
295 return g_strndup(name, p - name);
298 static GtkCMCTreeNode *grouplist_create_parent(const gchar *name,
299 const gchar *pattern)
301 GtkCMCTreeNode *parent;
302 GtkCMCTreeNode *node;
306 if (*name == '\0') return NULL;
307 node = grouplist_hash_get_branch_node(name);
308 if (node != NULL) return node;
310 cols[0] = (gchar *)name;
311 cols[1] = cols[2] = "";
313 parent_name = grouplist_get_parent_name(name);
314 parent = grouplist_create_parent(parent_name, pattern);
316 node = parent ? GTK_CMCTREE_ROW(parent)->children
317 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
318 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
321 if (parent && g_pattern_match_simple(pattern, parent_name) == FALSE)
322 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
323 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, FALSE);
325 grouplist_hash_set_branch_node(name, node);
332 static GtkCMCTreeNode *grouplist_create_branch(NewsGroupInfo *ginfo,
333 const gchar *pattern)
335 GtkCMCTreeNode *node;
336 GtkCMCTreeNode *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, pattern);
361 node = grouplist_hash_get_branch_node(name);
363 gtk_sctree_set_node_info(GTK_CMCTREE(ctree), node, cols[0], 0,
364 NULL, NULL, FALSE, FALSE);
365 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 1, cols[1]);
366 gtk_cmctree_node_set_text(GTK_CMCTREE(ctree), node, 2, cols[2]);
368 node = parent ? GTK_CMCTREE_ROW(parent)->children
369 : GTK_CMCTREE_NODE(GTK_CMCLIST(ctree)->row_list);
370 node = gtk_sctree_insert_node(GTK_CMCTREE(ctree), parent, node,
374 g_pattern_match_simple(pattern, parent_name) == FALSE)
375 gtk_cmctree_expand(GTK_CMCTREE(ctree), parent);
377 gtk_cmctree_node_set_selectable(GTK_CMCTREE(ctree), node, TRUE);
379 gtk_cmctree_node_set_row_data(GTK_CMCTREE(ctree), node, ginfo);
386 static void grouplist_expand_upwards(GtkCMCTree *ctree, const gchar *name) {
388 gchar *newname=g_malloc0(strlen(name));
390 for (ptr=name; *ptr; ptr++) {
392 gtk_cmctree_expand(ctree,
393 grouplist_hash_get_branch_node(newname));
394 newname[ptr-name] = *ptr;
399 static void grouplist_dialog_set_list(const gchar *pattern, gboolean refresh)
401 static GdkCursor *watch_cursor = NULL;
403 GtkCMCTreeNode *node;
409 if (!pattern || *pattern == '\0')
413 watch_cursor = gdk_cursor_new(GDK_WATCH);
414 gdk_window_set_cursor(dialog->window, watch_cursor);
415 main_window_cursor_wait(mainwindow_get_mainwindow());
421 recv_set_ui_func(grouplist_recv_func, NULL);
422 group_list = news_get_group_list(news_folder);
423 group_list = g_slist_reverse(group_list);
424 recv_set_ui_func(NULL, NULL);
425 if (group_list == NULL && ack == TRUE) {
426 alertpanel_error(_("Can't retrieve newsgroup list."));
428 gdk_window_set_cursor(dialog->window, NULL);
429 main_window_cursor_normal(mainwindow_get_mainwindow());
433 gtk_cmclist_clear(GTK_CMCLIST(ctree));
435 gtk_entry_set_text(GTK_ENTRY(entry), pattern);
437 grouplist_hash_init();
439 gtk_cmclist_freeze(GTK_CMCLIST(ctree));
441 pspec = g_pattern_spec_new(pattern);
443 for (cur = group_list; cur != NULL ; cur = cur->next) {
444 NewsGroupInfo *ginfo = (NewsGroupInfo *)cur->data;
446 if (g_pattern_match_string(pspec, ginfo->name)) {
447 node = grouplist_create_branch(ginfo, pattern);
448 if (g_slist_find_custom(subscribed, ginfo->name,
449 (GCompareFunc)g_ascii_strcasecmp)
451 gtk_cmctree_select(GTK_CMCTREE(ctree), node);
454 for (cur = subscribed; cur; cur = g_slist_next(cur))
455 grouplist_expand_upwards(GTK_CMCTREE(ctree), (gchar *)cur->data);
457 g_pattern_spec_free(pspec);
459 gtk_cmclist_thaw(GTK_CMCLIST(ctree));
461 grouplist_hash_done();
463 gtk_label_set_text(GTK_LABEL(status_label), _("Done."));
465 gdk_window_set_cursor(dialog->window, NULL);
466 main_window_cursor_normal(mainwindow_get_mainwindow());
471 static void grouplist_search(void)
477 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
478 grouplist_dialog_set_list(str, FALSE);
482 static void grouplist_clear(void)
484 gtk_cmclist_clear(GTK_CMCLIST(ctree));
485 gtk_entry_set_text(GTK_ENTRY(entry), "");
486 news_group_list_free(group_list);
490 static gboolean grouplist_recv_func(SockInfo *sock, gint count, gint read_bytes,
495 g_snprintf(buf, sizeof(buf),
496 _("%d newsgroups received (%s read)"),
497 count, to_human_readable((goffset)read_bytes));
498 gtk_label_set_text(GTK_LABEL(status_label), buf);
506 static void grouplist_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
508 cm_return_if_fail( allocation != NULL );
510 prefs_common.news_subscribe_width = allocation->width;
511 prefs_common.news_subscribe_height = allocation->height;
514 static gint window_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
517 if (gtk_main_level() > 1)
523 static void ok_clicked(GtkWidget *widget, gpointer data)
526 if (gtk_main_level() > 1)
530 static void cancel_clicked(GtkWidget *widget, gpointer data)
533 if (gtk_main_level() > 1)
537 static void refresh_clicked(GtkWidget *widget, gpointer data)
543 news_remove_group_list_cache(news_folder);
545 str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
546 grouplist_dialog_set_list(str, TRUE);
550 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
552 if (event && event->keyval == GDK_Escape)
553 cancel_clicked(NULL, NULL);
557 /* clist/ctree clear old selection on click (gtk2 only)
558 * - intercept all button clicks (always return TRUE)
559 * - only allow left button single click
560 * - handle click on expander
561 * - update "subscribed" list and un-/select row
563 static gboolean button_press_cb(GtkCMCTree *ctree, GdkEventButton *button,
567 GtkCMCTreeNode *node;
568 NewsGroupInfo *ginfo;
571 if (button->type != GDK_BUTTON_PRESS) return TRUE;
572 if (button->button != 1) return TRUE;
574 gtk_cmclist_get_selection_info(GTK_CMCLIST(ctree),
575 button->x, button->y, &row, &col);
576 node = gtk_cmctree_node_nth(ctree, row);
577 if (!node) return TRUE;
579 if (gtk_cmctree_is_hot_spot(ctree, button->x, button->y)) {
580 gtk_cmctree_toggle_expansion(ctree, node);
584 ginfo = gtk_cmctree_node_get_row_data(ctree, node);
585 if (!ginfo) return TRUE;
587 list = g_slist_find_custom(subscribed, ginfo->name,
588 (GCompareFunc)g_ascii_strcasecmp);
591 subscribed = g_slist_remove(subscribed, list->data);
592 gtk_cmclist_unselect_row(GTK_CMCLIST(ctree), row, 0);
594 subscribed = g_slist_append(subscribed, g_strdup(ginfo->name));
595 gtk_cmclist_select_row(GTK_CMCLIST(ctree), row, 0);
601 static void entry_activated(GtkEditable *editable)
606 static void search_clicked(GtkWidget *widget, gpointer data)