[gedit-list] [PATCH] Autosave



Hi Paolo,

this is the new patch for enabling autosave in the HEAD. There is still
some code remaining for managing the backups with the auto save feature
but it will be done ASAP.

Comments and suggestions are welcome.

Greetings.

If you want to add some Changelog entry this patch was developed by
Sergio Villar svillar at igalia dot com
Iago Toral itoral at igalia dot com

? mod.tgz
? dialogs/.libs
? dialogs/gedit-close-confirmation-dialog.lo
? dialogs/gedit-encodings-dialog.lo
? dialogs/gedit-open-location-dialog.lo
? dialogs/gedit-page-setup-dialog.lo
? dialogs/gedit-preferences-dialog.lo
? dialogs/gedit-search-dialog.lo
? dialogs/libdialogs.la
? recent-files/.deps
? recent-files/.libs
? recent-files/egg-recent-item.lo
? recent-files/egg-recent-model.lo
? recent-files/egg-recent-util.lo
? recent-files/egg-recent-view-gtk.lo
? recent-files/egg-recent-view-uimanager.lo
? recent-files/egg-recent-view.lo
? recent-files/librecent.la
Index: gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.116
diff -u -w -r1.116 gedit-document.c
--- gedit-document.c	12 Dec 2005 17:27:55 -0000	1.116
+++ gedit-document.c	26 Dec 2005 11:58:23 -0000
@@ -78,7 +78,6 @@
 			     
 struct _GeditDocumentPrivate
 {
-	gint	     auto_save : 1;
 	gint	     readonly : 1;
 	gint	     last_save_was_manually : 1; 	
 	gint	     language_set_by_user : 1;
@@ -95,9 +94,6 @@
 
 	gchar	    *mime_type;
 
-	gint	     auto_save_interval;
-	guint	     auto_save_timeout;
-
 	time_t       mtime;
 
 	GTimeVal     time_of_last_save_or_load;
@@ -194,9 +190,6 @@
 	
 	gedit_debug (DEBUG_DOCUMENT);
 
-	if (doc->priv->auto_save_timeout > 0)
-		g_source_remove (doc->priv->auto_save_timeout);
-
 	if (doc->priv->untitled_number > 0)
 	{
 		g_return_if_fail (doc->priv->uri == NULL);
@@ -571,13 +564,6 @@
 	/* TODO: Set the bracket matching tag style -- Paolo (10 Jan. 2005) */
 	gtk_source_buffer_set_check_brackets (GTK_SOURCE_BUFFER (doc), 
 					      gedit_prefs_manager_get_bracket_matching ());
-
-	doc->priv->auto_save = gedit_prefs_manager_get_auto_save ();
-	doc->priv->auto_save = (doc->priv->auto_save != FALSE);
-
-	doc->priv->auto_save_interval = gedit_prefs_manager_get_auto_save_interval ();
-	if (doc->priv->auto_save_interval <= 0)
-		doc->priv->auto_save_interval = GPM_DEFAULT_AUTO_SAVE_INTERVAL;
 }
 
 GeditDocument *
@@ -803,28 +789,6 @@
 	
 	readonly = (readonly != FALSE);
 
-	if (readonly) 
-	{
-		if (doc->priv->auto_save_timeout > 0)
-		{
-			g_source_remove (doc->priv->auto_save_timeout);
-			doc->priv->auto_save_timeout = 0;
-		}
-	}
-	else
-	{
-		if (doc->priv->auto_save &&
-		    (doc->priv->auto_save_timeout <= 0) && 
-                    !gedit_document_is_untitled (doc))
-		{
-/*			doc->priv->auto_save_timeout = g_timeout_add
-				 (doc->priv->auto_save_interval * 1000 * 60,
-		 		 (GSourceFunc)gedit_document_auto_save,
-		  		 doc);
-*/
-		}
-	}
-
 	if (doc->priv->readonly == readonly) 
 		return;
 
@@ -880,7 +844,7 @@
 
 		g_get_current_time (&doc->priv->time_of_last_save_or_load);
 
-		set_readonly (doc,
+		gedit_document_set_readonly (doc,
 		      gedit_document_loader_get_readonly (loader));
 
 		set_encoding (doc, 
@@ -1170,6 +1134,7 @@
 	return (doc->priv->uri == NULL);
 }
 
+
 gboolean
 gedit_document_get_deleted (GeditDocument *doc)
 {
@@ -1543,35 +1508,6 @@
 	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), NULL);
 
 	return doc->priv->encoding;
-}
-
-void
-gedit_document_set_auto_save_enabled (GeditDocument *doc, 
-				      gboolean       enable)
-{
-	gedit_debug (DEBUG_DOCUMENT);
-
-	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
-
-	enable = (enable != FALSE);
-	
-	// TODO
-	
-	return;
-}
-
-void
-gedit_document_set_auto_save_interval (GeditDocument *doc, 
-				       gint           interval)
-{
-	gedit_debug (DEBUG_DOCUMENT);
-
-	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
-	g_return_if_fail (interval > 0);
-
-	// TODO
-
-	return;
 }
 
 glong
