-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.vala
734 lines (676 loc) · 23.4 KB
/
window.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
* Smart Gtk+ menus that know about the sessions they are associated with.
*/
public class TabbedMux.MenuItem : Gtk.MenuItem {
public TMuxStream stream {
get; private set;
}
public MenuItem (TMuxStream stream) {
Object ();
this.stream = stream;
unowned MenuItem unowned_this = this;
stream.connection_closed.connect (unowned_this.on_rename);
on_rename ();
}
private void on_rename () {
set_label (@"$(stream.name) - $(stream.session_name)");
}
}
/**
* Smart menu item to create a new TMux window.
*/
public class TabbedMux.NewMenuItem : MenuItem {
public NewMenuItem (TMuxStream stream) {
base (stream);
}
public override void activate () {
stream.create_window ();
}
}
/**
* Smart menu item to disconnect from a TMux session.
*/
public class TabbedMux.DisconnectMenuItem : MenuItem {
public DisconnectMenuItem (TMuxStream stream) {
base (stream);
}
public override void activate () {
stream.cancel ();
}
}
bool is_monospace_font (Pango.FontFamily family, Pango.FontFace face) {
return family.is_monospace ();
}
/**
* The main window for holding the set of tabs.
*/
[GtkTemplate (ui = "/name/masella/tabbedmux/window.ui")]
public class TabbedMux.Window : Gtk.ApplicationWindow {
[GtkChild]
private unowned Gtk.MenuItem copy_item;
[GtkChild]
private unowned Gtk.Menu new_menu;
[GtkChild]
private unowned Gtk.MenuItem new_session_item;
[GtkChild]
private unowned Gtk.Notebook notebook;
[GtkChild]
private unowned Gtk.Menu disconnect_menu;
[GtkChild]
private unowned Gtk.MenuItem paste_item;
[GtkChild]
private unowned Gtk.Menu saved_menu;
[GtkChild]
private unowned Gtk.MenuItem saved_sessions_item;
[GtkChild]
private unowned Gtk.Menu remove_saved_menu;
[GtkChild]
private unowned Gtk.MenuItem remove_saved_item;
[GtkChild]
private unowned Gtk.MenuItem rename_window_item;
private Settings settings;
private uint configure_id;
private Gtk.Clipboard clipboard;
private Gtk.Clipboard clipboard_selection;
private Gtk.FontChooserDialog font_chooser;
internal Window (Application app) {
Object (application: app, title: "TabbedMux", show_menubar: true, icon_name: "utilities-terminal");
unowned Window unowned_this = this;
/* Allow receiving detailed resize information. */
add_events (Gdk.EventMask.STRUCTURE_MASK | Gdk.EventMask.SUBSTRUCTURE_MASK);
clipboard = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD);
clipboard_selection = Gtk.Clipboard.get_for_display (get_display (), Gdk.SELECTION_PRIMARY);
settings = new Settings (application.application_id);
settings.changed.connect (TMuxWindow.update_settings);
TMuxWindow.update_settings (settings, null);
font_chooser = new Gtk.FontChooserDialog ("Terminal Font", this);
font_chooser.set_filter_func (is_monospace_font);
settings.bind ("font", font_chooser, "font", SettingsBindFlags.GET);
settings.changed.connect (unowned_this.inform_change);
int width;
int height;
settings.get ("size", "(ii)", out width, out height);
set_default_size (width, height);
bool maximized;
settings.get ("maximized", "b", out maximized);
if (maximized) {
maximize ();
}
int x;
int y;
settings.get ("position", "(ii)", out x, out y);
move (x, y);
var accel_group = new Gtk.AccelGroup ();
add_accel_group (accel_group);
copy_item.add_accelerator ("activate", accel_group, 'C', Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE);
new_session_item.add_accelerator ("activate", accel_group, 'T', Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE);
paste_item.add_accelerator ("activate", accel_group, 'V', Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE);
rename_window_item.add_accelerator ("activate", accel_group, 'R', Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK, Gtk.AccelFlags.VISIBLE);
if (app is Application) {
/* Make menus and tabs for the open streams. */
foreach (var stream in ((Application) app).streams) {
add_new_stream (stream);
}
/* Populate a menu for saved sessions. */
var saved_sessions = ((Application) app).saved_sessions;
saved_sessions.changed.connect (this.on_saved_changed);
on_saved_changed (saved_sessions);
/* Notebook new window button. */
var add = new Gtk.Button ();
add.tooltip_text = "New terminal";
add.relief = Gtk.ReliefStyle.NONE;
add.add (new Gtk.Image.from_gicon (new ThemedIcon.with_default_fallbacks ("tab-new-symbolic"), Gtk.IconSize.MENU));
add.show_all ();
notebook.set_action_widget (add, Gtk.PackType.START);
add.clicked.connect (unowned_this.create_session);
/* Notebook close window button. */
var close = new Gtk.Button ();
close.tooltip_text = "Close terminal";
close.relief = Gtk.ReliefStyle.NONE;
close.add (new Gtk.Image.from_gicon (new ThemedIcon.with_default_fallbacks ("window-close-symbolic"), Gtk.IconSize.MENU));
close.show_all ();
notebook.set_action_widget (close, Gtk.PackType.END);
close.clicked.connect (unowned_this.destroy_window);
}
var timeout = new TimeoutSource (500);
timeout.set_callback ((SourceFunc) unowned_this.force_size_update);
timeout.attach (MainContext.default ());
}
public override bool window_state_event (Gdk.EventWindowState event) {
var result = base.window_state_event (event);
settings.set_boolean ("maximized", Gdk.WindowState.MAXIMIZED in get_window ().get_state ());
return result;
}
/**
* Check if the TMux session is currently connected.
*/
private bool is_stream_active (SessionItem item) {
if (!(application is Application)) {
return false;
}
foreach (var stream in ((Application) application).streams) {
if (item.matches (stream)) {
return true;
}
}
return false;
}
/**
* Repopulate the menu of saved sessions.
*/
private void on_saved_changed (SavedSessions sender) {
var non_empty = false;
foreach (var child in saved_menu.get_children ()) {
saved_menu.remove (child);
}
foreach (var child in remove_saved_menu.get_children ()) {
remove_saved_menu.remove (child);
}
sender.update ((session, binary) => {
var open_item = new LocalSessionItem (session, binary);
var remove_item = new RemoveLocalSessionItem (sender, session, binary);
open_item.sensitive = !is_stream_active (open_item);
saved_menu.add (open_item);
remove_saved_menu.add (remove_item);
non_empty = true;
}, (session, host, port, username, binary) => {
var open_item = new SshSessionItem (session, host, port, username, binary);
var remove_item = new RemoveSshSessionItem (sender, session, host, port, username, binary);
open_item.sensitive = !is_stream_active (open_item);
saved_menu.add (open_item);
remove_saved_menu.add (remove_item);
non_empty = true;
});
saved_menu.show_all ();
remove_saved_menu.show_all ();
saved_sessions_item.sensitive = non_empty;
remove_saved_item.sensitive = non_empty;
}
[GtkCallback]
private void add_stream () {
var open_dialog = new OpenDialog (this);
open_dialog.show ();
}
/**
* Create all the menu items for a newly-connected TMux stream.
*/
internal void add_new_stream (TMuxStream stream) {
var new_item = new NewMenuItem (stream);
new_menu.append (new_item);
var disconnect_item = new DisconnectMenuItem (stream);
disconnect_menu.append (disconnect_item);
unowned Window unowned_this = this;
stream.connection_closed.connect (unowned_this.on_connection_closed);
if (application is Application) {
on_saved_changed (((Application) application).saved_sessions);
}
}
/**
* Remove all matching smart menu items from a menu.
*/
private static void menu_remove (Gtk.Widget widget, Gtk.Menu parent, TMuxStream stream) {
if (widget is MenuItem && ((MenuItem) widget).stream == stream) {
parent.remove (widget);
}
}
/**
* Clean up all the menu items for a dead TMux stream.
*/
internal void on_connection_closed (TMuxStream stream, string reason) {
new_menu.@foreach ((widget) => menu_remove (widget, new_menu, stream));
disconnect_menu.@foreach ((widget) => menu_remove (widget, disconnect_menu, stream));
if (application is Application) {
on_saved_changed (((Application) application).saved_sessions);
}
}
/**
* Only have the Edit → Copy menu active if the selection is in the current tab.
*/
private void on_selection_changed (Vte.Terminal terminal) {
if (((Terminal) notebook.get_nth_page (notebook.page)).terminal == terminal) {
copy_item.sensitive = terminal.get_has_selection ();
}
}
[GtkCallback]
internal void open_choose_font () {
if (font_chooser.run () == Gtk.ResponseType.OK) {
settings.set_string ("font", font_chooser.font);
}
font_chooser.hide ();
}
private void inform_change (string setting) {
if (setting == "font" && application is Application) {
var font_desc = Pango.FontDescription.from_string (settings.get_string ("font"));
foreach (var stream in ((Application) application).streams) {
stream.change_font (font_desc);
}
}
}
/**
* Make a tab for a new TMux window.
*/
internal void add_window (TMuxWindow window) {
var terminal = new Terminal (window);
unowned Window unowned_this = this;
// TODO disconnect stream on close of last tab?
window.closed.connect (unowned_this.on_tmux_window_closed);
terminal.terminal.font_desc = Pango.FontDescription.from_string (settings.get_string ("font"));
var id = notebook.append_page (terminal, terminal.tab_label);
notebook.set_tab_reorderable (terminal, true);
terminal.terminal.selection_changed.connect (unowned_this.on_selection_changed);
message ("Adding window from %s.", window.stream.name);
show_all ();
notebook.set_current_page (id);
}
/**
* Create a “new” window.
*
* This will create one for the TMux stream of the current tab. If the current tab isn't helpful, show a dialog.
*/
[GtkCallback]
private void create_session () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).tmux_window.stream.create_window ();
} else if (application is Application) {
var streams = ((Application) application).streams;
if (streams.size == 1) {
foreach (var stream in streams) {
stream.create_window ();
}
} else if (streams.size > 1) {
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.OK, "Multiple TMux instances are currently connected.");
dialog.run ();
dialog.destroy ();
} else {
add_stream ();
}
}
}
/**
* Remove the current window.
*/
[GtkCallback]
private void destroy_window () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var window = ((!)widget).tmux_window;
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.YES_NO, "Kill TMux window “%s” and terminate running process?", window.title);
if (dialog.run () == Gtk.ResponseType.YES) {
window.destroy ();
}
dialog.destroy ();
}
}
/**
* Kill the remote TMux server for the current window.
*/
[GtkCallback]
private void destroy_server () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var stream = ((!)widget).tmux_window.stream;
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.YES_NO, "Kill TMux server running on %s and terminate all running sessions and their windows and processes?", stream.name);
if (dialog.run () == Gtk.ResponseType.YES) {
stream.kill ();
}
dialog.destroy ();
}
}
/**
* Kill the remote TMux session for the current window.
*/
[GtkCallback]
private void destroy_session () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var stream = ((!)widget).tmux_window.stream;
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.YES_NO, "Kill TMux server running session “%s” on %s and terminate all running windows and processes?", stream.session_name, stream.name);
if (dialog.run () == Gtk.ResponseType.YES) {
stream.destroy ();
}
dialog.destroy ();
}
}
/**
* Rename the remote TMux session for the current window.
*/
[GtkCallback]
private void rename_session () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var stream = ((!)widget).tmux_window.stream;
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, "Rename session on “%s”:", stream.name);
var entry = new Gtk.Entry ();
entry.text = stream.session_name;
entry.activates_default = true;
dialog.get_content_area ().pack_end (entry);
entry.show ();
dialog.set_default_response (Gtk.ResponseType.OK);
if (dialog.run () == Gtk.ResponseType.OK) {
var name = strip (entry.text);
if (name.length == 0 || ":" in name) {
var error_dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Invalid session name.");
error_dialog.run ();
error_dialog.destroy ();
} else {
stream.rename (name);
}
}
dialog.destroy ();
}
}
/**
* Rename the remote TMux window.
*/
[GtkCallback]
private void rename_window () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var tmux_window = ((!)widget).tmux_window;
var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, "Rename window on “%s”:", tmux_window.title);
var entry = new Gtk.Entry ();
entry.text = tmux_window.title;
entry.activates_default = true;
dialog.get_content_area ().pack_end (entry);
entry.show ();
dialog.set_default_response (Gtk.ResponseType.OK);
if (dialog.run () == Gtk.ResponseType.OK) {
var name = strip (entry.text);
if (name.length == 0) {
var error_dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Invalid window name.");
error_dialog.run ();
error_dialog.destroy ();
} else {
tmux_window.rename (name);
}
}
dialog.destroy ();
}
}
[GtkCallback]
private void force_size_update () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).tmux_window.pull_size ();
}
}
[GtkCallback]
private void resize_tmux () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).resize_tmux ();
}
}
[GtkCallback]
private void zoom_in () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).adjust_font (true);
}
}
[GtkCallback]
private void zoom_out () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).adjust_font (false);
}
}
[GtkCallback]
private void zoom_normal () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
var font_desc = Pango.FontDescription.from_string (settings.get_string ("font"));
((!)widget).tmux_window.stream.change_font (font_desc);
}
}
[GtkCallback]
private void on_about () {
Gtk.show_about_dialog (this,
"program-name", "TabbedMux",
"version", VERSION,
"logo_icon_name", "utilities-terminal",
"copyright", "Copyright 2013-2014 Andre Masella",
"authors", new string[] { "Andre Masella" },
"website", "https://github.com/apmasell/tabbedmux",
"website-label", "GitHub Repository"
);
}
[GtkCallback]
private void on_copy () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).terminal.copy_clipboard ();
}
}
[GtkCallback]
private void copy_to_tmux () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).tmux_window.stream.set_buffer (clipboard.wait_for_text ());
}
}
[GtkCallback]
private void on_paste () {
paste_clipboard (clipboard);
}
[GtkCallback]
private void on_paste_selection () {
paste_clipboard (clipboard_selection);
}
private void paste_clipboard (Gtk.Clipboard clipboard) {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
clipboard.request_text (((!)widget).paste_text);
}
}
[GtkCallback]
private void paste_tmux () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).tmux_window.paste_buffer ();
}
}
[GtkCallback]
private void on_quit () {
destroy ();
}
private void on_tmux_window_closed (TMuxWindow tmux_window) {
for (var it = 0; it < notebook.get_n_pages (); it++) {
var terminal = notebook.get_nth_page (it) as Terminal;
if (terminal != null && ((!)terminal).tmux_window == tmux_window) {
notebook.remove_page (it);
return;
}
}
}
[GtkCallback]
private void page_removed () {
if (notebook.get_n_pages () == 0) {
close ();
}
}
[GtkCallback]
private void page_switched (Gtk.Widget widget, uint index) {
var terminal = widget as Terminal;
if (terminal != null) {
message ("Switched terminal.");
/* If we've switched to a terminal that doesn't know about the size of the window, force it to resize. */
((!)terminal).resize_tmux ();
copy_item.sensitive = ((!)terminal).terminal.get_has_selection ();
var stream = ((!)terminal).tmux_window.stream;
for (var it = 0; it < notebook.get_n_pages (); it++) {
var other_terminal = notebook.get_nth_page (it) as Terminal;
if (other_terminal != null) {
((!)other_terminal).sibling_selected (((!)other_terminal).tmux_window.stream == stream);
}
}
} else {
message ("Non-terminal found in window.");
}
}
/**
* Force redrawing the terminal on the remote end.
*/
[GtkCallback]
private void refresh_tab () {
var widget = notebook.get_nth_page (notebook.page) as Terminal;
if (widget != null) {
((!)widget).tmux_window.refresh ();
}
}
/**
* When the container changes, resize the selected tab and mark that all the others are “the wrong size”.
*/
public override bool configure_event (Gdk.EventConfigure event) {
var result = base.configure_event (event);
if (configure_id != 0) {
GLib.Source.remove (configure_id);
}
configure_id = Timeout.add (100, update_window_configuration);
return result;
}
private bool update_window_configuration () {
configure_id = 0;
for (var it = 0; it < notebook.get_n_pages (); it++) {
var terminal = notebook.get_nth_page (it) as Terminal;
if (terminal != null) {
((!)terminal).resize_tmux ();
}
}
if (Gdk.WindowState.MAXIMIZED in get_window ().get_state ()) {
return false;
}
int width;
int height;
get_size (out width, out height);
settings.set ("size", "(ii)", width, height);
int x;
int y;
get_position (out x, out y);
settings.set ("position", "(ii)", x, y);
return false;
}
public abstract class SessionItem : Gtk.MenuItem {
protected async abstract TMuxStream? open () throws Error;
public abstract bool matches (TMuxStream stream);
public override void activate () {
var window = (Gtk.Window)get_ancestor (typeof (Gtk.Window));
while (window.attached_to != null) {
window = (Gtk.Window)window.attached_to.get_ancestor (typeof (Gtk.Window));
}
open.begin ((sender, result) => {
try {
var stream = open.end (result);
if (stream == null) {
show_error (window, "Could not connect.");
} else {
var application = window.application as Application;
if (application != null) {
((!)application).add_stream ((!)stream);
}
}
} catch (IOError.CANCELLED e) {
} catch (Error e) {
show_error (window, e.message);
}
});
}
}
private class LocalSessionItem : SessionItem {
private string session;
private string binary;
internal LocalSessionItem (string session, string binary) {
this.session = session;
this.binary = binary;
label = "%s (Local: %s)".printf (session, binary);
}
public override bool matches (TMuxStream stream) {
return stream is TMuxLocalStream && stream.session_name == session && stream.binary == binary;
}
protected async override TMuxStream? open () throws Error {
return TMuxLocalStream.open (session, binary);
}
}
private class SshSessionItem : SessionItem {
private string session;
private string host;
private uint16 port;
private string username;
private string binary;
internal SshSessionItem (string session, string host, uint16 port, string username, string binary) {
this.session = session;
this.host = host;
this.port = port;
this.username = username;
this.binary = binary;
label = port == 22 ? "%s@%s - %s - %s".printf (username, host, binary, session) : "%s@%s:%hu - %s - %s".printf (username, host, port, binary, session);
}
public override bool matches (TMuxStream stream) {
if (stream is TMuxSshStream && stream.session_name == session) {
var ssh_stream = (TMuxSshStream) stream;
return ssh_stream.host == host && ssh_stream.port == port && ssh_stream.username == username && ssh_stream.binary == binary;
}
return false;
}
protected async override TMuxStream? open () throws Error {
var keybd_dialog = new KeyboardInteractiveDialog ((Gtk.Window)get_toplevel (), host);
var busy_dialog = new BusyDialog ((Gtk.Window)get_toplevel ());
busy_dialog.show ();
try {
var stream = yield TMuxSshStream.open (session, host, port, username, binary, keybd_dialog.respond, busy_dialog);
busy_dialog.destroy ();
return stream;
} finally {
busy_dialog.destroy ();
}
}
}
public abstract class RemoveSessionItem : Gtk.MenuItem {
protected abstract void remove_session ();
public override void activate () {
var window = (Gtk.Window)get_toplevel ();
while (window.attached_to != null) {
window = (Gtk.Window)window.attached_to.get_toplevel ();
}
var dialog = new Gtk.MessageDialog (window, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.YES_NO, "Remove session “%s”?", label);
if (dialog.run () == Gtk.ResponseType.YES) {
remove_session ();
}
dialog.destroy ();
}
}
private class RemoveLocalSessionItem : RemoveSessionItem {
private SavedSessions saved;
private string session;
private string binary;
internal RemoveLocalSessionItem (SavedSessions saved, string session, string binary) {
this.saved = saved;
this.session = session;
this.binary = binary;
label = "%s (Local: %s)".printf (session, binary);
}
protected override void remove_session () {
saved.remove_local (session, binary);
}
} private class RemoveSshSessionItem : RemoveSessionItem {
private SavedSessions saved;
private string session;
private string host;
private uint16 port;
private string username;
private string binary;
internal RemoveSshSessionItem (SavedSessions saved, string session, string host, uint16 port, string username, string binary) {
this.saved = saved;
this.session = session;
this.host = host;
this.port = port;
this.username = username;
this.binary = binary;
label = port == 22 ? "%s@%s - %s - %s".printf (username, host, binary, session) : "%s@%s:%hu - %s - %s".printf (username, host, port, binary, session);
}
protected override void remove_session () {
saved.remove_ssh (session, host, port, username, binary);
}
}
}