summaryrefslogtreecommitdiff
path: root/src/model/EgoListModel.cpp
blob: 0cef0d4a550dd781d7ee13bd2d2e354691af5883 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
     This file is part of SecureShare
     (C) 2013 Bruno Cabral (and other contributing authors)

     SecureShare is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     SecureShare is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with SecureShare; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/


#include <sstream>
#include <QQmlEngine>

#include "EgoListModel.h"


EgoListModel::EgoListModel (const QList<Ego *> &data, QObject * parent)
  : QAbstractListModel (parent),
    m_data (data)
{
}


int
EgoListModel::rowCount (const QModelIndex &parent) const
{
  Q_UNUSED (parent);
  return m_data.count ();
}


QVariant
EgoListModel::data (const QModelIndex &index, int role) const
{
  if (index.row () < 0 || m_data.count () <= index.row ())
    return QVariant ();

  Ego *ego = m_data[index.row ()];

  switch (role)
  {
  case PUBKEY:
    return ego->pubKeyString ();

  case NAME:
    return ego->name ();

  default:
    return QVariant::Invalid;
  }

  return QVariant::Invalid;
}


QHash < int, QByteArray >
EgoListModel::roleNames () const
{
  QHash < int, QByteArray > roles;

  roles[NAME] = "name";
  roles[PUBKEY] = "pubKey";

  return roles;
}


/**
 * An ego was added to the source data.
 */
void
EgoListModel::rowAdded (Ego *ego)
{
  int index = m_data.count ();
  beginInsertRows (QModelIndex (), index, index);
  ego->setIndex (index);

  connect (ego, &Ego::modified,
           this, &EgoListModel::rowModified);

  //m_lookupIndex[pubKey] = index;
  endInsertRows ();
}


void
EgoListModel::rowModified (int index)
{
  QModelIndex modelIndex = this->index (index);
  emit dataChanged (modelIndex, modelIndex);
}


int
EgoListModel::getCount ()
{
  return m_data.count ();
}


Ego *
EgoListModel::get (int index)
{
  if (index < 0 || m_data.count () <= index)
    return NULL;

  Ego *ego = m_data[index];

  //Set the ownership so QML don't delete it.
  QQmlEngine::setObjectOwnership (ego, QQmlEngine::CppOwnership);

  return ego;
}


Ego *
EgoListModel::get (QModelIndex index)
{
  if (m_data.count () <= index.row ())
    return NULL;

  return m_data[index.row ()];
}


#if FIXME
bool
EgoListModel::contains (QString pubKey)
{
  return m_lookupIndex.contains (pubKey);
}


Ego *
EgoListModel::get (QString key)
{
  if (m_lookupIndex.contains (key))
  {
    Ego *ego = m_data[m_lookupIndex[key]];
    return ego;
  }
  else
    return NULL;
}
#endif