phoneuis/bubblemanager2/bubblecore/src/bubbleparticipantlistmodel.cpp
changeset 37 ba76fc04e6c2
child 50 377c906a8701
child 51 f39ed5e045e0
equal deleted inserted replaced
36:2eacb6118286 37:ba76fc04e6c2
       
     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 conference participant list.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QtCore>
       
    19 #include "bubbleparticipantlistmodel.h"
       
    20 
       
    21 class Participant
       
    22 {
       
    23 public:
       
    24     int mBubbleId;
       
    25     QString mName;
       
    26     int mState;
       
    27     bool mCiphering;
       
    28 
       
    29 public:
       
    30     Participant(int bubbleId, const QString &name, int state, bool ciphering);
       
    31 
       
    32 };
       
    33 
       
    34 Participant::Participant(
       
    35     int bubbleId,
       
    36     const QString &name,
       
    37     int state,
       
    38     bool ciphering) :
       
    39     mBubbleId(bubbleId), mName(name), mState(state), mCiphering(ciphering)
       
    40 {
       
    41 }
       
    42 
       
    43 
       
    44 BubbleParticipantListModel::BubbleParticipantListModel()
       
    45 {
       
    46 }
       
    47 
       
    48 BubbleParticipantListModel::~BubbleParticipantListModel()
       
    49 {
       
    50     qDeleteAll(mParticipants);
       
    51 }
       
    52 
       
    53 int BubbleParticipantListModel::rowCount(const QModelIndex &parent) const
       
    54 {
       
    55     Q_UNUSED(parent);
       
    56     return mParticipants.count();
       
    57 }
       
    58 
       
    59 QVariant BubbleParticipantListModel::data(
       
    60     const QModelIndex &index,
       
    61     int role ) const
       
    62 {
       
    63     Participant *player = mParticipants.at(index.row());
       
    64 
       
    65     if (role == Qt::DisplayRole) {
       
    66         return player->mName;
       
    67     } else if (role == Qt::DecorationRole) {
       
    68         return player->mState;
       
    69     } else if (role == Qt::StatusTipRole) {
       
    70         return player->mCiphering;
       
    71     } else {
       
    72         return QVariant();
       
    73     }
       
    74 }
       
    75 
       
    76 void BubbleParticipantListModel::addParticipant(
       
    77     int bubbleId,
       
    78     const QString &name,
       
    79     int state,
       
    80     bool ciphering)
       
    81 {
       
    82     bool itemExist=false;
       
    83 
       
    84     // check, if existing item (bubble)
       
    85     for (int i=0; i < mParticipants.count(); i++) {
       
    86         Participant* p = mParticipants[i];
       
    87         if (p->mBubbleId == bubbleId) {
       
    88             if (isDataChanged(*p,name,state,ciphering)) {
       
    89                 updateData(*p,name,state,ciphering);
       
    90                 QModelIndex index = QAbstractListModel::createIndex(i,0);
       
    91                 emit dataChanged(index,index);
       
    92             }
       
    93             itemExist=true;
       
    94             break;
       
    95         }
       
    96     }
       
    97 
       
    98     if (!itemExist) {
       
    99         // insert new item
       
   100         beginInsertRows(QModelIndex(), mParticipants.count(), mParticipants.count());
       
   101         Participant* p = new Participant(bubbleId,name,state, ciphering);
       
   102         mParticipants.append(p);
       
   103         endInsertRows();
       
   104     }
       
   105 }
       
   106 
       
   107 bool BubbleParticipantListModel::isDataChanged(
       
   108     const Participant& participant,
       
   109     const QString &name,
       
   110     int state,
       
   111     bool ciphering) const
       
   112 {
       
   113     if ( participant.mName != name ||
       
   114          participant.mState != state ||
       
   115          participant.mCiphering != ciphering ) {
       
   116         return true;
       
   117     } else {
       
   118         return false;
       
   119     }
       
   120 }
       
   121 
       
   122 void BubbleParticipantListModel::updateData(
       
   123     Participant& participant,
       
   124     const QString &name,
       
   125     int state,
       
   126     bool ciphering) const
       
   127 {
       
   128     participant.mName = name;
       
   129     participant.mState = state;
       
   130     participant.mCiphering = ciphering;
       
   131 }
       
   132 
       
   133 void BubbleParticipantListModel::removeParticipant(int bubbleId)
       
   134 {
       
   135     QMutableListIterator<Participant*> i(mParticipants);
       
   136     int j=0;
       
   137     while(i.hasNext()) {
       
   138         Participant* p = i.next();
       
   139         if (p->mBubbleId == bubbleId) {
       
   140             beginRemoveRows(QModelIndex(), j, j);
       
   141             i.remove();
       
   142             endRemoveRows();
       
   143         }
       
   144         j++;
       
   145     }
       
   146 }
       
   147 
       
   148 int BubbleParticipantListModel::bubbleId(int row)
       
   149 {
       
   150     if (mParticipants.count()>=row) {
       
   151         return mParticipants.at(row)->mBubbleId;
       
   152     } else {
       
   153         return -1;
       
   154     }
       
   155 }
       
   156 
       
   157 void BubbleParticipantListModel::reset()
       
   158 {
       
   159     qDeleteAll(mParticipants);
       
   160     mParticipants.clear();
       
   161 }
       
   162