messagingapp/msgui/appengine/src/conversationssummarymodel.cpp
branchRCL_3
changeset 26 ebe688cedc25
equal deleted inserted replaced
25:fa1df4b99609 26:ebe688cedc25
       
     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 <xqconversions.h>
       
    22 #include "convergedmessage.h"
       
    23 #include "unidatamodelloader.h"
       
    24 #include "unidatamodelplugininterface.h"
       
    25 #include "ringbc.h"
       
    26 #include "msgcontacthandler.h"
       
    27 #include "debugtraces.h"
       
    28 
       
    29 #include <ccsclientconversation.h>
       
    30 #include <ccsconversationentry.h>
       
    31 #include <QFile>
       
    32 #include <QFileInfo>
       
    33 
       
    34 //---------------------------------------------------------------
       
    35 // ConversationsSummaryModel::ConversationsSummaryModel
       
    36 // @see header
       
    37 //---------------------------------------------------------------
       
    38 ConversationsSummaryModel::ConversationsSummaryModel(QObject* parent)
       
    39 :QStandardItemModel(parent)
       
    40     {
       
    41     QStandardItemModel::setSortRole(TimeStamp);
       
    42     QStandardItemModel::sort(0, Qt::DescendingOrder);
       
    43     }
       
    44 
       
    45 //---------------------------------------------------------------
       
    46 // ConversationsSummaryModel::~ConversationsSummaryModel
       
    47 // @see header
       
    48 //---------------------------------------------------------------
       
    49 ConversationsSummaryModel::~ConversationsSummaryModel()
       
    50 {
       
    51     
       
    52 }
       
    53 
       
    54 //---------------------------------------------------------------
       
    55 // ConversationsSummaryModel::data
       
    56 // @see header
       
    57 //---------------------------------------------------------------
       
    58 QVariant ConversationsSummaryModel::data(const QModelIndex & index , 
       
    59     int role ) const
       
    60 {
       
    61     QVariant value;
       
    62     QStandardItem* item = itemFromIndex(index);
       
    63     switch(role)
       
    64     {
       
    65         case ConversationId:
       
    66         {
       
    67             value = item->data(ConversationId);
       
    68             break;
       
    69         }
       
    70         case UnReadStatus:
       
    71         {
       
    72             value = item->data(UnReadStatus);
       
    73             break;
       
    74         }
       
    75         case ContactId:
       
    76         {
       
    77             value = item->data(ContactId);
       
    78             break;
       
    79         }
       
    80         case ConvergedMsgId:
       
    81         {
       
    82             value = item->data(ConvergedMsgId);
       
    83             break;
       
    84         }
       
    85         case UnreadCount:
       
    86         {
       
    87             value = item->data(UnreadCount);
       
    88             break;
       
    89         }
       
    90         case TimeStamp:
       
    91         {
       
    92             value = item->data(TimeStamp);
       
    93             break;
       
    94         }
       
    95         case SendingState:
       
    96         {
       
    97             value = item->data(SendingState);
       
    98             break;
       
    99         }
       
   100         case MessageProperty:
       
   101         {
       
   102             value = item->data(MessageProperty);
       
   103             break;
       
   104         }
       
   105         case MessageType:
       
   106         {
       
   107             value = item->data(MessageType);
       
   108             break;
       
   109         }
       
   110         case MessageSubType:
       
   111         {
       
   112             value = item->data(MessageSubType);
       
   113             break;
       
   114         }
       
   115         case Subject:
       
   116         {
       
   117             value = item->data(Subject);
       
   118             break;
       
   119         }
       
   120         case BodyText:
       
   121         {
       
   122             value = item->data(BodyText);
       
   123             break;
       
   124         }
       
   125         case ConversationAddress:
       
   126         {
       
   127             value = item->data(ConversationAddress);
       
   128             break;
       
   129         }
       
   130         case DisplayName:
       
   131         {
       
   132             value = item->data(DisplayName);
       
   133             break;
       
   134         }
       
   135         case Avatar:
       
   136         {
       
   137             value = item->data(Avatar);
       
   138             break;
       
   139         }
       
   140         case Direction:
       
   141         {
       
   142             value = item->data(Direction);
       
   143             break;
       
   144         }
       
   145         default:
       
   146         {
       
   147             //No matching role found, set invalid variant.
       
   148             value = QVariant();
       
   149             break;
       
   150         }
       
   151     }
       
   152     return value;
       
   153 }
       
   154 
       
   155 
       
   156 //---------------------------------------------------------------
       
   157 // ConversationsSummaryModel::addRow
       
   158 // @see header
       
   159 //---------------------------------------------------------------
       
   160 void ConversationsSummaryModel::addRow(
       
   161         const CCsClientConversation& conversation, bool caching)
       
   162     {  
       
   163     int convId = conversation.GetConversationEntryId();
       
   164     
       
   165     //match convId in model, if found update else add item
       
   166     QModelIndexList indexList = this->match(index(0, 0), 
       
   167             ConversationId, 
       
   168             convId, 1, Qt::MatchExactly);
       
   169     
       
   170     // if not found, add new item
       
   171     if ( indexList.count() == 0 )
       
   172     {    
       
   173         QStandardItem* item = new QStandardItem();         
       
   174         populateItem(*item,conversation);
       
   175         appendRow(item);
       
   176     }
       
   177     else        
       
   178     {
       
   179         // Update an existing item
       
   180         QModelIndex index = indexList[0];
       
   181         QStandardItem* item = this->item(index.row(), 0);
       
   182         populateItem(*item,conversation);
       
   183     }  
       
   184     // no need to sort if it is initial caching, as sorting is already done
       
   185     if (!caching)
       
   186         {
       
   187         QStandardItemModel::sort(0, Qt::DescendingOrder);
       
   188         }
       
   189     }
       
   190 
       
   191 //---------------------------------------------------------------
       
   192 // ConversationsSummaryModel::deleteRow
       
   193 // @see header
       
   194 //---------------------------------------------------------------
       
   195 void ConversationsSummaryModel::deleteRow(qint64 convId)
       
   196     {
       
   197     //match, if found remove item
       
   198     QModelIndexList indexList = this->match(index(0, 0), 
       
   199             ConversationId, 
       
   200             convId, 1, Qt::MatchExactly);
       
   201 
       
   202     if( indexList.count() == 1 )
       
   203         {
       
   204         QModelIndex index = indexList[0];
       
   205         this->removeRow(index.row());
       
   206         }        
       
   207     }
       
   208 
       
   209 //---------------------------------------------------------------
       
   210 // ConversationsSummaryModel::populateItem
       
   211 // @see header
       
   212 //---------------------------------------------------------------
       
   213 void ConversationsSummaryModel::populateItem(QStandardItem& item, 
       
   214         const CCsClientConversation& conversation)
       
   215     {
       
   216     QCRITICAL_WRITE("ConversationsSummaryModel::populateItem start.");
       
   217             
       
   218     //get entry
       
   219     CCsConversationEntry* conEntry = conversation.GetConversationEntry(); 
       
   220     //error scenario
       
   221     if(conEntry == NULL)
       
   222         return;
       
   223     
       
   224     // message details
       
   225     // conversation-id 
       
   226     item.setData(conversation.GetConversationEntryId(), ConversationId);
       
   227     // message-id
       
   228     item.setData(conEntry->EntryId(), ConvergedMsgId);	
       
   229     // send status
       
   230     item.setData(conEntry->GetSendState(),SendingState);
       
   231     // direction
       
   232     item.setData(conEntry->ConversationDir(), Direction);
       
   233     //msg-type
       
   234     int msgType = ConversationsEngineUtility::messageType(conEntry->GetType());
       
   235     item.setData(msgType, MessageType);
       
   236     //message sub-type   
       
   237     item.setData(ConversationsEngineUtility::messageSubType(conEntry->GetType()), MessageSubType);
       
   238 
       
   239     //handle BT messages, needs to be revisited again, once vcal/vcard over BT issue is fixed
       
   240     if(ConvergedMessage::BT == msgType)
       
   241     {
       
   242         handleBlueToothMessages(item, *conEntry);
       
   243     }
       
   244     else if(msgType == ConvergedMessage::BioMsg)
       
   245     {
       
   246         handleBioMessages(item, *conEntry);
       
   247     }
       
   248     else
       
   249     {       
       
   250         // description
       
   251         HBufC* body = conEntry->Description();
       
   252         if( body && body->Length())
       
   253         {     
       
   254             QString bodytext(XQConversions::s60DescToQString(*body));
       
   255             item.setData(bodytext, BodyText); 
       
   256             item.setData(bodytext, Subject );
       
   257         }
       
   258     }
       
   259 
       
   260     
       
   261     // time stamp
       
   262     TTime unixEpoch(KUnixEpoch);
       
   263     TTimeIntervalSeconds seconds;
       
   264     TTime timeStamp(conEntry->TimeStamp() );
       
   265     timeStamp.SecondsFrom(unixEpoch, seconds);       
       
   266     item.setData(seconds.Int(), TimeStamp); 
       
   267 
       
   268     // unread count 
       
   269     item.setData(conversation.GetUnreadMessageCount(), UnreadCount);
       
   270 
       
   271     // contact details
       
   272     HBufC* disName = conversation.GetDisplayName(); 
       
   273     QString displayName("");
       
   274     //display name
       
   275     if(disName && disName->Length())
       
   276         {
       
   277         displayName = XQConversions::s60DescToQString(*disName);
       
   278         item.setData(displayName,DisplayName); 
       
   279         }
       
   280     
       
   281     // contact number
       
   282     HBufC* contactno = conEntry->Contact();
       
   283     QString contactNumber("");
       
   284     if ( contactno && contactno->Length() )
       
   285         {
       
   286         contactNumber = XQConversions::s60DescToQString(*contactno);                  
       
   287         }        
       
   288     item.setData(contactNumber, ConversationAddress);
       
   289 
       
   290     //set the contactId
       
   291     int contactId = conversation.GetContactId();
       
   292     item.setData(contactId, ContactId);
       
   293     
       
   294     // unread status        
       
   295     item.setData(conEntry->IsAttributeSet(ECsAttributeUnread),UnReadStatus); 
       
   296     
       
   297     QCRITICAL_WRITE("ConversationsSummaryModel::populateItem start.");
       
   298     }
       
   299 
       
   300 
       
   301 //---------------------------------------------------------------
       
   302 // ConversationsModel::handleBlueToothMessages
       
   303 // @see header
       
   304 //---------------------------------------------------------------
       
   305 void ConversationsSummaryModel::handleBlueToothMessages(QStandardItem& item,
       
   306     const CCsConversationEntry& entry)
       
   307 {
       
   308     int msgSubType = ConversationsEngineUtility::messageSubType(entry.GetType());
       
   309     if (msgSubType == ConvergedMessage::VCard) 
       
   310     {
       
   311         UniDataModelLoader* pluginLoader = new UniDataModelLoader;
       
   312         UniDataModelPluginInterface* bioMsgPlugin = pluginLoader->getDataModelPlugin(
       
   313             ConvergedMessage::BioMsg);
       
   314         bioMsgPlugin->setMessageId(entry.EntryId());
       
   315 
       
   316         if (bioMsgPlugin->attachmentCount() > 0) 
       
   317         	{
       
   318             UniMessageInfoList attList = bioMsgPlugin->attachmentList();
       
   319             QString attachmentPath = attList[0]->path();
       
   320 
       
   321             //get display-name and set as bodytext
       
   322             QString displayName = MsgContactHandler::getVCardDisplayName(attachmentPath);
       
   323             item.setData(displayName, BodyText);
       
   324 		
       
   325 						// clear attachement list : its allocated at data model
       
   326             while (!attList.isEmpty()) 
       
   327 						{
       
   328                 delete attList.takeFirst();
       
   329             }
       
   330 
       
   331         }
       
   332         delete pluginLoader;
       
   333     }
       
   334 
       
   335     else 
       
   336     {
       
   337         QString description = XQConversions::s60DescToQString(*(entry.Description()));
       
   338 
       
   339         if (msgSubType == ConvergedMessage::VCal) // "vCalendar"
       
   340         {
       
   341             //message sub-type
       
   342             item.setData(ConvergedMessage::VCal, MessageSubType);
       
   343         }
       
   344         else 
       
   345 				{
       
   346             //message sub-type
       
   347             item.setData(ConvergedMessage::None, MessageSubType);
       
   348         }
       
   349         //for BT messages we show filenames for all other (except vcard) messages
       
   350         //get filename and set as body
       
   351         QFileInfo fileinfo(description);
       
   352         QString filename = fileinfo.fileName();
       
   353         item.setData(filename, BodyText);
       
   354     }
       
   355 }
       
   356 
       
   357 //---------------------------------------------------------------
       
   358 // ConversationsSummaryModel::handleBioMessages
       
   359 // @see header
       
   360 //---------------------------------------------------------------
       
   361 void ConversationsSummaryModel::handleBioMessages(QStandardItem& item, const CCsConversationEntry& entry)
       
   362 {
       
   363     UniDataModelLoader* pluginLoader = new UniDataModelLoader;
       
   364     UniDataModelPluginInterface* bioMsgPlugin = pluginLoader->getDataModelPlugin(ConvergedMessage::BioMsg);
       
   365     bioMsgPlugin->setMessageId(entry.EntryId());
       
   366     
       
   367     int msgSubType = ConversationsEngineUtility::messageSubType(entry.GetType());
       
   368     if (ConvergedMessage::VCard == msgSubType) {
       
   369         if (bioMsgPlugin->attachmentCount() > 0) {
       
   370             UniMessageInfoList attList = bioMsgPlugin->attachmentList();
       
   371             QString attachmentPath = attList[0]->path();
       
   372 
       
   373             //get display-name and set as bodytext
       
   374             QString displayName = MsgContactHandler::getVCardDisplayName(
       
   375                     attachmentPath);
       
   376             item.setData(displayName, BodyText);
       
   377 
       
   378             // clear attachement list : its allocated at data model
       
   379             while (!attList.isEmpty()) {
       
   380                 delete attList.takeFirst();
       
   381             }
       
   382         }
       
   383     }
       
   384     else if (ConvergedMessage::VCal == msgSubType) {
       
   385         //not supported
       
   386     }
       
   387     else if (ConvergedMessage::RingingTone == msgSubType) {
       
   388         if (bioMsgPlugin->attachmentCount() > 0) {
       
   389             UniMessageInfoList attList = bioMsgPlugin->attachmentList();
       
   390             QString attachmentPath = attList[0]->path();
       
   391             	
       
   392             //get tone title, and set as bodytext
       
   393             RingBc ringBc;
       
   394             item.setData(ringBc.toneTitle(attachmentPath), BodyText);
       
   395             while (!attList.isEmpty()) {
       
   396                 delete attList.takeFirst();
       
   397             }
       
   398         }
       
   399     }
       
   400 
       
   401    else if(ConvergedMessage::NokiaService == msgSubType){
       
   402         // This is a bio message so lets parse it and see if it has a associated attachment ..        
       
   403         if (bioMsgPlugin->attachmentCount() > 0) {
       
   404 				// TODO : need to confirm if we need to read from attachment
       
   405         }
       
   406         else {// description
       
   407             HBufC* description = entry.Description();
       
   408             QString subject("");
       
   409             if (description && description->Length()) {
       
   410                 subject = (XQConversions::s60DescToQString(*description));
       
   411                 item.setData(subject, BodyText);
       
   412             }
       
   413 
       
   414         }
       
   415 
       
   416     }
       
   417 
       
   418    else {
       
   419         // description
       
   420         HBufC* description = entry.Description();
       
   421         QString subject("");
       
   422         if (description && description->Length()) {
       
   423             subject = (XQConversions::s60DescToQString(*description));
       
   424             item.setData(subject, BodyText);
       
   425         }
       
   426     }
       
   427     
       
   428     delete pluginLoader;
       
   429 }
       
   430 // EOF