about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/application.rs44
-rwxr-xr-xsrc/database.rs10
-rwxr-xr-xsrc/gtk/window.ui14
3 files changed, 63 insertions, 5 deletions
diff --git a/src/application.rs b/src/application.rs
index bfa53e2..b0972ec 100755
--- a/src/application.rs
+++ b/src/application.rs
@@ -131,6 +131,12 @@ impl FurtheranceApplication {
         }));
         self.add_action(&about_action);
 
+        let backup_database_action = gio::SimpleAction::new("backup-database", None);
+        backup_database_action.connect_activate(clone!(@weak self as app => move |_, _| {
+            app.backup_database();
+        }));
+        self.add_action(&backup_database_action);
+
         let discard_idle_action = gio::SimpleAction::new("discard-idle-action", None);
         discard_idle_action.connect_activate(clone!(@weak self as app => move |_, _| {
             let window = FurtheranceWindow::default();
@@ -318,6 +324,44 @@ impl FurtheranceApplication {
         self.withdraw_notification("idle");
         self.send_notification(Some("pomodoro"), &notification);
     }
+
+    pub fn backup_database(&self) {
+        let window = self.active_window().unwrap();
+        let dialog = gtk::FileChooserDialog::new(
+            Some(&gettext("Backup Database")),
+            Some(&window),
+            gtk::FileChooserAction::Save,
+            &[
+                (&gettext("Cancel"), gtk::ResponseType::Reject),
+                (&gettext("Save"), gtk::ResponseType::Accept),
+            ]
+        );
+        dialog.set_modal(true);
+
+        // Set a filter to show only SQLite files
+        let filter = gtk::FileFilter::new();
+        gtk::FileFilter::set_name(&filter, Some("*.db"));
+        filter.add_mime_type("application/x-sqlite3");
+        dialog.add_filter(&filter);
+        dialog.set_current_name("furtherance_bkup.db");
+
+        dialog.connect_response(
+            clone!(@strong dialog, @weak self as this => move |filechooser, resp| {
+                if resp == gtk::ResponseType::Accept {
+                    if let Some(path) = filechooser.file().and_then(|file| file.path()) {
+                        let path = &path.to_string_lossy();
+                        let _bkup = database::backup_db(path.to_string());
+                    }
+                    dialog.close();
+                } else {
+                    dialog.close();
+                }
+            }),
+        );
+
+        dialog.show();
+
+    }
 }
 
 impl Default for FurtheranceApplication {
diff --git a/src/database.rs b/src/database.rs
index 6c2649c..090814e 100755
--- a/src/database.rs
+++ b/src/database.rs
@@ -16,10 +16,11 @@
 
 use chrono::{DateTime, Local};
 use directories::ProjectDirs;
-use rusqlite::{Connection, Result};
+use rusqlite::{Connection, Result, backup};
 use std::convert::TryFrom;
 use std::fs::create_dir_all;
 use std::path::PathBuf;
+use std::time::Duration;
 
 #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 pub struct Task {
@@ -337,3 +338,10 @@ pub fn delete_all() -> Result<()> {
 
     Ok(())
 }
+
+pub fn backup_db(backup_file: String) -> Result<()> {
+    let mut bkup_conn = Connection::open(backup_file)?;
+    let conn = Connection::open(get_directory())?;
+    let backup = backup::Backup::new(&conn, &mut bkup_conn)?;
+    backup.run_to_completion(5, Duration::from_millis(250), None)
+}
diff --git a/src/gtk/window.ui b/src/gtk/window.ui
index ed18063..87317d9 100755
--- a/src/gtk/window.ui
+++ b/src/gtk/window.ui
@@ -94,16 +94,22 @@
         <attribute name="action">app.report</attribute>
       </item>
       <item>
-        <attribute name="label" translatable="yes">_Export as CSV</attribute>
-        <attribute name="action">app.export-csv</attribute>
-      </item>
-      <item>
         <attribute name="label" translatable="yes">_Delete history</attribute>
         <attribute name="action">app.delete-history</attribute>
       </item>
     </section>
     <section>
       <item>
+        <attribute name="label" translatable="yes">Back up</attribute>
+        <attribute name="action">app.backup-database</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Export as CSV</attribute>
+        <attribute name="action">app.export-csv</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
         <attribute name="label" translatable="yes">_Preferences</attribute>
         <attribute name="action">app.preferences</attribute>
       </item>