~ubuntu-branches/ubuntu/trusty/ricochet/trusty

« back to all changes in this revision

Viewing changes to client-host.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-05-30 15:19:02 UTC
  • Revision ID: package-import@ubuntu.com-20120530151902-z47w0cxilb0p6wmi
Tags: 0.2
* Rename 'rrclient' to 'ricochet'
* Add name/host selection dialog
* Add build dependency on 'nickle'. Closes: #674302.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; version 2 of the License.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 
16
 */
 
17
 
 
18
autoload Client;
 
19
autoload Client::Window;
 
20
autoload Client::Link;
 
21
autoload Client::Update;
 
22
autoload Client::Userlist;
 
23
autoload Client::Messages;
 
24
autoload Client::Games;
 
25
autoload ParseArgs;
 
26
autoload RR;
 
27
autoload Cairo;
 
28
autoload Nichrome;
 
29
autoload Nichrome::Button;
 
30
autoload Nichrome::Box;
 
31
autoload Nichrome::Label;
 
32
autoload Nichrome::Toggle;
 
33
autoload Nichrome::Textline;
 
34
autoload Nichrome::RRboard;
 
35
autoload Nichrome::Solid;
 
36
autoload Mutex;
 
37
autoload Process;
 
38
 
 
39
extend namespace Client {
 
40
        public namespace Host {
 
41
                import Nichrome;
 
42
                import Util;
 
43
                import Box;
 
44
 
 
45
                public string   host;
 
46
                public string   name;
 
47
                public string   rrserve_path = "rrserve";
 
48
 
 
49
                try {
 
50
                        name = Environ::get("USER");
 
51
                } catch invalid_argument(string message, int id, poly value) {
 
52
                        name = "user";
 
53
                }
 
54
 
 
55
                *Nichrome::nichrome_t   ui;
 
56
                *Label::label_t         name_title;
 
57
                *Label::label_t         name_label;
 
58
                *Textline::textline_t   name_text;
 
59
                *Label::label_t         host_title;
 
60
                *Label::label_t         master_label;
 
61
                *Label::label_t         master;
 
62
                *Button::button_t       master_connect;
 
63
                *Label::label_t         remote_label;
 
64
                *Textline::textline_t   host_text;
 
65
                *Button::button_t       remote_connect;
 
66
                *Label::label_t         local_label;
 
67
                *Button::button_t       local;
 
68
                *Button::button_t       local_connect;
 
69
                *Button::button_t       cancel;
 
70
 
 
71
                bool    ret;
 
72
 
 
73
                void stop (bool val) {
 
74
                        name = name_text->text;
 
75
                        ret = val;
 
76
                        Nichrome::destroy(ui);
 
77
                }
 
78
 
 
79
                void do_ok(*widget_t w, bool state) {
 
80
                        if (state) {
 
81
                                host = host_text->text;
 
82
                                stop(true);
 
83
                        }
 
84
                }
 
85
 
 
86
                void do_cancel(*widget_t w, bool state) {
 
87
                        if (state) {
 
88
                                if (is_uninit(&host))
 
89
                                        exit(0);
 
90
                                stop(false);
 
91
                        }
 
92
                }
 
93
 
 
94
                bool key_callback(*Textline::textline_t widget, string key) {
 
95
                        switch (key) {
 
96
                        case "Return": do_ok(widget, true); return true;
 
97
                        default:
 
98
                        }
 
99
                        return false;
 
100
                }
 
101
                
 
102
                string master_host = "rr.nickle.org";
 
103
 
 
104
                void do_master(*Button::button_t w, bool state) {
 
105
                        if (state) {
 
106
                                host = master_host;
 
107
                                stop(true);
 
108
                        }
 
109
                }
 
110
 
 
111
                void do_remote(*Button::button_t w, bool state) {
 
112
                        if (state) {
 
113
                                host = host_text->text;
 
114
                                stop(true);
 
115
                        }
 
116
                }
 
117
 
 
118
 
 
119
                void do_local(*Button::button_t w, bool state) {
 
120
                        if (state) {
 
121
                                host = "localhost";
 
122
                                stop(true);
 
123
                        }
 
124
                }
 
125
 
 
126
                void do_start_local(*Button::button_t w, bool state) {
 
127
                        if (state) {
 
128
                                Process::system(rrserve_path, "rrserve");
 
129
                        }
 
130
                }
 
131
 
 
132
                public bool select() {
 
133
                        ui = new("Connect to Ricochet Robots Server", 100, 100);
 
134
 
 
135
                        name_title = new_bold(ui, "Choose a name", Label::justify_t.center);
 
136
 
 
137
                        name_label = new_left(ui, "User name:");
 
138
                        name_text = Textline::new(ui, 40);
 
139
                        if (!is_uninit(&name))
 
140
                                Textline::set_text(name_text, name);
 
141
 
 
142
                        host_title = new_bold(ui, "Select a server", Label::justify_t.center);
 
143
 
 
144
                        master_label = new_left (ui, "Master server:");
 
145
                        master = new_left(ui, master_host);
 
146
                        master_connect = Button::new(ui, "Connect", do_master);
 
147
 
 
148
                        remote_label = new_left (ui, "Another server:");
 
149
                        host_text = Textline::new(ui, 40);
 
150
                        host_text->callback = key_callback;
 
151
                        if (!is_uninit(&host))
 
152
                                Textline::set_text(host_text, host);
 
153
                        remote_connect = Button::new(ui, "Connect", do_remote);
 
154
 
 
155
                        local_label = new_left (ui, "Local server:");
 
156
                        local = Button::new(ui, "Start", do_start_local);
 
157
                        local_connect = Button::new(ui, "Connect", do_local);
 
158
 
 
159
                        cancel = Button::new(ui, "Cancel", do_cancel);
 
160
                        *Box::box_t     box = Box::new_empty();
 
161
                        int row = 0;
 
162
                        Box::add_row(box, 0, row++,
 
163
                                     Box::widget_span_item(name_title, 4, 1, 1, 0));
 
164
                        Box::add_row(box, 0, row++,
 
165
                                     Box::widget_item(name_label, 0, 1),
 
166
                                     Box::glue_item(1),
 
167
                                     Box::widget_item(name_text, 1, 0));
 
168
                        Box::add_row(box, 0, row++,
 
169
                                     Box::widget_span_item(host_title, 4, 1, 1, 0));
 
170
                        Box::add_row(box, 0, row++,
 
171
                                     Box::widget_item(master_label, 0, 1),
 
172
                                     Box::glue_item(1),
 
173
                                     Box::widget_item(master, 1, 1),
 
174
                                     Box::widget_item(master_connect, 1, 0));
 
175
                        Box::add_row(box, 0, row++,
 
176
                                     Box::widget_item(remote_label, 0, 1),
 
177
                                     Box::glue_item(1),
 
178
                                     Box::widget_item(host_text, 1, 1),
 
179
                                     Box::widget_item(remote_connect, 1, 0));
 
180
                        Box::add_row(box, 0, row++,
 
181
                                     Box::widget_item(local_label, 0, 1),
 
182
                                     Box::glue_item(1),
 
183
                                     Box::widget_item(local, 1, 1),
 
184
                                     Box::widget_item(local_connect, 1, 0));
 
185
                        Box::add_row(box, 0, row++,
 
186
                                     Box::widget_item(cancel, 1, 0));
 
187
                        Nichrome::set_box(ui, box);
 
188
                        set_key_focus(ui, host_text);
 
189
                        Nichrome::main_loop(ui);
 
190
                        return ret;
 
191
                }
 
192
        }
 
193
}