2007-01-17 [colin] 2.7.1cvs16
[claws.git] / src / news_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto & the Claws Mail 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 #include "news.h"
43
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);
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         {"/---",                                NULL, NULL,                      0, "<Separator>"},
55         {N_("/Synchronise"),                    NULL, sync_cb,          0, NULL},
56         {N_("/Down_load messages"),             NULL, download_cb,               0, NULL},
57         {"/---",                                NULL, NULL,                      0, "<Separator>"},
58         {N_("/_Check for new messages"),        NULL, update_tree_cb,            0, NULL},
59         {"/---",                                NULL, NULL,                      0, "<Separator>"},
60 };
61
62 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
63
64 static FolderViewPopup news_popup =
65 {
66         "news",
67         "<NewsFolder>",
68         NULL,
69         set_sensitivity
70 };
71
72 void news_gtk_init(void)
73 {
74         guint i, n_entries;
75
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]);
80
81         folderview_register_popup(&news_popup);
82 }
83
84 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
85 {
86         MainWindow *mainwin = mainwindow_get_mainwindow();
87         
88 #define SET_SENS(name, sens) \
89         menu_set_sensitive(factory, name, sens)
90
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);
103         SET_SENS("/Synchronise",    
104                  item ? (folder_item_parent(item) != NULL && folder_want_synchronise(item->folder))
105                          : FALSE);
106
107 #undef SET_SENS
108 }
109
110 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
111 {
112         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
113         GtkCTreeNode *servernode, *node;
114         Folder *folder;
115         FolderItem *item;
116         FolderItem *rootitem;
117         FolderItem *newitem;
118         GSList *new_subscr;
119         GSList *cur;
120         GNode *gnode;
121         MainWindow *mainwin = mainwindow_get_mainwindow();
122         
123         if (!folderview->selected) return;
124
125         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
126         g_return_if_fail(item != NULL);
127
128         if (mainwin->lock_count || news_folder_locked(item->folder))
129                 return;
130
131         folder = item->folder;
132         g_return_if_fail(folder != NULL);
133         g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
134         g_return_if_fail(folder->account != NULL);
135
136         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
137                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
138         else
139                 servernode = folderview->selected;
140
141         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
142
143         new_subscr = grouplist_dialog(folder);
144
145         /* remove unsubscribed newsgroups */
146         for (gnode = folder->node->children; gnode != NULL; ) {
147                 GNode *next = gnode->next;
148
149                 item = FOLDER_ITEM(gnode->data);
150                 if (g_slist_find_custom(new_subscr, item->path,
151                                         (GCompareFunc)g_ascii_strcasecmp) != NULL) {
152                         gnode = next;
153                         continue;
154                 }
155
156                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
157                 if (!node) {
158                         gnode = next;
159                         continue;
160                 }
161
162                 if (folderview->opened == node) {
163                         summary_clear_all(folderview->summaryview);
164                         folderview->opened = NULL;
165                 }
166
167                 gtk_ctree_remove_node(ctree, node);
168                 folder_item_remove(item);
169
170                 gnode = next;
171         }
172
173         gtk_clist_freeze(GTK_CLIST(ctree));
174
175         /* add subscribed newsgroups */
176         for (cur = new_subscr; cur != NULL; cur = cur->next) {
177                 gchar *name = (gchar *)cur->data;
178                 FolderUpdateData hookdata;
179
180                 if (folder_find_child_item_by_name(rootitem, name) != NULL)
181                         continue;
182
183                 newitem = folder_item_new(folder, name, name);
184                 folder_item_append(rootitem, newitem);
185
186                 hookdata.folder = newitem->folder;
187                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
188                 hookdata.item = newitem;
189                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
190         }
191
192         gtk_clist_thaw(GTK_CLIST(ctree));
193
194         slist_free_strings(new_subscr);
195         g_slist_free(new_subscr);
196
197         folder_write_list();
198 }
199
200 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
201                                      GtkWidget *widget)
202 {
203         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
204         FolderItem *item;
205         gchar *name;
206         gchar *message;
207         gchar *old_id;
208         AlertValue avalue;
209         MainWindow *mainwin = mainwindow_get_mainwindow();
210         
211         if (!folderview->selected) return;
212
213         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
214         g_return_if_fail(item != NULL);
215
216         if (mainwin->lock_count || news_folder_locked(item->folder))
217                 return;
218
219         g_return_if_fail(item->folder != NULL);
220         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
221         g_return_if_fail(item->folder->account != NULL);
222
223         old_id = folder_item_get_identifier(item);
224
225         name = trim_string(item->path, 32);
226         message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
227         avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
228                                  GTK_STOCK_CANCEL, _("_Unsubscribe"), NULL, FALSE,
229                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
230         g_free(message);
231         g_free(name);
232         if (avalue != G_ALERTALTERNATE) return;
233
234         if (folderview->opened == folderview->selected) {
235                 summary_clear_all(folderview->summaryview);
236                 folderview->opened = NULL;
237         }
238
239         if(item->folder->klass->remove_folder(item->folder, item) < 0) {
240                 folder_item_scan(item);
241                 alertpanel_error(_("Can't remove the folder '%s'."), name);
242                 g_free(old_id);
243                 return;
244         }
245         
246         folder_write_list();
247         
248         prefs_filtering_delete_path(old_id);
249         g_free(old_id);
250 }
251
252 static void update_tree_cb(FolderView *folderview, guint action,
253                            GtkWidget *widget)
254 {
255         FolderItem *item;
256         MainWindow *mainwin = mainwindow_get_mainwindow();
257         
258         item = folderview_get_selected_item(folderview);
259         g_return_if_fail(item != NULL);
260
261         if (mainwin->lock_count || news_folder_locked(item->folder))
262                 return;
263
264         summary_show(folderview->summaryview, NULL);
265
266         g_return_if_fail(item->folder != NULL);
267
268         folderview_check_new(item->folder);
269 }
270
271 static void sync_cb(FolderView *folderview, guint action,
272                            GtkWidget *widget)
273 {
274         FolderItem *item;
275
276         item = folderview_get_selected_item(folderview);
277         g_return_if_fail(item != NULL);
278         folder_synchronise(item->folder);
279 }
280
281 void news_gtk_synchronise(FolderItem *item)
282 {
283         MainWindow *mainwin = mainwindow_get_mainwindow();
284         FolderView *folderview = mainwin->folderview;
285         
286         g_return_if_fail(item != NULL);
287         g_return_if_fail(item->folder != NULL);
288
289         if (mainwin->lock_count || news_folder_locked(item->folder))
290                 return;
291
292         main_window_cursor_wait(mainwin);
293         inc_lock();
294         main_window_lock(mainwin);
295         gtk_widget_set_sensitive(folderview->ctree, FALSE);
296         main_window_progress_on(mainwin);
297         GTK_EVENTS_FLUSH();
298         if (folder_item_fetch_all_msg(item) < 0) {
299                 gchar *name;
300
301                 name = trim_string(item->name, 32);
302                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
303                 g_free(name);
304         }
305         folder_set_ui_func(item->folder, NULL, NULL);
306         main_window_progress_off(mainwin);
307         gtk_widget_set_sensitive(folderview->ctree, TRUE);
308         main_window_unlock(mainwin);
309         inc_unlock();
310         main_window_cursor_normal(mainwin);
311 }
312
313 static void download_cb(FolderView *folderview, guint action,
314                         GtkWidget *widget)
315 {
316         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
317         FolderItem *item;
318
319         if (!folderview->selected) return;
320
321         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
322         news_gtk_synchronise(item);
323 }