1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
3 gnome-vfs-mime-monitor.c: Class for noticing changes in MIME data.
5 Copyright (C) 2000 Eazel, Inc.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public
18 License along with this program; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Authors: John Sullivan <sullivan@eazel.com>,
26 #include "gnome-vfs-mime-monitor.h"
27 #include "gnome-vfs-mime-private.h"
28 #include "gnome-vfs-ops.h"
40 static guint signals[LAST_SIGNAL];
42 static GnomeVFSMIMEMonitor *global_mime_monitor = NULL;
44 typedef struct _MonitorCallbackData
46 GnomeVFSMIMEMonitor *monitor;
48 } MonitorCallbackData;
50 struct _GnomeVFSMIMEMonitorPrivate
52 GnomeVFSMonitorHandle *global_handle;
53 GnomeVFSMonitorHandle *local_handle;
55 /* The hoops I jump through */
56 MonitorCallbackData *gnome_callback_data;
57 MonitorCallbackData *local_callback_data;
61 static void gnome_vfs_mime_monitor_class_init (GnomeVFSMIMEMonitorClass *klass);
62 static void gnome_vfs_mime_monitor_init (GnomeVFSMIMEMonitor *monitor);
63 static void mime_dir_changed_callback (GnomeVFSMonitorHandle *handle,
64 const gchar *monitor_uri,
65 const gchar *info_uri,
66 GnomeVFSMonitorEventType event_type,
68 static void gnome_vfs_mime_monitor_finalize (GObject *object);
73 gnome_vfs_mime_monitor_class_init (GnomeVFSMIMEMonitorClass *klass)
75 GObjectClass *object_class = G_OBJECT_CLASS (klass);
77 object_class->finalize = gnome_vfs_mime_monitor_finalize;
79 signals [DATA_CHANGED] =
80 g_signal_new ("data_changed",
81 G_TYPE_FROM_CLASS (klass),
83 G_STRUCT_OFFSET (GnomeVFSMIMEMonitorClass, data_changed),
85 g_cclosure_marshal_VOID__VOID,
90 gnome_vfs_mime_monitor_init (GnomeVFSMIMEMonitor *monitor)
94 monitor->priv = g_new (GnomeVFSMIMEMonitorPrivate, 1);
96 monitor->priv->gnome_callback_data = g_new (MonitorCallbackData, 1);
97 monitor->priv->local_callback_data = g_new (MonitorCallbackData, 1);
99 /* FIXME: Bug #80268. These wouldn't be private members if we had a
100 * _full variant. However, if I want to clean them up, I need to keep
102 monitor->priv->gnome_callback_data->type = GNOME_MIME_DIR;
103 monitor->priv->gnome_callback_data->monitor = monitor;
104 monitor->priv->local_callback_data->type = LOCAL_MIME_DIR;
105 monitor->priv->local_callback_data->monitor = monitor;
107 mime_dir = g_strdup (DATADIR "/mime-info");
108 gnome_vfs_monitor_add (&monitor->priv->global_handle,
110 GNOME_VFS_MONITOR_DIRECTORY,
111 mime_dir_changed_callback,
112 monitor->priv->gnome_callback_data);
115 mime_dir = g_strconcat (g_get_home_dir (), "/.gnome/mime-info", NULL);
116 if (!g_file_test (mime_dir, G_FILE_TEST_EXISTS)) {
117 mkdir (mime_dir, S_IRWXU);
119 gnome_vfs_monitor_add (&monitor->priv->local_handle,
121 GNOME_VFS_MONITOR_DIRECTORY,
122 mime_dir_changed_callback,
123 monitor->priv->local_callback_data);
129 mime_dir_changed_callback (GnomeVFSMonitorHandle *handle,
130 const gchar *monitor_uri,
131 const gchar *info_uri,
132 GnomeVFSMonitorEventType event_type,
135 MonitorCallbackData *monitor_callback_data = (MonitorCallbackData *)user_data;
137 if (monitor_callback_data->type == GNOME_MIME_DIR)
138 _gnome_vfs_mime_info_mark_gnome_mime_dir_dirty ();
139 else if (monitor_callback_data->type == LOCAL_MIME_DIR)
140 _gnome_vfs_mime_info_mark_user_mime_dir_dirty ();
142 _gnome_vfs_mime_monitor_emit_data_changed (monitor_callback_data->monitor);
146 gnome_vfs_mime_monitor_finalize (GObject *object)
148 gnome_vfs_monitor_cancel (GNOME_VFS_MIME_MONITOR (object)->priv->global_handle);
149 gnome_vfs_monitor_cancel (GNOME_VFS_MIME_MONITOR (object)->priv->local_handle);
150 g_free (GNOME_VFS_MIME_MONITOR (object)->priv->gnome_callback_data);
151 g_free (GNOME_VFS_MIME_MONITOR (object)->priv->local_callback_data);
152 g_free (GNOME_VFS_MIME_MONITOR (object)->priv);
156 * gnome_vfs_mime_monitor_get:
158 * Get access to the single global monitor.
160 * Return value: the global #GnomeVFSMIMEMonitor
162 GnomeVFSMIMEMonitor *
163 gnome_vfs_mime_monitor_get (void)
165 if (global_mime_monitor == NULL) {
166 global_mime_monitor = GNOME_VFS_MIME_MONITOR
167 (g_object_new (gnome_vfs_mime_monitor_get_type (), NULL));
169 return global_mime_monitor;
174 _gnome_vfs_mime_monitor_emit_data_changed (GnomeVFSMIMEMonitor *monitor)
176 g_return_if_fail (GNOME_VFS_IS_MIME_MONITOR (monitor));
178 g_signal_emit (G_OBJECT (monitor),
179 signals [DATA_CHANGED], 0);
183 gnome_vfs_mime_monitor_get_type (void)
185 static GType type = 0;
189 sizeof (GnomeVFSMIMEMonitorClass),
190 (GBaseInitFunc) NULL,
191 (GBaseFinalizeFunc) NULL,
192 (GClassInitFunc) gnome_vfs_mime_monitor_class_init,
193 NULL, /* class_finalize */
194 NULL, /* class_data */
195 sizeof (GnomeVFSMIMEMonitor),
197 (GInstanceInitFunc) gnome_vfs_mime_monitor_init
200 type = g_type_register_static (
201 G_TYPE_OBJECT, "GnomeVFSMIMEMonitor", &info, 0);