summaryrefslogtreecommitdiff
path: root/src/social/place.cpp
blob: 06e9c35a5a8a717e47908e0be0e66ac46e3a6c87 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "place.h"
#include "model/models.h"
#include "model/PlaceListModel.h"
#include "app.h"

#include <QApplication>
#include <QClipboard>
#include <QTime>

/**
 * @brief Place::Place, constructor should receive the public key as title
 * @param key
 * @param parent
 */
Place::Place (QObject *parent) : QObject (parent)
{
  m_type = "group";             //thread;group;contact
  m_isHost = false;
  m_inTransmit = false;
  m_msg = 0;
  m_mod = 0;

  connect (this, &Place::setTrayMessageSignal,
           theApp, &App::setTrayMessage, Qt::QueuedConnection);
}

void
Place::init (const GNUNET_CRYPTO_EddsaPublicKey pubKey)
{
  char *pubKeyChars = GNUNET_CRYPTO_eddsa_public_key_to_string (&pubKey);
  QString pubKeyStr = QString::fromLatin1 (pubKeyChars);

  setPubKey (pubKey);
  setName (pubKeyStr);
}

/**
 * Copy public key of place to clipboard.
 */
void
Place::copyToClipboard ()
{
  QClipboard *clipboard = QApplication::clipboard ();

  clipboard->setText (m_pubKeyStr);
}

/**
 * Send a message to the place.
 *
 * @param text
 *        The message.
 */
void
Place::talk (QString text)
{
#if FIXME
  if (m_isHost)
  {
    emit hostTalkSignal (this, text);
  }
  else
  {
    emit guestTalkSignal (this, text);
  }
#endif
}


GNUNET_PSYC_Slicer *
Place::createSlicer ()
{
  m_slicer = GNUNET_PSYC_slicer_create ();
  GNUNET_PSYC_slicer_method_add (m_slicer, "", NULL,
                                 recvMethodCallback,
                                 recvModifierCallback,
                                 recvDataCallback,
                                 recvEomCallback,
                                 this);
  return m_slicer;
}


void // static
Place::recvMethodCallback (void *cls,
                           const struct GNUNET_PSYC_MessageHeader *msg,
                           const struct GNUNET_PSYC_MessageMethod *meth,
                           uint64_t message_id,
                           const char *method_name)
{
  Place *p = (Place *) cls;
  p->recvMethod (msg, meth, message_id, method_name);
}


void
Place::recvMethod (const struct GNUNET_PSYC_MessageHeader *msg,
                   const struct GNUNET_PSYC_MessageMethod *meth,
                   uint64_t message_id,
                   const char *method_name)
{
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "Test #: Host received method for message ID %i:\n" "%s\n",
              message_id, method_name);
  if (m_msg)
    delete m_msg;
  m_msg = new Message (method_name);
  m_mod = 0;
}


void // static
Place::recvModifierCallback (void *cls,
                             const struct GNUNET_PSYC_MessageHeader *msg,
                             const struct GNUNET_MessageHeader *pmsg,
                             uint64_t message_id,
                             enum GNUNET_PSYC_Operator oper,
                             const char *name,
                             const void *value,
                             uint16_t value_size,
                             uint16_t full_value_size)
{
  Place *p = (Place *) cls;
  p->recvModifier (msg, pmsg, message_id, oper, name,
                   value, value_size, full_value_size);
}


void
Place::recvModifier (const struct GNUNET_PSYC_MessageHeader *msg,
                     const struct GNUNET_MessageHeader *pmsg,
                     uint64_t message_id,
                     enum GNUNET_PSYC_Operator oper,
                     const char *name,
                     const void *value,
                     uint16_t value_size,
                     uint16_t full_value_size)
{
  QString str = QString::fromLatin1 ((char *) value);

  qWarning () << "Host received modifier for message ID"
              << message_id << oper << name << value_size << str;
  Q_ASSERT (m_msg);

  if (!m_mod)
  {
    m_mod = new Modifier (oper,
                          QString (name),
                          QByteArray ((const char *) value, value_size));
  }
  else
  {
    m_mod->value ().append ((const char *) value, value_size);
  }

  if (m_mod->value ().size () == full_value_size)
  {
    m_msg->appendModifier (m_mod);
    m_mod = 0;
  }
}


