|
| 1 | +from os import system |
| 2 | + |
| 3 | +from gi import require_version |
| 4 | + |
| 5 | +require_version('Gdk', '3.0') |
| 6 | +require_version('Gtk', '3.0') |
| 7 | + |
| 8 | +from gi.repository import Gtk |
| 9 | + |
| 10 | +import Application |
| 11 | +from ssh_manager.Manager import Manager |
| 12 | +from ssh_manager.config.Window import Window |
| 13 | + |
| 14 | + |
| 15 | +class ConnectionListWindow: |
| 16 | + VERTICAL_MARGIN = 5 |
| 17 | + HORIZONTAL_MARGIN = 2 |
| 18 | + _listbox = None |
| 19 | + _frame = None |
| 20 | + _scrollable = None |
| 21 | + _list_store = Gtk.ListStore(str) |
| 22 | + |
| 23 | + def __init__(self, manager: Manager, window_config: Window): |
| 24 | + self._manager = manager |
| 25 | + self._window_config = window_config |
| 26 | + |
| 27 | + def show(self): |
| 28 | + win = Gtk.Window() |
| 29 | + win.set_size_request(self._window_config.width, self._window_config.height) |
| 30 | + win.move(self._window_config.x, self._window_config.y) |
| 31 | + win.set_resizable(False) |
| 32 | + |
| 33 | + btn_setting = Gtk.Button("Settings") |
| 34 | + btn_setting.set_margin_right(self.HORIZONTAL_MARGIN) |
| 35 | + btn_reload = Gtk.Button("Reload") |
| 36 | + |
| 37 | + btn_box = Gtk.HBox() |
| 38 | + btn_box.add(btn_setting) |
| 39 | + btn_box.add(btn_reload) |
| 40 | + |
| 41 | + self._listbox = Gtk.TreeView(self._list_store) |
| 42 | + self._listbox.set_headers_visible(False) |
| 43 | + self._listbox.connect("row-activated", self.row_activated) |
| 44 | + |
| 45 | + self._scrollable = Gtk.ScrolledWindow() |
| 46 | + self._scrollable.add(self._listbox) |
| 47 | + self._fill_listbox() |
| 48 | + |
| 49 | + self._frame = Gtk.Frame() |
| 50 | + self._frame.add(self._scrollable) |
| 51 | + self._frame.set_border_width(1) |
| 52 | + self._frame.set_margin_bottom(self.VERTICAL_MARGIN) |
| 53 | + |
| 54 | + vbox = Gtk.VBox() |
| 55 | + vbox.pack_start(self._frame, 1, 1, 1) |
| 56 | + vbox.pack_end(btn_box, 0, 0, 0) |
| 57 | + |
| 58 | + vbox.set_margin_top(self.VERTICAL_MARGIN) |
| 59 | + vbox.set_margin_bottom(self.VERTICAL_MARGIN) |
| 60 | + vbox.set_margin_left(self.VERTICAL_MARGIN) |
| 61 | + vbox.set_margin_right(self.VERTICAL_MARGIN) |
| 62 | + |
| 63 | + btn_setting.connect("clicked", self.btn_settings_click) |
| 64 | + btn_reload.connect("clicked", self.btn_reload_click) |
| 65 | + |
| 66 | + win.add(vbox) |
| 67 | + |
| 68 | + win.set_title("SshManager GTK") |
| 69 | + win.connect("delete-event", Gtk.main_quit) |
| 70 | + win.show_all() |
| 71 | + Gtk.main() |
| 72 | + |
| 73 | + # noinspection PyUnusedLocal |
| 74 | + @staticmethod |
| 75 | + def btn_settings_click(target): |
| 76 | + system("xdg-open " + Application.Application.get_config_file_path()) |
| 77 | + |
| 78 | + # noinspection PyUnusedLocal |
| 79 | + def btn_reload_click(self, target): |
| 80 | + self._manager = Application.Application.create_default_manager() |
| 81 | + self._fill_listbox() |
| 82 | + |
| 83 | + def _fill_listbox(self): |
| 84 | + self._list_store.clear() |
| 85 | + connections = self._manager.get_connections() |
| 86 | + for connection in connections: |
| 87 | + self._list_store.append([connection.label]) |
| 88 | + |
| 89 | + renderer = Gtk.CellRendererText() |
| 90 | + column = self._listbox.get_column(0) |
| 91 | + if not column: |
| 92 | + column = Gtk.TreeViewColumn(None, renderer, text=0) |
| 93 | + self._listbox.append_column(column) |
| 94 | + self._listbox.show_all() |
| 95 | + |
| 96 | + # noinspection PyUnusedLocal |
| 97 | + def row_activated(self, target: Gtk.TreeView, path: Gtk.TreePath, column: Gtk.TreeViewColumn): |
| 98 | + i = path.get_indices()[0] |
| 99 | + connection = self._manager.get_connection(i) |
| 100 | + system("gnome-terminal -x ssh {} -p {}".format(connection.host, connection.port)) |
0 commit comments