Account: Generate shorter account ids.

This commit is contained in:
Christian Kamm 2015-04-24 07:02:51 +02:00
parent f184d66ea2
commit a13a974e2d
3 changed files with 30 additions and 2 deletions

View file

@ -192,6 +192,12 @@ AccountPtr AccountManager::load(const QScopedPointer<QSettings>& settings)
void AccountManager::addAccount(const AccountPtr& newAccount)
{
auto id = newAccount->id();
if (id.isEmpty() || !isAccountIdAvailable(id)) {
id = generateFreeAccountId();
}
newAccount->_id = id;
AccountStatePtr newAccountState(new AccountState(newAccount));
_accounts << newAccountState;
emit accountAdded(newAccountState.data());
@ -206,5 +212,26 @@ void AccountManager::shutdown()
}
}
bool AccountManager::isAccountIdAvailable(const QString& id) const
{
foreach (const auto& acc, _accounts) {
if (acc->account()->id() == id) {
return false;
}
}
return true;
}
QString AccountManager::generateFreeAccountId() const
{
int i = 0;
forever {
QString id = QString::number(i);
if (isAccountIdAvailable(id)) {
return id;
}
++i;
}
}
}

View file

@ -55,6 +55,9 @@ private:
AccountPtr load(const QScopedPointer<QSettings>& settings);
bool restoreFromLegacySettings();
bool isAccountIdAvailable(const QString& id) const;
QString generateFreeAccountId() const;
Q_SIGNALS:
void accountAdded(AccountState *account);

View file

@ -32,7 +32,6 @@
#include <QDir>
#include <QDebug>
#include <QSslKey>
#include <QUuid>
namespace OCC {
@ -46,7 +45,6 @@ Account::Account(QObject *parent)
, _wasMigrated(false)
{
qRegisterMetaType<AccountPtr>("AccountPtr");
_id = QUuid::createUuid().toString();
}
AccountPtr Account::create()