remove message_number_capability and require uid capatibility.
[platform/upstream/evolution-data-server.git] / camel / providers / pop3 / camel-pop3-folder.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-pop3-folder.c : class for a pop3 folder */
3
4 /* 
5  * Authors:
6  *   Dan Winship <danw@helixcode.com>
7  *
8  * Copyright (C) 2000 Helix Code, Inc. (www.helixcode.com)
9  *
10  * This program is free software; you can redistribute it and/or 
11  * modify it under the terms of the GNU General Public License as 
12  * published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */
25
26 #include "camel-pop3-folder.h"
27 #include "camel-pop3-store.h"
28 #include "camel-exception.h"
29 #include "camel-stream-mem.h"
30 #include "camel-mime-message.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #define CF_CLASS(o) (CAMEL_FOLDER_CLASS (GTK_OBJECT (o)->klass))
36 static CamelFolderClass *parent_class;
37
38 static void pop3_open (CamelFolder *folder, CamelFolderOpenMode mode,
39                        CamelException *ex);
40 static void pop3_close (CamelFolder *folder, gboolean expunge,
41                         CamelException *ex);
42 static gboolean delete_messages (CamelFolder *folder, CamelException *ex);
43
44 static gint get_message_count (CamelFolder *folder, CamelException *ex);
45 static GPtrArray *get_uids (CamelFolder *folder, CamelException *ex);
46 static CamelMimeMessage *get_message_by_uid (CamelFolder *folder, 
47                                              const char *uid,
48                                              CamelException *ex);
49 static void delete_message_by_uid (CamelFolder *folder, const char *uid,
50                                    CamelException *ex);
51
52
53 static void
54 camel_pop3_folder_class_init (CamelPop3FolderClass *camel_pop3_folder_class)
55 {
56         CamelFolderClass *camel_folder_class =
57                 CAMEL_FOLDER_CLASS (camel_pop3_folder_class);
58
59         parent_class = gtk_type_class (camel_folder_get_type ());
60
61         /* virtual method overload */
62         camel_folder_class->open = pop3_open;
63         camel_folder_class->close = pop3_close;
64         camel_folder_class->delete_messages = delete_messages;
65
66         camel_folder_class->get_message_count = get_message_count;
67         camel_folder_class->get_uids = get_uids;
68
69         camel_folder_class->get_message_by_uid = get_message_by_uid;
70         camel_folder_class->delete_message_by_uid = delete_message_by_uid;
71 }
72
73
74 static void
75 camel_pop3_folder_init (gpointer object, gpointer klass)
76 {
77         CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (object);
78         CamelFolder *folder = CAMEL_FOLDER (object);
79
80         folder->can_hold_messages = TRUE;
81         folder->can_hold_folders = FALSE;
82         folder->has_summary_capability = FALSE;
83         folder->has_search_capability = FALSE;
84
85         pop3_folder->count = -1;
86 }
87
88
89 GtkType
90 camel_pop3_folder_get_type (void)
91 {
92         static GtkType camel_pop3_folder_type = 0;
93
94         if (!camel_pop3_folder_type) {
95                 GtkTypeInfo camel_pop3_folder_info =    
96                 {
97                         "CamelPop3Folder",
98                         sizeof (CamelPop3Folder),
99                         sizeof (CamelPop3FolderClass),
100                         (GtkClassInitFunc) camel_pop3_folder_class_init,
101                         (GtkObjectInitFunc) camel_pop3_folder_init,
102                                 /* reserved_1 */ NULL,
103                                 /* reserved_2 */ NULL,
104                         (GtkClassInitFunc) NULL,
105                 };
106
107                 camel_pop3_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_pop3_folder_info);
108         }
109
110         return camel_pop3_folder_type;
111 }
112
113
114 CamelFolder *camel_pop3_folder_new (CamelStore *parent, CamelException *ex)
115 {
116         CamelFolder *folder =
117                 CAMEL_FOLDER (gtk_object_new (camel_pop3_folder_get_type (),
118                                               NULL));
119
120         CF_CLASS (folder)->init (folder, parent, NULL, "inbox", '/', ex);
121         return folder;
122 }
123
124 static void
125 pop3_open (CamelFolder *folder, CamelFolderOpenMode mode, CamelException *ex)
126 {
127         camel_pop3_store_open (CAMEL_POP3_STORE (folder->parent_store), ex);
128         if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_NONE)
129                 parent_class->open (folder, mode, ex);
130 }
131
132 static void
133 pop3_close (CamelFolder *folder, gboolean expunge, CamelException *ex)
134 {
135         camel_pop3_store_close (CAMEL_POP3_STORE (folder->parent_store),
136                                 expunge, ex);
137         if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_NONE)
138                 parent_class->close (folder, expunge, ex);
139 }
140                                 
141 static gboolean
142 delete_messages (CamelFolder *folder, CamelException *ex)
143 {
144         int msgs;
145         gboolean status;
146
147         msgs = get_message_count (folder, ex);
148         if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE)
149                 return FALSE;
150
151         status = TRUE;
152         for (; msgs > 0; msgs--) {
153                 status = status &&
154                         (camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
155                                              NULL, "DELE %d", msgs) ==
156                          CAMEL_POP3_OK);
157         }
158
159         if (!status) {
160                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
161                                       "Unable to delete all messages.");
162         }
163
164         return status;
165 }
166
167
168 static CamelMimeMessage *
169 get_message_by_uid (CamelFolder *folder, const char *uid, CamelException *ex)
170 {
171         int status;
172         char *result, *body;
173         CamelStream *msgstream;
174         CamelMimeMessage *msg;
175
176         status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
177                                      &result, "RETR %d", atoi (uid));
178         if (status != CAMEL_POP3_OK) {
179                 CamelService *service = CAMEL_SERVICE (folder->parent_store);
180                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
181                                       "Could not retrieve message from POP "
182                                       "server %s: %s.", service->url->host,
183                                       status == CAMEL_POP3_ERR ? result :
184                                       "Unknown error");
185                 g_free (result);
186                 return NULL;
187         }
188         g_free (result);
189
190         body = camel_pop3_command_get_additional_data (CAMEL_POP3_STORE (folder->parent_store), ex);
191         if (!body) {
192                 CamelService *service = CAMEL_SERVICE (folder->parent_store);
193                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
194                                       "Could not retrieve message from POP "
195                                       "server %s: %s", service->url->host,
196                                       camel_exception_get_description (ex));
197                 return NULL;
198         }
199
200         msgstream = camel_stream_mem_new_with_buffer (body, strlen (body));
201         msg = camel_mime_message_new ();
202         camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (msg),
203                                                   msgstream);
204         gtk_object_unref (GTK_OBJECT (msgstream));
205
206         return msg;
207 }
208
209 static void
210 delete_message_by_uid (CamelFolder *folder, const char *uid,
211                        CamelException *ex)
212 {
213         int status;
214         char *resp;
215
216         status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
217                                      &resp, "DELE %d", atoi (uid));
218         if (status != CAMEL_POP3_OK) {
219                 camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
220                                       "Unable to delete message %s%s%s",
221                                       uid, resp ? ": " : "",
222                                       resp ? resp : "");
223         }
224         g_free (resp);
225 }
226
227 static gint
228 get_message_count (CamelFolder *folder, CamelException *ex)
229 {
230         CamelPop3Folder *pop3_folder = CAMEL_POP3_FOLDER (folder);
231         int status;
232         char *result;
233
234         if (pop3_folder->count != -1)
235                 return pop3_folder->count;
236
237         status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
238                                      &result, "STAT");
239         if (status != CAMEL_POP3_OK) {
240                 CamelService *service = CAMEL_SERVICE (folder->parent_store);
241                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
242                                       "Could not get message count from POP "
243                                       "server %s: %s.", service->url->host,
244                                       status == CAMEL_POP3_ERR ? result :
245                                       "Unknown error");
246                 g_free (result);
247                 return -1;
248         }
249
250         pop3_folder->count = atoi (result);
251         g_free (result);
252         return pop3_folder->count;
253 }
254
255 static GPtrArray *
256 get_uids (CamelFolder *folder, CamelException *ex)
257 {
258         int count, i;
259         GPtrArray *array;
260
261         count = get_message_count (folder, ex);
262         if (count == -1)
263                 return NULL;
264
265         array = g_ptr_array_new ();
266         g_ptr_array_set_size (array, count);
267         for (i = 0; i < count; i++)
268                 array->pdata[i] = g_strdup_printf ("%d", i + 1);
269
270         return array;
271 }