messagingapp/msgui/appengine/src/conversationsmodel.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 "conversationsmodel.h"
       
    19 #include "conversationsenginedefines.h"
       
    20 #include "conversationmsgstorehandler.h"
       
    21 #include "convergedmessage.h"
       
    22 #include "s60qconversions.h"
       
    23 #include "conversationsengineutility.h"
       
    24 #include "unidatamodelloader.h"
       
    25 #include "unidatamodelplugininterface.h"
       
    26 #include <ccsconversationentry.h>
       
    27 #include <QFile>
       
    28 
       
    29 // LOCAL CONSTANTS
       
    30 const QString MMSStr("mms");
       
    31 
       
    32 //---------------------------------------------------------------
       
    33 // ConversationsModel::ConversationsModel
       
    34 // Constructor
       
    35 //---------------------------------------------------------------
       
    36 ConversationsModel::ConversationsModel(
       
    37     ConversationMsgStoreHandler* msgStoreHandler,
       
    38     QObject* parent): QStandardItemModel(parent),
       
    39     mMsgStoreHandler(msgStoreHandler)
       
    40             {
       
    41             iDataModelPluginLoader = new UniDataModelLoader;
       
    42             iDataModelPluginLoader->loadPlugins();
       
    43             iMmsDataPlugin = iDataModelPluginLoader->getDataModelPlugin(MMSStr);
       
    44             }
       
    45 
       
    46 
       
    47 //---------------------------------------------------------------
       
    48 // ConversationsModel::~ConversationsModel
       
    49 // Destructor
       
    50 //---------------------------------------------------------------
       
    51 ConversationsModel::~ConversationsModel()
       
    52     {
       
    53     if(iDataModelPluginLoader)
       
    54     {
       
    55         delete iDataModelPluginLoader;
       
    56         iDataModelPluginLoader = NULL;
       
    57     }
       
    58     }
       
    59 
       
    60 //---------------------------------------------------------------
       
    61 // ConversationsModel::data
       
    62 // @see header
       
    63 //---------------------------------------------------------------
       
    64 QVariant ConversationsModel::data(const QModelIndex & index , int role ) const
       
    65 {
       
    66     QVariant value;
       
    67     QStandardItem* item = itemFromIndex(index);
       
    68     switch(role)
       
    69     {
       
    70         case ConversationId:
       
    71         {
       
    72             value = item->data(ConversationId);
       
    73             break;
       
    74         }
       
    75         case UnReadStatus:
       
    76         {
       
    77             value = item->data(UnReadStatus);
       
    78             break;
       
    79         }
       
    80         case ContactId:
       
    81         {
       
    82             value = item->data(ContactId);
       
    83             break;
       
    84         }
       
    85         case TimeStamp:
       
    86         {
       
    87             value = item->data(TimeStamp);
       
    88             break;
       
    89         }
       
    90         case ConvergedMsgId:
       
    91         {
       
    92             value = item->data(ConvergedMsgId);
       
    93             break;
       
    94         }
       
    95         case MessageProperty:
       
    96         {
       
    97             value = item->data(MessageProperty);
       
    98             break;
       
    99         }
       
   100         case MessageType:
       
   101         {
       
   102             value = item->data(MessageType);
       
   103             break;
       
   104         }
       
   105         case Subject:
       
   106         {
       
   107             value = item->data(Subject);
       
   108             break;
       
   109         }
       
   110         case BodyText:
       
   111         {
       
   112             value = item->data(BodyText);
       
   113             break;
       
   114         }
       
   115         case ConversationAddress:
       
   116         {
       
   117             value = item->data(ConversationAddress);
       
   118             break;
       
   119         }
       
   120         case Direction:
       
   121         {
       
   122             value = item->data(Direction);
       
   123             break;
       
   124         }
       
   125         case SendingState:
       
   126         {
       
   127             value = item->data(SendingState);
       
   128             break;
       
   129         }
       
   130         case MessagePriority:
       
   131         {
       
   132             value = item->data(MessagePriority);
       
   133             break;
       
   134         }
       
   135         case Attachments:
       
   136         {
       
   137             value = item->data(Attachments);
       
   138             break;
       
   139         }
       
   140         case MessageLocation:
       
   141         {
       
   142             value = item->data(MessageLocation);
       
   143             break;
       
   144         }
       
   145         case MessageStore:
       
   146         {
       
   147             value = item->data(MessageStore);
       
   148             break;
       
   149         }
       
   150         case ConversationAlias:
       
   151         {
       
   152             value = item->data(ConversationAlias);
       
   153             break;
       
   154         }
       
   155         case UnreadCount:
       
   156         {
       
   157 
       
   158             value = item->data(UnreadCount);
       
   159             break;
       
   160         }
       
   161         case FirstName: // Fall through start
       
   162             value = item->data(FirstName);
       
   163             break;
       
   164         case LastName:
       
   165             value = item->data(LastName);
       
   166             break;
       
   167         case NickName:
       
   168             value = item->data(NickName);
       
   169             break;
       
   170         case Avatar:    // Fall througn end
       
   171             value = item->data(Avatar);
       
   172             break;
       
   173         default:
       
   174         {
       
   175                 //No matching role found, set invalid variant
       
   176                 value = QVariant();
       
   177                 break;
       
   178         }
       
   179       }
       
   180         return value;
       
   181 }
       
   182 
       
   183 //---------------------------------------------------------------
       
   184 // ConversationsModel::addRow
       
   185 // @see header
       
   186 //---------------------------------------------------------------
       
   187 void ConversationsModel::addRow(
       
   188     const CCsConversationEntry& entry,
       
   189     bool dynamicUpdate)
       
   190 {
       
   191     int msgId = entry.EntryId();	
       
   192     //match, if found update else add item
       
   193     QModelIndexList indexList = this->match(index(0, 0), 
       
   194         ConvergedMsgId, 
       
   195         msgId, -1, Qt::MatchExactly);
       
   196 
       
   197     // if not found, add new item
       
   198     if ( indexList.count() == 0 )
       
   199     {    
       
   200         QStandardItem* item = new QStandardItem();
       
   201         populateItem(*item,entry);
       
   202         if(!dynamicUpdate)
       
   203         {
       
   204             insertRow(0,item);    
       
   205         }
       
   206         else
       
   207         {    
       
   208             int i;
       
   209             for( i= rowCount()-1; i >=0  ; --i)
       
   210             {
       
   211                 QStandardItem* modelItem = this->item(i,0);
       
   212                 if( modelItem->data(ConvergedMsgId).toInt() < 
       
   213                         item->data(ConvergedMsgId).toInt())
       
   214                 {
       
   215                     if( i == rowCount()-1)
       
   216                     {
       
   217                         appendRow(item);
       
   218                     }
       
   219                     else
       
   220                     {    
       
   221                         insertRow(i+1,item);
       
   222                     }
       
   223                     return;
       
   224                 }
       
   225             }
       
   226             if( i == 0 )
       
   227             {
       
   228                 insertRow(0,item);
       
   229             }
       
   230         }
       
   231     }
       
   232     else
       
   233     {
       
   234         // Update an existing item
       
   235         QModelIndex index = indexList[0];
       
   236         QStandardItem* item = this->item(index.row(), 0);
       
   237         populateItem(*item,entry);
       
   238         }
       
   239 }
       
   240 
       
   241 //---------------------------------------------------------------
       
   242 // ConversationsModel::deleteRow
       
   243 // @see header
       
   244 //---------------------------------------------------------------
       
   245 void ConversationsModel::deleteRow(int msgId)
       
   246     {
       
   247     //match, if found remove item
       
   248     QModelIndexList indexList = this->match(index(0, 0), 
       
   249             ConvergedMsgId, 
       
   250             msgId, 1, Qt::MatchExactly);
       
   251 
       
   252     if( indexList.count() == 1 )
       
   253         {
       
   254         QModelIndex index = indexList[0];
       
   255         this->removeRow(index.row());
       
   256         }
       
   257     }
       
   258 
       
   259 
       
   260 //---------------------------------------------------------------
       
   261 // ConversationsModel::populateItem
       
   262 // @see header
       
   263 //---------------------------------------------------------------
       
   264 void ConversationsModel::populateItem(QStandardItem& item, 
       
   265         const CCsConversationEntry& entry)
       
   266     {
       
   267     int msgId = entry.EntryId();
       
   268     // id
       
   269     item.setData(msgId, ConvergedMsgId);
       
   270 
       
   271     // description
       
   272     HBufC* description = entry.Description();
       
   273     QString subject("");
       
   274     if( description && description->Length())
       
   275         {
       
   276         subject = (S60QConversions::s60DescToQString(*description));
       
   277         item.setData(subject, Subject); 
       
   278         }
       
   279 
       
   280     // time stamp 
       
   281     TTime unixEpoch(KUnixEpoch);
       
   282     TTimeIntervalSeconds seconds;
       
   283     TTime timeStamp(entry.TimeStamp() );
       
   284     timeStamp.SecondsFrom(unixEpoch, seconds);
       
   285     item.setData(seconds.Int(), TimeStamp);
       
   286 
       
   287     //contact details
       
   288     HBufC* contact = entry.Contact();
       
   289     if( contact && contact->Length())
       
   290         {
       
   291         item.setData(S60QConversions::s60DescToQString(*contact), ConversationAddress);
       
   292         }
       
   293 
       
   294     // message type.
       
   295     item.setData(ConversationsEngineUtility::messageType(entry.GetType()), MessageType);
       
   296     // unread status
       
   297     if(entry.IsAttributeSet(ECsAttributeUnread))
       
   298     {    
       
   299     item.setData(true,UnReadStatus);
       
   300     }
       
   301     else
       
   302     {
       
   303     item.setData(false,UnReadStatus);
       
   304     }
       
   305     
       
   306     //sending state
       
   307     item.setData(entry.GetSendState(), SendingState);
       
   308     // direction
       
   309     item.setData(entry.ConversationDir(), Direction);
       
   310 
       
   311     //location
       
   312     if(entry.ConversationDir() == ECsDirectionIncoming)
       
   313     {
       
   314         item.setData(ConvergedMessage::Inbox, MessageLocation);
       
   315     }
       
   316     else if(entry.IsAttributeSet(ECsAttributeDraft))
       
   317     {
       
   318         item.setData(ConvergedMessage::Draft, MessageLocation);
       
   319     }
       
   320     else if(entry.IsAttributeSet(ECsAttributeSent))
       
   321     {
       
   322         item.setData(ConvergedMessage::Sent, MessageLocation);
       
   323     }
       
   324     else
       
   325     {
       
   326         item.setData(ConvergedMessage::Outbox, MessageLocation);
       
   327     }
       
   328 
       
   329     //message specific handling
       
   330     TCsType msgType = entry.GetType();
       
   331     if(msgType == ECsMMS)
       
   332     {
       
   333         iMmsDataPlugin->setMessageId(entry.EntryId());
       
   334 
       
   335         if( iMmsDataPlugin->attachmentCount() > 0 )
       
   336         {
       
   337         item.setData(ConvergedMessage::Attachment,MessageProperty);
       
   338         }
       
   339 
       
   340         int slideCount = iMmsDataPlugin->slideCount();
       
   341         bool isBodyTextSet = false;
       
   342         QString textContent;
       
   343         QStringList attachmentPaths;
       
   344 
       
   345         for( int i=0; i < slideCount; ++i )
       
   346         {
       
   347             UniMessageInfoList objectList = iMmsDataPlugin->slideContent(i);
       
   348             for( int index=0; index < objectList.count() ; ++index )
       
   349             {
       
   350                 attachmentPaths.append(objectList[index]->path());
       
   351                 if( !isBodyTextSet && objectList[index]->mimetype().contains("text") )
       
   352                 {
       
   353                     QFile file(objectList[index]->path());
       
   354                     file.open(QIODevice::ReadOnly);
       
   355                     textContent = file.readAll();
       
   356                     item.setData(textContent, BodyText);
       
   357                     isBodyTextSet = true;
       
   358                     file.close();
       
   359                 }
       
   360             }
       
   361             foreach(UniMessageInfo* slide,objectList)
       
   362                 {
       
   363                 delete slide;
       
   364                 }
       
   365         }
       
   366         //populate item  with the attachment list
       
   367         item.setData(attachmentPaths.join("|"),Attachments);
       
   368         if(entry.IsAttributeSet(ECsAttributeHighPriority))
       
   369         {
       
   370             item.setData(ConvergedMessage::High,MessagePriority);
       
   371         }
       
   372         else if(entry.IsAttributeSet(ECsAttributeLowPriority))
       
   373         {
       
   374             item.setData(ConvergedMessage::Low,MessagePriority);
       
   375         }
       
   376     }
       
   377     else
       
   378     {
       
   379         // sms bodytext
       
   380         item.setData(subject, BodyText);
       
   381     }
       
   382 
       
   383     //presence
       
   384     //TODO
       
   385     }
       
   386 
       
   387 //EOF