~neon/kdenetwork/trunk

« back to all changes in this revision

Viewing changes to kopete/protocols/gadu/gadusession.cpp

  • Committer: uwolfer
  • Date: 2013-06-08 10:12:41 UTC
  • Revision ID: svn-v4:283d02a7-25f6-0310-bc7c-ecb5cbfe19da:trunk/KDE/kdenetwork:1357331
kdenetwork has moved to Git

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set noet ts=4 sts=4 sw=4 :
2
 
// -*- Mode: c++-mode; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 2; -*-
3
 
//
4
 
// Copyright (C) 2003-2004       Grzegorz Jaskiewicz <gj at pointblue.com.pl>
5
 
// Copyright (C) 2002           Zack Rusin <zack@kde.org>
6
 
//
7
 
// gadusession.cpp
8
 
//
9
 
// This program is free software; you can redistribute it and/or
10
 
// modify it under the terms of the GNU General Public License
11
 
// as published by the Free Software Foundation; either version 2
12
 
// of the License, or (at your option) any later version.
13
 
//
14
 
// This program is distributed in the hope that it will be useful,
15
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
// GNU General Public License for more details.
18
 
//
19
 
// You should have received a copy of the GNU General Public License
20
 
// along with this program; if not, write to the Free Software
21
 
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
 
// 02110-1301, USA.
23
 
//
24
 
 
25
 
#include <ctime>
26
 
 
27
 
#include "gadusession.h"
28
 
 
29
 
#include <klocale.h>
30
 
#include <kdebug.h>
31
 
#include "kopetemessage.h"
32
 
 
33
 
#include <qsocketnotifier.h>
34
 
#include <qtextcodec.h>
35
 
#include <qdatetime.h>
36
 
//Added by qt3to4:
37
 
#include <QByteArray>
38
 
#include "gadurichtextformat.h"
39
 
 
40
 
#include <errno.h>
41
 
#include <string.h>
42
 
#include <netinet/in.h>
43
 
 
44
 
GaduSession::GaduSession( QObject* parent )
45
 
: QObject( parent ), session_( 0 ), searchSeqNr_( 0 ), deletingUserList( false )
46
 
{
47
 
        textcodec = QTextCodec::codecForName( "Windows-1250" );
48
 
        rtf = new GaduRichTextFormat;
49
 
}
50
 
 
51
 
GaduSession::~GaduSession()
52
 
{
53
 
        logoff();
54
 
}
55
 
 
56
 
bool
57
 
GaduSession::isConnected() const
58
 
{
59
 
        if ( session_ ) {
60
 
                return ( session_->state & GG_STATE_CONNECTED );
61
 
        }
62
 
        return false;
63
 
}
64
 
 
65
 
int
66
 
GaduSession::status() const
67
 
{
68
 
        if ( session_ ) {
69
 
                kDebug(14100)<<"Status = " << session_->status <<", initial = "<< session_->initial_status;
70
 
                return session_->status & ( ~GG_STATUS_FRIENDS_MASK );
71
 
        }
72
 
        return GG_STATUS_NOT_AVAIL;
73
 
}
74
 
 
75
 
void
76
 
GaduSession::login( struct gg_login_params* p )
77
 
{
78
 
        if ( !isConnected() ) {
79
 
 
80
 
// turn on in case you have any problems, and  you want
81
 
// to report it better. libgadu needs to be recompiled with debug enabled
82
 
//              gg_debug_level=GG_DEBUG_MISC|GG_DEBUG_FUNCTION;
83
 
 
84
 
                kDebug(14100) << "Login";
85
 
 
86
 
                if ( !( session_ = gg_login( p ) ) ) {
87
 
                        destroySession();
88
 
                        kDebug( 14100 ) << "libgadu internal error ";
89
 
                        emit connectionFailed(  GG_FAILURE_CONNECTING );
90
 
                        return;
91
 
                }
92
 
 
93
 
                createNotifiers( true );
94
 
                enableNotifiers( session_->check );
95
 
                searchSeqNr_=0;
96
 
        }
97
 
}
98
 
 
99
 
void
100
 
GaduSession::destroyNotifiers()
101
 
{
102
 
        disableNotifiers();
103
 
        delete read_;
104
 
        read_ = NULL;
105
 
        delete write_;
106
 
        write_ = NULL;
107
 
}
108
 
 
109
 
void
110
 
GaduSession::createNotifiers( bool connect )
111
 
