emailuis/nmailuiengine/src/nmfolderlistmodel.cpp
changeset 18 578830873419
child 30 759dc5235cdb
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 NmFolderListModel
       
    22     \brief The NmFolderListModel class represents data model for mailbox list.
       
    23     @alpha
       
    24 
       
    25     The NmFolderListModel class uses NmFolderMetaData class to represent data returned in its' data
       
    26     method to get all information needed for one list row for a widget by calling the method once.
       
    27 */
       
    28 
       
    29 class NmFolderListModelPrivate
       
    30 {
       
    31 public:
       
    32     NmFolderListModelPrivate();
       
    33     ~NmFolderListModelPrivate();
       
    34 public:
       
    35     QList<NmFolderMetaData*> mMetaDataList;
       
    36 };
       
    37 
       
    38 NmFolderListModelPrivate::NmFolderListModelPrivate()
       
    39 {
       
    40 }
       
    41 
       
    42 NmFolderListModelPrivate::~NmFolderListModelPrivate()
       
    43 {
       
    44     while (!mMetaDataList.isEmpty()) {
       
    45         delete mMetaDataList.takeLast();
       
    46     }
       
    47 }
       
    48 
       
    49 /*!
       
    50 	Constructor
       
    51  */
       
    52 NmFolderListModel::NmFolderListModel(NmDataManager &dataManager, QObject *parent) 
       
    53 :QAbstractListModel(parent),
       
    54 mDataManager(dataManager)
       
    55 {
       
    56     d = new NmFolderListModelPrivate;
       
    57 }
       
    58 
       
    59 /*!
       
    60 	Destructor
       
    61  */
       
    62 NmFolderListModel::~NmFolderListModel()
       
    63 {
       
    64     delete d;
       
    65 }
       
    66 
       
    67 /*!
       
    68     Returns parent index.
       
    69  */
       
    70 QModelIndex NmFolderListModel::parent(const QModelIndex &child) const
       
    71 {
       
    72     Q_UNUSED(child);
       
    73     return QModelIndex();
       
    74 }
       
    75 
       
    76 /*!
       
    77     Returns the number of list boxes for the current protocol plugin.
       
    78  */
       
    79 int NmFolderListModel::rowCount(const QModelIndex &parent) const
       
    80 {
       
    81     Q_UNUSED(parent);
       
    82     return d->mMetaDataList.size();
       
    83 }
       
    84 
       
    85 /*!
       
    86     This returns always 1.
       
    87  */
       
    88 int NmFolderListModel::columnCount(const QModelIndex &parent) const
       
    89 {
       
    90     Q_UNUSED(parent);
       
    91 	return 1;
       
    92 }
       
    93 
       
    94 /*!
       
    95     Returns data specified by \a index. Only Qt::DisplayRole is supported in \a role.
       
    96     The refresh method must have been called before this method can return any real data.
       
    97  */
       
    98 QVariant NmFolderListModel::data(const QModelIndex &index, int role) const
       
    99 {
       
   100     if (!index.isValid())
       
   101         return QVariant();
       
   102 
       
   103     if (index.row() >= rowCount())
       
   104         return QVariant();
       
   105 
       
   106     if (role == Qt::DisplayRole) {
       
   107         NmFolderMetaData* meta = d->mMetaDataList.at(index.row());
       
   108         // pass address for meta data structure, good enough to run in a single thread.
       
   109         int addr = (int)meta;
       
   110         return (QVariant)addr;
       
   111     }
       
   112     else
       
   113         return QVariant();
       
   114 
       
   115 }
       
   116 
       
   117 /*!
       
   118     This refreshes the data of the model.
       
   119  */
       
   120 void NmFolderListModel::refresh(
       
   121         QList<NmFolder*>& folderList)
       
   122 {
       
   123     NMLOG("nmuiengine: folder list model refresh");
       
   124     while (!d->mMetaDataList.isEmpty()) {
       
   125         delete d->mMetaDataList.takeLast();
       
   126     }
       
   127     d->mMetaDataList.clear();
       
   128     QList<NmFolder*>::const_iterator it = folderList.constBegin();
       
   129     QList<NmFolder*>::const_iterator itEnd = folderList.constEnd();
       
   130     while ( it != itEnd) {
       
   131         NmFolder* myFolder = *it;
       
   132         NmFolderMetaData* newMeta = new NmFolderMetaData();
       
   133         newMeta->setName( myFolder->name() );
       
   134         newMeta->setId( myFolder->folderId() );
       
   135         d->mMetaDataList.append( newMeta );
       
   136         it++;
       
   137     }
       
   138 }