blob: 0a9ffcd0d6340e1c8927d1229a5bf22eeda0d30c [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 Yuf8f572d2014-01-13 11:19:47 -080024#include <ndn-cpp/security/verifier.hpp>
25#include <ndn-cpp/security/signature/signature-sha256-with-rsa.hpp>
Yingdi Yu3b318c12013-10-15 17:54:00 -070026#include <boost/filesystem.hpp>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070027#include <boost/random/random_device.hpp>
28#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu7989eb22013-10-31 17:38:22 -070029#include "panel-policy-manager.h"
Yingdi Yu76dd8002013-12-24 11:16:32 +080030#include "null-ptrs.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070031#include "logging.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070032#endif
33
34namespace fs = boost::filesystem;
35using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080036using namespace ndn::ptr_lib;
Yingdi Yu978b3ae2013-10-23 11:50:51 -070037
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070038using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070039
40INIT_LOGGER("ContactPanel");
41
Yingdi Yu76dd8002013-12-24 11:16:32 +080042Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu7989eb22013-10-31 17:38:22 -070043Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070044
Yingdi Yu8e135832013-11-09 20:12:31 -080045ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080046 : QDialog(parent)
47 , ui(new Ui::ContactPanel)
Yingdi Yu8e135832013-11-09 20:12:31 -080048 , m_warningDialog(new WarningDialog)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080049 , m_contactListModel(new QStringListModel)
50 , m_startChatDialog(new StartChatDialog)
51 , m_invitationDialog(new InvitationDialog)
52 , m_settingDialog(new SettingDialog)
Yingdi Yu76dd8002013-12-24 11:16:32 +080053 , m_policyManager(new PanelPolicyManager())
Yingdi Yu847aa862013-10-09 16:35:53 -070054{
Yingdi Yu76dd8002013-12-24 11:16:32 +080055 qRegisterMetaType<IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070056 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu6eabbd72013-12-27 08:44:12 +080057
58 startFace();
59
Yingdi Yu46948282013-11-06 18:43:31 -080060 createAction();
61
Yingdi Yuf8f572d2014-01-13 11:19:47 -080062 m_keyChain = make_shared<KeyChain>();
Yingdi Yu76dd8002013-12-24 11:16:32 +080063
Yingdi Yuf8f572d2014-01-13 11:19:47 -080064 m_contactManager = make_shared<ContactManager>(m_keyChain, m_face);
Yingdi Yue35bdb82013-11-07 11:32:40 -080065
Yingdi Yu8e135832013-11-09 20:12:31 -080066 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
67 this, SLOT(showError(const QString&)));
Yingdi Yu8e135832013-11-09 20:12:31 -080068
Yingdi Yue35bdb82013-11-07 11:32:40 -080069 openDB();
Yingdi Yu46948282013-11-06 18:43:31 -080070
Yingdi Yueda39aa2013-10-23 23:07:29 -070071 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070072
Yingdi Yu76dd8002013-12-24 11:16:32 +080073 loadTrustAnchor();
Yingdi Yu46948282013-11-06 18:43:31 -080074
Yingdi Yuf8f572d2014-01-13 11:19:47 -080075 m_defaultIdentity = m_keyChain->getDefaultIdentity();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080076 if(m_defaultIdentity.size() == 0)
77 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yuf8f572d2014-01-13 11:19:47 -080078 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -080079 if(defaultCertName.size() == 0)
Yingdi Yuf0b9ea92013-11-10 10:54:16 -080080 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yub5a0fd32013-11-10 16:26:52 -080081
82
83 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080084 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080085 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu711f5892013-11-09 22:18:26 -080086
Yingdi Yu46948282013-11-06 18:43:31 -080087
Yingdi Yue35bdb82013-11-07 11:32:40 -080088 m_profileEditor = new ProfileEditor(m_contactManager);
89 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
90
Yingdi Yu8e135832013-11-09 20:12:31 -080091 m_addContactPanel = new AddContactPanel(m_contactManager);
92 m_browseContactDialog = new BrowseContactDialog(m_contactManager);
93 m_setAliasDialog = new SetAliasDialog(m_contactManager);
Yingdi Yue35bdb82013-11-07 11:32:40 -080094
95 ui->setupUi(this);
96
Yingdi Yu8e135832013-11-09 20:12:31 -080097
Yingdi Yua66183f2013-11-10 10:32:18 -080098 m_localPrefix = Name("/private/local");
Yingdi Yueda39aa2013-10-23 23:07:29 -070099 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700100
Yingdi Yueda39aa2013-10-23 23:07:29 -0700101 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -0800102
Yingdi Yub2e747d2013-11-05 23:06:43 -0800103 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700104
105 ui->ContactList->setModel(m_contactListModel);
106
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700107
Yingdi Yu46948282013-11-06 18:43:31 -0800108 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700109 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -0700110
Yingdi Yueda39aa2013-10-23 23:07:29 -0700111 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
112 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -0700113
Yingdi Yueda39aa2013-10-23 23:07:29 -0700114 connect(ui->EditProfileButton, SIGNAL(clicked()),
115 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -0700116
Yingdi Yu711f5892013-11-09 22:18:26 -0800117 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yue433a502013-11-10 01:34:57 -0800118 this, SLOT(showWarning(const QString&)));
Yingdi Yu711f5892013-11-09 22:18:26 -0800119
Yingdi Yueda39aa2013-10-23 23:07:29 -0700120 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yuae8217c2013-11-09 00:03:26 -0800121 this, SLOT(openBrowseContactDialog()));
122
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800123 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
124 this, SLOT(openAddContactPanel()));
125
Yingdi Yuae8217c2013-11-09 00:03:26 -0800126 connect(this, SIGNAL(refreshCertDirectory()),
127 m_browseContactDialog, SLOT(refreshList()));
128
129 connect(ui->DeleteContactButton, SIGNAL(clicked()),
130 this, SLOT(removeContactButton()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700131
Yingdi Yueda39aa2013-10-23 23:07:29 -0700132 connect(ui->settingButton, SIGNAL(clicked()),
133 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700134
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800135 connect(m_addContactPanel, SIGNAL(newContactAdded()),
136 this, SLOT(refreshContactList()));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800137 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700138 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700139 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
140 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700141
Yingdi Yueda39aa2013-10-23 23:07:29 -0700142 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
143 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700144
Yingdi Yu76dd8002013-12-24 11:16:32 +0800145 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::IdentityCertificate&)),
146 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::IdentityCertificate&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700147 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
148 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu72232692013-11-12 17:50:21 -0800149
150 connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)),
151 this, SLOT(addContactIntoPanelPolicy(const ndn::Name&)));
152 connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)),
153 this, SLOT(removeContactFromPanelPolicy(const ndn::Name&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700154
Yingdi Yu46948282013-11-06 18:43:31 -0800155 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
156 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700157
158 connect(this, SIGNAL(newInvitationReady()),
159 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700160
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800161 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
162 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800163
164 connect(ui->addScope, SIGNAL(clicked()),
165 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800166 connect(ui->deleteScope, SIGNAL(clicked()),
167 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800168 connect(ui->saveButton, SIGNAL(clicked()),
169 this, SLOT(saveScopeClicked()));
170
171 connect(ui->endorseButton, SIGNAL(clicked()),
172 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700173}
174
175ContactPanel::~ContactPanel()
176{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700177 delete ui;
178 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800179 delete m_startChatDialog;
180 delete m_invitationDialog;
181 delete m_settingDialog;
182
Yingdi Yueda39aa2013-10-23 23:07:29 -0700183 delete m_profileEditor;
184 delete m_addContactPanel;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800185 delete m_browseContactDialog;
Yingdi Yu46948282013-11-06 18:43:31 -0800186 delete m_setAliasDialog;
187
Yingdi Yub2e747d2013-11-05 23:06:43 -0800188 delete m_trustScopeModel;
189 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800190 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700191
Yingdi Yueda39aa2013-10-23 23:07:29 -0700192 delete m_menuInvite;
Yingdi Yu46948282013-11-06 18:43:31 -0800193 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700194
195 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
196 for(; it != m_chatDialogs.end(); it++)
197 delete it->second;
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800198
199 shutdownFace();
200}
201
202void
203ContactPanel::startFace()
204{
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800205 m_face = make_shared<Face>();
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800206
207 connectToDaemon();
208
209 m_running = true;
210 m_thread = boost::thread (&ContactPanel::eventLoop, this);
211}
212
213void
214ContactPanel::shutdownFace()
215{
216 {
217 boost::unique_lock<boost::recursive_mutex> lock(m_mutex);
218 m_running = false;
219 }
220
221 m_thread.join();
222 m_face->shutdown();
223}
224
225void
226ContactPanel::eventLoop()
227{
228 while (m_running)
229 {
230 try{
231 m_face->processEvents();
232 usleep(100);
233 }catch(std::exception& e){
234 _LOG_DEBUG(" " << e.what() );
235 }
236 }
Yingdi Yu847aa862013-10-09 16:35:53 -0700237}
238
Yingdi Yu40eca752013-10-10 15:00:58 -0700239void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800240ContactPanel::connectToDaemon()
241{
242 //Hack! transport does not connect to daemon unless an interest is expressed.
243 Name name("/ndn");
244 ndn::Interest interest(name);
245 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800246 func_lib::bind(&ContactPanel::onConnectionData, this, _1, _2),
247 func_lib::bind(&ContactPanel::onConnectionDataTimeout, this, _1));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800248}
249
250void
251ContactPanel::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
252 const shared_ptr<Data>& data)
253{ _LOG_DEBUG("onConnectionData"); }
254
255void
256ContactPanel::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
257{ _LOG_DEBUG("onConnectionDataTimeout"); }
258
259void
Yingdi Yu46948282013-11-06 18:43:31 -0800260ContactPanel::createAction()
261{
262 m_menuInvite = new QAction("&Chat", this);
263 m_menuAlias = new QAction("&Set Alias", this);
264}
265
266void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700267ContactPanel::openDB()
268{
269 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
270 QString path = (QDir::home().path());
271 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
272 db.setDatabaseName(path);
273 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800274 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800275
276 m_trustScopeModel = new QSqlTableModel;
277 m_endorseDataModel = new QSqlTableModel;
278 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700279}
280
281void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800282ContactPanel::loadTrustAnchor()
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700283{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800284 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700285 for(; it != m_contactList.end(); it++)
Yingdi Yue35bdb82013-11-07 11:32:40 -0800286 {
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800287 _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800288 m_policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800289 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700290}
291
292void
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800293ContactPanel::setLocalPrefix(int retry)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700294{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800295 Name interestName("/local/ndn/prefix");
296 Interest interest(interestName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700297
Yingdi Yu76dd8002013-12-24 11:16:32 +0800298 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800299 func_lib::bind(&ContactPanel::onLocalPrefix, this, _1, _2),
300 func_lib::bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800301
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700302}
303
304void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800305ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest,
306 const shared_ptr<Data>& data)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700307{
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800308 string originPrefix((const char*)data->getContent().value(), data->getContent().value_size());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700309 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
310 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700311 m_localPrefix = Name(prefix);
Yingdi Yue433a502013-11-10 01:34:57 -0800312
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700313}
314
315void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800316ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest,
317 int retry)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700318{
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800319 if(retry > 0)
Yingdi Yua66183f2013-11-10 10:32:18 -0800320 {
321 setLocalPrefix(retry - 1);
322 return;
323 }
324 else{
325 m_localPrefix = Name("/private/local");
326 }
Yingdi Yu7989eb22013-10-31 17:38:22 -0700327}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700328
329void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700330ContactPanel::setInvitationListener()
331{
Yingdi Yu46948282013-11-06 18:43:31 -0800332 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
333 m_inviteListenPrefix.append(m_defaultIdentity);
334 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800335 m_invitationListenerId = m_face->setInterestFilter(m_inviteListenPrefix,
336 func_lib::bind(&ContactPanel::onInvitation, this, _1, _2, _3, _4),
337 func_lib::bind(&ContactPanel::onInvitationRegisterFailed, this, _1));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700338}
339
340void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800341ContactPanel::sendInterest(const Interest& interest,
342 const OnVerified& onVerified,
343 const OnVerifyFailed& onVerifyFailed,
344 const TimeoutNotify& timeoutNotify,
345 int retry /* = 1 */,
346 int stepCount /* = 0 */)
347{
348 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800349 func_lib::bind(&ContactPanel::onTargetData,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800350 this,
351 _1,
352 _2,
353 stepCount,
354 onVerified,
355 onVerifyFailed,
356 timeoutNotify),
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800357 func_lib::bind(&ContactPanel::onTargetTimeout,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800358 this,
359 _1,
360 retry,
361 stepCount,
362 onVerified,
363 onVerifyFailed,
364 timeoutNotify));
365}
366
367void
368ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest,
369 const shared_ptr<Data>& data,
370 int stepCount,
371 const OnVerified& onVerified,
372 const OnVerifyFailed& onVerifyFailed,
373 const TimeoutNotify& timeoutNotify)
374{
375 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
376
377 if (nextStep)
378 m_face->expressInterest
379 (*nextStep->interest_,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800380 func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
381 func_lib::bind(&ContactPanel::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800382
383}
384
385void
386ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
387 int retry,
388 int stepCount,
389 const OnVerified& onVerified,
390 const OnVerifyFailed& onVerifyFailed,
391 const TimeoutNotify& timeoutNotify)
392{
393 if(retry > 0)
394 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
395 else
396 {
397 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
398 timeoutNotify();
399 }
400}
401
402void
403ContactPanel::onCertData(const shared_ptr<const ndn::Interest>& interest,
404 const shared_ptr<Data>& cert,
405 shared_ptr<ValidationRequest> previousStep)
406{
407 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
408 previousStep->stepCount_,
409 previousStep->onVerified_,
410 previousStep->onVerifyFailed_);
411
412 if (nextStep)
413 m_face->expressInterest
414 (*nextStep->interest_,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800415 func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
416 func_lib::bind(&ContactPanel::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800417}
418
419void
420ContactPanel::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
421 const OnVerifyFailed& onVerifyFailed,
422 const shared_ptr<Data>& data,
423 shared_ptr<ValidationRequest> nextStep)
424{
425 if(nextStep->retry_ > 0)
426 m_face->expressInterest(*interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800427 func_lib::bind(&ContactPanel::onCertData,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800428 this,
429 _1,
430 _2,
431 nextStep),
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800432 func_lib::bind(&ContactPanel::onCertTimeout,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800433 this,
434 _1,
435 onVerifyFailed,
436 data,
437 nextStep));
438 else
439 onVerifyFailed(data);
440}
441
442void
443ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix)
444{
445 showError(QString::fromStdString("Cannot register invitation listening prefix"));
446}
447
448void
449ContactPanel::onInvitation(const shared_ptr<const Name>& prefix,
450 const shared_ptr<const Interest>& interest,
451 Transport& transport,
452 uint64_t registeredPrefixId)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700453{
Yingdi Yu46948282013-11-06 18:43:31 -0800454 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800455
Yingdi Yue35bdb82013-11-07 11:32:40 -0800456
Yingdi Yu76dd8002013-12-24 11:16:32 +0800457 shared_ptr<ChronosInvitation> invitation;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800458 try{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800459 invitation = make_shared<ChronosInvitation>(interest->getName());
460 }catch(std::exception& e){
Yingdi Yue35bdb82013-11-07 11:32:40 -0800461 _LOG_ERROR("Exception: " << e.what());
462 return;
463 }
464
Yingdi Yu46948282013-11-06 18:43:31 -0800465 Name chatroomName("/ndn/broadcast/chronos");
466 chatroomName.append(invitation->getChatroom());
467 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
468 if(it != m_chatDialogs.end())
469 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800470 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu46948282013-11-06 18:43:31 -0800471 return;
472 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700473
Yingdi Yu76dd8002013-12-24 11:16:32 +0800474 shared_ptr<PublicKey> keyPtr = m_policyManager->getTrustedKey(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800475
476 SignatureSha256WithRsa invitationSig;
477 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
478 invitationSig.setValue(sigBlock);
479 if(CHRONOCHAT_NULL_PUBLICKEY_PTR != keyPtr && Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, *keyPtr))
Yingdi Yuaf305d72013-11-10 11:54:02 -0800480 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800481 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800482 // 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 -0800483 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800484 bool findCert = false;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800485 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800486 for(; it != m_contactList.end(); it++)
487 {
488 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
489 {
490 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
491 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
492 findCert = true;
493 break;
494 }
495 }
496 if(findCert == false)
497 {
498 _LOG_ERROR("No SelfEndorseCertificate found!");
499 return;
500 }
Yingdi Yuaf305d72013-11-10 11:54:02 -0800501 certificate->setPublicKeyInfo(*keyPtr);
502 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
503 return;
504 }
505
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800506 _LOG_DEBUG("Cannot find the inviter's key in trust anchors");
507
Yingdi Yu76dd8002013-12-24 11:16:32 +0800508 Interest newInterest(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800509 OnVerified onVerified = func_lib::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
510 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
511 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800512
513 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700514}
515
516void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800517ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
518 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu46948282013-11-06 18:43:31 -0800519{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800520 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800521
522 SignatureSha256WithRsa invitationSig;
523 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
524 invitationSig.setValue(sigBlock);
Yingdi Yu46948282013-11-06 18:43:31 -0800525
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800526 if(Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, certificate->getPublicKeyInfo()))
Yingdi Yu46948282013-11-06 18:43:31 -0800527 {
528 Name keyName = certificate->getPublicKeyName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800529 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu46948282013-11-06 18:43:31 -0800530 popChatInvitation(invitation, inviterNameSpace, certificate);
531 }
532}
533
534void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800535ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
536{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800537
538void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800539ContactPanel::onInvitationCertTimeoutNotify()
540{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800541
542void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800543ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu46948282013-11-06 18:43:31 -0800544 const Name& inviterNameSpace,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800545 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu46948282013-11-06 18:43:31 -0800546{
547 string alias;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800548 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800549 for(; it != m_contactList.end(); it++)
550 if((*it)->getNameSpace() == inviterNameSpace)
551 alias = (*it)->getAlias();
552
553 if(it != m_contactList.end())
554 return;
555
556 m_invitationDialog->setInvitation(alias, invitation, certificate);
557 emit newInvitationReady();
558}
559
560void
561ContactPanel::collectEndorsement()
562{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800563 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu46948282013-11-06 18:43:31 -0800564 m_collectStatus->assign(m_contactList.size(), false);
565
Yingdi Yu76dd8002013-12-24 11:16:32 +0800566 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800567 int count = 0;
568 for(; it != m_contactList.end(); it++, count++)
569 {
570 Name interestName = (*it)->getNameSpace();
571 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800572 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800573 interest.setInterestLifetime(1000);
Yingdi Yu46948282013-11-06 18:43:31 -0800574
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800575 OnVerified onVerified = func_lib::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
576 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
577 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800578
579 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu46948282013-11-06 18:43:31 -0800580 }
581}
582
583void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800584ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800585{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800586 Data endorseData;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800587 endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800588 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu46948282013-11-06 18:43:31 -0800589
590 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
591
592 updateCollectStatus(count);
593}
594
595void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800596ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800597{ updateCollectStatus(count); }
598
599void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800600ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800601{ updateCollectStatus(count); }
602
603void
604ContactPanel::updateCollectStatus(int count)
605{
606 m_collectStatus->at(count) = true;
607 vector<bool>::const_iterator it = m_collectStatus->begin();
608 for(; it != m_collectStatus->end(); it++)
609 if(*it == false)
610 return;
611
612 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
613}
614
615static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
616
617string
618ContactPanel::getRandomString()
619{
620 string randStr;
621 boost::random::random_device rng;
622 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
623 for (int i = 0; i < 10; i ++)
624 {
625 randStr += chars[index_dist(rng)];
626 }
627 return randStr;
628}
629
630void
Yingdi Yu8e135832013-11-09 20:12:31 -0800631ContactPanel::showError(const QString& msg){
632 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
633 exit(1);
634}
635
636void
Yingdi Yue433a502013-11-10 01:34:57 -0800637ContactPanel::showWarning(const QString& msg){
638 QMessageBox::information(this, tr("Chronos"), msg);
639}
640
641void
Yingdi Yu40eca752013-10-10 15:00:58 -0700642ContactPanel::updateSelection(const QItemSelection &selected,
643 const QItemSelection &deselected)
644{
645 QModelIndexList items = selected.indexes();
646 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800647 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700648
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700649 int i = 0;
650 for(; i < m_contactList.size(); i++)
651 {
652 if(alias == m_contactList[i]->getAlias())
653 break;
654 }
655
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800656 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800657 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
658 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
659 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700660
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800661 if(m_currentSelectedContact->isIntroducer())
662 {
663 ui->isIntroducer->setChecked(true);
664 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800665 ui->deleteScope->setEnabled(true);
666 ui->trustScopeList->setEnabled(true);
667
668 string filter("contact_namespace = '");
669 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
670
671 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
672 m_trustScopeModel->setTable("TrustScope");
673 m_trustScopeModel->setFilter(filter.c_str());
674 m_trustScopeModel->select();
675 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
676 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
677 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
678
Yingdi Yub2e747d2013-11-05 23:06:43 -0800679 ui->trustScopeList->setModel(m_trustScopeModel);
680 ui->trustScopeList->setColumnHidden(0, true);
681 ui->trustScopeList->setColumnHidden(1, true);
682 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800683 }
684 else
685 {
686 ui->isIntroducer->setChecked(false);
687 ui->addScope->setEnabled(false);
688 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800689
690 string filter("contact_namespace = '");
691 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
692
693 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
694 m_trustScopeModel->setTable("TrustScope");
695 m_trustScopeModel->setFilter(filter.c_str());
696 m_trustScopeModel->select();
697 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
698 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
699 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
700
701 ui->trustScopeList->setModel(m_trustScopeModel);
702 ui->trustScopeList->setColumnHidden(0, true);
703 ui->trustScopeList->setColumnHidden(1, true);
704 ui->trustScopeList->show();
705
706 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800707 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800708
709 string filter("profile_identity = '");
710 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
711
712 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
713 m_endorseDataModel->setTable("ContactProfile");
714 m_endorseDataModel->setFilter(filter.c_str());
715 m_endorseDataModel->select();
716
717 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
718 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
719 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
720 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
721
722 ui->endorseList->setModel(m_endorseDataModel);
723 ui->endorseList->setColumnHidden(0, true);
724 ui->endorseList->resizeColumnToContents(1);
725 ui->endorseList->resizeColumnToContents(2);
726 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
727 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700728}
Yingdi Yu847aa862013-10-09 16:35:53 -0700729
Yingdi Yu3b318c12013-10-15 17:54:00 -0700730void
Yingdi Yu46948282013-11-06 18:43:31 -0800731ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700732{
Yingdi Yu711f5892013-11-09 22:18:26 -0800733 // _LOG_DEBUG(identity.toStdString());
Yingdi Yue433a502013-11-10 01:34:57 -0800734 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800735 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -0800736 if(defaultCertName.size() == 0)
Yingdi Yue433a502013-11-10 01:34:57 -0800737 {
738 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
739 return;
740 }
741 m_defaultIdentity = defaultIdentity;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800742 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800743 m_nickName = nickName.toStdString();
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800744 m_face->unsetInterestFilter(m_invitationListenerId);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800745 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800746 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800747 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700748}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700749
750void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700751ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700752{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700753
Yingdi Yu0269c872013-10-16 13:16:49 -0700754void
755ContactPanel::openAddContactPanel()
756{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700757
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700758void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800759ContactPanel::openBrowseContactDialog()
760{
761 m_browseContactDialog->show();
762 emit refreshCertDirectory();
763}
764
765void
766ContactPanel::removeContactButton()
767{
768 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
769 QModelIndexList selectedList = selectionModel->selectedIndexes();
770 QModelIndexList::iterator it = selectedList.begin();
771 for(; it != selectedList.end(); it++)
772 {
773 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800774 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800775 for(; contactIt != m_contactList.end(); contactIt++)
776 {
777 if((*contactIt)->getAlias() == alias)
778 {
779 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
780 m_contactList.erase(contactIt);
781 break;
782 }
783 }
784 }
785 refreshContactList();
786}
787
788void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700789ContactPanel::openInvitationDialog()
790{ m_invitationDialog->show(); }
791
792void
Yingdi Yu72232692013-11-12 17:50:21 -0800793ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
794{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800795 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
796 if(contact != CHRONOCHAT_NULL_CONTACTITEM_PTR)
797 m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu72232692013-11-12 17:50:21 -0800798}
799
800void
801ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800802{ m_policyManager->removeTrustAnchor(keyName); }
Yingdi Yu72232692013-11-12 17:50:21 -0800803
804void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700805ContactPanel::refreshContactList()
806{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800807 m_contactList.clear();
808 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700809 QStringList contactNameList;
810 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800811 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700812
813 m_contactListModel->setStringList(contactNameList);
814}
815
816void
817ContactPanel::showContextMenu(const QPoint& pos)
818{
819 QMenu menu(ui->ContactList);
820 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700821 connect(m_menuInvite, SIGNAL(triggered()),
822 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800823 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700824 menu.addAction(m_menuAlias);
825 connect(m_menuAlias, SIGNAL(triggered()),
826 this, SLOT(openSetAliasDialog()));
827 menu.exec(ui->ContactList->mapToGlobal(pos));
828
829}
830
831void
832ContactPanel::openSetAliasDialog()
833{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800834 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700835 m_setAliasDialog->show();
836}
837
Yingdi Yu595aadc2013-10-21 15:01:40 -0700838void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700839ContactPanel::openSettingDialog()
840{
Yingdi Yu46948282013-11-06 18:43:31 -0800841 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700842 m_settingDialog->show();
843}
844
845void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700846ContactPanel::openStartChatDialog()
847{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700848 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700849 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700850
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800851 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700852 m_startChatDialog->show();
853}
854
Yingdi Yu7989eb22013-10-31 17:38:22 -0700855// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700856void
857ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
858{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800859 Name chatroomName(chatroom.toStdString());
Yingdi Yu72781e52013-11-06 23:00:21 -0800860
861 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800862 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu72781e52013-11-06 23:00:21 -0800863
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800864 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700865 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800866
867 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
868 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800869 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
870 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800871 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
872 this, SLOT(showWarning(const QString&)));
Yingdi Yu46948282013-11-06 18:43:31 -0800873
Yingdi Yue6476cd2013-11-06 18:51:19 -0800874 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700875 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700876
877 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700878}
879
Yingdi Yu7989eb22013-10-31 17:38:22 -0700880// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700881void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700882ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800883 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700884{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800885 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu72781e52013-11-06 23:00:21 -0800886
Yingdi Yu7989eb22013-10-31 17:38:22 -0700887 Name chatroomName("/ndn/broadcast/chronos");
888 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800889
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800890 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800891
Yingdi Yu46948282013-11-06 18:43:31 -0800892 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
893 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800894 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
895 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800896 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
897 this, SLOT(showWarning(const QString&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700898
Yingdi Yue6476cd2013-11-06 18:51:19 -0800899 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800900 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700901
Yingdi Yu7989eb22013-10-31 17:38:22 -0700902 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
903
904 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
905
906 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700907}
908
909void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700910ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800911 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700912{
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800913 Name dataName = invitation.getInterestName();
914 time_t nowSeconds = time(NULL);
915 struct tm current = *gmtime(&nowSeconds);
916 MillisecondsSince1970 version = timegm(&current) * 1000.0;
917 dataName.appendVersion(version);
918 Data data(dataName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800919 string content = m_localPrefix.toUri();
920 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800921
922 Name certificateName;
923 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
924
925 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800926 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800927 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800928 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
929 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800930
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800931 m_face->put(data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800932
Yingdi Yu7989eb22013-10-31 17:38:22 -0700933 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700934}
935
936void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700937ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700938{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800939 Data data(invitation.getInterestName());
940 string content("nack");
941 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800942
943 Name certificateName;
944 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
945 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800946 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800947 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800948 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
949 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800950
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800951 m_face->put(data);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700952}
953
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800954void
955ContactPanel::isIntroducerChanged(int state)
956{
957 if(state == Qt::Checked)
958 {
959 ui->addScope->setEnabled(true);
960 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800961 ui->trustScopeList->setEnabled(true);
962
963 string filter("contact_namespace = '");
964 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800965
966 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
967 m_trustScopeModel->setTable("TrustScope");
968 m_trustScopeModel->setFilter(filter.c_str());
969 m_trustScopeModel->select();
970 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
971 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
972 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800973
974 ui->trustScopeList->setModel(m_trustScopeModel);
975 ui->trustScopeList->setColumnHidden(0, true);
976 ui->trustScopeList->setColumnHidden(1, true);
977 ui->trustScopeList->show();
978
979 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800980 }
981 else
982 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800983 ui->addScope->setEnabled(false);
984 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800985
986 string filter("contact_namespace = '");
987 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
988
989 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
990 m_trustScopeModel->setTable("TrustScope");
991 m_trustScopeModel->setFilter(filter.c_str());
992 m_trustScopeModel->select();
993 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
994 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
995 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
996
997 ui->trustScopeList->setModel(m_trustScopeModel);
998 ui->trustScopeList->setColumnHidden(0, true);
999 ui->trustScopeList->setColumnHidden(1, true);
1000 ui->trustScopeList->show();
1001
1002 ui->trustScopeList->setEnabled(false);
1003
1004 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -08001005 }
Yingdi Yub2e747d2013-11-05 23:06:43 -08001006 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
1007}
1008
1009void
1010ContactPanel::addScopeClicked()
1011{
1012 int rowCount = m_trustScopeModel->rowCount();
1013 QSqlRecord record;
1014 QSqlField identityField("contact_namespace", QVariant::String);
1015 record.append(identityField);
Yingdi Yu76dd8002013-12-24 11:16:32 +08001016 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yub2e747d2013-11-05 23:06:43 -08001017 m_trustScopeModel->insertRow(rowCount);
1018 m_trustScopeModel->setRecord(rowCount, record);
1019}
1020
1021void
1022ContactPanel::deleteScopeClicked()
1023{
1024 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
1025 QModelIndexList indexList = selectionModel->selectedIndexes();
1026
1027 int i = indexList.size() - 1;
1028 for(; i >= 0; i--)
1029 m_trustScopeModel->removeRow(indexList[i].row());
1030
1031 m_trustScopeModel->submitAll();
1032}
1033
1034void
1035ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -08001036{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -08001037
1038void
1039ContactPanel::endorseButtonClicked()
1040{
1041 m_endorseDataModel->submitAll();
1042 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -08001043}
Yingdi Yu595aadc2013-10-21 15:01:40 -07001044
Yingdi Yu46948282013-11-06 18:43:31 -08001045void
1046ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
1047{
1048 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -08001049
1050 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -08001051 if(it != m_chatDialogs.end())
1052 {
Yingdi Yu46948282013-11-06 18:43:31 -08001053 deletedChat = it->second;
1054 m_chatDialogs.erase(it);
1055 }
Yingdi Yu83eae842013-11-06 22:07:38 -08001056 if (deletedChat != NULL)
1057 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -08001058}
1059
Yingdi Yu847aa862013-10-09 16:35:53 -07001060#if WAF
1061#include "contactpanel.moc"
1062#include "contactpanel.cpp.moc"
1063#endif