Index: gedit-document.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.h,v
retrieving revision 1.43
diff -u -w -r1.43 gedit-document.h
--- gedit-document.h	12 Dec 2005 17:27:55 -0000	1.43
+++ gedit-document.h	26 Dec 2005 11:58:23 -0000
@@ -214,14 +214,6 @@
 const GeditEncoding 
 		*gedit_document_get_encoding	(GeditDocument       *doc);
 
-// CHECK: I think this can be private
-void		 gedit_document_set_auto_save_enabled	
-						(GeditDocument       *doc, 
-						 gboolean             enable);
-void		 gedit_document_set_auto_save_interval 
-						(GeditDocument       *doc, 
-						 gint                 interval);
-
 /* 
  * Non exported functions
  */
Index: gedit-prefs-manager-app.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-prefs-manager-app.c,v
retrieving revision 1.10
diff -u -w -r1.10 gedit-prefs-manager-app.c
--- gedit-prefs-manager-app.c	12 Dec 2005 17:27:55 -0000	1.10
+++ gedit-prefs-manager-app.c	26 Dec 2005 11:58:23 -0000
@@ -1061,7 +1061,7 @@
 		{
 			GeditDocument *doc = GEDIT_DOCUMENT (l->data);
 
-			gedit_document_set_auto_save_enabled (doc, auto_save);
+			gedit_tab_set_auto_save_enabled (gedit_tab_get_from_document(doc), auto_save);
 
 			l = l->next;
 		}
@@ -1089,7 +1089,7 @@
 		{
 			GeditDocument *doc = GEDIT_DOCUMENT (l->data);
 
-			gedit_document_set_auto_save_interval (doc, auto_save_interval);
+			gedit_tab_set_auto_save_interval (gedit_tab_get_from_document(doc), auto_save_interval);
 
 			l = l->next;
 		}
Index: gedit-tab.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-tab.c,v
retrieving revision 1.2
diff -u -w -r1.2 gedit-tab.c
--- gedit-tab.c	12 Dec 2005 17:27:55 -0000	1.2
+++ gedit-tab.c	26 Dec 2005 11:58:23 -0000
@@ -78,6 +78,10 @@
 	gboolean	     not_editable;
 
 	GeditDocumentSaveFlags	save_flags;
+
+        gint auto_save : 1;
+        gint auto_save_interval;
+        guint auto_save_timeout;
 };
 
 G_DEFINE_TYPE(GeditTab, gedit_tab, GTK_TYPE_VBOX)
@@ -89,6 +93,38 @@
 	PROP_STATE
 };
 
