Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gedit/ChangeLog,v
retrieving revision 1.512
diff -u -r1.512 ChangeLog
--- a/ChangeLog	5 May 2004 11:42:12 -0000	1.512
+++ b/ChangeLog	8 May 2004 17:42:08 -0000
@@ -1,3 +1,13 @@
+2004-05-08  Basavaraja K.C. <ckmbasu@yahoo.co.in>, 
+            Purushotam M.R. <reach_mrp@yahoo.co.uk>,
+	    Shreejith B.L.  <shreejithbl@yahoo.com>,
+	    Sriharsha       <reach_sriharsha@yahoo.com>
+       
+        ** For bug# 92896
+        * gedit/gedit-document.c:
+        Option is provided to save changes to a file even if the directory
+	doesnt have the write permission, but the file belong to the user.
+	    
 
Index: gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.89
diff -u -r1.89 gedit-document.c
--- a/gedit-document.c	4 Apr 2004 16:58:00 -0000	1.89
+++ b/gedit-document.c	8 May 2004 17:54:27 -0000
@@ -1712,6 +1712,13 @@
 	gboolean res;
 	gboolean add_cr;
 	
+	gchar *only_filename; /* Filename without path */
+	gboolean dirwprm; /* flag indicating directory has write permission or no */
+	gchar *cmd;  /* command for copying using system function */
+	struct stat dir_st_prm; /* querying for directory write permission */
+	gint result_prm; /* result of the query for write permission */
+	
+	
 	gedit_debug (DEBUG_DOCUMENT, "");
 
 	g_return_val_if_fail (doc != NULL, FALSE);
@@ -1756,8 +1763,7 @@
 
 	/* Get filename from uri */
 	filename = gnome_vfs_get_local_path_from_uri (uri);
-
-	if (!filename)
+        if (!filename)
 	{
 		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, 0,
 			     _("Invalid filename."));
@@ -1774,7 +1780,6 @@
 	/* Get the directory in which the real filename lives */
 
 	slashpos = strrchr (real_filename, G_DIR_SEPARATOR);
-
 	if (slashpos)
 	{
 		dirname = g_strdup (real_filename);
@@ -1782,11 +1787,13 @@
 	}
 	else
 		dirname = g_strdup (".");
-
+		
+	only_filename = ++slashpos;	
 	/* If there is not an existing file with that name, compute the
 	 * permissions and uid/gid that we will use for the newly-created file.
 	 */
-
+ 
+	 
 	if (stat (real_filename, &st) != 0)
 	{
 		struct stat dir_st;
@@ -1797,35 +1804,45 @@
 
 		/* Use default permissions */
 		saved_umask = umask(0);
-		st.st_mode = 0666 & ~saved_umask;
+		st.st_mode = 066 & ~saved_umask;
 		umask(saved_umask);
 		st.st_uid = getuid ();
 
 		result = stat (dirname, &dir_st);
-
+		
+		
 		if (result == 0 && (dir_st.st_mode & S_ISGID))
 			st.st_gid = dir_st.st_gid;
 		else
 			st.st_gid = getgid ();
 	}
 
+	result_prm = stat(dirname, &dir_st_prm);	/* querying the write permission for directory */
+	dirwprm=(S_IWUSR & dir_st_prm.st_mode)?TRUE:FALSE; /* deciding whether the directory has write permission or not */
+	
 	/* Save to a temporary file.  We set the umask because some (buggy)
 	 * implementations of mkstemp() use permissions 0666 and we want 0600.
 	 */
-
-	temp_filename = g_build_filename (dirname, ".gedit-save-XXXXXX", NULL);
-	g_free (dirname);
-
+	if(dirwprm)
+	{
+		temp_filename = g_build_filename (dirname, ".gedit-save-XXXXXX", NULL);
+		g_free (dirname);
+	}
+	else                            
+		temp_filename = g_build_filename ("/tmp/", ".gedit-save-XXXXXX", NULL); /*save to /tmp directory instead of current directory */
+	
+		
 	saved_umask = umask (0077);
 	fd = g_mkstemp (temp_filename); /* this modifies temp_filename to the used name */
 	umask (saved_umask);
