SyncTk/classes/app.py

166 lines
7.0 KiB
Python
Raw Normal View History

2023-01-22 01:22:08 +02:00
import requests
import sv_ttk
from os import path
from tkinter import ttk, messagebox, N, S, W, E, NW
from ttkthemes import ThemedTk
from modules.dark_titlebar import dark_title_bar
from modules.utils import configGet, use_dark_mode
from classes.window_config import WindowConfig
from classes.frames import FrameDevices, FrameSaves, FrameSettings
class App(ThemedTk):
def __init__(self):
super().__init__()
self.window_width = 650
self.window_height = 400
self.screen_width = self.winfo_screenwidth()
self.screen_height = self.winfo_screenheight()
self.center_x = int(self.screen_width/2 - self.window_width / 2)
self.center_y = int(self.screen_height/2 - self.window_height / 2)
self.title("Stardew Sync")
self.geometry(f'{self.window_width}x{self.window_height}+{self.center_x}+{self.center_y}')
self.iconbitmap(path.join("assets", "favicon.ico"))
self.focus_set()
sv_ttk.init_theme(self)
if use_dark_mode():
self.configure(background="#1c1c1c")
dark_title_bar(self, mode="dark")
sv_ttk.set_theme("dark")
else:
sv_ttk.set_theme("light")
self.draw_main()
def try_connecting(self):
try:
requests.get(configGet("address")+"/apikey", headers={"apikey": configGet("apikey")})
# messagebox.showinfo(title="Connection succeeded", message="Server is reachable, apikey is valid, so your client is now ready to be used!")
self.destroy_everything()
except:
messagebox.showerror(title="Configuration error", message="Your client configuration is incorrect. Set it up and try again.")
self.destroy_everything()
def verify_connecting(self):
try:
if requests.get(configGet("address")+"/check").status_code == 200:
return
else:
raise requests.exceptions.HTTPError()
except:
self.verify_answer = messagebox.askyesno(title="Connection failed", message="You might have no connection to the server or your api key could expire. Reconfigure the client?\n\nYes to reconfigure and No to retry connecting")
if self.verify_answer is True:
window = WindowConfig(self)
window.grab_set()
if hasattr(self, "setup_button"):
self.setup_button.destroy()
if hasattr(self, "setup_label"):
self.setup_label.destroy()
ttk.Label(self, text="Client configured, try to connect").pack(expand=True)
ttk.Button(self, text="Connect", style="Accent.TButton", width=10, command=self.try_connecting).pack(expand=True)
return False
else:
self.destroy_everything()
return True
def verify_authorization(self):
try:
if requests.get(configGet("address")+"/apikey", headers={"apikey": configGet("apikey")}).status_code == 200:
return
else:
self.verify_answer = messagebox.askyesno(title="Authorization failed", message="Your apikey could have expired or is invalid. Reconfigure the client?\n\nYes to reconfigure and No to retry connecting")
if self.verify_answer is True:
window = WindowConfig(self)
window.grab_set()
if hasattr(self, "setup_button"):
self.setup_button.destroy()
if hasattr(self, "setup_label"):
self.setup_label.destroy()
ttk.Label(self, text="Client configured, try to connect").pack(expand=True)
ttk.Button(self, text="Connect", style="Accent.TButton", width=10, command=self.try_connecting).pack(expand=True)
return False
else:
self.destroy_everything()
return True
except:
self.verify_answer = messagebox.askyesno(title="Connection failed", message="You might have no connection to the server or your api key could expire. Reconfigure the client?\n\nYes to reconfigure and No to retry connecting")
if self.verify_answer is True:
window = WindowConfig(self)
window.grab_set()
ttk.Label(self, text="Client configured, try to connect").pack(expand=True)
ttk.Button(self, text="Connect", style="Accent.TButton", width=10, command=self.try_connecting).pack(expand=True)
else:
self.destroy_everything()
def frame_saves(self):
self.frame_saves_object = FrameSaves(self)
self.frame_saves_object.grid(column=1, row=0, sticky=N+S+W+E)
return
def frame_devices(self):
self.frame_devices_object = FrameDevices(self)
self.frame_devices_object.grid(column=1, row=0, sticky=N+S+W+E)
return
def frame_settings(self):
self.frame_settings_object = FrameSettings(self)
self.frame_settings_object.grid(column=1, row=0, sticky=N+S+W+E)
return
def draw_main(self):
if configGet("address") is None or configGet("apikey") is None:
self.setup_label = ttk.Label(self, text="Your client seems to be unconfigured")
self.setup_label.pack(expand=True)
self.setup_button = ttk.Button(self, text="Set up", style="Accent.TButton", width=10, command=self.open_window_config)
self.setup_button.pack(expand=True)
if self.verify_connecting() is False:
return
if self.verify_authorization() is False:
return
self.frame_sidebar = ttk.Frame(self)
self.frame_sidebar.grid(column=0, row=0, sticky=NW)
self.item_saves = ttk.Button(self.frame_sidebar, text="Saves", width=10, command=self.frame_saves)
self.item_saves.grid(column=0, row=0, sticky=W, padx=9, pady=9)
self.item_devices = ttk.Button(self.frame_sidebar, text="Devices", width=10, command=self.frame_devices)
self.item_devices.grid(column=0, row=1, sticky=W, padx=9, pady=3)
self.item_settings = ttk.Button(self.frame_sidebar, text="Settings", width=10, command=self.frame_settings)
self.item_settings.grid(column=0, row=2, sticky=W+S, padx=9, pady=9)
self.item_test = ttk.Button(self.frame_sidebar, text="Test", width=10, command=lambda:sv_ttk.set_theme("light"))
self.item_test.grid(column=0, row=3, sticky=W+S, padx=9, pady=9)
self.frame_saves()
def destroy_everything(self):
for widget in self.winfo_children():
widget.destroy()
self.draw_main()
def open_window_config(self):
window = WindowConfig(self)
window.grab_set()
self.setup_button.destroy()
self.setup_label.destroy()
ttk.Label(self, text="Client configured, try to connect").pack(expand=True)
ttk.Button(self, text="Connect", style="Accent.TButton", width=10, command=self.try_connecting).pack(expand=True)