1 #pragma once
2 
3 #include <functional>
4 
5 #include <gtkmm.h>
6 
7 using UnlockCallback = std::function<void(std::string path, std::string masterPassword)>;
8 
9 class LockScreen : public Gtk::Box {
10 public:
LockScreen(UnlockCallback _unlock_callback)11     LockScreen(UnlockCallback _unlock_callback)
12         : Gtk::Box(Gtk::ORIENTATION_VERTICAL),
13           file_chooser(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER),
14           password_field(),
15           unlock_button("Unlock"),
16           unlock_callback(_unlock_callback) {
17         set_halign(Gtk::ALIGN_CENTER);
18         set_valign(Gtk::ALIGN_CENTER);
19         set_spacing(5);
20         set_can_focus(false);
21 
22         auto welcome_label = Gtk::manage(new Gtk::Label("Unlock your vault"));
23         pack_start(*welcome_label, false, true, 0);
24 
25         pack_start(file_chooser, false, true, 0);
26 
27         password_field.set_placeholder_text("Master Password");
28         password_field.set_visibility(false);
29         pack_start(password_field, false, true, 0);
30 
31         unlock_button.set_image_from_icon_name("dialog-password");
32         unlock_button.set_always_show_image(true);
33         pack_start(unlock_button, false, true, 0);
34 
35         unlock_button.signal_clicked().connect([this]() {
36             auto password_text = password_field.get_text();
37             auto vault_path = file_chooser.get_filename();
38 
39             unlock_callback(vault_path, password_text);
40         });
41 
42         password_field.signal_activate().connect([this]() { unlock_button.clicked(); });
43 
44         show_all_children();
45         password_field.grab_focus();
46     };
47 
getPassword()48     std::string getPassword() {
49         return password_field.get_text();
50     }
51 
clearPassword()52     void clearPassword() {
53         password_field.set_text("");
54     }
55 
getPath()56     std::string getPath() {
57         return file_chooser.get_filename();
58     }
59 
setPath(std::string path)60     void setPath(std::string path) {
61         file_chooser.set_filename(path);
62     }
63 
~LockScreen()64     virtual ~LockScreen(){};
65 
66 protected:
67     Gtk::FileChooserButton file_chooser;
68     Gtk::Entry password_field;
69     Gtk::Button unlock_button;
70 
71     UnlockCallback unlock_callback;
72 };
73