1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-nntp-grouplist.c : getting/updating the list of newsgroups on the server. */
5 * Author : Chris Toshok <toshok@ximian.com>
7 * Copyright (C) 2000 Ximian .
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include <glib/gstdio.h>
34 #include "camel-exception.h"
36 #include "camel-nntp-grouplist.h"
37 #include "camel-nntp-resp-codes.h"
39 static CamelNNTPGroupList *
40 camel_nntp_get_grouplist_from_server (CamelNNTPStore *store, CamelException *ex)
43 gboolean done = FALSE;
44 CamelNNTPGroupList *list;
46 CAMEL_NNTP_STORE_LOCK(store);
47 status = camel_nntp_command (store, ex, NULL, &line, "LIST");
49 if (status != NNTP_LIST_FOLLOWS) {
50 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
51 _("Could not get group list from server."));
55 list = g_new0 (CamelNNTPGroupList, 1);
56 list->time = time (NULL);
61 if (camel_remote_store_recv_line (CAMEL_REMOTE_STORE (store), &line, ex) < 0) {
62 list->group_list = g_list_reverse(list->group_list);
70 CamelNNTPGroupListEntry *entry = g_new (CamelNNTPGroupListEntry, 1);
71 char **split_line = g_strsplit (line, " ", 4);
73 entry->group_name = g_strdup (split_line[0]);
74 entry->high = atoi (split_line[1]);
75 entry->low = atoi (split_line[2]);
77 g_strfreev (split_line);
79 list->group_list = g_list_prepend (list->group_list, entry);
82 CAMEL_NNTP_STORE_UNLOCK(store);
84 list->group_list = g_list_reverse(list->group_list);
88 static CamelNNTPGroupList*
89 camel_nntp_get_grouplist_from_file (CamelNNTPStore *store, CamelException *ex)
91 gchar *root_dir = camel_nntp_store_get_toplevel_dir(CAMEL_NNTP_STORE(store));
92 gchar *grouplist_file = g_strdup_printf ("%s/grouplist", root_dir);
93 CamelNNTPGroupList *list;
99 fp = g_fopen (grouplist_file, "r");
100 g_free (grouplist_file);
103 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
104 _("Unable to load grouplist file for %s: %s"),
105 CAMEL_SERVICE(store)->url->host,
111 if (!fgets (buf, sizeof (buf), fp)) {
112 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
113 _("Unable to load grouplist file for %s: %s"),
114 CAMEL_SERVICE(store)->url->host,
121 list = g_new0 (CamelNNTPGroupList, 1);
123 sscanf (buf, "%lu", &time);
126 while (fgets (buf, sizeof (buf), fp)) {
127 CamelNNTPGroupListEntry *entry = g_new (CamelNNTPGroupListEntry, 1);
128 char **split_line = g_strsplit (buf, " ", 4);
130 entry->group_name = g_strdup (split_line[0]);
131 entry->high = atoi (split_line[1]);
132 entry->low = atoi (split_line[2]);
134 g_strfreev (split_line);
136 list->group_list = g_list_prepend (list->group_list, entry);
141 list->group_list = g_list_reverse(list->group_list);
146 save_entry (CamelNNTPGroupListEntry *entry, FILE *fp)
148 fprintf (fp, "%s %d %d\n", entry->group_name, entry->low, entry->high);
152 camel_nntp_grouplist_save (CamelNNTPGroupList *group_list, CamelException *ex)
155 gchar *root_dir = camel_nntp_store_get_toplevel_dir(CAMEL_NNTP_STORE(group_list->store));
156 gchar *grouplist_file = g_strdup_printf ("%s/grouplist", root_dir);
159 fp = g_fopen (grouplist_file, "w");
160 g_free (grouplist_file);
163 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
164 _("Unable to save grouplist file for %s: %s"),
165 CAMEL_SERVICE(group_list->store)->url->host,
170 fprintf (fp, "%lu\n", (long)group_list->time);
172 g_list_foreach (group_list->group_list, (GFunc)save_entry, fp);
178 free_entry (CamelNNTPGroupListEntry *entry, void *data)
180 g_free (entry->group_name);
185 camel_nntp_grouplist_free (CamelNNTPGroupList *group_list)
187 g_return_if_fail (group_list);
189 g_list_foreach (group_list->group_list, (GFunc)free_entry, NULL);
195 camel_nntp_grouplist_fetch (CamelNNTPStore *store, CamelException *ex)
197 CamelNNTPGroupList *list;
199 list = camel_nntp_get_grouplist_from_file (store, ex);
201 printf ("camel_nntp_get_grouplist_from_file returned %p\n", list);
204 camel_exception_clear (ex);
206 list = camel_nntp_get_grouplist_from_server (store, ex);
209 camel_nntp_grouplist_free (list);
213 camel_nntp_grouplist_save (list, ex);
222 camel_nntp_grouplist_update (CamelNNTPGroupList *group_list, CamelException *ex)