2013-02-17 [colin] 3.9.0cvs75
[claws.git] / src / plugins / mailmbox / mailmbox_types.c
1 /*
2  * libEtPan! -- a mail stuff library
3  *
4  * Copyright (C) 2001, 2002 - DINH Viet Hoa
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the libEtPan! project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * $Id$
34  */
35
36 #include "mailmbox_types.h"
37
38 #include <string.h>
39 #include <stdlib.h>
40
41 #ifndef TRUE
42 #define TRUE 1
43 #endif
44
45 #ifndef FALSE
46 #define FALSE 0
47 #endif
48
49 /* *********************************************************************** */
50
51 int claws_mailmbox_msg_info_update(struct claws_mailmbox_folder * folder,
52                              size_t msg_start, size_t msg_start_len,
53                              size_t msg_headers, size_t msg_headers_len,
54                              size_t msg_body, size_t msg_body_len,
55                              size_t msg_size, size_t msg_padding,
56                              uint32_t msg_uid)
57 {
58   struct claws_mailmbox_msg_info * info;
59   int res;
60   chashdatum key;
61   chashdatum data;
62   int r;
63   
64   key.data = &msg_uid;
65   key.len = sizeof(msg_uid);
66   r = chash_get(folder->mb_hash, &key, &data);
67   if (r < 0) {
68     unsigned int index;
69
70     info = claws_mailmbox_msg_info_new(msg_start, msg_start_len,
71         msg_headers, msg_headers_len,
72         msg_body, msg_body_len, msg_size, msg_padding, msg_uid);
73     if (info == NULL) {
74       res = MAILMBOX_ERROR_MEMORY;
75       goto err;
76     }
77
78     r = carray_add(folder->mb_tab, info, &index);
79     if (r < 0) {
80       claws_mailmbox_msg_info_free(info);
81       res = MAILMBOX_ERROR_MEMORY;
82       goto err;
83     }
84
85     if (msg_uid != 0) {
86       chashdatum key;
87       chashdatum data;
88       
89       key.data = &msg_uid;
90       key.len = sizeof(msg_uid);
91       data.data = info;
92       data.len = 0;
93       
94       r = chash_set(folder->mb_hash, &key, &data, NULL);
95       if (r < 0) {
96         claws_mailmbox_msg_info_free(info);
97         carray_delete(folder->mb_tab, index);
98         res = MAILMBOX_ERROR_MEMORY;
99         goto err;
100       }
101     }
102     
103     info->msg_index = index;
104   }
105   else {
106     info = data.data;
107     
108     info->msg_start = msg_start;
109     info->msg_start_len = msg_start_len;
110     info->msg_headers = msg_headers;
111     info->msg_headers_len = msg_headers_len;
112     info->msg_body = msg_body;
113     info->msg_body_len = msg_body_len;
114     info->msg_size = msg_size;
115     info->msg_padding = msg_padding;
116   }
117
118   return MAILMBOX_NO_ERROR;
119
120  err:
121   return res;
122 }
123
124
125 struct claws_mailmbox_msg_info *
126 claws_mailmbox_msg_info_new(size_t msg_start, size_t msg_start_len,
127                       size_t msg_headers, size_t msg_headers_len,
128                       size_t msg_body, size_t msg_body_len,
129                       size_t msg_size, size_t msg_padding,
130                       uint32_t msg_uid)
131 {
132   struct claws_mailmbox_msg_info * info;
133
134   info = malloc(sizeof(* info));
135   if (info == NULL)
136     return NULL;
137
138   info->msg_index = 0;
139   info->msg_uid = msg_uid;
140   if (msg_uid != 0)
141     info->msg_written_uid = TRUE;
142   else
143     info->msg_written_uid = FALSE;
144   info->msg_deleted = FALSE;
145
146   info->msg_start = msg_start;
147   info->msg_start_len = msg_start_len;
148
149   info->msg_headers = msg_headers;
150   info->msg_headers_len = msg_headers_len;
151
152   info->msg_body = msg_body;
153   info->msg_body_len = msg_body_len;
154
155   info->msg_size = msg_size;
156
157   info->msg_padding = msg_padding;
158
159   return info;
160 }
161
162 void claws_mailmbox_msg_info_free(struct claws_mailmbox_msg_info * info)
163 {
164   free(info);
165 }
166
167
168 /* append info */
169
170 struct claws_mailmbox_append_info *
171 claws_mailmbox_append_info_new(const char * ai_message, size_t ai_size)
172 {
173   struct claws_mailmbox_append_info * info;
174
175   info = malloc(sizeof(* info));
176   if (info == NULL)
177     return NULL;
178
179   info->ai_message = ai_message;
180   info->ai_size = ai_size;
181
182   return info;
183 }
184
185 void claws_mailmbox_append_info_free(struct claws_mailmbox_append_info * info)
186 {
187   free(info);
188 }
189
190 struct claws_mailmbox_folder * claws_mailmbox_folder_new(const char * mb_filename)
191 {
192   struct claws_mailmbox_folder * folder;
193
194   folder = malloc(sizeof(* folder));
195   if (folder == NULL)
196     goto err;
197
198   strncpy(folder->mb_filename, mb_filename, PATH_MAX);
199
200   folder->mb_mtime = (time_t) -1;
201
202   folder->mb_fd = -1;
203   folder->mb_read_only = TRUE;
204   folder->mb_no_uid = TRUE;
205
206   folder->mb_changed = FALSE;
207   folder->mb_deleted_count = 0;
208   
209   folder->mb_mapping = NULL;
210   folder->mb_mapping_size = 0;
211
212   folder->mb_written_uid = 0;
213   folder->mb_max_uid = 0;
214
215   folder->mb_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
216   if (folder->mb_hash == NULL)
217     goto free;
218   
219   folder->mb_tab = carray_new(128);
220   if (folder->mb_tab == NULL)
221     goto free_hash;
222
223   return folder;
224
225  free_hash:
226   chash_free(folder->mb_hash);
227  free:
228   free(folder);
229  err:
230   return NULL;
231 }
232
233 void claws_mailmbox_folder_free(struct claws_mailmbox_folder * folder)
234 {
235   unsigned int i;
236
237   for(i = 0 ; i < carray_count(folder->mb_tab) ; i++) {
238     struct claws_mailmbox_msg_info * info;
239
240     info = carray_get(folder->mb_tab, i);
241     if (info != NULL)
242       claws_mailmbox_msg_info_free(info);
243   }
244
245   carray_free(folder->mb_tab);
246   
247   chash_free(folder->mb_hash);
248
249   free(folder);
250 }