-
Notifications
You must be signed in to change notification settings - Fork 48
/
new-db-cb.c
78 lines (71 loc) · 3.22 KB
/
new-db-cb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <gtk/gtk.h>
#include <gcrypt.h>
#include <libsecret/secret.h>
#include "data.h"
#include "db-misc.h"
#include "message-dialogs.h"
#include "password-cb.h"
#include "db-actions.h"
#include "secret-schema.h"
#include "change-file-cb.h"
int
new_db (AppData *app_data)
{
GtkWidget *newdb_diag = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "newdb_diag_id"));
GtkWidget *newdb_entry = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "newdb_entry_id"));
g_object_set_data (G_OBJECT(newdb_entry), "action", GINT_TO_POINTER(ACTION_SAVE));
g_signal_connect (newdb_entry, "icon-press", G_CALLBACK (select_file_icon_pressed_cb), app_data);
GString *new_db_path_with_suffix;
gint result = gtk_dialog_run (GTK_DIALOG (newdb_diag));
switch (result) {
case GTK_RESPONSE_OK:
if (gtk_entry_get_text_length (GTK_ENTRY(newdb_entry)) == 0) {
show_message_dialog (app_data->main_window, "Input cannot be empty.", GTK_MESSAGE_ERROR);
gtk_widget_hide (newdb_diag);
return RETRY_CHANGE;
}
new_db_path_with_suffix = g_string_new (gtk_entry_get_text (GTK_ENTRY(newdb_entry)));
g_string_append (new_db_path_with_suffix, ".enc");
if (g_file_test (new_db_path_with_suffix->str, G_FILE_TEST_IS_REGULAR) || g_file_test (new_db_path_with_suffix->str, G_FILE_TEST_IS_SYMLINK)) {
show_message_dialog (app_data->main_window, "Selected file already exists, please choose another filename.", GTK_MESSAGE_ERROR);
g_string_free (new_db_path_with_suffix, TRUE);
return RETRY_CHANGE;
} else {
g_free (app_data->db_data->db_path);
app_data->db_data->db_path = g_strdup (new_db_path_with_suffix->str);
update_cfg_file (app_data);
gcry_free (app_data->db_data->key);
app_data->db_data->key = prompt_for_password (app_data, NULL, NULL, FALSE);
secret_password_store (OTPCLIENT_SCHEMA, SECRET_COLLECTION_DEFAULT, "main_pwd", app_data->db_data->key, NULL, on_password_stored, NULL, "string", "main_pwd", NULL);
GError *err = NULL;
write_db_to_disk (app_data->db_data, &err);
if (err != NULL) {
show_message_dialog (app_data->main_window, err->message, GTK_MESSAGE_ERROR);
g_clear_error (&err);
} else {
load_new_db (app_data, &err);
if (err != NULL) {
show_message_dialog (app_data->main_window, err->message, GTK_MESSAGE_ERROR);
g_clear_error (&err);
}
}
}
g_string_free (new_db_path_with_suffix, TRUE);
break;
case GTK_RESPONSE_CANCEL:
gtk_widget_destroy (newdb_diag);
return QUIT_APP;
default:
break;
}
gtk_widget_destroy (newdb_diag);
return TRUE;
}
void
new_db_cb (GSimpleAction *simple __attribute__((unused)),
GVariant *parameter __attribute__((unused)),
gpointer user_data)
{
AppData *app_data = (AppData *)user_data;
new_db (app_data);
}