-
Notifications
You must be signed in to change notification settings - Fork 2
/
druserscontroller.cpp
215 lines (132 loc) · 6.03 KB
/
druserscontroller.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Deriv is a cross-platform client for th Wired 2.0 protocol
Copyright (C) 2014 Rafael Warnault, rw@read-write.fr
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QVector>
#include <QApplication>
#include <QDebug>
#include "druserscontroller.h"
#include "drpreferenceswindow.h"
#pragma mark -
DRUsersController::DRUsersController(DRServerConnection *connection) :
DRConnectionController(connection)
{
this->users = new QVector<DRUser*>();
this->connection->addDelegateForMessage(this, "wired.chat.user_list");
this->connection->addDelegateForMessage(this, "wired.chat.user_list.done");
this->connection->addDelegateForMessage(this, "wired.chat.user_join");
this->connection->addDelegateForMessage(this, "wired.chat.user_leave");
this->connection->addDelegateForMessage(this, "wired.chat.user_status");
}
DRUsersController::~DRUsersController() {
}
void DRUsersController::connectReceiver(QObject *object) {
QObject::connect(this, SIGNAL(usersControllerUserListLoaded(DRServerConnection *)), object, SLOT(usersControllerUserListLoaded(DRServerConnection *)));
QObject::connect(this, SIGNAL(usersControllerUserJoined(DRServerConnection *, DRUser*)), object, SLOT(usersControllerUserJoined(DRServerConnection *, DRUser*)));
QObject::connect(this, SIGNAL(usersControllerUserLeave(DRServerConnection *, DRUser*)), object, SLOT(usersControllerUserLeave(DRServerConnection *, DRUser*)));
}
void DRUsersController::disconnectReceiver(QObject *object) {
QObject::disconnect(this, SIGNAL(usersControllerUserListLoaded(DRServerConnection *)), object, SLOT(usersControllerUserListLoaded(DRServerConnection *)));
QObject::disconnect(this, SIGNAL(usersControllerUserJoined(DRServerConnection *, DRUser*)), object, SLOT(usersControllerUserJoined(DRServerConnection *, DRUser*)));
QObject::disconnect(this, SIGNAL(usersControllerUserLeave(DRServerConnection *, DRUser*)), object, SLOT(usersControllerUserLeave(DRServerConnection *, DRUser*)));
}
#pragma mark -
DRUser* DRUsersController::userAtIndex(int index) {
return this->users->at(index);
}
DRUser* DRUsersController::userWithID(wi_p7_int32_t userID) {
QVectorIterator<DRUser*> i(*this->users);
while (i.hasNext()) {
DRUser *user = i.next();
if(user->userID == userID)
return user;
}
return NULL;
}
#pragma mark -
void DRUsersController::connectionSucceeded(DRServerConnection *connection) {
}
void DRUsersController::connectionError(DRServerConnection *connection, DRError *error) {
}
void DRUsersController::connectionClosed(DRServerConnection *connection, DRError *error) {
}
void DRUsersController::receivedMessage(wi_p7_message_t *message, DRServerConnection *connection) {
qDebug() << wi_string_cstring(wi_p7_message_name(message));
if(QThread::currentThread() == QCoreApplication::instance()->thread())
qDebug() << "is On Main Thread";
else
qDebug() << "is Not On Main Thread";
if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_list"))) {
this->receivedWiredUserList(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_list.done"))) {
this->receivedWiredUserList(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_join"))) {
this->receivedWiredUserJoin(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_leave"))) {
this->receivedWiredUserLeave(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_status"))) {
this->receivedWiredUserStatus(message);
}
}
void DRUsersController::receivedError(DRError *error, DRServerConnection *connection) {
}
#pragma mark -
void DRUsersController::receivedWiredUserList(wi_p7_message_t *message) {
DRUser *user;
if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_list"))) {
user = new DRUser(message);
this->users->push_back(user);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.user_list.done"))) {
emit usersControllerUserListLoaded(this->connection);
}
}
void DRUsersController::receivedWiredUserJoin(wi_p7_message_t *message) {
DRUser *user;
user = new DRUser(message);
this->users->push_back(user);
emit usersControllerUserJoined(this->connection, user);
emit usersControllerUserListLoaded(this->connection);
}
void DRUsersController::receivedWiredUserLeave(wi_p7_message_t *message) {
DRUser *user;
wi_p7_int32_t userID;
wi_p7_message_get_int32_for_name(message, &userID, WI_STR("wired.user.id"));
if(userID < 0)
return;
user = this->userWithID(userID);
if(user == NULL)
return;
int index = this->users->indexOf(user);
if(index < 0)
return;
this->users->remove(index);
emit usersControllerUserListLoaded(this->connection);
emit usersControllerUserLeave(this->connection, user);
}
void DRUsersController::receivedWiredUserStatus(wi_p7_message_t *message) {
DRUser *user;
wi_p7_int32_t userID;
wi_p7_message_get_int32_for_name(message, &userID, WI_STR("wired.user.id"));
if(userID < 0)
return;
user = this->userWithID(userID);
if(user == NULL)
return;
user->setObjectWithMessage(message);
emit usersControllerUserListLoaded(this->connection);
}