Skip to content

Commit da4f2a3

Browse files
committed
Restoring settings loaded event, #443
1 parent 8d6b972 commit da4f2a3

3 files changed

Lines changed: 43 additions & 127 deletions

File tree

src/core/Core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This file is part of SmartProxy <https://github.com/salarcode/SmartProxy>,
3-
* Copyright (C) 2024 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

src/core/Settings.ts

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
@@ -46,9 +46,6 @@ export class Settings {
4646

4747
public static active: SettingsActive;
4848

49-
// TODO: remove this as it is not used anywhere
50-
public static currentOptionsSyncSettings: boolean = true;
51-
5249
public static onInitializedLocally: Function = null;
5350
public static onInitializedRemoteSync: Function = null;
5451

@@ -105,6 +102,11 @@ export class Settings {
105102
PolyFill.storageSyncGet(null, me.onInitializeGetSyncData, me.onInitializeGetSyncError);
106103
}
107104
}
105+
else {
106+
// Sync is disabled, so we just finish the initialization
107+
108+
me.raiseInitializeCompletedEvent();
109+
}
108110
}
109111

110112
private static onInitializeGetLocalError(error: any) {
@@ -133,7 +135,7 @@ export class Settings {
133135
Debug.log("readWebDavSyncData, sync data: ", JSON.stringify(restoredSettings));
134136

135137
// ---------
136-
me.applySyncSettings(restoredSettings);
138+
sop.applySyncSettings(restoredSettings);
137139

138140
if (me.onInitializedRemoteSync)
139141
me.onInitializedRemoteSync();
@@ -154,7 +156,7 @@ export class Settings {
154156
Debug.log("onInitializeGetSyncData, sync data: ", JSON.stringify(syncedSettings));
155157

156158
// ---------
157-
me.applySyncSettings(syncedSettings);
159+
sop.applySyncSettings(syncedSettings);
158160
} catch (e) {
159161
Debug.error(`settingsOperation.readSyncedSettings> onGetSyncData error: ${e} \r\n`, JSON.stringify(data));
160162
}
@@ -165,22 +167,6 @@ export class Settings {
165167
me.raiseInitializeCompletedEvent();
166168
}
167169

168-
private static applySyncSettings(restoredSyncedSettings: SettingsConfig) {
169-
if (!restoredSyncedSettings) {
170-
return;
171-
}
172-
173-
// use synced settings
174-
restoredSyncedSettings = me.getRestorableSettings(restoredSyncedSettings);
175-
me.revertSyncOptions(restoredSyncedSettings);
176-
sop.copyNonSyncableSettings(restoredSyncedSettings, me.current);
177-
178-
me.current = restoredSyncedSettings;
179-
180-
me.currentOptionsSyncSettings = restoredSyncedSettings.options.syncSettings;
181-
me.updateActiveSettings();
182-
}
183-
184170
private static onInitializeGetSyncError(error: Error) {
185171
Debug.error(`settingsOperation reading synced settings has failed error: ${error.message}`);
186172

@@ -396,7 +382,7 @@ export class Settings {
396382

397383
// proxyMode
398384
if (config.proxyMode != null && config.proxyProfiles) {
399-
let activeProfileType: SmartProfileType = -1;
385+
let activeProfileType: SmartProfileType | number = -1;
400386

401387
switch (+config.proxyMode) {
402388
case 0 /** Direct */:
@@ -457,28 +443,6 @@ export class Settings {
457443
});
458444
}
459445

460-
/** In local options if sync is disabled for these particular options, don't update them from sync server */
461-
public static revertSyncOptions(syncedConfig: SettingsConfig) {
462-
let settings = me.current;
463-
464-
syncedConfig.options.syncSettings = settings.options.syncSettings;
465-
syncedConfig.options.syncActiveProxy = settings.options.syncActiveProxy;
466-
syncedConfig.options.syncActiveProfile = settings.options.syncActiveProfile;
467-
468-
if (!settings.options.syncActiveProxy) {
469-
syncedConfig.defaultProxyServerId = settings.defaultProxyServerId;
470-
}
471-
if (!settings.options.syncActiveProfile) {
472-
syncedConfig.activeProfileId = settings.activeProfileId;
473-
}
474-
475-
syncedConfig.options.syncWebDavServerEnabled = settings.options.syncWebDavServerEnabled;
476-
syncedConfig.options.syncWebDavServerUrl = settings.options.syncWebDavServerUrl;
477-
syncedConfig.options.syncWebDavBackupFilename = settings.options.syncWebDavBackupFilename;
478-
syncedConfig.options.syncWebDavServerUser = settings.options.syncWebDavServerUser;
479-
syncedConfig.options.syncWebDavServerPassword = settings.options.syncWebDavServerPassword;
480-
}
481-
482446
/** Validates SmartProfiles and adds missing profile and properties */
483447
static setDefaultSettingsSmartProfiles(proxyProfiles: SmartProfile[]): SmartProfile[] {
484448

src/core/SettingsOperation.ts

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

Comments
 (0)