emailuis/nmhswidget/src/nmhswidgetlistmodel.cpp
changeset 76 38bf5461e270
parent 68 83cc6bae1de8
--- a/emailuis/nmhswidget/src/nmhswidgetlistmodel.cpp	Thu Sep 30 11:43:07 2010 +0300
+++ b/emailuis/nmhswidget/src/nmhswidgetlistmodel.cpp	Thu Oct 14 17:33:43 2010 +0300
@@ -66,22 +66,6 @@
     return qVariant;
 }
 
-/*!
-    This refreshes the data of the model.
-    NOTE: safe guard any call to this function with try-catch.
- */
-void NmHsWidgetListModel::refresh(
-        QList<NmMessageEnvelope*> &envelopeList)
-{
-    NM_FUNCTION;
-    
-    clear();
-    foreach(NmMessageEnvelope *env, envelopeList){
-       NmHsWidgetListModelItem *item = createMessageListModelItem(env);
-       appendRow(item);
-    }
-}
-
 
 /*!
     Create mailbox item
@@ -97,3 +81,136 @@
     item->setData(Hb::StandardItem, Hb::ItemTypeRole);
     return item;
 }
+
+
+/*!
+    Function determines model index in which the new message should be inserted.
+*/
+int NmHsWidgetListModel::getInsertionIndex(
+    const NmMessageEnvelope &envelope) const
+{
+    NM_FUNCTION;
+   
+   int ret(NmNotFoundError);
+   
+   // Date+descending sort mode based comparison.
+   // Go through model and compare sent times with QDateTime >= comparison operation.
+   QList<QStandardItem*> items = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
+   int count(items.count());
+   bool found(false);
+   int i(0);
+   while (i < count && !found) {
+       QModelIndex index = items[i]->index();
+       NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
+       found = envelope.sentTime() >= item->itemMetaData()->sentTime();
+       if (found) {
+           ret = i;
+       }
+       i++;
+    }
+   if (0 == count) {
+       ret = NmNoError;
+   }
+   items.clear();
+   return ret;
+}
+
+
+/*!
+    Inserts given messages into the model
+*/
+void NmHsWidgetListModel::addMessages(const QList<NmMessageEnvelope*> &messageEnvs)
+{
+    NM_FUNCTION;
+    int orig_count = rowCount();
+    foreach(NmMessageEnvelope* env, messageEnvs){
+        int insertionIndex = getInsertionIndex(*env);
+        NmHsWidgetListModelItem *newItem = createMessageListModelItem(env);
+        insertRow(insertionIndex, newItem);
+    }
+
+    if( !messageEnvs.isEmpty() ){
+        if (orig_count == 0){
+            emit modelIsEmpty(false);
+        }
+        emit messagesAddedToModel(); //emit messages added to model 
+    }
+}
+
+/*!
+    This refreshes the data of the model.
+    NOTE: safe guard any call to this function with try-catch.
+ */
+void NmHsWidgetListModel::refresh(const QList<NmMessageEnvelope*> &envelopeList)
+{
+    NM_FUNCTION;
+    
+    clear();
+    foreach(NmMessageEnvelope *env, envelopeList){
+       NmHsWidgetListModelItem *item = createMessageListModelItem(env);
+       appendRow(item);
+    }
+    
+    //As we refresh all data, emit signal in any case
+    if( rowCount() == 0 ){
+        emit modelIsEmpty(true);
+    }else{
+        emit modelIsEmpty(false);
+    }
+}
+
+/*!
+    Updates existing messages in model with given envelopes
+*/
+void NmHsWidgetListModel::updateMessages(const QList<NmMessageEnvelope*> &messageEnvs)
+{
+    NM_FUNCTION; 
+    QList<QStandardItem*> modelItemList = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
+    foreach(NmMessageEnvelope *env, messageEnvs){
+        foreach (QStandardItem *it, modelItemList){
+            QModelIndex index = it->index();
+            NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
+            if (env->messageId() == item->itemMetaData()->messageId()) {
+                item->setItemMetaData(env);
+                break;
+            }
+        }
+    }
+}
+
+
+/*!
+    Removes the given messages from the model
+*/
+void NmHsWidgetListModel::removeMessages(const QList<NmId> &messageIds)
+{
+    NM_FUNCTION; 
+    foreach(NmId msgId, messageIds){
+        //lets refresh the item list every time, so that it really is up to date after each iteration 
+        //(and the count stays valid!)
+        QList<QStandardItem*> modelItemList = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
+        foreach(QStandardItem *it, modelItemList){
+            QModelIndex index = it->index();
+            NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
+            if (msgId == item->itemMetaData()->messageId()) {
+                removeRow(index.row());
+                break;
+            }
+        }
+    }
+    
+    //if model is empty after removing messages, signal it.
+    if( rowCount() == 0 ){
+        emit modelIsEmpty(true);  
+    }
+    
+}
+
+/*!
+    Clears the model and emits modelIsEmpty signal
+*/
+void  NmHsWidgetListModel::removeAllMessages()
+{
+    clear();
+    emit modelIsEmpty(true);
+}