Now About Social Code
aboutsummaryrefslogtreecommitdiff
path: root/src/preferences.py
blob: 8e6d150e6c8b33eceee32f979ac62fd07c4041f5 (plain)
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
# 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 <https://www.gnu.org/licenses/>.
#
# 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'

    autoconnect = Gtk.Template.Child()
    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.autoconnect.set_active(config.str_to_bool(self.config["relay"]["autoconnect"]))
        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"]["autoconnect"] = str(self.autoconnect.get_active())
        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)