Win32: Port Directory Monitoring to New GLocalFileMonitor
[platform/upstream/glib.git] / gio / win32 / gwin32filemonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  * Copyright (C) 2006-2007 Red Hat, Inc.
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, see <http://www.gnu.org/licenses/>.
18  *
19  * Author: Vlad Grecescu <b100dian@gmail.com>
20  * Author: Chun-wei Fan <fanc999@yahoo.com.tw>
21  *
22  */
23
24 #include "config.h"
25
26 #include "gwin32filemonitor.h"
27 #include "gwin32fsmonitorutils.h"
28
29 #include <windows.h>
30
31 G_DEFINE_TYPE_WITH_CODE (GWin32FileMonitor, g_win32_file_monitor, G_TYPE_LOCAL_FILE_MONITOR,
32                          g_io_extension_point_implement (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME,
33                                                          g_define_type_id, "win32filemonitor", 20))
34
35 static void
36 g_win32_file_monitor_start (GLocalFileMonitor *monitor,
37                             const gchar *dirname,
38                             const gchar *basename,
39                             const gchar *filename,
40                             GFileMonitorSource *source)
41 {
42   GWin32FileMonitor *win32_monitor = G_WIN32_FILE_MONITOR (monitor);
43   gboolean isfile = (filename == NULL && basename == NULL) ? FALSE : TRUE;
44
45   win32_monitor->priv->fms = source;
46
47   if (isfile)
48     if (basename != NULL)
49       g_win32_fs_monitor_init (win32_monitor->priv, dirname, basename, TRUE);
50     else
51       g_win32_fs_monitor_init (win32_monitor->priv, NULL, filename, TRUE);
52   else
53     g_win32_fs_monitor_init (win32_monitor->priv, dirname, NULL, FALSE);
54 }
55
56 static gboolean
57 g_win32_file_monitor_is_supported (void)
58 {
59   return TRUE;
60 }
61
62 static void
63 g_win32_file_monitor_init (GWin32FileMonitor* monitor)
64 {
65   monitor->priv = g_win32_fs_monitor_create (TRUE);
66
67   monitor->priv->self = G_FILE_MONITOR (monitor);
68 }
69
70 static void
71 g_win32_file_monitor_finalize (GObject *base)
72 {
73   GWin32FileMonitor *monitor;
74   monitor = G_WIN32_FILE_MONITOR (base);
75
76   g_win32_fs_monitor_finalize (monitor->priv);
77
78   if (G_OBJECT_CLASS (g_win32_file_monitor_parent_class)->finalize)
79     (*G_OBJECT_CLASS (g_win32_file_monitor_parent_class)->finalize) (base);
80 }
81
82 static gboolean
83 g_win32_file_monitor_cancel (GFileMonitor* monitor)
84 {
85   GWin32FileMonitor *file_monitor;
86   file_monitor = G_WIN32_FILE_MONITOR (monitor);
87
88   g_win32_fs_monitor_close_handle (file_monitor->priv);
89
90   return TRUE;
91 }
92
93 static void
94 g_win32_file_monitor_class_init (GWin32FileMonitorClass *klass)
95 {
96   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
97   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
98   GLocalFileMonitorClass *local_file_monitor_class = G_LOCAL_FILE_MONITOR_CLASS (klass);
99
100   gobject_class->finalize = g_win32_file_monitor_finalize;
101   file_monitor_class->cancel = g_win32_file_monitor_cancel;
102
103   local_file_monitor_class->is_supported = g_win32_file_monitor_is_supported;
104   local_file_monitor_class->start = g_win32_file_monitor_start;
105 }