1 //  Copyright (C) 2011, 2014, 2015 Ben Asselstine
2 //
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.
7 //
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.
12 //
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
16 //  02110-1301, USA.
17 
18 #include <config.h>
19 
20 #include <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22 #include "new-network-game-dialog.h"
23 #include "Configuration.h"
24 #include "defs.h"
25 #include "ucompose.hpp"
26 #include "profile.h"
27 #include "profilelist.h"
28 #include "new-profile-dialog.h"
29 
30 #define method(x) sigc::mem_fun(*this, &NewNetworkGameDialog::x)
31 
NewNetworkGameDialog(Gtk::Window & parent,bool force_server)32 NewNetworkGameDialog::NewNetworkGameDialog(Gtk::Window &parent, bool force_server)
33  : LwDialog(parent, "new-network-game-dialog.ui")
34 {
35   xml->get_widget("client_radiobutton", client_radiobutton);
36   xml->get_widget("server_radiobutton", server_radiobutton);
37   client_radiobutton->signal_toggled().connect(method(on_client_radiobutton_toggled));
38   xml->get_widget("accept_button", accept_button);
39   xml->get_widget("add_button", add_button);
40   add_button->signal_clicked().connect(method(on_add_button_clicked));
41   xml->get_widget("remove_button", remove_button);
42   remove_button->signal_clicked().connect(method(on_remove_button_clicked));
43   xml->get_widget("profiles_treeview", profiles_treeview);
44   profiles_list = Gtk::ListStore::create(profiles_columns);
45   profiles_treeview->set_model(profiles_list);
46   profiles_treeview->append_column("", profiles_columns.nickname);
47   profiles_treeview->signal_row_activated().connect(sigc::hide(sigc::hide(method(on_profile_activated))));
48 
49   if (Profilelist::getInstance()->empty() == true)
50     Profilelist::getInstance()->push_back(new Profile(Glib::get_user_name()));
51 
52   for (Profilelist::iterator i = Profilelist::getInstance()->begin();
53        i != Profilelist::getInstance()->end(); i++)
54     add_profile(*i);
55 
56   xml->get_widget("advertise_checkbutton", advertise_checkbutton);
57   xml->get_widget("remote_checkbutton", remote_checkbutton);
58   remote_checkbutton->signal_toggled().connect
59     (method(on_remote_checkbutton_toggled));
60 
61   if (Configuration::s_gamelist_server_hostname == "" ||
62       Configuration::s_gamelist_server_port == 0)
63     advertise_checkbutton->set_label (_("List the game on a remote server."));
64   else
65     advertise_checkbutton->set_label
66     (String::ucompose(_("List the game on %1."),
67                       Configuration::s_gamelist_server_hostname));
68   if (Configuration::s_gamehost_server_hostname == "" ||
69       Configuration::s_gamehost_server_port == 0)
70     remote_checkbutton->set_label
71       (_("Host and list the game on a remote server."));
72   else
73     remote_checkbutton->set_label
74       (String::ucompose(_("Host and list the game on %1."),
75                         Configuration::s_gamehost_server_hostname));
76   select_preferred_profile(Glib::get_user_name());
77   update_buttons();
78   profiles_treeview->get_selection()->signal_changed().connect
79     (method(on_profile_selected));
80   if (force_server)
81     {
82       server_radiobutton->set_active(true);
83       server_radiobutton->set_sensitive(false);
84       client_radiobutton->set_sensitive(false);
85     }
86 }
87 
select_preferred_profile(Glib::ustring user)88 void NewNetworkGameDialog::select_preferred_profile(Glib::ustring user)
89 {
90   Profile *p = Profilelist::getInstance()->findLastPlayedProfileForUser(user);
91   Gtk::TreeModel::Children kids = profiles_list->children();
92   for (Gtk::TreeModel::Children::iterator i = kids.begin();
93        i != kids.end(); i++)
94     {
95       Gtk::TreeModel::Row row = *i;
96       if (row[profiles_columns.profile] == p)
97         {
98           profiles_treeview->get_selection()->select(row);
99           return;
100         }
101     }
102 
103   Gtk::TreeModel::Row row = profiles_treeview->get_model()->children()[0];
104   if(row)
105     profiles_treeview->get_selection()->select(row);
106 }
107 
add_profile(Profile * profile)108 void NewNetworkGameDialog::add_profile(Profile *profile)
109 {
110   Gtk::TreeIter i = profiles_list->append();
111   (*i)[profiles_columns.nickname] = profile->getNickname();
112   (*i)[profiles_columns.profile] = profile;
113 }
114 
update_buttons()115 void NewNetworkGameDialog::update_buttons()
116 {
117   Glib::RefPtr<Gtk::TreeSelection> selection =
118     profiles_treeview->get_selection();
119   Gtk::TreeModel::iterator iterrow = selection->get_selected();
120   if (iterrow)
121     accept_button->set_sensitive(true);
122   else
123     accept_button->set_sensitive(false);
124 
125   if (Configuration::s_gamelist_server_hostname != "" &&
126       Configuration::s_gamelist_server_port != 0)
127     advertise_checkbutton->set_sensitive(!client_radiobutton->get_active());
128   else
129     advertise_checkbutton->set_sensitive(false);
130 
131   if (Configuration::s_gamehost_server_hostname != "" &&
132       Configuration::s_gamehost_server_port != 0)
133     remote_checkbutton->set_sensitive(!client_radiobutton->get_active());
134   else
135     remote_checkbutton->set_sensitive(false);
136 
137   if (remote_checkbutton->get_active() && remote_checkbutton->property_sensitive())
138     {
139       advertise_checkbutton->set_sensitive(true);
140       advertise_checkbutton->set_active(false);
141       advertise_checkbutton->set_sensitive(false);
142     }
143 }
144 
run()145 bool NewNetworkGameDialog::run()
146 {
147   int response = dialog->run();
148   if (response == Gtk::RESPONSE_ACCEPT)
149     {
150       Profilelist::getInstance()->save();
151       Glib::RefPtr<Gtk::TreeSelection> selection =
152         profiles_treeview->get_selection();
153       Gtk::TreeModel::iterator iterrow = selection->get_selected();
154       if (iterrow)
155         {
156           Gtk::TreeModel::Row row = *iterrow;
157           d_profile = row[profiles_columns.profile];
158           d_profile->play();
159           Profilelist::getInstance()->save();
160         }
161       return true;
162     }
163   return false;
164 }
165 
on_add_button_clicked()166 void NewNetworkGameDialog::on_add_button_clicked()
167 {
168   NewProfileDialog d(*dialog);
169   if (d.run_and_hide() == Gtk::RESPONSE_ACCEPT)
170     {
171       Profile *profile = new Profile (d.getNickname());
172       Profilelist::getInstance()->push_back(profile);
173       add_profile(profile);
174       Gtk::TreeModel::Row row;
175       int n = profiles_treeview->get_model()->children().size();
176       row = profiles_treeview->get_model()->children()[n-1];
177       if(row)
178 	profiles_treeview->get_selection()->select(row);
179     }
180   update_buttons();
181 }
182 
on_remove_button_clicked()183 void NewNetworkGameDialog::on_remove_button_clicked()
184 {
185   Glib::RefPtr<Gtk::TreeSelection> selection =
186     profiles_treeview->get_selection();
187   Gtk::TreeModel::iterator iterrow = selection->get_selected();
188   if (iterrow)
189     {
190       Gtk::TreeModel::Row row = *iterrow;
191       Profile *profile = row[profiles_columns.profile];
192       if (profile)
193         Profilelist::getInstance()->remove(profile);
194       profiles_list->erase(iterrow);
195     }
196   update_buttons();
197 }
198 
on_profile_selected()199 void NewNetworkGameDialog::on_profile_selected()
200 {
201   update_buttons();
202 }
203 
on_client_radiobutton_toggled()204 void NewNetworkGameDialog::on_client_radiobutton_toggled()
205 {
206   update_buttons();
207 }
208 
on_remote_checkbutton_toggled()209 void NewNetworkGameDialog::on_remote_checkbutton_toggled()
210 {
211   update_buttons();
212 }
213 
on_profile_activated()214 void NewNetworkGameDialog::on_profile_activated()
215 {
216   accept_button->activate();
217 }
218