11/*
22 * This file is part of SmartProxy <https://github.com/salarcode/SmartProxy>,
3- * Copyright (C) 2022 Salar Khalilzadeh <salar2k@gmail.com>
3+ * Copyright (C) 2025 Salar Khalilzadeh <salar2k@gmail.com>
44 *
55 * SmartProxy is free software: you can redistribute it and/or modify
66 * it under the terms of the GNU General Public License version 3 as
@@ -117,6 +117,8 @@ export class SettingsOperation {
117117 }
118118
119119 public static readSyncedSettings ( success : Function ) {
120+ // Note: At this point, we are assuming that sync is enabled in the settings.
121+
120122 // getting synced data
121123 polyFillLib . storageSyncGet ( null ,
122124 onGetSyncData ,
@@ -135,23 +137,7 @@ export class SettingsOperation {
135137 return ;
136138 }
137139
138- if ( syncedSettings . options . syncSettings ) {
139-
140- syncedSettings = Settings . getRestorableSettings ( syncedSettings ) ;
141- Settings . revertSyncOptions ( syncedSettings ) ;
142- me . copyNonSyncableSettings ( syncedSettings , Settings . current ) ;
143-
144- // use synced settings
145- Settings . current = syncedSettings ;
146-
147- } else {
148- // sync is disabled
149- syncedSettings . options . syncSettings = false ;
150- syncedSettings = Settings . getRestorableSettings ( syncedSettings ) ;
151- }
152-
153- Settings . currentOptionsSyncSettings = syncedSettings . options . syncSettings ;
154- Settings . updateActiveSettings ( ) ;
140+ me . applySyncSettings ( syncedSettings ) ;
155141
156142 if ( success )
157143 success ( ) ;
@@ -166,74 +152,41 @@ export class SettingsOperation {
166152 }
167153 }
168154
169- // DEAD CODE????? =================================
170- public static initialize ( success : Function ) {
171- ///<summary>The initialization method</summary>
172- function onGetLocalData ( data : any ) {
173- // all the settings
174- data = Settings . getRestorableSettings ( data ) ;
175- me . copyNonSyncableSettings ( data , Settings . current ) ;
176- Settings . current = data ;
177-
178- // read all the synced data along with synced ones
179- polyFillLib . storageSyncGet ( null ,
180- onGetSyncData ,
181- onGetSyncError ) ;
155+ public static applySyncSettings ( restoredSyncedSettings : SettingsConfig ) {
156+ if ( ! restoredSyncedSettings ) {
157+ return ;
182158 }
183159
184- function onGetSyncData ( data : any ) {
185-
186- try {
187- let syncedSettings = utilsLib . decodeSyncData ( data ) ;
188-
189- // only if sync settings is enabled
190- if ( syncedSettings &&
191- syncedSettings . options ) {
192-
193- if ( syncedSettings . options . syncSettings ) {
194-
160+ // use synced settings
161+ restoredSyncedSettings = Settings . getRestorableSettings ( restoredSyncedSettings ) ;
162+ me . revertSyncOptions ( restoredSyncedSettings ) ;
163+ me . copyNonSyncableSettings ( restoredSyncedSettings , Settings . current ) ;
195164
196- // use synced settings
197- syncedSettings = Settings . getRestorableSettings ( syncedSettings ) ;
198- Settings . revertSyncOptions ( syncedSettings ) ;
199- me . copyNonSyncableSettings ( syncedSettings , Settings . current ) ;
165+ Settings . current = restoredSyncedSettings ;
200166
201- Settings . current = syncedSettings ;
202-
203- } else {
204- // sync is disabled
205- syncedSettings . options . syncSettings = false ;
206- syncedSettings = Settings . getRestorableSettings ( syncedSettings ) ;
207- }
167+ Settings . updateActiveSettings ( ) ;
168+ }
208169
209- Settings . currentOptionsSyncSettings = syncedSettings . options . syncSettings ;
210- Settings . updateActiveSettings ( ) ;
211- }
212- } catch ( e ) {
213- Debug . error ( `SettingsOperation.onGetSyncData error: ${ e } \r\n ${ data } ` ) ;
214- }
170+ /** In local options if sync is disabled for these particular options, don't update them from sync server */
171+ private static revertSyncOptions ( syncedConfig : SettingsConfig ) {
172+ let settings = Settings . current ;
215173
216- if ( success ) {
217- success ( ) ;
218- }
219- }
174+ syncedConfig . options . syncSettings = settings . options . syncSettings ;
175+ syncedConfig . options . syncActiveProxy = settings . options . syncActiveProxy ;
176+ syncedConfig . options . syncActiveProfile = settings . options . syncActiveProfile ;
220177
221- function onGetLocalError ( error : any ) {
222- Debug . error ( `SettingsOperation.initialize error: ${ error . message } ` ) ;
178+ if ( ! settings . options . syncActiveProxy ) {
179+ syncedConfig . defaultProxyServerId = settings . defaultProxyServerId ;
223180 }
224-
225- function onGetSyncError ( error : any ) {
226- Debug . error ( `SettingsOperation.initialize error: ${ error . message } ` ) ;
227-
228- // local settings should be used
229- if ( success ) {
230- success ( ) ;
231- }
181+ if ( ! settings . options . syncActiveProfile ) {
182+ syncedConfig . activeProfileId = settings . activeProfileId ;
232183 }
233- polyFillLib . storageLocalGet ( null ,
234- onGetLocalData ,
235- onGetLocalError ) ;
236184
185+ syncedConfig . options . syncWebDavServerEnabled = settings . options . syncWebDavServerEnabled ;
186+ syncedConfig . options . syncWebDavServerUrl = settings . options . syncWebDavServerUrl ;
187+ syncedConfig . options . syncWebDavBackupFilename = settings . options . syncWebDavBackupFilename ;
188+ syncedConfig . options . syncWebDavServerUser = settings . options . syncWebDavServerUser ;
189+ syncedConfig . options . syncWebDavServerPassword = settings . options . syncWebDavServerPassword ;
237190 }
238191
239192 public static findProxyServerByIdFromList ( id : string , proxyServers : ProxyServer [ ] , proxyServerSubs : ProxyServerSubscription [ ] ) : ProxyServer {
@@ -304,7 +257,7 @@ export class SettingsOperation {
304257 let proxiesFromSubscription : ProxyServerFromSubscription [ ] = subscription . proxies . map ( proxy => {
305258 return { ...proxy , subscriptionName : subscription . name } as ProxyServerFromSubscription ;
306259 } ) ;
307-
260+
308261 result = result . concat ( proxiesFromSubscription ) ;
309262 }
310263 }
@@ -383,7 +336,7 @@ export class SettingsOperation {
383336 if ( area !== "sync" ) return ;
384337
385338 if ( ! Settings . current . options . syncSettings ) {
386- Debug . log ( "Ignoring syncOnChanged. " , area , changes ) ;
339+ Debug . log ( "Sync is disabled, ignoring browser syncOnChanged. " , area , changes ) ;
387340 return ;
388341 }
389342
@@ -418,8 +371,7 @@ export class SettingsOperation {
418371 if ( ! saveToSyncServer )
419372 return ;
420373
421- if ( ! Settings . current . options . syncSettings &&
422- ! Settings . currentOptionsSyncSettings ) {
374+ if ( ! Settings . current . options . syncSettings ) {
423375 return ;
424376 }
425377
@@ -441,7 +393,7 @@ export class SettingsOperation {
441393 try {
442394 polyFillLib . storageSyncSet ( saveObject ,
443395 ( ) => {
444- Settings . currentOptionsSyncSettings = current . options . syncSettings ;
396+ Debug . log ( `SettingsOperation.saveAllSync: Settings saved to sync storage successfully.` , saveObject ) ;
445397 } ,
446398 ( error : Error ) => {
447399 Debug . error ( `SettingsOperation.saveAllSync error: ${ error . message } ` , saveObject ) ;
0 commit comments