~ubuntu-branches/ubuntu/karmic/eva/karmic

« back to all changes in this revision

Viewing changes to src/api/evauhmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): ZhengPeng Hou
  • Date: 2008-02-15 12:41:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080215124106-x3mvc2nharfsl99l
Tags: 0.4.9+svn20080215-0ubuntu1
New upstream SVN release, upstream has switched to
svn now. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include "evauhmanager.h"
22
22
#include "evauhpacket.h"
23
23
#include "evauhprotocols.h"
 
24
#include "evamain.h"
 
25
#include "evaresource.h"
24
26
#include <qfile.h>
25
27
#include <qdatastream.h>
26
28
#include <qtextstream.h>
30
32
#include <qapplication.h>
31
33
#include <string.h>
32
34
 
33
 
#define MaxBlockSize      800
34
 
 
35
 
#define CFACE_SERVER      "cface_tms.qq.com"
36
 
#define CFACE_PORT        4000
37
 
 
38
 
#define UH_PROFILE_NAME   "evauh.profile"
39
 
 
40
 
#define UH_TIME_OUT       30
 
35
#define MaxBlockSize       800
 
36
 
 
37
#define CFACE_SERVER       "cface_tms.qq.com"
 
38
#define CFACE_PORT         4000
 
39
 
 
40
#define UH_PROFILE_NAME    "evauh.profile"
 
41
 
 
42
#define UH_TIME_OUT        30
 
43
#define UH_MAX_RETRY_COUNT 4
41
44
 
42
45
// UH represents User Head which is used in Tencent QQ, sounds pretty @!#~*!$@# :)
43
46
class EvaUHFile {
403
406
/************************************************************************************************/
404
407
 
405
408
EvaUHManager::EvaUHManager(QObject *receiver, const QString &dir)
406
 
        : QObject(), QThread(), mReceiver(receiver), AllInfoGotCounter(0), 
 
409
        : QObject(), QThread(), mReceiver(receiver), m_retryCount(0), AllInfoGotCounter(0), 
407
410
        mUHDir(dir), mAskForStop(false), mSocket(NULL), 
408
411
        mProfileManager(NULL), mCurrentFile(NULL), mDns(NULL)
409
412
{
416
419
        mProfileManager = NULL;
417
420
}
418
421
 
419
 
void EvaUHManager::initiate()
 
422
void EvaUHManager::initiate(QSize size)
420
423
{
421
424
        if(mProfileManager) delete mProfileManager;
422
425
        mProfileManager = new EvaUHProfile();
423
426
        mProfileManager->dir = mUHDir;
424
427
        if(mProfileManager->loadProfile()){
 
428
                imageOnList.clear();
425
429
                QMap<unsigned int, UHInfoItem>::Iterator it = mProfileManager->list.begin();
426
430
                while(it != mProfileManager->list.end()){
427
431
                        QString filename = getFileName(it.data().id);
 
432
                        //printf("User Head: id(%d), data.id(%d), file(%s)\n", it.key(), it.data().id, filename.local8Bit().data());
 
433
//                      imageOnList[it.key()] = QImage(filename).smoothScale(size);
428
434
                        imageOnList[it.key()] = QImage(filename);
429
 
                        if(imageOnList[it.key()].isNull()) printf("EvaUHManager::initiate() -- NULL Image :%s\n", filename.ascii());
 
435
                        if(imageOnList[it.key()].isNull()) {
 
436
                                printf("EvaUHManager::initiate() -- buddy %d has a NULL Image :%s, remove it from evauh.profile\n", it.key(), filename.ascii());
 
437
                                mProfileManager->remove(it.key());
 
438
                                mProfileManager->saveProfile();
 
439
                                imageOnList.clear();
 
440
                                initiate(); // do it again
 
441
                                return;
 
442
                        }
430
443
                        filename = getFileName(it.data().id, true);
 
444
//                      imageOffList[it.key()] = QImage(filename).smoothScale(size);
431
445
                        imageOffList[it.key()] = QImage(filename);
432
446
                        ++it;
433
447
                }
454
468
                }
455
469
                if(mAskForStop) break;
456
470
        }
457
 
        printf("EvaUHManager runs finished\n");
 
471
        //printf("EvaUHManager runs finished\n");
458
472
        cleanUp();
459
473
}
460
474
 
467
481
        
468
482
        while(!mHostAddresses.size()){
469
483
                if(mAskForStop) break;
470
 
                printf("EvaUHManager::doDnsRequest -- waiting DNS\n");
 
484
                //printf("EvaUHManager::doDnsRequest -- waiting DNS\n");
471
485
                sleep(1);
472
486
        }
473
487
}
474
488
 
