11import os
2+ import json
3+
24from sys import platform
5+
36import src .back .util .print as print
47
58appID = "dev.pages.codedave.SendYourFiles"
6- configPath = ""
9+
710
811class Sys :
912 def getOSpath (self ):
@@ -12,22 +15,129 @@ def getOSpath(self):
1215 elif platform == "linux" :
1316 configPath = os .path .join (os .getenv ("XDG_CONFIG_HOME" ), appID )
1417 elif platform == "darwin" :
15- configPath = os .path .join (os .getenv ("HOME" ), "Library" , "Application Support" , appID )
18+ configPath = os .path .join (
19+ os .getenv ("HOME" ), "Library" , "Application Support" , appID
20+ )
1621 else :
1722 configPath = os .path .join (os .getenv ("HOME" ), ".config" , appID )
1823 print .debug (f"Config path: { configPath } " )
1924 return configPath
2025
26+
2127class Settings :
28+ def __init__ (self ):
29+ self .configPath = os .path .join (Sys .getOSpath (self ))
30+
31+ self .settingsPath = os .path .join (self .configPath , "settings.json" )
32+ self .defaultSettingsPath = os .path .join (os .path .dirname (__file__ ), "extra" , "settings.json" )
33+
34+ if not os .path .exists (self .defaultSettingsPath ):
35+ # Fallback for running from root
36+ self .defaultSettingsPath = os .path .abspath (os .path .join ("src" , "back" , "extra" , "settings.json" ))
37+
38+ self .defaultSettings = {}
39+ self .settings = {}
40+
41+ # passing through
42+ self .readSet = self .ReadSettings (self .settingsPath )
43+
2244 def checkFolder (self ):
23- configPath = Sys .getOSpath (self )
24- if not os .path .exists (configPath ):
45+ if os .path .exists (self .configPath ):
46+ print .success (f"Config path found at: { self .configPath } " )
47+ return True
48+ else :
49+ print .warning (f"Config path not found at: { self .configPath } " )
50+ print .debug (f"Creating config path at: { self .configPath } " )
51+
2552 try :
26- os .makedirs (configPath )
27- print .success (f"Created config path: { configPath } " )
53+ os .makedirs (self .configPath )
54+ print .success (f"Config path created at: { self .configPath } " )
55+ return True
2856 except Exception as e :
2957 print .error (f"Error creating config path: { e } " )
58+ return False
3059 except OSError as e :
31- print .fatal (f"Fatal error creating config path: { e } " )
60+ print .fatal (f"Fatal OS error creating config path: { e } " )
61+ return False
62+
63+ def checkFile (self ):
64+ if os .path .exists (self .settingsPath ):
65+ print .success (f"Settings found at: { self .settingsPath } " )
66+ return True
3267 else :
33- print .debug (f"Config path already exists: { configPath } " )
68+ print .warning (f"Settings not found at: { self .settingsPath } " )
69+ print .debug (f"Creating settings file at: { self .settingsPath } " )
70+
71+ try :
72+ with open (self .settingsPath , "w" ) as f :
73+ with open (self .defaultSettingsPath , "r" ) as f2 :
74+ data = json .load (f2 )
75+ newSettings = {}
76+ for key , value in data ["settings" ].items ():
77+ val = value ["default" ]
78+ if value .get ("type" ) == "boolean" :
79+ if str (val ).lower () == "true" :
80+ val = True
81+ elif str (val ).lower () == "false" :
82+ val = False
83+ newSettings [key ] = val
84+ json .dump (newSettings , f , indent = 4 )
85+ print .success (f"Settings created at: { self .settingsPath } " )
86+ return True
87+ except Exception as e :
88+ print .error (f"Error creating settings: { e } " )
89+ return False
90+ except OSError as e :
91+ print .fatal (f"Fatal OS error creating settings: { e } " )
92+ return False
93+
94+ def updateSetting (self , key , value ):
95+ try :
96+ data = {}
97+ if os .path .exists (self .settingsPath ):
98+ with open (self .settingsPath , "r" ) as f :
99+ data = json .load (f )
100+
101+ data [key ] = value
102+
103+ with open (self .settingsPath , "w" ) as f :
104+ json .dump (data , f , indent = 4 )
105+ return True
106+ except Exception as e :
107+ print .error (f"Error updating setting: { e } " )
108+ return False
109+
110+ class ReadSettings :
111+ def __init__ (self , path ):
112+ self .settingPath = path
113+
114+ def _checkForUpdates (self ):
115+ try :
116+ with open (self .settingPath , "r" ) as f :
117+ return json .load (f ).get ("checkForUpdates" , True )
118+ except Exception :
119+ pass
120+
121+ def _history (self ):
122+ try :
123+ with open (self .settingPath , "r" ) as f :
124+ return json .load (f ).get ("history" , True )
125+ except Exception :
126+ pass
127+ return True
128+
129+ def _minimizeToTray (self ):
130+ try :
131+ with open (self .settingPath , "r" ) as f :
132+ return json .load (f ).get ("minimizeToTray" , True )
133+ except Exception :
134+ pass
135+ return True
136+
137+ def _notifyYou (self ):
138+ try :
139+ with open (self .settingPath , "r" ) as f :
140+ return json .load (f ).get ("notifyYou" , True )
141+ except Exception :
142+ pass
143+ return True
0 commit comments