File: mn-gmime-stream-vfs.gob

package info (click to toggle)
mail-notification 5.4.dfsg.1-14
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,468 kB
  • ctags: 9,615
  • sloc: ansic: 13,970; sh: 2,770; xml: 2,113; makefile: 57
file content (294 lines) | stat: -rw-r--r-- 7,790 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Mail Notification
 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

%headertop{
#include <libgnomevfs/gnome-vfs.h>
#include <gmime/gmime.h>
%}

%{
#include <glib/gi18n.h>
#include "mn-vfs.h"

#define VFS_CALL(stream, expr, fail_format, fail_retval)		\
  {									\
    GnomeVFSResult result;						\
									\
    result = expr;							\
    if (result != GNOME_VFS_OK)						\
      {									\
	(stream)->_priv->eof = TRUE;					\
	if (result != GNOME_VFS_ERROR_EOF)				\
	  {								\
	    g_warning((fail_format),					\
		      (stream)->_priv->uri,				\
		      gnome_vfs_result_to_string(result));		\
	    return (fail_retval);					\
	  }								\
      }									\
  }

#define VFS_READ(stream, buffer, bytes, bytes_read, fail_retval) \
  VFS_CALL((stream), gnome_vfs_read((stream)->_priv->handle, (buffer), (bytes), (bytes_read)), _("unable to read %s: %s"), (fail_retval))
#define VFS_WRITE(stream, buffer, bytes, bytes_written, fail_retval) \
  VFS_CALL((stream), gnome_vfs_write((stream)->_priv->handle, (buffer), (bytes), (bytes_written)), _("unable to write to %s: %s"), (fail_retval))
#define VFS_SEEK(stream, whence, offset, fail_retval) \
  VFS_CALL((stream), gnome_vfs_seek((stream)->_priv->handle, (whence), (offset)), _("unable to seek in %s: %s"), (fail_retval))
#define VFS_TELL(stream, offset_return, fail_retval) \
  VFS_CALL((stream), gnome_vfs_tell((stream)->_priv->handle, (offset_return)), _("unable to tell position of %s: %s"), (fail_retval))
#define VFS_CLOSE(stream, fail_retval) \
  VFS_CALL((stream), gnome_vfs_close((stream)->_priv->handle), _("unable to close %s: %s"), (fail_retval))
%}

class MN:GMime:Stream:VFS from GMime:Stream
{
  private GnomeVFSHandle *handle;
  private gboolean handle_owned;

  private char *uri destroywith g_free; /* used in error messages only */
  private gboolean eof;

  finalize (self)
  {
    if (selfp->handle_owned)
      g_mime_stream_close(GMIME_STREAM(self));
  }

  override (GMime:Stream) ssize_t
    read (GMimeStream *stream, char *buf, size_t len)
  {
    Self *self = SELF(stream);
    GnomeVFSFileSize bytes_read;

    if (stream->bound_end != -1 && stream->position >= stream->bound_end)
      return -1;
    if (stream->bound_end != -1)
      len = MIN(stream->bound_end - stream->position, (off_t) len);

    /* make sure we are at the right position */
    VFS_SEEK(self, GNOME_VFS_SEEK_START, stream->position, -1);

    VFS_READ(self, buf, len, &bytes_read, -1);
    stream->position += bytes_read;

    return bytes_read;
  }

  override (GMime:Stream) ssize_t
    write (GMimeStream *stream, const char *buf, size_t len)
  {
    Self *self = SELF(stream);
    GnomeVFSFileSize bytes_written;

    if (stream->bound_end != -1 && stream->position >= stream->bound_end)
      return -1;
    if (stream->bound_end != -1)
      len = MIN(stream->bound_end - stream->position, (off_t) len);

    /* make sure we are at the right position */
    VFS_SEEK(self, GNOME_VFS_SEEK_START, stream->position, -1);

    VFS_WRITE(self, buf, len, &bytes_written, -1);
    stream->position += bytes_written;

    return bytes_written;
  }

  override (GMime:Stream) int
    flush (GMimeStream *stream)
  {
    /* nop */
    return 0;			/* success */
  }

  override (GMime:Stream) int
    close (GMimeStream *stream)
  {
    Self *self = SELF(stream);

    VFS_CLOSE(self, -1);

    return 0;			/* success */
  }

