SVN_SILENT
[kdenetwork.git] / kopete / protocols / yahoo / kyahoo.cpp
blob89f8a50ca023ef704112f6690564276cfe485c78
1 /*
2 kyahoo.cpp - Qt Based libyahoo2 wrapper II
4 Copyright (c) 2002-2003 by Duncan Mac-Vicar Prett <duncan@kde.org>
5 Copyright (c) 2003 by Matt Rogers <mattrogers@sbcglobal.net>
7 Copyright (c) 2002 by the Kopete developers <kopete-devel@kde.org>
9 *************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 *************************************************************************
19 // Local Includes
20 #include "kyahoo.h"
21 #include "kopeteuiglobal.h"
22 #include "kopetetransfermanager.h"
23 #include "kopetecontact.h"
24 #include "kopetemetacontact.h"
25 #include "yahoouserinfo.h"
26 #include "yahoobuddyiconloader.h"
28 // QT Includes
29 #include <qfile.h>
30 #include <qtimer.h>
31 #include <qdom.h>
32 #include <qtextstream.h>
34 // KDE Includes
35 #include <klocale.h>
36 #include <kdebug.h>
37 #include <kmessagebox.h>
38 #include <kiconloader.h>
39 #include <krun.h>
40 #include <kurl.h>
41 #include <kio/global.h>
42 #include <kio/job.h>
43 #include <kio/jobclasses.h>
44 //#include <kimageio.h>
45 #include <kprocess.h>
46 #include <ktempfile.h>
47 #include <kstandarddirs.h>
49 // System Includes
50 #include <cstdlib>
51 #include <cstring>
53 #include <iostream>
54 #include <errno.h>
56 /* exported to libyahoo */
57 #define MAX_PREF_LEN 255
58 char pager_host[MAX_PREF_LEN] = "scs.msg.yahoo.com";
59 char pager_port[MAX_PREF_LEN] = "5050";
60 char filetransfer_host[MAX_PREF_LEN] = "filetransfer.msg.yahoo.com";
61 char filetransfer_port[MAX_PREF_LEN] = "80";
63 char webcam_host[MAX_PREF_LEN] = "webcam.yahoo.com";
64 char webcam_port[MAX_PREF_LEN] = "5100";
65 char webcam_description[MAX_PREF_LEN] = "Philips ToUcam Pro";
66 char local_host[MAX_PREF_LEN] = "";
67 int conn_type = 1;
69 extern "C" {
70 void receive_file_callback( int id, int fd, int error,
71 const char *filename, unsigned long size, void *data );
72 void upload_buddyIcon_callback( int id, int fd, int error, void *data );
75 struct connect_callback_data
77 yahoo_connect_callback callback;
78 void * callback_data;
79 int id;
82 YahooConnectionManager::YahooConnectionManager()
86 YahooConnectionManager::~ YahooConnectionManager()
90 void YahooConnectionManager::addConnection( KExtendedSocket* socket )
92 kdDebug(14181) << k_funcinfo << "Adding socket with fd " << socket->fd() << endl;
93 m_connectionList.append( socket );
96 KExtendedSocket* YahooConnectionManager::connectionForFD( int fd )
98 kdDebug(14181) << k_funcinfo << "Looking for socket with fd " << fd << endl;
99 QValueList<KExtendedSocket*>::const_iterator it, ycEnd = m_connectionList.constEnd();
100 for ( it = m_connectionList.begin(); it != ycEnd; ++it )
102 if ( ( *it )->fd() == fd )
104 kdDebug(14181) << k_funcinfo << "Found socket" << endl;
105 KExtendedSocket* socket = ( *it );
106 return socket;
110 return 0L;
113 void YahooConnectionManager::remove( KExtendedSocket* socket )
115 QValueList<KExtendedSocket*>::iterator it, ycEnd = m_connectionList.end();
116 for ( it = m_connectionList.begin(); it != ycEnd; it++ )
118 if ( ( *it ) == socket )
120 it = m_connectionList.remove( it );
121 socket->reset();
122 delete socket;
123 return;
128 void YahooConnectionManager::reset()
130 QValueList<KExtendedSocket*>::iterator it = m_connectionList.begin();
132 while ( it != m_connectionList.end() && m_connectionList.count() > 0 )
134 KExtendedSocket* socket = ( *it );
135 it = m_connectionList.remove( it );
136 socket->reset();
137 delete socket;
142 YahooSessionManager::YahooSessionManager()
144 if ( managerStatic_ )
145 kdDebug(14181) << "Yahoo manager already initialized" << endl;
146 else
147 managerStatic_ = this;
150 YahooSessionManager::~YahooSessionManager()
152 managerStatic_ = 0L;
155 void YahooSessionManager::setPager( QString host, int port )
157 strcpy( pager_host, host.utf8() );
158 strcpy( pager_port, QString::number( port ).latin1() );
161 void YahooSessionManager::setFileTransfer( QString host, int port )
163 strcpy( filetransfer_host, host.utf8() );
164 strcpy( filetransfer_port, QString::number( port ).latin1() );
167 YahooSession* YahooSessionManager::createSession( const QString username, const QString password )
169 int id;
170 YahooSession *session;
172 kdDebug(14181) << k_funcinfo << " Initializing" << endl;
173 id = yahoo_init_with_attributes( username.local8Bit(), password.local8Bit(), "pager_host", pager_host, "pager_port", QString(pager_port).toInt(), 0L );
175 session = new YahooSession(id, username, password);
177 kdDebug(14181) << k_funcinfo << " Session created, got id "<< id << " !"<< endl;
178 m_sessionsMap[id] = session;
180 return session;
184 bool YahooSessionManager::cleanSessions()
186 QMap< int, YahooSession* >::iterator it;
187 for ( it=m_sessionsMap.begin(); it != m_sessionsMap.end(); it++)
189 it.data()->logOff();
190 delete it.data();
191 m_sessionsMap.remove( it.key() );
192 kdDebug(14181) << k_funcinfo << " logout " << it.key() << endl;
194 return true;
197 YahooSession* YahooSessionManager::session(int id)
199 return m_sessionsMap[id] ? m_sessionsMap[id] : 0L;
202 YahooSessionManager* YahooSessionManager::managerStatic_ = 0L;
204 YahooSessionManager* YahooSessionManager::manager()
206 if ( managerStatic_ )
207 return managerStatic_;
208 else
209 return ( new YahooSessionManager );
213 *************************************************************************
214 * YahooSession Methods *
215 *************************************************************************
218 YahooSession::YahooSession(int id, const QString username, const QString password)
220 kdDebug(14181) << k_funcinfo << endl;
221 m_connId = id;
222 m_Username = username;
223 m_Password = password;
224 m_lastWebcamTimestamp = 0;
225 currentImage = 0L;
226 m_iconLoader = new YahooBuddyIconLoader();
227 m_picture = 0;
229 connect( m_iconLoader, SIGNAL(fetchedBuddyIcon(const QString&, KTempFile*, int )), this, SLOT(slotBuddyIconFetched(const QString&, KTempFile*, int ) ) );
232 int YahooSession::sessionId() const
234 return m_connId;
237 void YahooSession::login(int initial)
239 kdDebug(14181) << k_funcinfo << endl;
240 m_Status = initial;
242 /* We try to login */
243 yahoo_login( m_connId, initial );
246 YahooSession::~YahooSession()
248 kdDebug(14181) << k_funcinfo << endl;
249 yahoo_logoff( m_connId );
250 yahoo_close( m_connId );
251 m_connManager.reset();
252 delete m_iconLoader;
255 int YahooSession::setLogLevel(enum yahoo_log_level level)
257 kdDebug(14181) << k_funcinfo << endl;
258 return yahoo_set_log_level( level );
261 void YahooSession::logOff()
263 kdDebug(14181)<< k_funcinfo << " " << m_connId <<endl;
264 yahoo_logoff( m_connId );
266 m_connManager.reset();
270 void YahooSession::refresh()
272 kdDebug(14181) << k_funcinfo << endl;
273 yahoo_refresh( m_connId );
276 void YahooSession::setIdentityStatus( const QString &identity, int active)
278 kdDebug(14181) << k_funcinfo << endl;
279 yahoo_set_identity_status( m_connId, identity.local8Bit(), active );
282 void YahooSession::setPictureFlag( int picture )
284 m_picture = picture;
287 void YahooSession::getList()
289 kdDebug(14181) << k_funcinfo << endl;
290 yahoo_get_list( m_connId );
293 void YahooSession::keepalive()
295 kdDebug(14181) << k_funcinfo << endl;
296 yahoo_keepalive( m_connId );
299 void YahooSession::sendIm( const QString &from, const QString &who, const QString &msg )
301 kdDebug(14181) << k_funcinfo << endl;
302 yahoo_send_im( m_connId, from.local8Bit(), who.local8Bit(), (const char *)msg.utf8(), 1, m_picture );
305 void YahooSession::sendTyping( const QString &from, const QString &who, int typ)
307 kdDebug(14181) << k_funcinfo << endl;
308 yahoo_send_typing( m_connId, from.local8Bit(), who.local8Bit(), typ );
311 void YahooSession::buzzContact( const QString &from, const QString &who )
313 kdDebug(14181) << k_funcinfo << endl;
314 yahoo_send_im( m_connId, from.local8Bit(), who.local8Bit(), "<ding>", 1, m_picture );
317 void YahooSession::setAway( enum yahoo_status state, const QString &msg, int away)
319 kdDebug(14181)<< k_funcinfo << state << ", " << msg << ", " << away << "]" << m_connId << endl;
321 yahoo_set_away( m_connId, state, msg.isNull() ? QCString() : msg.local8Bit(), away );
324 void YahooSession::addBuddy( const QString &who, const QString &group)
326 kdDebug(14181) << k_funcinfo << endl;
327 yahoo_add_buddy( m_connId, who.local8Bit(), group.local8Bit(), "Please add me" );
330 void YahooSession::removeBuddy( const QString &who, const QString &group)
332 kdDebug(14181) << k_funcinfo << endl;
333 yahoo_remove_buddy( m_connId, who.local8Bit(), group.local8Bit() );
336 void YahooSession::rejectBuddy( const QString &who, const QString &msg)
338 yahoo_reject_buddy( m_connId, who.local8Bit(), msg.local8Bit() );
341 void YahooSession::ignoreBuddy( const QString &who, int unignore)
343 yahoo_ignore_buddy( m_connId, who.local8Bit(), unignore );
347 void YahooSession::requestBuddyIcon( const QString &who )
349 kdDebug(14181) << k_funcinfo << "Requesting avatar for: " << who << endl;
350 yahoo_buddyicon_request( m_connId, who.local8Bit() );
353 void YahooSession::downloadBuddyIcon( const QString &who, KURL url, int checksum )
355 m_iconLoader->fetchBuddyIcon( QString(who), KURL(url), checksum );
358 void YahooSession::sendBuddyIconChecksum( int checksum, const QString &who )
360 kdDebug(14181) << k_funcinfo << checksum << " sent to " << who << endl;
361 if ( who.isEmpty() )
362 yahoo_send_picture_checksum( m_connId, 0, checksum );
363 else
364 yahoo_send_picture_checksum( m_connId, who.local8Bit(), checksum );
367 void YahooSession::sendBuddyIconInfo( const QString &who, const QString &url, int checksum )
369 kdDebug(14180) << k_funcinfo << "Url: " << url << " checksum: " << checksum << endl;
370 yahoo_send_picture_info( m_connId, who.local8Bit(), url.local8Bit(), checksum );
373 void YahooSession::sendBuddyIconUpdate( const QString &who, int type )
375 kdDebug(14181) << k_funcinfo << endl;
376 yahoo_send_picture_update( m_connId, who.local8Bit(), type );
379 void YahooSession::uploadBuddyIcon( const QString &url, int size )
381 kdDebug(14181) << k_funcinfo << endl;
382 YahooBuddyIconUploadData *uploadData = new YahooBuddyIconUploadData();
383 uploadData->url = url;
384 uploadData->size = size;
385 uploadData->transmitted = 0;
386 uploadData->file.setName( url );
388 yahoo_send_picture( m_connId, url.local8Bit(), size, upload_buddyIcon_callback, reinterpret_cast< void*>( uploadData ) );
392 void YahooSession::changeBuddyGroup( const QString &who, const QString &old_group, const QString &new_group)
394 yahoo_change_buddy_group( m_connId, who.local8Bit(), old_group.local8Bit(), new_group.local8Bit() );
397 void YahooSession::conferenceInvite( const QString & from, const QStringList &who,
398 const QString &room, const QString &msg )
400 YList *tmplist;
401 tmplist = (YList *) malloc(sizeof(YList));
403 for ( QStringList::ConstIterator it = who.begin(); it != who.end(); ++it )
405 char *member;
406 member = strdup( (*it).local8Bit() );
407 y_list_append( tmplist, member );
410 yahoo_conference_invite( m_connId, from.local8Bit(), tmplist, room.local8Bit(), msg.local8Bit() );
412 y_list_free_1( tmplist );
413 y_list_free( tmplist );
416 void YahooSession::conferenceAddinvite( const QString & from, const QString &who, const QString &room,
417 const QStringList &members, const QString &msg )
420 YList *tmplist;
421 tmplist = (YList *) malloc( sizeof( YList ) );
423 for ( QStringList::ConstIterator it = members.begin(); it != members.end(); ++it )
426 char *member;
427 member = strdup( (*it).local8Bit() );
428 y_list_append( tmplist, member );
431 yahoo_conference_addinvite( m_connId, from.local8Bit(), who.local8Bit(), room.local8Bit(), tmplist, msg.local8Bit() );
433 y_list_free_1( tmplist );
434 y_list_free( tmplist );
437 void YahooSession::conferenceDecline( const QString & from, const QStringList &who,
438 const QString &room, const QString &msg )
440 YList *tmplist;
441 tmplist = (YList *) malloc( sizeof( YList ) );
443 for ( QStringList::ConstIterator it = who.begin(); it != who.end(); ++it )
445 char *member;
446 member = strdup( (*it).local8Bit() );
447 y_list_append( tmplist, member );
450 yahoo_conference_decline( m_connId, from.local8Bit(), tmplist, room.local8Bit(), msg.local8Bit() );
452 y_list_free_1( tmplist );
453 y_list_free( tmplist );
456 void YahooSession::conferenceMessage( const QString & from, const QStringList &who,
457 const QString &room, const QString &msg )
459 YList *tmplist;
460 tmplist = (YList *) malloc( sizeof( YList ) );
462 for ( QStringList::ConstIterator it = who.begin(); it != who.end(); ++it )
464 char *member;
465 member = strdup( (*it).local8Bit() );
466 y_list_append( tmplist, member );
469 yahoo_conference_message( m_connId, from.local8Bit(), tmplist,
470 room.local8Bit(), (const char *) msg.utf8(), 1 );
472 y_list_free_1( tmplist );
473 y_list_free( tmplist );
476 void YahooSession::YahooSession::conferenceLogon( const QString & from, const QStringList &who,
477 const QString &room)
479 YList *tmplist;
480 tmplist = (YList *) malloc( sizeof( YList ) );
482 for ( QStringList::ConstIterator it = who.begin(); it != who.end(); ++it )
484 char *member;
485 member = strdup( (*it).local8Bit() );
486 y_list_append( tmplist, member );
489 yahoo_conference_logon( m_connId, from.local8Bit(), tmplist, room.local8Bit() );
491 y_list_free_1( tmplist );
492 y_list_free( tmplist );
495 void YahooSession::conferenceLogoff( const QString &from, const QStringList &who,
496 const QString &room )
498 YList *tmplist;
499 tmplist = (YList *) malloc(sizeof(YList));
501 for ( QStringList::ConstIterator it = who.begin(); it != who.end(); ++it )
503 char *member;
504 member = strdup( (*it).local8Bit() );
505 y_list_append( tmplist, member );
508 yahoo_conference_logoff( m_connId, from.local8Bit(), tmplist, room.local8Bit() );
510 y_list_free_1( tmplist );
511 y_list_free( tmplist );
514 int YahooSession::sendFile( const QString& /*who*/, const QString& /*msg*/,
515 const QString& /*name*/, long /*size*/ )
517 // FIXME 0,0 is the callback and void *data
518 // void (*yahoo_get_fd_callback)(int id, int fd, int error, void *data);
520 //return yahoo_send_file(m_connId, who.local8Bit(), msg.local8Bit(),
521 // name.local8Bit(), size, file_send_callback ,0);
522 return 0;
527 int YahooSession::getUrlHandle( Kopete::Transfer *trans )
529 char *_url;
531 m_kopeteTransfer = trans;
532 _url = strdup( trans->info().internalId().local8Bit() );
533 m_Filename = strdup( QFile::encodeName(trans->destinationURL().path()) );
535 yahoo_get_url_handle(m_connId, _url, receive_file_callback, 0);
537 free(_url);
538 return 0;
541 enum yahoo_status YahooSession::currentStatus()
543 return yahoo_current_status( m_connId );
546 const YList *YahooSession::getLegacyBuddyList()
548 return yahoo_get_buddylist( m_connId );
551 QStringList YahooSession::getBuddylist()
553 //return yahoo_get_buddylist(m_connId);
554 return QStringList();
557 QStringList YahooSession::getIgnorelist()
559 //return yahoo_get_ignorelist(m_connId);
560 return QStringList();
563 QStringList YahooSession::getIdentities()
565 //return yahoo_get_identities(m_connId);
566 return QStringList();
569 QString YahooSession::getCookie( const QString &which)
571 return QString( yahoo_get_cookie( m_connId, which.latin1() ) );
574 QString YahooSession::getProfile_url( void )
576 return QString( yahoo_get_profile_url() );
579 void YahooSession::requestWebcam( const QString& from )
581 yahoo_webcam_get_feed( m_connId, from.latin1() );
584 void YahooSession::closeWebcam( const QString& from )
586 yahoo_webcam_close_feed( m_connId, from.latin1() );
589 void YahooSession::slotLoginResponseReceiver( int /* succ */, char * /* url */ )
591 kdDebug(14181)<< k_funcinfo << endl;
594 void YahooSession::stealthContact( const QString &who, int unstealth )
596 kdDebug(14181)<< k_funcinfo << "Unstealth: " << unstealth << endl;
597 yahoo_stealth_buddy( m_connId, who.local8Bit(), unstealth );
600 void YahooSession::getUserInfo( const QString &who )
602 m_targetID = who;
603 m_UserInfo = QString::null;
604 //QString url = QString::fromLatin1("http://insider.msg.yahoo.com/ycontent/?filter=timef&ab2=0&intl=us&os=win&%1&%2").arg(getCookie("y")).arg(getCookie("t"));
605 QString url = QString::fromLatin1("http://insider.msg.yahoo.com/ycontent/?filter=timef&ab2=0&intl=us&os=win");
607 mTransferJob = KIO::get( url , false, false );
608 mTransferJob->addMetaData("cookies", "manual");
609 mTransferJob->addMetaData("setcookies", QString::fromLatin1("Cookie: Y=%1; T=%2").arg(getCookie("y")).arg(getCookie("t")) );
610 connect( mTransferJob, SIGNAL( data( KIO::Job *, const QByteArray & ) ), this, SLOT( slotUserInfoData( KIO::Job*, const QByteArray & ) ) );
611 connect( mTransferJob, SIGNAL( result( KIO::Job *) ), this, SLOT( slotUserInfoResult( KIO::Job* ) ) );
614 void YahooSession::slotUserInfoResult( KIO::Job* job )
616 kdDebug(14180) << k_funcinfo << endl;
617 if ( job->error () || mTransferJob->isErrorPage () )
618 kdDebug(14180) << k_funcinfo << "Could not retrieve server side addressbook for user info." << endl;
619 else {
620 QDomDocument doc;
621 QDomNodeList list;
622 QDomElement e;
623 QString msg;
624 uint it = 0;
626 kdDebug(14180) << k_funcinfo << "Server side Adressbook successfully retrieved." << endl;
627 //kdDebug(14180) << "Adressbook:" << endl << m_UserInfo << endl;
629 doc.setContent( m_UserInfo );
630 list = doc.elementsByTagName( "record" ); // Get records
632 for( it = 0; it < list.count(); it++ ) {
633 if( !list.item( it ).isElement() )
634 continue;
635 e = list.item( it ).toElement();
636 if( e.attribute("userid") != m_targetID )
637 continue;
639 YahooUserInfo info;
640 kdDebug(14180) << k_funcinfo << "dbID: " << e.attribute("dbid") << endl;
641 info.userID = e.attribute("userid");
642 info.abID = e.attribute("dbid");
643 info.firstName = e.attribute( "fname" );
644 info.lastName = e.attribute( "lname" );
645 info.nickName = e.attribute( "nname" );
646 info.email = e.attribute( "email" );
647 info.phoneHome = e.attribute( "hphone" );
648 info.phoneWork = e.attribute( "wphone" );
649 info.phoneMobile = e.attribute( "mphone" );
651 YahooUserInfoDialog* theDialog = new YahooUserInfoDialog( Kopete::UI::Global::mainWidget(), "User Information" );
652 theDialog->setUserInfo( info );
653 theDialog->setSession( this );
654 theDialog->show();
656 return;
659 // If we get here, no entry was found --> ask to create a new one
660 if( KMessageBox::Yes == KMessageBox::questionYesNo(Kopete::UI::Global::mainWidget(), i18n( "Do you want to create a new entry?" ), i18n( "No Yahoo Addressbook Entry Found" )) ){
661 YahooUserInfo info;
662 info.userID = m_targetID;
663 info.abID = "-1";
665 YahooUserInfoDialog* theDialog = new YahooUserInfoDialog( Kopete::UI::Global::mainWidget(), "User Information" );
666 theDialog->setUserInfo( info );
667 theDialog->setSession( this );
668 theDialog->show();
669 } else {
670 viewUserProfile( m_targetID );
675 void YahooSession::slotUserInfoData( KIO::Job* /*job*/, const QByteArray &info )
677 kdDebug(14180) << k_funcinfo << endl;
678 m_UserInfo += info;
681 void YahooSession::saveAdressBookEntry( const YahooUserInfo &entry)
683 kdDebug(14180) << k_funcinfo << endl;
684 QString url;
686 if( entry.abID.toInt() > 0 ) { // This is an Update --> append entry-ID
687 url = QString("http://insider.msg.yahoo.com/ycontent/?addab2=0&ee=1&ow=1&id=%0&fn=%1&ln=%2&yid=%3&nn=%4&e=%5&hp=%6&wp=%7")
688 .arg(entry.abID).arg(entry.firstName).arg(entry.lastName).arg(entry.userID).
689 arg(entry.nickName).arg(entry.email).arg(entry.phoneHome).arg(entry.phoneWork);
690 //url += QString::fromLatin1("&%1&%2&%3&%4").arg(getCookie("y")).arg(getCookie("t")).arg(getCookie("b")).arg(getCookie("q"));
691 } else {
692 url = QString("http://address.yahoo.com/yab/us?A=m&v=PG&ver=2&fn=%0&ln=%1&yid=%2&nn=%3&e=%4&hp=%5&wp=%6")
693 .arg(entry.firstName).arg(entry.lastName).arg(entry.userID).arg(entry.nickName)
694 .arg(entry.email).arg(entry.phoneHome).arg(entry.phoneWork);
695 /*url = QString::fromLatin1("http://insider.msg.yahoo.com/ycontent/?addab2=0&fn=%0&ln=%1&yid=%2&nn=%3&e=%4&hp=%5&wp=%6").arg(entry.firstName).arg(entry.lastName).arg(entry.userID).arg(entry.nickName).arg(entry.email).arg(entry.phoneHome).arg(entry.phoneWork);*/
696 //url += QString::fromLatin1("&%1&%2&%3&%4").arg(getCookie("y")).arg(getCookie("t")).arg(getCookie("b")).arg(getCookie("q"));
699 kdDebug(14180) << url << endl;
701 m_UserInfo = QString::null;
702 mTransferJob = KIO::get( url , false, false );
703 mTransferJob->addMetaData("cookies", "manual");
704 mTransferJob->addMetaData("setcookies", QString::fromLatin1("Cookie: Y=%1; T=%2").arg(getCookie("y")).arg(getCookie("t")) );
705 connect( mTransferJob, SIGNAL( data( KIO::Job *, const QByteArray & ) ), this, SLOT( slotUserInfoData( KIO::Job*, const QByteArray & ) ) );
706 connect( mTransferJob, SIGNAL( result( KIO::Job *) ), this, SLOT( slotUserInfoSaved( KIO::Job* ) ) );
708 void YahooSession::slotUserInfoSaved( KIO::Job* job )
710 kdDebug(14180) << k_funcinfo << endl << "Return data:" << m_UserInfo << endl;
712 if ( job->error() || mTransferJob->isErrorPage() || m_UserInfo.find(m_targetID) < 0 ) {
713 kdDebug(14180) << "Could not save the adressbook entry." << endl;
714 KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n( "An unknown error occurred. A possible reason is a invalid email address." ), i18n("Error") );
715 } else {
716 kdDebug(14180) << "Adressbook entry succesfully saved." << endl;
720 void YahooSession::viewUserProfile( const QString &who )
722 kdDebug(14180) << k_funcinfo << endl;
724 QString profileSiteString = QString::fromLatin1("http://profiles.yahoo.com/") + who;
725 KRun::runURL( KURL( profileSiteString ) , "text/html" );
729 *************************************************************************
730 * Callback implementation *
731 *************************************************************************
734 extern "C"
737 void YAHOO_CALLBACK_TYPE( ext_yahoo_login_response ) ( int id, int succ, const char *url )
739 YahooSession *session = YahooSessionManager::manager()->session(id);
740 session->_loginResponseReceiver( succ, url );
743 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_buddies ) ( int id, YList * buds )
745 YahooSession *session = YahooSessionManager::manager()->session( id );
746 session->_gotBuddiesReceiver( buds );
749 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_ignore ) ( int id, YList * igns )
751 YahooSession *session = YahooSessionManager::manager()->session( id );
752 session->_gotIgnoreReceiver( igns );
755 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_identities ) ( int id, YList * ids )
757 YahooSession *session = YahooSessionManager::manager()->session(id);
758 session->_gotIdentitiesReceiver(ids);
761 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_cookies )( int /*id*/ )
763 /* Not implemented , No receiver yet */
766 void YAHOO_CALLBACK_TYPE(ext_yahoo_got_ping)(int /*id*/, const char */*errormsg*/)
768 /* Not implemented , No receiver yet */
771 void YAHOO_CALLBACK_TYPE( ext_yahoo_status_changed )( int id, const char *who, int stat,
772 const char *msg, int away, int /*idle*/, int /*mobile*/ )
774 YahooSession *session = YahooSessionManager::manager()->session( id );
775 session->_statusChangedReceiver( (char*)who, stat, (char*)msg, away );
779 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_im )( int id, const char */*me*/, const char *who, const char *msg, long tm, int stat, int utf8 )
781 YahooSession *session = YahooSessionManager::manager()->session( id );
782 session->_gotImReceiver( (char*)who, (char*)msg, tm, stat, utf8 );
786 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_conf_invite )(int id, const char */*me*/, const char *who, const char *room, const char *msg, YList *members)
788 YahooSession *session = YahooSessionManager::manager()->session( id );
789 session->_gotConfInviteReceiver( (char*)who, (char*)room, (char*)msg, members );
793 void YAHOO_CALLBACK_TYPE( ext_yahoo_conf_userdecline )(int id, const char */*me*/, const char *who, const char *room, const char *msg)
795 YahooSession *session = YahooSessionManager::manager()->session( id );
796 session->_confUserDeclineReceiver( (char*)who, (char*)room, (char*)msg );
800 void YAHOO_CALLBACK_TYPE( ext_yahoo_conf_userjoin )(int id, const char */*me*/, const char *who, const char *room)
802 YahooSession *session = YahooSessionManager::manager()->session( id );
803 session->_confUserJoinReceiver( (char*)who, (char*)room );
807 void YAHOO_CALLBACK_TYPE( ext_yahoo_conf_userleave )(int id, const char */*me*/, const char *who, const char *room)
809 YahooSession *session = YahooSessionManager::manager()->session( id );
810 session->_confUserLeaveReceiver( (char*)who, (char*)room );
814 void YAHOO_CALLBACK_TYPE( ext_yahoo_conf_message )(int id, const char */*me*/, const char *who, const char *room, const char *msg, int utf8)
816 YahooSession *session = YahooSessionManager::manager()->session( id );
817 session->_confMessageReceiver( (char*)who, (char*)room, (char*)msg, utf8 );
821 void YAHOO_CALLBACK_TYPE( ext_yahoo_chat_cat_xml )( int /*id*/, const char* /*xml*/ )
823 /* Not implemented , No receiver yet */
826 void YAHOO_CALLBACK_TYPE( ext_yahoo_chat_join )( int /*id*/, const char */*me*/, const char */*room*/, const char */*topic*/, YList */*members*/, int /*fd*/ )
828 /* Not implemented , No receiver yet */
831 void YAHOO_CALLBACK_TYPE( ext_yahoo_chat_userjoin )( int /*id*/, const char */*me*/, const char */*room*/, struct yahoo_chat_member */*who*/ )
833 /* Not implemented , No receiver yet */
836 void YAHOO_CALLBACK_TYPE( ext_yahoo_chat_userleave )( int /*id*/, const char */*me*/, const char */*room*/, const char */*who*/ )
838 /* Not implemented , No receiver yet */
841 void YAHOO_CALLBACK_TYPE( ext_yahoo_chat_message )( int /*id*/, const char */*me*/, const char */*who*/, const char */*room*/, const char */*msg*/, int /*msgtype*/, int /*utf8*/)
843 /* Not implemented , No receiver yet */
846 void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahoologout)(int /*id*/, const char */*me*/)
848 /* Not implemented , No receiver yet */
851 void YAHOO_CALLBACK_TYPE(ext_yahoo_chat_yahooerror)(int /*id*/, const char */*me*/)
853 /* Not implemented , No receiver yet */
856 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_file )( int id, const char */*me*/, const char *who, const char *url, long expires, const char *msg, const char *fname, unsigned long fesize)
858 YahooSession *session = YahooSessionManager::manager()->session( id );
859 session->_gotFileReceiver( (char*)who, (char*)url, expires, (char*)msg, (char*)fname, fesize );
863 void YAHOO_CALLBACK_TYPE( ext_yahoo_contact_added )( int id, const char *myid, const char *who, const char *msg )
865 YahooSession *session = YahooSessionManager::manager()->session( id );
866 session->_contactAddedReceiver( (char*)myid, (char*)who, (char*)msg );
870 void YAHOO_CALLBACK_TYPE( ext_yahoo_rejected )( int id, const char *who, const char *msg )
872 YahooSession *session = YahooSessionManager::manager()->session( id );
873 session->_rejectedReceiver( (char*)who, (char*)msg );
876 void YAHOO_CALLBACK_TYPE( ext_yahoo_typing_notify )( int id, const char */*me*/, const char *who, int stat )
878 YahooSession *session = YahooSessionManager::manager()->session( id );
879 session->_typingNotifyReceiver( (char*)who, stat );
883 void YAHOO_CALLBACK_TYPE( ext_yahoo_game_notify )(int id, const char */*me*/, const char *who, int stat)
885 YahooSession *session = YahooSessionManager::manager()->session( id );
886 session->_gameNotifyReceiver( (char*)who, stat );
890 void YAHOO_CALLBACK_TYPE( ext_yahoo_mail_notify )( int id, const char *from, const char *subj, int cnt )
892 YahooSession *session = YahooSessionManager::manager()->session( id );
893 session->_mailNotifyReceiver( (char*)from, (char*)subj, cnt );
897 void YAHOO_CALLBACK_TYPE( ext_yahoo_system_message )( int id, const char *msg )
899 YahooSession *session = YahooSessionManager::manager()->session( id );
900 session->_systemMessageReceiver( (char*)msg );
904 void YAHOO_CALLBACK_TYPE( ext_yahoo_error )( int id, const char *err, int fatal, int /*num*/ )
906 YahooSession *session = YahooSessionManager::manager()->session( id );
907 session->_errorReceiver( (char*)err, fatal );
911 int YAHOO_CALLBACK_TYPE( ext_yahoo_log )( const char* /*fmt*/, ... )
913 /* Do nothing? */
914 return 0;
917 int YAHOO_CALLBACK_TYPE( ext_yahoo_add_handler )( int id, int fd, yahoo_input_condition cond, void * data )
919 YahooSession *session = YahooSessionManager::manager()->session( id );
920 return session->_addHandlerReceiver( fd, cond, data );
923 void YAHOO_CALLBACK_TYPE( ext_yahoo_remove_handler )( int id, int fd )
925 YahooSession *session = YahooSessionManager::manager()->session( id );
926 session->_removeHandlerReceiver( fd );
930 int YAHOO_CALLBACK_TYPE( ext_yahoo_connect )( const char *host, int port )
932 return YahooSessionManager::manager()->_hostConnectReceiver( (char*)host, port );
935 int YAHOO_CALLBACK_TYPE( ext_yahoo_connect_async )( int id, const char *host, int port,
936 yahoo_connect_callback callback, void *callback_data )
938 YahooSession *session = YahooSessionManager::manager()->session( id );
939 return session->_hostAsyncConnectReceiver( (char*)host, port, callback, callback_data );
943 void YAHOO_CALLBACK_TYPE( ext_yahoo_got_webcam_image )( int id, const char* who,
944 const unsigned char* image, unsigned int image_size, unsigned int real_size,
945 unsigned int timestamp )
947 YahooSession* session = YahooSessionManager::manager()->session( id );
948 return session->_gotWebcamImage( (char*)who, (unsigned char*)image, image_size, real_size, timestamp );
951 void YAHOO_CALLBACK_TYPE( ext_yahoo_webcam_invite )( int id, const char */*me*/, const char* from )
953 YahooSession *session = YahooSessionManager::manager()->session( id );
954 session->_gotWebcamInvite( (char*)from );
957 void YAHOO_CALLBACK_TYPE( ext_yahoo_webcam_invite_reply )( int /*id*/, const char */*me*/, const char* /*from*/, int /*accept*/ )
959 /* Not implemented , No receiver yet */
962 void YAHOO_CALLBACK_TYPE( ext_yahoo_webcam_closed )( int id, const char* who, int reason )
964 YahooSession* session = YahooSessionManager::manager()->session( id );
965 session->_webcamDisconnected( (char*)who, reason );
968 void YAHOO_CALLBACK_TYPE(ext_yahoo_got_search_result)(int /*id*/, int /*found*/, int /*start*/, int /*total*/, YList */*contacts*/)
973 void YAHOO_CALLBACK_TYPE( ext_yahoo_webcam_viewer )( int /*id*/, const char* /*who*/, int /*connect*/ )
975 /* Not implemented , No receiver yet */
978 void YAHOO_CALLBACK_TYPE( ext_yahoo_webcam_data_request )( int /*id*/, int /*send*/ )
980 /* Not implemented , No receiver yet */
983 void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon)(int id, const char */*me*/, const char *who, const char *url, int checksum)
985 YahooSession *session = YahooSessionManager::manager()->session( id );
986 if ( session )
987 session->_gotBuddyIconReceiver( id, (char*)who, (char*)url, checksum );
990 void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyicon_checksum)(int id, const char */*me*/, const char *who, int checksum)
992 YahooSession *session = YahooSessionManager::manager()->session( id );
993 if( session )
994 session->_gotBuddyIconChecksumReceiver( id, (char*)who, checksum );
997 void YAHOO_CALLBACK_TYPE(ext_yahoo_got_buddyiconrequest)(int id, const char */*me*/, const char *who)
999 YahooSession *session = YahooSessionManager::manager()->session( id );
1000 if ( session )
1001 session->_gotBuddyIconRequestReceiver( id, (char*)who );
1004 void YAHOO_CALLBACK_TYPE(ext_yahoo_buddyicon_uploaded)(int id, const char *url)
1006 YahooSession *session = YahooSessionManager::manager()->session( id );
1007 if ( session )
1008 session->_gotBuddyIconUploadResponseReceiver( id, (char*)url );
1011 void receive_file_callback( int id, int fd, int error,
1012 const char *filename, unsigned long size, void *data )
1014 YahooSession *session = YahooSessionManager::manager()->session( id );
1015 if ( session )
1016 session->_receiveFileProceed( id, fd, error, (char*)filename, size, (char*)data );
1019 void upload_buddyIcon_callback( int id, int fd, int error, void *data )
1021 YahooSession *session = YahooSessionManager::manager()->session( id );
1022 if ( session )
1023 session->_uploadBuddyIconReceiver( id, fd, error, (char*)data );
1025 /* End of extern C */
1030 *************************************************************************
1031 * Private Session Callback Receiver, don't use them *
1032 *************************************************************************
1035 void YahooSession::_uploadBuddyIconReceiver( int /*id*/, int fd, int error, void *data )
1037 YahooBuddyIconUploadData *uploadData = reinterpret_cast< YahooBuddyIconUploadData *>( data );
1038 kdDebug(14181) << k_funcinfo << "Url: " << uploadData->url << " Size: " << uploadData->size << endl;
1040 if ( error )
1042 kdDebug(14180) << "Could not upload buddy icon. Error: " << error << endl;
1043 KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n( "An unknown error occurred when trying to upload the buddy icon."
1044 " Your buddy con was not transferred." ), i18n("Error") );
1045 return;
1048 if ( !uploadData->file.open(IO_ReadOnly) )
1050 kdDebug(14180) << "Could not open local buddy icon file." << endl;
1051 KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n( "Could not open local buddy icon file!" ), i18n("Error") );
1052 return;
1055 slotTransmitBuddyIcon( fd, uploadData );
1058 void YahooSession::slotTransmitBuddyIcon( int fd, YahooBuddyIconUploadData *uploadData )
1060 KExtendedSocket* socket = m_connManager.connectionForFD( fd );
1061 if( !socket )
1062 return;
1064 if( uploadData->transmitted >= uploadData->size ) {
1065 char buf[1024];
1066 int r;
1067 if( socket->readBlock( buf, r) <=0 )
1068 kdDebug(14181) << k_funcinfo << "Icon " << uploadData->file.name() << " successfully uploaded" << endl;
1070 uploadData->file.close();
1071 delete uploadData;
1072 return;
1075 char buf[1024];
1076 int read;
1078 read = uploadData->file.readBlock( buf, 512 );
1080 if ( read < 0 )
1081 return;
1083 uploadData->transmitted += socket->writeBlock( buf, read );
1085 slotTransmitBuddyIcon(fd, uploadData);
1088 void YahooSession::_gotBuddyIconUploadResponseReceiver( int /*id*/, const char *url)
1090 kdDebug(14181) << k_funcinfo << endl;
1091 setPictureFlag( 2 );
1092 emit buddyIconUploaded( QString( url ) );
1095 void YahooSession::_gotBuddyIconReceiver( int /*id*/, char *who, char *url, int checksum )
1097 kdDebug(14181) << k_funcinfo << "BuddyIcon reveived from: " << who << " checksum: " << checksum <<endl;
1098 kdDebug(14181) << k_funcinfo << "BuddyIcon-Url: " << url <<endl;
1100 emit gotBuddyIconInfo( QString( who ), KURL( url ), checksum );
1103 void YahooSession::_gotBuddyIconChecksumReceiver( int /*id*/, char *who, int checksum )
1105 kdDebug(14181) << k_funcinfo << "checksum: " << checksum << " received from: " << who << endl;
1107 emit gotBuddyIconChecksum( QString( who ), checksum );
1111 void YahooSession::_gotBuddyIconRequestReceiver( int /*id*/, char *who )
1113 kdDebug(14181) << k_funcinfo << "Got Buddy Icon Request from: " << who << endl;
1115 emit gotBuddyIconRequest ( QString( who ) );
1118 void YahooSession::slotBuddyIconFetched(const QString &who, KTempFile *file, int checksum)
1120 emit gotBuddyIcon( who, file, checksum );
1123 void YahooSession::_receiveFileProceed( int id, int fd, int /*error*/,
1124 const char */*filename*/, unsigned long /*size*/, void */*data*/ )
1126 kdDebug(14181) << k_funcinfo << "FD:" << fd << " Filename:" << m_Filename <<endl;
1127 int read = 0, received = 0;
1128 char buf[1024];
1129 KExtendedSocket* socket = m_connManager.connectionForFD( fd );
1130 if ( !socket )
1132 kdDebug(14181) << k_funcinfo << "No existing socket for connection found. We're screwed" << endl;
1133 return;
1136 QFile file( m_Filename );
1137 if ( file.open(IO_WriteOnly ) )
1139 QTextStream stream( &file );
1140 while( (read = socket->readBlock( buf, 1024 )) > 0 )
1142 stream << buf;
1143 received += read;
1144 m_kopeteTransfer->slotProcessed( received );
1146 m_kopeteTransfer->slotComplete();
1147 file.close();
1148 } else
1149 m_kopeteTransfer->slotError( KIO::ERR_CANNOT_OPEN_FOR_WRITING, i18n("Cannot open file %1 for writing.\n%2")
1150 .arg( m_Filename, file.errorString() ) );
1152 ext_yahoo_remove_handler( id, fd );
1153 return;
1156 void YahooSession::_loginResponseReceiver( int succ, const char *url )
1159 kdDebug(14181) << k_funcinfo << endl;
1161 emit loginResponse( succ, QString( url ) );
1164 void YahooSession::_gotIgnoreReceiver( YList * igns )
1166 kdDebug(14181) << k_funcinfo << endl;
1168 YList *l;
1169 QStringList ign_list;
1171 for ( l = igns; l; l = l->next )
1173 struct yahoo_buddy *bud = ( yahoo_buddy* ) l->data;
1175 if( !bud )
1177 kdDebug(14181) << k_funcinfo << " Null Id" << endl;
1178 continue;
1180 else
1182 kdDebug(14181) << k_funcinfo << "Got buddy: " << bud->id << endl;
1183 ign_list.append( QString( bud->id ) );
1187 emit gotIgnore(ign_list);
1190 void YahooSession::_gotBuddiesReceiver( YList * buds )
1192 kdDebug(14181) << k_funcinfo << endl;
1194 int buddyListCount = 0;
1195 YList *l;
1197 for ( l = buds; l; l = l->next )
1199 struct yahoo_buddy *bud = ( yahoo_buddy* )l->data;
1201 if( !bud )
1203 kdDebug(14181) << k_funcinfo << " Null Buddy" << endl;
1204 continue;
1206 else
1208 kdDebug(14181) << k_funcinfo << " " << bud->id << endl;
1209 emit gotBuddy( QString( bud->id ) , QString::fromLocal8Bit( bud->real_name ),
1210 QString::fromLocal8Bit( bud->group ) );
1211 buddyListCount++;
1215 emit buddyListFetched( buddyListCount );
1218 void YahooSession::_gotIdentitiesReceiver( YList *ids )
1220 kdDebug(14181) << k_funcinfo << endl;
1223 YList *l;
1224 QStringList idslist;
1226 for ( l = ids; l; l = l->next )
1228 char *userid = ( char* ) l->data;
1230 if ( !userid )
1232 kdDebug(14181) << k_funcinfo << endl;
1233 continue;
1235 else
1237 kdDebug(14181) << k_funcinfo << userid << endl;
1238 idslist.append( QString( userid ) );
1242 emit gotIdentities( idslist );
1245 void YahooSession::_statusChangedReceiver( char *who, int stat, char *msg, int away )
1247 // kdDebug(14181) << k_funcinfo << endl;
1249 emit statusChanged( QString::fromLocal8Bit( who ), stat, QString::fromLocal8Bit( msg ), away );
1252 void YahooSession::_gotImReceiver( char *who, char *msg, long tm, int stat, int utf8 )
1254 kdDebug(14181) << k_funcinfo << endl;
1256 QString convertedMessage;
1258 if ( utf8 )
1259 convertedMessage = QString::fromUtf8( msg );
1260 else
1261 convertedMessage = QString::fromLocal8Bit( msg );
1263 if ( convertedMessage == "<ding>" ) {
1264 kdDebug(14181)<<"got BUZZ"<<endl;
1265 emit gotBuzz( QString::fromLocal8Bit( who ), tm );
1267 else {
1268 kdDebug(14181)<<"got IM"<<endl;
1269 emit gotIm( QString::fromLocal8Bit( who ), convertedMessage, tm, stat );
1273 void YahooSession::_gotConfInviteReceiver( char *who, char *room, char *msg, YList *members )
1275 kdDebug(14181) << k_funcinfo << endl;
1277 YList *l;
1278 QStringList member_list;
1280 for ( l = members; l; l = l->next )
1282 char *buddy = ( char* ) l->data;
1283 if ( !buddy )
1285 kdDebug(14181) << k_funcinfo << " Null Id" << endl;
1286 continue;
1288 else
1290 member_list.append( QString::fromLocal8Bit( buddy ) );
1294 emit gotConfInvite( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( room ),
1295 QString::fromLocal8Bit( msg ), member_list) ;
1298 void YahooSession::_confUserDeclineReceiver( char *who, char *room, char *msg )
1300 kdDebug(14181) << k_funcinfo << endl;
1301 emit confUserDecline( QString::fromLocal8Bit(who), QString::fromLocal8Bit( room ),
1302 QString::fromLocal8Bit(msg));
1305 void YahooSession::_confUserJoinReceiver( char *who, char *room )
1308 emit confUserJoin( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( room ) );
1311 void YahooSession::_confUserLeaveReceiver( char *who, char *room )
1314 emit confUserLeave( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( room ) );
1317 void YahooSession::_confMessageReceiver( char *who, char *room, char *msg, int utf8 )
1319 QString convertedMessage;
1321 if ( utf8 )
1322 convertedMessage = QString::fromUtf8( msg );
1323 else
1324 convertedMessage = QString::fromLocal8Bit( msg );
1326 emit confMessage( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( room ), convertedMessage);
1329 void YahooSession::_gotFileReceiver( char *who, char *url, long expires, char *msg,
1330 char *fname, unsigned long fesize )
1333 emit gotFile( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( url ),
1334 expires, QString::fromLocal8Bit( msg ), QString::fromLocal8Bit( fname ), fesize );
1337 void YahooSession::_contactAddedReceiver( char *myid, char *who, char *msg )
1340 emit contactAdded( QString::fromLocal8Bit( myid ), QString::fromLocal8Bit( who ),
1341 QString::fromLocal8Bit( msg ) );
1344 void YahooSession::_rejectedReceiver( char *who, char *msg )
1347 emit rejected( QString::fromLocal8Bit( who ), QString::fromLocal8Bit( msg ) );
1350 void YahooSession::_typingNotifyReceiver( char *who, int stat )
1353 emit typingNotify( QString::fromLocal8Bit( who ), stat );
1356 void YahooSession::_gameNotifyReceiver( char *who, int stat )
1358 emit gameNotify( QString::fromLocal8Bit( who ), stat );
1361 void YahooSession::_mailNotifyReceiver( char *from, char *subj, int cnt )
1363 //kdDebug(14181) << k_funcinfo << " session: " << endl;
1365 emit mailNotify( QString::fromLocal8Bit( from ), QString::fromLocal8Bit( subj ), cnt);
1368 void YahooSession::_systemMessageReceiver( char *msg )
1370 kdDebug(14181) << k_funcinfo << " session: " << endl;
1372 emit systemMessage( QString::fromLocal8Bit( msg ) );
1375 void YahooSession::_errorReceiver( char *err, int fatal )
1377 kdDebug(14181) << k_funcinfo << " session: " << m_connId << endl;
1379 emit error( err, fatal );
1382 int YahooSession::_logReceiver( char */*fmt*/, ... )
1384 kdDebug(14181) << k_funcinfo << endl;
1385 return 0;
1388 int YahooSession::_addHandlerReceiver( int fd, yahoo_input_condition cond, void *data )
1390 kdDebug(14181) << k_funcinfo << " " << m_connId << " Socket: " << fd << endl;
1392 m_data = data;
1393 if ( fd == -1 )
1395 kdDebug(14181) << k_funcinfo << "why is fd -1?" << endl;
1396 return -1;
1399 KExtendedSocket* socket = m_connManager.connectionForFD( fd );
1400 if ( !socket )
1402 kdDebug(14181) << k_funcinfo << "No existing socket for connection found. We're screwed"
1403 << endl;
1404 return -1;
1407 /* This works ONLY IF (YAHOO_INPUT_READ==1 && YAHOO_INPUT_WRITE==2) */
1408 int tag = 0;
1409 if ( cond == YAHOO_INPUT_READ )
1411 kdDebug(14181) << k_funcinfo << " add handler read" << endl;
1412 socket->enableRead( true );
1413 connect ( socket, SIGNAL( readyRead() ), this, SLOT( slotReadReady() ) );
1414 tag = 2*fd + YAHOO_INPUT_READ;
1416 else if ( cond == YAHOO_INPUT_WRITE )
1418 kdDebug(14181) << k_funcinfo << " add handler write" << endl;
1419 socket->enableWrite( true );
1420 connect ( socket, SIGNAL( readyWrite() ), this, SLOT( slotWriteReady() ) );
1421 tag = 2*fd + YAHOO_INPUT_WRITE;
1423 return tag;
1426 void YahooSession::addHandler( int /*fd*/, yahoo_input_condition /*cond*/ )
1430 void YahooSession::_removeHandlerReceiver( int tag )
1432 kdDebug(14181) << k_funcinfo << " " << m_connId << " tag: " << tag << endl;
1434 if ( tag == 0 )
1435 return;
1437 KExtendedSocket* socket = m_connManager.connectionForFD( (tag-1)/2 );
1438 if ( !socket )
1440 kdDebug(14181) << k_funcinfo << "No existing socket for connection found. We're screwed"
1441 << endl;
1442 return;
1444 /* This works ONLY IF (YAHOO_INPUT_READ==1 && YAHOO_INPUT_WRITE==2) */
1445 if( tag % 2 == YAHOO_INPUT_READ ) {
1446 kdDebug(14181) << k_funcinfo << " read off" << endl;
1447 socket->enableRead( false );
1448 disconnect ( socket, SIGNAL( readyRead() ), this, SLOT( slotReadReady() ) );
1450 else
1452 kdDebug(14181) << k_funcinfo << " write off" << endl;
1453 socket->enableWrite( false );
1454 disconnect ( socket, SIGNAL( readyWrite() ), this, SLOT( slotWriteReady() ) );
1458 void YahooSession::removeHandler( int /*fd*/ )
1462 int YahooSessionManager::_hostConnectReceiver( char* /*host*/, int /*port*/ )
1464 kdDebug(14181) << k_funcinfo << endl;
1465 return 0;
1468 int YahooSession::_hostAsyncConnectReceiver( char *host, int port,
1469 yahoo_connect_callback callback, void *callback_data )
1471 struct connect_callback_data *ccd;
1472 int error;
1473 KExtendedSocket* yahooSocket = new KExtendedSocket( host, port );
1475 // TODO Do an async connect in the future
1476 yahooSocket->setTimeout( 30 ); //prevent endless blocking, but an async connect would really be nice
1477 error = yahooSocket->connect();
1479 if ( !error )
1481 kdDebug(14181) << k_funcinfo << " Connected! fd "<< yahooSocket->fd() << endl;
1482 m_connManager.addConnection( yahooSocket );
1483 callback( yahooSocket->fd(), 0, callback_data );
1484 return 0;
1486 else if( error == -1 && errno == EINPROGRESS )
1488 kdDebug(14181) << k_funcinfo << " In progress?" << endl;
1489 m_connManager.addConnection( yahooSocket );
1490 ccd = ( struct connect_callback_data* ) calloc( 1, sizeof( struct connect_callback_data ) );
1491 ccd->callback = callback;
1492 ccd->callback_data = callback_data;
1493 ccd->id = m_connId;
1494 ext_yahoo_add_handler( -1, yahooSocket->fd(), YAHOO_INPUT_WRITE, ccd );
1495 return 1;
1497 else
1499 kdDebug(14181) << k_funcinfo << " Failed!" << endl;
1500 yahooSocket->close();
1501 delete yahooSocket;
1502 yahooSocket = 0L;
1503 _errorReceiver(0, 1);
1504 return -1;
1508 void YahooSession::_gotWebcamInvite( const char* who )
1510 emit gotWebcamInvite( QString::fromLocal8Bit( who ) );
1513 void YahooSession::_gotWebcamImage( const char* who, const unsigned char* image,
1514 unsigned int image_size, unsigned int real_size,
1515 unsigned int timestamp )
1517 m_lastWebcamTimestamp = timestamp;
1518 if ( image_size == 0 || real_size == 0)
1520 // use timestamp to handle syncronization here???
1521 return;
1524 if ( currentImage == NULL )
1526 currentImage = new QBuffer();
1527 currentImage->open(IO_ReadWrite);
1529 currentImage->writeBlock( (char *) image, real_size );
1530 //kdDebug(14181) << " real_size " << real_size << " image_size " << image_size << " timestamp " << timestamp << " " << who << endl;
1532 if ( currentImage->size() == image_size ) {
1533 QPixmap webcamImage;
1534 currentImage->close();
1535 // uncomment the following line when jpc is supported by kdelibs
1536 //webcamImage.loadFromData( currentImage->buffer() );
1538 /****** DELETE below once kdelibs has jpc support ******/
1539 KTempFile jpcTmpImageFile;
1540 KTempFile bmpTmpImageFile;
1541 QFile *file = jpcTmpImageFile.file();;
1542 file->writeBlock((currentImage->buffer()).data(), currentImage->size());
1543 file->close();
1545 KProcess p;
1546 p << "jasper";
1547 p << "--input" << jpcTmpImageFile.name() << "--output" << bmpTmpImageFile.name() << "--output-format" << "bmp";
1549 p.start( KProcess::Block );
1550 if( p.exitStatus() != 0 )
1552 kdDebug(14181) << " jasper exited with status " << p.exitStatus() << " " << who << endl;
1554 else
1556 webcamImage.load( bmpTmpImageFile.name() );
1557 /******* UPTO THIS POINT ******/
1558 kdDebug(14181) << " emitting image " << currentImage->size() << endl;
1559 emit webcamImageReceived( QString::fromLatin1( who ), webcamImage );
1561 QFile::remove(jpcTmpImageFile.name());
1562 QFile::remove(bmpTmpImageFile.name());
1564 delete currentImage;
1565 currentImage = NULL;
1569 void YahooSession::_webcamDisconnected( const char* who, int reason )
1571 kdDebug(14181) << k_funcinfo << "Webcam closed remotely, reason: " << reason << endl;
1572 emit remoteWebcamClosed( QString::fromLocal8Bit( who ), reason );
1576 void YahooSession::slotReadReady()
1578 int ret = 1;
1580 //using sender is the only way to reliably get the socket
1581 const KExtendedSocket* socket = dynamic_cast<const KExtendedSocket*>( sender() );
1582 if ( !socket )
1584 kdDebug(14181) << k_funcinfo << "sender() was not a KExtendedSocket!" << endl;
1585 return;
1588 int fd = socket->fd();
1589 //kdDebug(14181) << k_funcinfo << "Socket FD: " << fd << endl;
1590 ret = yahoo_read_ready( m_connId , fd, m_data );
1592 if ( ret == -1 )
1593 kdDebug(14181) << k_funcinfo << "Read Error (" << errno << ": " << strerror(errno) << endl;
1594 else if ( ret == 0 )
1595 kdDebug(14181) << k_funcinfo << "Server closed socket" << endl;
1599 void YahooSession::slotWriteReady()
1601 int ret = 1;
1603 //using sender is the only way to reliably get the socket
1604 const KExtendedSocket* socket = dynamic_cast<const KExtendedSocket*>( sender() );
1605 if ( !socket )
1607 kdDebug(14181) << k_funcinfo << "sender() was not a KExtendedSocket!" << endl;
1608 return;
1611 int fd = socket->fd();
1612 kdDebug(14181) << k_funcinfo << "Socket FD: " << fd << endl;
1614 ret = yahoo_write_ready( m_connId , fd, m_data );
1616 if ( ret == -1 )
1617 kdDebug(14181) << k_funcinfo << "Read Error (" << errno << ": " << strerror(errno) << endl;
1618 else if ( ret == 0 )
1619 kdDebug(14181) << k_funcinfo << "Server closed socket" << endl;
1622 #include "kyahoo.moc"
1624 // vim: set noet ts=4 sts=4 sw=4:
1625 // kate: indent-mode csands; tab-width 4;