{
112
 
        if ( !session_ ){
113
 
                return;
114
 
        }
115
 
 
116
 
        read_ = new QSocketNotifier( session_->fd, QSocketNotifier::Read, this );
117
 
        read_->setEnabled( false );
118
 
 
119
 
        write_ = new QSocketNotifier( session_->fd, QSocketNotifier::Write, this );
120
 
        write_->setEnabled( false );
121
 
 
122
 
        if ( connect ) {
123
 
                QObject::connect( read_, SIGNAL(activated(int)), SLOT(checkDescriptor()) );
124
 
                QObject::connect( write_, SIGNAL(activated(int)), SLOT(checkDescriptor()) );
125
 
        }
126
 
}
127
 
 
128
 
void
129
 
GaduSession::enableNotifiers( int checkWhat )
130
 
{
131
 
        if( (checkWhat & GG_CHECK_READ) && read_ ) {
132
 
                read_->setEnabled( true );
133
 
        }
134
 
        if( (checkWhat & GG_CHECK_WRITE) && write_ ) {
135
 
                write_->setEnabled( true );
136
 
        }
137
 
}
138
 
 
139
 
void
140
 
GaduSession::disableNotifiers()
141
 
{
142
 
        if ( read_ ) {
143
 
                read_->setEnabled( false );
144
 
        }
145
 
        if ( write_ ) {
146
 
                write_->setEnabled( false );
147
 
        }
148
 
}
149
 
 
150
 
void
151
 
GaduSession::dccRequest( const unsigned int uin )
152
 
{
153
 
        if ( session_ ) {
154
 
                gg_dcc_request( session_, uin );
155
 
        }
156
 
}
157
 
 
158
 
void
159
 
GaduSession::login( KGaduLoginParams* loginp )
160
 
{
161
 
        QByteArray desc = textcodec->fromUnicode( loginp->statusDescr );
162
 
 
163
 
        memset( &params_, 0, sizeof(params_) );
164
 
 
165
 
        params_.status_descr    = (char*)desc.data();
166
 
 
167
 
        params_.uin             = loginp->uin;
168
 
        params_.password        = loginp->password.data();
169
 
        params_.status          = loginp->status | ( loginp->forFriends ? GG_STATUS_FRIENDS_MASK : 0 );
170
 
        params_.async           = 1;
171
 
        params_.tls             = loginp->useTls;
172
 
        params_.server_addr     = loginp->server;
173
 
        params_.client_addr     = loginp->client_addr;
174
 
        params_.client_port     = loginp->client_port;
175
 
 
176
 
        kDebug(14100) << "LOGIN IP: " << loginp->client_addr;
177
 
 
178
 
        if ( loginp->useTls ) {
179
 
                params_.server_port = GG_HTTPS_PORT;
180
 
        }
181
 
        else {
182
 
                if ( loginp->server ) {
183
 
                        params_.server_port = GG_DEFAULT_PORT;
184
 
                }
185
 
        }
186
 
 
187
 
        kDebug(14100)<<"gadusession::login, server ( " << loginp->server << " ), tls(" << loginp->useTls << ") ";
188
 
        login( &params_ );
189
 
 
190
 
}
191
 
 
192
 
void
193
 
GaduSession::destroySession()
194
 
{
195
 
        if ( session_ ) {
196
 
                destroyNotifiers();
197
 
                gg_free_session( session_ );
198
 
                session_ = 0;
199
 
        }
200
 
}
201
 
 
202
 
void
203
 
GaduSession::logoff( Kopete::Account::DisconnectReason reason )
204
 
{
205
 
        destroySession();
206
 
        emit disconnect( reason );
207
 
}
208
 
 
209
 
int
210
 
GaduSession::notify( uin_t* userlist, int count )
211
 
{
212
 
        if ( isConnected() ) {
213
 
                return gg_notify( session_, userlist, count );
214
 
        }
215
 
        else {
216
 
                emit error( i18n("Not Connected"), i18n("You are not connected to the server.") );
217
 
        }
218
 
 
219
 
        return 1;
220
 
}
221
 
 
222
 
int
223
 
GaduSession::addNotify( uin_t uin )
224
 
{
225
 
        if ( isConnected() ) {
226
 
                return gg_add_notify( session_, uin );
227
 
        }
228
 
        else {
229
 
                emit error( i18n("Not Connected"), i18n("You are not connected to the server.") );
230
 
        }
231
 
        return 1;
232
 
}
233
 
 
234
 
int
235
 
GaduSession::removeNotify( uin_t uin )
236
 
