Local account settings is not part of the `local_settings` service any more,

cause it logically doesn't belong there as it is not a service. It is a global
instance, exposed to the UI (qml) part. Since it represents QSettings it should
be maintained from the single point.
This commit is contained in:
Sale Djenic 2021-10-18 10:21:59 +02:00 committed by Iuri Matias
parent 37e9150021
commit 162ced9c38
12 changed files with 97 additions and 119 deletions

View File

@ -94,23 +94,31 @@ proc newAppController*(appService: AppService): AppController =
result = AppController()
result.appService = appService
# Services
#################################################
# Since localSettingsService is a product of old architecture, and used only to
# manage `Settings` component (global and profile) in qml, this should be removed
# at the end of refactroing process and moved to the same approach we use for
# LocalAccountSettings, and that will be maintained only on the Nim side. There
# should not be two instances maintain the same settings.
result.localSettingsService = local_settings_service.newService()
result.keychainService = keychain_service.newService(result.localSettingsService,
appService.status.events)
#################################################
result.keychainService = keychain_service.newService(appService.status.events)
result.accountsService = accounts_service.newService()
result.contactService = contact_service.newService()
result.chatService = chat_service.newService()
result.communityService = community_service.newService(result.chatService)
# Core
result.localAccountSettings = newLocalAccountSettings(result.localSettingsService)
result.localAccountSettingsVariant = newQVariant(result.localAccountSettings)
result.localAccountSettingsVariant = newQVariant(
singletonInstance.localAccountSettings)
# Modules
result.startupModule = startup_module.newModule[AppController](result,
appService.status.events, appService.status.fleet, result.localSettingsService,
result.keychainService, result.accountsService)
appService.status.events, appService.status.fleet, result.keychainService,
result.accountsService)
result.mainModule = main_module.newModule[AppController](result,
appService.status.events, result.localSettingsService, result.keychainService,
result.accountsService, result.chatService, result.communityService)
appService.status.events, result.keychainService, result.accountsService,
result.chatService, result.communityService)
#################################################
# At the end of refactoring this will be moved to
@ -130,7 +138,6 @@ proc delete*(self: AppController) =
self.profile.delete
#################################################
self.localSettingsService.delete
self.localAccountSettingsVariant.delete
self.localSettingsService.delete

View File

@ -1,5 +1,9 @@
import NimQml
import local_account_settings as local_acc_settings
export local_acc_settings
type
GlobalSingleton = object
# Don't export GlobalSingleton type.
@ -14,5 +18,13 @@ proc engine*(self: GlobalSingleton): QQmlApplicationEngine =
return qmlEngine
proc localAccountSettings*(self: GlobalSingleton): LocalAccountSettings =
var localAccountSettings {.global.}: LocalAccountSettings
if (localAccountSettings.isNil):
localAccountSettings = newLocalAccountSettings()
return localAccountSettings
proc delete*(self: GlobalSingleton) =
self.engine.delete()
self.engine.delete()
self.localAccountSettings.delete()

View File

@ -1,31 +1,63 @@
import NimQml
import NimQml, os
import ../../app_service/service/local_settings/service as local_settings_service
import ../../constants
# Local Account Settings keys:
const LS_KEY_STORE_TO_KEYCHAIN* = "storeToKeychain"
# Local Account Settings values:
const LS_VALUE_STORE* = "store"
const LS_VALUE_NOTNOW* = "notNow"
const LS_VALUE_NEVER* = "never"
QtObject:
type LocalAccountSettings* = ref object of QObject
localSettingsService: local_settings_service.Service
settingsFileDir: string
settings: QSettings
proc setup(self: LocalAccountSettings) =
self.QObject.setup
self.settingsFileDir = os.joinPath(DATADIR, "qt")
proc delete*(self: LocalAccountSettings) =
if(not self.settings.isNil):
self.settings.delete
self.QObject.delete
proc newLocalAccountSettings*(localSettingsService: local_settings_service.Service):
proc newLocalAccountSettings*():
LocalAccountSettings =
new(result, delete)
result.setup
result.localSettingsService = localSettingsService
proc setFileName*(self: LocalAccountSettings, fileName: string) =
if(not self.settings.isNil):
self.settings.delete
let filePath = os.joinPath(self.settingsFileDir, fileName)
self.settings = newQSettings(filePath, QSettingsFormat.IniFormat)
proc storeToKeychainValueChanged*(self: LocalAccountSettings) {.signal.}
proc getStoreToKeychainValue(self: LocalAccountSettings): string {.slot.} =
self.localSettingsService.getAccountValue(LS_KEY_STORE_TO_KEYCHAIN).stringVal
proc removeKey*(self: LocalAccountSettings, key: string) =
if(self.settings.isNil):
return
self.settings.remove(key)
proc setStoreToKeychainValue(self: LocalAccountSettings, value: string) {.slot.} =
self.localSettingsService.setAccountValue(LS_KEY_STORE_TO_KEYCHAIN,
newQVariant(value))
if(key == LS_KEY_STORE_TO_KEYCHAIN):
self.storeToKeychainValueChanged()
proc getStoreToKeychainValue*(self: LocalAccountSettings): string {.slot.} =
if(self.settings.isNil):
return ""
self.settings.value(LS_KEY_STORE_TO_KEYCHAIN).stringVal
proc setStoreToKeychainValue*(self: LocalAccountSettings, value: string) {.slot.} =
if(self.settings.isNil):
return
self.settings.setValue(LS_KEY_STORE_TO_KEYCHAIN, newQVariant(value))
self.storeToKeychainValueChanged()
QtProperty[string] storeToKeychainValue:

