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