? gedit_monitor.diff
? src/.gedit-document.c.swp
Index: src/gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit-document.c,v
retrieving revision 1.42
diff -u -r1.42 gedit-document.c
--- src/gedit-document.c	4 Jul 2002 20:54:52 -0000	1.42
+++ src/gedit-document.c	9 Jul 2002 15:13:11 -0000
@@ -69,6 +69,7 @@
 	gchar		*uri;
 	gint 		 untitled_number;	
 
+	GnomeVFSMonitorHandle *monitor_handle;
 
 	gchar		*encoding;
 
@@ -118,6 +119,10 @@
 static gboolean gedit_document_auto_save (GeditDocument *doc, GError **error);
 static gboolean gedit_document_auto_save_timeout (GeditDocument *doc);
 
+static void gedit_document_monitor_callback (GnomeVFSMonitorHandle *handle,
+		const gchar *monitor_uri, const gchar *info_uri, 
+		GnomeVFSMonitorEventType event_type, gpointer user_data);
+
 static GtkTextBufferClass *parent_class 	= NULL;
 static guint document_signals[LAST_SIGNAL] 	= { 0 };
 
@@ -318,6 +323,8 @@
 
 	document->priv->undo_manager = gedit_undo_manager_new (document);
 
+	document->priv->monitor_handle = NULL;
+
 	g_signal_connect (G_OBJECT (document->priv->undo_manager), "can_undo",
 			  G_CALLBACK (gedit_document_can_undo_handler), 
 			  document);
@@ -363,6 +370,8 @@
 	if (document->priv->last_replace_text)
 		g_free (document->priv->last_replace_text);
 
+	if (document->priv->monitor_handle != NULL)
+		gnome_vfs_monitor_cancel (document->priv->monitor_handle);
 
 	g_object_unref (G_OBJECT (document->priv->undo_manager));
 	
@@ -416,7 +425,10 @@
 	document = GEDIT_DOCUMENT (g_object_new (GEDIT_TYPE_DOCUMENT, NULL));
 
 	g_return_val_if_fail (document->priv != NULL, NULL);
+
+	/*
 	document->priv->uri = g_strdup (uri);
+	*/
 
 	if (!gedit_document_load (document, uri, error))
 	{
@@ -943,7 +955,15 @@
 		gedit_document_release_untitled_number (doc->priv->untitled_number);
 		doc->priv->untitled_number = 0;
 	}
+
+	if (doc->priv->monitor_handle != NULL)
+		gnome_vfs_monitor_cancel (doc->priv->monitor_handle);
+
 	
+	if (gnome_vfs_monitor_add (&doc->priv->monitor_handle,  doc->priv->uri, 
+		   GNOME_VFS_MONITOR_FILE, gedit_document_monitor_callback, doc) != GNOME_VFS_OK)
+		doc->priv->monitor_handle = NULL;
+				
 	g_signal_emit (G_OBJECT (doc), document_signals[NAME_CHANGED], 0);
 }
 	
@@ -1066,14 +1086,32 @@
 
 /* Does readlink() recursively until we find a real filename. */
 static char *
-follow_symlinks (const gchar *filename, GError **error)
+follow_symlinks (const gchar *file_uri, GError **error)
 {
+	gchar *temp_filename;
 	gchar *followed_filename;
 	gint link_count;
 
-	g_return_val_if_fail (strlen (filename) + 1 <= GEDIT_MAX_PATH_LEN, NULL);
+	g_return_val_if_fail (strlen (file_uri) + 1 <= GEDIT_MAX_PATH_LEN, NULL);
+	
+	/* Get filename from uri */
+	temp_filename = eel_make_uri_canonical (file_uri);
+	
+	if ((temp_filename == NULL) || (strlen (temp_filename) <= 7))
+		followed_filename = NULL;
+	else
+		followed_filename = g_strdup (temp_filename + 7);
 
-	followed_filename = g_strdup (filename);
+	if (temp_filename != NULL)
+		g_free (temp_filename);
+	
+	if (!followed_filename)
+	{
+		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, 0,
+			     _("Invalid filename."));
+		return NULL;
+	}
+	
 	link_count = 0;
 
 	while (link_count < MAX_LINK_LEVEL)
