Home | History | Annotate | Download | only in gio
      1 /* GIO - GLib Input, Output and Streaming Library
      2  *
      3  * Copyright (C) 2006-2007 Red Hat, Inc.
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Lesser General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library 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 GNU
     13  * Lesser General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Lesser General
     16  * Public License along with this library; if not, write to the
     17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
     18  * Boston, MA 02111-1307, USA.
     19  *
     20  * Author: Alexander Larsson <alexl (at) redhat.com>
     21  */
     22 
     23 #include "config.h"
     24 
     25 #include "glocalfilemonitor.h"
     26 #include "giomodule-priv.h"
     27 #include "gioerror.h"
     28 #include "glibintl.h"
     29 
     30 #include <string.h>
     31 
     32 #include "gioalias.h"
     33 
     34 enum
     35 {
     36   PROP_0,
     37   PROP_FILENAME
     38 };
     39 
     40 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
     41 
     42 static void
     43 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
     44 {
     45 }
     46 
     47 static void
     48 g_local_file_monitor_set_property (GObject      *object,
     49                                    guint         property_id,
     50                                    const GValue *value,
     51                                    GParamSpec   *pspec)
     52 {
     53   switch (property_id)
     54   {
     55     case PROP_FILENAME:
     56       /* Do nothing */
     57       break;
     58     default:
     59       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     60       break;
     61   }
     62 }
     63 
     64 static GObject *
     65 g_local_file_monitor_constructor (GType                  type,
     66                                   guint                  n_construct_properties,
     67                                   GObjectConstructParam *construct_properties)
     68 {
     69   GObject *obj;
     70   GLocalFileMonitorClass *klass;
     71   GObjectClass *parent_class;
     72   GLocalFileMonitor *local_monitor;
     73   const gchar *filename = NULL;
     74   gint i;
     75 
     76   klass = G_LOCAL_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_FILE_MONITOR));
     77   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
     78   obj = parent_class->constructor (type,
     79                                    n_construct_properties,
     80                                    construct_properties);
     81 
     82   local_monitor = G_LOCAL_FILE_MONITOR (obj);
     83 
     84   for (i = 0; i < n_construct_properties; i++)
     85     {
     86       if (strcmp ("filename", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
     87         {
     88           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
     89           filename = g_value_get_string (construct_properties[i].value);
     90           break;
     91         }
     92     }
     93 
     94   g_warn_if_fail (filename != NULL);
     95 
     96   local_monitor->filename = g_strdup (filename);
     97   return obj;
     98 }
     99 
    100 static void
    101 g_local_file_monitor_finalize (GObject *object)
    102 {
    103   GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
    104   if (local_monitor->filename)
    105     {
    106       g_free (local_monitor->filename);
    107       local_monitor->filename = NULL;
    108     }
    109 
    110   G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize (object);
    111 }
    112 
    113 static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass)
    114 {
    115   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    116 
    117   gobject_class->set_property = g_local_file_monitor_set_property;
    118   gobject_class->finalize = g_local_file_monitor_finalize;
    119   gobject_class->constructor = g_local_file_monitor_constructor;
    120 
    121   g_object_class_install_property (gobject_class,
    122                                    PROP_FILENAME,
    123                                    g_param_spec_string ("filename",
    124                                                         P_("File name"),
    125                                                         P_("File name to monitor"),
    126                                                         NULL,
    127                                                         G_PARAM_CONSTRUCT_ONLY|
    128                                                         G_PARAM_WRITABLE|
    129                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
    130 }
    131 
    132 static gpointer
    133 get_default_local_file_monitor (gpointer data)
    134 {
    135   GLocalFileMonitorClass *chosen_class;
    136   GLocalFileMonitorClass **ret = data;
    137   GIOExtensionPoint *ep;
    138   GList *extensions, *l;
    139 
    140   _g_io_modules_ensure_loaded ();
    141 
    142   ep = g_io_extension_point_lookup (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
    143 
    144   extensions = g_io_extension_point_get_extensions (ep);
    145 
    146   chosen_class = NULL;
    147   for (l = extensions; l != NULL; l = l->next)
    148     {
    149       GIOExtension *extension = l->data;
    150       GLocalFileMonitorClass *klass;
    151 
    152       klass = G_LOCAL_FILE_MONITOR_CLASS (g_io_extension_ref_class (extension));
    153 
    154       if (klass->is_supported ())
    155 	{
    156 	  chosen_class = klass;
    157 	  break;
    158 	}
    159       else
    160 	g_type_class_unref (klass);
    161     }
    162 
    163   if (chosen_class)
    164     {
    165       *ret = chosen_class;
    166       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
    167     }
    168   else
    169     return (gpointer)G_TYPE_INVALID;
    170 }
    171 
    172 /**
    173  * g_local_file_monitor_new:
    174  * @pathname: path name to monitor.
    175  * @flags: #GFileMonitorFlags.
    176  *
    177  * Returns: a new #GFileMonitor for the given @pathname.
    178  **/
    179 GFileMonitor*
    180 _g_local_file_monitor_new (const char         *pathname,
    181 			   GFileMonitorFlags   flags,
    182 			   GError            **error)
    183 {
    184   static GOnce once_init = G_ONCE_INIT;
    185   GTypeClass *type_class;
    186   GFileMonitor *monitor;
    187   GType type;
    188 
    189   type_class = NULL;
    190   g_once (&once_init, get_default_local_file_monitor, &type_class);
    191   type = (GType)once_init.retval;
    192 
    193   monitor = NULL;
    194   if (type != G_TYPE_INVALID)
    195     monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, NULL));
    196   else
    197     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
    198                          _("Unable to find default local file monitor type"));
    199 
    200   /* This is non-null on first pass here. Unref the class now.
    201    * This is to avoid unloading the module and then loading it
    202    * again which would happen if we unrefed the class
    203    * before creating the monitor.
    204    */
    205 
    206   if (type_class)
    207     g_type_class_unref (type_class);
    208 
    209   return monitor;
    210 }
    211 
    212 #define __G_LOCAL_FILE_MONITOR_C__
    213 #include "gioaliasdef.c"
    214