{
237
 
        if ( isConnected() ) {
238
 
                gg_remove_notify( session_, uin );
239
 
        }
240
 
        else {
241
 
                emit error( i18n("Not Connected"), i18n("You are not connected to the server.") );
242
 
        }
243
 
 
244
 
        return 1;
245
 
}
246
 
 
247
 
int
248
 
GaduSession::sendMessage( uin_t recipient, const Kopete::Message& msg, int msgClass )
249
 
{
250
 
        QString sendMsg;
251
 
        QByteArray cpMsg;
252
 
        KGaduMessage* gadumessage;
253
 
 
254
 
        if ( isConnected() ) {
255
 
                gadumessage = rtf->convertToGaduMessage( msg );
256
 
                if ( gadumessage ) {
257
 
                        const void* data = (const void*)gadumessage->rtf.data();
258
 
                        cpMsg = textcodec->fromUnicode( gadumessage->message );
259
 
                        int o;
260
 
                        o = gg_send_message_richtext( session_, msgClass, recipient, (const unsigned char *)cpMsg.data(), (const unsigned char*) data, gadumessage->rtf.size() );
261
 
                        gadumessage->rtf.resize(0);
262
 
                        delete gadumessage;
263
 
                        return o;
264
 
                }
265
 
                else {
266
 
                        sendMsg = msg.plainBody();
267
 
                        sendMsg.replace( QLatin1Char( '\n' ), QString::fromAscii( "\r\n" ) );
268
 
                        cpMsg = textcodec->fromUnicode( sendMsg );
269
 
 
270
 
                        return gg_send_message( session_, msgClass, recipient, (const unsigned char *)cpMsg.data() );
271
 
                }
272
 
        }
273
 
        else {
274
 
                emit error( i18n("Not Connected"), i18n("You are not connected to the server.") );
275
 
        }
276
 
 
277
 
        return 1;
278
 
}
279
 
 
280
 
int
281
 
GaduSession::changeStatus( int status, bool forFriends )
282
 
{
283
 
        kDebug(14101)<<"## Changing to "<<status;
284
 
        if ( isConnected() ) {
285
 
                return gg_change_status( session_, status | ( forFriends ? GG_STATUS_FRIENDS_MASK : 0) );
286
 
        }
287
 
        else {
288
 
                emit error( i18n("Not Connected"),  i18n("You have to be connected to the server to change your status.") );
289
 
        }
290
 
 
291
 
        return 1;
292
 
}
293
 
 
294
 
int
295
 
GaduSession::changeStatusDescription( int status, const QString& descr, bool forFriends )
296
 
{
297
 
        QByteArray ndescr;
298
 
 
299
 
        ndescr= textcodec->fromUnicode(descr);
300
 
 
301
 
        if ( isConnected() ) {
302
 
                return gg_change_status_descr( session_,
303
 
                                status | ( forFriends ? GG_STATUS_FRIENDS_MASK : 0), ndescr.data() );
304
 
        }
305
 
        else {
306
 
                emit error( i18n("Not Connected"), i18n("You have to be connected to the server to change your status.") );
307
 
        }
308
 
 
309
 
        return 1;
310
 
}
311
 
 
312
 
int
313
 
GaduSession::ping()
314
 
{
315
 
        if ( isConnected() ) {
316
 
                return gg_ping( session_ );
317
 
        }
318
 
 
319
 
        return 1;
320
 
}
321
 
 
322
 
void
323
 
GaduSession::pubDirSearchClose()
324
 
{
325
 
        searchSeqNr_=0;
326
 
}
327
 
 
328
 
unsigned int
329
 
GaduSession::getPersonalInformation()
330
 
{
331
 
        gg_pubdir50_t searchRequest;
332
 
        unsigned int seqNr;
333
 
 
334
 
        if ( isConnected() == false ) {
335
 
                return 0;
336
 
        }
337
 
 
338
 
        searchRequest = gg_pubdir50_new( GG_PUBDIR50_READ );
339
 
        if ( !searchRequest ) {
340
 
                return 0;
341
 
        }
342
 
 
343
 
        seqNr = gg_pubdir50( session_, searchRequest );
344
 
        gg_pubdir50_free( searchRequest );
345
 
 
346
 
        return seqNr;
347
 
}
348
 
 
349
 
bool
350
 
GaduSession::publishPersonalInformation( ResLine& d )
351
 