@@ -1164,6 +1202,7 @@
 	gchar *temp_filename; /* Filename for saving */
 	gchar *slashpos;
 	gchar *dirname;
+	gchar *monitor_uri = NULL;
 	mode_t saved_umask;
 	struct stat st;
 	char *chars;
@@ -1212,31 +1251,39 @@
 		return FALSE;
 	}
 
-	/* Get filename from uri */
-	temp_filename = eel_make_uri_canonical (uri);
-	
-	if ((temp_filename == NULL) || (strlen (temp_filename) <= 7))
-		filename = NULL;
-	else
-		filename = g_strdup (temp_filename + 7);
+	/* Get the real filename and file permissions */
+	real_filename = follow_symlinks (uri, error);
 
-	if (temp_filename != NULL)
-		g_free (temp_filename);
-	
-	if (!filename)
-	{
-		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, 0,
-			     _("Invalid filename."));
+	if (!real_filename)
 		goto out;
-	}
 
-	/* Get the real filename and file permissions */
+	/* Stop monitoring doc->priv->uri if uri and doc->priv->uri are the same file */
+	if (doc->priv->monitor_handle != NULL)
+	{
+		gchar *monitored_file;
+		gchar *doc_uri;
 
-	real_filename = follow_symlinks (filename, error);
+		doc_uri = gedit_document_get_raw_uri (doc);
+		g_return_val_if_fail (doc_uri != NULL, FALSE);
+		g_return_val_if_fail (gedit_utils_uri_has_file_scheme (doc_uri), FALSE);
+	
+		monitored_file = follow_symlinks (doc_uri, NULL);
+		if (monitored_file != NULL)
+		{
+			if (eel_uris_match (monitored_file, real_filename))
+			{
+				g_print ("Stop monitoring %s\n", doc_uri);
+				gnome_vfs_monitor_cancel (doc->priv->monitor_handle);
+				doc->priv->monitor_handle = NULL;
+				monitor_uri = gedit_document_get_raw_uri (doc);
+			}
 
-	if (!real_filename)
-		goto out;
+			g_free (monitored_file);
+		}
 
+		g_free (doc_uri);
+	}
+	
 	if (stat (real_filename, &st) != 0)
 	{
 		/* File does not exist? */
@@ -1386,6 +1433,19 @@
 	/* Done */
 
  out:
+	/* Restart monitoring mornitor_uri */
+	if (monitor_uri != NULL)
+	{
+		g_print ("Restart monitoring %s\n", monitor_uri);
+
+		gnome_vfs_monitor_add (&doc->priv->monitor_handle,  
+				       monitor_uri, 
+				       GNOME_VFS_MONITOR_FILE, 
+				       gedit_document_monitor_callback, 
+				       doc);
+
+		g_free (monitor_uri);
+	}
 
 	g_free (filename);
 	g_free (real_filename);
@@ -2023,4 +2083,23 @@
 	/* Place the cursor at the requested position */
 	gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, cursor);
 	gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
+}
+
+static void 
+gedit_document_monitor_callback (GnomeVFSMonitorHandle *handle,
+		const gchar *monitor_uri, const gchar *info_uri, 
+		GnomeVFSMonitorEventType event_type, gpointer user_data)
+{
+	GeditDocument *doc;
+	gedit_debug (DEBUG_DOCUMENT, "");
+
+	g_return_if_fail (GEDIT_IS_DOCUMENT (user_data));
+
+	doc = GEDIT_DOCUMENT (user_data);
+
+	if (gedit_document_is_readonly (doc))
+		return;
+	
+	if (event_type == GNOME_VFS_MONITOR_EVENT_DELETED)
+		gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), TRUE);
 }
