File: client-host.5c

package info (click to toggle)
ricochet 0.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 672 kB
  • sloc: sh: 594; makefile: 133
file content (193 lines) | stat: -rw-r--r-- 5,192 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

autoload Client;
autoload Client::Window;
autoload Client::Link;
autoload Client::Update;
autoload Client::Userlist;
autoload Client::Messages;
autoload Client::Games;
autoload ParseArgs;
autoload RR;
autoload Cairo;
autoload Nichrome;
autoload Nichrome::Button;
autoload Nichrome::Box;
autoload Nichrome::Label;
autoload Nichrome::Toggle;
autoload Nichrome::Textline;
autoload Nichrome::RRboard;
autoload Nichrome::Solid;
autoload Mutex;
autoload Process;

extend namespace Client {
	public namespace Host {
		import Nichrome;
		import Util;
		import Box;

		public string	host;
		public string 	name;
		public string	rrserve_path = "rrserve";

		try {
			name = Environ::get("USER");
		} catch invalid_argument(string message, int id, poly value) {
			name = "user";
		}

		*Nichrome::nichrome_t	ui;
		*Label::label_t		name_title;
		*Label::label_t		name_label;
		*Textline::textline_t	name_text;
		*Label::label_t		host_title;
		*Label::label_t		master_label;
		*Label::label_t		master;
		*Button::button_t	master_connect;
		*Label::label_t		remote_label;
		*Textline::textline_t	host_text;
		*Button::button_t	remote_connect;
		*Label::label_t		local_label;
		*Button::button_t	local;
		*Button::button_t	local_connect;
		*Button::button_t	cancel;

		bool	ret;

		void stop (bool val) {
			name = name_text->text;
			ret = val;
			Nichrome::destroy(ui);
		}

		void do_ok(*widget_t w, bool state) {
			if (state) {
				host = host_text->text;
				stop(true);
			}
		}

		void do_cancel(*widget_t w, bool state) {
			if (state) {
				if (is_uninit(&host))
					exit(0);
				stop(false);
			}
		}

		bool key_callback(*Textline::textline_t widget, string key) {
			switch (key) {
			case "Return": do_ok(widget, true); return true;
			default:
			}
			return false;
		}
		
		string master_host = "rr.nickle.org";

		void do_master(*Button::button_t w, bool state) {
			if (state) {
				host = master_host;
				stop(true);
			}
		}

		void do_remote(*Button::button_t w, bool state) {
			if (state) {
				host = host_text->text;
				stop(true);
			}
		}


		void do_local(*Button::button_t w, bool state) {
			if (state) {
				host = "localhost";
				stop(true);
			}
		}

		void do_start_local(*Button::button_t w, bool state) {
			if (state) {
				Process::system(rrserve_path, "rrserve");
			}
		}

		public bool select() {
			ui = new("Connect to Ricochet Robots Server", 100, 100);

			name_title = new_bold(ui, "Choose a name", Label::justify_t.center);

			name_label = new_left(ui, "User name:");
			name_text = Textline::new(ui, 40);
			if (!is_uninit(&name))
				Textline::set_text(name_text, name);

			host_title = new_bold(ui, "Select a server", Label::justify_t.center);

			master_label = new_left (ui, "Master server:");
			master = new_left(ui, master_host);
			master_connect = Button::new(ui, "Connect", do_master);

			remote_label = new_left (ui, "Another server:");
			host_text = Textline::new(ui, 40);
			host_text->callback = key_callback;
			if (!is_uninit(&host))
				Textline::set_text(host_text, host);
			remote_connect = Button::new(ui, "Connect", do_remote);

			local_label = new_left (ui, "Local server:");
			local = Button::new(ui, "Start", do_start_local);
			local_connect = Button::new(ui, "Connect", do_local);

			cancel = Button::new(ui, "Cancel", do_cancel);
			*Box::box_t	box = Box::new_empty();
			int row = 0;
			Box::add_row(box, 0, row++,
				     Box::widget_span_item(name_title, 4, 1, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(name_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(name_text, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_span_item(host_title, 4, 1, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(master_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(master, 1, 1),
				     Box::widget_item(master_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(remote_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(host_text, 1, 1),
				     Box::widget_item(remote_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(local_label, 0, 1),
				     Box::glue_item(1),
				     Box::widget_item(local, 1, 1),
				     Box::widget_item(local_connect, 1, 0));
			Box::add_row(box, 0, row++,
				     Box::widget_item(cancel, 1, 0));
			Nichrome::set_box(ui, box);
			set_key_focus(ui, host_text);
			Nichrome::main_loop(ui);
			return ret;
		}
	}
}