summaryrefslogtreecommitdiff
path: root/src/model/ThreadListModel.cpp
blob: 7c6ca0c281b942875039d3c0a8fa38ff08166b6b (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <sstream>
#include <QQmlEngine>
#include <QDebug>

#include "ThreadListModel.h"


ThreadListModel::ThreadListModel (const QList<Message *> &data, QObject * parent)
  : QAbstractListModel (parent),
    m_data (data)
{
  QListIterator<Message *> it (m_data);
  while (it.hasNext ())
  {
    rowAdded (it.next ());
  }
}


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


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

  Message *msg = m_data[idx];

  switch (role)
  {
  case METHOD:
    return msg->method ();
  case TEXT:
    return msg->text ();
  case TITLE:
    return msg->title ();
  case PUBKEY:
    return msg->place ()->pubKeyString ();
  default:
    return QVariant();
  }
}


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

  roles[METHOD] = "method";
  roles[TEXT] = "text";
  roles[TITLE] = "title";
  roles[PUBKEY] = "pubKey";

  return roles;
}


/**
 * A message was added to the source data.
 */
void
ThreadListModel::rowAdded (Message *msg)
{
  int idx = m_count++;
  qDebug () << "ThreadListModel::rowAdded: " << idx;

  m_thread += new ThreadModel (msg, this);

  beginInsertRows (QModelIndex (), idx, idx);
  endInsertRows ();

  connect (msg, &Message::modified,
           this, &ThreadListModel::rowModified);

  connect (msg, &Message::childAdded,
           m_thread[idx], &ThreadModel::rowAdded);

}


void
ThreadListModel::rowModified (Message *msg)
{
  QModelIndex index = this->index (msg->index ());
  emit dataChanged (index, index);
}


int
ThreadListModel::count ()
{
  return m_count;
}


Message *
ThreadListModel::get (int idx)
{
  if (idx < 0 || count () < idx)
    return NULL;

  Message *msg = m_data[idx];

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

  return msg;
}


Message *
ThreadListModel::get (QModelIndex index)
{
  if (count () < index.row ())
    return NULL;

  return m_data[index.row ()];
}


ThreadModel *
ThreadListModel::threadModel (int idx)
{
  if (idx < 0 || count () < idx)
    return NULL;

  ThreadModel *thread = m_thread[idx];

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

  return thread;
}


ThreadModel *
ThreadListModel::threadModel (QModelIndex index)
{
  if (count () < index.row ())
    return NULL;

  return m_thread[index.row ()];
}


#if FIXME
bool
ThreadListModel::contains (QString id)
{
  return m_lookupIndex.contains (id);
}


Message *
ThreadListModel::get (QString key)
{
  if (m_lookupIndex.contains (key))
  {
    Message *msg = m_data[m_lookupIndex[key]];

    return msg;

  }
  else
    return NULL;
}
#endif