@@ -19,6 +19,7 @@ import (
1919 "github.com/lich0821/ccNexus/internal/proxy"
2020 "github.com/lich0821/ccNexus/internal/storage"
2121 "github.com/lich0821/ccNexus/internal/tray"
22+ "github.com/lich0821/ccNexus/internal/updater"
2223 "github.com/lich0821/ccNexus/internal/webdav"
2324 "github.com/wailsapp/wails/v2/pkg/runtime"
2425)
@@ -62,6 +63,7 @@ type App struct {
6263 configPath string
6364 ctxMutex sync.RWMutex
6465 trayIcon []byte
66+ updater * updater.Updater
6567}
6668
6769// NewApp creates a new App application struct
@@ -2239,3 +2241,130 @@ func (a *App) GenerateMockArchives(monthsCount int) string {
22392241 data , _ := json .Marshal (result )
22402242 return string (data )
22412243}
2244+
2245+ // CheckForUpdates checks if a new version is available
2246+ func (a * App ) CheckForUpdates () string {
2247+ logger .Info ("CheckForUpdates called, current version: %s" , a .GetVersion ())
2248+ if a .updater == nil {
2249+ a .updater = updater .New (a .GetVersion ())
2250+ }
2251+ info , err := a .updater .CheckForUpdates ()
2252+ if err != nil {
2253+ logger .Error ("CheckForUpdates failed: %v" , err )
2254+ result := map [string ]interface {}{
2255+ "success" : false ,
2256+ "error" : err .Error (),
2257+ }
2258+ data , _ := json .Marshal (result )
2259+ return string (data )
2260+ }
2261+
2262+ logger .Info ("Update check result: hasUpdate=%v, latest=%s" , info .HasUpdate , info .LatestVersion )
2263+
2264+ // Update last check time
2265+ updateCfg := a .config .GetUpdate ()
2266+ updateCfg .LastCheckTime = time .Now ().Format (time .RFC3339 )
2267+ a .config .UpdateUpdate (updateCfg )
2268+
2269+ if a .storage != nil {
2270+ configAdapter := storage .NewConfigStorageAdapter (a .storage )
2271+ a .config .SaveToStorage (configAdapter )
2272+ }
2273+
2274+ result := map [string ]interface {}{
2275+ "success" : true ,
2276+ "info" : info ,
2277+ }
2278+ data , _ := json .Marshal (result )
2279+ logger .Debug ("CheckForUpdates response: %s" , string (data ))
2280+ return string (data )
2281+ }
2282+
2283+ // GetUpdateSettings returns update settings
2284+ func (a * App ) GetUpdateSettings () string {
2285+ updateCfg := a .config .GetUpdate ()
2286+ data , _ := json .Marshal (updateCfg )
2287+ return string (data )
2288+ }
2289+
2290+ // SetUpdateSettings updates update settings
2291+ func (a * App ) SetUpdateSettings (autoCheck bool , checkInterval int ) error {
2292+ updateCfg := a .config .GetUpdate ()
2293+ updateCfg .AutoCheck = autoCheck
2294+ updateCfg .CheckInterval = checkInterval
2295+
2296+ a .config .UpdateUpdate (updateCfg )
2297+
2298+ if a .storage != nil {
2299+ configAdapter := storage .NewConfigStorageAdapter (a .storage )
2300+ if err := a .config .SaveToStorage (configAdapter ); err != nil {
2301+ return fmt .Errorf ("failed to save update settings: %w" , err )
2302+ }
2303+ }
2304+
2305+ logger .Info ("Update settings changed: autoCheck=%v, interval=%d hours" , autoCheck , checkInterval )
2306+ return nil
2307+ }
2308+
2309+ // SkipVersion skips a specific version
2310+ func (a * App ) SkipVersion (version string ) error {
2311+ updateCfg := a .config .GetUpdate ()
2312+ updateCfg .SkippedVersion = version
2313+
2314+ a .config .UpdateUpdate (updateCfg )
2315+
2316+ if a .storage != nil {
2317+ configAdapter := storage .NewConfigStorageAdapter (a .storage )
2318+ if err := a .config .SaveToStorage (configAdapter ); err != nil {
2319+ return fmt .Errorf ("failed to save skipped version: %w" , err )
2320+ }
2321+ }
2322+
2323+ logger .Info ("Version skipped: %s" , version )
2324+ return nil
2325+ }
2326+
2327+ // DownloadUpdate downloads the update file
2328+ func (a * App ) DownloadUpdate (url , filename string ) error {
2329+ if a .updater == nil {
2330+ a .updater = updater .New (a .GetVersion ())
2331+ }
2332+ return a .updater .DownloadUpdate (url , filename )
2333+ }
2334+
2335+ // GetDownloadProgress returns download progress
2336+ func (a * App ) GetDownloadProgress () string {
2337+ if a .updater == nil {
2338+ a .updater = updater .New (a .GetVersion ())
2339+ }
2340+ progress := a .updater .GetDownloadProgress ()
2341+ data , _ := json .Marshal (progress )
2342+ return string (data )
2343+ }
2344+
2345+ // InstallUpdate installs the downloaded update
2346+ func (a * App ) InstallUpdate (filePath string ) string {
2347+ if a .updater == nil {
2348+ a .updater = updater .New (a .GetVersion ())
2349+ }
2350+ result , err := a .updater .InstallUpdate (filePath )
2351+ if err != nil {
2352+ errorResult := map [string ]interface {}{
2353+ "success" : false ,
2354+ "error" : err .Error (),
2355+ }
2356+ data , _ := json .Marshal (errorResult )
2357+ return string (data )
2358+ }
2359+ data , _ := json .Marshal (result )
2360+ return string (data )
2361+ }
2362+
2363+ // SendUpdateNotification sends a system notification for updates
2364+ func (a * App ) SendUpdateNotification (title , message string ) error {
2365+ err := updater .SendNotification (title , message )
2366+ if err != nil {
2367+ logger .Error ("Failed to send notification: %v" , err )
2368+ }
2369+ return err
2370+ }
0 commit comments