blob: 27978da0f94df50bb850091589c8388ca3d97a37 [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 Yu68de61d2014-01-16 19:13:03 -080061 m_verifier = make_shared<Verifier>(m_policy);
62 m_verifier->setFace(m_face);
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 Yu93adb1a2014-01-16 10:30:26 -0800288 m_policy->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,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800345 int retry /* = 1 */)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800346{
347 m_face->expressInterest(interest,
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800348 func_lib::bind(&ContactPanel::onTargetData,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800349 this,
350 _1,
351 _2,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800352 onVerified,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800353 onVerifyFailed),
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800354 func_lib::bind(&ContactPanel::onTargetTimeout,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800355 this,
356 _1,
357 retry,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800358 onVerified,
359 onVerifyFailed,
360 timeoutNotify));
361}
362
363void
364ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest,
365 const shared_ptr<Data>& data,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800366 const OnVerified& onVerified,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800367 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800368{
Yingdi Yu68de61d2014-01-16 19:13:03 -0800369 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800370}
371
372void
373ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
374 int retry,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800375 const OnVerified& onVerified,
376 const OnVerifyFailed& onVerifyFailed,
377 const TimeoutNotify& timeoutNotify)
378{
379 if(retry > 0)
Yingdi Yu68de61d2014-01-16 19:13:03 -0800380 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800381 else
382 {
383 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
384 timeoutNotify();
385 }
386}
387
388void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800389ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix)
390{
391 showError(QString::fromStdString("Cannot register invitation listening prefix"));
392}
393
394void
395ContactPanel::onInvitation(const shared_ptr<const Name>& prefix,
396 const shared_ptr<const Interest>& interest,
397 Transport& transport,
398 uint64_t registeredPrefixId)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700399{
Yingdi Yu46948282013-11-06 18:43:31 -0800400 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800401
Yingdi Yue35bdb82013-11-07 11:32:40 -0800402
Yingdi Yu76dd8002013-12-24 11:16:32 +0800403 shared_ptr<ChronosInvitation> invitation;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800404 try{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800405 invitation = make_shared<ChronosInvitation>(interest->getName());
406 }catch(std::exception& e){
Yingdi Yue35bdb82013-11-07 11:32:40 -0800407 _LOG_ERROR("Exception: " << e.what());
408 return;
409 }
410
Yingdi Yu46948282013-11-06 18:43:31 -0800411 Name chatroomName("/ndn/broadcast/chronos");
412 chatroomName.append(invitation->getChatroom());
413 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
414 if(it != m_chatDialogs.end())
415 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800416 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu46948282013-11-06 18:43:31 -0800417 return;
418 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700419
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800420 shared_ptr<PublicKey> keyPtr = m_policy->getTrustedKey(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800421
422 SignatureSha256WithRsa invitationSig;
423 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
424 invitationSig.setValue(sigBlock);
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800425 if(static_cast<bool>(keyPtr) && Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, *keyPtr))
Yingdi Yuaf305d72013-11-10 11:54:02 -0800426 {
Yingdi Yu76dd8002013-12-24 11:16:32 +0800427 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800428 // 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 -0800429 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800430 bool findCert = false;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800431 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu1ecf81f2013-11-10 12:25:43 -0800432 for(; it != m_contactList.end(); it++)
433 {
434 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
435 {
436 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
437 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
438 findCert = true;
439 break;
440 }
441 }
442 if(findCert == false)
443 {
444 _LOG_ERROR("No SelfEndorseCertificate found!");
445 return;
446 }
Yingdi Yuaf305d72013-11-10 11:54:02 -0800447 certificate->setPublicKeyInfo(*keyPtr);
448 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
449 return;
450 }
451
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800452 _LOG_DEBUG("Cannot find the inviter's key in trust anchors");
453
Yingdi Yu76dd8002013-12-24 11:16:32 +0800454 Interest newInterest(invitation->getInviterCertificateName());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800455 OnVerified onVerified = func_lib::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
456 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
457 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800458
459 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700460}
461
462void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800463ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
464 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu46948282013-11-06 18:43:31 -0800465{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800466 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800467
468 SignatureSha256WithRsa invitationSig;
469 Block sigBlock(invitation->getSignatureBits().buf(), invitation->getSignatureBits().size());
470 invitationSig.setValue(sigBlock);
Yingdi Yu46948282013-11-06 18:43:31 -0800471
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800472 if(Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, certificate->getPublicKeyInfo()))
Yingdi Yu46948282013-11-06 18:43:31 -0800473 {
474 Name keyName = certificate->getPublicKeyName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800475 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu46948282013-11-06 18:43:31 -0800476 popChatInvitation(invitation, inviterNameSpace, certificate);
477 }
478}
479
480void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800481ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
482{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800483
484void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800485ContactPanel::onInvitationCertTimeoutNotify()
486{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu46948282013-11-06 18:43:31 -0800487
488void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800489ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu46948282013-11-06 18:43:31 -0800490 const Name& inviterNameSpace,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800491 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu46948282013-11-06 18:43:31 -0800492{
493 string alias;
Yingdi Yu76dd8002013-12-24 11:16:32 +0800494 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800495 for(; it != m_contactList.end(); it++)
496 if((*it)->getNameSpace() == inviterNameSpace)
497 alias = (*it)->getAlias();
498
499 if(it != m_contactList.end())
500 return;
501
502 m_invitationDialog->setInvitation(alias, invitation, certificate);
503 emit newInvitationReady();
504}
505
506void
507ContactPanel::collectEndorsement()
508{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800509 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu46948282013-11-06 18:43:31 -0800510 m_collectStatus->assign(m_contactList.size(), false);
511
Yingdi Yu76dd8002013-12-24 11:16:32 +0800512 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800513 int count = 0;
514 for(; it != m_contactList.end(); it++, count++)
515 {
516 Name interestName = (*it)->getNameSpace();
517 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800518 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800519 interest.setInterestLifetime(1000);
Yingdi Yu46948282013-11-06 18:43:31 -0800520
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800521 OnVerified onVerified = func_lib::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
522 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
523 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800524
525 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu46948282013-11-06 18:43:31 -0800526 }
527}
528
529void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800530ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800531{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800532 Data endorseData;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800533 endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800534 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu46948282013-11-06 18:43:31 -0800535
536 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
537
538 updateCollectStatus(count);
539}
540
541void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800542ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800543{ updateCollectStatus(count); }
544
545void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800546ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800547{ updateCollectStatus(count); }
548
549void
550ContactPanel::updateCollectStatus(int count)
551{
552 m_collectStatus->at(count) = true;
553 vector<bool>::const_iterator it = m_collectStatus->begin();
554 for(; it != m_collectStatus->end(); it++)
555 if(*it == false)
556 return;
557
558 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
559}
560
561static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
562
563string
564ContactPanel::getRandomString()
565{
566 string randStr;
567 boost::random::random_device rng;
568 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
569 for (int i = 0; i < 10; i ++)
570 {
571 randStr += chars[index_dist(rng)];
572 }
573 return randStr;
574}
575
576void
Yingdi Yu8e135832013-11-09 20:12:31 -0800577ContactPanel::showError(const QString& msg){
578 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
579 exit(1);
580}
581
582void
Yingdi Yue433a502013-11-10 01:34:57 -0800583ContactPanel::showWarning(const QString& msg){
584 QMessageBox::information(this, tr("Chronos"), msg);
585}
586
587void
Yingdi Yu40eca752013-10-10 15:00:58 -0700588ContactPanel::updateSelection(const QItemSelection &selected,
589 const QItemSelection &deselected)
590{
591 QModelIndexList items = selected.indexes();
592 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800593 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700594
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700595 int i = 0;
596 for(; i < m_contactList.size(); i++)
597 {
598 if(alias == m_contactList[i]->getAlias())
599 break;
600 }
601
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800602 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800603 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
604 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
605 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700606
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800607 if(m_currentSelectedContact->isIntroducer())
608 {
609 ui->isIntroducer->setChecked(true);
610 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800611 ui->deleteScope->setEnabled(true);
612 ui->trustScopeList->setEnabled(true);
613
614 string filter("contact_namespace = '");
615 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
616
617 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
618 m_trustScopeModel->setTable("TrustScope");
619 m_trustScopeModel->setFilter(filter.c_str());
620 m_trustScopeModel->select();
621 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
622 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
623 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
624
Yingdi Yub2e747d2013-11-05 23:06:43 -0800625 ui->trustScopeList->setModel(m_trustScopeModel);
626 ui->trustScopeList->setColumnHidden(0, true);
627 ui->trustScopeList->setColumnHidden(1, true);
628 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800629 }
630 else
631 {
632 ui->isIntroducer->setChecked(false);
633 ui->addScope->setEnabled(false);
634 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800635
636 string filter("contact_namespace = '");
637 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
638
639 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
640 m_trustScopeModel->setTable("TrustScope");
641 m_trustScopeModel->setFilter(filter.c_str());
642 m_trustScopeModel->select();
643 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
644 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
645 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
646
647 ui->trustScopeList->setModel(m_trustScopeModel);
648 ui->trustScopeList->setColumnHidden(0, true);
649 ui->trustScopeList->setColumnHidden(1, true);
650 ui->trustScopeList->show();
651
652 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800653 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800654
655 string filter("profile_identity = '");
656 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
657
658 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
659 m_endorseDataModel->setTable("ContactProfile");
660 m_endorseDataModel->setFilter(filter.c_str());
661 m_endorseDataModel->select();
662
663 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
664 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
665 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
666 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
667
668 ui->endorseList->setModel(m_endorseDataModel);
669 ui->endorseList->setColumnHidden(0, true);
670 ui->endorseList->resizeColumnToContents(1);
671 ui->endorseList->resizeColumnToContents(2);
672 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
673 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700674}
Yingdi Yu847aa862013-10-09 16:35:53 -0700675
Yingdi Yu3b318c12013-10-15 17:54:00 -0700676void
Yingdi Yu46948282013-11-06 18:43:31 -0800677ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700678{
Yingdi Yu711f5892013-11-09 22:18:26 -0800679 // _LOG_DEBUG(identity.toStdString());
Yingdi Yue433a502013-11-10 01:34:57 -0800680 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800681 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -0800682 if(defaultCertName.size() == 0)
Yingdi Yue433a502013-11-10 01:34:57 -0800683 {
684 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
685 return;
686 }
687 m_defaultIdentity = defaultIdentity;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800688 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800689 m_nickName = nickName.toStdString();
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800690 m_face->unsetInterestFilter(m_invitationListenerId);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800691 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800692 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800693 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700694}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700695
696void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700697ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700698{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700699
Yingdi Yu0269c872013-10-16 13:16:49 -0700700void
701ContactPanel::openAddContactPanel()
702{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700703
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700704void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800705ContactPanel::openBrowseContactDialog()
706{
707 m_browseContactDialog->show();
708 emit refreshCertDirectory();
709}
710
711void
712ContactPanel::removeContactButton()
713{
714 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
715 QModelIndexList selectedList = selectionModel->selectedIndexes();
716 QModelIndexList::iterator it = selectedList.begin();
717 for(; it != selectedList.end(); it++)
718 {
719 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800720 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800721 for(; contactIt != m_contactList.end(); contactIt++)
722 {
723 if((*contactIt)->getAlias() == alias)
724 {
725 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
726 m_contactList.erase(contactIt);
727 break;
728 }
729 }
730 }
731 refreshContactList();
732}
733
734void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700735ContactPanel::openInvitationDialog()
736{ m_invitationDialog->show(); }
737
738void
Yingdi Yu72232692013-11-12 17:50:21 -0800739ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
740{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800741 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800742 if(static_cast<bool>(contact))
743 m_policy->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu72232692013-11-12 17:50:21 -0800744}
745
746void
747ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800748{ m_policy->removeTrustAnchor(keyName); }
Yingdi Yu72232692013-11-12 17:50:21 -0800749
750void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700751ContactPanel::refreshContactList()
752{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800753 m_contactList.clear();
754 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700755 QStringList contactNameList;
756 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800757 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700758
759 m_contactListModel->setStringList(contactNameList);
760}
761
762void
763ContactPanel::showContextMenu(const QPoint& pos)
764{
765 QMenu menu(ui->ContactList);
766 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700767 connect(m_menuInvite, SIGNAL(triggered()),
768 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800769 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700770 menu.addAction(m_menuAlias);
771 connect(m_menuAlias, SIGNAL(triggered()),
772 this, SLOT(openSetAliasDialog()));
773 menu.exec(ui->ContactList->mapToGlobal(pos));
774
775}
776
777void
778ContactPanel::openSetAliasDialog()
779{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800780 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700781 m_setAliasDialog->show();
782}
783
Yingdi Yu595aadc2013-10-21 15:01:40 -0700784void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700785ContactPanel::openSettingDialog()
786{
Yingdi Yu46948282013-11-06 18:43:31 -0800787 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700788 m_settingDialog->show();
789}
790
791void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700792ContactPanel::openStartChatDialog()
793{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700794 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700795 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700796
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800797 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700798 m_startChatDialog->show();
799}
800
Yingdi Yu7989eb22013-10-31 17:38:22 -0700801// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700802void
803ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
804{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800805 Name chatroomName(chatroom.toStdString());
Yingdi Yu72781e52013-11-06 23:00:21 -0800806
807 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800808 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu72781e52013-11-06 23:00:21 -0800809
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800810 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700811 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800812
813 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
814 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800815 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
816 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800817 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
818 this, SLOT(showWarning(const QString&)));
Yingdi Yu46948282013-11-06 18:43:31 -0800819
Yingdi Yue6476cd2013-11-06 18:51:19 -0800820 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700821 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700822
823 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700824}
825
Yingdi Yu7989eb22013-10-31 17:38:22 -0700826// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700827void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700828ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800829 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700830{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800831 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu72781e52013-11-06 23:00:21 -0800832
Yingdi Yu7989eb22013-10-31 17:38:22 -0700833 Name chatroomName("/ndn/broadcast/chronos");
834 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800835
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800836 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800837
Yingdi Yu46948282013-11-06 18:43:31 -0800838 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
839 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800840 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
841 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800842 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
843 this, SLOT(showWarning(const QString&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700844
Yingdi Yue6476cd2013-11-06 18:51:19 -0800845 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800846 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700847
Yingdi Yu7989eb22013-10-31 17:38:22 -0700848 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
849
850 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
851
852 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700853}
854
855void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700856ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800857 const IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700858{
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800859 Name dataName = invitation.getInterestName();
860 time_t nowSeconds = time(NULL);
861 struct tm current = *gmtime(&nowSeconds);
862 MillisecondsSince1970 version = timegm(&current) * 1000.0;
863 dataName.appendVersion(version);
864 Data data(dataName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800865 string content = m_localPrefix.toUri();
866 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800867
868 Name certificateName;
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800869 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800870
871 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800872 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800873 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800874 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
875 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800876
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800877 m_face->put(data);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800878
Yingdi Yu7989eb22013-10-31 17:38:22 -0700879 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700880}
881
882void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700883ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700884{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800885 Data data(invitation.getInterestName());
886 string content("nack");
887 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800888
889 Name certificateName;
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800890 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800891 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800892 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800893 else
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800894 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
895 m_keyChain->sign(data, certificateName);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800896
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800897 m_face->put(data);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700898}
899
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800900void
901ContactPanel::isIntroducerChanged(int state)
902{
903 if(state == Qt::Checked)
904 {
905 ui->addScope->setEnabled(true);
906 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800907 ui->trustScopeList->setEnabled(true);
908
909 string filter("contact_namespace = '");
910 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800911
912 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
913 m_trustScopeModel->setTable("TrustScope");
914 m_trustScopeModel->setFilter(filter.c_str());
915 m_trustScopeModel->select();
916 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
917 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
918 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800919
920 ui->trustScopeList->setModel(m_trustScopeModel);
921 ui->trustScopeList->setColumnHidden(0, true);
922 ui->trustScopeList->setColumnHidden(1, true);
923 ui->trustScopeList->show();
924
925 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800926 }
927 else
928 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800929 ui->addScope->setEnabled(false);
930 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800931
932 string filter("contact_namespace = '");
933 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
934
935 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
936 m_trustScopeModel->setTable("TrustScope");
937 m_trustScopeModel->setFilter(filter.c_str());
938 m_trustScopeModel->select();
939 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
940 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
941 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
942
943 ui->trustScopeList->setModel(m_trustScopeModel);
944 ui->trustScopeList->setColumnHidden(0, true);
945 ui->trustScopeList->setColumnHidden(1, true);
946 ui->trustScopeList->show();
947
948 ui->trustScopeList->setEnabled(false);
949
950 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800951 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800952 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
953}
954
955void
956ContactPanel::addScopeClicked()
957{
958 int rowCount = m_trustScopeModel->rowCount();
959 QSqlRecord record;
960 QSqlField identityField("contact_namespace", QVariant::String);
961 record.append(identityField);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800962 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800963 m_trustScopeModel->insertRow(rowCount);
964 m_trustScopeModel->setRecord(rowCount, record);
965}
966
967void
968ContactPanel::deleteScopeClicked()
969{
970 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
971 QModelIndexList indexList = selectionModel->selectedIndexes();
972
973 int i = indexList.size() - 1;
974 for(; i >= 0; i--)
975 m_trustScopeModel->removeRow(indexList[i].row());
976
977 m_trustScopeModel->submitAll();
978}
979
980void
981ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -0800982{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800983
984void
985ContactPanel::endorseButtonClicked()
986{
987 m_endorseDataModel->submitAll();
988 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800989}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700990
Yingdi Yu46948282013-11-06 18:43:31 -0800991void
992ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
993{
994 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -0800995
996 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -0800997 if(it != m_chatDialogs.end())
998 {
Yingdi Yu46948282013-11-06 18:43:31 -0800999 deletedChat = it->second;
1000 m_chatDialogs.erase(it);
1001 }
Yingdi Yu83eae842013-11-06 22:07:38 -08001002 if (deletedChat != NULL)
1003 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -08001004}
1005
Yingdi Yu847aa862013-10-09 16:35:53 -07001006#if WAF
1007#include "contactpanel.moc"
1008#include "contactpanel.cpp.moc"
1009#endif