[gnome-latex: 189/205] Create a backup copy of files before saving



commit 57f2131b80174391e960f44063111d3162fdda9b
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Mon Jan 25 23:13:28 2010 +0100

    Create a backup copy of files before saving
    
    There is a new variable in document_t: backup_made. backup_made is FALSE
    when a document is created or opened. When a document is saved, and if
    the option in the preferences is enabled (by default, yes), before
    erasing the file, we rename the file with a name like "file~". When we
    save as a document, backup_made is reset to FALSE.

 TODO                      |  1 -
 src/callbacks.c           | 19 +++++++++++++++----
 src/external_commands.c   |  2 --
 src/file_browser.c        |  2 --
 src/latex_output_filter.c |  1 -
 src/main.c                |  1 -
 src/main.h                |  2 ++
 src/prefs.c               | 35 +++++++++++++++++++++++++++++++++--
 src/print.c               |  1 -
 src/symbols.c             |  2 --
 src/templates.c           |  1 -
 src/ui.c                  |  1 -
 src/utils.c               |  2 --
 13 files changed, 50 insertions(+), 20 deletions(-)
---
diff --git a/TODO b/TODO
index d251295..b8daab5 100644
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 TODO LaTeXila
 
 - Some various improvements:
-       - Make a copy of a file before saving it for the first time (with a name like "file~")
        - Autosave files every X minutes
        - Symbols: most recently used
        - Create personnal templates: choose an icon
diff --git a/src/callbacks.c b/src/callbacks.c
index 3ae3635..90f4c5f 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -18,10 +18,8 @@
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <gtk/gtk.h>
-#include <glib.h>
 #include <glib/gstdio.h>
 #include <gtksourceview/gtksourceview.h>
 #include <gtksourceview/gtksourcelanguage.h>
@@ -89,8 +87,8 @@ cb_open (void)
 
        /* save the current folder */
        g_free (latexila.prefs.file_chooser_dir);
-       latexila.prefs.file_chooser_dir = gtk_file_chooser_get_current_folder_uri (
-                       GTK_FILE_CHOOSER (dialog));
+       latexila.prefs.file_chooser_dir =
+               gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (dialog));
 
        gtk_widget_destroy (dialog);
 }
@@ -121,6 +119,7 @@ cb_save_as (void)
 
        latexila.active_doc->path = NULL;
        latexila.active_doc->saved = FALSE;
+       latexila.active_doc->backup_made = FALSE;
        cb_save ();
 
        // if the user click on cancel
@@ -1166,6 +1165,7 @@ create_document_in_new_tab (const gchar *path, const gchar *text,
        // it doesn't work with g_path_get_basename()...
        new_doc->basename = path != NULL ? g_strdup (title) : NULL;
        new_doc->saved = TRUE;
+       new_doc->backup_made = FALSE;
        new_doc->source_buffer = gtk_source_buffer_new (NULL);
        new_doc->source_view = gtk_source_view_new_with_buffer (new_doc->source_buffer);
 
@@ -1434,6 +1434,7 @@ save_as_dialog (void)
 
                latexila.active_doc->path = filename;
                latexila.active_doc->basename = g_path_get_basename (filename);
+               latexila.active_doc->backup_made = FALSE;
 
                gtk_widget_set_tooltip_text (latexila.active_doc->title, filename);
 
@@ -1473,6 +1474,16 @@ file_save (void)
                return;
        }
 
+       if (! latexila.active_doc->backup_made
+                       && latexila.prefs.make_backup
+                       && g_file_test (latexila.active_doc->path, G_FILE_TEST_IS_REGULAR))
+       {
+               gchar *backup_file = g_strdup_printf ("%s~", latexila.active_doc->path);
+               g_rename (latexila.active_doc->path, backup_file);
+               g_free (backup_file);
+               latexila.active_doc->backup_made = TRUE;
+       }
+
        g_file_set_contents (latexila.active_doc->path, locale, -1, &error);
 
        if (error != NULL)
diff --git a/src/external_commands.c b/src/external_commands.c
index 618c8ce..8025636 100644
--- a/src/external_commands.c
+++ b/src/external_commands.c
@@ -18,8 +18,6 @@
  */
 
 #define _POSIX_C_SOURCE 1
-#include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <sys/wait.h>
 #include <unistd.h> // for dup2
diff --git a/src/file_browser.c b/src/file_browser.c
index 275c80e..92fd83f 100644
--- a/src/file_browser.c
+++ b/src/file_browser.c
@@ -17,8 +17,6 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
 #include <string.h> // for strcmp
 #include <gtk/gtk.h>
 
diff --git a/src/latex_output_filter.c b/src/latex_output_filter.c
index 2b58ab9..aec3c27 100644
--- a/src/latex_output_filter.c
+++ b/src/latex_output_filter.c
@@ -18,7 +18,6 @@
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <ctype.h> // for isspace()
 #include <gtk/gtk.h>
diff --git a/src/main.c b/src/main.c
index 63fdaae..7ae58a3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,7 +18,6 @@
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <gtk/gtk.h>
 
 #include "main.h"
diff --git a/src/main.h b/src/main.h
index 0d39920..b54b16c 100644
--- a/src/main.h
+++ b/src/main.h
@@ -42,6 +42,7 @@ typedef struct
        gchar                   *path;
        gchar                   *basename;
        gboolean                saved;
+       gboolean                backup_made;
        GtkWidget               *source_view;
        GtkSourceBuffer *source_buffer;
        GtkWidget               *title;