  override (GMime:Stream) gboolean
    eos (GMimeStream *stream)
  {
    Self *self = SELF(stream);

    if (stream->bound_end == -1)
      return selfp->eof;
    else
      return stream->position >= stream->bound_end;
  }

  override (GMime:Stream) int
    reset (GMimeStream *stream)
  {
    Self *self = SELF(stream);

    if (stream->position == stream->bound_start)
      return 0;

    VFS_SEEK(self, GNOME_VFS_SEEK_START, stream->bound_start, -1);
    stream->position = stream->bound_start;

    return 0;
  }

  override (GMime:Stream) off_t
    seek (GMimeStream *stream, off_t offset, GMimeSeekWhence whence)
  {
    Self *self = SELF(stream);
    off_t real = stream->position;

    switch (whence)
      {
      case GMIME_STREAM_SEEK_SET:
	real = offset;
	break;

      case GMIME_STREAM_SEEK_CUR:
	real = stream->position + offset;
	break;

      case GMIME_STREAM_SEEK_END:
	if (stream->bound_end == -1)
	  {
	    GnomeVFSFileSize current_position;

	    VFS_SEEK(self, GNOME_VFS_SEEK_END, offset, -1);
	    VFS_TELL(self, &current_position, -1);

	    real = current_position;
	    if (real < stream->bound_start)
	      real = stream->bound_start;
	    stream->position = real;

	    return real;
	  }
	real = stream->bound_end + offset;
	break;
      }

    if (stream->bound_end != -1)
      real = MIN(real, stream->bound_end);
    real = MAX(real, stream->bound_start);

    VFS_SEEK(self, GNOME_VFS_SEEK_START, real, -1);
    stream->position = real;

    return real;
  }

  override (GMime:Stream) off_t
    tell (GMimeStream *stream)
  {
    return stream->position;
  }

  override (GMime:Stream) ssize_t
    length (GMimeStream *stream)
  {
    Self *self = SELF(stream);
    GnomeVFSFileSize bound_end;

    if (stream->bound_start != -1 && stream->bound_end != -1)
      return stream->bound_end - stream->bound_start;

    VFS_SEEK(self, GNOME_VFS_SEEK_END, 0, -1);
    VFS_TELL(self, &bound_end, -1);
    VFS_SEEK(self, GNOME_VFS_SEEK_START, stream->position, -1);

    if (bound_end < stream->bound_start)
      return -1;

    return bound_end - stream->bound_start;
  }

  override (GMime:Stream) GMimeStream *
    substream (GMimeStream *stream, off_t start, off_t end)
  {
    Self *self;

    self = GET_NEW;
    selfp->handle = SELF(stream)->_priv->handle;
    /* handle of the substream is not owned */

    g_mime_stream_construct(GMIME_STREAM(self), start, end);

    return GMIME_STREAM(self);
  }

  /*
   * Follows the GMime convention of owning the handle (even on error).
   */
  public GMimeStream *
    new (GnomeVFSHandle *handle (check null),
	 GnomeVFSURI *uri (check null),
	 GnomeVFSResult *result)
  {
    GMimeStream *stream;
    GnomeVFSResult _result;
    GnomeVFSFileSize current_position;

    if (gnome_vfs_seek(handle, GNOME_VFS_SEEK_CURRENT, 0) != GNOME_VFS_OK
	|| gnome_vfs_tell(handle, &current_position) != GNOME_VFS_OK)
      {
	char *buf;
	int size;

	/* unseekable or untellable file, use a GMimeStreamMem */

	_result = mn_vfs_read_entire_file_uri(uri, &size, &buf);
	if (_result == GNOME_VFS_OK)
	  {
	    stream = g_mime_stream_mem_new_with_buffer(buf, size);
	    g_free(buf);
	  }
	else
	  stream = NULL;

	gnome_vfs_close(handle);
      }
    else
      {
	Self *self;

	_result = GNOME_VFS_OK;

	self = GET_NEW;
	selfp->handle = handle;
	selfp->handle_owned = TRUE;
	selfp->uri = gnome_vfs_uri_to_string(uri, GNOME_VFS_URI_HIDE_NONE);

	stream = GMIME_STREAM(self);
	g_mime_stream_construct(stream, current_position, -1);

	/* check for EOF */
	if (g_mime_stream_length(stream) <= 0)
	  selfp->eof = TRUE;
      }

    if (result)
      *result = _result;

    return stream;
  }
}