-
Notifications
You must be signed in to change notification settings - Fork 0
/
qfetioncontacts.cpp
519 lines (437 loc) · 17.3 KB
/
qfetioncontacts.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "qfetioncontacts.h"
#include <QFile>
QFetionContacts::QFetionContacts(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = "name";
roleNames[MobileRole] = "phone";
roleNames[GroupRole] = "group";
roleNames[SectionRole] = "section";
roleNames[SidRole] = "sid";
roleNames[UserIdRole] = "userid";
roleNames[ImageRole] = "image";
roleNames[ShowRole] = "show";
setRoleNames(roleNames);
contactThread = new QFetionContactThread(this);
//connect(this,SIGNAL(doThread(QFetionContacts::DoThread)),contactThread,SLOT(doThread(QFetionContacts::DoThread)),Qt::QueuedConnection);
connect(this,SIGNAL(doThread(QFetionContacts::DoThread)),contactThread,SLOT(doThread(QFetionContacts::DoThread)));
contactThread->start();
mutex.lock();
connect(this,SIGNAL(groupShowChanged(int)),this,SLOT(groupStateChanged(int)));
reloadContacts();
}
void QFetionContacts::reloadContacts(){
QSqlQuery query;
QSqlQuery queryContacts;
if(!query.exec("select mobileno,password,nickname from user"))
qDebug() << "Get user information error!";
else
{
query.next();
nickname = query.value(2).toString();
password = query.value(1).toString();
mobileno = query.value(0).toString();
emit passwordChanged();
emit nicknameChanged();
emit mobilenoChanged();
}
beginResetModel();
int groupId;
if(!query.exec("select * from groups order by groupid"))
qDebug() <<"Select from groups error!";
while(query.next())
{
groupId = query.value(0).toInt();
groupsList[groupId] = QFetionGroups(query.value(1).toString());
QString queryString("select sId,userId,localname,nickname,mobileno from contacts_2_2_0 where groupids='");
if(!queryContacts.exec(queryString.append(query.value(0).toString()).append("' order by localname,nickname")))
qDebug() <<"select contacts from " << groupsList[groupId].group<<" error ";
while(queryContacts.next())
{
if (queryContacts.value(4).toString()!="")
contactsList.append(QFetionContact(queryContacts.value(0).toString(),queryContacts.value(1).toString(),
(queryContacts.value(2).toString()=="") ? queryContacts.value(3).toString() :
queryContacts.value(2).toString(),
queryContacts.value(4).toString(),groupId));
}
}
endResetModel();
}
void QFetionContacts::classBegin(){
qDebug() <<"QFetionContacts Class begin";
}
void QFetionContacts::componentComplete(){
qDebug() <<"QFetionContacts completed!";
}
void QFetionContacts::groupStateChanged(int groupid){
int from = -1,end = -1,count = contactsList.count();
for (int i= 0;i <count;i++)
{
if(contactsList.at(i).groupId == groupid)
{
if (from == -1)
from = i;
end = i;
}else if (from != -1)
break;
}
QModelIndex idx_from = index(from, 0);
QModelIndex idx_end = index(end,0);
emit dataChanged(idx_from,idx_end);
}
QVariant QFetionContacts::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QString();
if (index.row()>rowCount())
return QString();
QFetionContact contact = contactsList.at(index.row());
QString file("/home/user/.fetion/icons/");
switch(role)
{
case NameRole:
return contact.name;
break;
case MobileRole:
return contact.mobileno;
break;
case GroupRole:
return contact.groupId;
break;
case SectionRole:
return groupsList[contact.groupId].group;
break;
case SidRole:
return contact.sid;
break;
case UserIdRole:
return contact.userId;
break;
case ImageRole:
file.append(contact.sid).append(".jpg");
if (QFile(file).exists())
return file.insert(0,"file:");
else
return QString("images/default.jpg");
break;
case ShowRole:
return groupsList[contact.groupId].show;
break;
default:
break;
}
return QString();
}
int QFetionContacts::rowCount(const QModelIndex &parent ) const{
return contactsList.count();
}
void QFetionContacts::fx_process_presence(User* user , const char* xml)
{
Contact *contactlist;
Contact *contact;
contactlist = fetion_user_parse_presence_body(xml , user);
contact = contactlist;
foreach_contactlist(contactlist , contact){
if (contact->imageChanged)
fetion_user_download_portrait(user , contact->sipuri);
incPresenceCount();
}
}
void QFetionContacts::fx_process_notifications(User* user , const char* sipmsg)
{
int event;
int notification_type;
char *xml;
fetion_sip_parse_notification(sipmsg , ¬ification_type , &event , &xml);
usleep(1);
switch(notification_type)
{
case NOTIFICATION_TYPE_PRESENCE:
switch(event)
{
case NOTIFICATION_EVENT_PRESENCECHANGED :
fx_process_presence(user , xml);
break;
default:
break;
}
case NOTIFICATION_TYPE_CONVERSATION :
if(event == NOTIFICATION_EVENT_USERLEFT){
printf("\nuser left\n");
break;
}
break;
case NOTIFICATION_TYPE_REGISTRATION :
if(event == NOTIFICATION_EVENT_DEREGISTRATION){
printf("\nuser deregistration\n");
break;
}
break;
case NOTIFICATION_TYPE_SYNCUSERINFO :
if(event == NOTIFICATION_EVENT_SYNCUSERINFO){
printf("\nuser sync user info\n");
break;
}
break;
case NOTIFICATION_TYPE_CONTACT :
if(event == NOTIFICATION_EVENT_ADDBUDDYAPPLICATION){
printf("\nuser add bubby \n");
break;
}
break;
case NOTIFICATION_TYPE_PGGROUP :
if(event == NOTIFICATION_EVENT_PGGETGROUPINFO){
printf("\nget group info \n");
break;
}
if(event == NOTIFICATION_EVENT_PRESENCECHANGED){
printf("\npresence changed \n");
break;
}
break;
default:
break;
}
free(xml);
}
void* QFetionContacts::fx_listen_func(User* user)
{
FetionSip *sip;
SipMsg *msg;
SipMsg *pos;
int type;
int ret;
int error;
struct timeval tv;
debug_info("A new thread entered");
sip = user->sip;
Contact *contactlist;
Contact *contact;
user->contactCount = 0;
contactlist = contact = user->contactList;
foreach_contactlist(contactlist , contact){
user->contactCount++;
}
setTotalContacts(user->contactCount);
// printf("contactList count %d\n",user->contactCount);
for(;;){
FD_ZERO(&fd_read);
if(!sip || !sip->tcp){
debug_info("thread exited");
exit(-1);
}
FD_SET(sip->tcp->socketfd, &fd_read);
tv.tv_sec = 13;
tv.tv_usec = 0;
ret = select(sip->tcp->socketfd+1, &fd_read, NULL, NULL, &tv);
if(ret == 0)
continue;
if (ret == -1) {
debug_info ("Error.. to read socket %d,exit thread",
sip->tcp->socketfd);
if(sip != user->sip){
debug_info("Error.. thread sip freed\n");
free(sip);
}
exit(-1);
}
if (!FD_ISSET (sip->tcp->socketfd, &fd_read)) {
usleep (100);
continue;
}
msg = fetion_sip_listen(sip, &error);
if(!msg && error){
/* if it is the main listening thread */
if(sip == user->sip){
printf("\n\nError ...... break out...\n\n");
exit(-1);
}else{
printf("\n\n Error ... user listen thread break out\n\n");
tcp_connection_free(sip->tcp);
exit(-1);
}
}
pos = msg;
while(pos){
type = fetion_sip_get_type(pos->message);
switch(type){
case SIP_NOTIFICATION :
fx_process_notifications(user , pos->message);
break;
case SIP_MESSAGE:
printf("\n\n sip message\n\n");
printf("%s\n" , pos->message);
break;
case SIP_INVITATION:
printf("\n\n sip invitation\n\n");
printf("%s\n" , pos->message);
break;
case SIP_INCOMING :
printf("\n\n sip incoming\n\n");
printf("%s\n" , pos->message);
break;
case SIP_SIPC_4_0:
printf("\n\n sip 4.0\n\n");
printf("%s\n" , pos->message);
break;
default:
printf("%s\n" , pos->message);
break;
}
pos = pos->next;
}
if(msg)
fetion_sip_message_free(msg);
// printf("presence_count %d\n" , presence_count);
// setPresenceCount(presence_count);
if (presence_count >= user->contactCount)
break;
}
return NULL;
}
int QFetionContacts::fx_login(const char *mobileno, const char *password)
{
Config *config;
FetionConnection *tcp;
FetionSip *sip;
char *res;
char *nonce;
char *key;
char *aeskey;
char *response;
int local_group_count;
int local_buddy_count;
int group_count;
int buddy_count;
int ret;
presence_count = 0;
/* construct a user object */
user = fetion_user_new(mobileno, password);
/* construct a config object */
config = fetion_config_new();
/* attach config to user */
fetion_user_set_config(user, config);
/* start ssi authencation,result string needs to be freed after use */
setSyncStatusMessage("Start ssi auth...");
login:
res = ssi_auth_action(user);
if (!res)
{
setSyncStatusMessage("Login Failed");
return 1;
}
/* parse the ssi authencation result,if success,user's sipuri and userid
* are stored in user object,orelse user->loginStatus was marked failed */
parse_ssi_auth_response(res, user);
free(res);
/* whether needs to input a confirm code,or login failed
* for other reason like password error */
if(USER_AUTH_NEED_CONFIRM(user)){
debug_info(user->verification->text);
debug_info(user->verification->tips);
setSyncStatusMessage("Getting code picture,please wait...");
generate_pic_code(user);
authCode = "";
emit sync_Need_Auth();
waitCondition.wait(&mutex);
if (authCode=="")
{
setSyncStatusMessage("Cancel code authing...");\
return 1;
}
fetion_user_set_verification_code(user , authCode.toUtf8().data());
goto login;
}
if(USER_AUTH_ERROR(user)) {
setSyncStatusMessage("Incorrect cell phone number or password");
debug_error("authencation failed");
return 1;
}
/* initialize configuration for current user */
if(fetion_user_init_config(user) == -1) {
debug_error("initialize configuration");
return 1;
}
setSyncStatusMessage("Downloading configuration files");
if(fetion_config_download_configuration(user) == -1) {
setSyncStatusMessage("Connection has been shutdown by the server");
debug_error("download configuration");
return 1;
}
/* set user's login state to be hidden */
fetion_user_set_st(user, P_HIDDEN);
/* load user information and contact list information from local host */
fetion_user_load(user);
fetion_contact_load(user, &local_group_count, &local_buddy_count);
/* construct a tcp object and connect to the sipc proxy server */
setSyncStatusMessage("Connecting to registration server");
tcp = tcp_connection_new();
if((ret = tcp_connection_connect(tcp, config->sipcProxyIP, config->sipcProxyPort)) == -1) {
debug_error("connect sipc server %s:%d\n", config->sipcProxyIP, config->sipcProxyPort);
setSyncStatusMessage("Login failed");
return 1;
}
/* construct a sip object with the tcp object and attach it to user object */
sip = fetion_sip_new(tcp, user->sId);
fetion_user_set_sip(user, sip);
setSyncStatusMessage("Registering to SIPC Server");
/* register to sipc server */
if(!(res = sipc_reg_action(user))) {
debug_error("register to sipc server");
setSyncStatusMessage("Login failed");
return 1;
}
parse_sipc_reg_response(res, &nonce, &key);
free(res);
aeskey = generate_aes_key();
response = generate_response(nonce, user->userId, user->password, key, aeskey);
free(nonce);
free(key);
free(aeskey);
setSyncStatusMessage("SIPC Indentify");
auth:
/* sipc authencation,you can printf res to see what you received */
if(!(res = sipc_aut_action(user, response))) {
debug_error("sipc authencation");
setSyncStatusMessage("Login failed");
return 1;
}
if(parse_sipc_auth_response(res, user, &group_count, &buddy_count) == -1) {
debug_error("authencation failed");
setSyncStatusMessage("Authenticate failed.");
return 1;
}
free(res);
free(response);
if(USER_AUTH_ERROR(user)) {
setSyncStatusMessage("Authenticate failed.");
debug_error("login failed");
return 1;
}
if(USER_AUTH_NEED_CONFIRM(user)){
debug_info(user->verification->text);
debug_info(user->verification->tips);
setSyncStatusMessage("Getting code picture,please wait...");
generate_pic_code(user);
authCode = "";
emit sync_Need_Auth();
waitCondition.wait(&mutex);
if (authCode=="")
{
setSyncStatusMessage("Cancel code authing...");\
return 1;
}
fetion_user_set_verification_code(user , authCode.toUtf8().data());
goto auth;
}
setSyncStatusMessage("Downloading contacts...");
fetion_contact_subscribe_only(user);
fx_listen_func(user);
/* save the user information and contact list information back to the local database */
setSyncStatusMessage("Save contacts");
fetion_user_save(user);
fetion_contact_save(user);
fetion_user_free(user);
return 0;
}