summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-monitor.c
blob: da66ce6a68452de5bd5ae6bc04f81c7a57099e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-monitor.c: file and directory change monitoring for nautilus
 
   Copyright (C) 2000, 2001 Eazel, Inc.
  
   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.
  
   Authors: Seth Nickell <seth@eazel.com>
            Darin Adler <darin@bentspoon.com>
	    Alex Graveley <alex@ximian.com>
*/

#include <config.h>
#include "nautilus-monitor.h"
#include "nautilus-file-changes-queue.h"
#include "nautilus-file-utilities.h"

#include <libgnome/gnome-util.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-volume-monitor.h>

struct NautilusMonitor {
	GnomeVFSMonitorHandle *handle;
};

gboolean
nautilus_monitor_active (void)
{
	static gboolean tried_monitor = FALSE;
	static gboolean monitor_success;
	char *desktop_directory, *uri;
	NautilusMonitor *monitor;

	if (tried_monitor == FALSE) {	
		desktop_directory = nautilus_get_desktop_directory ();
		uri = gnome_vfs_get_uri_from_local_path (desktop_directory);

		monitor = nautilus_monitor_directory (uri);
		monitor_success = (monitor != NULL);

		if (monitor != NULL) {
			nautilus_monitor_cancel (monitor);
		}

		g_free (desktop_directory);
		g_free (uri);

		tried_monitor = TRUE;
	}

	return monitor_success;
}

static gboolean
path_is_on_readonly_volume (const char *path)
{
	GnomeVFSVolumeMonitor *volume_monitor;
	GnomeVFSVolume *volume;
	gboolean res;

	volume_monitor = gnome_vfs_get_volume_monitor ();
	volume = gnome_vfs_volume_monitor_get_volume_for_path (volume_monitor, 
							       path);
	res = FALSE;
	if (volume != NULL) {
		res = gnome_vfs_volume_is_read_only (volume);
		gnome_vfs_volume_unref (volume);
	}
	return res;
}

static gboolean call_consume_changes_idle_id = 0;

static gboolean
call_consume_changes_idle_cb (gpointer not_used)
{
	nautilus_file_changes_consume_changes (TRUE);
	call_consume_changes_idle_id = 0;
	return FALSE;
}

static void 
monitor_notify_cb (GnomeVFSMonitorHandle    *handle,
		   const gchar              *monitor_uri,
		   const gchar              *info_uri,
		   GnomeVFSMonitorEventType  event_type,
		   gpointer                  user_data)
{
	switch (event_type) {
	case GNOME_VFS_MONITOR_EVENT_CHANGED:
		nautilus_file_changes_queue_file_changed (info_uri);
		break;
	case GNOME_VFS_MONITOR_EVENT_DELETED:
		nautilus_file_changes_queue_file_removed (info_uri);
		break;
	case GNOME_VFS_MONITOR_EVENT_CREATED:
		nautilus_file_changes_queue_file_added (info_uri);
		break;

	/* None of the following are supported yet */
	case GNOME_VFS_MONITOR_EVENT_STARTEXECUTING:
	case GNOME_VFS_MONITOR_EVENT_STOPEXECUTING:
	case GNOME_VFS_MONITOR_EVENT_METADATA_CHANGED:
		break;
	}

	if (call_consume_changes_idle_id == 0) {
		call_consume_changes_idle_id = 
			g_idle_add (call_consume_changes_idle_cb, NULL);
	}
}

static NautilusMonitor *
monitor_add_internal (const char *uri, gboolean is_directory)
{
	gchar *path;
	NautilusMonitor *ret;
	GnomeVFSResult result;

	path = gnome_vfs_get_local_path_from_uri (uri);

	/*
	 * Don't monitor URIs on a read-only volume. 
	 * This is a hack to avoid FAM keeping open fds to CD-ROMs, 
	 * causing unmount/eject to fail.  
	 */
	if (path != NULL && path_is_on_readonly_volume (path) == TRUE) {
		g_free (path);
		return NULL;
	}
	g_free (path);

	ret = g_new0 (NautilusMonitor, 1);

	result = gnome_vfs_monitor_add (&ret->handle,
					uri,
					is_directory == TRUE ?  
						GNOME_VFS_MONITOR_DIRECTORY :
						GNOME_VFS_MONITOR_FILE,
					monitor_notify_cb,
					NULL);
	if (result != GNOME_VFS_OK) {
		g_free (ret);
		return NULL;
	}

	return ret;
}

NautilusMonitor *
nautilus_monitor_directory (const char *uri)
{
	return monitor_add_internal (uri, TRUE);
}

NautilusMonitor *
nautilus_monitor_file (const char *uri)
{
	return monitor_add_internal (uri, FALSE);
}

void 
nautilus_monitor_cancel (NautilusMonitor *monitor)
{
	if (monitor->handle != NULL) {
		gnome_vfs_monitor_cancel (monitor->handle);
	}

	g_free (monitor);
}