Files

81 lines
2.4 KiB
Python

import json
import tkinter as tk
from app.http import request_builder
from app.views.login import LoginView
from app.views.initial import InitialView
from app.views.signup import SignupView
from app.views.main_page import MainPageView
from app.encryption import AESEncryptor
import json
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.title("Messenger App")
screen_height = self.winfo_screenheight()
self.geometry(f"{screen_height // 2}x{screen_height // 2}")
icon_path = "/home/andrei/Documents/messenger/messenger_gui/resources/message-icon-png-17.png"
self.iconphoto(True, tk.PhotoImage(file=icon_path))
self.current_logged_user = {}
self.define_http_params()
self.encryptor = self.initialize_encryptor()
self.current_view = None
self.change_to_initial_view()
def define_http_params(self):
self.url = "http://localhost:8000/encryption/get_private_key"
self.headers = {
"Content-Type": "application/json",
"Authorization_Key": "messenger"
}
self.method = "GET"
self.params = None
def change_to_initial_view(self):
self.change_view(InitialView(self))
def change_to_sing_in_view(self):
self.change_view(SignupView(self))
def change_to_login_view(self):
self.change_view(LoginView(self))
def change_to_main_page_view(self):
self.change_view(MainPageView(self))
def change_to_profile_view(self):
pass
def change_view(self, view: tk.Frame):
if self.current_view:
self.current_view.destroy()
self.current_view = view
self.current_view.pack(expand=True, fill=tk.BOTH)
self.current_view.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
def get_user_information(self, credentials):
data = json.loads(credentials)
self.current_logged_user['id'] = data['info']['id']
self.current_logged_user['username'] = data['info']['username']
self.current_logged_user['email'] = data['info']['email']
def initialize_encryptor(self):
request_builder.set_url(self.url)
request_builder.set_headers(self.headers)
request_builder.set_method(self.method)
result = request_builder.build().make_request()
json_data = json.loads(result.text)
private_key = json_data.get("private_key", None)
return AESEncryptor(private_key)