@@ -94,6 +95,7 @@ typedef struct
        gboolean                                highlight_matching_brackets;
        gboolean                                toolbars_horizontal;
        gint                                    side_pane_page;
+       gboolean                                make_backup;
 } preferences_t;
 
 // actions from the menu or the toolbars
diff --git a/src/prefs.c b/src/prefs.c
index 3b11e10..e45704a 100644
--- a/src/prefs.c
+++ b/src/prefs.c
@@ -17,8 +17,6 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <gtk/gtk.h>
 #include <gtksourceview/gtksourceview.h>
@@ -47,6 +45,8 @@ static void cb_pref_highlight_current_line (GtkToggleButton *toggle_button,
                gpointer user_data);
 static void cb_pref_highlight_matching_brackets (GtkToggleButton *toggle_button,
                gpointer user_data);
+static void cb_pref_make_backup (GtkToggleButton *toggle_button,
+               gpointer user_data);
 static void cb_pref_font_set (GtkFontButton *font_button, gpointer user_data);
 static void cb_pref_command_view (GtkEditable *editable, gpointer user_data);
 static void cb_pref_command_latex (GtkEditable *editable, gpointer user_data);
@@ -108,6 +108,7 @@ static gboolean     highlight_current_line_                 = TRUE;
 static gboolean highlight_matching_brackets_   = TRUE;
 static gboolean        toolbars_horizontal_                    = FALSE;
 static gint            side_pane_page_                                 = 0;
+static gboolean        make_backup_                                    = TRUE;
 
 void
 load_preferences (preferences_t *prefs)
@@ -499,6 +500,16 @@ load_preferences (preferences_t *prefs)
                error = NULL;
        }
 
+       prefs->make_backup = g_key_file_get_boolean (key_file,
+                       PROGRAM_NAME, "make_backup", &error);
+       if (error != NULL)
+       {
+               print_warning ("%s", error->message);
+               prefs->make_backup = make_backup_;
+               g_error_free (error);
+               error = NULL;
+       }
+
        g_key_file_free (key_file);
 }
 
@@ -563,6 +574,8 @@ save_preferences (preferences_t *prefs)
                        prefs->highlight_matching_brackets);
        g_key_file_set_boolean (key_file, PROGRAM_NAME, "toolbars_horizontal",
                        prefs->toolbars_horizontal);
+       g_key_file_set_boolean (key_file, PROGRAM_NAME, "make_backup",
+                       prefs->make_backup);
 
        /* set the keys that must be taken from the widgets */
        GdkWindowState flag = gdk_window_get_state (gtk_widget_get_window (
@@ -674,6 +687,7 @@ load_default_preferences (preferences_t *prefs)
        prefs->highlight_matching_brackets = highlight_matching_brackets_;
        prefs->toolbars_horizontal = toolbars_horizontal_;
        prefs->side_pane_page = side_pane_page_;
+       prefs->make_backup = make_backup_;
 
        set_current_font_prefs (prefs);
 }
@@ -794,6 +808,12 @@ cb_pref_highlight_matching_brackets (GtkToggleButton *toggle_button,
        }
 }
 
+static void
+cb_pref_make_backup (GtkToggleButton *toggle_button, gpointer user_data)
+{
+       latexila.prefs.make_backup = gtk_toggle_button_get_active (toggle_button);
+}
+
 static void
 cb_pref_font_set (GtkFontButton *font_button, gpointer user_data)
 {
@@ -1130,6 +1150,17 @@ create_preferences (void)
                                FALSE, 0);
        }
 
+       /* make backup */
+       {
+               GtkWidget *make_backup = gtk_check_button_new_with_label (
+                               _("Create a backup copy of files before saving"));
+               gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (make_backup),
+                               latexila.prefs.make_backup);
+               g_signal_connect (G_OBJECT (make_backup), "toggled",
+                               G_CALLBACK (cb_pref_make_backup), NULL);
+               gtk_box_pack_start (GTK_BOX (vbox_editor), make_backup, FALSE, FALSE, 0);
+       }
+
        /* font */
        {
                GtkWidget *hbox = gtk_hbox_new (FALSE, 5);
diff --git a/src/print.c b/src/print.c
index bc2bf9f..fdca9da 100644
--- a/src/print.c
+++ b/src/print.c
@@ -17,7 +17,6 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
 
diff --git a/src/symbols.c b/src/symbols.c
index f17c8f0..56914a9 100644
--- a/src/symbols.c
+++ b/src/symbols.c
@@ -17,8 +17,6 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
 #include <gtk/gtk.h>
 
 #include "main.h"
diff --git a/src/templates.c b/src/templates.c
index 55e8673..fbd7a9c 100644
--- a/src/templates.c
+++ b/src/templates.c
@@ -18,7 +18,6 @@
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <gtk/gtk.h>
 #include <sys/stat.h> // for S_IRWXU
 #include <glib/gstdio.h> // for g_remove()
diff --git a/src/ui.c b/src/ui.c
index 970aa85..73fe968 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -18,7 +18,6 @@
  */
 
 #include <stdlib.h>
-#include <stdio.h>
 #include <gtk/gtk.h>
 
 #include "main.h"
diff --git a/src/utils.c b/src/utils.c
index 860cb44..e22e030 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -17,8 +17,6 @@
  * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
 #include <gtk/gtk.h>
 
 #include "main.h"


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