From 83e992c2077a4fb5281a461c74fc6f7265a24bde Mon Sep 17 00:00:00 2001 From: Ricky Kresslein Date: Thu, 22 Sep 2022 21:28:50 +0200 Subject: Backup database --- src/application.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/database.rs | 10 +++++++++- src/gtk/window.ui | 14 ++++++++++---- 3 files changed, 63 insertions(+), 5 deletions(-) (limited to 'src') 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"), ¬ification); } + + 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 @@ -93,15 +93,21 @@ _Generate Report app.report - - _Export as CSV - app.export-csv - _Delete history app.delete-history +
+ + Back up + app.backup-database + + + _Export as CSV + app.export-csv + +
_Preferences -- cgit 1.4.1