99d85d1506ff3eb95e94b8b12bd1ef9030ebf86d
[claws.git] / src / news_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto & the Sylpheed-Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27
28 #include <gtk/gtk.h>
29
30 #include "intl.h"
31 #include "utils.h"
32 #include "folder.h"
33 #include "folderview.h"
34 #include "menu.h"
35 #include "account.h"
36 #include "alertpanel.h"
37 #include "grouplistdialog.h"
38 #include "common/hooks.h"
39 #include "inc.h"
40
41 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
42 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
43 static void remove_news_server_cb(FolderView *folderview, guint action, GtkWidget *widget);
44 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
45 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
46
47 static GtkItemFactoryEntry news_popup_entries[] =
48 {
49         {N_("/_Subscribe to newsgroup..."),     NULL, subscribe_newsgroup_cb,    0, NULL},
50         {N_("/_Unsubscribe newsgroup"),         NULL, unsubscribe_newsgroup_cb,  0, NULL},
51         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
52         {N_("/Down_load"),                      NULL, download_cb,               0, NULL},
53         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
54         {N_("/_Check for new messages"),        NULL, update_tree_cb,            0, NULL},
55         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
56         {N_("/Remove _news account"),           NULL, remove_news_server_cb,     0, NULL},
57         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
58 };
59
60 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
61
62 static FolderViewPopup news_popup =
63 {
64         "news",
65         "<NewsFolder>",
66         NULL,
67         set_sensitivity
68 };
69
70 void news_gtk_init(void)
71 {
72         guint i, n_entries;
73
74         n_entries = sizeof(news_popup_entries) /
75                 sizeof(news_popup_entries[0]);
76         for (i = 0; i < n_entries; i++)
77                 news_popup.entries = g_slist_append(news_popup.entries, &news_popup_entries[i]);
78
79         folderview_register_popup(&news_popup);
80 }
81
82 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
83 {
84 #define SET_SENS(name, sens) \
85         menu_set_sensitive(factory, name, sens)
86
87         SET_SENS("/Subscribe to newsgroup...", folder_item_parent(item) == NULL);
88         SET_SENS("/Unsubscribe newsgroup",     folder_item_parent(item) != NULL);
89         SET_SENS("/Remove news account",       folder_item_parent(item) == NULL);
90
91         SET_SENS("/Check for new messages",    folder_item_parent(item) == NULL);
92
93 #undef SET_SENS
94 }
95
96 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
97 {
98         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
99         GtkCTreeNode *servernode, *node;
100         Folder *folder;
101         FolderItem *item;
102         FolderItem *rootitem;
103         FolderItem *newitem;
104         GSList *new_subscr;
105         GSList *cur;
106         GNode *gnode;
107
108         if (!folderview->selected) return;
109
110         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
111         g_return_if_fail(item != NULL);
112         folder = item->folder;
113         g_return_if_fail(folder != NULL);
114         g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
115         g_return_if_fail(folder->account != NULL);
116
117         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
118                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
119         else
120                 servernode = folderview->selected;
121
122         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
123
124         new_subscr = grouplist_dialog(folder);
125
126         /* remove unsubscribed newsgroups */
127         for (gnode = folder->node->children; gnode != NULL; ) {
128                 GNode *next = gnode->next;
129
130                 item = FOLDER_ITEM(gnode->data);
131                 if (g_slist_find_custom(new_subscr, item->path,
132                                         (GCompareFunc)g_strcasecmp) != NULL) {
133                         gnode = next;
134                         continue;
135                 }
136
137                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
138                 if (!node) {
139                         gnode = next;
140                         continue;
141                 }
142
143                 if (folderview->opened == node) {
144                         summary_clear_all(folderview->summaryview);
145                         folderview->opened = NULL;
146                 }
147
148                 gtk_ctree_remove_node(ctree, node);
149                 folder_item_remove(item);
150
151                 gnode = next;
152         }
153
154         gtk_clist_freeze(GTK_CLIST(ctree));
155
156         /* add subscribed newsgroups */
157         for (cur = new_subscr; cur != NULL; cur = cur->next) {
158                 gchar *name = (gchar *)cur->data;
159                 FolderUpdateData hookdata;
160
161                 if (folder_find_child_item_by_name(rootitem, name) != NULL)
162                         continue;
163
164                 newitem = folder_item_new(folder, name, name);
165                 folder_item_append(rootitem, newitem);
166
167                 hookdata.folder = newitem->folder;
168                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
169                 hookdata.item = newitem;
170                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
171         }
172
173         gtk_clist_thaw(GTK_CLIST(ctree));
174
175         slist_free_strings(new_subscr);
176         g_slist_free(new_subscr);
177
178         folder_write_list();
179 }
180
181 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
182                                      GtkWidget *widget)
183 {
184         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
185         FolderItem *item;
186         gchar *name;
187         gchar *message;
188         gchar *old_id;
189         AlertValue avalue;
190
191         if (!folderview->selected) return;
192
193         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
194         g_return_if_fail(item != NULL);
195         g_return_if_fail(item->folder != NULL);
196         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
197         g_return_if_fail(item->folder->account != NULL);
198
199         old_id = folder_item_get_identifier(item);
200
201         name = trim_string(item->path, 32);
202         message = g_strdup_printf(_("Really unsubscribe newsgroup `%s'?"), name);
203         avalue = alertpanel(_("Unsubscribe newsgroup"), message,
204                             _("Yes"), _("+No"), NULL);
205         g_free(message);
206         g_free(name);
207         if (avalue != G_ALERTDEFAULT) return;
208
209         if (folderview->opened == folderview->selected) {
210                 summary_clear_all(folderview->summaryview);
211                 folderview->opened = NULL;
212         }
213
214         folder_item_remove(item);
215         folder_write_list();
216         
217         prefs_filtering_delete_path(old_id);
218         g_free(old_id);
219 }
220
221 static void remove_news_server_cb(FolderView *folderview, guint action,
222                                   GtkWidget *widget)
223 {
224         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
225         FolderItem *item;
226         PrefsAccount *account;
227         gchar *name;
228         gchar *message;
229         AlertValue avalue;
230
231         if (!folderview->selected) return;
232
233         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
234         g_return_if_fail(item != NULL);
235         g_return_if_fail(item->folder != NULL);
236         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
237         g_return_if_fail(item->folder->account != NULL);
238
239         name = trim_string(item->folder->name, 32);
240         message = g_strdup_printf(_("Really delete news account `%s'?"), name);
241         avalue = alertpanel(_("Delete news account"), message,
242                             _("Yes"), _("+No"), NULL);
243         g_free(message);
244         g_free(name);
245
246         if (avalue != G_ALERTDEFAULT) return;
247
248         if (folderview->opened == folderview->selected ||
249             gtk_ctree_is_ancestor(ctree,
250                                   folderview->selected,
251                                   folderview->opened)) {
252                 summary_clear_all(folderview->summaryview);
253                 folderview->opened = NULL;
254         }
255
256         account = item->folder->account;
257         folderview_unselect(folderview);
258         summary_clear_all(folderview->summaryview);
259         folder_destroy(item->folder);
260         account_destroy(account);
261         account_set_menu();
262         main_window_reflect_prefs_all();
263         folder_write_list();
264 }
265
266 static void update_tree_cb(FolderView *folderview, guint action,
267                            GtkWidget *widget)
268 {
269         FolderItem *item;
270
271         item = folderview_get_selected_item(folderview);
272         g_return_if_fail(item != NULL);
273
274         summary_show(folderview->summaryview, NULL);
275
276         g_return_if_fail(item->folder != NULL);
277
278         folderview_check_new(item->folder);
279 }
280
281 static void download_cb(FolderView *folderview, guint action,
282                         GtkWidget *widget)
283 {
284         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
285         MainWindow *mainwin = folderview->mainwin;
286         FolderItem *item;
287
288         if (!folderview->selected) return;
289
290         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
291         g_return_if_fail(item != NULL);
292         g_return_if_fail(item->folder != NULL);
293 #if 0
294         if (!prefs_common.online_mode) {
295                 if (alertpanel(_("Offline"),
296                                _("You are offline. Go online?"),
297                                _("Yes"), _("No"), NULL) == G_ALERTDEFAULT)
298                         main_window_toggle_online(folderview->mainwin, TRUE);
299                 else
300                         return;
301         }
302 #endif
303         main_window_cursor_wait(mainwin);
304         inc_lock();
305         main_window_lock(mainwin);
306         gtk_widget_set_sensitive(folderview->ctree, FALSE);
307         main_window_progress_on(mainwin);
308         GTK_EVENTS_FLUSH();
309         if (folder_item_fetch_all_msg(item) < 0) {
310                 gchar *name;
311
312                 name = trim_string(item->name, 32);
313                 alertpanel_error(_("Error occurred while downloading messages in `%s'."), name);
314                 g_free(name);
315         }
316         folder_set_ui_func(item->folder, NULL, NULL);
317         main_window_progress_off(mainwin);
318         gtk_widget_set_sensitive(folderview->ctree, TRUE);
319         main_window_unlock(mainwin);
320         inc_unlock();
321         main_window_cursor_normal(mainwin);
322 }