2783f78c56ee2dda102a51ce1d139de3b7902849
[claws.git] / src / news_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include <gtk/gtk.h>
31
32 #include "utils.h"
33 #include "folder.h"
34 #include "folderview.h"
35 #include "menu.h"
36 #include "account.h"
37 #include "alertpanel.h"
38 #include "grouplistdialog.h"
39 #include "prefs_common.h"
40 #include "news_gtk.h"
41 #include "common/hooks.h"
42 #include "inc.h"
43 #include "news.h"
44 #include "statusbar.h"
45 #include "inputdialog.h"
46
47 static void subscribe_newsgroup_cb(GtkAction *action, gpointer data);
48 static void unsubscribe_newsgroup_cb(GtkAction *action, gpointer data);
49 static void rename_newsgroup_cb(GtkAction *action, gpointer data);
50 static void update_tree_cb(GtkAction *action, gpointer data);
51 static void download_cb(GtkAction *action, gpointer data);
52 static void sync_cb(GtkAction *action, gpointer data);
53
54 static GtkActionEntry news_popup_entries[] =
55 {
56         {"FolderViewPopup/Subscribe",           NULL, N_("_Subscribe to newsgroup..."), NULL, NULL, G_CALLBACK(subscribe_newsgroup_cb) },
57         {"FolderViewPopup/Unsubscribe",         NULL, N_("_Unsubscribe newsgroup"), NULL, NULL, G_CALLBACK(unsubscribe_newsgroup_cb) },
58
59         {"FolderViewPopup/Synchronise",         NULL, N_("Synchronise"), NULL, NULL, G_CALLBACK(sync_cb) },
60         {"FolderViewPopup/DownloadMessages",    NULL, N_("Down_load messages"), NULL, NULL, G_CALLBACK(download_cb) },
61         {"FolderViewPopup/RenameFolder",        NULL, N_("_Rename folder..."), NULL, NULL, G_CALLBACK(rename_newsgroup_cb) },
62
63         {"FolderViewPopup/CheckNewMessages",    NULL, N_("_Check for new messages"), NULL, NULL, G_CALLBACK(update_tree_cb) },
64
65 };
66
67 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
68 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
69
70 static FolderViewPopup news_popup =
71 {
72         "news",
73         "<NewsFolder>",
74         news_popup_entries,
75         G_N_ELEMENTS(news_popup_entries),
76         NULL, 0,
77         NULL, 0, 0, NULL,
78         add_menuitems,
79         set_sensitivity
80 };
81
82 void news_gtk_init(void)
83 {
84         folderview_register_popup(&news_popup);
85 }
86
87 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
88 {
89         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Subscribe", "FolderViewPopup/Subscribe", GTK_UI_MANAGER_MENUITEM)
90         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Unsubscribe", "FolderViewPopup/Unsubscribe", GTK_UI_MANAGER_MENUITEM)
91         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorNews1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
92         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Synchronise", "FolderViewPopup/Synchronise", GTK_UI_MANAGER_MENUITEM)
93         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DownloadMessages", "FolderViewPopup/DownloadMessages", GTK_UI_MANAGER_MENUITEM)
94         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
95         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorNews2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
96         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewMessages", "FolderViewPopup/CheckNewMessages", GTK_UI_MANAGER_MENUITEM)
97         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorNews3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
98 }
99         
100 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
101 {
102         MainWindow *mainwin = mainwindow_get_mainwindow();
103         
104 #define SET_SENS(name, sens) \
105         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
106
107         SET_SENS("FolderViewPopup/Subscribe", 
108                         folder_item_parent(item) == NULL 
109                         && mainwin->lock_count == 0
110                         && news_folder_locked(item->folder) == 0);
111         SET_SENS("FolderViewPopup/Unsubscribe",     
112                         folder_item_parent(item) != NULL 
113                         && mainwin->lock_count == 0
114                         && news_folder_locked(item->folder) == 0);
115         SET_SENS("FolderViewPopup/CheckNewMessages",    
116                         folder_item_parent(item) == NULL 
117                         && mainwin->lock_count == 0
118                         && news_folder_locked(item->folder) == 0);
119         SET_SENS("FolderViewPopup/Synchronise",    
120                         item ? (folder_item_parent(item) != NULL
121                         && folder_want_synchronise(item->folder))
122                         : FALSE);
123         SET_SENS("FolderViewPopup/DownloadMessages",
124                         item ? (folder_item_parent(item) != NULL
125                         && !item->no_select)
126                         : FALSE);
127         SET_SENS("FolderViewPopup/RenameFolder",
128                         folder_item_parent(item) != NULL 
129                         && mainwin->lock_count == 0
130                         && news_folder_locked(item->folder) == 0);
131 #undef SET_SENS
132 }
133
134 static FolderItem *news_find_child_item(FolderItem *item, const gchar *path)
135 {
136         GNode *node;
137         FolderItem *child;
138
139         for (node = item->node->children; node != NULL; node = node->next) {
140                 child = FOLDER_ITEM(node->data);
141                 if (strcmp2(child->path, path) == 0) {
142                         return child;
143                 }
144         }
145
146         return NULL;
147 }
148
149 static void subscribe_newsgroup_cb(GtkAction *action, gpointer data)
150 {
151         FolderView *folderview = (FolderView *)data;
152         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
153         GtkCMCTreeNode *servernode, *node;
154         Folder *folder;
155         FolderItem *item;
156         FolderItem *rootitem;
157         FolderItem *newitem;
158         GSList *new_subscr;
159         GSList *cur;
160         GNode *gnode;
161         MainWindow *mainwin = mainwindow_get_mainwindow();
162         
163         if (!folderview->selected) return;
164
165         item = folderview_get_selected_item(folderview);
166         cm_return_if_fail(item != NULL);
167
168         if (mainwin->lock_count || news_folder_locked(item->folder))
169                 return;
170
171         folder = item->folder;
172         cm_return_if_fail(folder != NULL);
173         cm_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
174         cm_return_if_fail(folder->account != NULL);
175
176         if (GTK_CMCTREE_ROW(folderview->selected)->parent != NULL)
177                 servernode = GTK_CMCTREE_ROW(folderview->selected)->parent;
178         else
179                 servernode = folderview->selected;
180
181         rootitem = gtk_cmctree_node_get_row_data(ctree, servernode);
182
183         new_subscr = grouplist_dialog(folder);
184
185         /* remove unsubscribed newsgroups */
186         for (gnode = folder->node->children; gnode != NULL; ) {
187                 GNode *next = gnode->next;
188
189                 item = FOLDER_ITEM(gnode->data);
190                 if (g_slist_find_custom(new_subscr, item->path,
191                                         (GCompareFunc)g_ascii_strcasecmp) != NULL) {
192                         gnode = next;
193                         continue;
194                 }
195
196                 node = gtk_cmctree_find_by_row_data(ctree, servernode, item);
197                 if (!node) {
198                         gnode = next;
199                         continue;
200                 }
201
202                 if (folderview->opened == node) {
203                         summary_clear_all(folderview->summaryview);
204                         folderview->opened = NULL;
205                 }
206
207                 gtk_cmctree_remove_node(ctree, node);
208                 folder_item_remove(item);
209
210                 gnode = next;
211         }
212
213         gtk_cmclist_freeze(GTK_CMCLIST(ctree));
214
215         /* add subscribed newsgroups */
216         for (cur = new_subscr; cur != NULL; cur = cur->next) {
217                 gchar *name = (gchar *)cur->data;
218                 FolderUpdateData hookdata;
219
220                 if (news_find_child_item(rootitem, name) != NULL)
221                         continue;
222
223                 newitem = folder_item_new(folder, name, name);
224                 folder_item_append(rootitem, newitem);
225
226                 hookdata.folder = newitem->folder;
227                 hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
228                 hookdata.item = newitem;
229                 hookdata.item2 = NULL;
230                 hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
231         }
232
233         gtk_cmclist_thaw(GTK_CMCLIST(ctree));
234
235         slist_free_strings_full(new_subscr);
236
237         folder_write_list();
238 }
239
240 static void unsubscribe_newsgroup_cb(GtkAction *action, gpointer data)
241 {
242         FolderView *folderview = (FolderView *)data;
243         FolderItem *item;
244         gchar *name;
245         gchar *message;
246         gchar *old_id;
247         AlertValue avalue;
248         MainWindow *mainwin = mainwindow_get_mainwindow();
249         
250         if (!folderview->selected) return;
251
252         item = folderview_get_selected_item(folderview);
253         cm_return_if_fail(item != NULL);
254
255         if (mainwin->lock_count || news_folder_locked(item->folder))
256                 return;
257
258         cm_return_if_fail(item->folder != NULL);
259         cm_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
260         cm_return_if_fail(item->folder->account != NULL);
261
262         old_id = folder_item_get_identifier(item);
263
264         name = trim_string(item->path, 32);
265         message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
266         avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
267                                  GTK_STOCK_CANCEL, _("_Unsubscribe"), NULL, FALSE,
268                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
269         g_free(message);
270         g_free(name);
271         if (avalue != G_ALERTALTERNATE) return;
272
273         if (folderview->opened == folderview->selected) {
274                 summary_clear_all(folderview->summaryview);
275                 folderview->opened = NULL;
276         }
277
278         if(item->folder->klass->remove_folder(item->folder, item) < 0) {
279                 folder_item_scan(item);
280                 alertpanel_error(_("Can't remove the folder '%s'."), name);
281                 g_free(old_id);
282                 return;
283         }
284         
285         folder_write_list();
286         
287         prefs_filtering_delete_path(old_id);
288         g_free(old_id);
289 }
290
291 static void rename_newsgroup_cb(GtkAction *action, gpointer data)
292 {
293         FolderView *folderview = (FolderView *)data;
294         FolderItem *item;
295         gchar *new_folder;
296         gchar *name;
297         gchar *message;
298
299         item = folderview_get_selected_item(folderview);
300         cm_return_if_fail(item != NULL);
301         cm_return_if_fail(item->path != NULL);
302         cm_return_if_fail(item->folder != NULL);
303
304         name = trim_string(item->name, 32);
305         message = g_strdup_printf(_("Input new name for '%s':"), name);
306         new_folder = input_dialog(_("Rename newsgroup folder"), message, item->name);
307         g_free(message);
308         g_free(name);
309
310         if (!new_folder) return;
311         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
312
313         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
314                 alertpanel_error(_("'%c' can't be included in folder name."),
315                                  G_DIR_SEPARATOR);
316                 return;
317         }
318
319         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
320                 name = trim_string(new_folder, 32);
321                 alertpanel_error(_("The folder '%s' already exists."), name);
322                 g_free(name);
323                 return;
324         }
325
326         if (folder_item_rename(item, new_folder) < 0) {
327                 alertpanel_error(_("The folder could not be renamed.\n"
328                                    "The new folder name is not allowed."));
329                 return;
330         }
331
332         folder_write_list();
333 }
334
335 static void update_tree_cb(GtkAction *action, gpointer data)
336 {
337         FolderView *folderview = (FolderView *)data;
338         FolderItem *item;
339         MainWindow *mainwin = mainwindow_get_mainwindow();
340         
341         item = folderview_get_selected_item(folderview);
342         cm_return_if_fail(item != NULL);
343
344         if (mainwin->lock_count || news_folder_locked(item->folder))
345                 return;
346
347         summary_show(folderview->summaryview, NULL);
348
349         cm_return_if_fail(item->folder != NULL);
350
351         folderview_check_new(item->folder);
352 }
353
354 static void sync_cb(GtkAction *action, gpointer data)
355 {
356         FolderView *folderview = (FolderView *)data;
357         FolderItem *item;
358
359         item = folderview_get_selected_item(folderview);
360         cm_return_if_fail(item != NULL);
361         folder_synchronise(item->folder);
362 }
363
364 void news_gtk_synchronise(FolderItem *item, gint days)
365 {
366         MainWindow *mainwin = mainwindow_get_mainwindow();
367         FolderView *folderview = mainwin->folderview;
368         GSList *mlist;
369         GSList *cur;
370         gint num = 0;
371         gint total = 0;
372         time_t t = time(NULL);
373
374         cm_return_if_fail(item != NULL);
375         cm_return_if_fail(item->folder != NULL);
376
377         if (mainwin->lock_count || news_folder_locked(item->folder))
378                 return;
379
380         total = item->total_msgs;
381
382         main_window_cursor_wait(mainwin);
383         inc_lock();
384         main_window_lock(mainwin);
385         gtk_widget_set_sensitive(folderview->ctree, FALSE);
386         main_window_progress_on(mainwin);
387         GTK_EVENTS_FLUSH();
388
389         mlist = folder_item_get_msg_list(item);
390         for (cur = mlist; cur != NULL; cur = cur->next) {
391                 MsgInfo *msginfo = (MsgInfo *)cur->data;
392                 gint age = (t - msginfo->date_t) / (60*60*24);
393                 if (days == 0 || age <= days)
394                         folder_item_fetch_msg_full(msginfo->folder, msginfo->msgnum, TRUE, TRUE);
395                 statusbar_progress_all(num++,total, 100);
396                 if (num % 100 == 0)
397                         GTK_EVENTS_FLUSH();
398         }
399
400         statusbar_progress_all(0,0,0);
401         procmsg_msg_list_free(mlist);
402         folder_set_ui_func(item->folder, NULL, NULL);
403         main_window_progress_off(mainwin);
404         gtk_widget_set_sensitive(folderview->ctree, TRUE);
405         main_window_unlock(mainwin);
406         inc_unlock();
407         main_window_cursor_normal(mainwin);
408 }
409
410 static void download_cb(GtkAction *action, gpointer data)
411 {
412         FolderView *folderview = (FolderView *)data;
413         FolderItem *item;
414
415         if (!folderview->selected) return;
416
417         item = folderview_get_selected_item(folderview);
418         news_gtk_synchronise(item, 0);
419 }