Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / fen / gfenfilemonitor.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set expandtab ts=4 shiftwidth=4: */
3 /* GIO - GLib Input, Output and Streaming Library
4  * 
5  * Copyright (C) 2006-2007 Red Hat, Inc.
6  * Copyright (C) 2007 Sebastian Dröge.
7  * Copyright (c) 2008, 2010 Oracle and/or its affiliates, Inc. All rights
8  * reserved.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General
21  * Public License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * Authors: Alexander Larsson <alexl@redhat.com>
26  *          John McCutchan <john@johnmccutchan.com> 
27  *          Sebastian Dröge <slomo@circular-chaos.org>
28  *          Lin Ma <lin.ma@sun.com>
29  */
30
31 #include "config.h"
32
33 #include "gfenfilemonitor.h"
34 #include <gio/giomodule.h>
35
36 #include "fen-helper.h"
37
38 struct _GFenFileMonitor
39 {
40     GLocalFileMonitor parent_instance;
41     gboolean enabled;
42 };
43
44 static gboolean g_fen_file_monitor_cancel (GFileMonitor* monitor);
45
46 #define g_fen_file_monitor_get_type _g_fen_file_monitor_get_type
47 G_DEFINE_TYPE_WITH_CODE (GFenFileMonitor, g_fen_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
48   g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
49     g_define_type_id,
50     "fen",
51     20))
52
53 static void
54 g_fen_file_monitor_finalize (GObject *object)
55 {
56     GFenFileMonitor *self = G_FEN_FILE_MONITOR (object);
57     
58     if (self->enabled) {
59         fen_remove (G_LOCAL_FILE_MONITOR (self)->filename, self, FALSE);
60         self->enabled = FALSE;
61     }
62     
63     if (G_OBJECT_CLASS (g_fen_file_monitor_parent_class)->finalize)
64         (*G_OBJECT_CLASS (g_fen_file_monitor_parent_class)->finalize) (object);
65 }
66
67 static GObject *
68 g_fen_file_monitor_constructor (GType type,
69   guint n_construct_properties,
70   GObjectConstructParam *construct_properties)
71 {
72     GObject *obj;
73     GFenFileMonitorClass *klass;
74     GObjectClass *parent_class;
75     GFenFileMonitor *self;
76     const gchar *filename = NULL;
77   
78     klass = G_FEN_FILE_MONITOR_CLASS (g_type_class_peek (G_TYPE_FEN_FILE_MONITOR));
79     parent_class = g_fen_file_monitor_parent_class;
80     obj = parent_class->constructor (type,
81       n_construct_properties,
82       construct_properties);
83
84     self = G_FEN_FILE_MONITOR (obj);
85
86     filename = G_LOCAL_FILE_MONITOR (obj)->filename;
87
88     g_assert (filename != NULL);
89
90     /* Will never fail as is_supported() should be called before instanciating
91      * anyway */
92     if (!fen_init ())
93         g_assert_not_reached ();
94     
95     /* FIXME: what to do about errors here? we can't return NULL or another
96      * kind of error and an assertion is probably too hard */
97     fen_add (filename, self, FALSE);
98     self->enabled = TRUE;
99
100     return obj;
101 }
102
103 static gboolean
104 g_fen_file_monitor_is_supported (void)
105 {
106     return fen_init ();
107 }
108
109 static void
110 g_fen_file_monitor_class_init (GFenFileMonitorClass* klass)
111 {
112     GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
113     GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
114     GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
115   
116     gobject_class->finalize = g_fen_file_monitor_finalize;
117     gobject_class->constructor = g_fen_file_monitor_constructor;
118     file_monitor_class->cancel = g_fen_file_monitor_cancel;
119
120     local_file_monitor_class->is_supported = g_fen_file_monitor_is_supported;
121 }
122
123 static void
124 g_fen_file_monitor_init (GFenFileMonitor* monitor)
125 {
126 }
127
128 static gboolean
129 g_fen_file_monitor_cancel (GFileMonitor* monitor)
130 {
131     GFenFileMonitor *self = G_FEN_FILE_MONITOR (monitor);
132     
133     if (self->enabled) {
134         fen_remove (G_LOCAL_FILE_MONITOR (self)->filename, self, FALSE);
135         self->enabled = FALSE;
136     }
137     
138     if (G_FILE_MONITOR_CLASS (g_fen_file_monitor_parent_class)->cancel)
139         (*G_FILE_MONITOR_CLASS (g_fen_file_monitor_parent_class)->cancel) (monitor);
140
141     return TRUE;
142 }