File: mn-vfs-mailbox.gob

package info (click to toggle)
mail-notification 5.4.dfsg.1-13
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,448 kB
  • ctags: 9,615
  • sloc: ansic: 13,970; sh: 2,770; xml: 2,113; makefile: 58
file content (487 lines) | stat: -rw-r--r-- 12,022 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * 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 "mn-reentrant-mailbox.h"
%}

%h{
#define MN_VFS_MAILBOX_MAX_BACKEND_TYPES	5
extern GType mn_vfs_mailbox_backend_types[MN_VFS_MAILBOX_MAX_BACKEND_TYPES + 1];
%}

%privateheader{
#include "mn-vfs-mailbox-backend.h"
%}

%{
#include <glib/gi18n.h>
#include "mn-mailbox-private.h"
#include "mn-reentrant-mailbox-private.h"
#if WITH_MBOX
#include "mn-mbox-mailbox-backend.h"
#endif
#if WITH_MOZILLA
#include "mn-mozilla-mailbox-backend.h"
#endif
#if WITH_MH
#include "mn-mh-mailbox-backend.h"
#endif
#if WITH_MAILDIR
#include "mn-maildir-mailbox-backend.h"
#endif
#if WITH_SYLPHEED
#include "mn-sylpheed-mailbox-backend.h"
#endif
#include "mn-vfs-mailbox-backend-private.h"
#include "mn-vfs.h"
#include "mn-stock.h"
#include "mn-conf.h"
#include "mn-util.h"
#include "mn-locked-callback.h"

GType mn_vfs_mailbox_backend_types[MN_VFS_MAILBOX_MAX_BACKEND_TYPES + 1];

typedef struct
{
  char				*uri;
  MNVFSMailboxBackend		*owner;
  GnomeVFSMonitorType		type;
  GnomeVFSMonitorCallback	callback;
  gpointer			user_data;

  GnomeVFSResult		result;
  GnomeVFSMonitorHandle		*handle;
} Monitor;
%}

class MN:VFS:Mailbox from MN:Reentrant:Mailbox (abstract)
{
  public char *uri destroywith g_free;
  public GnomeVFSURI *vfs_uri unrefwith gnome_vfs_uri_unref;

  private GSList *monitors;
  private MNVFSMailboxBackend *backend unrefwith g_object_unref;
  private unsigned int update_poll_id;

  private GMutex *mutex = {g_mutex_new()} destroywith g_mutex_free;

  public void
    init_types (void)
  {
    int i = 0;

#if WITH_MBOX
    mn_vfs_mailbox_backend_types[i++] = MN_TYPE_MBOX_MAILBOX_BACKEND;
#endif
#if WITH_MOZILLA
    mn_vfs_mailbox_backend_types[i++] = MN_TYPE_MOZILLA_MAILBOX_BACKEND;
#endif
    /*
     * Claws Mail mailboxes contain both a .sylpheed_mark and a
     * .mh_sequences file. List the Sylpheed backend first, in order
     * to treat them as Sylpheed mailboxes. This way, the need to use
     * rcvstore from procmail is removed.
     */
#if WITH_SYLPHEED
    mn_vfs_mailbox_backend_types[i++] = MN_TYPE_SYLPHEED_MAILBOX_BACKEND;
#endif
#if WITH_MH
    mn_vfs_mailbox_backend_types[i++] = MN_TYPE_MH_MAILBOX_BACKEND;
#endif
#if WITH_MAILDIR
    mn_vfs_mailbox_backend_types[i++] = MN_TYPE_MAILDIR_MAILBOX_BACKEND;
#endif
    mn_vfs_mailbox_backend_types[i] = 0;
  }

  class_init (class)
  {
    /*
     * Most VFS mailboxes will probably be local files, or remote
     * files located in the same LAN. We therefore choose a small
     * default check delay (1 minute).
     */
    MN_MAILBOX_CLASS(class)->default_check_delay = 60 * 1;
  }

  constructor (self)
  {
    /* set default poll to the most likely value */
    mn_mailbox_set_poll(MN_MAILBOX(self), FALSE);
  }

  dispose (self)
  {
    /*
     * We are no longer operational after dispose (see unrefwith
     * above), so we must remove our monitors.
     */
    mn_source_clear(&selfp->update_poll_id);
    mn_g_slist_clear_deep_custom(&selfp->monitors, (GFunc) self_monitor_free, NULL);
  }

