diff --git a/src/friendlist.cpp b/src/friendlist.cpp index d75da5796..4429215e5 100644 --- a/src/friendlist.cpp +++ b/src/friendlist.cpp @@ -61,6 +61,7 @@ void FriendList::clear() { for (auto friendptr : friendList) delete friendptr; + friendList.clear(); } Friend* FriendList::findFriend(const ToxID& userId) diff --git a/src/friendlist.h b/src/friendlist.h index 6d41d44ce..9e6807197 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -26,7 +26,6 @@ struct ToxID; class FriendList { public: - FriendList(); static Friend* addFriend(int friendId, const ToxID &userId); static Friend* findFriend(int friendId); static Friend* findFriend(const ToxID &userId); diff --git a/src/grouplist.cpp b/src/grouplist.cpp index 72b7cf872..52ea2f66f 100644 --- a/src/grouplist.cpp +++ b/src/grouplist.cpp @@ -16,32 +16,54 @@ #include "grouplist.h" #include "group.h" +#include +#include -QList GroupList::groupList; +QHash GroupList::groupList; Group* GroupList::addGroup(int groupId, const QString& name, bool isAvGroupchat) { + auto checker = groupList.find(groupId); + if (checker != groupList.end()) + qWarning() << "GroupList::addGroup: groupId already taken"; + Group* newGroup = new Group(groupId, name, isAvGroupchat); - groupList.append(newGroup); + groupList[groupId] = newGroup; + return newGroup; } Group* GroupList::findGroup(int groupId) { - for (Group* g : groupList) - if (g->groupId == groupId) - return g; + auto g_it = groupList.find(groupId); + if (g_it != groupList.end()) + return *g_it; + return nullptr; } void GroupList::removeGroup(int groupId, bool /*fake*/) { - for (int i=0; igroupId == groupId) - { - groupList.removeAt(i); - return; - } + groupList.erase(g_it); } } + +QList GroupList::getAllGroups() +{ + QList res; + + for (auto it : groupList) + res.append(it); + + return res; +} + +void GroupList::clear() +{ + for (auto groupptr : groupList) + delete groupptr; + groupList.clear(); +} diff --git a/src/grouplist.h b/src/grouplist.h index 36d8281f7..39fd66508 100644 --- a/src/grouplist.h +++ b/src/grouplist.h @@ -17,21 +17,22 @@ #ifndef GROUPLIST_H #define GROUPLIST_H -template -class QList; +template class QHash; +template class QList; class Group; class QString; class GroupList { public: - GroupList(); static Group* addGroup(int groupId, const QString& name, bool isAvGroupchat); static Group* findGroup(int groupId); static void removeGroup(int groupId, bool fake = false); + static QList getAllGroups(); + static void clear(); -public: - static QList groupList; +private: + static QHash groupList; }; #endif // GROUPLIST_H diff --git a/src/widget/friendwidget.cpp b/src/widget/friendwidget.cpp index 7d675a8c6..e06568078 100644 --- a/src/widget/friendwidget.cpp +++ b/src/widget/friendwidget.cpp @@ -56,7 +56,7 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event) QAction* copyId = menu.addAction(tr("Copy friend ID","Menu to copy the Tox ID of that friend")); QMap groupActions; - for (Group* group : GroupList::groupList) + for (Group* group : GroupList::getAllGroups()) { QAction* groupAction = inviteMenu->addAction(group->widget->getName()); groupActions[groupAction] = group; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 3caaecd44..cc3afb3ce 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -297,9 +297,7 @@ Widget::~Widget() delete filesForm; FriendList::clear(); - for (Group* g : GroupList::groupList) - delete g; - GroupList::groupList.clear(); + GroupList::clear(); delete trayMenu; delete ui; delete translator; @@ -878,7 +876,9 @@ void Widget::clearContactsList() QList friends = FriendList::getAllFriends(); for (Friend* f : friends) removeFriend(f, true); - for (Group* g : GroupList::groupList) + + QList groups = GroupList::getAllGroups(); + for (Group* g : groups) removeGroup(g, true); } @@ -1213,6 +1213,6 @@ void Widget::reloadTheme() for (Friend* f : FriendList::getAllFriends()) f->getFriendWidget()->reloadTheme(); - for (Group* g : GroupList::groupList) + for (Group* g : GroupList::getAllGroups()) g->widget->reloadTheme(); }