blob: f3ad6188343bab3e6c46ed0e80f16a0fc72cd97b [file] [log] [blame]
Yingdi Yu847aa862013-10-09 16:35:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu40eca752013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu3b318c12013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yub2e747d2013-11-05 23:06:43 -080019#include <QtSql/QSqlRecord>
20#include <QtSql/QSqlField>
21#include <QtSql/QSqlError>
Yingdi Yu40eca752013-10-10 15:00:58 -070022
Yingdi Yu3b318c12013-10-15 17:54:00 -070023#ifndef Q_MOC_RUN
Yingdi Yu76dd8002013-12-24 11:16:32 +080024#include <ndn-cpp/security/identity/osx-private-key-storage.hpp>
25#include <ndn-cpp/security/identity/basic-identity-storage.hpp>
26#include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp>
Yingdi Yu3b318c12013-10-15 17:54:00 -070027#include <boost/filesystem.hpp>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070028#include <boost/random/random_device.hpp>
29#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu7989eb22013-10-31 17:38:22 -070030#include "panel-policy-manager.h"
Yingdi Yu76dd8002013-12-24 11:16:32 +080031#include "null-ptrs.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070032#include "logging.h"
33#include "exception.h"
34#endif
35
36namespace fs = boost::filesystem;
37using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080038using namespace ndn::ptr_lib;
Yingdi Yu978b3ae2013-10-23 11:50:51 -070039
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070040using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070041
42INIT_LOGGER("ContactPanel");
43
Yingdi Yu76dd8002013-12-24 11:16:32 +080044Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu7989eb22013-10-31 17:38:22 -070045Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070046
Yingdi Yu8e135832013-11-09 20:12:31 -080047ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080048 : QDialog(parent)
49 , ui(new Ui::ContactPanel)
Yingdi Yu8e135832013-11-09 20:12:31 -080050 , m_warningDialog(new WarningDialog)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080051 , m_contactListModel(new QStringListModel)
52 , m_startChatDialog(new StartChatDialog)
53 , m_invitationDialog(new InvitationDialog)
54 , m_settingDialog(new SettingDialog)
Yingdi Yu76dd8002013-12-24 11:16:32 +080055 , m_policyManager(new PanelPolicyManager())
Yingdi Yu847aa862013-10-09 16:35:53 -070056{
Yingdi Yu76dd8002013-12-24 11:16:32 +080057 qRegisterMetaType<IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070058 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu3b318c12013-10-15 17:54:00 -070059
Yingdi Yu46948282013-11-06 18:43:31 -080060 createAction();
61
Yingdi Yu76dd8002013-12-24 11:16:32 +080062 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
63 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
64 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
65
66 m_contactManager = make_shared<ContactManager>(m_identityManager);
Yingdi Yue35bdb82013-11-07 11:32:40 -080067
Yingdi Yu8e135832013-11-09 20:12:31 -080068 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
69 this, SLOT(showError(const QString&)));
Yingdi Yu8e135832013-11-09 20:12:31 -080070
Yingdi Yue35bdb82013-11-07 11:32:40 -080071 openDB();
Yingdi Yu46948282013-11-06 18:43:31 -080072
Yingdi Yueda39aa2013-10-23 23:07:29 -070073 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070074
Yingdi Yu76dd8002013-12-24 11:16:32 +080075 loadTrustAnchor();
Yingdi Yu46948282013-11-06 18:43:31 -080076
Yingdi Yu76dd8002013-12-24 11:16:32 +080077 m_defaultIdentity = m_identityManager->getDefaultIdentity();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080078 if(m_defaultIdentity.size() == 0)
79 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yu76dd8002013-12-24 11:16:32 +080080 Name defaultCertName = m_identityManager->getDefaultCertificateNameForIdentity(m_defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -080081 if(defaultCertName.size() == 0)
Yingdi Yuf0b9ea92013-11-10 10:54:16 -080082 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yub5a0fd32013-11-10 16:26:52 -080083
84
85 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080086 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080087 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu711f5892013-11-09 22:18:26 -080088
Yingdi Yu46948282013-11-06 18:43:31 -080089
Yingdi Yue35bdb82013-11-07 11:32:40 -080090 m_profileEditor = new ProfileEditor(m_contactManager);
91 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
92
Yingdi Yu8e135832013-11-09 20:12:31 -080093 m_addContactPanel = new AddContactPanel(m_contactManager);
94 m_browseContactDialog = new BrowseContactDialog(m_contactManager);
95 m_setAliasDialog = new SetAliasDialog(m_contactManager);
Yingdi Yue35bdb82013-11-07 11:32:40 -080096
97 ui->setupUi(this);
98
Yingdi Yu76dd8002013-12-24 11:16:32 +080099 m_transport = make_shared<TcpTransport>();
100 m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
101
102 connectToDaemon();
Yingdi Yu8e135832013-11-09 20:12:31 -0800103
Yingdi Yua66183f2013-11-10 10:32:18 -0800104 m_localPrefix = Name("/private/local");
Yingdi Yueda39aa2013-10-23 23:07:29 -0700105 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700106
Yingdi Yueda39aa2013-10-23 23:07:29 -0700107 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -0800108
Yingdi Yub2e747d2013-11-05 23:06:43 -0800109 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700110
111 ui->ContactList->setModel(m_contactListModel);
112
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700113
Yingdi Yu46948282013-11-06 18:43:31 -0800114 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700115 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -0700116
Yingdi Yueda39aa2013-10-23 23:07:29 -0700117 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
118 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -0700119
Yingdi Yueda39aa2013-10-23 23:07:29 -0700120 connect(ui->EditProfileButton, SIGNAL(clicked()),
121 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -0700122
Yingdi Yu711f5892013-11-09 22:18:26 -0800123 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yue433a502013-11-10 01:34:57 -0800124 this, SLOT(showWarning(const QString&)));
Yingdi Yu711f5892013-11-09 22:18:26 -0800125
Yingdi Yueda39aa2013-10-23 23:07:29 -0700126 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yuae8217c2013-11-09 00:03:26 -0800127 this, SLOT(openBrowseContactDialog()));
128
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800129 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
130 this, SLOT(openAddContactPanel()));
131
Yingdi Yuae8217c2013-11-09 00:03:26 -0800132 connect(this, SIGNAL(refreshCertDirectory()),
133 m_browseContactDialog, SLOT(refreshList()));
134
135 connect(ui->DeleteContactButton, SIGNAL(clicked()),
136 this, SLOT(removeContactButton()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700137
Yingdi Yueda39aa2013-10-23 23:07:29 -0700138 connect(ui->settingButton, SIGNAL(clicked()),
139 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700140
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800141 connect(m_addContactPanel, SIGNAL(newContactAdded()),
142 this, SLOT(refreshContactList()));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800143 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700144 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700145 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
146 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700147
Yingdi Yueda39aa2013-10-23 23:07:29 -0700148 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
149 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700150
Yingdi Yu76dd8002013-12-24 11:16:32 +0800151 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::IdentityCertificate&)),
152 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::IdentityCertificate&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700153 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
154 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu72232692013-11-12 17:50:21 -0800155
156 connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)),
157 this, SLOT(addContactIntoPanelPolicy(const ndn::Name&)));
158 connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)),
159 this, SLOT(removeContactFromPanelPolicy(const ndn::Name&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700160
Yingdi Yu46948282013-11-06 18:43:31 -0800161 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
162 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700163
164 connect(this, SIGNAL(newInvitationReady()),
165 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700166
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800167 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
168 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800169
170 connect(ui->addScope, SIGNAL(clicked()),
171 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800172 connect(ui->deleteScope, SIGNAL(clicked()),
173 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800174 connect(ui->saveButton, SIGNAL(clicked()),
175 this, SLOT(saveScopeClicked()));
176
177 connect(ui->endorseButton, SIGNAL(clicked()),
178 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700179}
180
181ContactPanel::~ContactPanel()
182{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700183 delete ui;
184 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800185 delete m_startChatDialog;
186 delete m_invitationDialog;
187 delete m_settingDialog;
188
Yingdi Yueda39aa2013-10-23 23:07:29 -0700189 delete m_profileEditor;
190 delete m_addContactPanel;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800191 delete m_browseContactDialog;
Yingdi Yu46948282013-11-06 18:43:31 -0800192 delete m_setAliasDialog;
193
Yingdi Yub2e747d2013-11-05 23:06:43 -0800194 delete m_trustScopeModel;
195 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800196 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700197
Yingdi Yueda39aa2013-10-23 23:07:29 -0700198 delete m_menuInvite;
Yingdi Yu46948282013-11-06 18:43:31 -0800199 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700200
201 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
202 for(; it != m_chatDialogs.end(); it++)
203 delete it->second;
Yingdi Yu847aa862013-10-09 16:35:53 -0700204}
205
Yingdi Yu40eca752013-10-10 15:00:58 -0700206void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800207ContactPanel::connectToDaemon()
208{
209 //Hack! transport does not connect to daemon unless an interest is expressed.
210 Name name("/ndn");
211 ndn::Interest interest(name);
212 m_face->expressInterest(interest,
213 bind(&ContactPanel::onConnectionData, this, _1, _2),
214 bind(&ContactPanel::onConnectionDataTimeout, this, _1));
215}
216
217void
218ContactPanel::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
219 const shared_ptr<Data>& data)
220{ _LOG_DEBUG("onConnectionData"); }
221
222void
223ContactPanel::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
224{ _LOG_DEBUG("onConnectionDataTimeout"); }
225
226void
Yingdi Yu46948282013-11-06 18:43:31 -0800227ContactPanel::createAction()
228{
229 m_menuInvite = new QAction("&Chat", this);
230 m_menuAlias = new QAction("&Set Alias", this);
231}
232
233void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700234ContactPanel::openDB()
235{
236 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
237 QString path = (QDir::home().path());
238 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
239 db.setDatabaseName(path);
240 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800241 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800242
243 m_trustScopeModel = new QSqlTableModel;
244 m_endorseDataModel = new QSqlTableModel;
245 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700246}
247
248void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800249ContactPanel::loadTrustAnchor()
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700250{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800251 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700252 for(; it != m_contactList.end(); it++)
Yingdi Yue35bdb82013-11-07 11:32:40 -0800253 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800254 m_policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800255 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700256}
257
258void
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800259ContactPanel::setLocalPrefix(int retry)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700260{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800261 Name interestName("/local/ndn/prefix");
262 Interest interest(interestName);
263 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700264
Yingdi Yu76dd8002013-12-24 11:16:32 +0800265 m_face->expressInterest(interest,
266 bind(&ContactPanel::onLocalPrefix, this, _1, _2),
267 bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
268
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700269}
270
271void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800272ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest,
273 const shared_ptr<Data>& data)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700274{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800275 string originPrefix((const char*)data->getContent().buf(), data->getContent().size());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700276 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
277 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700278 m_localPrefix = Name(prefix);
Yingdi Yue433a502013-11-10 01:34:57 -0800279
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700280}
281
282void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800283ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest,
284 int retry)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700285{
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800286 if(retry > 0)
Yingdi Yua66183f2013-11-10 10:32:18 -0800287 {
288 setLocalPrefix(retry - 1);
289 return;
290 }
291 else{
292 m_localPrefix = Name("/private/local");
293 }
Yingdi Yu7989eb22013-10-31 17:38:22 -0700294}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700295
296void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700297ContactPanel::setInvitationListener()
298{
Yingdi Yu46948282013-11-06 18:43:31 -0800299 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
300 m_inviteListenPrefix.append(m_defaultIdentity);
301 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800302 m_invitationListenerId = m_face->registerPrefix(m_inviteListenPrefix,
303 boost::bind(&ContactPanel::onInvitation, this, _1, _2, _3, _4),
304 boost::bind(&ContactPanel::onInvitationRegisterFailed, this, _1));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700305}
306
307void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800308ContactPanel::sendInterest(const Interest& interest,
309 const OnVerified& onVerified,
310 const OnVerifyFailed& onVerifyFailed,
311 const TimeoutNotify& timeoutNotify,
312 int retry /* = 1 */,
313 int stepCount /* = 0 */)
314{
315 m_face->expressInterest(interest,
316 boost::bind(&ContactPanel::onTargetData,
317 this,
318 _1,
319 _2,
320 stepCount,
321 onVerified,
322 onVerifyFailed,
323 timeoutNotify),
324 boost::bind(&ContactPanel::onTargetTimeout,
325 this,
326 _1,
327 retry,
328 stepCount,
329 onVerified,
330 onVerifyFailed,
331 timeoutNotify));
332}
333
334void
335ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest,
336 const shared_ptr<Data>& data,
337 int stepCount,
338 const OnVerified& onVerified,
339 const OnVerifyFailed& onVerifyFailed,
340 const TimeoutNotify& timeoutNotify)
341{
342 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
343
344 if (nextStep)
345 m_face->expressInterest
346 (*nextStep->interest_,
347 bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
348 bind(&ContactPanel::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
349
350}
351
352void
353ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
354 int retry,
355 int stepCount,
356 const OnVerified& onVerified,
357 const OnVerifyFailed& onVerifyFailed,
358 const TimeoutNotify& timeoutNotify)
359{
360 if(retry > 0)
361 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
362 else
363 {
364 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
365 timeoutNotify();
366 }
367}
368
369void
370ContactPanel::onCertData(const shared_ptr<const ndn::Interest>& interest,
371 const shared_ptr<Data>& cert,
372 shared_ptr<ValidationRequest> previousStep)
373{
374 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
375 previousStep->stepCount_,
376 previousStep->onVerified_,
377 previousStep->onVerifyFailed_);
378
379 if (nextStep)
380 m_face->expressInterest
381 (*nextStep->interest_,
382 bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
383 bind(&ContactPanel::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
384}
385
386void
387ContactPanel::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
388 const OnVerifyFailed& onVerifyFailed,
389 const shared_ptr<Data>& data,
390 shared_ptr<ValidationRequest> nextStep)
391{
392 if(nextStep->retry_ > 0)
393 m_face->expressInterest(*interest,
394 bind(&ContactPanel::onCertData,
395 this,
396 _1,
397 _2,
398 nextStep),
399 bind(&ContactPanel::onCertTimeout,
400 this,
401 _1,
402 onVerifyFailed,
403 data,
404 nextStep));
405 else
406 onVerifyFailed(data);
407}
408
409void
410ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix)
411{
412 showError(QString::fromStdString("Cannot register invitation listening prefix"));
413}
414
415void
416ContactPanel::onInvitation(const shared_ptr<const Name>& prefix,
417 const shared_ptr<const Interest>& interest,
418 Transport& transport,
419 uint64_t registeredPrefixId)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700420{
Yingdi Yu46948282013-11-06 18:43:31 -0800421 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800422
Yingdi Yu76dd8002013-12-24 11:16:32 +0800423 shared_ptr<ChronosInvitation> invitation;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800424 try{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800425 invitation = make_shared<ChronosInvitation>(interest->getName());
426 }catch(std::exception& e){
Yingdi Yue35bdb82013-11-07 11:32:40 -0800427 _LOG_ERROR("Exception: " << e.what());
428 return;
429 }
430
Yingdi Yu46948282013-11-06 18:43:31 -0800431 Name chatroomName("/ndn/broadcast/chronos");
432 chatroomName.append(invitation->getChatroom());
433 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
434 if(it != m_chatDialogs.end())
435 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800436 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu46948282013-11-06 18:43:31 -0800437 return;
438 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700439
Yingdi Yu76dd8002013-12-24 11:16:32 +0800440 shared_ptr<PublicKey> keyPtr = m_policyManager->getTrustedKey(invitation->getInviterCertificateName());
441 if(CHRONOCHAT_NULL_PUBLICKEY_PTR != keyPtr && Sha256WithRsaHandler::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), *keyPtr))
Yingdi Yuaf305d72013-11-10 11:54:02 -0800442 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800443 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800444 // hack: incomplete certificate, we don't send it to the wire nor store it anywhere, we only use it to carry information
Yingdi Yuaf305d72013-11-10 11:54:02 -0800445 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800446 bool findCert = false;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800447 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800448 for(; it != m_contactList.end(); it++)
449 {
450 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
451 {
452 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
453 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
454 findCert = true;
455 break;
456 }
457 }
458 if(findCert == false)
459 {
460 _LOG_ERROR("No SelfEndorseCertificate found!");
461 return;
462 }
Yingdi Yuaf305d72013-11-10 11:54:02 -0800463 certificate->setPublicKeyInfo(*keyPtr);
464 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
465 return;
466 }
467
Yingdi Yu76dd8002013-12-24 11:16:32 +0800468 Interest newInterest(invitation->getInviterCertificateName());
469 OnVerified onVerified = boost::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
470 OnVerifyFailed onVerifyFailed = boost::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
471 TimeoutNotify timeoutNotify = boost::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
472
473 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700474}
475
476void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800477ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
478 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu46948282013-11-06 18:43:31 -0800479{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800480 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yu46948282013-11-06 18:43:31 -0800481
Yingdi Yu76dd8002013-12-24 11:16:32 +0800482 if(Sha256WithRsaHandler::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu46948282013-11-06 18:43:31 -0800483 {
484 Name keyName = certificate->getPublicKeyName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800485 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu46948282013-11-06 18:43:31 -0800486 popChatInvitation(invitation, inviterNameSpace, certificate);
487 }
488}
489
490void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800491ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
492{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800493
494void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800495ContactPanel::onInvitationCertTimeoutNotify()
496{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800497
498void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800499ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu46948282013-11-06 18:43:31 -0800500 const Name& inviterNameSpace,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800501 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu46948282013-11-06 18:43:31 -0800502{
503 string alias;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800504 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800505 for(; it != m_contactList.end(); it++)
506 if((*it)->getNameSpace() == inviterNameSpace)
507 alias = (*it)->getAlias();
508
509 if(it != m_contactList.end())
510 return;
511
512 m_invitationDialog->setInvitation(alias, invitation, certificate);
513 emit newInvitationReady();
514}
515
516void
517ContactPanel::collectEndorsement()
518{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800519 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu46948282013-11-06 18:43:31 -0800520 m_collectStatus->assign(m_contactList.size(), false);
521
Yingdi Yu76dd8002013-12-24 11:16:32 +0800522 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800523 int count = 0;
524 for(; it != m_contactList.end(); it++, count++)
525 {
526 Name interestName = (*it)->getNameSpace();
527 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800528 Interest interest(interestName);
529 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
530 interest.setInterestLifetimeMilliseconds(1000);
Yingdi Yu46948282013-11-06 18:43:31 -0800531
Yingdi Yu76dd8002013-12-24 11:16:32 +0800532 OnVerified onVerified = boost::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
533 OnVerifyFailed onVerifyFailed = boost::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
534 TimeoutNotify timeoutNotify = boost::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
535
536 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu46948282013-11-06 18:43:31 -0800537 }
538}
539
540void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800541ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800542{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800543 Data endorseData;
544 endorseData.wireDecode(data->getContent().buf(), data->getContent().size());
545 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu46948282013-11-06 18:43:31 -0800546
547 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
548
549 updateCollectStatus(count);
550}
551
552void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800553ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800554{ updateCollectStatus(count); }
555
556void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800557ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800558{ updateCollectStatus(count); }
559
560void
561ContactPanel::updateCollectStatus(int count)
562{
563 m_collectStatus->at(count) = true;
564 vector<bool>::const_iterator it = m_collectStatus->begin();
565 for(; it != m_collectStatus->end(); it++)
566 if(*it == false)
567 return;
568
569 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
570}
571
572static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
573
574string
575ContactPanel::getRandomString()
576{
577 string randStr;
578 boost::random::random_device rng;
579 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
580 for (int i = 0; i < 10; i ++)
581 {
582 randStr += chars[index_dist(rng)];
583 }
584 return randStr;
585}
586
587void
Yingdi Yu8e135832013-11-09 20:12:31 -0800588ContactPanel::showError(const QString& msg){
589 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
590 exit(1);
591}
592
593void
Yingdi Yue433a502013-11-10 01:34:57 -0800594ContactPanel::showWarning(const QString& msg){
595 QMessageBox::information(this, tr("Chronos"), msg);
596}
597
598void
Yingdi Yu40eca752013-10-10 15:00:58 -0700599ContactPanel::updateSelection(const QItemSelection &selected,
600 const QItemSelection &deselected)
601{
602 QModelIndexList items = selected.indexes();
603 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800604 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700605
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700606 int i = 0;
607 for(; i < m_contactList.size(); i++)
608 {
609 if(alias == m_contactList[i]->getAlias())
610 break;
611 }
612
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800613 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800614 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
615 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
616 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700617
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800618 if(m_currentSelectedContact->isIntroducer())
619 {
620 ui->isIntroducer->setChecked(true);
621 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800622 ui->deleteScope->setEnabled(true);
623 ui->trustScopeList->setEnabled(true);
624
625 string filter("contact_namespace = '");
626 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
627
628 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
629 m_trustScopeModel->setTable("TrustScope");
630 m_trustScopeModel->setFilter(filter.c_str());
631 m_trustScopeModel->select();
632 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
633 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
634 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
635
Yingdi Yub2e747d2013-11-05 23:06:43 -0800636 ui->trustScopeList->setModel(m_trustScopeModel);
637 ui->trustScopeList->setColumnHidden(0, true);
638 ui->trustScopeList->setColumnHidden(1, true);
639 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800640 }
641 else
642 {
643 ui->isIntroducer->setChecked(false);
644 ui->addScope->setEnabled(false);
645 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800646
647 string filter("contact_namespace = '");
648 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
649
650 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
651 m_trustScopeModel->setTable("TrustScope");
652 m_trustScopeModel->setFilter(filter.c_str());
653 m_trustScopeModel->select();
654 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
655 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
656 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
657
658 ui->trustScopeList->setModel(m_trustScopeModel);
659 ui->trustScopeList->setColumnHidden(0, true);
660 ui->trustScopeList->setColumnHidden(1, true);
661 ui->trustScopeList->show();
662
663 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800664 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800665
666 string filter("profile_identity = '");
667 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
668
669 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
670 m_endorseDataModel->setTable("ContactProfile");
671 m_endorseDataModel->setFilter(filter.c_str());
672 m_endorseDataModel->select();
673
674 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
675 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
676 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
677 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
678
679 ui->endorseList->setModel(m_endorseDataModel);
680 ui->endorseList->setColumnHidden(0, true);
681 ui->endorseList->resizeColumnToContents(1);
682 ui->endorseList->resizeColumnToContents(2);
683 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
684 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700685}
Yingdi Yu847aa862013-10-09 16:35:53 -0700686
Yingdi Yu3b318c12013-10-15 17:54:00 -0700687void
Yingdi Yu46948282013-11-06 18:43:31 -0800688ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700689{
Yingdi Yu711f5892013-11-09 22:18:26 -0800690 // _LOG_DEBUG(identity.toStdString());
Yingdi Yue433a502013-11-10 01:34:57 -0800691 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800692 Name defaultCertName = m_identityManager->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -0800693 if(defaultCertName.size() == 0)
Yingdi Yue433a502013-11-10 01:34:57 -0800694 {
695 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
696 return;
697 }
698 m_defaultIdentity = defaultIdentity;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800699 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800700 m_nickName = nickName.toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800701 m_face->removeRegisteredPrefix(m_invitationListenerId);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800702 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800703 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800704 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700705}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700706
707void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700708ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700709{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700710
Yingdi Yu0269c872013-10-16 13:16:49 -0700711void
712ContactPanel::openAddContactPanel()
713{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700714
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700715void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800716ContactPanel::openBrowseContactDialog()
717{
718 m_browseContactDialog->show();
719 emit refreshCertDirectory();
720}
721
722void
723ContactPanel::removeContactButton()
724{
725 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
726 QModelIndexList selectedList = selectionModel->selectedIndexes();
727 QModelIndexList::iterator it = selectedList.begin();
728 for(; it != selectedList.end(); it++)
729 {
730 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800731 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800732 for(; contactIt != m_contactList.end(); contactIt++)
733 {
734 if((*contactIt)->getAlias() == alias)
735 {
736 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
737 m_contactList.erase(contactIt);
738 break;
739 }
740 }
741 }
742 refreshContactList();
743}
744
745void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700746ContactPanel::openInvitationDialog()
747{ m_invitationDialog->show(); }
748
749void
Yingdi Yu72232692013-11-12 17:50:21 -0800750ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
751{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800752 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
753 if(contact != CHRONOCHAT_NULL_CONTACTITEM_PTR)
754 m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu72232692013-11-12 17:50:21 -0800755}
756
757void
758ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800759{ m_policyManager->removeTrustAnchor(keyName); }
Yingdi Yu72232692013-11-12 17:50:21 -0800760
761void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700762ContactPanel::refreshContactList()
763{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800764 m_contactList.clear();
765 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700766 QStringList contactNameList;
767 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800768 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700769
770 m_contactListModel->setStringList(contactNameList);
771}
772
773void
774ContactPanel::showContextMenu(const QPoint& pos)
775{
776 QMenu menu(ui->ContactList);
777 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700778 connect(m_menuInvite, SIGNAL(triggered()),
779 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800780 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700781 menu.addAction(m_menuAlias);
782 connect(m_menuAlias, SIGNAL(triggered()),
783 this, SLOT(openSetAliasDialog()));
784 menu.exec(ui->ContactList->mapToGlobal(pos));
785
786}
787
788void
789ContactPanel::openSetAliasDialog()
790{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800791 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700792 m_setAliasDialog->show();
793}
794
Yingdi Yu595aadc2013-10-21 15:01:40 -0700795void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700796ContactPanel::openSettingDialog()
797{
Yingdi Yu46948282013-11-06 18:43:31 -0800798 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700799 m_settingDialog->show();
800}
801
802void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700803ContactPanel::openStartChatDialog()
804{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700805 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700806 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700807
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800808 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700809 m_startChatDialog->show();
810}
811
Yingdi Yu7989eb22013-10-31 17:38:22 -0700812// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700813void
814ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
815{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800816 Name chatroomName(chatroom.toStdString());
Yingdi Yu72781e52013-11-06 23:00:21 -0800817
818 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800819 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu72781e52013-11-06 23:00:21 -0800820
Yingdi Yue6476cd2013-11-06 18:51:19 -0800821 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700822 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800823
824 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
825 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800826 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
827 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800828 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
829 this, SLOT(showWarning(const QString&)));
Yingdi Yu46948282013-11-06 18:43:31 -0800830
Yingdi Yue6476cd2013-11-06 18:51:19 -0800831 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700832 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700833
834 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700835}
836
Yingdi Yu7989eb22013-10-31 17:38:22 -0700837// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700838void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700839ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800840 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700841{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800842 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu72781e52013-11-06 23:00:21 -0800843
Yingdi Yu7989eb22013-10-31 17:38:22 -0700844 Name chatroomName("/ndn/broadcast/chronos");
845 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800846
Yingdi Yue6476cd2013-11-06 18:51:19 -0800847 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800848
Yingdi Yu46948282013-11-06 18:43:31 -0800849 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
850 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800851 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
852 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800853 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
854 this, SLOT(showWarning(const QString&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700855
Yingdi Yue6476cd2013-11-06 18:51:19 -0800856 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800857 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700858
Yingdi Yu7989eb22013-10-31 17:38:22 -0700859 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
860
861 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
862
863 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700864}
865
866void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700867ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800868 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700869{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800870 Data data(invitation.getInterestName());
871 string content = m_localPrefix.toUri();
872 data.setContent((const uint8_t *)&content[0], content.size());
873 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
874
875 Name certificateName;
876 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
877
878 if(inferredIdentity.getComponentCount() == 0)
879 certificateName = m_identityManager->getDefaultCertificateName();
880 else
881 certificateName = m_identityManager->getDefaultCertificateNameForIdentity(inferredIdentity);
882 m_identityManager->signByCertificate(data, certificateName);
883
884 m_transport->send(*data.wireEncode());
885
Yingdi Yu7989eb22013-10-31 17:38:22 -0700886 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700887}
888
889void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700890ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700891{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800892 Data data(invitation.getInterestName());
893 string content("nack");
894 data.setContent((const uint8_t *)&content[0], content.size());
895 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
896
897 Name certificateName;
898 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
899 if(inferredIdentity.getComponentCount() == 0)
900 certificateName = m_identityManager->getDefaultCertificateName();
901 else
902 certificateName = m_identityManager->getDefaultCertificateNameForIdentity(inferredIdentity);
903 m_identityManager->signByCertificate(data, certificateName);
904
905 m_transport->send(*data.wireEncode());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700906}
907
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800908void
909ContactPanel::isIntroducerChanged(int state)
910{
911 if(state == Qt::Checked)
912 {
913 ui->addScope->setEnabled(true);
914 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800915 ui->trustScopeList->setEnabled(true);
916
917 string filter("contact_namespace = '");
918 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800919
920 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
921 m_trustScopeModel->setTable("TrustScope");
922 m_trustScopeModel->setFilter(filter.c_str());
923 m_trustScopeModel->select();
924 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
925 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
926 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800927
928 ui->trustScopeList->setModel(m_trustScopeModel);
929 ui->trustScopeList->setColumnHidden(0, true);
930 ui->trustScopeList->setColumnHidden(1, true);
931 ui->trustScopeList->show();
932
933 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800934 }
935 else
936 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800937 ui->addScope->setEnabled(false);
938 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800939
940 string filter("contact_namespace = '");
941 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
942
943 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
944 m_trustScopeModel->setTable("TrustScope");
945 m_trustScopeModel->setFilter(filter.c_str());
946 m_trustScopeModel->select();
947 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
948 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
949 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
950
951 ui->trustScopeList->setModel(m_trustScopeModel);
952 ui->trustScopeList->setColumnHidden(0, true);
953 ui->trustScopeList->setColumnHidden(1, true);
954 ui->trustScopeList->show();
955
956 ui->trustScopeList->setEnabled(false);
957
958 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800959 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800960 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
961}
962
963void
964ContactPanel::addScopeClicked()
965{
966 int rowCount = m_trustScopeModel->rowCount();
967 QSqlRecord record;
968 QSqlField identityField("contact_namespace", QVariant::String);
969 record.append(identityField);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800970 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800971 m_trustScopeModel->insertRow(rowCount);
972 m_trustScopeModel->setRecord(rowCount, record);
973}
974
975void
976ContactPanel::deleteScopeClicked()
977{
978 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
979 QModelIndexList indexList = selectionModel->selectedIndexes();
980
981 int i = indexList.size() - 1;
982 for(; i >= 0; i--)
983 m_trustScopeModel->removeRow(indexList[i].row());
984
985 m_trustScopeModel->submitAll();
986}
987
988void
989ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -0800990{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800991
992void
993ContactPanel::endorseButtonClicked()
994{
995 m_endorseDataModel->submitAll();
996 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800997}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700998
Yingdi Yu46948282013-11-06 18:43:31 -0800999void
1000ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
1001{
1002 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -08001003
1004 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -08001005 if(it != m_chatDialogs.end())
1006 {
Yingdi Yu46948282013-11-06 18:43:31 -08001007 deletedChat = it->second;
1008 m_chatDialogs.erase(it);
1009 }
Yingdi Yu83eae842013-11-06 22:07:38 -08001010 if (deletedChat != NULL)
1011 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -08001012}
1013
Yingdi Yu847aa862013-10-09 16:35:53 -07001014#if WAF
1015#include "contactpanel.moc"
1016#include "contactpanel.cpp.moc"
1017#endif