phonebookui/pbkcommonui/src/cntpresencelistener.cpp
changeset 46 efe85016a067
child 50 77bc263e1626
equal deleted inserted replaced
40:b46a585f6909 46:efe85016a067
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntpresencelistener.h"
       
    19 
       
    20 #include <prcpresencebuddyinfo_qt.h>
       
    21 #include <prcpresencereader_qt.h>
       
    22 
       
    23 CntPresenceListener::CntPresenceListener(const QContact& contact, QObject* parent) :
       
    24     QObject(parent),
       
    25     mReader(NULL),
       
    26     mContact(contact)
       
    27 {
       
    28     mReader = PrcPresenceReader::createReader();
       
    29     connect(mReader, SIGNAL(signalPresenceNotification(bool, PrcPresenceBuddyInfoQt*)), 
       
    30                            this, SLOT(handlePresenceUpdate(bool, PrcPresenceBuddyInfoQt*)));
       
    31 }
       
    32 
       
    33 CntPresenceListener::~CntPresenceListener()
       
    34 {
       
    35     delete mReader;
       
    36     mReader = NULL;
       
    37 }
       
    38 
       
    39 QMap<QString, bool> CntPresenceListener::initialPresences(bool &combinedOnlineStatus)
       
    40 {
       
    41     QMap<QString, bool> initialMap;
       
    42 
       
    43     QList<QContactOnlineAccount> accounts = mContact.details<QContactOnlineAccount>();
       
    44     
       
    45     QList<PrcPresenceBuddyInfoQt*> buddies;
       
    46     
       
    47     foreach (QContactOnlineAccount account, accounts)
       
    48     {
       
    49         QString fullAccount = account.serviceProvider() + ':' + account.accountUri();
       
    50         PrcPresenceBuddyInfoQt* buddy = mReader->presenceInfo(fullAccount);
       
    51         
       
    52         if (buddy)
       
    53         {
       
    54             buddies.append(buddy);
       
    55             if (!mAccountList.contains(buddy->buddyId()))
       
    56             {
       
    57                 bool isAvailable = (buddy->availability() == PrcPresenceBuddyInfoQt::PrcAvailable);
       
    58                 initialMap.insert(fullAccount, isAvailable);
       
    59                 mAccountList.append(buddy->buddyId());
       
    60                 mReader->subscribePresenceBuddyChange(buddy->buddyId());
       
    61             }
       
    62         }
       
    63     }
       
    64     
       
    65     combinedOnlineStatus = parsePresence(buddies);
       
    66     qDeleteAll(buddies);
       
    67     
       
    68     return initialMap;
       
    69 }
       
    70     
       
    71 void CntPresenceListener::handlePresenceUpdate(bool aSuccess, PrcPresenceBuddyInfoQt* aPresenceBuddyInfo)
       
    72 {
       
    73     if (aSuccess && aPresenceBuddyInfo != NULL)
       
    74     {
       
    75         if (mAccountList.contains(aPresenceBuddyInfo->buddyId()))
       
    76         {
       
    77             // First emit the account-specific presence updated signal
       
    78             bool isAvailable = (aPresenceBuddyInfo->availability() == PrcPresenceBuddyInfoQt::PrcAvailable);
       
    79             emit accountPresenceUpdated(aPresenceBuddyInfo->buddyId(), isAvailable);
       
    80             
       
    81             QList<PrcPresenceBuddyInfoQt*> buddies;
       
    82 
       
    83             foreach (QString account, mAccountList)
       
    84             {
       
    85                 PrcPresenceBuddyInfoQt* buddy = mReader->presenceInfo(account);
       
    86 
       
    87                 if (buddy)
       
    88                 {
       
    89                     buddies.append(buddy);
       
    90                 }
       
    91             }
       
    92             
       
    93             // emit the combined presence status
       
    94             emit fullPresenceUpdated(parsePresence(buddies));
       
    95             
       
    96             qDeleteAll(buddies);
       
    97         }
       
    98     }
       
    99 }
       
   100     
       
   101 bool CntPresenceListener::parsePresence(QList<PrcPresenceBuddyInfoQt*> buddyList)
       
   102 {
       
   103     foreach (PrcPresenceBuddyInfoQt* buddy, buddyList)
       
   104     {
       
   105         PrcPresenceBuddyInfoQt::AvailabilityValues availability = buddy->availability();
       
   106         
       
   107         if (availability == PrcPresenceBuddyInfoQt::PrcAvailable)
       
   108         {
       
   109             return true;
       
   110         }
       
   111     }
       
   112     
       
   113     return false;
       
   114 }
       
   115 
       
   116 // EOF