# window.py # # Copyright 2024 Lucas Fryzek # # 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 . # # SPDX-License-Identifier: GPL-3.0-or-later from gi.repository import Adw from gi.repository import Gtk from gi.repository import GLib from weegtk import config import os @Gtk.Template(resource_path='/com/fryzekconcepts/weegtk/gtk/preferences.ui') class WeegtkPreferences(Adw.PreferencesDialog): __gtype_name__ = 'WeegtkPreferences' hostname = Gtk.Template.Child() port = Gtk.Template.Child() password = Gtk.Template.Child() ssh_host = Gtk.Template.Child() ssh_port = Gtk.Template.Child() ssh_username = Gtk.Template.Child() ssh_key_label = Gtk.Template.Child() ssh_key_path = "" upload_url = Gtk.Template.Child() def __init__(self, **kwargs): super().__init__(**kwargs) self.config = config.read() self.hostname.set_text(self.config["relay"]["hostname"]) self.port.set_text(self.config["relay"]["port"]) self.password.set_text(self.config["relay"]["password"]) self.ssh_host.set_text(self.config["ssh"]["host"]) self.ssh_port.set_text(self.config["ssh"]["port"]) self.ssh_username.set_text(self.config["ssh"]["username"]) self.set_ssh_key(self.config["ssh"]["key"]) self.upload_url.set_text(self.config["upload"]["url"]) self.install_action("ssh_key.open", None, self.ssh_key_open) def read_preferences(self): self.config["relay"]["hostname"] = self.hostname.get_text() self.config["relay"]["port"] = self.port.get_text() self.config["relay"]["password"] = self.password.get_text() self.config["ssh"]["host"] = self.ssh_host.get_text() self.config["ssh"]["port"] = self.ssh_port.get_text() self.config["ssh"]["username"] = self.ssh_username.get_text() self.config["ssh"]["key"] = self.ssh_key_path self.config["upload"]["url"] = self.upload_url.get_text() def set_ssh_key(self, key_path): self.ssh_key_path = key_path short_name = os.path.basename(key_path) self.ssh_key_label.set_label(short_name) def ssh_key_open_dialog(self, dialog, result, caller): try: file = dialog.open_finish(result) except GLib.GError: # gtk-dialog-error-quark: Dismissed by user pass else: self.set_ssh_key(file.get_path()) def ssh_key_open(self, *args): dialog = Gtk.FileDialog() dialog.set_title("Select SSH private key") dialog.open(self.get_root(), None, self.ssh_key_open_dialog, self) @Gtk.Template.Callback() def preferences_closed(self, *args): self.read_preferences() config.write(self.config)