1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(settings): Consolidate friendProp insert logic

This commit is contained in:
Mick Sayson 2018-10-28 13:02:26 -07:00
parent e1e42f8d60
commit 4af88a3775
2 changed files with 46 additions and 87 deletions

View File

@ -354,8 +354,7 @@ void Settings::loadPersonal(Profile* profile)
friendLst.reserve(size); friendLst.reserve(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ps.setArrayIndex(i); ps.setArrayIndex(i);
friendProp fp; friendProp fp{ps.value("addr").toString()};
fp.addr = ps.value("addr").toString();
fp.alias = ps.value("alias").toString(); fp.alias = ps.value("alias").toString();
fp.note = ps.value("note").toString(); fp.note = ps.value("note").toString();
fp.autoAcceptDir = ps.value("autoAcceptDir").toString(); fp.autoAcceptDir = ps.value("autoAcceptDir").toString();
@ -370,7 +369,7 @@ void Settings::loadPersonal(Profile* profile)
if (getEnableLogging()) if (getEnableLogging())
fp.activity = ps.value("activity", QDate()).toDate(); fp.activity = ps.value("activity", QDate()).toDate();
friendLst[ToxId(fp.addr).getPublicKey().getKey()] = fp; friendLst.insert(ToxId(fp.addr).getPublicKey().getKey(), fp);
} }
ps.endArray(); ps.endArray();
} }
@ -1398,15 +1397,10 @@ void Settings::setAutoAcceptDir(const ToxPk& id, const QString& dir)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it == friendLst.end()) {
updateFriendAddress(id.toString());
setAutoAcceptDir(id, dir);
return;
}
if (it->autoAcceptDir != dir) { if (frnd.autoAcceptDir != dir) {
it->autoAcceptDir = dir; frnd.autoAcceptDir = dir;
emit autoAcceptDirChanged(id, dir); emit autoAcceptDirChanged(id, dir);
} }
} }
@ -1426,15 +1420,10 @@ void Settings::setAutoAcceptCall(const ToxPk& id, AutoAcceptCallFlags accept)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it == friendLst.end()) {
updateFriendAddress(id.toString());
setAutoAcceptCall(id, accept);
return;
}
if (it->autoAcceptCall != accept) { if (frnd.autoAcceptCall != accept) {
it->autoAcceptCall = accept; frnd.autoAcceptCall = accept;
emit autoAcceptCallChanged(id, accept); emit autoAcceptCallChanged(id, accept);
} }
} }
@ -1455,15 +1444,10 @@ void Settings::setAutoGroupInvite(const ToxPk& id, bool accept)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it == friendLst.end()) {
updateFriendAddress(id.toString());
setAutoGroupInvite(id, accept);
return;
}
if (it->autoGroupInvite != accept) { if (frnd.autoGroupInvite != accept) {
it->autoGroupInvite = accept; frnd.autoGroupInvite = accept;
emit autoGroupInviteChanged(id, accept); emit autoGroupInviteChanged(id, accept);
} }
} }
@ -1483,15 +1467,10 @@ void Settings::setContactNote(const ToxPk& id, const QString& note)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it == friendLst.end()) {
updateFriendAddress(id.toString());
setContactNote(id, note);
return;
}
if (it->note != note) { if (frnd.note != note) {
it->note = note; frnd.note = note;
emit contactNoteChanged(id, note); emit contactNoteChanged(id, note);
} }
} }
@ -2071,18 +2050,9 @@ void Settings::updateFriendAddress(const QString& newAddr)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
// TODO: using ToxId here is a hack // TODO: using ToxId here is a hack
QByteArray key = ToxId(newAddr).getPublicKey().getKey(); auto key = ToxId(newAddr).getPublicKey();
auto it = friendLst.find(key); auto& frnd = getOrInsertFriendPropRef(key);
if (it != friendLst.end()) { frnd.addr = newAddr;
it->addr = newAddr;
} else {
friendProp fp;
fp.addr = newAddr;
fp.alias = "";
fp.note = "";
fp.autoAcceptDir = "";
friendLst[key] = fp;
}
} }
QString Settings::getFriendAlias(const ToxPk& id) const QString Settings::getFriendAlias(const ToxPk& id) const
@ -2098,17 +2068,8 @@ QString Settings::getFriendAlias(const ToxPk& id) const
void Settings::setFriendAlias(const ToxPk& id, const QString& alias) void Settings::setFriendAlias(const ToxPk& id, const QString& alias)
{ {
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it != friendLst.end()) { frnd.alias = alias;
it->alias = alias;
} else {
friendProp fp;
fp.addr = id.toString();
fp.alias = alias;
fp.note = "";
fp.autoAcceptDir = "";
friendLst[id.getKey()] = fp;
}
} }
int Settings::getFriendCircleID(const ToxPk& id) const int Settings::getFriendCircleID(const ToxPk& id) const
@ -2122,18 +2083,8 @@ int Settings::getFriendCircleID(const ToxPk& id) const
void Settings::setFriendCircleID(const ToxPk& id, int circleID) void Settings::setFriendCircleID(const ToxPk& id, int circleID)
{ {
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it != friendLst.end()) { frnd.circleID = circleID;
it->circleID = circleID;
} else {
friendProp fp;
fp.addr = id.toString();
fp.alias = "";
fp.note = "";
fp.autoAcceptDir = "";
fp.circleID = circleID;
friendLst[id.getKey()] = fp;
}
} }
QDate Settings::getFriendActivity(const ToxPk& id) const QDate Settings::getFriendActivity(const ToxPk& id) const
@ -2147,19 +2098,8 @@ QDate Settings::getFriendActivity(const ToxPk& id) const
void Settings::setFriendActivity(const ToxPk& id, const QDate& activity) void Settings::setFriendActivity(const ToxPk& id, const QDate& activity)
{ {
auto it = friendLst.find(id.getKey()); auto& frnd = getOrInsertFriendPropRef(id);
if (it != friendLst.end()) { frnd.activity = activity;
it->activity = activity;
} else {
friendProp fp;
fp.addr = id.toString();
fp.alias = "";
fp.note = "";
fp.autoAcceptDir = "";
fp.circleID = -1;
fp.activity = activity;
friendLst[id.getKey()] = fp;
}
} }
void Settings::saveFriendSettings(const ToxPk& id) void Settings::saveFriendSettings(const ToxPk& id)
@ -2463,3 +2403,15 @@ void Settings::sync()
QMutexLocker locker{&bigLock}; QMutexLocker locker{&bigLock};
qApp->processEvents(); qApp->processEvents();
} }
Settings::friendProp& Settings::getOrInsertFriendPropRef(const ToxPk& id)
{
// No mutex lock, this is a private fn that should only be called by other
// public functions that already locked the mutex
auto it = friendLst.find(id.getKey());
if (it == friendLst.end()) {
it = friendLst.insert(id.getKey(), friendProp{id.toString()});
}
return *it;
}

View File

@ -568,11 +568,14 @@ public:
static uint32_t makeProfileId(const QString& profile); static uint32_t makeProfileId(const QString& profile);
private: private:
struct friendProp;
Settings(); Settings();
~Settings(); ~Settings();
Settings(Settings& settings) = delete; Settings(Settings& settings) = delete;
Settings& operator=(const Settings&) = delete; Settings& operator=(const Settings&) = delete;
void savePersonal(QString profileName, const ToxEncrypt* passkey); void savePersonal(QString profileName, const ToxEncrypt* passkey);
friendProp& getOrInsertFriendPropRef(const ToxPk& id);
public slots: public slots:
void savePersonal(Profile* profile); void savePersonal(Profile* profile);
@ -684,10 +687,14 @@ private:
struct friendProp struct friendProp
{ {
QString alias; friendProp() = delete;
QString addr; friendProp(QString addr)
QString autoAcceptDir; : addr(addr)
QString note; {}
QString alias = "";
QString addr = "";
QString autoAcceptDir = "";
QString note = "";
int circleID = -1; int circleID = -1;
QDate activity = QDate(); QDate activity = QDate();
AutoAcceptCallFlags autoAcceptCall; AutoAcceptCallFlags autoAcceptCall;