nautilus-monitor.c 3.69 KB
Newer Older
1 2 3 4
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-monitor.c: file and directory change monitoring for nautilus
 
Darin Adler's avatar
Darin Adler committed
5
   Copyright (C) 2000, 2001 Eazel, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
Darin Adler's avatar
Darin Adler committed
22
   Authors: Seth Nickell <seth@eazel.com>
23
            Darin Adler <darin@bentspoon.com>
Alex Graveley's avatar
Alex Graveley committed
24
	    Alex Graveley <alex@ximian.com>
25 26
*/

Darin Adler's avatar
Darin Adler committed
27
#include <config.h>
28
#include "nautilus-monitor.h"
Darin Adler's avatar
Darin Adler committed
29
#include "nautilus-file-changes-queue.h"
Alex Graveley's avatar
Alex Graveley committed
30 31
#include "nautilus-file-utilities.h"

32
#include <gio/gio.h>
33

Darin Adler's avatar
Darin Adler committed
34
struct NautilusMonitor {
Alexander Larsson's avatar
Alexander Larsson committed
35
	GFileMonitor *monitor;
Darin Adler's avatar
Darin Adler committed
36
};
37

Alex Graveley's avatar
Alex Graveley committed
38 39
gboolean
nautilus_monitor_active (void)
Darin Adler's avatar
Darin Adler committed
40
{
Alex Graveley's avatar
Alex Graveley committed
41 42
	static gboolean tried_monitor = FALSE;
	static gboolean monitor_success;
Alexander Larsson's avatar
Alexander Larsson committed
43
	GFileMonitor *dir_monitor;
Alexander Larsson's avatar
Alexander Larsson committed
44
	GFile *file;
45

Alex Graveley's avatar
Alex Graveley committed
46
	if (tried_monitor == FALSE) {	
Alexander Larsson's avatar
Alexander Larsson committed
47
		file = g_file_new_for_path (g_get_home_dir ());
Alexander Larsson's avatar
Alexander Larsson committed
48
		dir_monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, NULL);
Alexander Larsson's avatar
Alexander Larsson committed
49 50 51 52 53
		g_object_unref (file);
		
		monitor_success = (dir_monitor != NULL);
		if (dir_monitor) {
			g_object_unref (dir_monitor);
Alex Graveley's avatar
Alex Graveley committed
54
		}
Darin Adler's avatar
Darin Adler committed
55

Alex Graveley's avatar
Alex Graveley committed
56
		tried_monitor = TRUE;
57 58
	}

Alex Graveley's avatar
Alex Graveley committed
59
	return monitor_success;
60 61
}

Alex Graveley's avatar
Alex Graveley committed
62
static gboolean call_consume_changes_idle_id = 0;
63

Alex Graveley's avatar
Alex Graveley committed
64 65
static gboolean
call_consume_changes_idle_cb (gpointer not_used)
66
{
Alex Graveley's avatar
Alex Graveley committed
67 68
	nautilus_file_changes_consume_changes (TRUE);
	call_consume_changes_idle_id = 0;
69 70 71
	return FALSE;
}

Alexander Larsson's avatar
Alexander Larsson committed
72
static void
Alexander Larsson's avatar
Alexander Larsson committed
73
dir_changed (GFileMonitor* monitor,
Alexander Larsson's avatar
Alexander Larsson committed
74 75 76 77
	     GFile *child,
	     GFile *other_file,
	     GFileMonitorEvent event_type,
	     gpointer user_data)
78
{
Alexander Larsson's avatar
Alexander Larsson committed
79 80 81 82 83 84 85 86
	char *uri, *to_uri;
	
	uri = g_file_get_uri (child);
	to_uri = NULL;
	if (other_file) {
		to_uri = g_file_get_uri (other_file);
	}

Alex Graveley's avatar
Alex Graveley committed
87
	switch (event_type) {
Alexander Larsson's avatar
Alexander Larsson committed
88 89 90
	default:
	case G_FILE_MONITOR_EVENT_CHANGED:
		/* ignore */
Alex Graveley's avatar
Alex Graveley committed
91
		break;
Alexander Larsson's avatar
Alexander Larsson committed
92 93 94
	case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
		nautilus_file_changes_queue_file_changed (child);
Alex Graveley's avatar
Alex Graveley committed
95
		break;
Alexander Larsson's avatar
Alexander Larsson committed
96 97
	case G_FILE_MONITOR_EVENT_DELETED:
		nautilus_file_changes_queue_file_removed (child);
Alex Graveley's avatar
Alex Graveley committed
98
		break;
Alexander Larsson's avatar
Alexander Larsson committed
99 100 101 102 103 104 105 106 107
	case G_FILE_MONITOR_EVENT_CREATED:
		nautilus_file_changes_queue_file_added (child);
		break;
		
	case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
		/* TODO: Do something */
		break;
	case G_FILE_MONITOR_EVENT_UNMOUNTED:
		/* TODO: Do something */
Alex Graveley's avatar
Alex Graveley committed
108 109
		break;
	}
Darin Adler's avatar
Darin Adler committed
110

Alexander Larsson's avatar
Alexander Larsson committed
111 112 113
	g_free (uri);
	g_free (to_uri);

Alex Graveley's avatar
Alex Graveley committed
114 115 116
	if (call_consume_changes_idle_id == 0) {
		call_consume_changes_idle_id = 
			g_idle_add (call_consume_changes_idle_cb, NULL);
Darin Adler's avatar
Darin Adler committed
117
	}
Alex Graveley's avatar
Alex Graveley committed
118
}
Alexander Larsson's avatar
Alexander Larsson committed
119 120 121
 
NautilusMonitor *
nautilus_monitor_directory (GFile *location)
Alex Graveley's avatar
Alex Graveley committed
122
{
Alexander Larsson's avatar
Alexander Larsson committed
123
	GFileMonitor *dir_monitor;
Alex Graveley's avatar
Alex Graveley committed
124
	NautilusMonitor *ret;
Darin Adler's avatar
Darin Adler committed
125

Alexander Larsson's avatar
Alexander Larsson committed
126
	dir_monitor = g_file_monitor_directory (location, G_FILE_MONITOR_WATCH_MOUNTS, NULL, NULL);
127

Alex Graveley's avatar
Alex Graveley committed
128
	ret = g_new0 (NautilusMonitor, 1);
Alexander Larsson's avatar
Alexander Larsson committed
129
	ret->monitor = dir_monitor;
Alex Graveley's avatar
Alex Graveley committed
130

131 132 133
	if (ret->monitor) {
		g_signal_connect (ret->monitor, "changed", (GCallback)dir_changed, ret);
	}
134

135
	/* We return a monitor even on failure, so we can avoid later trying again */
Alex Graveley's avatar
Alex Graveley committed
136
	return ret;
137 138
}

Alex Graveley's avatar
Alex Graveley committed
139
void 
Darin Adler's avatar
Darin Adler committed
140
nautilus_monitor_cancel (NautilusMonitor *monitor)
Alex Graveley's avatar
Alex Graveley committed
141
{
Alexander Larsson's avatar
Alexander Larsson committed
142 143
	if (monitor->monitor != NULL) {
		g_signal_handlers_disconnect_by_func (monitor->monitor, dir_changed, monitor);
Alexander Larsson's avatar
Alexander Larsson committed
144
		g_file_monitor_cancel (monitor->monitor);
Alexander Larsson's avatar
Alexander Larsson committed
145
		g_object_unref (monitor->monitor);
Darin Adler's avatar
Darin Adler committed
146
	}
147

Darin Adler's avatar
Darin Adler committed
148
	g_free (monitor);
149
}