phonebookui/cntlistmodel/cntpresenceinfoprovider.cpp
changeset 81 640d30f4fb64
parent 46 efe85016a067
equal deleted inserted replaced
77:c18f9fa7f42e 81:640d30f4fb64
       
     1 /*
       
     2 * Copyright (c) 2010 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: Presence info provider plugin for CntListModel. It can provide
       
    15 *              the presence information of a contact (icon2 field).
       
    16 *
       
    17 */
       
    18 
       
    19 #include <qtcontacts.h>
       
    20 #include "cntpresenceinfoprovider.h"
       
    21 
       
    22 #include <prcpresencebuddyinfo_qt.h>
       
    23 #include <prcpresencereader_qt.h>
       
    24 
       
    25 CntPresenceInfoProvider::CntPresenceInfoProvider() :
       
    26     iReader(NULL),
       
    27     mManager(NULL)
       
    28 {
       
    29     iReader = PrcPresenceReader::createReader();
       
    30     connect(iReader, SIGNAL(signalPresenceNotification(bool , PrcPresenceBuddyInfoQt*)), 
       
    31                            this, SLOT(handlePresenceUpdate(bool , PrcPresenceBuddyInfoQt*)));
       
    32     
       
    33     mManager = new QContactManager("symbian");
       
    34 }
       
    35 
       
    36 CntPresenceInfoProvider::~CntPresenceInfoProvider()
       
    37 {
       
    38     delete iReader;
       
    39     delete mManager;
       
    40 }
       
    41 
       
    42 /*!
       
    43     /return the info fields supported by this provider
       
    44  */
       
    45 ContactInfoFields CntPresenceInfoProvider::supportedFields() const
       
    46 {
       
    47     // this provider has only info for the icon2 field
       
    48     return ContactInfoIcon2Field;
       
    49 }
       
    50 
       
    51 /*!
       
    52     The contact contains all the info this provider needs, so signals with the requested info
       
    53     fields are emitted immediately.
       
    54 
       
    55     /param contact the contact for which info is requested
       
    56     /param requestedInfo one or more of the flags in ContactInfoFields
       
    57  */
       
    58 void CntPresenceInfoProvider::requestInfo(const QContact& contact, ContactInfoFields requestedInfo)
       
    59 {
       
    60     if (requestedInfo & ContactInfoIcon2Field)
       
    61     {
       
    62         QList<QContactOnlineAccount> accounts = contact.details<QContactOnlineAccount>();
       
    63         
       
    64         QList<PrcPresenceBuddyInfoQt*> buddies;
       
    65         
       
    66         foreach (QContactOnlineAccount account, accounts)
       
    67         {
       
    68             QString fullAccount = account.serviceProvider() + ':' + account.accountUri();
       
    69             PrcPresenceBuddyInfoQt* buddy = iReader->presenceInfo(fullAccount);
       
    70             
       
    71             if (buddy)
       
    72             {
       
    73                 buddies.append(buddy);
       
    74                 if (!mBuddyMap.contains(buddy->buddyId()))
       
    75                 {
       
    76                     mBuddyMap.insert(buddy->buddyId(), contact.localId());
       
    77                     iReader->subscribePresenceBuddyChange(buddy->buddyId());
       
    78                 }
       
    79             }
       
    80         }
       
    81         
       
    82         if (buddies.count())
       
    83         {
       
    84             QString icon = parsePresence(buddies);
       
    85             
       
    86             if (!icon.isEmpty())
       
    87             {
       
    88                 emit infoFieldReady(this, contact.localId(), ContactInfoIcon2Field, icon);
       
    89             }
       
    90             
       
    91             qDeleteAll(buddies);
       
    92         }
       
    93     }
       
    94 }
       
    95 
       
    96 /*!
       
    97     Update presence icon for contact if needed. Stop listening to presence changes for buddy
       
    98     if online account detail was deleted.
       
    99 
       
   100     /param aErrorCode error (if any)
       
   101     /param aPresenceBuddyInfo presence buddy that was updated
       
   102  */
       
   103 void CntPresenceInfoProvider::handlePresenceUpdate(bool aSuccess, PrcPresenceBuddyInfoQt* aPresenceBuddyInfo)
       
   104 {
       
   105     if (aSuccess && aPresenceBuddyInfo != NULL)
       
   106     {
       
   107         QContactLocalId id = mBuddyMap.value(aPresenceBuddyInfo->buddyId());
       
   108         QContact contact = mManager->contact(id);
       
   109         
       
   110         QList<QContactOnlineAccount> accounts = contact.details<QContactOnlineAccount>();
       
   111         
       
   112         QList<PrcPresenceBuddyInfoQt*> buddies;
       
   113         bool accountFound = false;
       
   114         
       
   115         foreach (QContactOnlineAccount account, accounts)
       
   116         {
       
   117             QString fullAccount = account.serviceProvider() + ':' + account.accountUri();
       
   118             PrcPresenceBuddyInfoQt* buddy = iReader->presenceInfo(fullAccount);
       
   119             
       
   120             if (buddy)
       
   121             {
       
   122                 buddies.append(buddy);
       
   123                 
       
   124                 if (fullAccount == aPresenceBuddyInfo->buddyId())
       
   125                 {
       
   126                     accountFound = true;
       
   127                 }
       
   128                 
       
   129                 if (!mBuddyMap.contains(buddy->buddyId()))
       
   130                 {
       
   131                     mBuddyMap.insert(buddy->buddyId(), contact.localId());
       
   132                     iReader->subscribePresenceBuddyChange(buddy->buddyId());
       
   133                 }
       
   134             }
       
   135         }
       
   136         
       
   137         // Account was removed, no need to listen to presence changes anymore
       
   138         if (accounts.isEmpty() || !accountFound)
       
   139         {
       
   140             mBuddyMap.remove(aPresenceBuddyInfo->buddyId());
       
   141             iReader->unSubscribePresenceBuddyChange(aPresenceBuddyInfo->buddyId());
       
   142         }
       
   143 
       
   144         if (id > 0)
       
   145         {
       
   146             QString icon = parsePresence(buddies);
       
   147             emit infoFieldReady(this, id, ContactInfoIcon2Field, icon);
       
   148         }
       
   149 
       
   150         qDeleteAll(buddies);
       
   151     }
       
   152 }
       
   153 
       
   154 /*!
       
   155     Parse the required presence icon from multiple accounts.
       
   156 
       
   157     /param buddyList list of buddies
       
   158  */
       
   159 QString CntPresenceInfoProvider::parsePresence(const QList<PrcPresenceBuddyInfoQt*>& buddyList)
       
   160 {
       
   161     foreach (PrcPresenceBuddyInfoQt* buddy, buddyList)
       
   162     {
       
   163         PrcPresenceBuddyInfoQt::AvailabilityValues availability = buddy->availability();
       
   164         
       
   165         if (availability == PrcPresenceBuddyInfoQt::PrcAvailable)
       
   166         {
       
   167             return QString("qtg_small_online");
       
   168         }
       
   169     }
       
   170     
       
   171     return QString();
       
   172 }