emailuis/nmailuiengine/src/nmmailboxlistmodel.cpp
changeset 18 578830873419
child 27 9ba4404ef423
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     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 "nmuiengineheaders.h"
       
    19 
       
    20 /*!
       
    21     \class NmMailboxListModel
       
    22     \brief The NmMailboxListModel class represents data model for mailbox list.
       
    23     @alpha
       
    24 
       
    25     The NmMailboxListModel class uses NmMailboxMetaData class to represent data returned in its'
       
    26     data method to get all information needed for one list row for a widget by calling the method
       
    27     once.
       
    28 */
       
    29 
       
    30 /*!
       
    31     Constructor
       
    32  */
       
    33 NmMailboxListModel::NmMailboxListModel(NmDataManager &dataManager, QObject *parent) 
       
    34 :QStandardItemModel(parent),
       
    35 mDataManager(dataManager)
       
    36 {
       
    37 }
       
    38 
       
    39 /*!
       
    40 	Destructor
       
    41  */
       
    42 NmMailboxListModel::~NmMailboxListModel()
       
    43 {
       
    44     clear();
       
    45 }
       
    46 
       
    47 /*!
       
    48     Returns data specified by \a index. Only Qt::DisplayRole is supported in \a role.
       
    49     The refresh method must have been called before this method can return any real data.
       
    50  */
       
    51 QVariant NmMailboxListModel::data(const QModelIndex &index, int role) const
       
    52 {
       
    53     QVariant qVariant;
       
    54     if (index.isValid() && Qt::DisplayRole == role) {
       
    55         NmMailboxListModelItem *item = static_cast<NmMailboxListModelItem*>(itemFromIndex(index));
       
    56         NmMailboxMetaData *mailbox = item->itemMetaData();
       
    57         qVariant = QVariant::fromValue(mailbox);
       
    58     }
       
    59     return qVariant;
       
    60 }
       
    61 
       
    62 /*!
       
    63     This refreshes the data of the model.
       
    64  */
       
    65 void NmMailboxListModel::refresh(
       
    66         QList<NmMailbox*> &mailboxList)
       
    67 {
       
    68     NMLOG("nmuiengine: mailbox list model refresh");
       
    69     clear();
       
    70     for (int i(0); i < mailboxList.count(); i++) {
       
    71         NmMailbox *mailbox = mailboxList[i];
       
    72         if (mailbox) {
       
    73             NmMailboxListModelItem *item = createMailboxItem(mailbox);
       
    74             appendRow(item);
       
    75         }
       
    76     }
       
    77 }
       
    78 
       
    79 /*!
       
    80     Updates specific model item.
       
    81     \param mailboxId Mailbox id.
       
    82     \param allowEmitDataChanged If <code>true</code> data changed signal
       
    83            is emitted \sa QStandardItem, otherwise signal is not send.
       
    84  */
       
    85 void NmMailboxListModel::refreshModelItem(const NmId &mailboxId, bool allowEmitDataChanged)
       
    86 {
       
    87     NMLOG("NmMailboxListModel::refreshModelItem");
       
    88 
       
    89     // Get correct mailbox data.
       
    90     NmMailbox* mailbox = mDataManager.mailbox(mailboxId);
       
    91     NmMailboxListModelItem *entryItem(NULL);
       
    92 
       
    93     // Get correct entry item.
       
    94     const int itemCount(rowCount());
       
    95     for (int itemIndex(0); itemIndex < itemCount; ++itemIndex) {
       
    96         entryItem = static_cast<NmMailboxListModelItem *>(itemFromIndex(index(itemIndex,0)));
       
    97         if (entryItem->itemMetaData()->id() == mailboxId) {
       
    98             break;
       
    99         }
       
   100         entryItem = NULL;
       
   101     }
       
   102 
       
   103     // Update entry item's data.
       
   104     if (mailbox && entryItem) {
       
   105         if (allowEmitDataChanged) {
       
   106             // Changes data and emits datachanged.
       
   107             NmMailboxMetaData *metaData = new NmMailboxMetaData;
       
   108             metaData->setId(mailbox->id());
       
   109             metaData->setName(mailbox->name());
       
   110             entryItem->setItemMetaData(metaData);
       
   111         } else {
       
   112             // Only changes meta data information (mailbox name).
       
   113             // No need for updating mailbox id.
       
   114             // Do not call emitDataChanged, because it seems that if
       
   115             // emitDataChanged is called twice, it leads to KERN-EXEC 3 Panic.
       
   116             entryItem->itemMetaData()->setName(mailbox->name());
       
   117        }
       
   118     }
       
   119     NMLOG("NmMailboxListModel::refreshModelItem - OK");
       
   120 }
       
   121 
       
   122 /*!
       
   123     Function handles mailbox events such as created, renamed and deleted
       
   124  */
       
   125 void NmMailboxListModel::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
       
   126 {
       
   127     NMLOG("NmMailboxListModel::handleMailboxEvent");
       
   128     for (int a(0); a < mailboxIds.count(); a++) {
       
   129         NmId mailboxId = mailboxIds[a];
       
   130         switch (event) {
       
   131             case NmMailboxCreated: {
       
   132                 NmMailbox* mailbox = mDataManager.mailbox(mailboxId);
       
   133                 if (mailbox) {
       
   134                     NmMailboxListModelItem *item = createMailboxItem(mailbox);
       
   135                     appendRow(item);
       
   136                     item->callEmitDataChanged();
       
   137                 }
       
   138                 delete mailbox;
       
   139                 mailbox = NULL;
       
   140                 break;
       
   141             }
       
   142             case NmMailboxChanged: {
       
   143                 refreshModelItem(mailboxId);
       
   144                 break;
       
   145             }
       
   146             case NmMailboxDeleted: {
       
   147 				int rowIndex(-1);
       
   148 				NmMailboxListModelItem *entryItem(NULL);
       
   149 				// Get correct entry item.
       
   150 				const int itemCount(rowCount());
       
   151 				for (int itemIndex(0); itemIndex < itemCount; ++itemIndex) {
       
   152 					entryItem = static_cast<NmMailboxListModelItem *>(itemFromIndex(index(itemIndex,0)));
       
   153 					if (entryItem && entryItem->itemMetaData()->id() == mailboxId) {
       
   154 						rowIndex = itemIndex;
       
   155 						break;
       
   156 					}
       
   157 				}
       
   158 				if (rowIndex >= 0 && entryItem) {
       
   159 					QStandardItem *parent = entryItem->parent();
       
   160 					removeRow(rowIndex, indexFromItem(parent));
       
   161 				}
       
   162                 break;
       
   163             }
       
   164             default: {
       
   165                 break;
       
   166             }
       
   167         }
       
   168     }
       
   169 }
       
   170 
       
   171 /*!
       
   172     Create mailbox item
       
   173     \param mailbox
       
   174  */
       
   175 NmMailboxListModelItem *NmMailboxListModel::createMailboxItem(const NmMailbox *mailbox)
       
   176 {
       
   177     NmMailboxMetaData *newMeta = new NmMailboxMetaData();
       
   178     newMeta->setId(mailbox->id());
       
   179     newMeta->setName(mailbox->name());
       
   180 
       
   181     NmMailboxListModelItem *item = new NmMailboxListModelItem();
       
   182     item->setItemMetaData(newMeta);
       
   183     item->setData(Hb::StandardItem, Hb::ItemTypeRole);
       
   184     return item;
       
   185 }