From 52fdd252d25e1f1995784aa6589459f4d0d503e4 Mon Sep 17 00:00:00 2001 From: FelisCatus Date: Tue, 10 Oct 2017 14:08:31 -0700 Subject: [PATCH] Remove duplicate profile names in quickSwitchProfiles. Fix #1232. --- omega-target/src/options.coffee | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/omega-target/src/options.coffee b/omega-target/src/options.coffee index d421f13..f4ec9b1 100644 --- a/omega-target/src/options.coffee +++ b/omega-target/src/options.coffee @@ -374,7 +374,9 @@ class Options @_setOptions({'-showExternalProfile': true}, {persist: true}) @_state.set({'showExternalProfile': showExternal}) - if changes['-enableQuickSwitch']? or changes['-quickSwitchProfiles']? + quickSwitchProfiles = changes['-quickSwitchProfiles'] + quickSwitchProfiles = @_cleanUpQuickSwitchProfiles(quickSwitchProfiles) + if changes['-enableQuickSwitch']? or quickSwitchProfiles? @reloadQuickSwitch() if changes['-downloadInterval']? @schedule 'updateProfile', @_options['-downloadInterval'], => @@ -395,6 +397,20 @@ class Options handler() @_storage.watch null, handler + _cleanUpQuickSwitchProfiles: (quickSwitchProfiles) -> + return unless quickSwitchProfiles? + seenQuickSwitchProfile = {} + validQuickSwitchProfiles = quickSwitchProfiles.filter (name) => + key = OmegaPac.Profiles.nameAsKey(name) + return false if seenQuickSwitchProfile[key] + return false if not OmegaPac.Profiles.byName(name, @_options) + seenQuickSwitchProfile[key] = true + return true + if validQuickSwitchProfiles.length != quickSwitchProfiles.length + @_setOptions( + {'-quickSwitchProfiles': validQuickSwitchProfiles}, {persist: true}) + return validQuickSwitchProfiles + ###* # Reload the quick switch according to settings. # @returns {Promise} A promise which is fulfilled when the quick switch is set @@ -695,10 +711,13 @@ class Options if @_options['-startupProfileName'] == fromName changes['-startupProfileName'] = toName quickSwitch = @_options['-quickSwitchProfiles'] - for i in [0...quickSwitch.length] - if quickSwitch[i] == fromName - quickSwitch[i] = toName - changes['-quickSwitchProfiles'] = quickSwitch + # Change fromName to toName in Quick Switch, but only if it does not contain + # toName already. Otherwise it may cause duplicates. + if quickSwitch.indexOf(toName) < 0 + for i in [0...quickSwitch.length] + if quickSwitch[i] == fromName + quickSwitch[i] = toName + changes['-quickSwitchProfiles'] = quickSwitch return changes