2005-10-26 [colin] 1.9.15cvs111
[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 update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
46 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
47 static void sync_cb(FolderView *folderview, guint action, GtkWidget *widget);
48
49 static GtkItemFactoryEntry news_popup_entries[] =
50 {
51         {N_("/_Subscribe to newsgroup..."),     NULL, subscribe_newsgroup_cb,    0, NULL},
52         {N_("/_Unsubscribe newsgroup"),         NULL, unsubscribe_newsgroup_cb,  0, NULL},
53         {N_("/---"),                            NULL, NULL,                      0, "<Separator>"},
54         {N_("/Synchronise"),                    NULL, sync_cb,          0, NULL},
55         {N_("/Down_load messages"),             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 };
60
61 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
62
63 static FolderViewPopup news_popup =
64 {
65         "news",
66         "<NewsFolder>",
67         NULL,
68         set_sensitivity
69 };
70
71 void news_gtk_init(void)
72 {
73         guint i, n_entries;
74
75         n_entries = sizeof(news_popup_entries) /
76                 sizeof(news_popup_entries[0]);
77         for (i = 0; i < n_entries; i++)
78                 news_popup.entries = g_slist_append(news_popup.entries, &news_popup_entries[i]);
79
80         folderview_register_popup(&news_popup);
81 }
82
83 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
84 {
85 #define SET_SENS(name, sens) \
86         menu_set_sensitive(factory, name, sens)
87
88         SET_SENS("/Subscribe to newsgroup...", folder_item_parent(item) == NULL);
89         SET_SENS("/Unsubscribe newsgroup",     folder_item_parent(item) != NULL);
90         SET_SENS("/Check for new messages",    folder_item_parent(item) == NULL);
91
92 #undef SET_SENS
93 }
94
95 static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
96 {
97         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
98         GtkCTreeNode *servernode, *node;
99         Folder *folder;
100         FolderItem *item;
101         FolderItem *rootitem;
102         FolderItem *newitem;
103         GSList *new_subscr;
104         GSList *cur;
105         GNode *gnode;
106
107         if (!folderview->selected) return;
108
109         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
110         g_return_if_fail(item != NULL);
111         folder = item->folder;
112         g_return_if_fail(folder != NULL);
113         g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
114         g_return_if_fail(folder->account != NULL);
115
116         if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
117                 servernode = GTK_CTREE_ROW(folderview->selected)->parent;
118         else
119                 servernode = folderview->selected;
120
121         rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
122
123         new_subscr = grouplist_dialog(folder);
124
125         /* remove unsubscribed newsgroups */
126         for (gnode = folder->node->children; gnode != NULL; ) {
127                 GNode *next = gnode->next;
128
129                 item = FOLDER_ITEM(gnode->data);
130                 if (g_slist_find_custom(new_subscr, item->path,
131                                         (GCompareFunc)g_ascii_strcasecmp) != NULL) {
132                         gnode = next;
133                         continue;
134                 }
135
136                 node = gtk_ctree_find_by_row_data(ctree, servernode, item);
137                 if (!node) {
138                         gnode = next;
139                         continue;
140                 }
141
142                 if (folderview->opened == node) {
143                         summary_clear_all(folderview->summaryview);
144                         folderview->opened = NULL;
145                 }
146
147                 gtk_ctree_remove_node(ctree, node);
148                 folder_item_remove(item);
149
150                 gnode = next;
151         }
152
153         gtk_clist_freeze(GTK_CLIST(ctree));
154
155         /* add subscribed newsgroups */
156         for (cur = new_subscr; cur != NULL; cur = cur->next) {
157                 gchar *name = (gchar *)cur->data;
158                 FolderUpdateData hookdata;
159
160                 if (folder_find_child_item_by_name(rootitem, name) != NULL)
161                         continue;
162
163                 newitem = folder_item_new(folder, name, name);
164                 folder_item_append(rootitem, newitem);
165
166                 hookdata.folder = newitem->folder;
167                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
168                 hookdata.item = newitem;
169                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
170         }
171
172         gtk_clist_thaw(GTK_CLIST(ctree));
173
174         slist_free_strings(new_subscr);
175         g_slist_free(new_subscr);
176
177         folder_write_list();
178 }
179
180 static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
181                                      GtkWidget *widget)
182 {
183         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
184         FolderItem *item;
185         gchar *name;
186         gchar *message;
187         gchar *old_id;
188         AlertValue avalue;
189
190         if (!folderview->selected) return;
191
192         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
193         g_return_if_fail(item != NULL);
194         g_return_if_fail(item->folder != NULL);
195         g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
196         g_return_if_fail(item->folder->account != NULL);
197
198         old_id = folder_item_get_identifier(item);
199
200         name = trim_string(item->path, 32);
201         message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
202         avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
203                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
204                                  NULL, ALERT_WARNING, G_ALERTALTERNATE);
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 update_tree_cb(FolderView *folderview, guint action,
222                            GtkWidget *widget)
223 {
224         FolderItem *item;
225
226         item = folderview_get_selected_item(folderview);
227         g_return_if_fail(item != NULL);
228
229         summary_show(folderview->summaryview, NULL);
230
231         g_return_if_fail(item->folder != NULL);
232
233         folderview_check_new(item->folder);
234 }
235
236 static void sync_cb(FolderView *folderview, guint action,
237                            GtkWidget *widget)
238 {
239         FolderItem *item;
240
241         item = folderview_get_selected_item(folderview);
242         g_return_if_fail(item != NULL);
243         folder_synchronise(item->folder);
244 }
245
246 void news_gtk_synchronise(FolderItem *item)
247 {
248         MainWindow *mainwin = mainwindow_get_mainwindow();
249         FolderView *folderview = mainwin->folderview;
250         
251         g_return_if_fail(item != NULL);
252         g_return_if_fail(item->folder != NULL);
253
254         main_window_cursor_wait(mainwin);
255         inc_lock();
256         main_window_lock(mainwin);
257         gtk_widget_set_sensitive(folderview->ctree, FALSE);
258         main_window_progress_on(mainwin);
259         GTK_EVENTS_FLUSH();
260         if (folder_item_fetch_all_msg(item) < 0) {
261                 gchar *name;
262
263                 name = trim_string(item->name, 32);
264                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
265                 g_free(name);
266         }
267         folder_set_ui_func(item->folder, NULL, NULL);
268         main_window_progress_off(mainwin);
269         gtk_widget_set_sensitive(folderview->ctree, TRUE);
270         main_window_unlock(mainwin);
271         inc_unlock();
272         main_window_cursor_normal(mainwin);
273 }
274
275 static void download_cb(FolderView *folderview, guint action,
276                         GtkWidget *widget)
277 {
278         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
279         FolderItem *item;
280
281         if (!folderview->selected) return;
282
283         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
284         news_gtk_synchronise(item);
285 }