phoneuis/bubblemanager2/bubblecore/src/bubbleparticipantlistmodel.cpp
changeset 22 6bb1b21d2484
parent 21 92ab7f8d0eab
child 51 f39ed5e045e0
equal deleted inserted replaced
21:92ab7f8d0eab 22:6bb1b21d2484
    22 {
    22 {
    23 public:
    23 public:
    24     int mBubbleId;
    24     int mBubbleId;
    25     QString mName;
    25     QString mName;
    26     int mState;
    26     int mState;
       
    27     bool mCiphering;
    27 
    28 
    28 public:
    29 public:
    29     Participant(int bubbleId, const QString &name, int state);
    30     Participant(int bubbleId, const QString &name, int state, bool ciphering);
    30 
    31 
    31 };
    32 };
    32 
    33 
    33 Participant::Participant(int bubbleId, const QString &name, int state) :
    34 Participant::Participant(
    34     mBubbleId(bubbleId), mName(name), mState(state)
    35     int bubbleId,
       
    36     const QString &name,
       
    37     int state,
       
    38     bool ciphering) :
       
    39     mBubbleId(bubbleId), mName(name), mState(state), mCiphering(ciphering)
    35 {
    40 {
    36 }
    41 }
    37 
    42 
    38 
    43 
    39 BubbleParticipantListModel::BubbleParticipantListModel()
    44 BubbleParticipantListModel::BubbleParticipantListModel()
    59 
    64 
    60     if (role == Qt::DisplayRole) {
    65     if (role == Qt::DisplayRole) {
    61         return player->mName;
    66         return player->mName;
    62     } else if (role == Qt::DecorationRole) {
    67     } else if (role == Qt::DecorationRole) {
    63         return player->mState;
    68         return player->mState;
       
    69     } else if (role == Qt::StatusTipRole) {
       
    70         return player->mCiphering;
    64     } else {
    71     } else {
    65         return QVariant();
    72         return QVariant();
    66     }
    73     }
    67 }
    74 }
    68 
    75 
    69 void BubbleParticipantListModel::addParticipant(
    76 void BubbleParticipantListModel::addParticipant(
    70     int bubbleId,
    77     int bubbleId,
    71     const QString &name,
    78     const QString &name,
    72     int state )
    79     int state,
       
    80     bool ciphering)
    73 {
    81 {
    74     Participant* p = new Participant(bubbleId,name,state);
    82     bool itemExist=false;
    75     mParticipants.append(p);
    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     }
    76 }
   146 }
    77 
   147 
    78 int BubbleParticipantListModel::bubbleId(int row)
   148 int BubbleParticipantListModel::bubbleId(int row)
    79 {
   149 {
    80     if (mParticipants.count()>=row) {
   150     if (mParticipants.count()>=row) {