messagingapp/msgui/appengine/src/conversationssummarymodel.cpp
changeset 23 238255e8b033
child 25 84d9eb65b26f
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     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 "conversationssummarymodel.h"
       
    19 #include "conversationsenginedefines.h"
       
    20 #include "conversationsengineutility.h"
       
    21 #include "s60qconversions.h"
       
    22 #include "convergedmessage.h"
       
    23 
       
    24 #include <ccsclientconversation.h>
       
    25 #include <ccsconversationentry.h>
       
    26 
       
    27 //---------------------------------------------------------------
       
    28 // ConversationsSummaryModel::ConversationsSummaryModel
       
    29 // @see header
       
    30 //---------------------------------------------------------------
       
    31 ConversationsSummaryModel::ConversationsSummaryModel(QObject* parent)
       
    32 :QStandardItemModel(parent)
       
    33     {
       
    34     }
       
    35 
       
    36 //---------------------------------------------------------------
       
    37 // ConversationsSummaryModel::~ConversationsSummaryModel
       
    38 // @see header
       
    39 //---------------------------------------------------------------
       
    40 ConversationsSummaryModel::~ConversationsSummaryModel()
       
    41 {
       
    42     
       
    43 }
       
    44 
       
    45 //---------------------------------------------------------------
       
    46 // ConversationsSummaryModel::data
       
    47 // @see header
       
    48 //---------------------------------------------------------------
       
    49 QVariant ConversationsSummaryModel::data(const QModelIndex & index , 
       
    50     int role ) const
       
    51 {
       
    52     QVariant value;
       
    53     QStandardItem* item = itemFromIndex(index);
       
    54     switch(role)
       
    55     {
       
    56         case ConversationId:
       
    57         {
       
    58             value = item->data(ConversationId);
       
    59             break;
       
    60         }
       
    61         case UnReadStatus:
       
    62         {
       
    63             value = item->data(UnReadStatus);
       
    64             break;
       
    65         }
       
    66         case ContactId:
       
    67         {
       
    68             value = item->data(ContactId);
       
    69             break;
       
    70         }
       
    71         case ConvergedMsgId:
       
    72         {
       
    73             value = item->data(ConvergedMsgId);
       
    74             break;
       
    75         }
       
    76         case UnreadCount:
       
    77         {
       
    78             value = item->data(UnreadCount);
       
    79             break;
       
    80         }
       
    81         case TimeStamp:
       
    82         {
       
    83             value = item->data(TimeStamp);   
       
    84             break;
       
    85             
       
    86         }
       
    87         case MessageProperty:
       
    88         {
       
    89             value = item->data(MessageProperty);
       
    90             break;
       
    91         }
       
    92         case MessageType:
       
    93         {
       
    94             value = item->data(MessageType);                    
       
    95             break;
       
    96         }
       
    97         case Subject:
       
    98         {
       
    99             
       
   100             value = item->data(Subject);
       
   101             break;
       
   102         }
       
   103         case BodyText:
       
   104         {
       
   105             value = item->data(BodyText);
       
   106             break;
       
   107         }
       
   108         case ConversationAddress:
       
   109         {
       
   110             value = item->data(ConversationAddress);
       
   111             break;
       
   112         }
       
   113         case FirstName:
       
   114         {
       
   115             value = item->data(FirstName);
       
   116             break;
       
   117         }
       
   118         case LastName:
       
   119         {
       
   120             value = item->data(LastName);
       
   121             break;
       
   122         }
       
   123         case NickName:
       
   124         {
       
   125             value = item->data(NickName);
       
   126             break;
       
   127         }
       
   128         case Avatar:
       
   129         {
       
   130             value = item->data(Avatar);
       
   131             break;
       
   132         }
       
   133         default:
       
   134         {
       
   135             //No matching role found, set invalid variant.
       
   136             value = QVariant();
       
   137             break;
       
   138         }
       
   139     }
       
   140     return value;
       
   141 }
       
   142 
       
   143 
       
   144 //---------------------------------------------------------------
       
   145 // ConversationsSummaryModel::addRow
       
   146 // @see header
       
   147 //---------------------------------------------------------------
       
   148 void ConversationsSummaryModel::addRow(
       
   149         const CCsClientConversation& conversation)
       
   150     {  
       
   151     int convId = conversation.GetConversationEntryId();
       
   152     
       
   153     //match convId in model, if found update else add item
       
   154     QModelIndexList indexList = this->match(index(0, 0), 
       
   155             ConversationId, 
       
   156             convId, 1, Qt::MatchExactly);
       
   157     
       
   158     // if not found, add new item
       
   159     if ( indexList.count() == 0 )
       
   160     {    
       
   161         QStandardItem* item = new QStandardItem();         
       
   162         populateItem(*item,conversation);
       
   163         appendRow(item);
       
   164     }
       
   165     else        
       
   166     {
       
   167         // Update an existing item
       
   168         QModelIndex index = indexList[0];
       
   169         QStandardItem* item = this->item(index.row(), 0);
       
   170         populateItem(*item,conversation);
       
   171     }        
       
   172     }
       
   173 
       
   174 //---------------------------------------------------------------
       
   175 // ConversationsSummaryModel::deleteRow
       
   176 // @see header
       
   177 //---------------------------------------------------------------
       
   178 void ConversationsSummaryModel::deleteRow(qint64 convId)
       
   179     {
       
   180     //match, if found remove item
       
   181     QModelIndexList indexList = this->match(index(0, 0), 
       
   182             ConversationId, 
       
   183             convId, 1, Qt::MatchExactly);
       
   184 
       
   185     if( indexList.count() == 1 )
       
   186         {
       
   187         QModelIndex index = indexList[0];
       
   188         this->removeRow(index.row());
       
   189         }        
       
   190     }
       
   191 
       
   192 //---------------------------------------------------------------
       
   193 // ConversationsSummaryModel::populateItem
       
   194 // @see header
       
   195 //---------------------------------------------------------------
       
   196 void ConversationsSummaryModel::populateItem(QStandardItem& item, 
       
   197         const CCsClientConversation& conversation)
       
   198     {
       
   199     //get entry
       
   200     CCsConversationEntry* conEntry = conversation.GetConversationEntry();   
       
   201     
       
   202     // id 
       
   203     item.setData(conversation.GetConversationEntryId(), ConversationId);
       
   204 
       
   205     // message details
       
   206     item.setData(conEntry->EntryId(), ConvergedMsgId);
       
   207     item.setData(ConversationsEngineUtility::messageType(conEntry->GetType()), MessageType);
       
   208     
       
   209     // description
       
   210     HBufC* body = conEntry->Description();
       
   211     if( body && body->Length())
       
   212         {     
       
   213         QString bodytext(S60QConversions::s60DescToQString(*body));
       
   214         item.setData(bodytext, BodyText); 
       
   215         item.setData(bodytext, Subject );
       
   216         }
       
   217 
       
   218     // time stamp
       
   219     TTime unixEpoch(KUnixEpoch);
       
   220     TTimeIntervalSeconds seconds;
       
   221     TTime timeStamp(conEntry->TimeStamp() );
       
   222     timeStamp.SecondsFrom(unixEpoch, seconds);       
       
   223     item.setData(seconds.Int(), TimeStamp); 
       
   224 
       
   225     // unread count 
       
   226     item.setData(conversation.GetUnreadMessageCount(), UnreadCount);
       
   227 
       
   228     // contact details
       
   229     HBufC* firstname = conversation.GetFirstName(); 
       
   230     QString displayName("");
       
   231     //first name
       
   232     if(firstname && firstname->Length())
       
   233         {
       
   234     displayName = S60QConversions::s60DescToQString(*firstname);
       
   235     item.setData(displayName,FirstName); 
       
   236         }
       
   237     //last name
       
   238     HBufC* lastname = conversation.GetLastName();
       
   239     if( lastname &&  lastname->Length())
       
   240         {          
       
   241     item.setData(S60QConversions::s60DescToQString(*lastname),LastName);
       
   242         }
       
   243     //nick name
       
   244     HBufC* nickname = conversation.GetNickName();
       
   245     if (nickname && nickname->Length())
       
   246     {
       
   247         item.setData(S60QConversions::s60DescToQString(*nickname), NickName);
       
   248     }
       
   249 
       
   250     // contact number
       
   251     HBufC* contactno = conEntry->Contact();
       
   252     QString contactNumber("");
       
   253     if ( contactno && contactno->Length() )
       
   254         {
       
   255         contactNumber = S60QConversions::s60DescToQString(*contactno);                  
       
   256         }        
       
   257     item.setData(contactNumber, ConversationAddress);
       
   258 
       
   259     //set the contactId
       
   260     int contactId = conversation.GetContactId();
       
   261     item.setData(contactId, ContactId);
       
   262     
       
   263     // unread status        
       
   264     item.setData(conEntry->IsAttributeSet(ECsAttributeUnread),UnReadStatus);      
       
   265     }
       
   266 
       
   267 // EOF