Skip to content

Commit

Permalink
Add options to select another/create new DB
Browse files Browse the repository at this point in the history
this fixes #303
  • Loading branch information
paolostivanin committed Oct 24, 2023
1 parent b6fb69c commit 551824b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 57 deletions.
7 changes: 2 additions & 5 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "show-qr-cb.h"
#include "dbinfo-cb.h"


#ifndef USE_FLATPAK_APP_FOLDER
static gchar *get_db_path (AppData *app_data);
#endif
Expand Down Expand Up @@ -214,8 +213,6 @@ activate (GtkApplication *app,
g_free (app_data);
g_application_quit (G_APPLICATION(app));
return;
} else {
goto retry;
}
}
}
Expand Down Expand Up @@ -606,12 +603,12 @@ get_db_path (AppData *app_data)
}
new_db: ; // empty statement workaround
GtkFileChooserNative *dialog = gtk_file_chooser_native_new (_("Select database location"),
GTK_WINDOW (app_data->main_window),
GTK_WINDOW(app_data->main_window),
app_data->open_db_file_action,
"OK",
"Cancel");

GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
gtk_file_chooser_set_select_multiple (chooser, FALSE);
if (app_data->open_db_file_action == GTK_FILE_CHOOSER_ACTION_SAVE) {
Expand Down
53 changes: 12 additions & 41 deletions src/change-file-cb.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,32 @@
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "new-db-cb.h"
#include "change-db-cb.h"
#include "db-misc.h"
#include "message-dialogs.h"

gboolean
change_file (AppData *app_data)
{
gboolean res = FALSE;
GtkWidget *label = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "diag_changefile_label_id"));
gchar *partial_msg_start = g_markup_printf_escaped ("%s <span font_family=\"monospace\">%s</span>", "The currently selected file is:\n", app_data->db_data->db_path);
const gchar *partial_msg_end = "\n\nDo you want to change it?\n\n"
"If you select <b>Yes</b>, you will be asked to pick another\n"
"database and then you will be prompted for the\n"
"decryption password.\n\n"
"\nIf you select <b>No</b>, then the app will close.";
const gchar *partial_msg_end = "\n\nWhat would you like to do?";
gchar *msg = g_strconcat (partial_msg_start, partial_msg_end, NULL);
gtk_label_set_markup (GTK_LABEL(label), msg);
g_free (msg);
g_free (partial_msg_start);

GtkFileChooserNative *fl_diag;
gboolean res = FALSE;
GtkWidget *diag_changefile = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "diag_changefile_id"));
gint result = gtk_dialog_run (GTK_DIALOG(diag_changefile));
switch (result) {
case GTK_RESPONSE_ACCEPT:
// select an existing DB.
change_db_cb (NULL, NULL, app_data);
res = TRUE;
break;
case GTK_RESPONSE_OK:
fl_diag = gtk_file_chooser_native_new ("Open File",
GTK_WINDOW(app_data->main_window),
GTK_FILE_CHOOSER_ACTION_OPEN,
"Open",
"Cancel");

gint native_diag_res = gtk_native_dialog_run (GTK_NATIVE_DIALOG(fl_diag));
if (native_diag_res == GTK_RESPONSE_ACCEPT) {
GtkFileChooser *chooser = GTK_FILE_CHOOSER(fl_diag);
gchar *db_path = gtk_file_chooser_get_filename (chooser);
GKeyFile *kf = g_key_file_new ();
gchar *cfg_file_path;
#ifndef USE_FLATPAK_APP_FOLDER
cfg_file_path = g_build_filename (g_get_user_config_dir (), "otpclient.cfg", NULL);
#else
cfg_file_path = g_build_filename (g_get_user_data_dir (), "otpclient.cfg", NULL);
#endif
g_key_file_set_string (kf, "config", "db_path", db_path);
GError *err = NULL;
if (!g_key_file_save_to_file (kf, cfg_file_path, &err)) {
gchar *err_msg = g_strconcat (_("Couldn't save the config file: "), err->message, NULL);
show_message_dialog (app_data->main_window, err_msg, GTK_MESSAGE_ERROR);
g_free (err_msg);
g_clear_error (&err);
}
g_free (app_data->db_data->db_path);
app_data->db_data->db_path = g_strdup (db_path);
g_free (db_path);
g_free (cfg_file_path);
g_key_file_free (kf);
}
g_object_unref (fl_diag);
// create a new db.
new_db_cb (NULL, NULL, app_data);
res = TRUE;
break;
case GTK_RESPONSE_CANCEL:
Expand All @@ -65,4 +36,4 @@ change_file (AppData *app_data)
gtk_widget_hide (diag_changefile);

return res;
}
}
10 changes: 5 additions & 5 deletions src/treeview.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ create_treeview (AppData *app_data)
void
update_model (AppData *app_data)
{
GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model (app_data->tree_view));

gtk_list_store_clear (store);

add_data_to_model (app_data->db_data, store);
if (app_data->tree_view != NULL) {
GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model (app_data->tree_view));
gtk_list_store_clear (store);
add_data_to_model (app_data->db_data, store);
}
}


Expand Down
26 changes: 20 additions & 6 deletions src/ui/otpclient.ui
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<object class="GtkButton" id="btn_changefile_no_id">
<property name="label" translatable="yes">No</property>
<object class="GtkButton" id="btn_changefile_quit_id">
<property name="label" translatable="yes">Quit</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
Expand All @@ -710,8 +710,8 @@
</packing>
</child>
<child>
<object class="GtkButton" id="btn_changefile_yes_id">
<property name="label" translatable="yes">Yes</property>
<object class="GtkButton" id="btn_changefile_selex_id">
<property name="label" translatable="yes">Select another DB</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
Expand All @@ -722,6 +722,19 @@
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_changefile_new_id">
<property name="label" translatable="yes">Create new DB</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -747,8 +760,9 @@
</object>
</child>
<action-widgets>
<action-widget response="-6">btn_changefile_no_id</action-widget>
<action-widget response="-5">btn_changefile_yes_id</action-widget>
<action-widget response="-7">btn_changefile_quit_id</action-widget>
<action-widget response="-3">btn_changefile_selex_id</action-widget>
<action-widget response="-5">btn_changefile_new_id</action-widget>
</action-widgets>
</object>
<object class="GtkDialog" id="diag_qr_clipboard_id">
Expand Down

0 comments on commit 551824b

Please sign in to comment.