2005-08-04 [colin] 1.9.13cvs13
[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 #include <glib/gi18n.h>
28
29 #include <gtk/gtk.h>
30
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 "prefs_common.h"
39 #include "news_gtk.h"
40 #include "common/hooks.h"
41 #include "inc.h"
42
43 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
44 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
45 static void news_settings_cb(FolderView *folderview, guint action, GtkWidget *widget);
46 static void remove_news_server_cb(FolderView *folderview, guint action, GtkWidget *widget);
47 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
48 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
49
50 static GtkItemFactoryEntry news_popup_entries[] =
51 {
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_("/Down_load"),                      NULL, download_cb,               0, NULL},
56         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
57         {N_("/_Check for new messages"),        NULL, update_tree_cb,            0, NULL},
58         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
59         {N_("/News _account settings"),         NULL, news_settings_cb,          0, NULL},
60         {N_("/Remove _news account"),           NULL, remove_news_server_cb,     0, NULL},
61         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
62 };
63
64 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
65
66 static FolderViewPopup news_popup =
67 {
68         "news",
69         "<NewsFolder>",
70         NULL,
71         set_sensitivity
72 };
73
74 gboolean news_gtk_should_override(void)
75 {
76         static time_t overridden_yes = 0;
77         static time_t overridden_no  = 0;
78         gboolean answer = TRUE;
79
80         if (prefs_common.work_offline) {
81                 if (time(NULL) - overridden_yes < 600)
82                          return TRUE;
83                 else if (time(NULL) - overridden_no < 3)
84                          return FALSE;
85                 
86                 answer = (alertpanel(_("Offline warning"), 
87                                _("You're working offline. Override during 10 minutes?"),
88                                GTK_STOCK_YES, GTK_STOCK_NO, NULL) == G_ALERTDEFAULT);
89                 
90                 if (answer == TRUE)
91                         overridden_yes = time(NULL);
92                 else
93                         overridden_no  = time(NULL);
94         }
95         return answer;
96 }
97
98 void news_gtk_init(void)
99 {
100         guint i, n_entries;
101
102         n_entries = sizeof(news_popup_entries) /
103                 sizeof(news_popup_entries[0]);
104         for (i = 0; i < n_entries; i++)
105                 news_popup.entries = g_slist_append(news_popup.entries, &news_popup_entries[i]);
106
107         folderview_register_popup(&news_popup);
108 }
109
110 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
111 {
112 #define SET_SENS(name, sens) \
113         menu_set_sensitive(factory, name, sens)
114
115         SET_SENS("/Subscribe to newsgroup...", folder_item_parent(item) == NULL);
116         SET_SENS("/Unsubscribe newsgroup",     folder_item_parent(item) != NULL);
117         SET_SENS("/Remove news account",       folder_item_parent(item) == NULL);
118
119         SET_SENS("/Check for new messages",    folder_item_parent(item) == NULL);
120
121 #undef SET_SENS
122 }
123
124 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
125 {
126         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
127         GtkCTreeNode *servernode, *node;
128         Folder *folder;
129         FolderItem *item;
130         FolderItem *rootitem;
131         FolderItem *newitem;
132         GSList *new_subscr;
133         GSList *cur;
134         GNode *gnode;
135
136         if (!folderview->selected) return;
137
138         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
139         g_return_if_fail(item != NULL);
140         folder = item->folder;
141         g_return_if_fail(folder != NULL);
142         g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
143         g_return_if_fail(folder->account != NULL);
144
145         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
146                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
147         else
148                 servernode = folderview->selected;
149
150         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
151
152         new_subscr = grouplist_dialog(folder);
153
154         /* remove unsubscribed newsgroups */
155         for (gnode = folder->node->children; gnode != NULL; ) {
156                 GNode *next = gnode->next;
157
158                 item = FOLDER_ITEM(gnode->data);
159                 if (g_slist_find_custom(new_subscr, item->path,
160                                         (GCompareFunc)g_ascii_strcasecmp) != NULL) {
161                         gnode = next;
162                         continue;
163                 }
164
165                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
166                 if (!node) {
167                         gnode = next;
168                         continue;
169                 }
170
171                 if (folderview->opened == node) {
172                         summary_clear_all(folderview->summaryview);
173                         folderview->opened = NULL;
174                 }
175
176                 gtk_ctree_remove_node(ctree, node);
177                 folder_item_remove(item);
178
179                 gnode = next;
180         }
181
182         gtk_clist_freeze(GTK_CLIST(ctree));
183
184         /* add subscribed newsgroups */
185         for (cur = new_subscr; cur != NULL; cur = cur->next) {
186                 gchar *name = (gchar *)cur->data;
187                 FolderUpdateData hookdata;
188
189                 if (folder_find_child_item_by_name(rootitem, name) != NULL)
190                         continue;
191
192                 newitem = folder_item_new(folder, name, name);
193                 folder_item_append(rootitem, newitem);
194
195                 hookdata.folder = newitem->folder;
196                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
197                 hookdata.item = newitem;
198                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
199         }
200
201         gtk_clist_thaw(GTK_CLIST(ctree));
202
203         slist_free_strings(new_subscr);
204         g_slist_free(new_subscr);
205
206         folder_write_list();
207 }
208
209 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
210                                      GtkWidget *widget)
211 {
212         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
213         FolderItem *item;
214         gchar *name;
215         gchar *message;
216         gchar *old_id;
217         AlertValue avalue;
218
219         if (!folderview->selected) return;
220
221         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
222         g_return_if_fail(item != NULL);
223         g_return_if_fail(item->folder != NULL);
224         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
225         g_return_if_fail(item->folder->account != NULL);
226
227         old_id = folder_item_get_identifier(item);
228
229         name = trim_string(item->path, 32);
230         message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
231         avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
232                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
233                                  NULL, ALERT_WARNING, G_ALERTALTERNATE);
234         g_free(message);
235         g_free(name);
236         if (avalue != G_ALERTDEFAULT) return;
237
238         if (folderview->opened == folderview->selected) {
239                 summary_clear_all(folderview->summaryview);
240                 folderview->opened = NULL;
241         }
242
243         folder_item_remove(item);
244         folder_write_list();
245         
246         prefs_filtering_delete_path(old_id);
247         g_free(old_id);
248 }
249
250 static void news_settings_cb(FolderView *folderview, guint action, GtkWidget *widget)
251 {
252         FolderItem *item;
253
254         item = folderview_get_selected_item(folderview);
255         if (item == NULL)
256                 return;
257
258         account_open(item->folder->account);
259 }
260
261 static void remove_news_server_cb(FolderView *folderview, guint action,
262                                   GtkWidget *widget)
263 {
264         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
265         FolderItem *item;
266         PrefsAccount *account;
267         gchar *name;
268         gchar *message;
269         AlertValue avalue;
270
271         if (!folderview->selected) return;
272
273         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
274         g_return_if_fail(item != NULL);
275         g_return_if_fail(item->folder != NULL);
276         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
277         g_return_if_fail(item->folder->account != NULL);
278
279         name = trim_string(item->folder->name, 32);
280         message = g_strdup_printf(_("Really delete news account '%s'?"), name);
281         avalue = alertpanel_full(_("Delete news account"), message,
282                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
283                                  NULL, ALERT_WARNING, G_ALERTALTERNATE);
284         g_free(message);
285         g_free(name);
286
287         if (avalue != G_ALERTDEFAULT) return;
288
289         if (folderview->opened == folderview->selected ||
290             gtk_ctree_is_ancestor(ctree,
291                                   folderview->selected,
292                                   folderview->opened)) {
293                 summary_clear_all(folderview->summaryview);
294                 folderview->opened = NULL;
295         }
296
297         account = item->folder->account;
298         folderview_unselect(folderview);
299         summary_clear_all(folderview->summaryview);
300         folder_destroy(item->folder);
301         account_destroy(account);
302         account_set_menu();
303         main_window_reflect_prefs_all();
304         folder_write_list();
305 }
306
307 static void update_tree_cb(FolderView *folderview, guint action,
308                            GtkWidget *widget)
309 {
310         FolderItem *item;
311
312         item = folderview_get_selected_item(folderview);
313         g_return_if_fail(item != NULL);
314
315         summary_show(folderview->summaryview, NULL);
316
317         g_return_if_fail(item->folder != NULL);
318
319         folderview_check_new(item->folder);
320 }
321
322 static void download_cb(FolderView *folderview, guint action,
323                         GtkWidget *widget)
324 {
325         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
326         MainWindow *mainwin = folderview->mainwin;
327         FolderItem *item;
328
329         if (!folderview->selected) return;
330
331         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
332         g_return_if_fail(item != NULL);
333         g_return_if_fail(item->folder != NULL);
334
335         if (prefs_common.work_offline && !news_gtk_should_override())
336                 return;
337
338         main_window_cursor_wait(mainwin);
339         inc_lock();
340         main_window_lock(mainwin);
341         gtk_widget_set_sensitive(folderview->ctree, FALSE);
342         main_window_progress_on(mainwin);
343         GTK_EVENTS_FLUSH();
344         if (folder_item_fetch_all_msg(item) < 0) {
345                 gchar *name;
346
347                 name = trim_string(item->name, 32);
348                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
349                 g_free(name);
350         }
351         folder_set_ui_func(item->folder, NULL, NULL);
352         main_window_progress_off(mainwin);
353         gtk_widget_set_sensitive(folderview->ctree, TRUE);
354         main_window_unlock(mainwin);
355         inc_unlock();
356         main_window_cursor_normal(mainwin);
357 }