summaryrefslogtreecommitdiffstats
path: root/Widgets/settings.py
blob: 11800a29d15545124e3ad23f6362cac3f65ae85e (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#   Copyright 2023 Suchinton Chakravarty
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import extras.Kuksa_Instance as kuksa_instance
import os
import sys
import time
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QLineEdit, QPushButton, QLabel, QCheckBox

current_dir = os.path.dirname(os.path.abspath(__file__))

# ========================================

sys.path.append(os.path.dirname(current_dir))


Form, Base = uic.loadUiType(os.path.join(
    current_dir, "../ui/Settings_Window.ui"))

# ========================================


class settings(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)

        self.SSLToggle = self.findChild(QCheckBox, "SSLToggle")

        self.connectionStatus = self.findChild(QLabel, "connectionStatus")
        self.connectionLogo = self.findChild(QLabel, "connectionLogo")

        self.IPAddrInput = self.findChild(QLineEdit, "IPAddrInput")
        self.tokenPathInput = self.findChild(QLineEdit, "tokenPathInput")

        self.reconnectBtn = self.findChild(QPushButton, "reconnectBtn")
        self.refreshBtn = self.findChild(QPushButton, "refreshBtn")
        self.startClientBtn = self.findChild(QPushButton, "startClientBtn")

        self.startClientBtn.clicked.connect(self.set_instance)
        self.reconnectBtn.clicked.connect(self.reconnectClient)
        self.refreshBtn.clicked.connect(self.refreshStatus)

        self.refreshStatus()
        self.show()

    def set_instance(self):
        self.kuksa = kuksa_instance.KuksaClientSingleton.get_instance()
        self.client = self.kuksa.get_client()

        self.config = self.kuksa.get_config()
        self.token = self.kuksa.get_token()

        self.IPAddrInput.setText(self.config["ip"])
        self.SSLToggle.setChecked(self.config["insecure"])
        self.tokenPathInput.setText(self.token)

        time.sleep(2)

        if (self.client is None):
            self.connectionStatus.setText('Not Connected')
            self.connectionLogo.setStyleSheet("background-color: red")

        self.refreshStatus()

    def refreshStatus(self):
        try:
            if (self.client is None):
                self.connectionStatus.setText('Not Connected')
                self.connectionLogo.setStyleSheet("background-color: red")
                return None

            if (self.client.checkConnection() == True):
                self.connectionStatus.setText('Connected')
                self.connectionLogo.setPixmap(":/icons/feather/check-circle.svg")
                self.connectionLogo.setStyleSheet("background-color: green")
                self.client.start()
                return True

            if (self.client.checkConnection() == False):
                self.client.stop()
                self.connectionStatus.setText('Disconnected')
                self.connectionLogo.setStyleSheet("background-color: yellow")
                return False
        except:
            pass

    def reconnectClient(self):
        try:
            self.config["ip"] = self.IPAddrInput.text()
            self.config["insecure"] = self.SSLToggle.isChecked()
            self.token = self.tokenPathInput.text()
            self.client = self.kuksa.reconnect_client(self.config, self.token)
            self.client.start()
            self.refreshStatus()
        except Exception as e:
            print(e)

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = settings()
    w.show()
    sys.exit(app.exec_())