-
Notifications
You must be signed in to change notification settings - Fork 2
/
sshconf.vala
385 lines (328 loc) · 12.5 KB
/
sshconf.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* sshconf.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]>
*/
/**
* Todo list:
*
* @todo: Make validator for entry. (separate widget that shows this)
* @todo: ComboBox entry is very long, needs better interface.
*/
using Gtk;
using Gee;
namespace SSHConf
{
class Overview : Gtk.Dialog
{
private string source_filename = null;
private Gtk.Widget apply_button;
private Gtk.ToolButton add_rule_button;
private Gtk.ToolButton remove_rule_button;
private Gtk.ToolButton copy_rule_button;
private Gtk.TreeView tree;
private Gtk.TreeModel model;
private Gtk.Widget editor = null;
private Gtk.Box main_box = null;
private Entry default_settings = new DefaultEntry();
~Overview()
{
stdout.printf("~Overview\n");
}
/**
* Loading file
*/
public async void load_file(string filename)
{
this.source_filename = filename;
File file = GLib.File.new_for_path(filename);
try
{
string? res = null;
Entry? current = default_settings;
var stream = yield file.open_readwrite_async();
var bdstream = new DataInputStream(stream.input_stream);
do
{
res = yield bdstream.read_line_async();
if(res != null)
{
string[] cmds = res.strip().split(" ",2);
if(cmds.length > 0)
{
bool disabled = false;
string command = cmds[0].down();
if(command[0] == '#')
{
disabled = true;
command = command.substring(1, -1).strip();
}
if(command == "host")
{
current = new Entry();
current.name = cmds[1].strip();
current.enabled = !disabled;
(model as EntryModel).add_entry(current);
}
else if (command == "hostname")
{
if(current == null)
continue;
current.hostname = cmds[1].strip();
}
else
{
if(current == null)
continue;
current.add_pair(command.strip(), cmds[1].strip());
}
}
}
}while(res != null);
}catch(Error e)
{
}
}
public void write_file()
{
stdout.printf("write file: %s\n", this.source_filename);
File file = GLib.File.new_for_path(this.source_filename);
try
{
stdout.printf("write file 1\n");
var stream = file.replace(null,true,
FileCreateFlags.REPLACE_DESTINATION );
var da = new DataOutputStream (stream);
Gtk.TreeIter iter;
stdout.printf("write file 2\n");
default_settings.write_entry(da);
da.put_string("\n");
if(model.get_iter_first(out iter))
{
do
{
Entry? en = null;
model.get(iter, 0, out en);
en.write_entry(da);
da.put_string("\n");
}while(model.iter_next(ref iter));
}
}catch(Error e)
{
stdout.printf("failed to write file: %s\n", e.message);
}
}
construct
{
/* SSH Config */
this.set_title("SSH Config");
this.set_size_request(600, 400);
/* Apply button */
apply_button = this.add_button("gtk-cancel", -2);
/* Close button */
this.add_button("gtk-save", -1);
/* The internal */
main_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
(this.get_content_area() as Gtk.Box).pack_end(main_box, true, true, 0);
main_box.set_border_width(8);
var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
box.set_size_request(250, -1);
main_box.pack_start(box, false, false, 0);
/* Model */
model = new EntryModel();
/* Treeview */
var sw = new Gtk.ScrolledWindow(null,null);
sw.shadow_type = Gtk.ShadowType.ETCHED_IN;
tree = new Gtk.TreeView();
tree.set_model(model);
tree.rules_hint = true;
sw.add(tree);
box.pack_start(sw, true, true, 0);
var column = new Gtk.TreeViewColumn();
tree.headers_visible = false;
tree.append_column(column);
var renderer = new Gtk.CellRendererText();
column.pack_start(renderer, true);
column.set_attributes(renderer, "text", EntryModel.Columns.NAME);
/* button box */
var hbox = new Gtk.Toolbar();
hbox.toolbar_style = Gtk.ToolbarStyle.ICONS;
/* add */
var icon = new GLib.ThemedIcon.with_default_fallbacks( "list-add-symbolic");
var image = new Gtk.Image.from_gicon(icon, Gtk.IconSize.SMALL_TOOLBAR);
add_rule_button = new Gtk.ToolButton(image, null);//.from_stock("gtk-add");
add_rule_button.tooltip_text = "Add entry";
(add_rule_button as Gtk.ToolButton).clicked.connect(add_entry);
hbox.insert(add_rule_button,0);
icon = new GLib.ThemedIcon.with_default_fallbacks( "gtk-copy");
image = new Gtk.Image.from_gicon(icon, Gtk.IconSize.SMALL_TOOLBAR);
copy_rule_button = new Gtk.ToolButton(image, null);//.from_stock("gtk-add");
copy_rule_button.tooltip_text = "Copy entry";
(copy_rule_button as Gtk.ToolButton).clicked.connect(copy_entry);
hbox.insert(copy_rule_button,-1);
copy_rule_button.sensitive = false;
/* remove */
icon = new GLib.ThemedIcon.with_default_fallbacks( "list-remove-symbolic");
image = new Gtk.Image.from_gicon(icon, Gtk.IconSize.SMALL_TOOLBAR);
remove_rule_button = new Gtk.ToolButton(image, null);//.from_stock("gtk-remove");
remove_rule_button.sensitive = false;
remove_rule_button.tooltip_text = "Remove entry";
hbox.insert(remove_rule_button,1);
(remove_rule_button as Gtk.ToolButton).clicked.connect((source) =>
{
Gtk.TreeIter iter;
if(tree.get_selection().get_selected(null, out iter))
{
Entry? en = null;
model.get(iter, 0, out en);
(model as EntryModel).remove_entry(en);
}
});
/**
* Setup the styles for the button box
*/
var context = hbox.get_style_context();
context.set_junction_sides(Gtk.JunctionSides.TOP);
context.add_class(Gtk.STYLE_CLASS_INLINE_TOOLBAR);
context = sw.get_style_context();
context.set_junction_sides(Gtk.JunctionSides.BOTTOM);
/* selection */
tree.get_selection().changed.connect((source) =>
{
Gtk.TreeIter iter;
if(tree.get_selection().get_selected(null, out iter))
{
remove_rule_button.sensitive = true;
copy_rule_button.sensitive = true;
Entry? en = null;
model.get(iter, EntryModel.Columns.ENTRY, out en);
set_editor(en);
}
else
{
remove_rule_button.sensitive = false;
copy_rule_button.sensitive = false;
set_editor(default_settings);
}
});
box.pack_start(hbox, false, false, 0);
this.show_all();
set_editor(default_settings);
}
private void set_editor(Entry? en)
{
stdout.printf("set editor: %p\n",en);
if(editor != null) editor.destroy();
editor = null;
if(en == null)
{
return;
}
editor = new Editor(this,en);
main_box.pack_start(editor, true, true,0);
main_box.show();
}
private void copy_entry()
{
Gtk.TreeIter iter;
if(tree.get_selection().get_selected(null, out iter))
{
Entry? en = null;
model.get(iter, 0, out en);
Entry? entry_copy = new Entry.copy(en);
entry_copy.name = en.name + ".copy";
Gtk.TreePath path = (model as EntryModel).add_entry(entry_copy);
tree.get_selection().select_path(path);
}
}
private void add_entry()
{
Entry e = new Entry();
e.name = "New entry";
Gtk.TreePath path = (model as EntryModel).add_entry(e);
tree.get_selection().select_path(path);
}
/**
* Quit when dialog get closed either via escape, cancel, window close
* or save. In case of save, write out file
*
* @todo: when there are changes, ask confirmation before discarding
*/
public override void response(int response_id)
{
if(response_id == -1)
{
write_file();
}
Gtk.main_quit();
this.destroy();
}
static int main(string[] argv)
{
Gtk.init(ref argv);
/* Create GtkApplication */
var application = new Gtk.Application(
"org.gmpclient.sshconf",
GLib.ApplicationFlags.HANDLES_OPEN);
try
{
application.register();
}
catch (Error err)
{
stderr.printf("Failed to register application: %s\n", err.message);
return 1;
}
/* Only allow one instance off the application to be running */
if(application.is_remote)
{
application.activate();
application = null;
return 0;
}
/* Create the gui */
var a = new SSHConf.Overview();
application.add_window(a);
/* Create path to ~/.ssh/config */
if(argv.length > 1) {
a.load_file(argv[1]);
}else{
var path = GLib.Path.build_filename(
GLib.Environment.get_home_dir(),
".ssh",
"config");
/* Load the file */
a.load_file(path);
}
/* Response to the activate signal on GApplication by rasing
* window */
application.activate.connect(()=>
{
(a as Gtk.Window).present();
});
/* Show all and run */
a.show();
Gtk.main();
/* quit & cleanup */
stdout.printf("quiting...\n");
a = null;
application = null;
return 0;
}
}
} // end SSHConf