-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrchatcontroller.cpp
179 lines (107 loc) · 5.6 KB
/
drchatcontroller.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
/*
Deriv is a cross-platform client for th Wired 2.0 protocol
Copyright (C) 2014 Rafael Warnault, [email protected]
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 <QDebug>
#include "drchatcontroller.h"
#include "druserscontroller.h"
#pragma mark -
DRChatController::DRChatController(DRServerConnection *connection) :
DRConnectionController(connection)
{
this->topic = NULL;
this->chatBuffer = new QString("");
// QObject::connect(this->connection, SIGNAL(connectionClosed(DRConnection*,DRError*)),
// this, SLOT(connectionClosed(DRConnection*,DRError*)));
// QObject::connect(this->connection, SIGNAL(receivedMessage(wi_p7_message_t*)),
// this, SLOT(receivedMessage(wi_p7_message_t*)));
this->connection->addDelegateForMessage(this, "wired.chat.say");
this->connection->addDelegateForMessage(this, "wired.chat.me");
this->connection->addDelegateForMessage(this, "wired.chat.topic");
}
DRChatController::~DRChatController() {
if(this->topic != NULL)
delete this->topic, this->topic = NULL;
if(this->chatBuffer)
delete this->chatBuffer, this->chatBuffer = NULL;
}
void DRChatController::connectReceiver(QObject *object) {
QObject::connect(this, SIGNAL(chatControllerReceivedChatMe(DRServerConnection*,QString,DRUser*)), object, SLOT(chatControllerReceivedChatMe(DRServerConnection*,QString,DRUser*)));
QObject::connect(this, SIGNAL(chatControllerReceivedChatSay(DRServerConnection*,QString,DRUser*)), object, SLOT(chatControllerReceivedChatSay(DRServerConnection*,QString,DRUser*)));
QObject::connect(this, SIGNAL(chatControllerTopicChanged(DRServerConnection*,DRTopic*,bool)), object, SLOT(chatControllerTopicChanged(DRServerConnection*,DRTopic*,bool)));
}
void DRChatController::disconnectReceiver(QObject *object) {
QObject::disconnect(this, SIGNAL(chatControllerReceivedChatMe(DRServerConnection*,QString,DRUser*)), object, SLOT(chatControllerReceivedChatMe(DRServerConnection*,QString,DRUser*)));
QObject::disconnect(this, SIGNAL(chatControllerReceivedChatSay(DRServerConnection*,QString,DRUser*)), object, SLOT(chatControllerReceivedChatSay(DRServerConnection*,QString,DRUser*)));
QObject::disconnect(this, SIGNAL(chatControllerTopicChanged(DRServerConnection*,DRTopic*,bool)), object, SLOT(chatControllerTopicChanged(DRServerConnection*,DRTopic*,bool)));
}
#pragma mark -
void DRChatController::connectionSucceeded(DRServerConnection *connection) {
}
void DRChatController::connectionError(DRServerConnection *connection, DRError *error) {
}
void DRChatController::connectionClosed(DRServerConnection *connection, DRError *error) {
}
void DRChatController::receivedMessage(wi_p7_message_t *message, DRServerConnection *connection) {
if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.say"))) {
this->receivedWiredChatSay(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.me"))) {
this->receivedWiredChatMe(message);
}
else if(wi_is_equal(wi_p7_message_name(message), WI_STR("wired.chat.topic"))) {
this->receivedWiredChatTopic(message);
}
}
void DRChatController::receivedError(DRError *error, DRServerConnection *connection) {
}
#pragma mark -
void DRChatController::receivedWiredChatSay(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->connection->usersController->userWithID(userID);
if(user == NULL)
return;
wi_string_t *wstring = wi_p7_message_string_for_name(message, WI_STR("wired.chat.say"));
if(!wstring || wi_string_length(wstring) <= 0)
return;
QString qstring = QString("%1 : %2").arg(user->nick, QString(wi_string_cstring(wstring)));
emit chatControllerReceivedChatSay(this->connection, qstring, user);
}
void DRChatController::receivedWiredChatMe(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->connection->usersController->userWithID(userID);
if(user == NULL)
return;
wi_string_t *wstring = wi_p7_message_string_for_name(message, WI_STR("wired.chat.me"));
if(!wstring || wi_string_length(wstring) <= 0)
return;
QString qstring = QString("*** %1 %2").arg(user->nick, QString(wi_string_cstring(wstring)));
emit chatControllerReceivedChatMe(this->connection, qstring, user);
}
void DRChatController::receivedWiredChatTopic(wi_p7_message_t *message) {
if(this->topic == NULL) {
this->topic = new DRTopic(message);
emit chatControllerTopicChanged(this->connection, this->topic, true);
} else {
this->topic->setObjectWithMessage(message);
emit chatControllerTopicChanged(this->connection, this->topic, false);
}
}