This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qUserList.cpp
118 lines (99 loc) · 2.84 KB
/
qUserList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "qUserList.h"
#include "qchat_pch.h"
#include "qChat.h"
#include "qConfig.h"
#include "qUser.h"
QUserListModel userList;
QUserListModel::QUserListModel(QObject *parent) :
QAbstractListModel(parent)
{
}
Qt::ItemFlags QUserListModel::flags ( const QModelIndex & /*index*/ ) const
{
return (Qt::ItemIsSelectable | Qt::ItemIsEnabled);
}
QVariant QUserListModel::data ( const QModelIndex & index, int role) const
{
int r = index.row();
QHash<QString, qUser*>::iterator i(userList.begin());
i+=r;
qUser* u = *i;
switch (role)
{
case Qt::DisplayRole:
return u->nick;
break;
case Qt::ToolTipRole:
return u->address.toString();
break;
case Qt::DecorationRole:
return statusIcon(u->status);
break;
}
return QVariant::Invalid;
}
int QUserListModel::rowCount ( const QModelIndex & /*parent*/ ) const
{
return userList.count();
}
QVariant QUserListModel::headerData ( int /*section*/, Qt::Orientation /*orientation*/, int /*role*/ ) const
{
return QString("");
}
void QUserListModel::updateUser(QHostAddress addr, QString nick, userStatus us)
{
qUser* user = 0;
bool adding = false;
if(userList.contains(addr.toString()))
user = userList.value(addr.toString());
else
{
beginInsertRows(index(0),0,0);
user = new qUser;
adding=true;
}
user->address = addr;
user->lastCheck = QDateTime::currentDateTime();
user->nick = nick;
if(((!adding) && (user->status == usOffline)) || adding)
emit nowOnline(user); //вызываем сигнал при добавлении нового или возврате в онлайн старого пользователя
if(user->status != us)
{
user->status = us;
emit statusChanged(user);
}
userList[addr.toString()] = user;
if(adding)
endInsertRows();
else
emit dataChanged(QModelIndex(),QModelIndex());
}
void QUserListModel::removeUser(QHostAddress addr)
{
if(!userList.contains(addr.toString())) return;
userList.value(addr.toString())->status = usOffline;
emit nowOffline(userList.value(addr.toString()));
emit dataChanged(QModelIndex(),QModelIndex());
}
void QUserListModel::clearOfflineUsers()
{
QHash<QString, qUser*>::iterator i = userList.begin();
while (i != userList.end()) {
qUser* u = *i;
if(u->status!=usOffline)
if (u->lastCheck.msecsTo(QDateTime::currentDateTime())>=20000)
{ //20 секунд неактивности
u->status=usOffline;
emit nowOffline(u);
}
++i;
}
emit dataChanged(QModelIndex(),QModelIndex());
}
qUser* QUserListModel::operator[] (const QString& n)
{
if(userList.contains(n))
return userList.value(n);
else
return NULL;
}