475
489
void EvaUHManager::slotDnsReady()
476
490
{
477
 
        printf("EvaUHManager::slotDnsReady ----   got\n");
 
491
        //printf("EvaUHManager::slotDnsReady ----   got\n");
478
492
        mHostAddresses = mDns->addresses();
479
493
        if(!mHostAddresses.size()){
480
494
                QHostAddress host;
568
582
                }
569
583
                
570
584
                if(AllInfoGotCounter < mUHList.size()){
 
585
                        m_retryCount = 0;
571
586
                        doAllInfoRequest();
572
587
                }else{
573
588
                        // now we start download User Head :)
585
600
        if(!(item.id))  // if id == 0, no file need download, we finish
586
601
                return false;
587
602
 
 
603
        m_retryCount++;
588
604
        std::list<unsigned int> qqList;
589
605
        qqList.push_back(item.id);
590
606
        EvaUHInfoRequest *packet = new EvaUHInfoRequest();
599
615
                                const unsigned  int start, const unsigned  int end)
600
616
{
601
617
        //printf("EvaUHManager::doTransferRequest:\n\tid: %d, sid: %8x, s: %8x, e: %8x\n", id, sid, start, end);
 
618
        m_retryCount++;
602
619
        EvaUHTransferRequest *packet = new EvaUHTransferRequest();
603
620
        packet->setUHInfo(id, sid);
604
621
        packet->setFileInfo(start, end);
609
626
 
610
627
void EvaUHManager::processBuddyInfoReply()
611
628
{
612
 
        //printf("EvaUHManager::processBuddyInfoReply\n");
613
629
        if(!mProfileManager) return;
614
630
        EvaUHInfoReply *packet = new EvaUHInfoReply((unsigned char *)mBuffer, bytesRead);
615
631
        if(packet->parse()){         //  good packet 
616
632
                if(!packet->isSuccessful()){ // no UH picture available
 
633
                        printf("EvaUHManager::processBuddyInfoReply -- no UH picture available\n");
617
634
                        delete packet;
618
635
                        return;
619
636
                }
620
637
                std::list<UHInfoItem> list= packet->getInfoItems();
621
638
                std::list<UHInfoItem>::iterator it = list.begin(); // actually this should be at most one element
622
639
                //FIXME we should check the new information
623
 
                //if(mProfileManager) mProfileManager->updateInfo(*it);
 
640
                if(mProfileManager) mProfileManager->updateInfo(*it);
624
641
                
625
642
                
626
643
                // now we start download User Head :)
627
644
                UHInfoItem item = mProfileManager->nextDownload();
628
645
                if(!(item.id)) {  // if id == 0, no file need download, we finish
629
646
                        mAskForStop = true;
 
647
                        m_retryCount = 0;
630
648
                        delete packet;
631
649
                        return;
632
650
                }
636
654
                mCurrentFile->setFileInfo(item.md5, item.sessionId);
637
655
                unsigned int fstart, fend;
638
656
                mCurrentFile->nextBlock(&fstart, &fend);
 
657
                m_retryCount = 0;
639
658
                doTransferRequest(item.id, item.sessionId, fstart, fend);
640
659
        }
641
660
        delete packet;
653
672
                if(mCurrentFile){
654
673
                        //printf("EvaUHManager::processBuddyFileReply -- No Pkts:%d,  Pkt No:%d\n", 
655
674
                        //                      packet->getNumPackets(), packet->getPacketNum());
 
675
                        m_retryCount = 0;
656
676
                        mCurrentFile->setFileBlock(packet->getNumPackets(), packet->getPacketNum(),
657
677
                                                packet->getFileSize(), packet->getStart(), 
658
678
                                                packet->getPartSize(), packet->getPartData() );
662
682
                                        mProfileManager->fileFinished(packet->getQQ());
663
683
                                        mProfileManager->saveProfile();
664
684
                                        
665
 
                                        QImage imgOn  = QImage(getFileName(packet->getQQ()));
666
 
                                        QImage imgOff = QImage(getFileName(packet->getQQ(), true));
 
685
                                        QImage imgOn  = QImage(getFileName(packet->getQQ())).smoothScale(EvaMain::global->getFaceSize());
 
686
                                        QImage imgOff = QImage(getFileName(packet->getQQ(), true)).smoothScale(EvaMain::global->getFaceSize());
667
687
                                        imageOnList[packet->getQQ()]  = imgOn;
668
688
                                        imageOffList[packet->getQQ()] = imgOff;
669
689
                                        EvaUHReadyEvent *event = new EvaUHReadyEvent();
673
693
                                        
674
694
                                        delete mCurrentFile;
675
695
                                        mCurrentFile = NULL;
676
 
                                        // hah, let GUI know we got one pic!
677
 
                                        //QApplication::postEvent();
678
696
                                        // now we download next User Head
679
697
                                        if(!doInfoRequest()){    // return false means all done
680
698
                                                mAskForStop = true;
681
699
                                                cmdSent = All_Done;
682
700
                                        }
683
 
                                }else
 
701
                                }else{
 
702
                                        fprintf(stderr, "[EvaUHManager] md5 checking error\n");
684
703
                                        mCurrentFile->initSettings(); // we clear all contents of the file
 
704
                                }
685
705
                        }
686
706
                }
687
707
        }
690
710
void EvaUHManager::checkTimeout()
691
711
{
692
712
        if(mAskForStop || cmdSent == All_Done) return;
 
713
        
 
714
        if(m_retryCount >= UH_MAX_RETRY_COUNT){
 
715
                UHInfoItem item = mProfileManager->nextDownload();
 
716
                mProfileManager->fileFinished(item.id);
 
717
                // we won't save the non-image user head
 
718
                //mProfileManager->saveProfile();
 
719
                fprintf(stderr, "[EvaUHManager] downloading user(%d) avator error, try next one\n", item.id);
 
720
                m_retryCount = 0;
 
721
                cmdSent = Buddy_Info;
 
722
        }
 
723
        
693
724
        int last = timeSent.secsTo(QDateTime::currentDateTime());
694
725
        if(last >= UH_TIME_OUT){
695
726
                //printf("EvaUHManager::checkTimeout : %d\n", cmdSent);
702
733
                                mAskForStop = true;
703
734
                        break;
704
735
                case Buddy_File:{
705
 
                        
706
736
                        if(!mCurrentFile){
707
737
                                UHInfoItem item = mProfileManager->nextDownload();
708
738
                                mCurrentFile = new EvaUHFile(item.id);
724
754
 
725
755
void EvaUHManager::cleanUp()
726
756
{
727
 
        printf("EvaUHManager::cleanUp\n");
728
757
        AllInfoGotCounter = 0;
729
758
        if(mSocket ) delete mSocket;
730
759
        mSocket = NULL;