{
352
 
        gg_pubdir50_t r;
353
 
        
354
 
        if ( !session_ ) {
355
 
                return 0;
356
 
        }               
357
 
        
358
 
        r = gg_pubdir50_new( GG_PUBDIR50_WRITE );
359
 
 
360
 
        if ( d.firstname.length() )
361
 
                gg_pubdir50_add( r, GG_PUBDIR50_FIRSTNAME, 
362
 
                         (const char *)((const char*)textcodec->fromUnicode( d.firstname ) ) );
363
 
        if ( d.surname.length() )
364
 
                gg_pubdir50_add( r, GG_PUBDIR50_LASTNAME,
365
 
                        (const char *)((const char*)textcodec->fromUnicode( d.surname ) ) );
366
 
        if ( d.nickname.length() )
367
 
                gg_pubdir50_add( r, GG_PUBDIR50_NICKNAME,
368
 
                        (const char *)((const char*)textcodec->fromUnicode( d.nickname ) ) );
369
 
        if ( d.age.length() )
370
 
                gg_pubdir50_add( r, GG_PUBDIR50_BIRTHYEAR,
371
 
                        (const char *)((const char*)textcodec->fromUnicode( d.age ) ) );
372
 
        if ( d.city.length() )
373
 
                gg_pubdir50_add( r, GG_PUBDIR50_CITY,
374
 
                        (const char *)((const char*)textcodec->fromUnicode( d.city ) ) );
375
 
        if ( d.meiden.length() )
376
 
                gg_pubdir50_add( r, GG_PUBDIR50_FAMILYNAME,
377
 
                        (const char *)((const char*)textcodec->fromUnicode( d.meiden ) ) );
378
 
        if ( d.orgin.length() )
379
 
                gg_pubdir50_add( r, GG_PUBDIR50_FAMILYCITY,
380
 
                        (const char *)((const char*)textcodec->fromUnicode( d.orgin ) ) );
381
 
        if ( d.gender.length() == 1 )
382
 
                gg_pubdir50_add( r, GG_PUBDIR50_GENDER, 
383
 
                        (const char *)((const char*)textcodec->fromUnicode( d.gender ) ) );
384
 
        
385
 
        gg_pubdir50( session_, r );
386
 
        
387
 
        gg_pubdir50_free( r );
388
 
 
389
 
        return true;
390
 
}
391
 
 
392
 
unsigned int
393
 
GaduSession::pubDirSearch(  ResLine& query, int ageFrom, int ageTo, bool onlyAlive )
394
 
{
395
 
        QString bufYear;
396
 
        unsigned int reqNr;
397
 
        gg_pubdir50_t searchRequest;
398
 
 
399
 
        if ( !session_ ) {
400
 
                return 0;
401
 
        }
402
 
 
403
 
        searchRequest = gg_pubdir50_new( GG_PUBDIR50_SEARCH_REQUEST );
404
 
        if ( !searchRequest ) {
405
 
                return 0;
406
 
        }
407
 
 
408
 
        if ( query.uin == 0 ) {
409
 
                if (query.firstname.length()) {
410
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_FIRSTNAME,
411
 
                                                (const char*)textcodec->fromUnicode( query.firstname ) );
412
 
                }
413
 
                if ( query.surname.length() ) {
414
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_LASTNAME,
415
 
                                                (const char*)textcodec->fromUnicode( query.surname ) );
416
 
                }
417
 
                if ( query.nickname.length() ) {
418
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_NICKNAME,
419
 
                                                (const char*)textcodec->fromUnicode( query.nickname ) );
420
 
                }
421
 
                if ( query.city.length() ) {
422
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_CITY,
423
 
                                                (const char*)textcodec->fromUnicode( query.city ) );
424
 
                }
425
 
                if ( ageFrom || ageTo ) {
426
 
                        QString yearFrom = QString::number( QDate::currentDate().year() - ageFrom );
427
 
                        QString yearTo = QString::number( QDate::currentDate().year() - ageTo );
428
 
 
429
 
                        if ( ageFrom && ageTo ) {
430
 
                                gg_pubdir50_add( searchRequest, GG_PUBDIR50_BIRTHYEAR,
431
 
                                                        (const char*)textcodec->fromUnicode( yearFrom + ' ' + yearTo ) );
432
 
                        }
433
 
                        if ( ageFrom ) {
434
 
                                gg_pubdir50_add( searchRequest, GG_PUBDIR50_BIRTHYEAR,
435
 
                                                        (const char*)textcodec->fromUnicode( yearFrom ) );
436
 
                        }
437
 
                        else {
438
 
                                gg_pubdir50_add( searchRequest, GG_PUBDIR50_BIRTHYEAR,
439
 
                                                        (const char*)textcodec->fromUnicode( yearTo ) );
440
 
                        }
441
 
                }
