/
join-create-network.vala
331 lines (274 loc) · 10.9 KB
/
join-create-network.vala
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* This file is part of Haguichi, a graphical frontend for Hamachi.
* Copyright (C) 2007-2017 Stephen Brandt <stephen@stephenbrandt.com>
*
* Haguichi 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, either version 3 of the License,
* or (at your option) any later version.
*/
using Gtk;
using Widgets;
namespace Dialogs
{
public class JoinCreateNetwork : Dialog
{
private string mode;
private string network_id;
private string network_password;
private MessageBar message_bar;
private Label heading;
private Label id_label;
private Entry id_entry;
private Label password_label;
private Entry password_entry;
private Button cancel_but;
private Button ok_but;
public JoinCreateNetwork (string _mode, string _title)
{
Object (title: _title,
transient_for: Haguichi.window,
modal: true,
use_header_bar: (int) Haguichi.dialog_use_header_bar);
GlobalEvents.set_modal_dialog (this);
mode = _mode;
heading = new Label (null);
heading.halign = Align.START;
message_bar = new MessageBar();
cancel_but = (Button) add_button (Text.cancel_label, ResponseType.CANCEL);
if (mode == "Join")
{
ok_but = (Button) add_button (Text.join_label, ResponseType.OK);
}
if (mode == "Create")
{
ok_but = (Button) add_button (Text.create_label, ResponseType.OK);
}
ok_but.can_default = true;
id_entry = new Entry();
id_entry.changed.connect (check_id_length);
id_entry.changed.connect (hide_message);
id_entry.activates_default = true;
id_entry.width_chars = 30;
id_entry.max_length = 64;
id_label = new Label.with_mnemonic (Utils.remove_colons (Text.network_id_label) + " ");
id_label.halign = Align.END;
id_label.mnemonic_widget = id_entry;
password_entry = new Entry();
password_entry.changed.connect (hide_message);
password_entry.activates_default = true;
password_entry.visibility = false;
password_entry.width_chars = 30;
password_label = new Label.with_mnemonic (Utils.remove_colons (Text.password_label) + " ");
password_label.halign = Align.END;
password_label.mnemonic_widget = password_entry;
Grid grid = new Grid();
grid.row_spacing = 6;
grid.column_spacing = 6;
grid.attach (id_label, 0, 1, 1, 1);
grid.attach (id_entry, 1, 1, 1, 1);
grid.attach (password_label, 0, 2, 1, 1);
grid.attach (password_entry, 1, 2, 1, 1);
grid.margin = 11;
Box container = new Box (Orientation.VERTICAL, 0);
container.pack_start (message_bar, true, true, 0);
container.pack_start (grid, true, true, 0);
container.show_all();
get_content_area().add (container);
get_action_area().margin = 6;
get_action_area().margin_top = 0;
hide_message();
set_mode ("Normal");
id_entry.grab_focus();
show();
resizable = false;
response.connect ((response_id) =>
{
if (response_id == ResponseType.OK)
{
set_mode ("Working");
if (mode == "Join")
{
new Thread<void*> (null, go_join_thread);
}
if (mode == "Create")
{
new Thread<void*> (null, go_create_thread);
}
}
else
{
dismiss();
}
});
}
private void dismiss ()
{
GlobalEvents.set_modal_dialog (null);
destroy();
}
private void* go_join_thread ()
{
network_id = id_entry.get_chars (0, -1);
network_password = password_entry.get_chars (0, -1);
string output = "";
if (Haguichi.demo_mode)
{
output = ".. ok, request sent";
}
else
{
output = Hamachi.join_network (network_id, network_password);
}
if (output.contains (".. ok, request sent"))
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
dismiss();
HaguichiWindow.message_bar.set_message_with_timeout (Text.request_sent_message, null, MessageType.INFO, 3000);
return false;
});
}
else if (output.contains (".. ok"))
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
dismiss();
return false;
});
Thread.usleep (1000000); // Wait a second to get an updated list
Idle.add_full (Priority.HIGH_IDLE, () =>
{
Controller.update_connection(); // Update list
return false;
});
Thread.usleep (2000000);
Hamachi.set_nick ((string) Settings.nickname.val); // Set nick to make sure any clients in this network will see it
}
else if (output.contains (".. failed, network not found"))
{
show_message (output, Text.error_network_not_found);
}
else if (output.contains (".. failed, invalid password"))
{
show_message (output, Text.error_invalid_password);
}
else if (output.contains (".. failed, the network is full"))
{
show_message (output, Text.error_network_full);
}
else if (output.contains (".. failed, network is locked"))
{
show_message (output, Text.error_network_locked);
}
else if (output.contains (".. failed, you are already a member") ||
output.contains (".. failed, you are an owner"))
{
show_message (output, Text.error_network_already_joined);
}
else
{
show_message (output, Text.error_unknown);
}
return null;
}
private void* go_create_thread ()
{
network_id = id_entry.get_chars (0, -1);
network_password = password_entry.get_chars (0, -1);
if (Haguichi.demo_mode)
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
dismiss();
Network new_network = new Network (new Status ("*"), Hamachi.random_network_id(), network_id, "This computer", 5);
Haguichi.connection.add_network (new_network);
Haguichi.window.network_view.add_network (new_network);
return false;
});
return null;
}
string output = Hamachi.create_network (network_id, network_password);
if (output.contains (".. ok"))
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
dismiss();
return false;
});
Thread.usleep (1000000); // Wait a second to get an updated list
Idle.add_full (Priority.HIGH_IDLE, () =>
{
Controller.update_connection(); // Update list
return false;
});
Thread.usleep (2000000);
Hamachi.set_nick ((string) Settings.nickname.val); // Set nick to make sure any clients in this network will see it
}
else if (output.contains (".. failed, network name is already taken"))
{
show_message (output, Text.error_network_id_taken);
}
else
{
show_message (output, Text.error_unknown);
}
return null;
}
private void show_message (string output, string message)
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
set_mode ("Normal");
if (output.contains ("password"))
{
password_entry.grab_focus();
}
else
{
id_entry.grab_focus();
}
message_bar.set_message (message, null, MessageType.ERROR);
return false;
});
}
private void hide_message ()
{
message_bar.hide();
}
private void check_id_length ()
{
string id = id_entry.get_chars (0, -1);
if (id.length >= 4)
{
ok_but.sensitive = true;
ok_but.get_style_context().add_class ("suggested-action");
ok_but.grab_default();
}
else
{
ok_but.sensitive = false;
ok_but.get_style_context().remove_class ("suggested-action");
}
}
private void set_mode (string _mode)
{
switch (_mode)
{
case "Working":
cancel_but.sensitive = false;
ok_but.sensitive = false;
id_entry.sensitive = false;
password_entry.sensitive = false;
break;
case "Normal":
cancel_but.sensitive = true;
ok_but.sensitive = false;
id_entry.sensitive = true;
password_entry.sensitive = true;
check_id_length();
break;
}
}
}
}