mail-notification

Fork of Jean-Yves Lefort's mail-notification, a tray icon to notify of new mail
git clone https://code.djc.id.au/git/mail-notification/

src/mn-vfs-mailbox-backend.gob (4151B) - raw

      1 /*
      2  * Mail Notification
      3  * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation; either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License along
     16  * with this program; if not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 %headertop{
     21 #include "mn-vfs-mailbox.h"
     22 %}
     23 
     24 %{
     25 #include <glib/gi18n.h>
     26 #include <gtk/gtk.h>
     27 #include "mn-mailbox-private.h"
     28 #include "mn-reentrant-mailbox-private.h"
     29 #include "mn-vfs-mailbox-private.h"
     30 #include "mn-conf.h"
     31 #include "mn-util.h"
     32 %}
     33 
     34 class MN:VFS:Mailbox:Backend from G:Object (abstract)
     35 {
     36   classwide const char *format;
     37 
     38   /*
     39    * In order to not create reference cycles, we do not hold a
     40    * reference to the mailbox. The code is arranged so that a VFS
     41    * backend cannot survive its containing mailbox (no reference to
     42    * the backend is held outside of the mailbox code).
     43    */
     44   protected MNVFSMailbox *mailbox;
     45   property POINTER mailbox (link, flags = CONSTRUCT_ONLY, type = MNVFSMailbox *);
     46 
     47   private unsigned int queue_check_id;
     48 
     49   /*
     50    * The amount of time to wait between a queue_check() call and the
     51    * actual mailbox check, in milliseconds. If 0, the check is
     52    * performed immediately. Used to avoid race conditions with mailbox
     53    * formats which do not support locking.
     54    */
     55   protected int check_latency;
     56 
     57   finalize (self)
     58   {
     59     if (selfp->queue_check_id)
     60       g_source_remove(selfp->queue_check_id);
     61 
     62     /*
     63      * If the calling mailbox is being finalized, there is no need to
     64      * hold its lock. Otherwise, it holds its lock while finalizing us
     65      * (see reentrant_check() in mn-vfs-mailbox.gob).
     66      */
     67     mn_vfs_mailbox_remove_monitors_by_owner(self->mailbox, self);
     68   }
     69 
     70   protected void
     71     monitor (self,
     72 	     int check_id,
     73 	     const char *uri (check null),
     74 	     GnomeVFSMonitorType type)
     75   {
     76     mn_vfs_mailbox_lock(self->mailbox);
     77     mn_vfs_mailbox_monitor(self->mailbox, uri, self, type, self_real_monitor_cb, self);
     78     mn_vfs_mailbox_unlock(self->mailbox);
     79   }
     80 
     81   private void
     82     real_monitor_cb (GnomeVFSMonitorHandle *handle,
     83 		     const char *monitor_uri,
     84 		     const char *info_uri,
     85 		     GnomeVFSMonitorEventType event_type,
     86 		     gpointer user_data)
     87   {
     88     self_monitor_cb(user_data, info_uri, event_type);
     89   }
     90 
     91   virtual private void
     92     monitor_cb (self,
     93 		const char *info_uri,
     94 		GnomeVFSMonitorEventType event_type)
     95   {
     96     if (event_type == GNOME_VFS_MONITOR_EVENT_CHANGED
     97 	|| event_type == GNOME_VFS_MONITOR_EVENT_DELETED
     98 	|| event_type == GNOME_VFS_MONITOR_EVENT_CREATED)
     99       self_queue_check(self);
    100   }
    101 
    102   /**
    103    * queue_check():
    104    * @self: a backend
    105    *
    106    * Queues a mail check on the mailbox of @self or replaces an
    107    * already queued check. The check_latency member is respected.
    108    *
    109    * This function is not thread-safe. It is only called from main
    110    * loop callbacks (hence always from the same thread).
    111    **/
    112   protected void
    113     queue_check (self)
    114   {
    115     if (selfp->queue_check_id)
    116       mn_source_clear(&selfp->queue_check_id);
    117 
    118     if (self->check_latency)
    119       selfp->queue_check_id = gdk_threads_add_timeout(self->check_latency, self_queue_check_cb, self);
    120     else
    121       mn_reentrant_mailbox_queue_check(MN_REENTRANT_MAILBOX(self->mailbox));
    122   }
    123 
    124   private gboolean
    125     queue_check_cb (gpointer data)
    126   {
    127     Self *self = data;
    128 
    129     mn_mailbox_check(MN_MAILBOX(self->mailbox));
    130 
    131     selfp->queue_check_id = 0;
    132     return FALSE;		/* remove source */
    133   }
    134 
    135   virtual public gboolean
    136     is (self, MNVFSMailboxBackendClass *class, MN:VFS:Mailbox *mailbox);
    137 
    138   virtual public void
    139     check (self, int check_id (check != 0));
    140 }