442
 
 
443
 
                if ( query.gender.length() == 1 ) {
444
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_GENDER,
445
 
                                (const char *)((const char*)textcodec->fromUnicode( query.gender ) ) );
446
 
                }
447
 
 
448
 
                if ( onlyAlive ) {
449
 
                        gg_pubdir50_add( searchRequest, GG_PUBDIR50_ACTIVE, GG_PUBDIR50_ACTIVE_TRUE );
450
 
                }
451
 
        }
452
 
        // otherwise we are looking only for one fellow with this nice UIN
453
 
        else{
454
 
                gg_pubdir50_add( searchRequest, GG_PUBDIR50_UIN, QString::number( query.uin ).toAscii() );
455
 
        }
456
 
 
457
 
        gg_pubdir50_add( searchRequest, GG_PUBDIR50_START, QString::number( searchSeqNr_ ).toAscii() );
458
 
        reqNr = gg_pubdir50( session_, searchRequest );
459
 
        gg_pubdir50_free( searchRequest );
460
 
 
461
 
        return reqNr;
462
 
}
463
 
 
464
 
void
465
 
GaduSession::sendResult( gg_pubdir50_t result )
466
 
{
467
 
        int i, count, age;
468
 
        ResLine resultLine;
469
 
        SearchResult sres;
470
 
 
471
 
        count = gg_pubdir50_count( result );
472
 
 
473
 
        if ( !count ) {
474
 
                kDebug(14100) << "there was nothing found in public directory for requested details";
475
 
        }
476
 
 
477
 
        for ( i = 0; i < count; i++ ) {
478
 
                resultLine.uin          = QString( gg_pubdir50_get( result, i, GG_PUBDIR50_UIN ) ).toInt();
479
 
                resultLine.firstname    = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_FIRSTNAME ) );
480
 
                resultLine.surname      = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_LASTNAME ) );
481
 
                resultLine.nickname     = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_NICKNAME ) );
482
 
                resultLine.age          = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_BIRTHYEAR ) );
483
 
                resultLine.city         = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_CITY ) );
484
 
                QString stat            = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_STATUS ) );
485
 
                resultLine.orgin        = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_FAMILYCITY ) );
486
 
                resultLine.meiden       = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_FAMILYNAME ) );
487
 
                resultLine.gender       = textcodec->toUnicode( gg_pubdir50_get( result, i, GG_PUBDIR50_GENDER ) );
488
 
 
489
 
                resultLine.status       = stat.toInt();
490
 
                age = resultLine.age.toInt();
491
 
                if ( age ) {
492
 
                        resultLine.age = QString::number( QDate::currentDate().year() - age );
493
 
                }
494
 
                else {
495
 
                        resultLine.age.truncate( 0 );
496
 
                }
497
 
                sres.append( resultLine );
498
 
                kDebug(14100) << "found line "<< resultLine.uin << ' ' << resultLine.firstname;
499
 
        }
500
 
 
501
 
        searchSeqNr_ = gg_pubdir50_next( result );
502
 
        emit pubDirSearchResult( sres, gg_pubdir50_seq( result ) );
503
 
}
504
 
 
505
 
void
506
 
GaduSession::requestContacts()
507
 
{
508
 
        if ( !session_ || session_->state != GG_STATE_CONNECTED ) {
509
 
                kDebug(14100) <<" you need to be connected to send ";
510
 
                return;
511
 
        }
512
 
 
513
 
        if ( gg_userlist_request( session_, GG_USERLIST_GET, NULL ) == -1 ) {
514
 
                kDebug(14100) <<" userlist export ERROR ";
515
 
                return;
516
 
        }
517
 
        kDebug( 14100 ) << "Contacts list import..started ";
518
 
}
519
 
 
520
 
void
521
 
GaduSession::exportContactsOnServer( GaduContactsList* contactsList )
522
 
{
523
 
        QByteArray plist;
524
 
 
525
 
        if ( !session_ || session_->state != GG_STATE_CONNECTED ) {
526
 
                kDebug( 14100 ) << "you need to connect to export Contacts list ";
527
 
                return;
528
 
        }
529
 
        if ( deletingUserList) {
530
 
                kDebug( 14100 ) << "you are currently deleting list ";
531
 
                return;
532
 
        }
533
 
        plist = textcodec->fromUnicode( contactsList->asString() );
534
 
        kDebug(14100) <<"--------------------userlists\n" << plist;
535
 
        kDebug(14100) << "----------------------------";
536
 
        if ( gg_userlist_request( session_, GG_USERLIST_PUT, plist.data() ) == -1 ) {
537
 
                kDebug( 14100 ) << "export contact list failed ";
538
 
                return;
539
 
        }
540
 
        kDebug( 14100 ) << "Contacts list export..started ";
541
 
}
542
 
 
543
 
 
544
 
