qtmobility/plugins/contacts/symbian/src/cntsymbiandatabase.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 //system includes
       
    42 #include <e32base.h>
       
    43 #include <s32mem.h>
       
    44 
       
    45 //user includes
       
    46 #include "cntsymbiandatabase.h"
       
    47 #include "cntsymbiantransformerror.h"
       
    48 #include "qcontactchangeset.h"
       
    49 #include "qcontactmanagerengine.h"
       
    50 
       
    51 // Constant
       
    52 typedef QPair<QContactLocalId, QContactLocalId> QOwnCardPair;
       
    53 
       
    54 CntSymbianDatabase::CntSymbianDatabase(QContactManagerEngine *engine, QContactManager::Error& error) :
       
    55     m_contactDatabase(0),
       
    56     m_currentOwnCardId(0)
       
    57 {
       
    58     TRAPD(err, m_contactDatabase = CContactDatabase::OpenL())
       
    59 
       
    60     //Database not found, create it
       
    61     if(err == KErrNotFound)
       
    62     {
       
    63         TRAP(err, m_contactDatabase = CContactDatabase::CreateL())
       
    64     }
       
    65 
       
    66     //Database opened successfully
       
    67     if (err == KErrNone)
       
    68     {
       
    69     // In pre 10.1 platforms the AddObserverL & RemoveObserver functions are not
       
    70     // exported so we need to use CContactChangeNotifier.
       
    71 #ifndef SYMBIAN_BACKEND_USE_SQLITE
       
    72         TRAP(err, m_contactChangeNotifier = CContactChangeNotifier::NewL(*m_contactDatabase, this));
       
    73 #else
       
    74         TRAP(err, m_contactDatabase->AddObserverL(*this));
       
    75 #endif
       
    76         if (err == KErrNone)
       
    77             m_engine = engine;
       
    78 
       
    79         // Read current own card id (self contact id)
       
    80         TContactItemId myCard = m_contactDatabase->OwnCardId();
       
    81         if (myCard > 0)
       
    82             m_currentOwnCardId = QContactLocalId(myCard);
       
    83     }
       
    84     CntSymbianTransformError::transformError(err, error);
       
    85 }
       
    86 
       
    87 CntSymbianDatabase::~CntSymbianDatabase()
       
    88 {
       
    89     m_engine = NULL;
       
    90 #ifndef SYMBIAN_BACKEND_USE_SQLITE
       
    91     delete m_contactChangeNotifier;
       
    92 #else
       
    93     if (m_contactDatabase != 0) {
       
    94         m_contactDatabase->RemoveObserver(*this);
       
    95     }
       
    96 #endif
       
    97     delete m_contactDatabase;
       
    98 }
       
    99 
       
   100 CContactDatabase* CntSymbianDatabase::contactDatabase()
       
   101 {
       
   102     return m_contactDatabase;
       
   103 }
       
   104 
       
   105 void CntSymbianDatabase::appendContactsEmitted(const QList<QContactLocalId> &contactList)
       
   106 {
       
   107     m_contactsEmitted += contactList;
       
   108 }
       
   109 
       
   110 void CntSymbianDatabase::appendContactEmitted(QContactLocalId id)
       
   111 {
       
   112     m_contactsEmitted.append(id);
       
   113 }
       
   114 
       
   115 /*!
       
   116  * Respond to a contacts database event, delegating this event to
       
   117  * an appropriate signal as required.
       
   118  *
       
   119  * \param aEvent Contacts database event describing the change to the
       
   120  *  database.
       
   121  */
       
   122 void CntSymbianDatabase::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
   123 {
       
   124     QContactChangeSet changeSet;
       
   125     TContactItemId id = aEvent.iContactId;
       
   126 
       
   127     switch (aEvent.iType)
       
   128     {
       
   129     case EContactDbObserverEventContactAdded:
       
   130         if(m_contactsEmitted.contains(id))
       
   131             m_contactsEmitted.removeOne(id);
       
   132         else
       
   133             changeSet.addedContacts().insert(id);
       
   134         break;
       
   135     case EContactDbObserverEventOwnCardDeleted:
       
   136     case EContactDbObserverEventContactDeleted:
       
   137         if(m_contactsEmitted.contains(id))
       
   138             m_contactsEmitted.removeOne(id);
       
   139         else
       
   140             changeSet.removedContacts().insert(id);
       
   141         break;
       
   142     case EContactDbObserverEventContactChanged:
       
   143         if(m_contactsEmitted.contains(id))
       
   144             m_contactsEmitted.removeOne(id);
       
   145         else
       
   146             changeSet.changedContacts().insert(id);
       
   147         break;
       
   148     case EContactDbObserverEventGroupAdded:
       
   149         if(m_contactsEmitted.contains(id))
       
   150             m_contactsEmitted.removeOne(id);
       
   151         else
       
   152             changeSet.addedRelationshipsContacts().insert(id);
       
   153         break;
       
   154     case EContactDbObserverEventGroupDeleted:
       
   155         if(m_contactsEmitted.contains(id))
       
   156             m_contactsEmitted.removeOne(id);
       
   157         else
       
   158             changeSet.removedRelationshipsContacts().insert(id);
       
   159         break;
       
   160     case EContactDbObserverEventGroupChanged:
       
   161         if(m_contactsEmitted.contains(id))
       
   162             m_contactsEmitted.removeOne(id);
       
   163         else
       
   164             changeSet.changedContacts().insert(id); //group is a contact
       
   165         break;
       
   166     case EContactDbObserverEventOwnCardChanged:
       
   167         {
       
   168             QOwnCardPair ownCard(m_currentOwnCardId, QContactLocalId(id));
       
   169             changeSet.oldAndNewSelfContactId() = ownCard;
       
   170             m_currentOwnCardId = QContactLocalId(id);
       
   171             break;
       
   172         }
       
   173     default:
       
   174         break; // ignore other events
       
   175     }
       
   176 
       
   177     changeSet.emitSignals(m_engine);
       
   178 }
       
   179 
       
   180 
       
   181 
       
   182 
       
   183 
       
   184