-
Notifications
You must be signed in to change notification settings - Fork 2
/
sshconf-entry.vala
326 lines (289 loc) · 8.13 KB
/
sshconf-entry.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
/* sshconf-entry.vala
*
* Copyright (C) 2011 Qball Cow <[email protected]>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Author:
* Qball Cow <[email protected]>
*/
using Gtk;
using Gee;
namespace SSHConf
{
public const string VALUE_YES = "yes";
public const string VALUE_NO = "no";
public class DefaultEntry : Entry
{
public override void write_entry(GLib.DataOutputStream da) throws GLib.IOError
{
try
{
foreach(var prop in settings)
{
unowned string value = prop.get_as_string();
// do not write out values without a name
if(value != null && value.length > 0)
{
da.put_string(prop.ep.name);
da.put_string(" ");
da.put_string(value);
da.put_string("\n");
}
}
}
catch (GLib.IOError er)
{
throw er;
}
}
}
/* Class representing a property */
public class Property : GLib.Object
{
public unowned Entry entry;
/* The value */
private string value =null;
public EntryProperty? ep = null;
~Property()
{
GLib.debug("~destroy property");
}
public Property(Entry en,string key)
{
this.entry = en;
for(uint i = 0; i<SSHConf.KEYS.length; i++)
{
if(KEYS[i].name.down() == key.down())
{
ep = KEYS[i];
break;
}
}
if(ep == null)
{
GLib.error("Unknown key: %s", key);
}
if(ep.has_default)
{
value = ep.default_value;
}
}
public void set_as_int(int val)
{
value = "%i".printf(val);
}
public void set_as_bool(bool val)
{
value = (val)?VALUE_YES:VALUE_NO;
GLib.debug("set value: %s", value);
}
public void set_as_string(string? val)
{
value = val;
}
public bool get_as_bool()
{
if(value != null)
{
return value.down() == VALUE_YES.down();
}
return false;
}
public int get_as_int()
{
if(value != null)
{
return int.parse(value);
}
return -1;
}
public unowned string? get_as_string()
{
return value;
}
/**
* get|set_path():
*
* Converts from and to relative path.
* ~/ is replaced by homedir
*/
public string? get_as_path()
{
if(value == null) return null;
string homedir = GLib.Environment.get_home_dir()+"/";
string val = value.replace("~/",homedir);
return val;
}
public void set_as_path(string? val)
{
if(val == null)
{
value = null;
return;
}
string homedir = GLib.Environment.get_home_dir()+"/";
value = val.replace(homedir, "~/");
}
public signal void removed();
}
public class Entry : GLib.Object
{
/* Name of the entry */
private string _name = "";
public bool validate_pattern(string val)
{
return Regex.match_simple("^[a-zA-Z0-9\\.\\?\\*]+$", val);
}
public bool validate_hostname(string val)
{
return Regex.match_simple("^[a-zA-Z0-9\\.]*$", val);
}
public string name
{
get
{
return _name;
}
set
{
if(_name != value)
{
_name = value;
}
}
}
/* Hostname */
private string _hostname = "";
public string hostname
{
get
{
return _hostname;
}
set
{
if(_hostname != value)
{
_hostname = value;
}
}
}
private bool _enabled =true;
public bool enabled
{
get
{
return _enabled;
}
set
{
if(_enabled != value)
{
_enabled = value;
}
}
}
public Entry.copy(Entry en)
{
name = en.name;
hostname = en.hostname;
enabled = en.enabled;
foreach(var prop in en.settings)
{
add_pair(prop.ep.name, prop.get_as_string());
}
}
public enum ChangedType
{
PROPERTY_ADDED
}
public signal void changed(ChangedType what);
public GLib.List<SSHConf.Property> settings = null;
/* Check if entry has a property with key */
public bool has_prop_key(string key)
{
foreach(var prop in settings)
{
if(prop.ep.name.down() == key.down())
{
return true;
}
}
return false;
}
public void new_prop(string key)
{
var entry = new Property(this,key);
settings.append((owned)entry);
changed(ChangedType.PROPERTY_ADDED);
}
public void add_pair(string key, string value)
{
var entry = new Property(this,key);
entry.set_as_string(value);
settings.append((owned)entry);
changed(ChangedType.PROPERTY_ADDED);
}
public void remove_prop(Property prop)
{
weak GLib.List<SSHConf.Property> item = settings.find(prop);
if(item != null)
{
Property p =(owned)item.data;
settings.delete_link(item);
p.removed();
p = null;
}
}
public virtual void write_entry(GLib.DataOutputStream da) throws GLib.IOError
{
try
{
if(!_enabled) da.put_string("#");
da.put_string("Host ");
da.put_string(name);
da.put_string("\n");
if(hostname != null && hostname.length > 0)
{
if(!_enabled) da.put_string("#");
da.put_string("\tHostname ");
da.put_string(hostname);
da.put_string("\n");
}
foreach(var prop in settings)
{
unowned string value = prop.get_as_string();
if(value != null && value.length > 0 )
{
if(!_enabled) da.put_string("#");
da.put_string("\t");
da.put_string(prop.ep.name);
da.put_string(" ");
da.put_string(value);
da.put_string("\n");
}
}
}
catch (GLib.IOError er)
{
throw er;
}
}
~Entry()
{
stdout.printf("~Destroy entry\n");
}
}
}