phonebookui/cnthistorymodel/src/cnthistorymodel_p.cpp
branchRCL_3
changeset 62 5b6f26637ad3
equal deleted inserted replaced
58:d4f567ce2e7c 62:5b6f26637ad3
       
     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 <QStringList>
       
    19 #include <QtAlgorithms>
       
    20 #include <hbglobal.h>
       
    21 #include <hbicon.h>
       
    22 #include <hbframebackground.h>
       
    23 
       
    24 #include "cnthistorymodel_p.h"
       
    25 #include "cntdebug.h"
       
    26 
       
    27 
       
    28 // Unnamed namespace for helper functions
       
    29 namespace
       
    30 {
       
    31     bool greaterThan(const HItemPointer& t1, const HItemPointer& t2)
       
    32     {
       
    33         return ((*t1).timeStamp > (*t2).timeStamp);
       
    34     }
       
    35     
       
    36     bool lessThan(const HItemPointer& t1, const HItemPointer& t2)
       
    37     {
       
    38         return ((*t1).timeStamp < (*t2).timeStamp);
       
    39     }
       
    40     
       
    41 #ifdef TRACES
       
    42 #define LOG_ITEM(i) logItem(i)
       
    43 #else
       
    44 #define LOG_ITEM(i)
       
    45 #endif
       
    46     
       
    47     void logItem(const HistoryItem& item)
       
    48     {
       
    49         QStringList l;
       
    50         
       
    51         l << item.message << item.number << item.timeStamp.toString() << item.title;
       
    52         l << (item.flags & CntIncoming ? "incoming" : "outgoing");
       
    53         l << (item.flags & CntUnseen ? "unseen" : "seen");
       
    54         l << (item.flags & CntAttachment ? "attachment" : "");
       
    55         l << (item.flags & CntCallLog ? "call" : "message");
       
    56         if (item.flags & CntReceivedCall)
       
    57             l << "recieved";
       
    58         else if (item.flags & CntMissedCall)
       
    59             l << "missed";
       
    60         else if (item.flags & CntDialledCall)
       
    61             l << "dialled";
       
    62         
       
    63         l.removeAll("");
       
    64         
       
    65         qDebug() << l;
       
    66     }
       
    67 }
       
    68 
       
    69 Q_DECLARE_METATYPE(LogsEvent *)
       
    70 
       
    71 /*!
       
    72  * Construct a new CntHistoryModelPrivatePrivate object to communicate 
       
    73  * with the conversations and logs databases.
       
    74  *
       
    75  * \param contactId History specific to this contact is cached. 
       
    76  * If no contact is specified all the call logs and conversation 
       
    77  * history from all contacts will be cached.
       
    78  */
       
    79 CntHistoryModelPrivate::CntHistoryModelPrivate(QContactLocalId contactId, QContactManager* manager)
       
    80     : QObject(),
       
    81       m_logsModel(NULL),
       
    82       m_logsFilter(NULL),
       
    83       m_AbstractLogsModel(NULL),
       
    84       m_msgHistory(NULL),
       
    85       m_contactId(contactId),
       
    86       m_contactManager(manager),
       
    87       m_isMyCard(false),
       
    88       m_isMarkedAsSeen(false),
       
    89       m_initLogs(false),
       
    90       m_extendedLocale(HbExtendedLocale::system())
       
    91 {
       
    92     // Create the model structure and cache history data from the databases
       
    93     initializeModel();
       
    94 }
       
    95 
       
    96 CntHistoryModelPrivate::~CntHistoryModelPrivate()
       
    97 {
       
    98     if (m_logsModel) {
       
    99         delete m_logsModel;
       
   100         m_logsModel = NULL;
       
   101     }
       
   102     if (m_logsFilter) {
       
   103         delete m_logsFilter;
       
   104         m_logsFilter = NULL;
       
   105     }
       
   106     if (m_msgHistory) {
       
   107         delete m_msgHistory;
       
   108         m_msgHistory = NULL;
       
   109     }
       
   110 }
       
   111 
       
   112 /*!
       
   113  * Return the data to be used by the view or delegates for a particular
       
   114  * item and role.
       
   115  *
       
   116  * \param index The index of the item to return data about.
       
   117  * \param role The data should be relevant to this particular purpose.
       
   118  * \return QVariant The data for the specified index and role.
       
   119  */
       
   120 QVariant CntHistoryModelPrivate::data(const QModelIndex& index, int role) const
       
   121 {
       
   122     // Invalid index
       
   123     int row = index.row();
       
   124     
       
   125     if ( !validateRowIndex(row) )
       
   126         return QVariant();
       
   127     
       
   128     HItemPointer p = m_List.at(row);
       
   129     if ( p.isNull() )
       
   130         return QVariant();
       
   131     
       
   132     switch( role )
       
   133     {       
       
   134         case Qt::DisplayRole:
       
   135             return displayRoleData(*p);
       
   136         case Qt::DecorationRole:
       
   137             return decorationRoleData(*p);
       
   138         case Qt::BackgroundRole:
       
   139             return backgroundRoleData(*p);
       
   140         case CntFlagsRole:
       
   141             return QVariant((*p).flags);
       
   142         case CntPhoneNumberRole:
       
   143             return QVariant((*p).number);
       
   144         case CntConverstaionIdRole:
       
   145             return conversationIdRoleData(row);
       
   146         default:
       
   147             return QVariant();
       
   148     }
       
   149 }
       
   150 
       
   151 /*!
       
   152  * Return the data to be used by the view for a display role.
       
   153  *
       
   154  * \param item The History item to return data about.
       
   155  *  return QVariant List of strings to be displayed on the view.
       
   156  *  The stings can also be NULL
       
   157  *  index 0 Title of the conversation item.
       
   158  *  index 1 Body text
       
   159  *  index 2 Time stamp
       
   160  */
       
   161 QVariant CntHistoryModelPrivate::displayRoleData(const HistoryItem& item) const
       
   162 {
       
   163     QStringList list;
       
   164     HbExtendedLocale locale = m_extendedLocale;
       
   165     
       
   166     if (item.timeStamp.date() == QDateTime::currentDateTime().date())
       
   167     {
       
   168         list << item.title << item.message << locale.format(item.timeStamp.time(), r_qtn_time_usual);
       
   169     }
       
   170     else
       
   171     {
       
   172         list << item.title << item.message << locale.format(item.timeStamp.date(), r_qtn_date_usual);
       
   173     }
       
   174     
       
   175     return QVariant(list);
       
   176 }
       
   177 
       
   178 /*!
       
   179  * Return the data to be used by the view for a decoration role.
       
   180  *
       
   181  * \param item The History item to return data about.
       
   182  *  return QVariant String of the icon path.
       
   183  */
       
   184 QVariant CntHistoryModelPrivate::decorationRoleData(const HistoryItem& item) const
       
   185 {
       
   186     // Messages
       
   187     if (item.flags & CntMessage)
       
   188         return QVariant(HbIcon(MESSAGE_ICON));
       
   189     
       
   190     // Call logs
       
   191     if (item.flags & CntCallLog) {
       
   192         if (item.flags & CntMissedCall)
       
   193             return QVariant(HbIcon(MISSED_CALL_ICON));
       
   194         if (item.flags & CntDialledCall)
       
   195             return QVariant(HbIcon(DAILED_CALL_ICON));
       
   196         if (item.flags & CntReceivedCall)
       
   197             return QVariant(HbIcon(RECEIVED_CALL_ICON));
       
   198     }
       
   199     
       
   200     return QVariant();
       
   201 }
       
   202 
       
   203 /*!
       
   204  * Return the data to be used to draw the background of list items
       
   205  *
       
   206  * \param item The History item to return data about.
       
   207  *  return QVariant HbFrameBackground of the list item.
       
   208  */
       
   209 QVariant CntHistoryModelPrivate::backgroundRoleData(const HistoryItem& item) const
       
   210 {
       
   211     if (item.flags & CntIncoming)
       
   212         return QVariant(HbFrameBackground("qtg_fr_convlist_received_normal", HbFrameDrawer::NinePieces));
       
   213     else
       
   214         return QVariant(HbFrameBackground("qtg_fr_convlist_sent_normal", HbFrameDrawer::NinePieces));
       
   215 }
       
   216 
       
   217 /*!
       
   218  * Return the conversation id of this row if it is a message
       
   219  *
       
   220  * \param item The History item to return data about.
       
   221  *  return QVariant HbFrameBackground of the list item.
       
   222  */
       
   223 QVariant CntHistoryModelPrivate::conversationIdRoleData(const int row) const
       
   224 {
       
   225     HItemPointer p = m_List.at(row);
       
   226     
       
   227     int id(-1);
       
   228     if ( p.data()->flags & CntMessage )
       
   229         id = m_msgMap.key(p, -1);
       
   230     
       
   231     if (id != -1)
       
   232         return QVariant(id);
       
   233     else
       
   234         return QVariant();
       
   235 }
       
   236 
       
   237 /*!
       
   238  * Get the number of rows (conversations) in this model.
       
   239  *
       
   240  * \param parent Optional parent index value.
       
   241  * \return Number of rows in this model.
       
   242  */
       
   243 int CntHistoryModelPrivate::rowCount(const QModelIndex& /*parent*/) const
       
   244 {
       
   245     return m_List.size();
       
   246 }
       
   247 
       
   248 /*!
       
   249  * Sort list items on the model.
       
   250  *
       
   251  * \param column Column to be sorted. It is not used.
       
   252  * \param order Order to sort the list items.
       
   253  */
       
   254 void CntHistoryModelPrivate::sort(int /*column*/, Qt::SortOrder order)
       
   255 {
       
   256     if ( order == Qt::AscendingOrder )
       
   257         qStableSort(m_List.begin(), m_List.end(), lessThan);
       
   258     else
       
   259         qStableSort(m_List.begin(), m_List.end(), greaterThan);
       
   260 }
       
   261 
       
   262 /*!
       
   263  * Clear history from the database. If the history cached
       
   264  * is specific to one contact, only that history is cleared.
       
   265  * 
       
   266  */
       
   267 void CntHistoryModelPrivate::clearHistory()
       
   268 {
       
   269     Q_Q(CntHistoryModel);
       
   270     
       
   271     if ( m_List.isEmpty() )
       
   272         return;
       
   273     
       
   274     // Call logs
       
   275     if ( !m_isMyCard && m_logsFilter )
       
   276         m_logsFilter->clearEvents();
       
   277     else if ( m_logsModel )
       
   278         m_logsModel->clearList(LogsModel::TypeLogsClearAll);
       
   279     
       
   280     // Messages
       
   281     if (m_msgHistory)
       
   282         m_msgHistory->clearMessages( (int)m_contactId );
       
   283     
       
   284     // Clear all data from the history model.
       
   285     int count = rowCount();
       
   286     
       
   287     m_List.clear();
       
   288     m_msgMap.clear();
       
   289     m_logsMap.clear();
       
   290     
       
   291     q->doBeginRemoveRows( QModelIndex(), 0, count );
       
   292     q->doEndRemoveRows();
       
   293 }
       
   294 
       
   295 /*!
       
   296  * Mark all the conversations in the view as seen.
       
   297  * 
       
   298  */
       
   299 void CntHistoryModelPrivate::markAllAsSeen()
       
   300 {
       
   301     if ( m_isMarkedAsSeen )
       
   302         return;
       
   303     
       
   304     // Messages
       
   305     if (m_msgHistory->markRead( m_contactId ))
       
   306         m_isMarkedAsSeen = true;
       
   307 }
       
   308 
       
   309 /*!
       
   310  * Create the model structure and cache history data from
       
   311  * conversations and call logs databases.
       
   312  *
       
   313  */
       
   314 void CntHistoryModelPrivate::initializeModel()
       
   315 {
       
   316     initializeLogsModel();
       
   317     initializeMsgModel();
       
   318 }
       
   319 
       
   320 void CntHistoryModelPrivate::initializeMsgModel()
       
   321 {
       
   322     if( m_isMyCard )
       
   323         return;
       
   324     
       
   325     // Contact centric
       
   326     MsgHistory* m = new MsgHistory();
       
   327     
       
   328     m_msgHistory = m;
       
   329     
       
   330     // Connect to signals emitted by msg model
       
   331     connect(m, SIGNAL(messagesReady(QList<MsgItem>& )), this, SLOT(messagesReady(QList<MsgItem>& )));
       
   332     connect(m, SIGNAL(messageAdded(MsgItem& )), this, SLOT(messageAdded(MsgItem& )));
       
   333     connect(m, SIGNAL(messageChanged(MsgItem& )), this, SLOT(messageChanged(MsgItem& )));
       
   334     connect(m, SIGNAL(messageDeleted(MsgItem& )), this, SLOT(messageDeleted(MsgItem& )));
       
   335     
       
   336     // Subscribe to get new messages
       
   337     // received from this contact
       
   338     m->subscribe(m_contactId);
       
   339     
       
   340     // Initial fetch of all messages
       
   341     m->getMessages(m_contactId);
       
   342 }
       
   343 
       
   344 void CntHistoryModelPrivate::initializeLogsModel()
       
   345 {
       
   346     //populate model with call events
       
   347     m_logsModel = new LogsModel(LogsModel::LogsFullModel);
       
   348     if (!m_isMyCard) {
       
   349         //do the filtering to get call events for the target contact
       
   350         m_logsFilter = new LogsCustomFilter;
       
   351         m_logsFilter->setContactId(m_contactId);
       
   352         m_logsFilter->setSourceModel(m_logsModel);
       
   353         m_AbstractLogsModel = m_logsFilter;
       
   354     } else {
       
   355         //get all call events
       
   356         m_AbstractLogsModel = m_logsModel;
       
   357     }
       
   358     
       
   359     //read first call events if any and start listening for more 
       
   360     for ( int i = 0; i < m_AbstractLogsModel->rowCount(); ++i ) {
       
   361         LogsEvent* event = qVariantValue<LogsEvent*>(
       
   362                 m_AbstractLogsModel->data(m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) );
       
   363         
       
   364         if ( event ) {
       
   365             HItemPointer item = HItemPointer(new HistoryItem());
       
   366             readLogEvent(event, *item);
       
   367             m_logsMap.insert(i, item);
       
   368             m_List.append( item );
       
   369         }
       
   370     }
       
   371     
       
   372     connect(m_AbstractLogsModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), 
       
   373             this, SLOT(logsRowsInserted(const QModelIndex &, int, int)));
       
   374     connect(m_AbstractLogsModel, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), 
       
   375             this, SLOT(logsRowsRemoved(const QModelIndex &, int, int)));
       
   376     connect(m_AbstractLogsModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), 
       
   377                 this, SLOT(logsDataChanged(const QModelIndex &, const QModelIndex &)));
       
   378     connect(m_AbstractLogsModel, SIGNAL(modelReset()), this, SLOT(handleLogsReset()));
       
   379 
       
   380 }
       
   381 
       
   382 /*!
       
   383  * Read call event into a history item
       
   384  *
       
   385  * \param event Call log event
       
   386  * \param item Conversation history item
       
   387  */
       
   388 void CntHistoryModelPrivate::readLogEvent(LogsEvent* event, HistoryItem& item)
       
   389 {    
       
   390     QString bodyText;
       
   391     QString title;
       
   392     
       
   393     if ( m_isMyCard ) {
       
   394         if ( event->remoteParty().length() > 0 ) {
       
   395             title = QString(event->remoteParty());
       
   396         }
       
   397         else {
       
   398             title = QString(event->number());
       
   399         }
       
   400     } else {
       
   401         if ( event->direction() == LogsEvent::DirIn ) {
       
   402             bodyText = hbTrId("txt_phob_list_received");
       
   403             item.flags |= CntReceivedCall;
       
   404         } else if ( event->direction() == LogsEvent::DirOut ) {
       
   405             bodyText = hbTrId("txt_phob_list_dialled_call");
       
   406             item.flags |= CntDialledCall;
       
   407         } else if ( event->direction() == LogsEvent::DirMissed ) {
       
   408             bodyText = hbTrId("txt_phob_list_missed_call");
       
   409             item.flags |= CntMissedCall;
       
   410         }
       
   411     }
       
   412 
       
   413     if ( event->direction() == LogsEvent::DirMissed
       
   414         || event->direction() == LogsEvent::DirIn ) {
       
   415         item.flags |= CntIncoming;
       
   416     } else {
       
   417         item.flags &= ~CntIncoming;
       
   418     }
       
   419     
       
   420     item.message = bodyText;
       
   421     item.title = title;
       
   422     item.timeStamp = event->time().toLocalTime();
       
   423     item.flags |= CntCallLog;
       
   424     item.number = QString(event->number());
       
   425     
       
   426     LOG_ITEM(item);
       
   427 }
       
   428 
       
   429 /*!
       
   430  * Slot used for receiving new rows from the LogsModel.
       
   431  *
       
   432  * \param parent Optional parent index value.
       
   433  * \param first The first row item to be received from the model.
       
   434  * \param last The last row item to be received from the model.
       
   435  */
       
   436 void CntHistoryModelPrivate::logsRowsInserted(const QModelIndex& /*parent*/, int first, int last)
       
   437 {
       
   438     Q_Q(CntHistoryModel);
       
   439     
       
   440     int oldRowCount = rowCount();
       
   441     QList<HItemPointer> l;
       
   442     
       
   443     for ( int i = first; i < m_AbstractLogsModel->rowCount() && i <= last; ++i ) {
       
   444         LogsEvent* event = qVariantValue<LogsEvent*>(
       
   445                 m_AbstractLogsModel->data(m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) );
       
   446         
       
   447         if ( event ) {
       
   448             HItemPointer item(new HistoryItem());
       
   449             readLogEvent(event, *item);
       
   450             m_logsMap.insert(i, item);
       
   451             m_List.append( item );
       
   452         }
       
   453     }
       
   454     
       
   455     // Check if this is the first time to receive events
       
   456     // and sort the entire list.
       
   457     if ( !m_initLogs ) {
       
   458         sort();
       
   459         oldRowCount = 0;
       
   460         m_initLogs = true;
       
   461     }
       
   462     
       
   463     q->doBeginInsertRows(QModelIndex(), oldRowCount, rowCount());
       
   464     q->doEndInsertRows();
       
   465 }
       
   466 
       
   467 /*!
       
   468  * Slot used for receiving new rows from the LogsModel.
       
   469  *
       
   470  * \param parent Optional parent index value.
       
   471  * \param first The first row item to be received from the model.
       
   472  * \param last The last row item to be received from the model.
       
   473  */
       
   474 void CntHistoryModelPrivate::logsRowsRemoved(const QModelIndex& /*parent*/, int first, int last)
       
   475 {
       
   476     Q_Q(CntHistoryModel);
       
   477     
       
   478     QList< int > indices;
       
   479     
       
   480     for ( int i = first; i <= last; ++i ) {
       
   481         HItemPointer item = m_logsMap.value( i );
       
   482         int index = m_List.indexOf( item );
       
   483         if ( index > -1 ) {
       
   484             m_logsMap.remove( i );        
       
   485             indices.append( index );
       
   486         }
       
   487     }
       
   488     
       
   489     foreach(int i, indices) {
       
   490         m_List.removeAt( i );
       
   491     }
       
   492     
       
   493     // Remove list items in batches
       
   494     if ( !indices.isEmpty() ) {
       
   495         QList< QList<int> > batches = findIndices(indices);
       
   496         foreach( QList<int> l, batches ) {
       
   497             q->doBeginRemoveRows(QModelIndex(), l.first(), l.last());
       
   498             q->doEndRemoveRows();
       
   499         }
       
   500     }
       
   501 }
       
   502 
       
   503 /*!
       
   504  * Update events from logs model. Events are
       
   505  * received as a batch
       
   506  *
       
   507  * \param first First updated model index
       
   508  * \param last Last updated model index
       
   509  */
       
   510 void CntHistoryModelPrivate::logsDataChanged(const QModelIndex& first, const QModelIndex& last)
       
   511 {
       
   512     Q_Q(CntHistoryModel);
       
   513     
       
   514     int f = first.row();
       
   515     int l = last.row();
       
   516     QList< int > indices;
       
   517     
       
   518     for ( int i = f; i < m_AbstractLogsModel->rowCount() && i <= l; ++i ) {
       
   519         
       
   520         LogsEvent* event = qVariantValue<LogsEvent*>(
       
   521                 m_AbstractLogsModel->data(m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) );
       
   522         
       
   523         // Fetch item from the mapped logs model items
       
   524         HItemPointer item = m_logsMap.value( i );
       
   525         
       
   526         // Found item in the logs map
       
   527         if ( !item.isNull() && event ) {
       
   528             int index = m_List.indexOf( item );
       
   529             readLogEvent( event, *item );
       
   530             indices.append( index );
       
   531         }
       
   532     }
       
   533     
       
   534     // Emit dataChanged signal only if there were updates
       
   535     if ( !indices.isEmpty() ) {
       
   536         QList< QList<int> > batches = findIndices( indices );
       
   537         foreach( QList<int> l, batches )
       
   538             q->doDataChanged( q->index(l.first(), 0), q->index(l.last(), 0) );
       
   539     }
       
   540 }
       
   541 
       
   542 /*
       
   543  * Clear all call logs after receiving a reset model
       
   544  * signal from logs model
       
   545  */
       
   546 void CntHistoryModelPrivate::handleLogsReset()
       
   547 {
       
   548     Q_Q(CntHistoryModel);
       
   549     
       
   550     // Remove all call logs
       
   551     QList<HItemPointer> values = m_logsMap.values();
       
   552     foreach(HItemPointer p, values) {
       
   553         m_List.removeOne( p );
       
   554     }
       
   555     
       
   556     m_logsMap.clear();
       
   557     
       
   558     //read first call events if any and start listening for more 
       
   559     for ( int i = 0; i < m_AbstractLogsModel->rowCount(); ++i ) {
       
   560         LogsEvent* event = qVariantValue<LogsEvent*>(
       
   561                 m_AbstractLogsModel->data(m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) );
       
   562         
       
   563         if ( event ) {
       
   564             HItemPointer item = HItemPointer(new HistoryItem());
       
   565             readLogEvent(event, *item);
       
   566             m_logsMap.insert(i, item);
       
   567             m_List.append( item );
       
   568         }
       
   569     }
       
   570     
       
   571     sort();
       
   572     q->doBeginInsertRows(QModelIndex(), 0, rowCount());
       
   573     q->doEndInsertRows();
       
   574 }
       
   575 
       
   576 /*!
       
   577  * Check whether an idex is out of bound of our list
       
   578  *
       
   579  * \param index Index to be validated
       
   580  */
       
   581 
       
   582 bool CntHistoryModelPrivate::validateRowIndex( const int index) const
       
   583 {
       
   584     return( index < rowCount() && index >= 0 );
       
   585 }
       
   586 
       
   587 /*!
       
   588  * Find the sequences of indices for the given indices from the private list
       
   589  * 
       
   590  * \param indices List of indices
       
   591  * \return sequences of indices
       
   592  */
       
   593 QList< QList<int> > CntHistoryModelPrivate::findIndices( const QList< int >& indices )
       
   594 {
       
   595     QList< QList<int> > sequences;
       
   596     QList<int> currSequence;
       
   597     int prevIndex = indices.at(0) - 1;
       
   598     
       
   599     foreach( int currIndex, indices )
       
   600     {
       
   601         if ( currIndex >= 0 )
       
   602         {
       
   603             if ( prevIndex+1 != currIndex && !currSequence.isEmpty() )
       
   604             {
       
   605                 sequences.append( currSequence );
       
   606                 currSequence.clear();
       
   607             } 
       
   608             currSequence.append( currIndex );
       
   609             prevIndex = currIndex;
       
   610         }
       
   611     }
       
   612     
       
   613     if ( !currSequence.isEmpty() )
       
   614     {
       
   615         // Add last sequence if such exist
       
   616         sequences.append( currSequence );
       
   617     }
       
   618     
       
   619     return sequences;
       
   620 }
       
   621 
       
   622 /*!
       
   623  * Read message event into a history item
       
   624  *
       
   625  * \param event Message event
       
   626  * \param item Conversation history item
       
   627  */
       
   628 void CntHistoryModelPrivate::readMsgEvent(MsgItem& event, HistoryItem& item)
       
   629 {
       
   630     // Msg direction
       
   631     if ( event.direction() == MsgItem::MsgDirectionIncoming ) {
       
   632         item.flags |= CntIncoming;
       
   633         // Read status
       
   634         if ( event.isAttributeSet(MsgItem::MsgAttributeUnread) )
       
   635             item.flags |= CntUnseen;
       
   636         else
       
   637             item.flags &= ~CntUnseen;
       
   638     } else if ( event.direction() == MsgItem::MsgDirectionOutgoing )
       
   639         item.flags &= ~CntIncoming;
       
   640     
       
   641     // Attachment
       
   642     if (event.isAttributeSet(MsgItem::MsgAttributeAttachment))
       
   643         item.flags |= CntAttachment;
       
   644     
       
   645     item.flags |= CntMessage;
       
   646     item.number = event.phoneNumber();
       
   647     
       
   648     if (event.body().isEmpty())
       
   649     {
       
   650         item.message = " ";
       
   651     }
       
   652     else
       
   653     {
       
   654         item.message = event.body();
       
   655     }
       
   656     
       
   657     item.timeStamp = event.timeStamp().toLocalTime();
       
   658     
       
   659     LOG_ITEM(item);
       
   660 }
       
   661 
       
   662 /*!
       
   663  * Slot to receive new messages for the first time
       
   664  * from the messages model
       
   665  *
       
   666  * \param event Message event
       
   667  * \param item Conversation history item
       
   668  */
       
   669 void CntHistoryModelPrivate::messagesReady(QList<MsgItem>& msgs)
       
   670 {
       
   671     Q_Q(CntHistoryModel);
       
   672     
       
   673     foreach( MsgItem m, msgs ) {
       
   674         // Create a new hst item
       
   675         HItemPointer item(new HistoryItem());
       
   676         
       
   677         // Parse the MsgItem and add data into hst item
       
   678         readMsgEvent( m, *item );
       
   679         
       
   680         // Map the hist item to a MsgItem in the msgModel
       
   681         m_msgMap.insert( m.id(), item );
       
   682         
       
   683         // Append the hst item to our list
       
   684         m_List.append( item );
       
   685     }
       
   686     
       
   687     sort();
       
   688     
       
   689     q->doBeginInsertRows(QModelIndex(), 0, rowCount());
       
   690     q->doEndInsertRows();
       
   691 }
       
   692 
       
   693 /*!
       
   694  * Slot to receive new messages from the messages model
       
   695  *
       
   696  * \param event Message event
       
   697  * \param item Conversation history item
       
   698  */
       
   699 void CntHistoryModelPrivate::messageAdded(MsgItem& msg)
       
   700 {
       
   701     Q_Q(CntHistoryModel);
       
   702     
       
   703     int oldRowCount = rowCount();
       
   704     
       
   705     // Create a new hst item
       
   706     HItemPointer item(new HistoryItem());
       
   707     
       
   708     // Parse the MsgItem and add data into hst item
       
   709     readMsgEvent( msg, *item );
       
   710     
       
   711     // Map the hist item to a MsgItem in the msgModel
       
   712     m_msgMap.insert( msg.id(), item );
       
   713     
       
   714     // Append the hst item to our list
       
   715     m_List.append( item );
       
   716     
       
   717     q->doBeginInsertRows( QModelIndex(), oldRowCount, rowCount() );
       
   718     q->doEndInsertRows();
       
   719 }
       
   720 
       
   721 /*!
       
   722  * Slot to update a message from the messages model
       
   723  *
       
   724  * \param event Message event
       
   725  * \param item Conversation history item
       
   726  */
       
   727 void CntHistoryModelPrivate::messageChanged(MsgItem& msg)
       
   728 {
       
   729     Q_Q(CntHistoryModel);
       
   730     
       
   731     // Fetch the hst item that maps to this MsgItem
       
   732     HItemPointer p = m_msgMap.value( msg.id() );
       
   733     
       
   734     // No item was found.
       
   735     if ( p.isNull() )
       
   736         return;
       
   737 
       
   738     // Parse the MsgItem and add data into hst item
       
   739     readMsgEvent(msg, *p);
       
   740     
       
   741     // Get the index of the the hst item in the list
       
   742     int pIndex = m_List.indexOf( p );
       
   743     
       
   744     q->doDataChanged(q->index(pIndex, 0), q->index(pIndex, 0));
       
   745 }
       
   746 
       
   747 /*!
       
   748  * Slot to delete a message from the messages model
       
   749  *
       
   750  * \param event Message event
       
   751  * \param item Conversation history item
       
   752  */
       
   753 void CntHistoryModelPrivate::messageDeleted(MsgItem& msg)
       
   754 {
       
   755     Q_Q(CntHistoryModel);
       
   756     
       
   757     // Fetch the hst item that maps to this MsgItem
       
   758     HItemPointer p = m_msgMap.value( msg.id() );
       
   759     
       
   760     // No item was found.
       
   761     if ( p.isNull() )
       
   762         return;
       
   763     
       
   764     // Remove the item in stored containers
       
   765     m_msgMap.remove( msg.id() );
       
   766     int index = m_List.indexOf( p );
       
   767     if ( index > -1 ) {
       
   768         m_List.removeAt( index );
       
   769         q->doBeginRemoveRows(QModelIndex(), index, index);
       
   770         q->doEndRemoveRows();
       
   771     }
       
   772 }