about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRicky Kresslein <rk@lakoliu.com>2022-09-23 00:29:26 +0200
committerRicky Kresslein <rk@lakoliu.com>2022-09-23 00:29:26 +0200
commit54e7ba5b16045d4e653f392d38f045a4f7183732 (patch)
tree7162209f596606da225f2d97af358667510beb38 /src
parent83e992c2077a4fb5281a461c74fc6f7265a24bde (diff)
downloadFurtherance-54e7ba5b16045d4e653f392d38f045a4f7183732.tar.zst
Import database
Diffstat (limited to 'src')
-rwxr-xr-xsrc/application.rs46
-rwxr-xr-xsrc/database.rs50
-rwxr-xr-xsrc/gtk/window.ui4
3 files changed, 100 insertions, 0 deletions
diff --git a/src/application.rs b/src/application.rs
index b0972ec..1646a34 100755
--- a/src/application.rs
+++ b/src/application.rs
@@ -137,6 +137,12 @@ impl FurtheranceApplication {
         }));
         self.add_action(&backup_database_action);
 
+        let import_database_action = gio::SimpleAction::new("import-database", None);
+        import_database_action.connect_activate(clone!(@weak self as app => move |_, _| {
+            app.import_database();
+        }));
+        self.add_action(&import_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();
@@ -326,6 +332,7 @@ impl FurtheranceApplication {
     }
 
     pub fn backup_database(&self) {
+        // TODO Disable if db is empty
         let window = self.active_window().unwrap();
         let dialog = gtk::FileChooserDialog::new(
             Some(&gettext("Backup Database")),
@@ -362,6 +369,45 @@ impl FurtheranceApplication {
         dialog.show();
 
     }
+
+    pub fn import_database(&self) {
+        let window = self.active_window().unwrap();
+        let dialog = gtk::FileChooserDialog::new(
+            Some(&gettext("Backup Database")),
+            Some(&window),
+            gtk::FileChooserAction::Open,
+            &[
+                (&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.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::import_db(path.to_string());
+
+                        let window = FurtheranceWindow::default();
+                        window.reset_history_box();
+                    }
+                    dialog.close();
+                } else {
+                    dialog.close();
+                }
+            }),
+        );
+
+        dialog.show();
+    }
 }
 
 impl Default for FurtheranceApplication {
diff --git a/src/database.rs b/src/database.rs
index 090814e..1f7b762 100755
--- a/src/database.rs
+++ b/src/database.rs
@@ -16,12 +16,18 @@
 
 use chrono::{DateTime, Local};
 use directories::ProjectDirs;
+use gettextrs::*;
+use glib::clone;
+use gtk::prelude::*;
+use gtk::glib;
 use rusqlite::{Connection, Result, backup};
 use std::convert::TryFrom;
 use std::fs::create_dir_all;
 use std::path::PathBuf;
 use std::time::Duration;
 
+use crate::ui::FurtheranceWindow;
+
 #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
 pub struct Task {
     pub id: i32,
@@ -312,6 +318,16 @@ pub fn check_for_tasks() -> Result<String> {
     )
 }
 
+pub fn check_db_validity(db_path: String) -> Result<String> {
+    let conn = Connection::open(db_path)?;
+
+    conn.query_row(
+        "SELECT task_name FROM tasks ORDER BY ROWID ASC LIMIT 1",
+        [],
+        |row| row.get(0),
+    )
+}
+
 pub fn delete_by_ids(id_list: Vec<i32>) -> Result<()> {
     let conn = Connection::open(get_directory())?;
 
@@ -345,3 +361,37 @@ pub fn backup_db(backup_file: String) -> Result<()> {
     let backup = backup::Backup::new(&conn, &mut bkup_conn)?;
     backup.run_to_completion(5, Duration::from_millis(250), None)
 }
+
+pub fn import_db(new_db: String) -> Result<()> {
+    let new_conn = Connection::open(new_db.clone())?;
+    let valid = match check_db_validity(new_db) {
+        Ok(_) => true,
+        Err(_) => false
+    };
+
+    if valid {
+        let mut conn = Connection::open(get_directory())?;
+        let backup = backup::Backup::new(&new_conn, &mut conn)?;
+        backup.run_to_completion(5, Duration::from_millis(250), None)
+    } else {
+        let window = FurtheranceWindow::default();
+        let dialog = gtk::MessageDialog::with_markup(
+            Some(&window),
+            gtk::DialogFlags::MODAL,
+            gtk::MessageType::Error,
+            gtk::ButtonsType::Ok,
+            Some(&format!(
+                "<span size='large' weight='bold'>{}</span>",
+                &gettext("Not a valid database")
+            )),
+        );
+
+        dialog.connect_response(clone!(@weak dialog = > move |_, _| {
+            dialog.close();
+        }));
+
+        dialog.show();
+
+        Ok(())
+    }
+}
diff --git a/src/gtk/window.ui b/src/gtk/window.ui
index 87317d9..f11d3ef 100755
--- a/src/gtk/window.ui
+++ b/src/gtk/window.ui
@@ -107,6 +107,10 @@
         <attribute name="label" translatable="yes">_Export as CSV</attribute>
         <attribute name="action">app.export-csv</attribute>
       </item>
+      <item>
+        <attribute name="label" translatable="yes">Import</attribute>
+        <attribute name="action">app.import-database</attribute>
+      </item>
     </section>
     <section>
       <item>