void
545
 
GaduSession::deleteContactsOnServer( )
546
 
{
547
 
 
548
 
        if ( !session_ || session_->state != GG_STATE_CONNECTED ) {
549
 
                kDebug( 14100 ) << "you need to connect to delete Contacts list ";
550
 
                return;
551
 
        }
552
 
 
553
 
        if ( gg_userlist_request( session_, GG_USERLIST_PUT, " " ) == -1 ) {
554
 
                kDebug( 14100 ) << "delete contact list failed ";
555
 
                return;
556
 
        }
557
 
        deletingUserList=true;
558
 
        kDebug( 14100 ) << "Contacts list delete... started ";
559
 
}
560
 
 
561
 
 
562
 
void
563
 
GaduSession::handleUserlist( gg_event* event )
564
 
{
565
 
        QString ul;
566
 
        switch( event->event.userlist.type ) {
567
 
                case GG_USERLIST_GET_REPLY:
568
 
                        if ( event->event.userlist.reply ) {
569
 
                                ul = textcodec->toUnicode(event->event.userlist.reply);
570
 
                                kDebug( 14100 ) << "Got Contacts list  OK ";
571
 
                        }
572
 
                        else {
573
 
                                kDebug( 14100 ) << "Got Contacts list  FAILED/EMPTY ";
574
 
                                // FIXME: send failed?
575
 
                        }
576
 
                        emit userListRecieved( ul );
577
 
                        break;
578
 
 
579
 
                case GG_USERLIST_PUT_REPLY:
580
 
                        if ( deletingUserList ) {
581
 
                                deletingUserList = false;
582
 
                                kDebug( 14100 ) << "Contacts list deleted  OK ";
583
 
                                emit userListDeleted();
584
 
                        }
585
 
                        else {
586
 
                                kDebug( 14100 ) << "Contacts list exported  OK ";
587
 
                                emit userListExported();
588
 
                        }
589
 
                        break;
590
 
 
591
 
        }
592
 
}
593
 
 
594
 
QString
595
 
GaduSession::stateDescription( int state )
596
 
{
597
 
        switch( state ) {
598
 
                case GG_STATE_IDLE:
599
 
                        return i18n( "idle" );
600
 
                case GG_STATE_RESOLVING:
601
 
                        return i18n( "resolving host" );
602
 
                case GG_STATE_CONNECTING:
603
 
                        return i18n( "connecting" );
604
 
                case GG_STATE_READING_DATA:
605
 
                        return i18n( "reading data" );
606
 
                case GG_STATE_ERROR:
607
 
                        return i18n( "error" );
608
 
                case GG_STATE_CONNECTING_HUB:
609
 
                        return i18n( "connecting to hub" );
610
 
                case GG_STATE_CONNECTING_GG:
611
 
                        return i18n( "connecting to server" );
612
 
                case GG_STATE_READING_KEY:
613
 
                        return i18n( "retrieving key" );
614
 
                case GG_STATE_READING_REPLY:
615
 
                        return i18n( "waiting for reply" );
616
 
                case GG_STATE_CONNECTED:
617
 
                        return i18n( "connected" );
618
 
                case GG_STATE_SENDING_QUERY:
619
 
                        return i18n( "sending query" );
620
 
                case GG_STATE_READING_HEADER:
621
 
                        return i18n( "reading header" );
622
 
                case GG_STATE_PARSING:
623
 
                        return i18n( "parsing data" );
624
 
                case GG_STATE_DONE:
625
 
                        return i18n( "done" );
626
 
                case GG_STATE_TLS_NEGOTIATION:
627
 
                        return i18n( "TLS connection negotiation" );
628
 
                default:
629
 
                        return i18n( "unknown" );
630
 
        }
631
 
}
632
 
QString
633
 
GaduSession::errorDescription( int err )
634
 
{
635
 
        switch( err ){
636
 
                case GG_ERROR_RESOLVING:
637
 
                        return i18n( "Resolving error." );
638
 
                case GG_ERROR_CONNECTING:
639
 
                        return i18n( "Connecting error." );
640
 
                case GG_ERROR_READING:
641
 
                        return i18n( "Reading error." );
642
 
                case GG_ERROR_WRITING:
643
 
                        return i18n( "Writing error." );
644
 
                default:
645
 
                        return i18n( "Unknown error number %1." , err );
646
 
        }
647
 
}
648
 
 
649
 
