messagingapp/msgui/appengine/src/draftsmodel.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: model for drafts view.
       
    15  *
       
    16  */
       
    17 
       
    18 #include "draftsmodel.h"
       
    19 #include "conversationsenginedefines.h"
       
    20 #include "conversationsengineutility.h"
       
    21 #include "s60qconversions.h"
       
    22 #include "conversationsengineutility.h"
       
    23 
       
    24 #include <ccsclientconversation.h>
       
    25 #include <ccsconversationentry.h>
       
    26 #include <msvstd.h>
       
    27 #include <QDateTime>
       
    28 #include <hbicon.h>
       
    29 
       
    30 //CONSTANTS
       
    31 const QString DATE_FORMAT("dd/MM"); //Date format.
       
    32 const QString TIME_FORMAT("hh:mm"); //Time format.
       
    33 //priority icons
       
    34 const QString MSG_HIGH_PRIORITY_ICON("qtg_small_priority_high");
       
    35 const QString MSG_LOW_PRIORITY_ICON("qtg_small_priority_low");
       
    36 
       
    37 //---------------------------------------------------------------
       
    38 // DraftsModel::DraftsModel
       
    39 // @see header
       
    40 //---------------------------------------------------------------
       
    41 DraftsModel::DraftsModel(QObject* parent) :
       
    42     QStandardItemModel(parent), mReady(false)
       
    43 {    
       
    44 }
       
    45 
       
    46 //---------------------------------------------------------------
       
    47 // DraftsModel::~DraftsModel
       
    48 // @see header
       
    49 //---------------------------------------------------------------
       
    50 DraftsModel::~DraftsModel()
       
    51 {
       
    52 }
       
    53 
       
    54 //---------------------------------------------------------------
       
    55 // DraftsModel::data
       
    56 // @see header
       
    57 //---------------------------------------------------------------
       
    58 QVariant DraftsModel::data(const QModelIndex & index, int role) const
       
    59 {
       
    60     QVariant value;
       
    61     QStandardItem* item = itemFromIndex(index);
       
    62     switch (role) {
       
    63     case Qt::DisplayRole:
       
    64     {
       
    65         QStringList displayList;
       
    66         //timestamp conversion
       
    67         QDateTime dateTime;
       
    68         dateTime.setTime_t(item->data(TimeStamp).toUInt());
       
    69         QString dateString;
       
    70         if (dateTime.date() == QDateTime::currentDateTime().date())
       
    71         {
       
    72             dateString = dateTime.toString(TIME_FORMAT);
       
    73         }
       
    74         else
       
    75         {
       
    76             dateString = dateTime.toString(DATE_FORMAT);
       
    77         }
       
    78         //display name
       
    79         QString contactName = item->data(NickName).toString();
       
    80         if (contactName.isEmpty()) {
       
    81             contactName = tr("(no recipient)");
       
    82         }
       
    83         //description
       
    84         QString description = item->data(Subject).toString();
       
    85         if(description.isEmpty())
       
    86         {
       
    87             description = QString(" ");
       
    88         }
       
    89         displayList << contactName << description << dateString;      
       
    90 
       
    91         value = displayList;
       
    92         break;       
       
    93     }
       
    94     case Qt::DecorationRole:
       
    95     {
       
    96         QVariant icon;
       
    97         int priority = item->data(MessagePriority).toInt();
       
    98         
       
    99         if(priority == ConvergedMessage::Low)
       
   100         {
       
   101             icon = HbIcon(MSG_LOW_PRIORITY_ICON);
       
   102         }
       
   103         else if (priority == ConvergedMessage::High)
       
   104         {
       
   105             icon = HbIcon(MSG_HIGH_PRIORITY_ICON);
       
   106         }
       
   107 
       
   108         QList<QVariant> decorationList;
       
   109         decorationList << QVariant() << icon;
       
   110         value = decorationList;
       
   111 
       
   112         break;
       
   113     }
       
   114     case TimeStamp:
       
   115         {
       
   116             value = item->data(TimeStamp);
       
   117             break;
       
   118         }
       
   119     case ConvergedMsgId:
       
   120         {
       
   121             value = item->data(ConvergedMsgId);
       
   122             break;
       
   123         }
       
   124     case MessageType:
       
   125     {
       
   126         value = item->data(MessageType);
       
   127         break;
       
   128     }
       
   129     case Subject:
       
   130     {
       
   131         value = item->data(Subject);
       
   132         break;
       
   133     }
       
   134     case NickName:
       
   135     {
       
   136         value = item->data(NickName);
       
   137         break;
       
   138     }
       
   139     case MessageProperty:
       
   140     {
       
   141         value = item->data(MessageProperty);
       
   142         break;
       
   143     }
       
   144     case MessagePriority:
       
   145     {
       
   146         value = item->data(MessagePriority);        
       
   147         break;
       
   148     }
       
   149     }
       
   150     return value;
       
   151 }
       
   152 
       
   153 //---------------------------------------------------------------
       
   154 // DraftsModel::addRow
       
   155 // @see header
       
   156 //---------------------------------------------------------------
       
   157 void DraftsModel::addRow(const TMsvEntry& entry)
       
   158 {
       
   159     int msgId = entry.Id();
       
   160 
       
   161     //match msgId in model, if found update else add item
       
   162     QModelIndexList indexList =
       
   163         this->match(index(0, 0), ConvergedMsgId, msgId, 1, Qt::MatchExactly);
       
   164 
       
   165     // if not found, add new item
       
   166     if (indexList.count() == 0) {
       
   167         QStandardItem* item = new QStandardItem();
       
   168         populateItem(*item, entry);
       
   169         appendRow(item);
       
   170     }
       
   171     else {
       
   172         // Update an existing item
       
   173         QModelIndex index = indexList[0];
       
   174         QStandardItem* item = this->item(index.row(), 0);
       
   175         populateItem(*item, entry);
       
   176     }
       
   177 }
       
   178 
       
   179 //---------------------------------------------------------------
       
   180 // DraftsModel::deleteRow
       
   181 // @see header
       
   182 //---------------------------------------------------------------
       
   183 void DraftsModel::deleteRow(int msgId)
       
   184 {
       
   185     //match, if found remove item
       
   186     QModelIndexList indexList =
       
   187         this->match(index(0, 0), ConvergedMsgId, msgId, 1, Qt::MatchExactly);
       
   188 
       
   189     if (indexList.count() == 1) {
       
   190         QModelIndex index = indexList[0];
       
   191         this->removeRow(index.row());
       
   192     }
       
   193 }
       
   194 
       
   195 //---------------------------------------------------------------
       
   196 // DraftsModel::setReady
       
   197 // @see header
       
   198 //---------------------------------------------------------------
       
   199 void DraftsModel::setReady()
       
   200 {
       
   201     mReady = true;
       
   202 }
       
   203 
       
   204 //---------------------------------------------------------------
       
   205 // DraftsModel::isReady
       
   206 // @see header
       
   207 //---------------------------------------------------------------
       
   208 bool DraftsModel::isReady()
       
   209 {
       
   210     return mReady;
       
   211 }
       
   212 
       
   213 //---------------------------------------------------------------
       
   214 // DraftsModel::populateItem
       
   215 // @see header
       
   216 //---------------------------------------------------------------
       
   217 void DraftsModel::populateItem(QStandardItem& item, const TMsvEntry& entry)
       
   218 {
       
   219     // id 
       
   220     item.setData((int) entry.Id(), ConvergedMsgId);
       
   221 
       
   222     //message type
       
   223     item.setData(ConversationsEngineUtility::messageType(entry.iMtm.iUid), MessageType);
       
   224 
       
   225     // description
       
   226     QString Description(S60QConversions::s60DescToQString(entry.iDescription));
       
   227     item.setData(Description, Subject);
       
   228 
       
   229     // time stamp
       
   230     TTime unixEpoch(KUnixEpoch);
       
   231     TTimeIntervalSeconds seconds;
       
   232     TTime timeStamp(entry.iDate.Int64());
       
   233     timeStamp.SecondsFrom(unixEpoch, seconds);
       
   234     item.setData(seconds.Int(), TimeStamp);
       
   235 
       
   236     // contact details
       
   237     QString contact(S60QConversions::s60DescToQString(entry.iDetails));
       
   238     item.setData(contact, NickName);
       
   239     
       
   240     // Attachments
       
   241     if (entry.Attachment()) {
       
   242         item.setData(ConvergedMessage::Attachment, MessageProperty);
       
   243     }
       
   244 
       
   245     // Priority
       
   246     TMsvPriority priority = entry.Priority();
       
   247     if (EMsvHighPriority == priority) {
       
   248         item.setData(ConvergedMessage::High, MessagePriority);
       
   249     }
       
   250     else if (EMsvLowPriority == priority) {
       
   251         item.setData(ConvergedMessage::Low, MessagePriority);
       
   252     }
       
   253 }
       
   254 
       
   255 // EOF