1 /*
2  * gnote
3  *
4  * Copyright (C) 2012-2013,2017,2019,2021 Aurimas Cernius
5  * Copyright (C) 2009 Hubert Figuiere
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #include <glibmm/i18n.h>
23 
24 #include "sharp/string.hpp"
25 #include "notebooks/createnotebookdialog.hpp"
26 #include "notebooks/notebookmanager.hpp"
27 #include "iconmanager.hpp"
28 #include "ignote.hpp"
29 #include "utils.hpp"
30 
31 namespace gnote {
32   namespace notebooks {
33 
CreateNotebookDialog(Gtk::Window * parent,GtkDialogFlags f,IGnote & g)34     CreateNotebookDialog::CreateNotebookDialog(Gtk::Window *parent, GtkDialogFlags f, IGnote & g)
35       : utils::HIGMessageDialog(parent, f, Gtk::MESSAGE_OTHER, Gtk::BUTTONS_NONE)
36       , m_gnote(g)
37     {
38       set_title(_("Create Notebook"));
39       Gtk::Grid *table = manage(new Gtk::Grid);
40       table->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
41       table->set_column_spacing(6);
42 
43       Gtk::Label *label = manage(new Gtk::Label (_("N_otebook name:"), true));
44       label->property_xalign() = 0;
45       label->show ();
46 
47       m_nameEntry.signal_changed().connect(
48         sigc::mem_fun(*this, &CreateNotebookDialog::on_name_entry_changed));
49       m_nameEntry.set_activates_default(true);
50       m_nameEntry.show ();
51       label->set_mnemonic_widget(m_nameEntry);
52 
53       m_errorLabel.property_xalign() = 0;
54       m_errorLabel.set_markup(
55         Glib::ustring::compose("<span foreground='red' style='italic'>%1</span>",
56             _("Name already taken")));
57 
58       table->attach(*label, 0, 0, 1, 1);
59       table->attach(m_nameEntry, 1, 0, 1, 1);
60       table->attach(m_errorLabel, 1, 1, 1, 1);
61       table->show ();
62 
63       set_extra_widget(table);
64 
65       add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL, false);
66       // Translation note: This is the Create button in the Create
67       // New Note Dialog.
68       add_button(_("C_reate"), Gtk::RESPONSE_OK, true);
69 
70       // Only let the Ok response be sensitive when
71       // there's something in nameEntry
72       set_response_sensitive (Gtk::RESPONSE_OK, false);
73       m_errorLabel.hide ();
74 
75     }
76 
77 
get_notebook_name()78     Glib::ustring CreateNotebookDialog::get_notebook_name()
79     {
80       return sharp::string_trim(m_nameEntry.get_text());
81     }
82 
83 
set_notebook_name(const Glib::ustring & value)84     void CreateNotebookDialog::set_notebook_name(const Glib::ustring & value)
85     {
86       m_nameEntry.set_text(sharp::string_trim(value));
87     }
88 
89 
on_name_entry_changed()90     void CreateNotebookDialog::on_name_entry_changed()
91     {
92       bool nameTaken = false;
93       if(m_gnote.notebook_manager().notebook_exists(get_notebook_name())) {
94         m_errorLabel.show ();
95         nameTaken = true;
96       }
97       else {
98         m_errorLabel.hide ();
99       }
100 
101       set_response_sensitive (Gtk::RESPONSE_OK,
102         (get_notebook_name().empty() || nameTaken) ? false : true);
103 
104     }
105 
106   }
107 }
108