Made moo_file_watch_monitor_* return monitor id, instead of boolean status

This commit is contained in:
Yevgen Muntyan 2006-06-17 00:54:04 -05:00
parent 0788fbcc74
commit eb7132c9eb
4 changed files with 144 additions and 108 deletions

View File

@ -797,9 +797,9 @@ _moo_edit_start_file_watch (MooEdit *edit)
g_return_if_fail ((edit->priv->status & MOO_EDIT_CHANGED_ON_DISK) == 0); g_return_if_fail ((edit->priv->status & MOO_EDIT_CHANGED_ON_DISK) == 0);
g_return_if_fail (edit->priv->filename != NULL); g_return_if_fail (edit->priv->filename != NULL);
moo_file_watch_monitor_file (watch, edit->priv->filename, edit, edit->priv->file_monitor_id = moo_file_watch_monitor_file (watch,
&edit->priv->file_monitor_id, edit->priv->filename,
&error); edit, &error);
if (!edit->priv->file_monitor_id) if (!edit->priv->file_monitor_id)
{ {

View File

@ -866,11 +866,12 @@ start_monitor (MooFolder *folder)
folder->priv->fam = _moo_file_system_get_file_watch (folder->priv->fs); folder->priv->fam = _moo_file_system_get_file_watch (folder->priv->fs);
g_return_if_fail (folder->priv->fam != NULL); g_return_if_fail (folder->priv->fam != NULL);
if (!moo_file_watch_monitor_directory (folder->priv->fam, folder->priv->fam_request =
folder->priv->path, moo_file_watch_monitor_directory (folder->priv->fam,
folder, folder->priv->path,
&folder->priv->fam_request, folder, &error);
&error))
if (!folder->priv->fam_request)
{ {
g_warning ("%s: moo_fam_monitor_directory failed", G_STRLOC); g_warning ("%s: moo_fam_monitor_directory failed", G_STRLOC);
g_warning ("%s: %s", G_STRLOC, error->message); g_warning ("%s: %s", G_STRLOC, error->message);

View File

@ -62,6 +62,16 @@ typedef enum {
MONITOR_FILE MONITOR_FILE
} MonitorType; } MonitorType;
struct _Monitor {
MooFileWatch *parent;
MonitorType type;
char *filename;
gpointer user_data;
int request;
guint suspended : 1;
struct stat statbuf;
};
typedef struct { typedef struct {
gboolean (*start) (MooFileWatch *watch, gboolean (*start) (MooFileWatch *watch,
GError **error); GError **error);
@ -162,7 +172,8 @@ static guint signals[LAST_SIGNAL];
static void moo_file_watch_finalize (GObject *object); static void moo_file_watch_finalize (GObject *object);
static void moo_file_watch_class_init (MooFileWatchClass *klass) static void
moo_file_watch_class_init (MooFileWatchClass *klass)
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
@ -176,7 +187,7 @@ static void moo_file_watch_class_init (MooFileWatchClass *klass)
NULL, NULL, NULL, NULL,
_moo_marshal_VOID__POINTER, _moo_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_NONE, 1,
G_TYPE_POINTER); MOO_TYPE_FILE_WATCH_EVENT);
signals[ERROR_SIGNAL] = signals[ERROR_SIGNAL] =
g_signal_new ("error", g_signal_new ("error",
@ -190,7 +201,8 @@ static void moo_file_watch_class_init (MooFileWatchClass *klass)
} }
static void moo_file_watch_init (MooFileWatch *watch) static void
moo_file_watch_init (MooFileWatch *watch)
{ {
watch->priv = g_new0 (MooFileWatchPrivate, 1); watch->priv = g_new0 (MooFileWatchPrivate, 1);
watch->priv->requests = g_hash_table_new (g_direct_hash, g_direct_equal); watch->priv->requests = g_hash_table_new (g_direct_hash, g_direct_equal);
@ -241,7 +253,8 @@ static void moo_file_watch_init (MooFileWatch *watch)
} }
static void moo_file_watch_finalize (GObject *object) static void
moo_file_watch_finalize (GObject *object)
{ {
GError *error = NULL; GError *error = NULL;
MooFileWatch *watch = MOO_FILE_WATCH (object); MooFileWatch *watch = MOO_FILE_WATCH (object);
@ -266,7 +279,8 @@ static void moo_file_watch_finalize (GObject *object)
} }
GQuark moo_file_watch_error_quark (void) GQuark
moo_file_watch_error_quark (void)
{ {
static GQuark quark = 0; static GQuark quark = 0;
if (!quark) if (!quark)
@ -304,6 +318,18 @@ moo_file_watch_method_get_type (void)
} }
GType
moo_file_watch_event_get_type (void)
{
static GType type = 0;
if (!type)
type = g_pointer_type_register_static ("MooFileWatchEvent");
return type;
}
GType GType
moo_file_watch_event_code_get_type (void) moo_file_watch_event_code_get_type (void)
{ {
@ -326,16 +352,15 @@ moo_file_watch_event_code_get_type (void)
} }
static gboolean static int
moo_file_watch_create_monitor (MooFileWatch *watch, moo_file_watch_create_monitor (MooFileWatch *watch,
MonitorType type, MonitorType type,
const char *filename, const char *filename,
gpointer data, gpointer data,
int *request,
GError **error) GError **error)
{ {
Monitor *monitor; Monitor *monitor;
int req; int request;
g_return_val_if_fail (MOO_IS_FILE_WATCH (watch), FALSE); g_return_val_if_fail (MOO_IS_FILE_WATCH (watch), FALSE);
g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
@ -348,45 +373,41 @@ moo_file_watch_create_monitor (MooFileWatch *watch,
return FALSE; return FALSE;
} }
monitor = watch->priv->funcs.create (watch, type, filename, data, &req, error); monitor = watch->priv->funcs.create (watch, type, filename, data, &request, error);
if (!monitor) if (!monitor)
return FALSE; return 0;
watch->priv->monitors = g_slist_prepend (watch->priv->monitors, monitor); watch->priv->monitors = g_slist_prepend (watch->priv->monitors, monitor);
g_hash_table_insert (watch->priv->requests, GINT_TO_POINTER (req), monitor); g_hash_table_insert (watch->priv->requests, GINT_TO_POINTER (request), monitor);
if (request) return request;
*request = req;
return TRUE;
} }
gboolean moo_file_watch_monitor_directory (MooFileWatch *watch, int
const char *filename, moo_file_watch_monitor_directory (MooFileWatch *watch,
gpointer data, const char *filename,
int *monitor_id, gpointer data,
GError **error) GError **error)
{ {
return moo_file_watch_create_monitor (watch, MONITOR_DIR, filename, return moo_file_watch_create_monitor (watch, MONITOR_DIR, filename, data, error);
data, monitor_id, error);
} }
gboolean moo_file_watch_monitor_file (MooFileWatch *watch, int
const char *filename, moo_file_watch_monitor_file (MooFileWatch *watch,
gpointer data, const char *filename,
int *monitor_id, gpointer data,
GError **error) GError **error)
{ {
return moo_file_watch_create_monitor (watch, MONITOR_FILE, filename, return moo_file_watch_create_monitor (watch, MONITOR_FILE, filename, data, error);
data, monitor_id, error);
} }
void moo_file_watch_suspend_monitor (MooFileWatch *watch, void
int monitor_id) moo_file_watch_suspend_monitor (MooFileWatch *watch,
int monitor_id)
{ {
Monitor *monitor; Monitor *monitor;
@ -400,8 +421,9 @@ void moo_file_watch_suspend_monitor (MooFileWatch *watch,
} }
void moo_file_watch_resume_monitor (MooFileWatch *watch, void
int monitor_id) moo_file_watch_resume_monitor (MooFileWatch *watch,
int monitor_id)
{ {
Monitor *monitor; Monitor *monitor;
@ -415,8 +437,9 @@ void moo_file_watch_resume_monitor (MooFileWatch *watch,
} }
void moo_file_watch_cancel_monitor (MooFileWatch *watch, void
int monitor_id) moo_file_watch_cancel_monitor (MooFileWatch *watch,
int monitor_id)
{ {
Monitor *monitor; Monitor *monitor;
@ -433,8 +456,9 @@ void moo_file_watch_cancel_monitor (MooFileWatch *watch,
} }
static void emit_event (MooFileWatch *watch, static void
MooFileWatchEvent *event) emit_event (MooFileWatch *watch,
MooFileWatchEvent *event)
{ {
DEBUG_PRINT ("watch %p, monitor %d: event %s for %s", DEBUG_PRINT ("watch %p, monitor %d: event %s for %s",
watch, event->monitor_id, watch, event->monitor_id,
@ -444,7 +468,8 @@ static void emit_event (MooFileWatch *watch,
} }
MooFileWatch *moo_file_watch_new (GError **error) MooFileWatch *
moo_file_watch_new (GError **error)
{ {
MooFileWatch *watch; MooFileWatch *watch;
@ -461,8 +486,9 @@ MooFileWatch *moo_file_watch_new (GError **error)
} }
gboolean moo_file_watch_close (MooFileWatch *watch, gboolean
GError **error) moo_file_watch_close (MooFileWatch *watch,
GError **error)
{ {
g_return_val_if_fail (MOO_IS_FILE_WATCH (watch), FALSE); g_return_val_if_fail (MOO_IS_FILE_WATCH (watch), FALSE);
@ -480,17 +506,6 @@ gboolean moo_file_watch_close (MooFileWatch *watch,
} }
struct _Monitor {
MooFileWatch *parent;
MonitorType type;
char *filename;
gpointer user_data;
int request;
guint suspended : 1;
struct stat statbuf;
};
/*****************************************************************************/ /*****************************************************************************/
/* FAM /* FAM
*/ */
@ -520,8 +535,9 @@ static gboolean read_fam_events (GIOChannel *source,
GIOCondition condition, GIOCondition condition,
MooFileWatch *watch); MooFileWatch *watch);
static gboolean watch_fam_start (MooFileWatch *watch, static gboolean
GError **error) watch_fam_start (MooFileWatch *watch,
GError **error)
{ {
GIOChannel *fam_socket; GIOChannel *fam_socket;
@ -539,8 +555,9 @@ static gboolean watch_fam_start (MooFileWatch *watch,
} }
static gboolean watch_fam_shutdown (MooFileWatch *watch, static gboolean
GError **error) watch_fam_shutdown (MooFileWatch *watch,
GError **error)
{ {
if (watch->priv->fam_connection_watch) if (watch->priv->fam_connection_watch)
g_source_remove (watch->priv->fam_connection_watch); g_source_remove (watch->priv->fam_connection_watch);
@ -552,12 +569,13 @@ static gboolean watch_fam_shutdown (MooFileWatch *watch,
} }
static Monitor *monitor_fam_create (MooFileWatch *watch, static Monitor *
MonitorType type, monitor_fam_create (MooFileWatch *watch,
const char *filename, MonitorType type,
gpointer data, const char *filename,
int *request, gpointer data,
GError **error) int *request,
GError **error)
{ {
FAMRequest fr; FAMRequest fr;
int result; int result;
@ -605,7 +623,8 @@ static Monitor *monitor_fam_create (MooFileWatch *watch,
} }
static void monitor_fam_suspend (Monitor *monitor) static void
monitor_fam_suspend (Monitor *monitor)
{ {
FAMRequest fr; FAMRequest fr;
int result; int result;
@ -640,7 +659,8 @@ static void monitor_fam_suspend (Monitor *monitor)
} }
static void monitor_fam_resume (Monitor *monitor) static void
monitor_fam_resume (Monitor *monitor)
{ {
FAMRequest fr; FAMRequest fr;
int result; int result;
@ -675,11 +695,11 @@ static void monitor_fam_resume (Monitor *monitor)
} }
static void monitor_fam_delete (Monitor *monitor) static void
monitor_fam_delete (Monitor *monitor)
{ {
FAMRequest fr; FAMRequest fr;
int result; int result;
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
fr.reqnum = monitor->request; fr.reqnum = monitor->request;
@ -699,9 +719,10 @@ static void monitor_fam_delete (Monitor *monitor)
} }
static gboolean read_fam_events (G_GNUC_UNUSED GIOChannel *source, static gboolean
GIOCondition condition, read_fam_events (G_GNUC_UNUSED GIOChannel *source,
MooFileWatch *watch) GIOCondition condition,
MooFileWatch *watch)
{ {
GError *error = NULL; GError *error = NULL;
int result; int result;
@ -826,8 +847,9 @@ static MooFileWatchError errno_to_file_error (int code);
static gboolean do_stat (MooFileWatch *watch); static gboolean do_stat (MooFileWatch *watch);
static gboolean watch_stat_start (MooFileWatch *watch, static gboolean
G_GNUC_UNUSED GError **error) watch_stat_start (MooFileWatch *watch,
G_GNUC_UNUSED GError **error)
{ {
watch->priv->stat_timeout = watch->priv->stat_timeout =
g_timeout_add_full (MOO_STAT_PRIORITY, g_timeout_add_full (MOO_STAT_PRIORITY,
@ -838,8 +860,9 @@ static gboolean watch_stat_start (MooFileWatch *watch,
} }
static gboolean watch_stat_shutdown (MooFileWatch *watch, static gboolean
G_GNUC_UNUSED GError **error) watch_stat_shutdown (MooFileWatch *watch,
G_GNUC_UNUSED GError **error)
{ {
if (watch->priv->stat_timeout) if (watch->priv->stat_timeout)
g_source_remove (watch->priv->stat_timeout); g_source_remove (watch->priv->stat_timeout);
@ -848,12 +871,13 @@ static gboolean watch_stat_shutdown (MooFileWatch *watch,
} }
static Monitor *monitor_stat_create (MooFileWatch *watch, static Monitor *
MonitorType type, monitor_stat_create (MooFileWatch *watch,
const char *filename, MonitorType type,
gpointer data, const char *filename,
int *request, gpointer data,
GError **error) int *request,
GError **error)
{ {
Monitor *monitor; Monitor *monitor;
struct stat buf; struct stat buf;
@ -908,21 +932,24 @@ static Monitor *monitor_stat_create (MooFileWatch *watch,
} }
static void monitor_stat_suspend (Monitor *monitor) static void
monitor_stat_suspend (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
monitor->suspended = TRUE; monitor->suspended = TRUE;
} }
static void monitor_stat_resume (Monitor *monitor) static void
monitor_stat_resume (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
monitor->suspended = FALSE; monitor->suspended = FALSE;
} }
static void monitor_stat_delete (Monitor *monitor) static void
monitor_stat_delete (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
DEBUG_PRINT ("removing monitor for '%s'", monitor->filename); DEBUG_PRINT ("removing monitor for '%s'", monitor->filename);
@ -931,7 +958,8 @@ static void monitor_stat_delete (Monitor *monitor)
} }
static gboolean do_stat (MooFileWatch *watch) static gboolean
do_stat (MooFileWatch *watch)
{ {
GSList *l, *list, *to_remove = NULL; GSList *l, *list, *to_remove = NULL;
gboolean result = TRUE; gboolean result = TRUE;
@ -1002,7 +1030,8 @@ out:
} }
static MooFileWatchError errno_to_file_error (int code) static MooFileWatchError
errno_to_file_error (int code)
{ {
MooFileWatchError fcode = MOO_FILE_WATCH_ERROR_FAILED; MooFileWatchError fcode = MOO_FILE_WATCH_ERROR_FAILED;
@ -1032,26 +1061,29 @@ static MooFileWatchError errno_to_file_error (int code)
*/ */
#ifdef __WIN32__ #ifdef __WIN32__
static gboolean watch_win32_start (MooFileWatch *watch, static gboolean
GError **error) watch_win32_start (MooFileWatch *watch,
GError **error)
{ {
return watch_stat_start (watch, error); return watch_stat_start (watch, error);
} }
static gboolean watch_win32_shutdown (MooFileWatch *watch, static gboolean
GError **error) watch_win32_shutdown (MooFileWatch *watch,
GError **error)
{ {
return watch_stat_shutdown (watch, error); return watch_stat_shutdown (watch, error);
} }
static Monitor *monitor_win32_create (MooFileWatch *watch, static Monitor *
MonitorType type, monitor_win32_create (MooFileWatch *watch,
const char *filename, MonitorType type,
gpointer data, const char *filename,
int *request, gpointer data,
GError **error) int *request,
GError **error)
{ {
g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (filename != NULL, FALSE);
@ -1070,7 +1102,8 @@ static Monitor *monitor_win32_create (MooFileWatch *watch,
} }
static void monitor_win32_suspend (Monitor *monitor) static void
monitor_win32_suspend (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
@ -1084,7 +1117,8 @@ static void monitor_win32_suspend (Monitor *monitor)
} }
static void monitor_win32_resume (Monitor *monitor) static void
monitor_win32_resume (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);
@ -1098,7 +1132,8 @@ static void monitor_win32_resume (Monitor *monitor)
} }
static void monitor_win32_delete (Monitor *monitor) static void
monitor_win32_delete (Monitor *monitor)
{ {
g_return_if_fail (monitor != NULL); g_return_if_fail (monitor != NULL);

View File

@ -47,6 +47,7 @@ GQuark moo_file_watch_error_quark (void);
#define MOO_IS_FILE_WATCH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_FILE_WATCH)) #define MOO_IS_FILE_WATCH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_FILE_WATCH))
#define MOO_FILE_WATCH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_FILE_WATCH, MooFileWatchClass)) #define MOO_FILE_WATCH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_FILE_WATCH, MooFileWatchClass))
#define MOO_TYPE_FILE_WATCH_EVENT (moo_file_watch_event_get_type ())
#define MOO_TYPE_FILE_WATCH_EVENT_CODE (moo_file_watch_event_code_get_type ()) #define MOO_TYPE_FILE_WATCH_EVENT_CODE (moo_file_watch_event_code_get_type ())
#define MOO_TYPE_FILE_WATCH_METHOD (moo_file_watch_method_get_type ()) #define MOO_TYPE_FILE_WATCH_METHOD (moo_file_watch_method_get_type ())
@ -100,6 +101,7 @@ struct _MooFileWatchClass
}; };
GType moo_file_watch_get_type (void) G_GNUC_CONST; GType moo_file_watch_get_type (void) G_GNUC_CONST;
GType moo_file_watch_event_get_type (void) G_GNUC_CONST;
GType moo_file_watch_event_code_get_type (void) G_GNUC_CONST; GType moo_file_watch_event_code_get_type (void) G_GNUC_CONST;
GType moo_file_watch_method_get_type (void) G_GNUC_CONST; GType moo_file_watch_method_get_type (void) G_GNUC_CONST;
@ -111,15 +113,13 @@ gboolean moo_file_watch_close (MooFileWatch *watch,
GError **error); GError **error);
/* FAMMonitorDirectory, FAMMonitorFile */ /* FAMMonitorDirectory, FAMMonitorFile */
gboolean moo_file_watch_monitor_directory (MooFileWatch *watch, int moo_file_watch_monitor_directory (MooFileWatch *watch,
const char *filename, const char *filename,
gpointer data, gpointer data,
int *monitor_id,
GError **error); GError **error);
gboolean moo_file_watch_monitor_file (MooFileWatch *watch, int moo_file_watch_monitor_file (MooFileWatch *watch,
const char *filename, const char *filename,
gpointer data, gpointer data,
int *monitor_id,
GError **error); GError **error);
/* FAMSuspendMonitor, FAMResumeMonitor, FAMCancelMonitor */ /* FAMSuspendMonitor, FAMResumeMonitor, FAMCancelMonitor */