void // static
Place::recvDataCallback (void *cls,
                         const struct GNUNET_PSYC_MessageHeader *msg,
                         const struct GNUNET_MessageHeader *pmsg,
                         uint64_t message_id,
                         const void *data,
                         uint16_t data_size)
{
  Place *p = (Place *) cls;
  p->recvData (msg, pmsg, message_id, data, data_size);
}


void
Place::recvData (const struct GNUNET_PSYC_MessageHeader *msg,
                 const struct GNUNET_MessageHeader *pmsg,
                 uint64_t message_id,
                 const void *data,
                 uint16_t data_size)
{
  QString str = QString::fromLatin1 ((char *) data);
  qWarning () << "Host received data for message ID "
              << message_id << data_size << str;

  Q_ASSERT (m_msg);
  m_msg->appendData ((char *) data, data_size);
}


void // static
Place::recvEomCallback (void *cls,
                        const struct GNUNET_PSYC_MessageHeader *msg,
                        const struct GNUNET_MessageHeader *pmsg,
                        uint64_t message_id,
                        uint8_t is_cancelled)
{
  Place *p = (Place *) cls;
  p->recvEom (msg, pmsg, message_id, is_cancelled);
}


void
Place::recvEom (const struct GNUNET_PSYC_MessageHeader *msg,
                const struct GNUNET_MessageHeader *pmsg,
                uint64_t message_id,
                uint8_t is_cancelled)
{
  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
              "Test #: Host received end of message ID %i, cancelled: %u\n",
              message_id, is_cancelled);
  Q_ASSERT (m_msg);

  if (GNUNET_NO == is_cancelled)
    insertMessage (m_msg);
  m_msg = 0;
}


/**
 * Insert message to its place in the thread.
 */
void
Place::insertMessage (Message *msg)
{
  m_msgHash.insert (msg->id (), msg);

  // Find thread

  uint64_t threadId = 0;
  Modifier *mod = msg->findModifier ("_id_thread");
  if (mod)
    threadId = mod->amount ();

  Message *thread = m_threadHash.value (threadId);
  if (!thread)
  {
    if (msg->id () == threadId)
    {
      thread = msg;
    }
    else // missing thread starter message, or message not in thread
    {
      // Create a placeholder message that will be replaced
      // when the missing message arrives (or not if it's not in a thread).
      thread = new Message ("_missing");

      if (0 < threadId)
      {
        /// @todo request missing message(s) in thread from history
        //FIXME: GNUNET_SOCIAL_history_replay_thread (m_plc, threadId, ...);
      }
    }

    m_threadHash.insert (threadId, thread);
    m_threadList.append (thread);
  }

  // Find parent message

  Message *parent = 0;
  uint64_t parentId = 0;
  mod = msg->findModifier ("_id_reply");
  if (mod)
    parentId = mod->amount ();

  if (0 < parentId)
    parent = m_msgHash.value (parentId);

  if (!parent && thread != msg)
    parent = thread;

  if (parent)
    parent->appendChild (msg);
}


int
Place::notifyDataCallback (void *cls, uint16_t *data_size, void *data)
{
  Place *p = (Place *) cls;
  return p->notifyData (data_size, data);
}


int
Place::notifyData (uint16_t *data_size, void *data)
{
  qDebug () << "Place: data_sent=" << m_tmitSent << ", "
            << "data_size=" << *data_size;

  std::string sub = m_tmitData.substr (m_tmitSent, *data_size);
  *data_size = sub.size ();
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data_size: %u\n", *data_size);
  memcpy (data, sub.c_str (), *data_size);
  m_tmitSent += *data_size;

  if (m_tmitSent < m_tmitData.size ())
    return GNUNET_NO;

  m_inTransmit = false;
  emit dataSentSignal ();
  return GNUNET_YES;
}