Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / imapp / camel-imapp-fetch-stream.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
2  *
3  * Author:
4  *  Michael Zucchi <notzed@ximian.com>
5  *
6  * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2 of the GNU Lesser General Public
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <glib.h>
33
34 #include "camel-stream-mem.h"
35
36 #include "camel-imapp-exception.h"
37 #include "camel-imapp-stream.h"
38
39 #define t(x) 
40 #define io(x) x
41
42 static CamelObjectClass *parent_class = NULL;
43
44 /* Returns the class for a CamelStream */
45 #define CS_CLASS(so) CAMEL_IMAPP_FETCH_STREAM_CLASS(CAMEL_OBJECT_GET_CLASS(so))
46
47 static ssize_t
48 stream_read(CamelStream *stream, char *buffer, size_t n)
49 {
50         CamelIMAPPFetchStream *is = (CamelIMAPPFetchStream *)stream;
51         ssize_t max;
52
53         /* make sure we have all the data read in */
54         while (camel_imapp_engine_iterate(is->engine, is->command)>0)
55                 ;
56
57         if (is->literal == 0 || n == 0)
58                 return 0;
59
60         max = is->end - is->ptr;
61         if (max > 0) {
62                 max = MIN(max, is->literal);
63                 max = MIN(max, n);
64                 memcpy(buffer, is->ptr, max);
65                 is->ptr += max;
66         } else {
67                 max = MIN(is->literal, n);
68                 max = camel_stream_read(is->source, buffer, max);
69                 if (max <= 0)
70                         return max;
71         }
72
73         is->literal -= max;
74         
75         return max;
76 }
77
78 static ssize_t
79 stream_write(CamelStream *stream, const char *buffer, size_t n)
80 {
81         CamelIMAPPFetchStream *is = (CamelIMAPPFetchStream *)stream;
82
83         return camel_stream_write(is->source, buffer, n);
84 }
85
86 static int
87 stream_close(CamelStream *stream)
88 {
89         /* nop? */
90         return 0;
91 }
92
93 static int
94 stream_flush(CamelStream *stream)
95 {
96         /* nop? */
97         return 0;
98 }
99
100 static gboolean
101 stream_eos(CamelStream *stream)
102 {
103         CamelIMAPPFetchStream *is = (CamelIMAPPFetchStream *)stream;
104
105         return is->literal == 0;
106 }
107
108 static int
109 stream_reset(CamelStream *stream)
110 {
111         /* nop?  reset literal mode? */
112         return 0;
113 }
114
115 static void
116 camel_imapp_fetch_stream_class_init (CamelStreamClass *camel_imapp_fetch_stream_class)
117 {
118         CamelStreamClass *camel_stream_class = (CamelStreamClass *)camel_imapp_fetch_stream_class;
119
120         parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );
121
122         /* virtual method definition */
123         camel_stream_class->read = stream_read;
124         camel_stream_class->write = stream_write;
125         camel_stream_class->close = stream_close;
126         camel_stream_class->flush = stream_flush;
127         camel_stream_class->eos = stream_eos;
128         camel_stream_class->reset = stream_reset;
129 }
130
131 static void
132 camel_imapp_fetch_stream_init(CamelIMAPPFetchStream *is, CamelIMAPPFetchStreamClass *isclass)
133 {
134         ;
135 }
136
137 static void
138 camel_imapp_fetch_stream_finalise(CamelIMAPPFetchStream *is)
139 {
140         if (is->engine)
141                 camel_object_unref(is->engine);
142 }
143
144 CamelType
145 camel_imapp_fetch_stream_get_type (void)
146 {
147         static CamelType camel_imapp_fetch_stream_type = CAMEL_INVALID_TYPE;
148
149         if (camel_imapp_fetch_stream_type == CAMEL_INVALID_TYPE) {
150                 setup_table();
151                 camel_imapp_fetch_stream_type = camel_type_register( camel_stream_get_type(),
152                                                             "CamelIMAPPFetchStream",
153                                                             sizeof( CamelIMAPPFetchStream ),
154                                                             sizeof( CamelIMAPPFetchStreamClass ),
155                                                             (CamelObjectClassInitFunc) camel_imapp_fetch_stream_class_init,
156                                                             NULL,
157                                                             (CamelObjectInitFunc) camel_imapp_fetch_stream_init,
158                                                             (CamelObjectFinalizeFunc) camel_imapp_fetch_stream_finalise );
159         }
160
161         return camel_imapp_fetch_stream_type;
162 }
163
164 /**
165  * camel_imapp_fetch_stream_new:
166  *
167  * Return value: the stream
168  **/
169 CamelStream *
170 camel_imapp_fetch_stream_new(CamelIMAPPEngine *ie, const char *uid, const char *body)
171 {
172         CamelIMAPPFetchStream *is;
173
174         is = (CamelIMAPPFetchStream *)camel_object_new(camel_imapp_fetch_stream_get_type ());
175         is->engine = ie;
176         camel_object_ref(ie);
177
178         is->command = camel_imapp_engine_command_new(ie, "FETCH", NULL, "FETCH %t (BODY[%t]<0.4096>", uid, body);
179         camel_imapp_engine_command_queue(ie, command);
180
181         return (CamelStream *)is;
182 }
183