Skip to content
API Documentation
BastionGuard™
PasswordDialogs.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
21#include "PasswordDialogs.hpp"
22#include <glibmm/i18n.h>
23#include <sstream>
24
26
27PasswordDialog::PasswordDialog(Gtk::Window& parent, const Glib::ustring& title)
28: Gtk::Dialog(title, parent, true)
29{
30 build();
31}
32
33PasswordDialog::PasswordDialog(Gtk::Window& parent, const Glib::ustring& title,
34 const PasswordEntry& entry, const std::string& secret)
35: Gtk::Dialog(title, parent, true)
36{
37 build();
38 fillFrom(entry, secret);
39}
40
41
43 set_default_size(520, 460);
44
45 auto* content = get_content_area();
46 content->set_spacing(8);
47 content->set_margin(12);
48
49 // Campi testo normali
50 nameEntry_.set_placeholder_text(_("Nome"));
51 nameEntry_.set_max_length(128);
52
53 usernameEntry_.set_placeholder_text(_("Username / Email"));
54 usernameEntry_.set_max_length(256);
55
56 urlEntry_.set_placeholder_text(_("URL / dominio"));
57 urlEntry_.set_max_length(512);
58
59 tagsEntry_.set_placeholder_text(_("tag1, tag2, tag3"));
60 tagsEntry_.set_max_length(256);
61 passwordEntry_.set_placeholder_text(_("Password"));
62 passwordEntry_.set_visibility(false);
63 passwordEntry_.set_input_purpose(Gtk::InputPurpose::PASSWORD);
64 passwordEntry_.set_max_length(512);
65
66 showPasswordCheck_.set_label(_("Mostra password"));
67 showPasswordCheck_.signal_toggled().connect([this]() {
68 passwordEntry_.set_visibility(showPasswordCheck_.get_active());
69 });
70
71 // Note
72 notesBuffer_ = Gtk::TextBuffer::create();
73 notesView_.set_buffer(notesBuffer_);
74 notesView_.set_wrap_mode(Gtk::WrapMode::WORD);
75 notesView_.set_vexpand(true);
76 notesView_.set_size_request(-1, 80);
77
78 auto notesFrame = Gtk::make_managed<Gtk::Frame>();
79 notesFrame->set_label(_("Note"));
80 notesFrame->set_child(notesView_);
81
82 content->append(nameEntry_);
83 content->append(usernameEntry_);
84 content->append(urlEntry_);
85 content->append(tagsEntry_);
86 content->append(passwordEntry_);
87 content->append(showPasswordCheck_);
88 content->append(*notesFrame);
89
90 add_button(_("Annulla"), Gtk::ResponseType::CANCEL);
91 add_button(_("Salva"), Gtk::ResponseType::OK);
92
93 set_default_response(Gtk::ResponseType::OK);
94}
95
96
97void PasswordDialog::fillFrom(const PasswordEntry& entry, const std::string& secret) {
98 nameEntry_.set_text(entry.name);
99 usernameEntry_.set_text(entry.username);
100 urlEntry_.set_text(entry.url);
101
102 std::ostringstream tags;
103 for (size_t i = 0; i < entry.tags.size(); ++i) {
104 if (i != 0) tags << ", ";
105 tags << entry.tags[i];
106 }
107 tagsEntry_.set_text(tags.str());
108
109 notesBuffer_->set_text(entry.notes);
110 passwordEntry_.set_text(secret);
111}
112
113
116 e.name = nameEntry_.get_text();
117 e.username = usernameEntry_.get_text();
118 e.url = urlEntry_.get_text();
119 e.notes = notesBuffer_->get_text();
120
121 std::stringstream ss(tagsEntry_.get_text());
122 std::string item;
123 while (std::getline(ss, item, ',')) {
124 const auto start = item.find_first_not_of(" \t");
125 const auto end = item.find_last_not_of(" \t");
126 if (start != std::string::npos)
127 e.tags.push_back(item.substr(start, end - start + 1));
128 }
129
130 return e;
131}
132
133std::string PasswordDialog::getSecret() const {
134 return passwordEntry_.get_text();
135}
#define _(s)
Gtk::Entry nameEntry_
PasswordDialog(Gtk::Window &parent, const Glib::ustring &title)
Gtk::CheckButton showPasswordCheck_
std::string getSecret() const
void fillFrom(const PasswordEntry &entry, const std::string &secret)
Gtk::Entry usernameEntry_
Gtk::Entry tagsEntry_
~PasswordDialog() override
Glib::RefPtr< Gtk::TextBuffer > notesBuffer_
Gtk::Entry passwordEntry_
Gtk::TextView notesView_
PasswordEntry getEntry() const
std::string url
std::string username
std::vector< std::string > tags
std::string notes
std::string name