SyncTk/classes/app.py

155 lines
5.9 KiB
Python
Raw Normal View History

2023-01-22 01:35:32 +02:00
from os import path
from tkinter import NW, E, N, S, W, messagebox, ttk
2023-01-22 01:22:08 +02:00
import requests
import sv_ttk
from ttkthemes import ThemedTk
2023-01-22 18:53:55 +02:00
from classes.custom.themed_frame import ThemedFrame
2023-01-22 01:35:32 +02:00
2023-01-22 18:53:55 +02:00
from classes.frames.devices import FrameDevices
from classes.frames.saves import FrameSaves
from classes.frames.settings import FrameSettings
from classes.frames.errors import FrameErrorConnection, FrameErrorSavesFolder, FrameErrorUnconfigured
2023-01-22 21:06:55 +02:00
from classes.toplevel.welcome import ToplevelWelcome
2023-01-22 01:35:32 +02:00
from modules.theme_titlebar import theme_title_bar
2023-01-22 18:53:55 +02:00
from modules.utils import configGet, get_string_mode, use_dark_mode
2023-01-22 01:35:32 +02:00
2023-01-22 01:22:08 +02:00
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}')
2023-01-22 18:53:55 +02:00
self.resizable(False, True)
2023-01-22 01:22:08 +02:00
sv_ttk.init_theme(self)
if use_dark_mode():
2023-01-22 01:35:32 +02:00
theme_title_bar(self, mode="dark")
2023-01-22 18:53:55 +02:00
self.update()
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
self.iconbitmap(path.join("assets", "favicon.ico"))
self.focus_set()
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
self.draw_main()
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
def verify_authorization(self):
2023-01-22 01:22:08 +02:00
try:
2023-01-22 18:53:55 +02:00
if requests.get(configGet("address")+"/apikey", headers={"apikey": configGet("apikey")}, verify=not configGet("allow_self_signed")).status_code == 200:
return True
2023-01-22 01:22:08 +02:00
else:
return False
2023-01-22 18:53:55 +02:00
except:
return False
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
def verify_saves_dir(self):
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
if configGet("saves_location") is None or path.exists(configGet("saves_location")) is False:
return False
else:
return True
2023-01-22 01:22:08 +02:00
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):
2023-01-22 18:53:55 +02:00
sv_ttk.set_theme(get_string_mode())
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
# self.grid_rowconfigure(1, weight=2)
# self.grid_rowconfigure(0, weight=2)
# self.grid_columnconfigure(1, weight=1)
# self.setup_label = ttk.Label(self, text="Your client seems to be unconfigured")
# self.setup_label.grid(column=1, row=0, padx=9, pady=9, sticky=S)
# self.setup_button = ttk.Button(self, text="Set up", style="Accent.TButton", width=10, command=self.open_window_config)
# self.setup_button.grid(column=1, row=1, padx=9, pady=9, sticky=N)
# return
2023-01-22 21:06:55 +02:00
if configGet("first_run") is True:
self.welcome_window = ToplevelWelcome(self)
self.welcome_window.grab_set()
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
self.frame_sidebar = ThemedFrame(self)
self.frame_sidebar.grid(column=0, row=0, columnspan=1, sticky=N+S+W+E)
2023-01-22 01:22:08 +02:00
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)
2023-01-22 18:53:55 +02:00
if configGet("address") in ["", None] or configGet("apikey") in ["", None]:
self.item_saves.state(["disabled"])
self.item_devices.state(["disabled"])
self.connection_error = FrameErrorUnconfigured(self)
self.connection_error.grid(column=1, row=0, rowspan=2, sticky=N+S+W+E)
# messagebox.showerror(title="Configuration error", message="Your client is not properly configured.")
return
if self.verify_authorization() is False:
self.item_saves.state(["disabled"])
self.item_devices.state(["disabled"])
self.connection_error = FrameErrorConnection(self)
self.connection_error.grid(column=1, row=0, rowspan=2, sticky=N+S+W+E)
messagebox.showerror(title="Authentication error", message="Your API key seems to be invalid.")
return
2023-01-22 01:22:08 +02:00
2023-01-22 18:53:55 +02:00
if self.verify_saves_dir() is False:
self.item_saves.state(["disabled"])
self.connection_error = FrameErrorSavesFolder(self)
self.connection_error.grid(column=1, row=0, rowspan=2, sticky=N+S+W+E)
messagebox.showerror(title="Configuration error", message="Saves folder seems to be invalid.")
return
2023-01-22 01:22:08 +02:00
self.frame_saves()
2023-01-22 18:53:55 +02:00
def destroy_everything(self, draw=True):
2023-01-22 01:22:08 +02:00
for widget in self.winfo_children():
widget.destroy()
2023-01-22 18:53:55 +02:00
if draw is True:
self.draw_main()
# def open_window_config(self):
# window = WindowConfig(self)
# window.grab_set()
# self.setup_button.destroy()
# self.setup_label.destroy()
# self.grid_rowconfigure(1, weight=2)
# self.grid_rowconfigure(0, weight=2)
# self.grid_columnconfigure(1, weight=1)
# ttk.Label(self, text="Client configured, try to connect").grid(column=1, row=0, padx=9, pady=9, sticky=S)
# ttk.Button(self, text="Connect", style="Accent.TButton", width=10, command=self.try_connecting).grid(column=1, row=1, padx=9, pady=9, sticky=N)