Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / imapp / camel-imapp-summary.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright(C) 2000 Ximian Inc.
4  *
5  *  Authors:
6  *    Michael Zucchi <notzed@ximian.com>
7  *    Dan Winship <danw@ximian.com>
8  *
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.
12  *
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 GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/uio.h>
34
35 #include "camel-file-utils.h"
36
37 #include "camel-imapp-summary.h"
38
39 #define CAMEL_IMAPP_SUMMARY_VERSION (1)
40
41 static int summary_header_load(CamelFolderSummary *, FILE *);
42 static int summary_header_save(CamelFolderSummary *, FILE *);
43
44 static CamelMessageInfo *message_info_load(CamelFolderSummary *s, FILE *in);
45 static int message_info_save(CamelFolderSummary *s, FILE *out, CamelMessageInfo *info);
46
47 static void camel_imapp_summary_class_init(CamelIMAPPSummaryClass *klass);
48 static void camel_imapp_summary_init      (CamelIMAPPSummary *obj);
49
50 static CamelFolderSummaryClass *camel_imapp_summary_parent;
51
52 CamelType
53 camel_imapp_summary_get_type(void)
54 {
55         static CamelType type = CAMEL_INVALID_TYPE;
56
57         if (type == CAMEL_INVALID_TYPE) {
58                 type = camel_type_register(
59                         camel_folder_summary_get_type(), "CamelIMAPPSummary",
60                         sizeof(CamelIMAPPSummary),
61                         sizeof(CamelIMAPPSummaryClass),
62                         (CamelObjectClassInitFunc) camel_imapp_summary_class_init,
63                         NULL,
64                         (CamelObjectInitFunc) camel_imapp_summary_init,
65                         NULL);
66         }
67
68         return type;
69 }
70
71 static void
72 camel_imapp_summary_class_init(CamelIMAPPSummaryClass *klass)
73 {
74         CamelFolderSummaryClass *cfs_class =(CamelFolderSummaryClass *) klass;
75
76         camel_imapp_summary_parent = CAMEL_FOLDER_SUMMARY_CLASS(camel_type_get_global_classfuncs(camel_folder_summary_get_type()));
77
78         cfs_class->summary_header_load = summary_header_load;
79         cfs_class->summary_header_save = summary_header_save;
80         cfs_class->message_info_load = message_info_load;
81         cfs_class->message_info_save = message_info_save;
82 }
83
84 static void
85 camel_imapp_summary_init(CamelIMAPPSummary *obj)
86 {
87         CamelFolderSummary *s =(CamelFolderSummary *)obj;
88
89         /* subclasses need to set the right instance data sizes */
90         s->message_info_size = sizeof(CamelIMAPPMessageInfo);
91         s->content_info_size = sizeof(CamelMessageContentInfo);
92
93         /* and a unique file version */
94         s->version += CAMEL_IMAPP_SUMMARY_VERSION;
95 }
96
97 /**
98  * camel_imapp_summary_new:
99  * @filename: the file to store the summary in.
100  *
101  * This will create a new CamelIMAPPSummary object and read in the
102  * summary data from disk, if it exists.
103  *
104  * Return value: A new CamelIMAPPSummary object.
105  **/
106 CamelFolderSummary *
107 camel_imapp_summary_new(void)
108 {
109         CamelFolderSummary *summary = CAMEL_FOLDER_SUMMARY(camel_object_new(camel_imapp_summary_get_type()));
110
111         return summary;
112 }
113
114
115 static int
116 summary_header_load(CamelFolderSummary *s, FILE *in)
117 {
118         CamelIMAPPSummary *ims = CAMEL_IMAPP_SUMMARY(s);
119
120         if (camel_imapp_summary_parent->summary_header_load(s, in) == -1)
121                 return -1;
122
123         /* Legacy version */
124         if (s->version == 0x100c)
125                 return camel_file_util_decode_uint32(in, &ims->uidvalidity);
126
127         if (camel_file_util_decode_fixed_int32(in, &ims->version) == -1
128             || camel_file_util_decode_fixed_int32(in, &ims->uidvalidity) == -1)
129                 return -1;
130
131         if (ims->version > CAMEL_IMAPP_SUMMARY_VERSION) {
132                 g_warning("Unkown summary version\n");
133                 errno = EINVAL;
134                 return -1;
135         }
136
137         return 0;
138 }       
139
140 static int
141 summary_header_save(CamelFolderSummary *s, FILE *out)
142 {
143         CamelIMAPPSummary *ims = CAMEL_IMAPP_SUMMARY(s);
144
145         if (camel_imapp_summary_parent->summary_header_save(s, out) == -1)
146                 return -1;
147
148         if (camel_file_util_encode_fixed_int32(out, CAMEL_IMAPP_SUMMARY_VERSION) == -1
149             || camel_file_util_encode_fixed_int32(out, ims->uidvalidity) == -1)
150                 return -1;
151
152         return 0;
153 }
154
155
156 static CamelMessageInfo *
157 message_info_load(CamelFolderSummary *s, FILE *in)
158 {
159         CamelMessageInfo *info;
160         CamelIMAPPMessageInfo *iinfo;
161
162         info = camel_imapp_summary_parent->message_info_load(s, in);
163         if (info) {
164                 iinfo =(CamelIMAPPMessageInfo *)info;
165
166                 if (camel_file_util_decode_uint32(in, &iinfo->server_flags) == -1)
167                         goto error;
168         }
169
170         return info;
171 error:
172         camel_message_info_free(info);
173         return NULL;
174 }
175
176 static int
177 message_info_save(CamelFolderSummary *s, FILE *out, CamelMessageInfo *info)
178 {
179         CamelIMAPPMessageInfo *iinfo =(CamelIMAPPMessageInfo *)info;
180
181         if (camel_imapp_summary_parent->message_info_save(s, out, info) == -1)
182                 return -1;
183
184         return camel_file_util_encode_uint32(out, iinfo->server_flags);
185 }