+static void
+document_read_only_notify_handler (GeditDocument *doc, 
+			           GParamSpec    *pspec,
+				   GeditView     *view)
+{
+	gedit_debug (DEBUG_DOCUMENT);
+
+	gboolean readonly = gedit_document_get_readonly (doc);
+	GeditTab *tab = gedit_tab_get_from_document(doc); 
+	
+	if (readonly) 
+	{
+		if (tab->priv->auto_save_timeout > 0)
+		{
+			g_source_remove (tab->priv->auto_save_timeout);
+			tab->priv->auto_save_timeout = 0;
+		}
+	}
+	else
+	{	 
+		if (tab->priv->auto_save &&
+		    (tab->priv->auto_save_timeout <= 0) && 
+                    !gedit_document_is_untitled (doc))
+		{
+			gint timeout = g_timeout_add(tab->priv->auto_save_interval * 1000 * 60,
+                                                     (GSourceFunc)_gedit_tab_auto_save,
+                                                     tab);
+
+			tab->priv->auto_save_timeout = timeout;
+		}
+	}
+}
 
 static void
 gedit_tab_get_property (GObject    *object,
@@ -132,6 +168,9 @@
 
 	g_free (tab->priv->tmp_save_uri);
 
+	if (tab->priv->auto_save_timeout > 0)
+		g_source_remove (tab->priv->auto_save_timeout);
+
 	G_OBJECT_CLASS (gedit_tab_parent_class)->finalize (object);
 }
 
@@ -1206,6 +1245,14 @@
 					GTK_POLICY_AUTOMATIC,
 					GTK_POLICY_AUTOMATIC);
 
+	/* Manage auto save data */
+	tab->priv->auto_save = gedit_prefs_manager_get_auto_save ();
+	tab->priv->auto_save = (tab->priv->auto_save != FALSE);
+
+	tab->priv->auto_save_interval = gedit_prefs_manager_get_auto_save_interval ();
+	if (tab->priv->auto_save_interval <= 0)
+		tab->priv->auto_save_interval = GPM_DEFAULT_AUTO_SAVE_INTERVAL;
+
 	/* Create the view */
 	doc = gedit_document_new ();
 	g_object_set_data (G_OBJECT (doc), GEDIT_TAB_KEY, tab);
@@ -1226,6 +1273,10 @@
 			  G_CALLBACK (document_uri_notify_handler),
 			  tab);
 	g_signal_connect (doc,
+			  "notify::read-only",
+			  G_CALLBACK (document_read_only_notify_handler),
+			  tab->priv->view);
+	g_signal_connect (doc,
 			  "modified_changed",
 			  G_CALLBACK (document_modified_changed),
 			  tab);
@@ -1669,6 +1720,33 @@
 }
 
 void
+_gedit_tab_auto_save (GeditTab *tab)
+{
+	GeditDocument *doc;
+
+	g_return_if_fail (GEDIT_IS_TAB (tab));
+	g_return_if_fail ((tab->priv->state == GEDIT_TAB_STATE_NORMAL) ||
+			  (tab->priv->state == GEDIT_TAB_STATE_SHOWING_PRINT_PREVIEW));
+	g_return_if_fail (tab->priv->tmp_save_uri == NULL);
+	g_return_if_fail (tab->priv->tmp_encoding == NULL);
+
+	doc = gedit_tab_get_document (tab);
+	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+	g_return_if_fail (!gedit_document_is_untitled (doc));
+
+	if (!gtk_text_buffer_get_modified (GTK_TEXT_BUFFER(doc)))
+	  return; 
+
+	gedit_tab_set_state (tab, GEDIT_TAB_STATE_SAVING);
+
+	/* uri used in error messages, will be freed in document_loaded */
+	tab->priv->tmp_save_uri = gedit_document_get_uri (doc);
+	tab->priv->tmp_encoding = gedit_document_get_encoding (doc); 
+
+	gedit_document_save (doc, 0);
+}
+
+void
 _gedit_tab_save_as (GeditTab            *tab,
 		    const gchar         *uri,
 		    const GeditEncoding *encoding)
@@ -1975,5 +2053,95 @@
 
 	return (!gtk_text_buffer_get_modified (GTK_TEXT_BUFFER (doc)) &&
 		!gedit_document_get_deleted (doc));
