/
attach.vala
202 lines (166 loc) · 6.37 KB
/
attach.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
/*
* 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 Attach : Dialog
{
private MessageBar message_bar;
private Label account_label;
private Entry account_entry;
private Box account_box;
private Button cancel_but;
private Button attach_but;
private CheckButton with_networks;
public Attach ()
{
Object (title: Text.attach_title,
transient_for: Haguichi.window,
modal: true,
use_header_bar: (int) Haguichi.dialog_use_header_bar);
GlobalEvents.set_modal_dialog (this);
message_bar = new MessageBar();
cancel_but = (Button) add_button (Text.cancel_label, ResponseType.CANCEL);
attach_but = (Button) add_button (Text.attach_button_label, ResponseType.OK);
attach_but.can_default = true;
attach_but.sensitive = false;
account_entry = new Entry();
account_entry.activates_default = true;
account_entry.width_chars = 30;
account_entry.changed.connect (check_entry_length);
account_entry.changed.connect (hide_message);
account_label = new Label.with_mnemonic (Utils.remove_colons (Text.account_label) + " ");
account_label.halign = Align.END;
account_label.mnemonic_widget = account_entry;
account_box = new Box (Orientation.HORIZONTAL, 0);
account_box.pack_start (account_label, false, false, 0);
account_box.pack_start (account_entry, true, true, 0);
with_networks = new CheckButton.with_mnemonic (Text.attach_with_networks_checkbox);
with_networks.active = true;
Box vbox = new Box (Orientation.VERTICAL, 0);
vbox.pack_start (account_box, false, false, 0);
vbox.pack_start (with_networks, false, false, 6);
vbox.margin = 11;
vbox.margin_bottom = 0;
Box container = new Box (Orientation.VERTICAL, 0);
container.pack_start (message_bar, true, true, 0);
container.pack_start (vbox, true, true, 0);
container.show_all();
get_content_area().add (container);
get_action_area().margin = 6;
get_action_area().margin_top = 0;
hide_message();
account_entry.grab_focus();
show();
resizable = false;
response.connect ((response_id) =>
{
if (response_id == ResponseType.OK)
{
set_mode ("Attaching");
new Thread<void*> (null, go_attach_thread);
}
else
{
dismiss();
}
});
}
private void* go_attach_thread ()
{
string output;
if (Haguichi.demo_mode)
{
output = ".. failed, not found";
}
else
{
output = Hamachi.attach (account_entry.get_chars (0, -1), with_networks.active);
}
if (output.index_of (".. ok") != -1)
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
dismiss();
return false;
});
Thread.usleep (2000000);
Hamachi.get_info();
GlobalEvents.set_attach();
}
else if ((output.index_of (".. failed, not found") != -1) ||
(output.index_of (".. failed, [248]") != -1))
{
show_message (Text.attach_error_account_not_found);
}
else if (output.index_of (".. failed") != -1)
{
show_message (Text.error_unknown);
}
else
{
// Unknown output
}
return null;
}
private void check_entry_length ()
{
string account = account_entry.get_chars (0, -1);
if (account.length > 0)
{
attach_but.sensitive = true;
attach_but.get_style_context().add_class ("suggested-action");
attach_but.grab_default();
}
else
{
attach_but.sensitive = false;
attach_but.get_style_context().remove_class ("suggested-action");
}
}
private void show_message (string message)
{
Idle.add_full (Priority.HIGH_IDLE, () =>
{
set_mode ("Normal");
message_bar.set_message (message, null, MessageType.ERROR);
return false;
});
}
private void hide_message ()
{
message_bar.hide();
}
private void dismiss ()
{
GlobalEvents.set_modal_dialog (null);
destroy();
}
private void set_mode (string mode)
{
switch (mode)
{
case "Attaching":
account_entry.sensitive = false;
with_networks.sensitive = false;
cancel_but.sensitive = false;
attach_but.sensitive = false;
break;
case "Normal":
account_entry.sensitive = true;
with_networks.sensitive = true;
cancel_but.sensitive = true;
attach_but.sensitive = true;
break;
}
}
}
}