1 /* GIO - GLib Input, Output and Streaming Library 2 * 3 * Copyright (C) 2006-2007 Red Hat, Inc. 4 * Copyright (C) 2007 Sebastian Drge. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General 17 * Public License along with this library; if not, write to the 18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 19 * Boston, MA 02111-1307, USA. 20 * 21 * Authors: Alexander Larsson <alexl (at) redhat.com> 22 * John McCutchan <john (at) johnmccutchan.com> 23 * Sebastian Drge <slomo (at) circular-chaos.org> 24 */ 25 26 #include "config.h" 27 28 #include "gfamfilemonitor.h" 29 #include "giomodule.h" 30 31 #include "fam-helper.h" 32 33 struct _GFamFileMonitor 34 { 35 GLocalFileMonitor parent_instance; 36 fam_sub *sub; 37 }; 38 39 static gboolean g_fam_file_monitor_cancel (GFileMonitor* monitor); 40 41 G_DEFINE_DYNAMIC_TYPE (GFamFileMonitor, g_fam_file_monitor, G_TYPE_LOCAL_FILE_MONITOR) 42 43 static void 44 g_fam_file_monitor_finalize (GObject *object) 45 { 46 GFamFileMonitor *fam_monitor = G_FAM_FILE_MONITOR (object); 47 fam_sub *sub = fam_monitor->sub; 48 49 if (sub) { 50 if (!_fam_sub_cancel (sub)) 51 g_warning ("Unexpected error cancelling fam monitor"); 52 fam_monitor->sub = NULL; 53 } 54 55 if (G_OBJECT_CLASS (g_fam_file_monitor_parent_class)->finalize) 56 (*G_OBJECT_CLASS (g_fam_file_monitor_parent_class)->finalize) (object); 57 } 58 59 static GObject * 60 g_fam_file_monitor_constructor (GType type, 61 guint n_construct_properties, 62 GObjectConstructParam *construct_properties) 63 { 64 GObject *obj; 65 GFamFileMonitorClass *klass; 66 GObjectClass *parent_class; 67 GFamFileMonitor *fam_monitor; 68 const gchar *filename = NULL; 69 fam_sub *sub = NULL; 70 71 klass = G_FAM_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_FAM_FILE_MONITOR)); 72 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass)); 73 obj = parent_class->constructor (type, 74 n_construct_properties, 75 construct_properties); 76 77 fam_monitor = G_FAM_FILE_MONITOR (obj); 78 79 filename = G_LOCAL_FILE_MONITOR (obj)->filename; 80 81 g_assert (filename != NULL); 82 83 sub = _fam_sub_add (filename, FALSE, fam_monitor); 84 /* FIXME: what to do about errors here? we can't return NULL or another 85 * kind of error and an assertion is probably too hard */ 86 g_assert (sub != NULL); 87 88 fam_monitor->sub = sub; 89 90 return obj; 91 } 92 93 static void 94 g_fam_file_monitor_class_finalize (GFamFileMonitorClass *klass) 95 { 96 } 97 98 static gboolean 99 g_fam_file_monitor_is_supported (void) 100 { 101 return _fam_sub_startup (); 102 } 103 104 static void 105 g_fam_file_monitor_class_init (GFamFileMonitorClass* klass) 106 { 107 GObjectClass* gobject_class = G_OBJECT_CLASS (klass); 108 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass); 109 GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass); 110 111 gobject_class->finalize = g_fam_file_monitor_finalize; 112 gobject_class->constructor = g_fam_file_monitor_constructor; 113 file_monitor_class->cancel = g_fam_file_monitor_cancel; 114 115 local_file_monitor_class->is_supported = g_fam_file_monitor_is_supported; 116 } 117 118 static void 119 g_fam_file_monitor_init (GFamFileMonitor* monitor) 120 { 121 122 } 123 124 static gboolean 125 g_fam_file_monitor_cancel (GFileMonitor* monitor) 126 { 127 GFamFileMonitor *fam_monitor = G_FAM_FILE_MONITOR (monitor); 128 fam_sub *sub = fam_monitor->sub; 129 130 if (sub) { 131 if (!_fam_sub_cancel (sub)) 132 g_warning ("Unexpected error cancelling fam monitor"); 133 fam_monitor->sub = NULL; 134 } 135 136 if (G_FILE_MONITOR_CLASS (g_fam_file_monitor_parent_class)->cancel) 137 (*G_FILE_MONITOR_CLASS (g_fam_file_monitor_parent_class)->cancel) (monitor); 138 139 return TRUE; 140 } 141 142 void 143 g_fam_file_monitor_register (GIOModule *module) 144 { 145 g_fam_file_monitor_register_type (G_TYPE_MODULE (module)); 146 g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME, 147 G_TYPE_FAM_FILE_MONITOR, 148 "fam", 149 10); 150 } 151 152