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 "armyset-info-dialog.h"
28 #include "glade-helpers.h"
29 #include "ucompose.hpp"
34 ArmySetInfoDialog::ArmySetInfoDialog(Armyset *armyset, bool readonly)
37 d_readonly = readonly;
39 Glib::RefPtr<Gtk::Builder> xml
40 = Gtk::Builder::create_from_file(get_glade_path()
41 + "/armyset-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(armyset->getName());
50 if (readonly == false)
51 name_entry->signal_changed().connect
52 (sigc::mem_fun(this, &ArmySetInfoDialog::on_name_changed));
54 xml->get_widget("subdir_entry", subdir_entry);
55 subdir_entry->set_text(armyset->getSubDir());
56 if (readonly == false)
57 subdir_entry->signal_changed().connect
58 (sigc::mem_fun(this, &ArmySetInfoDialog::on_subdir_changed));
60 xml->get_widget("id_spinbutton", id_spinbutton);
61 id_spinbutton->set_value(armyset->getId());
62 id_spinbutton->set_sensitive(false);
64 xml->get_widget("copyright_textview", copyright_textview);
65 copyright_textview->get_buffer()->set_text(d_armyset->getCopyright());
66 xml->get_widget("license_textview", license_textview);
67 license_textview->get_buffer()->set_text(d_armyset->getLicense());
70 subdir_entry->set_sensitive(false);
73 //remove spaces and lowercase the text
74 char *sanify(const char *string)
78 size_t len = strlen(string);
79 result = (char*) malloc (resultlen);
81 for (unsigned int i = 0; i < len; i++)
83 int letter = tolower(string[i]);
84 if (strchr("abcdefghijklmnopqrstuvwxyz0123456789-", letter) == NULL)
88 result = (char *) realloc (result, resultlen);
91 result[resultlen-2] = char(letter);
92 result[resultlen-1] = '\0';
98 void ArmySetInfoDialog::on_subdir_changed()
100 std::string dir = File::getUserArmysetDir() + subdir_entry->get_text();
101 if (File::exists(dir) == true)
103 accept_button->set_sensitive(false);
104 status_label->set_markup(String::ucompose("<b>%1</b>",
105 _("That subdirectory is already in use.")));
109 accept_button->set_sensitive(true);
110 status_label->set_markup("");
113 void ArmySetInfoDialog::on_name_changed()
115 char *s = sanify(name_entry->get_text().c_str());
116 subdir_entry->set_text(s);
120 ArmySetInfoDialog::~ArmySetInfoDialog()
124 void ArmySetInfoDialog::set_parent_window(Gtk::Window &parent)
126 dialog->set_transient_for(parent);
127 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
130 int ArmySetInfoDialog::run()
133 int response = dialog->run();
135 if (response == Gtk::RESPONSE_ACCEPT) // accepted
137 d_armyset->setName(name_entry->get_text());
138 d_armyset->setId(int(id_spinbutton->get_value()));
139 if (d_readonly == false)
140 d_armyset->setSubDir(subdir_entry->get_text());
141 d_armyset->setCopyright(copyright_textview->get_buffer()->get_text());
142 d_armyset->setLicense(license_textview->get_buffer()->get_text());