gio: Prevent hang when finalizing GThreadedSocketService
[platform/upstream/glib.git] / gio / glocalfilemonitor.c
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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include "gioenumtypes.h"
24 #include "glocalfilemonitor.h"
25 #include "giomodule-priv.h"
26 #include "gioerror.h"
27 #include "glibintl.h"
28
29 #include <string.h>
30
31
32 enum
33 {
34   PROP_0,
35   PROP_FILENAME,
36   PROP_FLAGS
37 };
38
39 G_DEFINE_ABSTRACT_TYPE (GLocalFileMonitor, g_local_file_monitor, G_TYPE_FILE_MONITOR)
40
41 static void
42 g_local_file_monitor_init (GLocalFileMonitor* local_monitor)
43 {
44 }
45
46 static void
47 g_local_file_monitor_set_property (GObject      *object,
48                                    guint         property_id,
49                                    const GValue *value,
50                                    GParamSpec   *pspec)
51 {
52   GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
53
54   switch (property_id)
55     {
56     case PROP_FILENAME:
57       local_monitor->filename = g_value_dup_string (value);
58       break;
59
60     case PROP_FLAGS:
61       local_monitor->flags = g_value_get_flags (value);
62       break;
63
64     default:
65       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
66       break;
67     }
68 }
69
70 void
71 g_local_file_monitor_start (GLocalFileMonitor *local_monitor)
72 {
73   GLocalFileMonitorClass *class;
74
75   class = G_LOCAL_FILE_MONITOR_GET_CLASS (local_monitor);
76
77   if (class->start)
78     class->start (local_monitor);
79 }
80
81 static void
82 g_local_file_monitor_finalize (GObject *object)
83 {
84   GLocalFileMonitor *local_monitor = G_LOCAL_FILE_MONITOR (object);
85
86   g_free (local_monitor->filename);
87
88   G_OBJECT_CLASS (g_local_file_monitor_parent_class)->finalize (object);
89 }
90
91 static void g_local_file_monitor_class_init (GLocalFileMonitorClass *klass)
92 {
93   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94
95   gobject_class->set_property = g_local_file_monitor_set_property;
96   gobject_class->finalize = g_local_file_monitor_finalize;
97
98   g_object_class_install_property (gobject_class, 
99                                    PROP_FILENAME,
100                                    g_param_spec_string ("filename", 
101                                                         P_("File name"), 
102                                                         P_("File name to monitor"),
103                                                         NULL, 
104                                                         G_PARAM_CONSTRUCT_ONLY|
105                                                         G_PARAM_WRITABLE|
106                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
107
108   g_object_class_install_property (gobject_class,
109                                    PROP_FLAGS,
110                                    g_param_spec_flags ("flags",
111                                                        P_("Monitor flags"),
112                                                        P_("Monitor flags"),
113                                                        G_TYPE_FILE_MONITOR_FLAGS,
114                                                        0,
115                                                        G_PARAM_CONSTRUCT_ONLY|
116                                                        G_PARAM_WRITABLE|
117                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
118 }
119
120 GFileMonitor*
121 _g_local_file_monitor_new (const char         *pathname,
122                            GFileMonitorFlags   flags,
123                            GMainContext       *context,
124                            gboolean            is_remote_fs,
125                            gboolean            do_start,
126                            GError            **error)
127 {
128   GFileMonitor *monitor = NULL;
129   GType type = G_TYPE_INVALID;
130
131   if (is_remote_fs)
132     type = _g_io_module_get_default_type (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME,
133                                           "GIO_USE_FILE_MONITOR",
134                                           G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));
135
136   if (type == G_TYPE_INVALID)
137     type = _g_io_module_get_default_type (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
138                                           "GIO_USE_FILE_MONITOR",
139                                           G_STRUCT_OFFSET (GLocalFileMonitorClass, is_supported));
140
141   if (type != G_TYPE_INVALID)
142     monitor = G_FILE_MONITOR (g_object_new (type, "filename", pathname, "flags", flags, "context", context, NULL));
143   else
144     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
145                          _("Unable to find default local file monitor type"));
146
147   if (monitor && do_start)
148     g_local_file_monitor_start (G_LOCAL_FILE_MONITOR (monitor));
149
150   return monitor;
151 }