blob: cde6c552b717fc68935ca9dc4a85501d2963995b [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>
Yingdi Yu93adb1a2014-01-16 10:30:26 -080025#include <ndn-cpp/security/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 Yu3b318c12013-10-15 17:54:00 -070029#include "logging.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070030#endif
31
32namespace fs = boost::filesystem;
33using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080034using namespace ndn::ptr_lib;
Yingdi Yu978b3ae2013-10-23 11:50:51 -070035
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070036using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070037
38INIT_LOGGER("ContactPanel");
39
Yingdi Yu76dd8002013-12-24 11:16:32 +080040Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu7989eb22013-10-31 17:38:22 -070041Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070042
Yingdi Yu8e135832013-11-09 20:12:31 -080043ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080044 : QDialog(parent)
45 , ui(new Ui::ContactPanel)
Yingdi Yu8e135832013-11-09 20:12:31 -080046 , m_warningDialog(new WarningDialog)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080047 , m_contactListModel(new QStringListModel)
48 , m_startChatDialog(new StartChatDialog)
49 , m_invitationDialog(new InvitationDialog)
50 , m_settingDialog(new SettingDialog)
Yingdi Yu93adb1a2014-01-16 10:30:26 -080051 , m_policy(new SecPolicyChronoChatPanel())
Yingdi Yu847aa862013-10-09 16:35:53 -070052{
Yingdi Yu76dd8002013-12-24 11:16:32 +080053 qRegisterMetaType<IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070054 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu6eabbd72013-12-27 08:44:12 +080055
56 startFace();
57
Yingdi Yu46948282013-11-06 18:43:31 -080058 createAction();
59
Yingdi Yuf8f572d2014-01-13 11:19:47 -080060 m_keyChain = make_shared<KeyChain>();
Yingdi Yu76dd8002013-12-24 11:16:32 +080061
Yingdi Yuf8f572d2014-01-13 11:19:47 -080062 m_contactManager = make_shared<ContactManager>(m_keyChain, m_face);
Yingdi Yue35bdb82013-11-07 11:32:40 -080063
Yingdi Yu8e135832013-11-09 20:12:31 -080064 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
65 this, SLOT(showError(const QString&)));
Yingdi Yu8e135832013-11-09 20:12:31 -080066
Yingdi Yue35bdb82013-11-07 11:32:40 -080067 openDB();
Yingdi Yu46948282013-11-06 18:43:31 -080068
Yingdi Yueda39aa2013-10-23 23:07:29 -070069 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070070
Yingdi Yu76dd8002013-12-24 11:16:32 +080071 loadTrustAnchor();
Yingdi Yu46948282013-11-06 18:43:31 -080072
Yingdi Yuf8f572d2014-01-13 11:19:47 -080073 m_defaultIdentity = m_keyChain->getDefaultIdentity();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080074 if(m_defaultIdentity.size() == 0)
75 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yuf8f572d2014-01-13 11:19:47 -080076 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -080077 if(defaultCertName.size() == 0)
Yingdi Yuf0b9ea92013-11-10 10:54:16 -080078 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yub5a0fd32013-11-10 16:26:52 -080079
80
81 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080082 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080083 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu711f5892013-11-09 22:18:26 -080084
Yingdi Yu46948282013-11-06 18:43:31 -080085
Yingdi Yue35bdb82013-11-07 11:32:40 -080086 m_profileEditor = new ProfileEditor(m_contactManager);
87 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
88
Yingdi Yu8e135832013-11-09 20:12:31 -080089 m_addContactPanel = new AddContactPanel(m_contactManager);
90 m_browseContactDialog = new BrowseContactDialog(m_contactManager);
91 m_setAliasDialog = new SetAliasDialog(m_contactManager);
Yingdi Yue35bdb82013-11-07 11:32:40 -080092
93 ui->setupUi(this);
94
Yingdi Yu8e135832013-11-09 20:12:31 -080095
Yingdi Yua66183f2013-11-10 10:32:18 -080096 m_localPrefix = Name("/private/local");
Yingdi Yueda39aa2013-10-23 23:07:29 -070097 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -070098
Yingdi Yueda39aa2013-10-23 23:07:29 -070099 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -0800100
Yingdi Yub2e747d2013-11-05 23:06:43 -0800101 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700102
103 ui->ContactList->setModel(m_contactListModel);
104
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700105
Yingdi Yu46948282013-11-06 18:43:31 -0800106 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700107 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -0700108
Yingdi Yueda39aa2013-10-23 23:07:29 -0700109 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
110 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -0700111
Yingdi Yueda39aa2013-10-23 23:07:29 -0700112 connect(ui->EditProfileButton, SIGNAL(clicked()),
113 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -0700114
Yingdi Yu711f5892013-11-09 22:18:26 -0800115 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yue433a502013-11-10 01:34:57 -0800116 this, SLOT(showWarning(const QString&)));
Yingdi Yu711f5892013-11-09 22:18:26 -0800117
Yingdi Yueda39aa2013-10-23 23:07:29 -0700118 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yuae8217c2013-11-09 00:03:26 -0800119 this, SLOT(openBrowseContactDialog()));
120
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800121 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
122 this, SLOT(openAddContactPanel()));
123
Yingdi Yuae8217c2013-11-09 00:03:26 -0800124 connect(this, SIGNAL(refreshCertDirectory()),
125 m_browseContactDialog, SLOT(refreshList()));
126
127 connect(ui->DeleteContactButton, SIGNAL(clicked()),
128 this, SLOT(removeContactButton()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700129
Yingdi Yueda39aa2013-10-23 23:07:29 -0700130 connect(ui->settingButton, SIGNAL(clicked()),
131 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700132
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800133 connect(m_addContactPanel, SIGNAL(newContactAdded()),
134 this, SLOT(refreshContactList()));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800135 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700136 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700137 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
138 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700139
Yingdi Yueda39aa2013-10-23 23:07:29 -0700140 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
141 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700142
Yingdi Yu76dd8002013-12-24 11:16:32 +0800143 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::IdentityCertificate&)),
144 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::IdentityCertificate&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700145 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
146 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu72232692013-11-12 17:50:21 -0800147
148 connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)),
149 this, SLOT(addContactIntoPanelPolicy(const ndn::Name&)));
150 connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)),
151 this, SLOT(removeContactFromPanelPolicy(const ndn::Name&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700152
Yingdi Yu46948282013-11-06 18:43:31 -0800153 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
154 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700155
156 connect(this, SIGNAL(newInvitationReady()),
157 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700158
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800159 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
160 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800161
162 connect(ui->addScope, SIGNAL(clicked()),
163 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800164 connect(ui->deleteScope, SIGNAL(clicked()),
165 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800166 connect(ui->saveButton, SIGNAL(clicked()),
167 this, SLOT(saveScopeClicked()));
168
169 connect(ui->endorseButton, SIGNAL(clicked()),
170 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700171}
172
173ContactPanel::~ContactPanel()
174{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700175 delete ui;
176 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800177 delete m_startChatDialog;
178 delete m_invitationDialog;
179 delete m_settingDialog;
180
Yingdi Yueda39aa2013-10-23 23:07:29 -0700181 delete m_profileEditor;
182 delete m_addContactPanel;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800183 delete m_browseContactDialog;
Yingdi Yu46948282013-11-06 18:43:31 -0800184 delete m_setAliasDialog;
185
Yingdi Yub2e747d2013-11-05 23:06:43 -0800186 delete m_trustScopeModel;
187 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800188 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700189
Yingdi Yueda39aa2013-10-23 23:07:29 -0700190 delete m_menuInvite;
Yingdi Yu46948282013-11-06 18:43:31 -0800191 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700192
193 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
194 for(; it != m_chatDialogs.end(); it++)
195 delete it->second;
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800196
197 shutdownFace();
198}
199
200void
201ContactPanel::startFace()
202{
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800203 m_face = make_shared<Face>();
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800204
205 connectToDaemon();
206
207 m_running = true;
208 m_thread = boost::thread (&ContactPanel::eventLoop, this);
209}
210
211void
212ContactPanel::shutdownFace()
213{
214 {
215 boost::unique_lock<boost::recursive_mutex> lock(m_mutex);
216 m_running = false;
217 }
218
219 m_thread.join();
220 m_face->shutdown();
221}
222
223void
224ContactPanel::eventLoop()
225{
226 while (m_running)
227 {
228 try{
229 m_face->processEvents();
230 usleep(100);
231 }catch(std::exception& e){
232 _LOG_DEBUG(" " << e.what() );
233 }
234 }
Yingdi Yu847aa862013-10-09 16:35:53 -0700235}
236
Yingdi Yu40eca752013-10-10 15:00:58 -0700237void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800238ContactPanel::connectToDaemon()
239{
240 //Hack! transport does not connect to daemon unless an interest is expressed.
241 Name name("/ndn");
242 ndn::Interest interest(name);
243 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800244 func_lib::bind(&ContactPanel::onConnectionData, this, _1, _2),
245 func_lib::bind(&ContactPanel::onConnectionDataTimeout, this, _1));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800246}
247
248void
249ContactPanel::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
250 const shared_ptr<Data>& data)
251{ _LOG_DEBUG("onConnectionData"); }
252
253void
254ContactPanel::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
255{ _LOG_DEBUG("onConnectionDataTimeout"); }
256
257void
Yingdi Yu46948282013-11-06 18:43:31 -0800258ContactPanel::createAction()
259{
260 m_menuInvite = new QAction("&Chat", this);
261 m_menuAlias = new QAction("&Set Alias", this);
262}
263
264void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700265ContactPanel::openDB()
266{
267 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
268 QString path = (QDir::home().path());
269 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
270 db.setDatabaseName(path);
271 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800272 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800273
274 m_trustScopeModel = new QSqlTableModel;
275 m_endorseDataModel = new QSqlTableModel;
276 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700277}
278
279void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800280ContactPanel::loadTrustAnchor()
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700281{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800282 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700283 for(; it != m_contactList.end(); it++)
Yingdi Yue35bdb82013-11-07 11:32:40 -0800284 {
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800285 _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri());
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800286 m_policy->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800287 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700288}
289
290void
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800291ContactPanel::setLocalPrefix(int retry)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700292{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800293 Name interestName("/local/ndn/prefix");
294 Interest interest(interestName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700295
Yingdi Yu76dd8002013-12-24 11:16:32 +0800296 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800297 func_lib::bind(&ContactPanel::onLocalPrefix, this, _1, _2),
298 func_lib::bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800299
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700300}
301
302void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800303ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest,
304 const shared_ptr<Data>& data)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700305{
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800306 string originPrefix((const char*)data->getContent().value(), data->getContent().value_size());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700307 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
308 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700309 m_localPrefix = Name(prefix);
Yingdi Yue433a502013-11-10 01:34:57 -0800310
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700311}
312
313void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800314ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest,
315 int retry)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700316{
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800317 if(retry > 0)
Yingdi Yua66183f2013-11-10 10:32:18 -0800318 {
319 setLocalPrefix(retry - 1);
320 return;
321 }
322 else{
323 m_localPrefix = Name("/private/local");
324 }
Yingdi Yu7989eb22013-10-31 17:38:22 -0700325}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700326
327void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700328ContactPanel::setInvitationListener()
329{
Yingdi Yu46948282013-11-06 18:43:31 -0800330 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
331 m_inviteListenPrefix.append(m_defaultIdentity);
332 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800333 m_invitationListenerId = m_face->setInterestFilter(m_inviteListenPrefix,
334 func_lib::bind(&ContactPanel::onInvitation, this, _1, _2, _3, _4),
335 func_lib::bind(&ContactPanel::onInvitationRegisterFailed, this, _1));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700336}
337
338void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800339ContactPanel::sendInterest(const Interest& interest,
340 const OnVerified& onVerified,
341 const OnVerifyFailed& onVerifyFailed,
342 const TimeoutNotify& timeoutNotify,
343 int retry /* = 1 */,
344 int stepCount /* = 0 */)
345{
346 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800347 func_lib::bind(&ContactPanel::onTargetData,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800348 this,
349 _1,
350 _2,
351 stepCount,
352 onVerified,
353 onVerifyFailed,
354 timeoutNotify),
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800355 func_lib::bind(&ContactPanel::onTargetTimeout,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800356 this,
357 _1,
358 retry,
359 stepCount,
360 onVerified,
361 onVerifyFailed,
362 timeoutNotify));
363}
364
365void
366ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest,
367 const shared_ptr<Data>& data,
368 int stepCount,
369 const OnVerified& onVerified,
370 const OnVerifyFailed& onVerifyFailed,
371 const TimeoutNotify& timeoutNotify)
372{
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800373 shared_ptr<ValidationRequest> nextStep = m_policy->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800374
375 if (nextStep)
376 m_face->expressInterest
377 (*nextStep->interest_,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800378 func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
379 func_lib::bind(&ContactPanel::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800380
381}
382
383void
384ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
385 int retry,
386 int stepCount,
387 const OnVerified& onVerified,
388 const OnVerifyFailed& onVerifyFailed,
389 const TimeoutNotify& timeoutNotify)
390{
391 if(retry > 0)
392 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
393 else
394 {
395 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
396 timeoutNotify();
397 }
398}
399
400void
401ContactPanel::onCertData(const shared_ptr<const ndn::Interest>& interest,
402 const shared_ptr<Data>& cert,
403 shared_ptr<ValidationRequest> previousStep)
404{
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800405 shared_ptr<ValidationRequest> nextStep = m_policy->checkVerificationPolicy(cert,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800406 previousStep->stepCount_,
407 previousStep->onVerified_,
408 previousStep->onVerifyFailed_);
409
410 if (nextStep)
411 m_face->expressInterest
412 (*nextStep->interest_,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800413 func_lib::bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
414 func_lib::bind(&ContactPanel::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800415}
416
417void
418ContactPanel::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
419 const OnVerifyFailed& onVerifyFailed,
420 const shared_ptr<Data>& data,
421 shared_ptr<ValidationRequest> nextStep)
422{
423 if(nextStep->retry_ > 0)
424 m_face->expressInterest(*interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800425 func_lib::bind(&ContactPanel::onCertData,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800426 this,
427 _1,
428 _2,
429 nextStep),
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800430 func_lib::bind(&ContactPanel::onCertTimeout,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800431 this,
432 _1,
433 onVerifyFailed,
434 data,
435 nextStep));
436 else
437 onVerifyFailed(data);
438}
439
440void
441ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix)
442{
443 showError(QString::fromStdString("Cannot register invitation listening prefix"));
444}
445
446void
447ContactPanel::onInvitation(const shared_ptr<const Name>& prefix,
448 const shared_ptr<const Interest>& interest,
449 Transport& transport,
450 uint64_t registeredPrefixId)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700451{
Yingdi Yu46948282013-11-06 18:43:31 -0800452 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800453
Yingdi Yue35bdb82013-11-07 11:32:40 -0800454
Yingdi Yu76dd8002013-12-24 11:16:32 +0800455 shared_ptr<ChronosInvitation> invitation;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800456 try{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800457 invitation = make_shared<ChronosInvitation>(interest->getName());
458 }catch(std::exception& e){
Yingdi Yue35bdb82013-11-07 11:32:40 -0800459 _LOG_ERROR("Exception: " << e.what());
460 return;
461 }
462
Yingdi Yu46948282013-11-06 18:43:31 -0800463 Name chatroomName("/ndn/broadcast/chronos");
464 chatroomName.append(invitation->getChatroom());
465 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
466 if(it != m_chatDialogs.end())
467 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800468 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu46948282013-11-06 18:43:31 -0800469 return;
470 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700471
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800472 shared_ptr<PublicKey> keyPtr = m_policy->getTrustedKey(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800473
474 SignatureSha256WithRsa invitationSig;
475 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
476 invitationSig.setValue(sigBlock);
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800477 if(static_cast<bool>(keyPtr) && Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, *keyPtr))
Yingdi Yuaf305d72013-11-10 11:54:02 -0800478 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800479 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800480 // 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 -0800481 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800482 bool findCert = false;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800483 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800484 for(; it != m_contactList.end(); it++)
485 {
486 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
487 {
488 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
489 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
490 findCert = true;
491 break;
492 }
493 }
494 if(findCert == false)
495 {
496 _LOG_ERROR("No SelfEndorseCertificate found!");
497 return;
498 }
Yingdi Yuaf305d72013-11-10 11:54:02 -0800499 certificate->setPublicKeyInfo(*keyPtr);
500 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
501 return;
502 }
503
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800504 _LOG_DEBUG("Cannot find the inviter's key in trust anchors");
505
Yingdi Yu76dd8002013-12-24 11:16:32 +0800506 Interest newInterest(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800507 OnVerified onVerified = func_lib::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
508 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
509 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800510
511 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700512}
513
514void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800515ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
516 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu46948282013-11-06 18:43:31 -0800517{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800518 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800519
520 SignatureSha256WithRsa invitationSig;
521 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
522 invitationSig.setValue(sigBlock);
Yingdi Yu46948282013-11-06 18:43:31 -0800523
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800524 if(Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, certificate->getPublicKeyInfo()))
Yingdi Yu46948282013-11-06 18:43:31 -0800525 {
526 Name keyName = certificate->getPublicKeyName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800527 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu46948282013-11-06 18:43:31 -0800528 popChatInvitation(invitation, inviterNameSpace, certificate);
529 }
530}
531
532void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800533ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
534{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800535
536void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800537ContactPanel::onInvitationCertTimeoutNotify()
538{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800539
540void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800541ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu46948282013-11-06 18:43:31 -0800542 const Name& inviterNameSpace,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800543 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu46948282013-11-06 18:43:31 -0800544{
545 string alias;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800546 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800547 for(; it != m_contactList.end(); it++)
548 if((*it)->getNameSpace() == inviterNameSpace)
549 alias = (*it)->getAlias();
550
551 if(it != m_contactList.end())
552 return;
553
554 m_invitationDialog->setInvitation(alias, invitation, certificate);
555 emit newInvitationReady();
556}
557
558void
559ContactPanel::collectEndorsement()
560{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800561 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu46948282013-11-06 18:43:31 -0800562 m_collectStatus->assign(m_contactList.size(), false);
563
Yingdi Yu76dd8002013-12-24 11:16:32 +0800564 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800565 int count = 0;
566 for(; it != m_contactList.end(); it++, count++)
567 {
568 Name interestName = (*it)->getNameSpace();
569 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800570 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800571 interest.setInterestLifetime(1000);
Yingdi Yu46948282013-11-06 18:43:31 -0800572
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800573 OnVerified onVerified = func_lib::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
574 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
575 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800576
577 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu46948282013-11-06 18:43:31 -0800578 }
579}
580
581void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800582ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800583{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800584 Data endorseData;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800585 endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800586 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu46948282013-11-06 18:43:31 -0800587
588 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
589
590 updateCollectStatus(count);
591}
592
593void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800594ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800595{ updateCollectStatus(count); }
596
597void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800598ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800599{ updateCollectStatus(count); }
600
601void
602ContactPanel::updateCollectStatus(int count)
603{
604 m_collectStatus->at(count) = true;
605 vector<bool>::const_iterator it = m_collectStatus->begin();
606 for(; it != m_collectStatus->end(); it++)
607 if(*it == false)
608 return;
609
610 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
611}
612
613static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
614
615string
616ContactPanel::getRandomString()
617{
618 string randStr;
619 boost::random::random_device rng;
620 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
621 for (int i = 0; i < 10; i ++)
622 {
623 randStr += chars[index_dist(rng)];
624 }
625 return randStr;
626}
627
628void
Yingdi Yu8e135832013-11-09 20:12:31 -0800629ContactPanel::showError(const QString& msg){
630 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
631 exit(1);
632}
633
634void
Yingdi Yue433a502013-11-10 01:34:57 -0800635ContactPanel::showWarning(const QString& msg){
636 QMessageBox::information(this, tr("Chronos"), msg);
637}
638
639void
Yingdi Yu40eca752013-10-10 15:00:58 -0700640ContactPanel::updateSelection(const QItemSelection &selected,
641 const QItemSelection &deselected)
642{
643 QModelIndexList items = selected.indexes();
644 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800645 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700646
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700647 int i = 0;
648 for(; i < m_contactList.size(); i++)
649 {
650 if(alias == m_contactList[i]->getAlias())
651 break;
652 }
653
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800654 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800655 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
656 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
657 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700658
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800659 if(m_currentSelectedContact->isIntroducer())
660 {
661 ui->isIntroducer->setChecked(true);
662 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800663 ui->deleteScope->setEnabled(true);
664 ui->trustScopeList->setEnabled(true);
665
666 string filter("contact_namespace = '");
667 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
668
669 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
670 m_trustScopeModel->setTable("TrustScope");
671 m_trustScopeModel->setFilter(filter.c_str());
672 m_trustScopeModel->select();
673 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
674 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
675 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
676
Yingdi Yub2e747d2013-11-05 23:06:43 -0800677 ui->trustScopeList->setModel(m_trustScopeModel);
678 ui->trustScopeList->setColumnHidden(0, true);
679 ui->trustScopeList->setColumnHidden(1, true);
680 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800681 }
682 else
683 {
684 ui->isIntroducer->setChecked(false);
685 ui->addScope->setEnabled(false);
686 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800687
688 string filter("contact_namespace = '");
689 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
690
691 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
692 m_trustScopeModel->setTable("TrustScope");
693 m_trustScopeModel->setFilter(filter.c_str());
694 m_trustScopeModel->select();
695 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
696 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
697 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
698
699 ui->trustScopeList->setModel(m_trustScopeModel);
700 ui->trustScopeList->setColumnHidden(0, true);
701 ui->trustScopeList->setColumnHidden(1, true);
702 ui->trustScopeList->show();
703
704 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800705 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800706
707 string filter("profile_identity = '");
708 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
709
710 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
711 m_endorseDataModel->setTable("ContactProfile");
712 m_endorseDataModel->setFilter(filter.c_str());
713 m_endorseDataModel->select();
714
715 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
716 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
717 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
718 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
719
720 ui->endorseList->setModel(m_endorseDataModel);
721 ui->endorseList->setColumnHidden(0, true);
722 ui->endorseList->resizeColumnToContents(1);
723 ui->endorseList->resizeColumnToContents(2);
724 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
725 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700726}
Yingdi Yu847aa862013-10-09 16:35:53 -0700727
Yingdi Yu3b318c12013-10-15 17:54:00 -0700728void
Yingdi Yu46948282013-11-06 18:43:31 -0800729ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700730{
Yingdi Yu711f5892013-11-09 22:18:26 -0800731 // _LOG_DEBUG(identity.toStdString());
Yingdi Yue433a502013-11-10 01:34:57 -0800732 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800733 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -0800734 if(defaultCertName.size() == 0)
Yingdi Yue433a502013-11-10 01:34:57 -0800735 {
736 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
737 return;
738 }
739 m_defaultIdentity = defaultIdentity;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800740 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800741 m_nickName = nickName.toStdString();
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800742 m_face->unsetInterestFilter(m_invitationListenerId);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800743 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800744 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800745 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700746}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700747
748void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700749ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700750{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700751
Yingdi Yu0269c872013-10-16 13:16:49 -0700752void
753ContactPanel::openAddContactPanel()
754{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700755
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700756void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800757ContactPanel::openBrowseContactDialog()
758{
759 m_browseContactDialog->show();
760 emit refreshCertDirectory();
761}
762
763void
764ContactPanel::removeContactButton()
765{
766 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
767 QModelIndexList selectedList = selectionModel->selectedIndexes();
768 QModelIndexList::iterator it = selectedList.begin();
769 for(; it != selectedList.end(); it++)
770 {
771 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800772 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800773 for(; contactIt != m_contactList.end(); contactIt++)
774 {
775 if((*contactIt)->getAlias() == alias)
776 {
777 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
778 m_contactList.erase(contactIt);
779 break;
780 }
781 }
782 }
783 refreshContactList();
784}
785
786void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700787ContactPanel::openInvitationDialog()
788{ m_invitationDialog->show(); }
789
790void
Yingdi Yu72232692013-11-12 17:50:21 -0800791ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
792{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800793 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800794 if(static_cast<bool>(contact))
795 m_policy->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu72232692013-11-12 17:50:21 -0800796}
797
798void
799ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800800{ m_policy->removeTrustAnchor(keyName); }
Yingdi Yu72232692013-11-12 17:50:21 -0800801
802void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700803ContactPanel::refreshContactList()
804{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800805 m_contactList.clear();
806 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700807 QStringList contactNameList;
808 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800809 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700810
811 m_contactListModel->setStringList(contactNameList);
812}
813
814void
815ContactPanel::showContextMenu(const QPoint& pos)
816{
817 QMenu menu(ui->ContactList);
818 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700819 connect(m_menuInvite, SIGNAL(triggered()),
820 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800821 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700822 menu.addAction(m_menuAlias);
823 connect(m_menuAlias, SIGNAL(triggered()),
824 this, SLOT(openSetAliasDialog()));
825 menu.exec(ui->ContactList->mapToGlobal(pos));
826
827}
828
829void
830ContactPanel::openSetAliasDialog()
831{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800832 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700833 m_setAliasDialog->show();
834}
835
Yingdi Yu595aadc2013-10-21 15:01:40 -0700836void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700837ContactPanel::openSettingDialog()
838{
Yingdi Yu46948282013-11-06 18:43:31 -0800839 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700840 m_settingDialog->show();
841}
842
843void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700844ContactPanel::openStartChatDialog()
845{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700846 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700847 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700848
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800849 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700850 m_startChatDialog->show();
851}
852
Yingdi Yu7989eb22013-10-31 17:38:22 -0700853// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700854void
855ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
856{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800857 Name chatroomName(chatroom.toStdString());
Yingdi Yu72781e52013-11-06 23:00:21 -0800858
859 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800860 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu72781e52013-11-06 23:00:21 -0800861
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800862 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700863 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800864
865 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
866 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800867 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
868 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800869 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
870 this, SLOT(showWarning(const QString&)));
Yingdi Yu46948282013-11-06 18:43:31 -0800871
Yingdi Yue6476cd2013-11-06 18:51:19 -0800872 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700873 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700874
875 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700876}
877
Yingdi Yu7989eb22013-10-31 17:38:22 -0700878// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700879void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700880ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800881 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700882{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800883 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu72781e52013-11-06 23:00:21 -0800884
Yingdi Yu7989eb22013-10-31 17:38:22 -0700885 Name chatroomName("/ndn/broadcast/chronos");
886 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800887
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800888 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800889
Yingdi Yu46948282013-11-06 18:43:31 -0800890 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
891 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800892 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
893 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800894 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
895 this, SLOT(showWarning(const QString&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700896
Yingdi Yue6476cd2013-11-06 18:51:19 -0800897 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800898 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700899
Yingdi Yu7989eb22013-10-31 17:38:22 -0700900 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
901
902 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
903
904 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700905}
906
907void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700908ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800909 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700910{
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800911 Name dataName = invitation.getInterestName();
912 time_t nowSeconds = time(NULL);
913 struct tm current = *gmtime(&nowSeconds);
914 MillisecondsSince1970 version = timegm(&current) * 1000.0;
915 dataName.appendVersion(version);
916 Data data(dataName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800917 string content = m_localPrefix.toUri();
918 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800919
920 Name certificateName;
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800921 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800922
923 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800924 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800925 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800926 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
927 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800928
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800929 m_face->put(data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800930
Yingdi Yu7989eb22013-10-31 17:38:22 -0700931 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700932}
933
934void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700935ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700936{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800937 Data data(invitation.getInterestName());
938 string content("nack");
939 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800940
941 Name certificateName;
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800942 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800943 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800944 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800945 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800946 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
947 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800948
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800949 m_face->put(data);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700950}
951
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800952void
953ContactPanel::isIntroducerChanged(int state)
954{
955 if(state == Qt::Checked)
956 {
957 ui->addScope->setEnabled(true);
958 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800959 ui->trustScopeList->setEnabled(true);
960
961 string filter("contact_namespace = '");
962 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800963
964 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
965 m_trustScopeModel->setTable("TrustScope");
966 m_trustScopeModel->setFilter(filter.c_str());
967 m_trustScopeModel->select();
968 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
969 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
970 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800971
972 ui->trustScopeList->setModel(m_trustScopeModel);
973 ui->trustScopeList->setColumnHidden(0, true);
974 ui->trustScopeList->setColumnHidden(1, true);
975 ui->trustScopeList->show();
976
977 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800978 }
979 else
980 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800981 ui->addScope->setEnabled(false);
982 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800983
984 string filter("contact_namespace = '");
985 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
986
987 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
988 m_trustScopeModel->setTable("TrustScope");
989 m_trustScopeModel->setFilter(filter.c_str());
990 m_trustScopeModel->select();
991 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
992 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
993 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
994
995 ui->trustScopeList->setModel(m_trustScopeModel);
996 ui->trustScopeList->setColumnHidden(0, true);
997 ui->trustScopeList->setColumnHidden(1, true);
998 ui->trustScopeList->show();
999
1000 ui->trustScopeList->setEnabled(false);
1001
1002 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -08001003 }
Yingdi Yub2e747d2013-11-05 23:06:43 -08001004 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
1005}
1006
1007void
1008ContactPanel::addScopeClicked()
1009{
1010 int rowCount = m_trustScopeModel->rowCount();
1011 QSqlRecord record;
1012 QSqlField identityField("contact_namespace", QVariant::String);
1013 record.append(identityField);
Yingdi Yu76dd8002013-12-24 11:16:32 +08001014 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yub2e747d2013-11-05 23:06:43 -08001015 m_trustScopeModel->insertRow(rowCount);
1016 m_trustScopeModel->setRecord(rowCount, record);
1017}
1018
1019void
1020ContactPanel::deleteScopeClicked()
1021{
1022 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
1023 QModelIndexList indexList = selectionModel->selectedIndexes();
1024
1025 int i = indexList.size() - 1;
1026 for(; i >= 0; i--)
1027 m_trustScopeModel->removeRow(indexList[i].row());
1028
1029 m_trustScopeModel->submitAll();
1030}
1031
1032void
1033ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -08001034{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -08001035
1036void
1037ContactPanel::endorseButtonClicked()
1038{
1039 m_endorseDataModel->submitAll();
1040 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -08001041}
Yingdi Yu595aadc2013-10-21 15:01:40 -07001042
Yingdi Yu46948282013-11-06 18:43:31 -08001043void
1044ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
1045{
1046 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -08001047
1048 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -08001049 if(it != m_chatDialogs.end())
1050 {
Yingdi Yu46948282013-11-06 18:43:31 -08001051 deletedChat = it->second;
1052 m_chatDialogs.erase(it);
1053 }
Yingdi Yu83eae842013-11-06 22:07:38 -08001054 if (deletedChat != NULL)
1055 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -08001056}
1057
Yingdi Yu847aa862013-10-09 16:35:53 -07001058#if WAF
1059#include "contactpanel.moc"
1060#include "contactpanel.cpp.moc"
1061#endif