-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotp_entry.cpp
73 lines (65 loc) · 2.73 KB
/
otp_entry.cpp
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
#include <string>
#include "otp_entry.hpp"
#include "totp_command.hpp"
OTP_Entry::OTP_Entry(GtkContainer* cont,
const std::string_view& entry_name,
std::shared_ptr<CommandReader> command_reader)
: container(cont),
name(entry_name.data(), entry_name.size()),
cmd_reader(command_reader) {
button = gtk_button_new_with_label(name.c_str());
password_output = gtk_entry_new();
layout = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_container_add(GTK_CONTAINER(container), layout);
gtk_box_set_homogeneous(reinterpret_cast<GtkBox*>(layout), TRUE);
gtk_container_add(GTK_CONTAINER(layout), button);
gtk_container_add(GTK_CONTAINER(layout), password_output);
gtk_editable_set_editable((GtkEditable*) password_output, false);
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), this);
}
OTP_Entry::~OTP_Entry() {
gtk_container_remove(GTK_CONTAINER(layout), password_output);
gtk_container_remove(GTK_CONTAINER(layout), button);
gtk_container_remove(GTK_CONTAINER(container), layout);
}
void OTP_Entry::on_button_clicked(GtkButton* button, gpointer data) {
OTP_Entry* instance = static_cast<OTP_Entry*>(data);
instance->on_button_clicked_member();
}
void OTP_Entry::on_button_clicked_member() {
std::string result;
try {
std::shared_ptr<TOTP_Command> cmd_ate(new TOTP_Command(name));
cmd_reader->execute_cmd(cmd_ate);
result = cmd_ate->get_result().get();
while (result.back() == '\r' || result.back() == '\n') {
result.pop_back();
}
gtk_entry_set_text(reinterpret_cast<GtkEntry*>(password_output), result.c_str());
// gtk_label_set_text(info_bar, "");
} catch (std::invalid_argument e) {
result = e.what();
printf("%s\n", result.c_str());
// color_text(result, "yellow");
// gtk_label_set_markup(info_bar, result.c_str());
} catch (std::out_of_range e) {
result = e.what();
printf("%s\n", result.c_str());
// color_text(result, "yellow");
// gtk_label_set_markup(info_bar, result.c_str());
} catch (ReaderException e) {
result = std::string("ERROR: ").append(e.what());
result.append(".");
if (e.get_error_number() == EPERM) {
result.append(" No permission to open device.");
} else if (e.get_error_number() == ENOENT) {
result.append(" Device doesn't exist.");
} else {
result.append(" Unknown error: ");
result.append(std::to_string(e.get_error_number()));
}
printf("result: %s\n", result.c_str());
// color_text(result, "yellow");
// gtk_label_set_markup(info_bar, result.c_str());
}
}