QString
650
 
GaduSession::failureDescription( gg_failure_t f )
651
 
{
652
 
        switch( f ) {
653
 
                case GG_FAILURE_RESOLVING:
654
 
                        return i18n( "Unable to resolve server address. DNS failure." );
655
 
                case GG_FAILURE_CONNECTING:
656
 
                        return i18n( "Unable to connect to server." );
657
 
                case GG_FAILURE_INVALID:
658
 
                        return i18n( "Server sent incorrect data. Protocol error." );
659
 
                case GG_FAILURE_READING:
660
 
                        return i18n( "Problem reading data from server." );
661
 
                case GG_FAILURE_WRITING:
662
 
                        return i18n( "Problem sending data to server." );
663
 
                case GG_FAILURE_PASSWORD:
664
 
                        return i18n( "Incorrect password." );
665
 
                case GG_FAILURE_404:
666
 
                        return QString::fromAscii( "404." );
667
 
                case GG_FAILURE_TLS:
668
 
                        return i18n( "Unable to connect over an encrypted channel.\nTry to turn off encryption support in the Gadu account settings, then reconnect." );
669
 
                default:
670
 
                        return i18n( "Unknown error number %1." , f );
671
 
        }
672
 
}
673
 
 
674
 
void
675
 
GaduSession::notify60( gg_event* event )
676
 
{
677
 
        KGaduNotify* gn = NULL;
678
 
        unsigned int n;
679
 
        
680
 
        if ( event->event.notify60[0].uin ) {
681
 
                gn = new KGaduNotify;
682
 
        }
683
 
        else {
684
 
                return;
685
 
        }
686
 
 
687
 
        for( n=0 ; event->event.notify60[n].uin ; n++ ) {
688
 
                gn->contact_id  = event->event.notify60[n].uin;
689
 
                gn->status      = event->event.notify60[n].status;
690
 
                gn->remote_ip.setAddress( ntohl( event->event.notify60[n].remote_ip ) );
691
 
                gn->remote_port = event->event.notify60[n].remote_port;
692
 
                if ( event->event.notify60[n].remote_ip && gn->remote_port > 10 ) {
693
 
                        gn->fileCap = true;
694
 
                }
695
 
                else {
696
 
                        gn->fileCap = false;
697
 
                }
698
 
                gn->version     = event->event.notify60[n].version;
699
 
                gn->image_size  = event->event.notify60[n].image_size;
700
 
                gn->description = textcodec->toUnicode( event->event.notify60[n].descr );
701
 
                emit contactStatusChanged( gn );        
702
 
        }
703
 
        delete gn;
704
 
}
705
 
 
706
 
void
707
 
GaduSession::checkDescriptor()
708
 
