2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws 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 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>
33 #include "folderview.h"
36 #include "alertpanel.h"
37 #include "grouplistdialog.h"
38 #include "prefs_common.h"
40 #include "common/hooks.h"
44 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
45 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
46 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
47 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
48 static void sync_cb(FolderView *folderview, guint action, GtkWidget *widget);
50 static GtkItemFactoryEntry news_popup_entries[] =
52 {N_("/_Subscribe to newsgroup..."), NULL, subscribe_newsgroup_cb, 0, NULL},
53 {N_("/_Unsubscribe newsgroup"), NULL, unsubscribe_newsgroup_cb, 0, NULL},
54 {N_("/---"), NULL, NULL, 0, "<Separator>"},
55 {N_("/Synchronise"), NULL, sync_cb, 0, NULL},
56 {N_("/Down_load messages"), NULL, download_cb, 0, NULL},
57 {N_("/---"), NULL, NULL, 0, "<Separator>"},
58 {N_("/_Check for new messages"), NULL, update_tree_cb, 0, NULL},
59 {N_("/---"), NULL, NULL, 0, "<Separator>"},
62 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
64 static FolderViewPopup news_popup =
72 void news_gtk_init(void)
76 n_entries = sizeof(news_popup_entries) /
77 sizeof(news_popup_entries[0]);
78 for (i = 0; i < n_entries; i++)
79 news_popup.entries = g_slist_append(news_popup.entries, &news_popup_entries[i]);
81 folderview_register_popup(&news_popup);
84 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
86 MainWindow *mainwin = mainwindow_get_mainwindow();
88 #define SET_SENS(name, sens) \
89 menu_set_sensitive(factory, name, sens)
91 SET_SENS("/Subscribe to newsgroup...",
92 folder_item_parent(item) == NULL
93 && mainwin->lock_count == 0
94 && news_folder_locked(item->folder) == 0);
95 SET_SENS("/Unsubscribe newsgroup",
96 folder_item_parent(item) != NULL
97 && mainwin->lock_count == 0
98 && news_folder_locked(item->folder) == 0);
99 SET_SENS("/Check for new messages",
100 folder_item_parent(item) == NULL
101 && mainwin->lock_count == 0
102 && news_folder_locked(item->folder) == 0);
107 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
109 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
110 GtkCTreeNode *servernode, *node;
113 FolderItem *rootitem;
118 MainWindow *mainwin = mainwindow_get_mainwindow();
120 if (!folderview->selected) return;
122 item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
123 g_return_if_fail(item != NULL);
125 if (mainwin->lock_count || news_folder_locked(item->folder))
128 folder = item->folder;
129 g_return_if_fail(folder != NULL);
130 g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
131 g_return_if_fail(folder->account != NULL);
133 if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
134 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
136 servernode = folderview->selected;
138 rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
140 new_subscr = grouplist_dialog(folder);
142 /* remove unsubscribed newsgroups */
143 for (gnode = folder->node->children; gnode != NULL; ) {
144 GNode *next = gnode->next;
146 item = FOLDER_ITEM(gnode->data);
147 if (g_slist_find_custom(new_subscr, item->path,
148 (GCompareFunc)g_ascii_strcasecmp) != NULL) {
153 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
159 if (folderview->opened == node) {
160 summary_clear_all(folderview->summaryview);
161 folderview->opened = NULL;
164 gtk_ctree_remove_node(ctree, node);
165 folder_item_remove(item);
170 gtk_clist_freeze(GTK_CLIST(ctree));
172 /* add subscribed newsgroups */
173 for (cur = new_subscr; cur != NULL; cur = cur->next) {
174 gchar *name = (gchar *)cur->data;
175 FolderUpdateData hookdata;
177 if (folder_find_child_item_by_name(rootitem, name) != NULL)
180 newitem = folder_item_new(folder, name, name);
181 folder_item_append(rootitem, newitem);
183 hookdata.folder = newitem->folder;
184 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
185 hookdata.item = newitem;
186 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
189 gtk_clist_thaw(GTK_CLIST(ctree));
191 slist_free_strings(new_subscr);
192 g_slist_free(new_subscr);
197 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
200 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
206 MainWindow *mainwin = mainwindow_get_mainwindow();
208 if (!folderview->selected) return;
210 item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
211 g_return_if_fail(item != NULL);
213 if (mainwin->lock_count || news_folder_locked(item->folder))
216 g_return_if_fail(item->folder != NULL);
217 g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
218 g_return_if_fail(item->folder->account != NULL);
220 old_id = folder_item_get_identifier(item);
222 name = trim_string(item->path, 32);
223 message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
224 avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
225 GTK_STOCK_CANCEL, _("_Unsubscribe"), NULL, FALSE,
226 NULL, ALERT_WARNING, G_ALERTDEFAULT);
229 if (avalue != G_ALERTALTERNATE) return;
231 if (folderview->opened == folderview->selected) {
232 summary_clear_all(folderview->summaryview);
233 folderview->opened = NULL;
236 if(item->folder->klass->remove_folder(item->folder, item) < 0) {
237 folder_item_scan(item);
238 alertpanel_error(_("Can't remove the folder '%s'."), name);
245 prefs_filtering_delete_path(old_id);
249 static void update_tree_cb(FolderView *folderview, guint action,
253 MainWindow *mainwin = mainwindow_get_mainwindow();
255 item = folderview_get_selected_item(folderview);
256 g_return_if_fail(item != NULL);
258 if (mainwin->lock_count || news_folder_locked(item->folder))
261 summary_show(folderview->summaryview, NULL);
263 g_return_if_fail(item->folder != NULL);
265 folderview_check_new(item->folder);
268 static void sync_cb(FolderView *folderview, guint action,
273 item = folderview_get_selected_item(folderview);
274 g_return_if_fail(item != NULL);
275 folder_synchronise(item->folder);
278 void news_gtk_synchronise(FolderItem *item)
280 MainWindow *mainwin = mainwindow_get_mainwindow();
281 FolderView *folderview = mainwin->folderview;
283 g_return_if_fail(item != NULL);
284 g_return_if_fail(item->folder != NULL);
286 if (mainwin->lock_count || news_folder_locked(item->folder))
289 main_window_cursor_wait(mainwin);
291 main_window_lock(mainwin);
292 gtk_widget_set_sensitive(folderview->ctree, FALSE);
293 main_window_progress_on(mainwin);
295 if (folder_item_fetch_all_msg(item) < 0) {
298 name = trim_string(item->name, 32);
299 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
302 folder_set_ui_func(item->folder, NULL, NULL);
303 main_window_progress_off(mainwin);
304 gtk_widget_set_sensitive(folderview->ctree, TRUE);
305 main_window_unlock(mainwin);
307 main_window_cursor_normal(mainwin);
310 static void download_cb(FolderView *folderview, guint action,
313 GtkCTree *ctree = GTK_CTREE(folderview->ctree);
316 if (!folderview->selected) return;
318 item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
319 news_gtk_synchronise(item);