  override (MN:Mailbox) void
    seal (MNMailbox *mailbox)
  {
    Self *self = SELF(mailbox);

    PARENT_HANDLER(mailbox);

    if (self->uri)
      {
	self->vfs_uri = gnome_vfs_uri_new(self->uri);

	if (! mailbox->stock_id && self->vfs_uri && ! gnome_vfs_uri_is_local(self->vfs_uri))
	  mn_mailbox_set_stock_id(mailbox, MN_STOCK_REMOTE);

	if (! mailbox->runtime_name)
	  {
	    char *path;

	    path = gnome_vfs_get_local_path_from_uri(self->uri);
	    if (path)
	      {
		mailbox->runtime_name = g_filename_display_name(path);
		g_free(path);
	      }
	    else
	      mailbox->runtime_name = self->vfs_uri
		? gnome_vfs_uri_to_string(self->vfs_uri, GNOME_VFS_URI_HIDE_PASSWORD)
		: g_strdup(self->uri);
	  }
      }

    if (! mailbox->stock_id)
      mn_mailbox_set_stock_id(mailbox, MN_STOCK_LOCAL);
  }

  private Monitor *
    find_monitor (self,
		  const char *uri (check null),
		  MNVFSMailboxBackend *owner,
		  GnomeVFSMonitorType type,
		  GnomeVFSMonitorCallback callback,
		  gpointer user_data)
  {
    GSList *l;

    MN_LIST_FOREACH(l, selfp->monitors)
      {
	Monitor *monitor = l->data;

	if (monitor->owner == owner
	    && monitor->type == type
	    && monitor->callback == callback
	    && monitor->user_data == user_data
	    && ! strcmp(monitor->uri, uri))
	  return monitor;
      }

    return NULL;
  }

  protected void
    monitor (self,
	     const char *uri (check null),
	     MNVFSMailboxBackend *owner,
	     GnomeVFSMonitorType type,
	     GnomeVFSMonitorCallback callback,
	     gpointer user_data)
  {
    Monitor *monitor;

    g_return_if_fail(owner == NULL || MN_IS_VFS_MAILBOX_BACKEND(owner));

    monitor = self_find_monitor(self, uri, owner, type, callback, user_data);
    if (! monitor)
      {
	monitor = g_new0(Monitor, 1);
	monitor->uri = g_strdup(uri);
	monitor->owner = owner;
	monitor->type = type;
	monitor->callback = callback;
	monitor->user_data = user_data;
	monitor->result = -1;

	selfp->monitors = g_slist_append(selfp->monitors, monitor);
      }

    if (monitor->result != GNOME_VFS_OK)
      {
	GnomeVFSResult result;

	result = mn_gnome_vfs_monitor_add_locked(&monitor->handle, uri, type, callback, user_data);
	if (result != monitor->result)
	  {
	    monitor->result = result;
	    if (result != GNOME_VFS_OK)
	      mn_mailbox_warning(MN_MAILBOX(self), _("unable to monitor %s (%s), poll mode activated"), uri, gnome_vfs_result_to_string(result));

	    self_queue_update_poll(self);
	  }
      }
  }

  private void
    monitor_free (Monitor *monitor (check null))
  {
    g_free(monitor->uri);

    if (monitor->result == GNOME_VFS_OK)
      mn_gnome_vfs_monitor_cancel_locked(monitor->handle);

    g_free(monitor);
  }

