news_gtk.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 1999-2006 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. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "defs.h"
  23. #include <glib.h>
  24. #include <glib/gi18n.h>
  25. #include <gtk/gtk.h>
  26. #include "utils.h"
  27. #include "folder.h"
  28. #include "folderview.h"
  29. #include "menu.h"
  30. #include "account.h"
  31. #include "alertpanel.h"
  32. #include "grouplistdialog.h"
  33. #include "prefs_common.h"
  34. #include "news_gtk.h"
  35. #include "common/hooks.h"
  36. #include "inc.h"
  37. #include "news.h"
  38. static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
  39. static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget);
  40. static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
  41. static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
  42. static void sync_cb(FolderView *folderview, guint action, GtkWidget *widget);
  43. static GtkItemFactoryEntry news_popup_entries[] =
  44. {
  45. {N_("/_Subscribe to newsgroup..."), NULL, subscribe_newsgroup_cb, 0, NULL},
  46. {N_("/_Unsubscribe newsgroup"), NULL, unsubscribe_newsgroup_cb, 0, NULL},
  47. {N_("/---"), NULL, NULL, 0, "<Separator>"},
  48. {N_("/Synchronise"), NULL, sync_cb, 0, NULL},
  49. {N_("/Down_load messages"), NULL, download_cb, 0, NULL},
  50. {N_("/---"), NULL, NULL, 0, "<Separator>"},
  51. {N_("/_Check for new messages"), NULL, update_tree_cb, 0, NULL},
  52. {N_("/---"), NULL, NULL, 0, "<Separator>"},
  53. };
  54. static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
  55. static FolderViewPopup news_popup =
  56. {
  57. "news",
  58. "<NewsFolder>",
  59. NULL,
  60. set_sensitivity
  61. };
  62. void news_gtk_init(void)
  63. {
  64. guint i, n_entries;
  65. n_entries = sizeof(news_popup_entries) /
  66. sizeof(news_popup_entries[0]);
  67. for (i = 0; i < n_entries; i++)
  68. news_popup.entries = g_slist_append(news_popup.entries, &news_popup_entries[i]);
  69. folderview_register_popup(&news_popup);
  70. }
  71. static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
  72. {
  73. MainWindow *mainwin = mainwindow_get_mainwindow();
  74. #define SET_SENS(name, sens) \
  75. menu_set_sensitive(factory, name, sens)
  76. SET_SENS("/Subscribe to newsgroup...",
  77. folder_item_parent(item) == NULL
  78. && mainwin->lock_count == 0
  79. && news_folder_locked(item->folder) == 0);
  80. SET_SENS("/Unsubscribe newsgroup",
  81. folder_item_parent(item) != NULL
  82. && mainwin->lock_count == 0
  83. && news_folder_locked(item->folder) == 0);
  84. SET_SENS("/Check for new messages",
  85. folder_item_parent(item) == NULL
  86. && mainwin->lock_count == 0
  87. && news_folder_locked(item->folder) == 0);
  88. #undef SET_SENS
  89. }
  90. static void subscribe_newsgroup_cb(FolderView *folderview, guint action, GtkWidget *widget)
  91. {
  92. GtkCTree *ctree = GTK_CTREE(folderview->ctree);
  93. GtkCTreeNode *servernode, *node;
  94. Folder *folder;
  95. FolderItem *item;
  96. FolderItem *rootitem;
  97. FolderItem *newitem;
  98. GSList *new_subscr;
  99. GSList *cur;
  100. GNode *gnode;
  101. MainWindow *mainwin = mainwindow_get_mainwindow();
  102. if (!folderview->selected) return;
  103. item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
  104. g_return_if_fail(item != NULL);
  105. if (mainwin->lock_count || news_folder_locked(item->folder))
  106. return;
  107. folder = item->folder;
  108. g_return_if_fail(folder != NULL);
  109. g_return_if_fail(FOLDER_TYPE(folder) == F_NEWS);
  110. g_return_if_fail(folder->account != NULL);
  111. if (GTK_CTREE_ROW(folderview->selected)->parent != NULL)
  112. servernode = GTK_CTREE_ROW(folderview->selected)->parent;
  113. else
  114. servernode = folderview->selected;
  115. rootitem = gtk_ctree_node_get_row_data(ctree, servernode);
  116. new_subscr = grouplist_dialog(folder);
  117. /* remove unsubscribed newsgroups */
  118. for (gnode = folder->node->children; gnode != NULL; ) {
  119. GNode *next = gnode->next;
  120. item = FOLDER_ITEM(gnode->data);
  121. if (g_slist_find_custom(new_subscr, item->path,
  122. (GCompareFunc)g_ascii_strcasecmp) != NULL) {
  123. gnode = next;
  124. continue;
  125. }
  126. node = gtk_ctree_find_by_row_data(ctree, servernode, item);
  127. if (!node) {
  128. gnode = next;
  129. continue;
  130. }
  131. if (folderview->opened == node) {
  132. summary_clear_all(folderview->summaryview);
  133. folderview->opened = NULL;
  134. }
  135. gtk_ctree_remove_node(ctree, node);
  136. folder_item_remove(item);
  137. gnode = next;
  138. }
  139. gtk_clist_freeze(GTK_CLIST(ctree));
  140. /* add subscribed newsgroups */
  141. for (cur = new_subscr; cur != NULL; cur = cur->next) {
  142. gchar *name = (gchar *)cur->data;
  143. FolderUpdateData hookdata;
  144. if (folder_find_child_item_by_name(rootitem, name) != NULL)
  145. continue;
  146. newitem = folder_item_new(folder, name, name);
  147. folder_item_append(rootitem, newitem);
  148. hookdata.folder = newitem->folder;
  149. hookdata.update_flags = FOLDER_TREE_CHANGED | FOLDER_ADD_FOLDERITEM;
  150. hookdata.item = newitem;
  151. hooks_invoke(FOLDER_UPDATE_HOOKLIST, &hookdata);
  152. }
  153. gtk_clist_thaw(GTK_CLIST(ctree));
  154. slist_free_strings(new_subscr);
  155. g_slist_free(new_subscr);
  156. folder_write_list();
  157. }
  158. static void unsubscribe_newsgroup_cb(FolderView *folderview, guint action,
  159. GtkWidget *widget)
  160. {
  161. GtkCTree *ctree = GTK_CTREE(folderview->ctree);
  162. FolderItem *item;
  163. gchar *name;
  164. gchar *message;
  165. gchar *old_id;
  166. AlertValue avalue;
  167. MainWindow *mainwin = mainwindow_get_mainwindow();
  168. if (!folderview->selected) return;
  169. item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
  170. g_return_if_fail(item != NULL);
  171. if (mainwin->lock_count || news_folder_locked(item->folder))
  172. return;
  173. g_return_if_fail(item->folder != NULL);
  174. g_return_if_fail(FOLDER_TYPE(item->folder) == F_NEWS);
  175. g_return_if_fail(item->folder->account != NULL);
  176. old_id = folder_item_get_identifier(item);
  177. name = trim_string(item->path, 32);
  178. message = g_strdup_printf(_("Really unsubscribe newsgroup '%s'?"), name);
  179. avalue = alertpanel_full(_("Unsubscribe newsgroup"), message,
  180. GTK_STOCK_CANCEL, _("_Unsubscribe"), NULL, FALSE,
  181. NULL, ALERT_WARNING, G_ALERTDEFAULT);
  182. g_free(message);
  183. g_free(name);
  184. if (avalue != G_ALERTALTERNATE) return;
  185. if (folderview->opened == folderview->selected) {
  186. summary_clear_all(folderview->summaryview);
  187. folderview->opened = NULL;
  188. }
  189. if(item->folder->klass->remove_folder(item->folder, item) < 0) {
  190. folder_item_scan(item);
  191. alertpanel_error(_("Can't remove the folder '%s'."), name);
  192. g_free(old_id);
  193. return;
  194. }
  195. folder_write_list();
  196. prefs_filtering_delete_path(old_id);
  197. g_free(old_id);
  198. }
  199. static void update_tree_cb(FolderView *folderview, guint action,
  200. GtkWidget *widget)
  201. {
  202. FolderItem *item;
  203. MainWindow *mainwin = mainwindow_get_mainwindow();
  204. item = folderview_get_selected_item(folderview);
  205. g_return_if_fail(item != NULL);
  206. if (mainwin->lock_count || news_folder_locked(item->folder))
  207. return;
  208. summary_show(folderview->summaryview, NULL);
  209. g_return_if_fail(item->folder != NULL);
  210. folderview_check_new(item->folder);
  211. }
  212. static void sync_cb(FolderView *folderview, guint action,
  213. GtkWidget *widget)
  214. {
  215. FolderItem *item;
  216. item = folderview_get_selected_item(folderview);
  217. g_return_if_fail(item != NULL);
  218. folder_synchronise(item->folder);
  219. }
  220. void news_gtk_synchronise(FolderItem *item)
  221. {
  222. MainWindow *mainwin = mainwindow_get_mainwindow();
  223. FolderView *folderview = mainwin->folderview;
  224. g_return_if_fail(item != NULL);
  225. g_return_if_fail(item->folder != NULL);
  226. if (mainwin->lock_count || news_folder_locked(item->folder))
  227. return;
  228. main_window_cursor_wait(mainwin);
  229. inc_lock();
  230. main_window_lock(mainwin);
  231. gtk_widget_set_sensitive(folderview->ctree, FALSE);
  232. main_window_progress_on(mainwin);
  233. GTK_EVENTS_FLUSH();
  234. if (folder_item_fetch_all_msg(item) < 0) {
  235. gchar *name;
  236. name = trim_string(item->name, 32);
  237. alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
  238. g_free(name);
  239. }
  240. folder_set_ui_func(item->folder, NULL, NULL);
  241. main_window_progress_off(mainwin);
  242. gtk_widget_set_sensitive(folderview->ctree, TRUE);
  243. main_window_unlock(mainwin);
  244. inc_unlock();
  245. main_window_cursor_normal(mainwin);
  246. }
  247. static void download_cb(FolderView *folderview, guint action,
  248. GtkWidget *widget)
  249. {
  250. GtkCTree *ctree = GTK_CTREE(folderview->ctree);
  251. FolderItem *item;
  252. if (!folderview->selected) return;
  253. item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
  254. news_gtk_synchronise(item);
  255. }