diff --git a/src/gui/accountmanager.cpp b/src/gui/accountmanager.cpp index 052cdcfa28e..0289eef87c2 100644 --- a/src/gui/accountmanager.cpp +++ b/src/gui/accountmanager.cpp @@ -51,6 +51,11 @@ QString davUserDisplyNameC() return "display-name"; } +QString accountAliasC() +{ + return "account-alias"; +} + QString userUUIDC() { return "uuid"; @@ -108,7 +113,7 @@ bool AccountManager::restore() for (const auto &accountIndex : childGroups) { settings->beginGroup(accountIndex); if (auto acc = loadAccountHelper(*settings)) { - acc->_groupIndex = accountIndex; + acc->setGroupIndex(accountIndex); if (auto accState = AccountState::loadFromSettings(acc, *settings)) { addAccountState(accState); } @@ -124,6 +129,7 @@ Account *AccountManager::createAccount(const NewAccountModel &model) auto account = new Account(QUuid::createUuid(), model.davUser(), model.effectiveUserInfoUrl()); account->setDavDisplayName(model.displayName()); + account->setAccountAlias(account->calculateAlias()); Credentials *creds = new Credentials(model.authToken(), model.refreshToken(), account); account->setCredentials(creds); @@ -161,9 +167,10 @@ void AccountManager::saveAccount(Account *account) auto settings = ConfigFile::settingsWithGroup(accountsC()); settings->beginGroup(account->groupIndex()); - settings->setValue(urlC(), account->_url.toString()); - settings->setValue(davUserC(), account->_davUser); - settings->setValue(davUserDisplyNameC(), account->_displayName); + settings->setValue(urlC(), account->url().toString()); + settings->setValue(davUserC(), account->davUser()); + settings->setValue(davUserDisplyNameC(), account->davDisplayName()); + settings->setValue(accountAliasC(), account->accountAlias()); settings->setValue(userUUIDC(), account->uuid()); if (account->hasCapabilities()) { settings->setValue(capabilitesC(), account->capabilities().raw()); @@ -283,6 +290,10 @@ Account *AccountManager::loadAccountHelper(QSettings &settings) auto acc = new Account(uid, user, url); acc->setDavDisplayName(settings.value(davUserDisplyNameC()).toString()); + QString alias = settings.value(accountAliasC()).toString(); + if (alias.isEmpty()) + alias = acc->calculateAlias(); + acc->setAccountAlias(alias); acc->setCapabilities(caps); acc->setDefaultSyncRoot(settings.value(defaultSyncRootC()).toString()); @@ -332,7 +343,7 @@ AccountState *AccountManager::addAccount(Account *newAccount) if (id.isEmpty() || !isAccountIndexAvailable(id)) { id = generateFreeAccountIndex(); } - newAccount->_groupIndex = id; + newAccount->setGroupIndex(id); return addAccountState(new AccountState(newAccount)); diff --git a/src/gui/accountsgui/accountsguicontroller.cpp b/src/gui/accountsgui/accountsguicontroller.cpp index 3d59a0ed2bd..ea26d428547 100644 --- a/src/gui/accountsgui/accountsguicontroller.cpp +++ b/src/gui/accountsgui/accountsguicontroller.cpp @@ -94,7 +94,7 @@ void AccountsGuiController::onAccountAdded(AccountState *state) // it's really dumb to have "long" text on any toolbar component in the first place. We hope to fix this someday // by allowing the user to set an account alias which will show as the account name but that needs a major release // to support the config change - accountAction->setText(account->hostName()); + accountAction->setText(account->accountAlias()); accountAction->setToolTip(QString("%1\n%2").arg(account->davDisplayName(), account->url().toDisplayString())); accountAction->setData(QVariant::fromValue(accountView)); diff --git a/src/libsync/account.cpp b/src/libsync/account.cpp index 5de7ccdd443..dbd4773b5b4 100644 --- a/src/libsync/account.cpp +++ b/src/libsync/account.cpp @@ -155,6 +155,25 @@ bool Account::hasAvatar() const return !_avatarImg.isNull(); } +QString Account::accountAlias() const +{ + return _alias; +} + +void Account::setAccountAlias(const QString &newAlias) +{ + if (_alias != newAlias) { + _alias = newAlias; + emit accountAliasChanged(_alias); + } +} + +QString Account::calculateAlias() const +{ + QString first = davDisplayName().section(QRegularExpression("\\s"), 0, 0, QString::SectionSkipEmpty); + return first; +} + QString Account::displayNameWithHost() const { QString user = davDisplayName(); @@ -175,11 +194,6 @@ QString Account::initials() const return out; } -QGradient::Preset Account::avatarGradient() const -{ - return static_cast(qHash(displayNameWithHost()) % QGradient::NumPresets + 1); -} - QString Account::davDisplayName() const { if (_displayName.isEmpty()) { @@ -196,6 +210,12 @@ void Account::setDavDisplayName(const QString &newDisplayName) } } +void Account::setGroupIndex(const QString &index) +{ + if (index != _groupIndex) + _groupIndex = index; +} + QString Account::groupIndex() const { return _groupIndex; diff --git a/src/libsync/account.h b/src/libsync/account.h index 0b1bcac311c..e3828dd7888 100644 --- a/src/libsync/account.h +++ b/src/libsync/account.h @@ -13,8 +13,7 @@ */ -#ifndef SERVERCONNECTION_H -#define SERVERCONNECTION_H +#pragma once #include "owncloudlib.h" @@ -60,17 +59,17 @@ class ResourcesCache; class OWNCLOUDSYNC_EXPORT Account : public QObject { Q_OBJECT - Q_PROPERTY(QUuid uid READ uuid CONSTANT) - Q_PROPERTY(QString davUser READ davUser CONSTANT) - Q_PROPERTY(QString davDisplayName READ davDisplayName NOTIFY displayNameChanged) - Q_PROPERTY(QString displayNameWithHost READ displayNameWithHost NOTIFY displayNameChanged) - Q_PROPERTY(QString initials READ initials NOTIFY displayNameChanged) - Q_PROPERTY(QString hostName READ hostName CONSTANT) - Q_PROPERTY(bool hasAvatar READ hasAvatar NOTIFY avatarChanged) - Q_PROPERTY(QGradient::Preset avatarGradient READ avatarGradient NOTIFY displayNameChanged) - Q_PROPERTY(QUrl url READ url CONSTANT) - QML_ELEMENT - QML_UNCREATABLE("Only created in the C++ code") + /* Q_PROPERTY(QUuid uid READ uuid CONSTANT) + Q_PROPERTY(QString davUser READ davUser CONSTANT) + Q_PROPERTY(QString davDisplayName READ davDisplayName NOTIFY displayNameChanged) + Q_PROPERTY(QString displayNameWithHost READ displayNameWithHost NOTIFY displayNameChanged) + Q_PROPERTY(QString initials READ initials NOTIFY displayNameChanged) + Q_PROPERTY(QString hostName READ hostName CONSTANT) + Q_PROPERTY(bool hasAvatar READ hasAvatar NOTIFY avatarChanged) + Q_PROPERTY(QGradient::Preset avatarGradient READ avatarGradient NOTIFY displayNameChanged) + Q_PROPERTY(QUrl url READ url CONSTANT) + QML_ELEMENT + QML_UNCREATABLE("Only created in the C++ code")*/ public: /** @@ -107,6 +106,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject */ QUrl url() const; + /*** * This is the default folder containing all spaces. */ @@ -131,13 +131,20 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject void setAvatar(const QIcon &img); bool hasAvatar() const; + // alias for the account which appears in the toolbar button text. + // the user can edit to make each account easily identifiable. + // default value is first section of davDisplayName() (all text appearing before first whitespace) + QString accountAlias() const; + void setAccountAlias(const QString &newAlias); + QString calculateAlias() const; + /// The name of the account as shown in the toolbar QString displayNameWithHost() const; QString initials() const; - QGradient::Preset avatarGradient() const; /// The value used to group the account's setttings QString groupIndex() const; + void setGroupIndex(const QString &index); QString hostName() const; @@ -236,6 +243,7 @@ public Q_SLOTS: void avatarChanged(); void displayNameChanged(); + void accountAliasChanged(const QString &newAlias); void unknownConnectionState(); @@ -250,6 +258,7 @@ public Q_SLOTS: QUuid _uuid; QString _davUser; QString _displayName; + QString _alias; QString _defaultSyncRoot; QIcon _avatarImg; @@ -270,7 +279,7 @@ public Q_SLOTS: AppProvider _appProvider; GraphApi::SpacesManager *_spacesManager = nullptr; - friend class AccountManager; + // friend class AccountManager; }; } @@ -278,5 +287,3 @@ Q_DECLARE_METATYPE(OCC::Account) QDebug OWNCLOUDSYNC_EXPORT operator<<(QDebug debug, const OCC::Account *job); - -#endif //SERVERCONNECTION_H