View File

@ -2,8 +2,8 @@ import NimQml, Tables
import controller_interface
import io_interface
import ../../core/global_singleton
import ../../../app_service/service/local_settings/service as local_settings_service
import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/accounts/service_interface as accounts_service
import ../../../app_service/service/community/service as community_service
@ -17,14 +17,12 @@ type
Controller* = ref object of controller_interface.AccessInterface
delegate: io_interface.AccessInterface
events: EventEmitter
localSettingsService: local_settings_service.Service
keychainService: keychain_service.Service
accountsService: accounts_service.ServiceInterface
communityService: community_service.ServiceInterface
proc newController*(delegate: io_interface.AccessInterface,
events: EventEmitter,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface,
communityService: community_service.ServiceInterface):
@ -32,7 +30,6 @@ proc newController*(delegate: io_interface.AccessInterface,
result = Controller()
result.delegate = delegate
result.events = events
result.localSettingsService = localSettingsService
result.keychainService = keychainService
result.accountsService = accountsService
result.communityService = communityService
@ -43,7 +40,7 @@ method delete*(self: Controller) =
method init*(self: Controller) =
if(defined(macosx)):
let account = self.accountsService.getLoggedInAccount()
self.localSettingsService.updateAccountSettingsFilePath(account.name)
singletonInstance.localAccountSettings.setFileName(account.name)
self.events.on("keychainServiceSuccess") do(e:Args):
let args = KeyChainServiceArg(e)
@ -51,8 +48,7 @@ method init*(self: Controller) =
self.events.on("keychainServiceError") do(e:Args):
let args = KeyChainServiceArg(e)
self.localSettingsService.setAccountValue(LS_KEY_STORE_TO_KEYCHAIN,
newQVariant(LS_VALUE_NOTNOW))
singletonInstance.localAccountSettings.removeKey(LS_KEY_STORE_TO_KEYCHAIN)
self.delegate.emitStoringPasswordError(args.errDescription)
method getCommunities*(self: Controller): seq[community_service.CommunityDto] =
@ -66,9 +62,7 @@ method checkForStoringPassword*(self: Controller) =
if(not defined(macosx)):
return
let value = self.localSettingsService.getAccountValue(
LS_KEY_STORE_TO_KEYCHAIN).stringVal
let value = singletonInstance.localAccountSettings.getStoreToKeychainValue()
if (value == LS_VALUE_STORE or value == LS_VALUE_NEVER):
return
@ -78,4 +72,9 @@ method checkForStoringPassword*(self: Controller) =
method storePassword*(self: Controller, password: string) =
let account = self.accountsService.getLoggedInAccount()
let value = singletonInstance.localAccountSettings.getStoreToKeychainValue()
if (value != LS_VALUE_STORE or account.name.len == 0):
return
self.keychainService.storePassword(account.name, password)

View File

@ -1,11 +1,10 @@
import NimQml, Tables
import io_interface, view, controller, item
import ../../../app/core/global_singleton
import ../../core/global_singleton
import chat_section/module as chat_section_module
import ../../../app_service/service/local_settings/service as local_settings_service
import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/accounts/service_interface as accounts_service
import ../../../app_service/service/chat/service as chat_service
@ -36,7 +35,6 @@ type
proc newModule*[T](delegate: T,
events: EventEmitter,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface,
chatService: chat_service.Service,
@ -46,8 +44,8 @@ proc newModule*[T](delegate: T,
result.delegate = delegate
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(result, events, localSettingsService,
keychainService, accountsService, communityService)
result.controller = controller.newController(result, events, keychainService,
accountsService, communityService)
# Submodules
result.chatSectionModule = chat_section_module.newModule(result, "chat",

View File

@ -3,7 +3,6 @@ import Tables, chronicles
import controller_interface
import io_interface
import ../../../app_service/service/local_settings/service as local_settings_service
import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/accounts/service_interface as accounts_service
@ -19,14 +18,10 @@ type
Controller* = ref object of controller_interface.AccessInterface
delegate: io_interface.AccessInterface
events: EventEmitter
localSettingsService: local_settings_service.Service
keychainService: keychain_service.Service
accountsService: accounts_service.ServiceInterface
proc newController*(delegate: io_interface.AccessInterface,
events: EventEmitter,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface):
Controller =
result = Controller()

View File

@ -2,8 +2,8 @@ import NimQml, Tables
import controller_interface
import io_interface
import ../../../core/global_singleton
import ../../../../app_service/service/local_settings/service as local_settings_service
import ../../../../app_service/service/keychain/service as keychain_service
import ../../../../app_service/service/accounts/service_interface as accounts_service
@ -16,21 +16,18 @@ type
Controller* = ref object of controller_interface.AccessInterface
delegate: io_interface.AccessInterface
events: EventEmitter
localSettingsService: local_settings_service.Service
keychainService: keychain_service.Service
accountsService: accounts_service.ServiceInterface
selectedAccountKeyUid: string
proc newController*(delegate: io_interface.AccessInterface,
events: EventEmitter,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface):
Controller =
result = Controller()
result.delegate = delegate
result.events = events
result.localSettingsService = localSettingsService
result.keychainService = keychainService
result.accountsService = accountsService
@ -53,7 +50,7 @@ method init*(self: Controller) =
if (args.errType == ERROR_TYPE_AUTHENTICATION):
return
self.localSettingsService.removeAccountValue(LS_KEY_STORE_TO_KEYCHAIN)
singletonInstance.localAccountSettings.removeKey(LS_KEY_STORE_TO_KEYCHAIN)
self.delegate.emitObtainingPasswordError(args.errDescription)
method getOpenedAccounts*(self: Controller): seq[AccountDto] =
@ -73,7 +70,11 @@ method setSelectedAccountKeyUid*(self: Controller, keyUid: string) =
return
let selectedAccount = self.getSelectedAccount()
self.localSettingsService.updateAccountSettingsFilePath(selectedAccount.name)
singletonInstance.localAccountSettings.setFileName(selectedAccount.name)
let value = singletonInstance.localAccountSettings.getStoreToKeychainValue()
if (value != LS_VALUE_STORE):
return
self.keychainService.tryToObtainPassword(selectedAccount.name)

View File

@ -2,9 +2,8 @@ import NimQml
import io_interface
import ../io_interface as delegate_interface
import view, controller, item
import ../../../../app/core/global_singleton
import ../../../core/global_singleton
import ../../../../app_service/service/local_settings/service as local_settings_service
import ../../../../app_service/service/keychain/service as keychain_service
import ../../../../app_service/service/accounts/service_interface as accounts_service
@ -22,7 +21,6 @@ type
proc newModule*(delegate: delegate_interface.AccessInterface,
events: EventEmitter,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface):
Module =
@ -30,8 +28,8 @@ proc newModule*(delegate: delegate_interface.AccessInterface,
result.delegate = delegate
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(result, events, localSettingsService,
keychainService, accountsService)
result.controller = controller.newController(result, events, keychainService,
accountsService)
result.moduleLoaded = false
singletonInstance.engine.setRootContextProperty("loginModule", result.viewVariant)

View File

@ -7,7 +7,6 @@ import ../../../app/core/global_singleton
import onboarding/module as onboarding_module
import login/module as login_module
import ../../../app_service/service/local_settings/service as local_settings_service
import ../../../app_service/service/keychain/service as keychain_service
import ../../../app_service/service/accounts/service_interface as accounts_service
@ -28,7 +27,6 @@ type
proc newModule*[T](delegate: T,
events: EventEmitter,
fleet: FleetModel,
localSettingsService: local_settings_service.Service,
keychainService: keychain_service.Service,
accountsService: accounts_service.ServiceInterface):
Module[T] =
@ -36,14 +34,13 @@ proc newModule*[T](delegate: T,
result.delegate = delegate
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.controller = controller.newController(result, events, localSettingsService,
keychainService, accountsService)
result.controller = controller.newController(result, events, accountsService)
# Submodules
result.onboardingModule = onboarding_module.newModule(result, events, fleet,
accountsService)
result.loginModule = login_module.newModule(result, events, localSettingsService,
keychainService, accountsService)
result.loginModule = login_module.newModule(result, events, keychainService,
accountsService)
method delete*[T](self: Module[T]) =
self.onboardingModule.delete

View File

@ -1,7 +1,5 @@
import NimQml, chronicles
import ../local_settings/service as local_settings_service
import eventemitter
logScope:
@ -19,7 +17,6 @@ type
QtObject:
type Service* = ref object of QObject
localSettingsService: local_settings_service.Service
events: EventEmitter
keychainManager: StatusKeychainManager
@ -35,30 +32,15 @@ QtObject:
self.keychainManager.delete
self.QObject.delete
proc newService*(localSettingsService: local_settings_service.Service,
events: EventEmitter):
Service =
proc newService*(events: EventEmitter): Service =
new(result, delete)
result.setup()
result.localSettingsService = localSettingsService
result.events = events
proc storePassword*(self: Service, username: string, password: string) =
let value = self.localSettingsService.getAccountValue(
LS_KEY_STORE_TO_KEYCHAIN).stringVal
if (value != LS_VALUE_STORE or username.len == 0):
return
self.keychainManager.storeDataAsync(username, password)
proc tryToObtainPassword*(self: Service, username: string) =
let value = self.localSettingsService.getAccountValue(
LS_KEY_STORE_TO_KEYCHAIN).stringVal
if (value != LS_VALUE_STORE):
return
self.keychainManager.readDataAsync(username)
proc onKeychainManagerError*(self: Service, errorType: string, errorCode: int,

View File

@ -4,30 +4,18 @@ import ../../../constants
logScope:
topics = "local-settings"
const UNKNOWN_ACCOUNT = "unknownAccount"
const UNKNOWN_PROFILE = "unknownProfile"
# Local Account Settings keys:
const LS_KEY_STORE_TO_KEYCHAIN* = "storeToKeychain"
# Local Account Settings values:
const LS_VALUE_STORE* = "store"
const LS_VALUE_NOTNOW* = "notNow"
const LS_VALUE_NEVER* = "never"
QtObject:
type Service* = ref object of QObject
settingsFilePath: string
settings: QSettings
accountSettingsFilePath: string
accountSettings: QSettings
globalSettingsFilePath: string
globalSettings: QSettings
proc setup(self: Service) =
self.settingsFilePath = os.joinPath(DATADIR, "qt", UNKNOWN_PROFILE)
self.settings = newQSettings(self.settingsFilePath, QSettingsFormat.IniFormat)
self.accountSettingsFilePath = os.joinPath(DATADIR, "qt", UNKNOWN_ACCOUNT)
self.accountSettings = newQSettings(self.accountSettingsFilePath, QSettingsFormat.IniFormat)
self.globalSettingsFilePath = os.joinPath(DATADIR, "qt", "global")
self.globalSettings = newQSettings(self.globalSettingsFilePath, QSettingsFormat.IniFormat)
self.QObject.setup
@ -44,9 +32,6 @@ QtObject:
proc getGlobalSettingsFilePath*(self: Service): string =
return self.globalSettingsFilePath
proc getAccountSettingsFilePath*(self: Service): string =
return self.accountSettingsFilePath
proc getSettingsFilePath*(self: Service): string =
return self.settingsFilePath
@ -61,27 +46,6 @@ QtObject:
self.settingsFilePath = os.joinPath(DATADIR, "qt", pubKey)
self.settings = newQSettings(self.settingsFilePath, QSettingsFormat.IniFormat)
proc updateAccountSettingsFilePath*(self: Service, filePath: string) =
let unknownAccountSettingsPath = os.joinPath(DATADIR, "qt", UNKNOWN_ACCOUNT)
if (not unknownAccountSettingsPath.tryRemoveFile):
# Only fails if the file exists and an there was an error removing it
# More info: https://nim-lang.org/docs/os.html#tryRemoveFile%2Cstring
warn "Failed to remove unused settings file", file=unknownAccountSettingsPath
self.accountSettings.delete
self.accountSettingsFilePath = os.joinPath(DATADIR, "qt", filePath)
self.accountSettings = newQSettings(self.accountSettingsFilePath, QSettingsFormat.IniFormat)
proc setAccountValue*(self: Service, key: string, value: QVariant) =
self.accountSettings.setValue(key, value)
proc getAccountValue*(self: Service, key: string,
defaultValue: QVariant = newQVariant()): QVariant =
self.accountSettings.value(key, defaultValue)
proc removeAccountValue*(self: Service, key: string) =
self.accountSettings.remove(key)
proc setValue*(self: Service, key: string, value: QVariant) =
self.settings.setValue(key, value)

View File

@ -43,13 +43,6 @@ ModalPopup {
}
}
Connections {
target: mainModule
onStoringPasswordError: {
updateListState()
}
}
ButtonGroup {
id: openLinksWithGroup
}