1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <sigc++/functors/mem_fun.h>
23 #include "shieldset-info-dialog.h"
28 #include "glade-helpers.h"
29 #include "ucompose.hpp"
34 ShieldSetInfoDialog::ShieldSetInfoDialog(Shieldset *shieldset, bool readonly)
36 d_shieldset = shieldset;
37 d_readonly = readonly;
39 Glib::RefPtr<Gtk::Builder> xml
40 = Gtk::Builder::create_from_file(get_glade_path()
41 + "/shieldset-info-dialog.ui");
43 xml->get_widget("dialog", dialog);
45 xml->get_widget("accept_button", accept_button);
46 xml->get_widget("status_label", status_label);
48 xml->get_widget("name_entry", name_entry);
49 name_entry->set_text(shieldset->getName());
50 if (readonly == false)
51 name_entry->signal_changed().connect
52 (sigc::mem_fun(this, &ShieldSetInfoDialog::on_name_changed));
54 xml->get_widget("subdir_entry", subdir_entry);
55 subdir_entry->set_text(shieldset->getSubDir());
56 if (readonly == false)
57 subdir_entry->signal_changed().connect
58 (sigc::mem_fun(this, &ShieldSetInfoDialog::on_subdir_changed));
60 xml->get_widget("id_spinbutton", id_spinbutton);
61 id_spinbutton->set_value(shieldset->getId());
62 id_spinbutton->set_sensitive(false);
64 xml->get_widget("copyright_textview", copyright_textview);
65 copyright_textview->get_buffer()->set_text(d_shieldset->getCopyright());
66 xml->get_widget("license_textview", license_textview);
67 license_textview->get_buffer()->set_text(d_shieldset->getLicense());
68 xml->get_widget("description_textview", description_textview);
69 description_textview->get_buffer()->set_text(shieldset->getInfo());
72 subdir_entry->set_sensitive(false);
75 //remove spaces and lowercase the text
76 char *sanify(const char *string)
80 size_t len = strlen(string);
81 result = (char*) malloc (resultlen);
83 for (unsigned int i = 0; i < len; i++)
85 int letter = tolower(string[i]);
86 if (strchr("abcdefghijklmnopqrstuvwxyz0123456789-", letter) == NULL)
90 result = (char *) realloc (result, resultlen);
93 result[resultlen-2] = char(letter);
94 result[resultlen-1] = '\0';
100 void ShieldSetInfoDialog::on_subdir_changed()
102 std::string dir = File::getUserShieldsetDir() + subdir_entry->get_text();
103 if (File::exists(dir) == true)
105 accept_button->set_sensitive(false);
106 status_label->set_markup(String::ucompose("<b>%1</b>",
107 _("That subdirectory is already in use.")));
111 accept_button->set_sensitive(true);
112 status_label->set_markup("");
115 void ShieldSetInfoDialog::on_name_changed()
117 char *s = sanify(name_entry->get_text().c_str());
118 subdir_entry->set_text(s);
122 ShieldSetInfoDialog::~ShieldSetInfoDialog()
126 void ShieldSetInfoDialog::set_parent_window(Gtk::Window &parent)
128 dialog->set_transient_for(parent);
129 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
132 int ShieldSetInfoDialog::run()
135 int response = dialog->run();
137 if (response == Gtk::RESPONSE_ACCEPT) // accepted
139 d_shieldset->setName(name_entry->get_text());
140 d_shieldset->setId(int(id_spinbutton->get_value()));
141 if (d_readonly == false)
142 d_shieldset->setSubDir(subdir_entry->get_text());
143 d_shieldset->setCopyright(copyright_textview->get_buffer()->get_text());
144 d_shieldset->setLicense(license_textview->get_buffer()->get_text());
145 d_shieldset->setInfo(description_textview->get_buffer()->get_text());