-
+	
+	
 	if (fd == -1)
 	{
 		g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, errno, " ");
 		goto out;
 	}
-
+	
 	chars = gedit_document_get_chars (doc, 0, -1);
 	
 	if (encoding != gedit_encoding_get_utf8 ())
@@ -1909,15 +1926,24 @@
 		gint result;
 
 		bak_ext = gedit_prefs_manager_get_backup_extension ();
-		
-		backup_filename = g_strconcat (real_filename, 
-					       bak_ext,
-					       NULL);
+		if(dirwprm) 
+			backup_filename = g_strconcat (real_filename, bak_ext, NULL);
+		else    	
+		{	
+			backup_filename = g_strconcat ("/tmp/", only_filename, NULL);
+			backup_filename = g_strconcat (backup_filename, bak_ext, NULL);
+		}						       
 
 		g_free (bak_ext);
-		
-		result = rename (real_filename, backup_filename);
-
+		if(dirwprm)
+			result = rename (real_filename, backup_filename);
+		else		
+		{
+			cmd = g_strconcat ("cp ", real_filename, NULL);
+			backup_filename = g_strconcat ( " ", backup_filename, NULL);
+			cmd = g_strconcat (cmd, backup_filename, NULL);
+			result = system (cmd);
+		}	
 		if (result != 0)
 		{
 			g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, errno,
@@ -1926,47 +1952,66 @@
 			goto out;
 		}
 	}
-
+	
+	
 	/* Move the temp file to the original file */
-
-	if (rename (temp_filename, real_filename) != 0)
+	if(dirwprm)
 	{
-		gint saved_errno;
-
-		saved_errno = errno;
-
-		if (create_backup_copy && rename (backup_filename, real_filename) != 0)
-			g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, errno,
-				     " ");
-		else
-			g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, saved_errno,
-				     " ");
+	
+		if (rename (temp_filename, real_filename) != 0)
+		{
+			gint saved_errno;
 
-		goto out;
-	}
+			saved_errno = errno;
+			if (create_backup_copy && rename (backup_filename, real_filename) != 0)
+				g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, errno,
+					     " ");
+			else
+				g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, saved_errno,
+					     " ");
 
+			goto out;
+		}
+	
 	/* Restore permissions.  There is not much error checking we can do
 	 * here, I'm afraid.  The final data is saved anyways.
 	 */
+		chmod (real_filename, st.st_mode);
+		chown (real_filename, st.st_uid, st.st_gid);
 
-	chmod (real_filename, st.st_mode);
-	chown (real_filename, st.st_uid, st.st_gid);
-
-	gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), FALSE);
-
-	retval = TRUE;
+		gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), FALSE);
 
+		retval = TRUE;
+	}
+	else		
+	{
+		cmd = g_strconcat ("cp ", temp_filename, NULL);
+		real_filename = g_strconcat ( " ", real_filename, NULL);
+		cmd = g_strconcat (cmd, real_filename, NULL);
+		if(system (cmd)!=0)
+		{
+			gint saved_errno;
+			saved_errno = errno;
+			cmd = g_strconcat ("cp ", backup_filename, NULL);
+			real_filename = g_strconcat ( " ", real_filename, NULL);
+			cmd = g_strconcat (cmd, real_filename, NULL);
+			if(system(cmd)!=0)
+			  g_set_error(error, GEDIT_DOCUMENT_IO_ERROR, errno, " ");
+		}
+		unlink(temp_filename);	
+		
+		gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), FALSE);
+		retval= TRUE;
+		
+	}	 
 	/* Done */
 
  out:
-
 	g_free (chars);
-
 	g_free (filename);
 	g_free (real_filename);
 	g_free (backup_filename);
 	g_free (temp_filename);
-
 	return retval;
 }
 