{
709
 
        disableNotifiers();
710
 
 
711
 
        struct gg_event* event;
712
 
//      struct gg_dcc*   dccSock;
713
 
        KGaduMessage    gaduMessage;
714
 
        KGaduNotify     gaduNotify;
715
 
 
716
 
        if ( !( event = gg_watch_fd( session_ ) ) ) {
717
 
                kDebug(14100)<<"Connection was broken for some reason";
718
 
                destroyNotifiers();
719
 
                logoff( Kopete::Account::ConnectionReset );
720
 
                return;
721
 
        }
722
 
 
723
 
        // FD changed, recreate socket notifiers
724
 
        if ( session_->state == GG_STATE_CONNECTING_HUB || session_->state == GG_STATE_CONNECTING_GG ) {
725
 
                kDebug(14100)<<"recreating notifiers";
726
 
                destroyNotifiers();
727
 
                createNotifiers( true );
728
 
        }
729
 
 
730
 
        switch( event->type ) {
731
 
                case GG_EVENT_MSG:
732
 
                        kDebug(14100) << "incoming message:class:" << event->event.msg.msgclass;
733
 
                        if ( event->event.msg.msgclass & GG_CLASS_CTCP ) {
734
 
                                kDebug( 14100 ) << "incoming ctcp ";
735
 
                                // TODO: DCC CONNECTION
736
 
                                emit incomingCtcp( event->event.msg.sender );
737
 
                        }
738
 
 
739
 
                        if ( (event->event.msg.msgclass & GG_CLASS_MSG) || (event->event.msg.msgclass & GG_CLASS_CHAT) ) {
740
 
                                gaduMessage.message =
741
 
                                        textcodec->toUnicode((const char*)event->event.msg.message);
742
 
                                gaduMessage.sender_id = event->event.msg.sender;
743
 
                                gaduMessage.sendTime.setTime_t( event->event.msg.time );
744
 
                                gaduMessage.message = rtf->convertToHtml( gaduMessage.message, event->event.msg.formats_length, event->event.msg.formats );
745
 
                                emit messageReceived( &gaduMessage );
746
 
                        }
747
 
                break;
748
 
                case GG_EVENT_ACK:
749
 
                        emit ackReceived( event->event.ack.recipient );
750
 
                break;
751
 
                case GG_EVENT_STATUS:
752
 
                        gaduNotify.status = event->event.status.status;
753
 
                        gaduNotify.contact_id = event->event.status.uin;
754
 
                        if ( event->event.status.descr ) {
755
 
                                gaduNotify.description = textcodec->toUnicode( event->event.status.descr );
756
 
                        }
757
 
                        else {
758
 
                                gaduNotify.description.clear();
759
 
                        }
760
 
                        gaduNotify.remote_port  = 0;
761
 
                        gaduNotify.version      = 0;
762
 
                        gaduNotify.image_size   = 0;
763
 
                        gaduNotify.time         = 0;
764
 
                        gaduNotify.fileCap      = false;
765
 
 
766
 
                        emit contactStatusChanged( &gaduNotify );
767
 
                break;
768
 
                case GG_EVENT_STATUS60:
769
 
                        gaduNotify.status       = event->event.status60.status;
770
 
                        gaduNotify.contact_id   = event->event.status60.uin;
771
 
                        if ( event->event.status60.descr ) {
772
 
                                gaduNotify.description = textcodec->toUnicode( event->event.status60.descr );
773
 
                        }
774
 
                        else {
775
 
                                gaduNotify.description.clear();
776
 
                        }
777
 
                        gaduNotify.remote_ip.setAddress( ntohl( event->event.status60.remote_ip ) );
778
 
                        gaduNotify.remote_port  = event->event.status60.remote_port;
779
 
                        gaduNotify.version      = event->event.status60.version;
780
 
                        gaduNotify.image_size   = event->event.status60.image_size;
781
 
                        gaduNotify.time         = event->event.status60.time;
782
 
                        if ( event->event.status60.remote_ip && gaduNotify.remote_port > 10 ) {
783
 
                                gaduNotify.fileCap = true;
784
 
                        }
785
 
                        else {
786
 
                                gaduNotify.fileCap = false;
787
 
                        }
788
 
 
789
 
                        emit contactStatusChanged( &gaduNotify );
790
 
                break;
791
 
                case GG_EVENT_NOTIFY60:
792
 
                        notify60( event );
793
 
                break;
794
 
                case GG_EVENT_CONN_SUCCESS:
795
 
                        kDebug(14100) << "success server: " << session_->server_addr;
796
 
                        emit connectionSucceed();
797
 
                break;
798
 
                case GG_EVENT_CONN_FAILED:
799
 
                        kDebug(14100) << "failed server: " << session_->server_addr;
800
 
                        destroySession();
801
 
                        kDebug(14100) << "emit connection failed(" << event->event.failure << ") signal";
802
 
                        emit connectionFailed( (gg_failure_t)event->event.failure );
803
 
                        break;
804
 
                case GG_EVENT_DISCONNECT:
805
 
                        kDebug(14100)<<"event Disconnected";
806
 
                        // it should be called either when we requested disconnect, or when other client connects with our UID
807
 
                        logoff( Kopete::Account::Manual );
808
 
                break;
809
 
                case GG_EVENT_PONG:
810
 
                        emit pong();
811
 
                break;
812
 
                case GG_EVENT_NONE:
813
 
                        break;
814
 
                case GG_EVENT_PUBDIR50_SEARCH_REPLY:
815
 
                case GG_EVENT_PUBDIR50_WRITE:
816
 
                case GG_EVENT_PUBDIR50_READ:
817
 
                        sendResult( event->event.pubdir50 );
818
 
                break;
819
 
                case GG_EVENT_USERLIST:
820
 
                        handleUserlist( event );
821
 
                break;
822
 
                default:
823
 
                        kDebug(14100)<<"Unprocessed GaduGadu Event = "<<event->type;
824
 
                break;
825
 
        }
826
 
 
827
 
        if ( event ) {
828
 
                gg_free_event( event );
829
 
        }
830
 
 
831
 
        if ( session_ ) {
832
 
                enableNotifiers( session_->check );
833
 
        }
834
 
}
835
 
 
836
 
#include "gadusession.moc"