Commit 80dcdb66 authored by Martin Straka's avatar Martin Straka Committed by Karel Slaný

Code refactoring

parent ed2608bc
......@@ -98,12 +98,14 @@ Item {
} else {
// Change account user name.
if (userNameTextField.text.toString() !== oldUserName) {
// Create temporary account with a new user name.
if (accounts.prepareChangeUserName(accountModel, sLoginMethod,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
passwordTextField.text.toString(),
testAccount.checked, rememberPassword.checked,
useLS.checked, certPathLabelId.text.toString(), oldUserName)) {
// Login to idsd with the new user name.
isds.doIsdsAction("changeUserName", userNameTextField.text.toString())
}
} else {
......@@ -137,7 +139,10 @@ Item {
isds.getAccountInfo(userName)
}
onRunChangeUserNameSig: {
// Login to isds with new user name has been succeeded.
// Get databox ID of new user name.
var dbId = isds.getAccountDbId(userName)
// Change username and rename databases.
if (accounts.changeAccountUserName(accountModel,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
......
......@@ -170,7 +170,7 @@ bool Accounts::createAccount(const QVariant &acntModelVariant,
goto fail;
}
if (!checkCorrectLengthUserName(newUserName)) {
if (!hasCorrectLengthUserName(newUserName)) {
errTxt = tr("User name '%1' has wrong length.").arg(newUserName);
goto fail;
}
......@@ -423,6 +423,7 @@ bool Accounts::prepareChangeUserName(const QVariant &acntModelVariant,
const QString &pwd, bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath, const QString &oldUserName)
{
debugFuncCall();
/* Show confirm dialogue about use name changing */
int msgResponse = Dialogues::message(Dialogues::QUESTION,
......@@ -443,6 +444,8 @@ bool Accounts::changeAccountUserName(const QVariant &acntModelVariant,
const QString &acntName, const QString &newUserName, bool storeToDisk,
const QString &oldUserName, const QString &newDbId)
{
debugFuncCall();
QString errTxt;
AccountListModel *accountModel = Q_NULLPTR;
......@@ -462,7 +465,7 @@ bool Accounts::changeAccountUserName(const QVariant &acntModelVariant,
}
/* Check if new user name corresponds with databox id */
if (!isRelevantUserName(oldUserName, newDbId)) {
if (!isRelevantUserNameToDbId(oldUserName, newDbId)) {
errTxt = tr("New user name '%1' not corresponds with databox ID.").arg(newUserName);
goto fail;
}
......@@ -483,12 +486,8 @@ bool Accounts::changeAccountUserName(const QVariant &acntModelVariant,
goto rollbackFilesChanges;
}
/* Remove old account */
accountModel->deleteAccount(oldUserName);
/* Delete isds session context of old account */
emit removeIsdsCtx(oldUserName);
/* Load message counters of new account */
loadModelCounters(accountModel);
/* Last: Delete original/old account from model and settings */
deleteAccountFromModel(accountModel, oldUserName);
logInfoNL("Username has been changed on '%s'.",
newUserName.toUtf8().constData());
......@@ -510,24 +509,21 @@ rollbackFilesChanges:
rollbackAccountChanges:
/* Change back user name in the account database */
/* Change back origial user name in the account database */
if (!GlobInstcs::accountDbPtr->changeUserName(newUserName, oldUserName)) {
logErrorNL("%s", "Cannot change back user name in the account database.");
}
fail:
/* Show error dialog with message what happened */
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("User name problem: %1").arg(oldUserName),
tr("The new user name '%1' for account '%2' has not been set!").arg(newUserName).arg(acntName),
errTxt + " " +tr("Account will use the original settings."));
exit:
/* Remove new account */
accountModel->deleteAccount(newUserName);
/* Delete isds session context of new account */
emit removeIsdsCtx(newUserName);
deleteAccountFromModel(accountModel, newUserName);
return false;
}
......@@ -535,6 +531,8 @@ exit:
bool Accounts::changeFileDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt)
{
debugFuncCall();
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (fDb == Q_NULLPTR) {
......@@ -542,6 +540,7 @@ bool Accounts::changeFileDbName(const QString &oldUserName,
return false;
}
/* Rename file database */
QString currentDbFileName = fDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!fDb->moveDb(currentDbFileName)) {
......@@ -555,6 +554,8 @@ bool Accounts::changeFileDbName(const QString &oldUserName,
bool Accounts::changeMessageDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt)
{
debugFuncCall();
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (msgDb == Q_NULLPTR) {
......@@ -562,6 +563,7 @@ bool Accounts::changeMessageDbName(const QString &oldUserName,
return false;
}
/* Rename message database */
QString currentDbFileName = msgDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!msgDb->moveDb(currentDbFileName)) {
......@@ -572,14 +574,25 @@ bool Accounts::changeMessageDbName(const QString &oldUserName,
return true;
}
bool Accounts::checkCorrectLengthUserName(const QString &userName)
bool Accounts::hasCorrectLengthUserName(const QString &userName)
{
/* Check if new user name has correct length */
return (userName.length() == 6);
}
bool Accounts::isRelevantUserName(const QString &userName, const QString &dbId)
bool Accounts::isRelevantUserNameToDbId(const QString &userName, const QString &dbId)
{
/* Check if new user corresponds with databox id */
return (dbId == GlobInstcs::accountDbPtr->dbId(userName));
}
void Accounts::deleteAccountFromModel(AccountListModel *accountModel,
const QString &userName)
{
/* Remove old account from model */
accountModel->deleteAccount(userName);
/* Delete isds session context of old account */
emit removeIsdsCtx(userName);
/* Load message counters of all accounts */
loadModelCounters(accountModel);
}
......@@ -238,13 +238,13 @@ private:
const QString &newUserName, bool storeToDisk, QString &errTxt);
/*!
* @brief Check correct length of user name.
* @brief Check if user name has correct length.
*
* @param[in] userName New user name identifying account.
* @return True if length is correct.
*/
static
bool checkCorrectLengthUserName(const QString &userName);
bool hasCorrectLengthUserName(const QString &userName);
/*!
* @brief Check if new user name is relevant to databox id.
......@@ -253,6 +253,15 @@ private:
* @param[in] dbId Databox ID obtained from isds.
* @return True if user name is relevant to databox id.
*/
bool isRelevantUserName(const QString &userName,
bool isRelevantUserNameToDbId(const QString &userName,
const QString &dbId);
/*!
* @brief Delete account from model.
*
* @param[in,out] accountModel Model whose counters should be updated.
* @param[in] userName User name identifying account.
*/
void deleteAccountFromModel(AccountListModel *accountModel,
const QString &userName);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment