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

GroupList refactoring

This commit is contained in:
apprb 2014-11-20 18:13:13 +09:00
parent 1ec67cb100
commit 92c330b7bd
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
6 changed files with 46 additions and 23 deletions

View File

@ -61,6 +61,7 @@ void FriendList::clear()
{
for (auto friendptr : friendList)
delete friendptr;
friendList.clear();
}
Friend* FriendList::findFriend(const ToxID& userId)

View File

@ -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);

View File

@ -16,32 +16,54 @@
#include "grouplist.h"
#include "group.h"
#include <QHash>
#include <QDebug>
QList<Group*> GroupList::groupList;
QHash<int, Group*> 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; i<groupList.size(); i++)
auto g_it = groupList.find(groupId);
if (g_it != groupList.end())
{
if (groupList[i]->groupId == groupId)
{
groupList.removeAt(i);
return;
}
groupList.erase(g_it);
}
}
QList<Group *> GroupList::getAllGroups()
{
QList<Group*> res;
for (auto it : groupList)
res.append(it);
return res;
}
void GroupList::clear()
{
for (auto groupptr : groupList)
delete groupptr;
groupList.clear();
}

View File

@ -17,21 +17,22 @@
#ifndef GROUPLIST_H
#define GROUPLIST_H
template <typename T>
class QList;
template <class A, class B> class QHash;
template <class T> 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<Group*> getAllGroups();
static void clear();
public:
static QList<Group*> groupList;
private:
static QHash<int, Group*> groupList;
};
#endif // GROUPLIST_H

View File

@ -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<QAction*, Group*> groupActions;
for (Group* group : GroupList::groupList)
for (Group* group : GroupList::getAllGroups())
{
QAction* groupAction = inviteMenu->addAction(group->widget->getName());
groupActions[groupAction] = group;

View File

@ -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<Friend*> friends = FriendList::getAllFriends();
for (Friend* f : friends)
removeFriend(f, true);
for (Group* g : GroupList::groupList)
QList<Group*> 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();
}