  private void
    monitor_cb (GnomeVFSMonitorHandle *handle,
		const char *monitor_uri,
		const char *info_uri,
		GnomeVFSMonitorEventType event_type,
		gpointer user_data)
  {
    if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED
	|| event_type == GNOME_VFS_MONITOR_EVENT_DELETED
	|| event_type == GNOME_VFS_MONITOR_EVENT_CREATED)
      {
	Self *self = user_data;
	MNVFSMailboxBackend *backend;

	/*
	 * Queue a check to detect if the mailbox type has changed. If
	 * the mailbox has a backend, we must use
	 * mn_vfs_mailbox_backend_queue_check() (which respects the
	 * check_latency member of MNVFSMailboxBackend) in order to
	 * avoid race conditions.
	 */

	self_lock(self);

	backend = selfp->backend;
	if (backend)
	  mn_vfs_mailbox_backend_queue_check(backend);

	self_unlock(self);

	/*
	 * Note that at this point, we have released the lock and the
	 * backend might have been finalized by another thread: we can
	 * only test the nullity of the pointer.
	 */
	if (! backend)
	  mn_reentrant_mailbox_queue_check(MN_REENTRANT_MAILBOX(self));
      }
  }

  protected void
    remove_monitors_by_owner (self, MNVFSMailboxBackend *owner)
  {
    GSList *l;

    g_return_if_fail(owner == NULL || MN_IS_VFS_MAILBOX_BACKEND(owner));

  start:
    MN_LIST_FOREACH(l, selfp->monitors)
      {
	Monitor *monitor = l->data;

	if (monitor->owner == owner)
	  {
	    selfp->monitors = mn_g_slist_delete_link_deep_custom(selfp->monitors, l, (GFunc) self_monitor_free, NULL);
	    self_queue_update_poll(self);
	    goto start;
	  }
      }
  }

  private void
    queue_update_poll (self)
  {
    if (! selfp->update_poll_id)
      selfp->update_poll_id = gdk_threads_add_idle(self_update_poll_cb, self);
  }

  private gboolean
    update_poll_cb (gpointer data)
  {
    Self *self = data;
    gboolean polled = FALSE;
    GSList *l;

    self_lock(self);

    MN_LIST_FOREACH(l, selfp->monitors)
      {
	Monitor *monitor = l->data;

	if (monitor->result != GNOME_VFS_OK)
	  {
	    polled = TRUE;
	    break;
	  }
      }

    selfp->update_poll_id = 0;

    self_unlock(self);

    if (mn_mailbox_get_poll(MN_MAILBOX(self)) != polled)
      mn_mailbox_set_poll(MN_MAILBOX(self), polled);

    return FALSE;		/* remove source */
  }

  override (MN:Reentrant:Mailbox) void
    reentrant_check (MNReentrantMailbox *mailbox, int check_id)
  {
    Self *self = SELF(mailbox);
    MNVFSMailboxBackend *backend;
    MNVFSMailboxBackendClass *class;
    GType backend_type = 0;
    gboolean exists;

    self_lock(self);

    /*
     * Install the monitors that detect a mailbox type change. These
     * monitors are not meant to detect if the mail status of a
     * mailbox changes (it is the responsability of the backend),
     * although they will do so with some mailbox formats.
     *
     * For properly detecting the new mailbox type when the file or
     * directory changes we need install both a file and a directory
     * monitor, since depending on the underlying implementation we
     * might not get file events if the URI is a directory, etc.
     */
    self_monitor(self, self->uri, NULL, GNOME_VFS_MONITOR_FILE, self_monitor_cb, self);
    self_monitor(self, self->uri, NULL, GNOME_VFS_MONITOR_DIRECTORY, self_monitor_cb, self);

    backend = selfp->backend;
    if (backend)
      g_object_ref(backend);

    self_unlock(self);

    class = backend ? MN_VFS_MAILBOX_BACKEND_GET_CLASS(backend) : NULL;
    if (class && class->is(NULL, class, self))
      backend_type = G_OBJECT_TYPE(backend);
    else
      {
	exists = mn_vfs_test(self->vfs_uri, G_FILE_TEST_EXISTS);
	if (exists)
	  {
	    int i;

	    for (i = 0; mn_vfs_mailbox_backend_types[i]; i++)
	      {
		class = g_type_class_peek(mn_vfs_mailbox_backend_types[i]);
		g_assert(class != NULL);

		if (class->is(NULL, class, self))
		  {
		    backend_type = mn_vfs_mailbox_backend_types[i];
		    break;
		  }

		if (mn_reentrant_mailbox_check_aborted(mailbox, check_id))
		  goto end;
	      }
	  }
      }

    if (! backend || backend_type != G_OBJECT_TYPE(backend))
      {
	MNVFSMailboxBackend *new_backend = NULL;

	if (! mn_reentrant_mailbox_check_aborted(mailbox, check_id))
	  {
	    GDK_THREADS_ENTER();

	    if (backend_type)
	      {
		new_backend = g_object_new(backend_type, MN_VFS_MAILBOX_BACKEND_PROP_MAILBOX(self), NULL);
		mn_mailbox_set_format(MN_MAILBOX(self), (char *) MN_VFS_MAILBOX_BACKEND_GET_CLASS(new_backend)->format);
	      }
	    else
	      {
		mn_mailbox_set_format(MN_MAILBOX(self), NULL);
		mn_mailbox_set_error(MN_MAILBOX(self), exists ? _("unknown mailbox format") : _("does not exist"));
	      }

	    gdk_flush();
	    GDK_THREADS_LEAVE();
	  }

	self_lock(self);

	if (backend)
	  {
	    g_object_unref(backend);
	    backend = NULL;
	  }

	if (! mn_reentrant_mailbox_check_aborted(mailbox, check_id))
	  {
	    if (selfp->backend)
	      {
		g_object_unref(selfp->backend);
		selfp->backend = NULL;
	      }

	    if (new_backend)
	      {
		selfp->backend = backend = new_backend;
		new_backend = NULL;

		g_object_ref(backend);
	      }
	  }

	self_unlock(self);

	if (new_backend)
	  g_object_unref(new_backend);
      }

    if (backend)
      mn_vfs_mailbox_backend_check(backend, check_id);

  end:
    if (backend)
      {
	self_lock(self);
	g_object_unref(backend);
	self_unlock(self);
      }
  }

  public void
    lock (self)
  {
    g_mutex_lock(selfp->mutex);
  }

  public void
    unlock (self)
  {
    g_mutex_unlock(selfp->mutex);
  }
}