+}
+
+void
+gedit_tab_set_auto_save_enabled	(GeditTab *tab, 
+				 gboolean enable)
+{
+	gedit_debug (DEBUG_DOCUMENT);
+
+	g_return_if_fail (GEDIT_IS_TAB (tab));
+
+	GeditDocument *doc = NULL;
+
+	doc = gedit_tab_get_document (tab);
+	
+	if (tab->priv->auto_save == enable)
+		return;
+
+	tab->priv->auto_save = enable;
+
+ 	if (enable && 
+ 	    (tab->priv->auto_save_timeout <=0) && 
+ 	    !gedit_document_is_untitled (doc) &&
+ 	    !gedit_document_get_readonly (doc))
+ 	{ 
+ 		gedit_debug_message (DEBUG_DOCUMENT, "Install autosave timeout");
+
+		gint timeout = g_timeout_add(tab->priv->auto_save_interval * 1000 * 60,
+					(GSourceFunc)_gedit_tab_auto_save,
+					tab);
+
+		tab->priv->auto_save_timeout = timeout;
+
+ 		return;
+ 	} 
+	
+ 	if (!enable && (tab->priv->auto_save_timeout > 0))
+ 	{
+ 		gedit_debug_message (DEBUG_DOCUMENT, "Remove autosave timeout");
+
+ 		g_source_remove (tab->priv->auto_save_timeout);
+ 		tab->priv->auto_save_timeout = 0; 
+
+ 		return; 
+ 	} 
+
+ 	g_return_if_fail ((!enable && (tab->priv->auto_save_timeout <= 0)) ||  
+ 			  gedit_document_is_untitled (doc) || gedit_document_get_readonly (doc)); 
+ 	return; 
+}
+
+
+void 
+gedit_tab_set_auto_save_interval (GeditTab *tab, 
+				  gint interval)
+{
+	gedit_debug (DEBUG_DOCUMENT);
+	
+	g_return_if_fail (GEDIT_IS_TAB (tab));
+
+	GeditDocument *doc = NULL;
+	doc = gedit_tab_get_document(tab);
+
+	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+	g_return_if_fail (interval > 0);
+
+	if (tab->priv->auto_save_interval == interval)
+		return;
+
+	tab->priv->auto_save_interval = interval;
+		
+	if (!tab->priv->auto_save)
+		return;
+
+	if (tab->priv->auto_save_timeout > 0)
+	{
+		gedit_debug_message (DEBUG_DOCUMENT, "Remove autosave timeout");
+
+		g_source_remove (tab->priv->auto_save_timeout);
+	
+		gedit_debug_message (DEBUG_DOCUMENT, "Install new autosave timeout");
+
+		g_return_if_fail (!gedit_document_is_untitled (doc));
+		g_return_if_fail (!gedit_document_get_readonly (doc));
+
+		gint timeout = g_timeout_add (interval * 1000 * 60,
+					      (GSourceFunc)_gedit_tab_auto_save,	
+					      tab);
+
+		tab->priv->auto_save_timeout = timeout;
+	}
 }
 
Index: gedit-tab.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-tab.h,v
retrieving revision 1.2
diff -u -w -r1.2 gedit-tab.h
--- gedit-tab.h	12 Dec 2005 17:27:55 -0000	1.2
+++ gedit-tab.h	26 Dec 2005 11:58:23 -0000
@@ -108,6 +108,13 @@
 
 GeditTabState	 gedit_tab_get_state		(GeditTab	     *tab);
 
+void		 gedit_tab_set_auto_save_enabled	
+						(GeditTab            *tab, 
+						 gboolean       enable);
+void		 gedit_tab_set_auto_save_interval 
+						(GeditTab            *tab, 
+						 gint           interval);
+
 /*
  * Non exported methods
  */
@@ -129,6 +136,7 @@
 						 gboolean             create);
 void		 _gedit_tab_revert		(GeditTab            *tab);
 void		 _gedit_tab_save		(GeditTab            *tab);
+void		 _gedit_tab_auto_save		(GeditTab            *tab);
 void		 _gedit_tab_save_as		(GeditTab            *tab,
 						 const gchar         *uri,
 						 const GeditEncoding *encoding);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]