blob: 0c0c5b80330641226720d078630ed3d773dd2655 [file] [log] [blame]
Yingdi Yu9e0dc292013-10-10 11:39:45 -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
Yingdi Yudef90612013-10-09 22:34:42 -070011#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu01a942b2013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yu8dacdf22013-11-05 23:06:43 -080019#include <QtSql/QSqlRecord>
20#include <QtSql/QSqlField>
21#include <QtSql/QSqlError>
Yingdi Yu01a942b2013-10-10 15:00:58 -070022
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070023#ifndef Q_MOC_RUN
Yingdi Yu6df61252014-01-21 11:05:11 -080024#include <ndn-cpp-dev/security/verifier.hpp>
25#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070026#include <boost/filesystem.hpp>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070027#include <boost/random/random_device.hpp>
28#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070029#include "logging.h"
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070030#endif
31
32namespace fs = boost::filesystem;
33using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080034using namespace ndn::ptr_lib;
Yingdi Yu53eb8a72013-10-23 11:50:51 -070035
Yingdi Yu79c25a22013-10-21 13:38:38 -070036using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070037
38INIT_LOGGER("ContactPanel");
39
Yingdi Yu64206112013-12-24 11:16:32 +080040Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu42f66462013-10-31 17:38:22 -070041Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070042
Yingdi Yub29f78c2013-11-09 20:12:31 -080043ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yua50c3252013-11-03 15:05:26 -080044 : QDialog(parent)
45 , ui(new Ui::ContactPanel)
Yingdi Yub29f78c2013-11-09 20:12:31 -080046 , m_warningDialog(new WarningDialog)
Yingdi Yua50c3252013-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 Yueaa84e22014-01-16 10:30:26 -080051 , m_policy(new SecPolicyChronoChatPanel())
Yingdi Yudef90612013-10-09 22:34:42 -070052{
Yingdi Yu64206112013-12-24 11:16:32 +080053 qRegisterMetaType<IdentityCertificate>("IdentityCertificate");
Yingdi Yu42f66462013-10-31 17:38:22 -070054 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yuaccbda92013-12-27 08:44:12 +080055
56 startFace();
57
Yingdi Yu42372442013-11-06 18:43:31 -080058 createAction();
59
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080060 m_keyChain = make_shared<KeyChain>();
Yingdi Yu7630f642014-01-16 19:13:03 -080061 m_verifier = make_shared<Verifier>(m_policy);
62 m_verifier->setFace(m_face);
Yingdi Yu64206112013-12-24 11:16:32 +080063
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080064 m_contactManager = make_shared<ContactManager>(m_keyChain, m_face);
Yingdi Yub35b8652013-11-07 11:32:40 -080065
Yingdi Yub29f78c2013-11-09 20:12:31 -080066 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
67 this, SLOT(showError(const QString&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -080068
Yingdi Yub35b8652013-11-07 11:32:40 -080069 openDB();
Yingdi Yu42372442013-11-06 18:43:31 -080070
Yingdi Yuc4d08d22013-10-23 23:07:29 -070071 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070072
Yingdi Yu64206112013-12-24 11:16:32 +080073 loadTrustAnchor();
Yingdi Yu42372442013-11-06 18:43:31 -080074
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080075 m_defaultIdentity = m_keyChain->getDefaultIdentity();
Yingdi Yu10436892013-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 Yuc9ffa9f2014-01-13 11:19:47 -080078 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(m_defaultIdentity);
Yingdi Yu71ec9652013-11-09 22:18:26 -080079 if(defaultCertName.size() == 0)
Yingdi Yucf1bdcb2013-11-10 10:54:16 -080080 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yu10436892013-11-10 16:26:52 -080081
82
83 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu64206112013-12-24 11:16:32 +080084 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yu10436892013-11-10 16:26:52 -080085 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu71ec9652013-11-09 22:18:26 -080086
Yingdi Yu42372442013-11-06 18:43:31 -080087
Yingdi Yub35b8652013-11-07 11:32:40 -080088 m_profileEditor = new ProfileEditor(m_contactManager);
89 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
90
Yingdi Yub29f78c2013-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 Yub35b8652013-11-07 11:32:40 -080094
95 ui->setupUi(this);
96
Yingdi Yub29f78c2013-11-09 20:12:31 -080097
Yingdi Yu0a953c32013-11-10 10:32:18 -080098 m_localPrefix = Name("/private/local");
Yingdi Yuc4d08d22013-10-23 23:07:29 -070099 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700100
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700101 setInvitationListener();
Yingdi Yu42372442013-11-06 18:43:31 -0800102
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800103 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700104
105 ui->ContactList->setModel(m_contactListModel);
106
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700107
Yingdi Yu42372442013-11-06 18:43:31 -0800108 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700109 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -0700110
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700111 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
112 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -0700113
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700114 connect(ui->EditProfileButton, SIGNAL(clicked()),
115 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -0700116
Yingdi Yu71ec9652013-11-09 22:18:26 -0800117 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800118 this, SLOT(showWarning(const QString&)));
Yingdi Yu71ec9652013-11-09 22:18:26 -0800119
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700120 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yu908f8412013-11-09 00:03:26 -0800121 this, SLOT(openBrowseContactDialog()));
122
Yingdi Yu3cd30df2013-11-10 17:59:58 -0800123 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
124 this, SLOT(openAddContactPanel()));
125
Yingdi Yu908f8412013-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 Yu53eb8a72013-10-23 11:50:51 -0700131
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700132 connect(ui->settingButton, SIGNAL(clicked()),
133 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700134
Yingdi Yu3cd30df2013-11-10 17:59:58 -0800135 connect(m_addContactPanel, SIGNAL(newContactAdded()),
136 this, SLOT(refreshContactList()));
Yingdi Yu908f8412013-11-09 00:03:26 -0800137 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700138 this, SLOT(refreshContactList()));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700139 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
140 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700141
Yingdi Yuc4d08d22013-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 Yu53eb8a72013-10-23 11:50:51 -0700144
Yingdi Yu64206112013-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 Yu42f66462013-10-31 17:38:22 -0700147 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
148 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu6ea54e42013-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 Yu53eb8a72013-10-23 11:50:51 -0700154
Yingdi Yu42372442013-11-06 18:43:31 -0800155 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
156 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700157
158 connect(this, SIGNAL(newInvitationReady()),
159 this, SLOT(openInvitationDialog()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700160
Yingdi Yua50c3252013-11-03 15:05:26 -0800161 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
162 this, SLOT(isIntroducerChanged(int)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800163
164 connect(ui->addScope, SIGNAL(clicked()),
165 this, SLOT(addScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800166 connect(ui->deleteScope, SIGNAL(clicked()),
167 this, SLOT(deleteScopeClicked()));
Yingdi Yu8dacdf22013-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 Yudef90612013-10-09 22:34:42 -0700173}
174
175ContactPanel::~ContactPanel()
176{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700177 delete ui;
178 delete m_contactListModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800179 delete m_startChatDialog;
180 delete m_invitationDialog;
181 delete m_settingDialog;
182
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700183 delete m_profileEditor;
184 delete m_addContactPanel;
Yingdi Yu908f8412013-11-09 00:03:26 -0800185 delete m_browseContactDialog;
Yingdi Yu42372442013-11-06 18:43:31 -0800186 delete m_setAliasDialog;
187
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800188 delete m_trustScopeModel;
189 delete m_endorseDataModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800190 delete m_endorseComboBoxDelegate;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700191
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700192 delete m_menuInvite;
Yingdi Yu42372442013-11-06 18:43:31 -0800193 delete m_menuAlias;
Yingdi Yuc4d08d22013-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 Yuaccbda92013-12-27 08:44:12 +0800198
199 shutdownFace();
200}
201
202void
203ContactPanel::startFace()
204{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800205 m_face = make_shared<Face>();
Yingdi Yuaccbda92013-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 Yudef90612013-10-09 22:34:42 -0700237}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700238
Yingdi Yu01a942b2013-10-10 15:00:58 -0700239void
Yingdi Yu64206112013-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 Yuc9ffa9f2014-01-13 11:19:47 -0800246 func_lib::bind(&ContactPanel::onConnectionData, this, _1, _2),
247 func_lib::bind(&ContactPanel::onConnectionDataTimeout, this, _1));
Yingdi Yu64206112013-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 Yu42372442013-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 Yu53eb8a72013-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 Yu42372442013-11-06 18:43:31 -0800274 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800275
276 m_trustScopeModel = new QSqlTableModel;
277 m_endorseDataModel = new QSqlTableModel;
278 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700279}
280
281void
Yingdi Yu64206112013-12-24 11:16:32 +0800282ContactPanel::loadTrustAnchor()
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700283{
Yingdi Yu64206112013-12-24 11:16:32 +0800284 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700285 for(; it != m_contactList.end(); it++)
Yingdi Yub35b8652013-11-07 11:32:40 -0800286 {
Yingdi Yuaccbda92013-12-27 08:44:12 +0800287 _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri());
Yingdi Yueaa84e22014-01-16 10:30:26 -0800288 m_policy->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yub35b8652013-11-07 11:32:40 -0800289 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700290}
291
292void
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800293ContactPanel::setLocalPrefix(int retry)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700294{
Yingdi Yu64206112013-12-24 11:16:32 +0800295 Name interestName("/local/ndn/prefix");
296 Interest interest(interestName);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700297
Yingdi Yu64206112013-12-24 11:16:32 +0800298 m_face->expressInterest(interest,
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800299 func_lib::bind(&ContactPanel::onLocalPrefix, this, _1, _2),
300 func_lib::bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
Yingdi Yu64206112013-12-24 11:16:32 +0800301
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700302}
303
304void
Yingdi Yu64206112013-12-24 11:16:32 +0800305ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest,
306 const shared_ptr<Data>& data)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700307{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800308 string originPrefix((const char*)data->getContent().value(), data->getContent().value_size());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700309 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
310 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700311 m_localPrefix = Name(prefix);
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800312
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700313}
314
315void
Yingdi Yu64206112013-12-24 11:16:32 +0800316ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest,
317 int retry)
Yingdi Yu42f66462013-10-31 17:38:22 -0700318{
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800319 if(retry > 0)
Yingdi Yu0a953c32013-11-10 10:32:18 -0800320 {
321 setLocalPrefix(retry - 1);
322 return;
323 }
324 else{
325 m_localPrefix = Name("/private/local");
326 }
Yingdi Yu42f66462013-10-31 17:38:22 -0700327}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700328
329void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700330ContactPanel::setInvitationListener()
331{
Yingdi Yu42372442013-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 Yuc9ffa9f2014-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 Yu53eb8a72013-10-23 11:50:51 -0700338}
339
340void
Yingdi Yu64206112013-12-24 11:16:32 +0800341ContactPanel::sendInterest(const Interest& interest,
342 const OnVerified& onVerified,
343 const OnVerifyFailed& onVerifyFailed,
344 const TimeoutNotify& timeoutNotify,
Yingdi Yu7630f642014-01-16 19:13:03 -0800345 int retry /* = 1 */)
Yingdi Yu64206112013-12-24 11:16:32 +0800346{
347 m_face->expressInterest(interest,
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800348 func_lib::bind(&ContactPanel::onTargetData,
Yingdi Yu64206112013-12-24 11:16:32 +0800349 this,
350 _1,
351 _2,
Yingdi Yu64206112013-12-24 11:16:32 +0800352 onVerified,
Yingdi Yu7630f642014-01-16 19:13:03 -0800353 onVerifyFailed),
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800354 func_lib::bind(&ContactPanel::onTargetTimeout,
Yingdi Yu64206112013-12-24 11:16:32 +0800355 this,
356 _1,
357 retry,
Yingdi Yu64206112013-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 Yu64206112013-12-24 11:16:32 +0800366 const OnVerified& onVerified,
Yingdi Yu7630f642014-01-16 19:13:03 -0800367 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu64206112013-12-24 11:16:32 +0800368{
Yingdi Yu7630f642014-01-16 19:13:03 -0800369 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu64206112013-12-24 11:16:32 +0800370}
371
372void
373ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
374 int retry,
Yingdi Yu64206112013-12-24 11:16:32 +0800375 const OnVerified& onVerified,
376 const OnVerifyFailed& onVerifyFailed,
377 const TimeoutNotify& timeoutNotify)
378{
379 if(retry > 0)
Yingdi Yu7630f642014-01-16 19:13:03 -0800380 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1);
Yingdi Yu64206112013-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 Yu64206112013-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 Yu53eb8a72013-10-23 11:50:51 -0700399{
Yingdi Yu42372442013-11-06 18:43:31 -0800400 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yuaccbda92013-12-27 08:44:12 +0800401
Yingdi Yub35b8652013-11-07 11:32:40 -0800402
Yingdi Yu64206112013-12-24 11:16:32 +0800403 shared_ptr<ChronosInvitation> invitation;
Yingdi Yub35b8652013-11-07 11:32:40 -0800404 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800405 invitation = make_shared<ChronosInvitation>(interest->getName());
406 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800407 _LOG_ERROR("Exception: " << e.what());
408 return;
409 }
410
Yingdi Yu42372442013-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 Yub35b8652013-11-07 11:32:40 -0800416 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu42372442013-11-06 18:43:31 -0800417 return;
418 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700419
Yingdi Yu6df61252014-01-21 11:05:11 -0800420 const SignatureSha256WithRsa& invitationSig = invitation->getSignature();
Yingdi Yueaa84e22014-01-16 10:30:26 -0800421 shared_ptr<PublicKey> keyPtr = m_policy->getTrustedKey(invitation->getInviterCertificateName());
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800422
Yingdi Yueaa84e22014-01-16 10:30:26 -0800423 if(static_cast<bool>(keyPtr) && Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, *keyPtr))
Yingdi Yu6b56f092013-11-10 11:54:02 -0800424 {
Yingdi Yu64206112013-12-24 11:16:32 +0800425 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu6df61252014-01-21 11:05:11 -0800426 // hack: incomplete certificate, we don't send it to the wire nor store it anywhere, we only use it to pass information
Yingdi Yu6b56f092013-11-10 11:54:02 -0800427 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu9f657242013-11-10 12:25:43 -0800428 bool findCert = false;
Yingdi Yu64206112013-12-24 11:16:32 +0800429 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu9f657242013-11-10 12:25:43 -0800430 for(; it != m_contactList.end(); it++)
431 {
432 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
433 {
434 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
435 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
436 findCert = true;
437 break;
438 }
439 }
440 if(findCert == false)
441 {
442 _LOG_ERROR("No SelfEndorseCertificate found!");
443 return;
444 }
Yingdi Yu6b56f092013-11-10 11:54:02 -0800445 certificate->setPublicKeyInfo(*keyPtr);
446 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
447 return;
448 }
449
Yingdi Yuaccbda92013-12-27 08:44:12 +0800450 _LOG_DEBUG("Cannot find the inviter's key in trust anchors");
451
Yingdi Yu64206112013-12-24 11:16:32 +0800452 Interest newInterest(invitation->getInviterCertificateName());
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800453 OnVerified onVerified = func_lib::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
454 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
455 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
Yingdi Yu64206112013-12-24 11:16:32 +0800456
457 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700458}
459
460void
Yingdi Yu64206112013-12-24 11:16:32 +0800461ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
462 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu42372442013-11-06 18:43:31 -0800463{
Yingdi Yu64206112013-12-24 11:16:32 +0800464 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800465
Yingdi Yu6df61252014-01-21 11:05:11 -0800466 SignatureSha256WithRsa invitationSig(invitation->getSignature());
Yingdi Yu42372442013-11-06 18:43:31 -0800467
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800468 if(Verifier::verifySignature(invitation->getSignedBlob(), invitationSig, certificate->getPublicKeyInfo()))
Yingdi Yu42372442013-11-06 18:43:31 -0800469 {
470 Name keyName = certificate->getPublicKeyName();
Yingdi Yu64206112013-12-24 11:16:32 +0800471 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu42372442013-11-06 18:43:31 -0800472 popChatInvitation(invitation, inviterNameSpace, certificate);
473 }
474}
475
476void
Yingdi Yu64206112013-12-24 11:16:32 +0800477ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
478{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu42372442013-11-06 18:43:31 -0800479
480void
Yingdi Yu64206112013-12-24 11:16:32 +0800481ContactPanel::onInvitationCertTimeoutNotify()
482{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu42372442013-11-06 18:43:31 -0800483
484void
Yingdi Yu64206112013-12-24 11:16:32 +0800485ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu42372442013-11-06 18:43:31 -0800486 const Name& inviterNameSpace,
Yingdi Yu64206112013-12-24 11:16:32 +0800487 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu42372442013-11-06 18:43:31 -0800488{
489 string alias;
Yingdi Yu64206112013-12-24 11:16:32 +0800490 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu42372442013-11-06 18:43:31 -0800491 for(; it != m_contactList.end(); it++)
492 if((*it)->getNameSpace() == inviterNameSpace)
493 alias = (*it)->getAlias();
494
495 if(it != m_contactList.end())
496 return;
497
498 m_invitationDialog->setInvitation(alias, invitation, certificate);
499 emit newInvitationReady();
500}
501
502void
503ContactPanel::collectEndorsement()
504{
Yingdi Yu64206112013-12-24 11:16:32 +0800505 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu42372442013-11-06 18:43:31 -0800506 m_collectStatus->assign(m_contactList.size(), false);
507
Yingdi Yu64206112013-12-24 11:16:32 +0800508 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu42372442013-11-06 18:43:31 -0800509 int count = 0;
510 for(; it != m_contactList.end(); it++, count++)
511 {
512 Name interestName = (*it)->getNameSpace();
513 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu64206112013-12-24 11:16:32 +0800514 Interest interest(interestName);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800515 interest.setInterestLifetime(1000);
Yingdi Yu42372442013-11-06 18:43:31 -0800516
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800517 OnVerified onVerified = func_lib::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
518 OnVerifyFailed onVerifyFailed = func_lib::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
519 TimeoutNotify timeoutNotify = func_lib::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
Yingdi Yu64206112013-12-24 11:16:32 +0800520
521 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu42372442013-11-06 18:43:31 -0800522 }
523}
524
525void
Yingdi Yu64206112013-12-24 11:16:32 +0800526ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800527{
Yingdi Yu64206112013-12-24 11:16:32 +0800528 Data endorseData;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800529 endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu64206112013-12-24 11:16:32 +0800530 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu42372442013-11-06 18:43:31 -0800531
532 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
533
534 updateCollectStatus(count);
535}
536
537void
Yingdi Yu64206112013-12-24 11:16:32 +0800538ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800539{ updateCollectStatus(count); }
540
541void
Yingdi Yu64206112013-12-24 11:16:32 +0800542ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800543{ updateCollectStatus(count); }
544
545void
546ContactPanel::updateCollectStatus(int count)
547{
548 m_collectStatus->at(count) = true;
549 vector<bool>::const_iterator it = m_collectStatus->begin();
550 for(; it != m_collectStatus->end(); it++)
551 if(*it == false)
552 return;
553
554 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
555}
556
557static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
558
559string
560ContactPanel::getRandomString()
561{
562 string randStr;
563 boost::random::random_device rng;
564 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
565 for (int i = 0; i < 10; i ++)
566 {
567 randStr += chars[index_dist(rng)];
568 }
569 return randStr;
570}
571
572void
Yingdi Yub29f78c2013-11-09 20:12:31 -0800573ContactPanel::showError(const QString& msg){
574 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
575 exit(1);
576}
577
578void
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800579ContactPanel::showWarning(const QString& msg){
580 QMessageBox::information(this, tr("Chronos"), msg);
581}
582
583void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700584ContactPanel::updateSelection(const QItemSelection &selected,
585 const QItemSelection &deselected)
586{
587 QModelIndexList items = selected.indexes();
588 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu42372442013-11-06 18:43:31 -0800589 string alias = text.toStdString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700590
Yingdi Yu79c25a22013-10-21 13:38:38 -0700591 int i = 0;
592 for(; i < m_contactList.size(); i++)
593 {
594 if(alias == m_contactList[i]->getAlias())
595 break;
596 }
597
Yingdi Yua50c3252013-11-03 15:05:26 -0800598 m_currentSelectedContact = m_contactList[i];
Yingdi Yua0594092013-11-06 22:07:38 -0800599 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
600 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
601 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700602
Yingdi Yua50c3252013-11-03 15:05:26 -0800603 if(m_currentSelectedContact->isIntroducer())
604 {
605 ui->isIntroducer->setChecked(true);
606 ui->addScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800607 ui->deleteScope->setEnabled(true);
608 ui->trustScopeList->setEnabled(true);
609
610 string filter("contact_namespace = '");
611 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
612
613 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
614 m_trustScopeModel->setTable("TrustScope");
615 m_trustScopeModel->setFilter(filter.c_str());
616 m_trustScopeModel->select();
617 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
618 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
619 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
620
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800621 ui->trustScopeList->setModel(m_trustScopeModel);
622 ui->trustScopeList->setColumnHidden(0, true);
623 ui->trustScopeList->setColumnHidden(1, true);
624 ui->trustScopeList->show();
Yingdi Yua50c3252013-11-03 15:05:26 -0800625 }
626 else
627 {
628 ui->isIntroducer->setChecked(false);
629 ui->addScope->setEnabled(false);
630 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800631
632 string filter("contact_namespace = '");
633 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
634
635 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
636 m_trustScopeModel->setTable("TrustScope");
637 m_trustScopeModel->setFilter(filter.c_str());
638 m_trustScopeModel->select();
639 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
640 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
641 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
642
643 ui->trustScopeList->setModel(m_trustScopeModel);
644 ui->trustScopeList->setColumnHidden(0, true);
645 ui->trustScopeList->setColumnHidden(1, true);
646 ui->trustScopeList->show();
647
648 ui->trustScopeList->setEnabled(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800649 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800650
651 string filter("profile_identity = '");
652 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
653
654 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
655 m_endorseDataModel->setTable("ContactProfile");
656 m_endorseDataModel->setFilter(filter.c_str());
657 m_endorseDataModel->select();
658
659 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
660 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
661 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
662 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
663
664 ui->endorseList->setModel(m_endorseDataModel);
665 ui->endorseList->setColumnHidden(0, true);
666 ui->endorseList->resizeColumnToContents(1);
667 ui->endorseList->resizeColumnToContents(2);
668 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
669 ui->endorseList->show();
Yingdi Yu01a942b2013-10-10 15:00:58 -0700670}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700671
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700672void
Yingdi Yu42372442013-11-06 18:43:31 -0800673ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700674{
Yingdi Yu71ec9652013-11-09 22:18:26 -0800675 // _LOG_DEBUG(identity.toStdString());
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800676 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800677 Name defaultCertName = m_keyChain->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu71ec9652013-11-09 22:18:26 -0800678 if(defaultCertName.size() == 0)
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800679 {
680 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
681 return;
682 }
683 m_defaultIdentity = defaultIdentity;
Yingdi Yub35b8652013-11-07 11:32:40 -0800684 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800685 m_nickName = nickName.toStdString();
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800686 m_face->unsetInterestFilter(m_invitationListenerId);
Yingdi Yu908f8412013-11-09 00:03:26 -0800687 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800688 setInvitationListener();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800689 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700690}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700691
692void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700693ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700694{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700695
Yingdi Yud59b5932013-10-16 13:16:49 -0700696void
697ContactPanel::openAddContactPanel()
698{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700699
Yingdi Yu79c25a22013-10-21 13:38:38 -0700700void
Yingdi Yu908f8412013-11-09 00:03:26 -0800701ContactPanel::openBrowseContactDialog()
702{
703 m_browseContactDialog->show();
704 emit refreshCertDirectory();
705}
706
707void
708ContactPanel::removeContactButton()
709{
710 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
711 QModelIndexList selectedList = selectionModel->selectedIndexes();
712 QModelIndexList::iterator it = selectedList.begin();
713 for(; it != selectedList.end(); it++)
714 {
715 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu64206112013-12-24 11:16:32 +0800716 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yu908f8412013-11-09 00:03:26 -0800717 for(; contactIt != m_contactList.end(); contactIt++)
718 {
719 if((*contactIt)->getAlias() == alias)
720 {
721 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
722 m_contactList.erase(contactIt);
723 break;
724 }
725 }
726 }
727 refreshContactList();
728}
729
730void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700731ContactPanel::openInvitationDialog()
732{ m_invitationDialog->show(); }
733
734void
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800735ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
736{
Yingdi Yu64206112013-12-24 11:16:32 +0800737 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
Yingdi Yueaa84e22014-01-16 10:30:26 -0800738 if(static_cast<bool>(contact))
739 m_policy->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800740}
741
742void
743ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yueaa84e22014-01-16 10:30:26 -0800744{ m_policy->removeTrustAnchor(keyName); }
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800745
746void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700747ContactPanel::refreshContactList()
748{
Yingdi Yu64206112013-12-24 11:16:32 +0800749 m_contactList.clear();
750 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700751 QStringList contactNameList;
752 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yua0594092013-11-06 22:07:38 -0800753 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700754
755 m_contactListModel->setStringList(contactNameList);
756}
757
758void
759ContactPanel::showContextMenu(const QPoint& pos)
760{
761 QMenu menu(ui->ContactList);
762 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700763 connect(m_menuInvite, SIGNAL(triggered()),
764 this, SLOT(openStartChatDialog()));
Yingdi Yua0594092013-11-06 22:07:38 -0800765 menu.addSeparator();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700766 menu.addAction(m_menuAlias);
767 connect(m_menuAlias, SIGNAL(triggered()),
768 this, SLOT(openSetAliasDialog()));
769 menu.exec(ui->ContactList->mapToGlobal(pos));
770
771}
772
773void
774ContactPanel::openSetAliasDialog()
775{
Yingdi Yua50c3252013-11-03 15:05:26 -0800776 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700777 m_setAliasDialog->show();
778}
779
Yingdi Yu100267f2013-10-21 15:01:40 -0700780void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700781ContactPanel::openSettingDialog()
782{
Yingdi Yu42372442013-11-06 18:43:31 -0800783 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700784 m_settingDialog->show();
785}
786
787void
Yingdi Yu100267f2013-10-21 15:01:40 -0700788ContactPanel::openStartChatDialog()
789{
Yingdi Yu100267f2013-10-21 15:01:40 -0700790 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700791 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700792
Yingdi Yua50c3252013-11-03 15:05:26 -0800793 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu100267f2013-10-21 15:01:40 -0700794 m_startChatDialog->show();
795}
796
Yingdi Yu42f66462013-10-31 17:38:22 -0700797// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700798void
799ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
800{
Yingdi Yu64206112013-12-24 11:16:32 +0800801 Name chatroomName(chatroom.toStdString());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800802
803 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu64206112013-12-24 11:16:32 +0800804 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800805
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800806 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700807 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu42372442013-11-06 18:43:31 -0800808
809 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
810 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -0800811 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
812 this, SLOT(showError(const QString&)));
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800813 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
814 this, SLOT(showWarning(const QString&)));
Yingdi Yu42372442013-11-06 18:43:31 -0800815
Yingdi Yuc90deb12013-11-06 18:51:19 -0800816 // send invitation
Yingdi Yu42f66462013-10-31 17:38:22 -0700817 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700818
819 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700820}
821
Yingdi Yu42f66462013-10-31 17:38:22 -0700822// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700823void
Yingdi Yu42f66462013-10-31 17:38:22 -0700824ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu64206112013-12-24 11:16:32 +0800825 const IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700826{
Yingdi Yu64206112013-12-24 11:16:32 +0800827 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800828
Yingdi Yu42f66462013-10-31 17:38:22 -0700829 Name chatroomName("/ndn/broadcast/chronos");
830 chatroomName.append(invitation.getChatroom());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800831
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800832 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800833
Yingdi Yu42372442013-11-06 18:43:31 -0800834 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
835 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -0800836 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
837 this, SLOT(showError(const QString&)));
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800838 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
839 this, SLOT(showWarning(const QString&)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700840
Yingdi Yu6df61252014-01-21 11:05:11 -0800841 chatDialog->addChatDataRule(invitation.getInviterRoutingPrefix(), identityCertificate, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800842 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu42f66462013-10-31 17:38:22 -0700843
Yingdi Yu42f66462013-10-31 17:38:22 -0700844 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
845
846 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
847
848 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700849}
850
851void
Yingdi Yu42f66462013-10-31 17:38:22 -0700852ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu64206112013-12-24 11:16:32 +0800853 const IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700854{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800855 Name dataName = invitation.getInterestName();
856 time_t nowSeconds = time(NULL);
857 struct tm current = *gmtime(&nowSeconds);
858 MillisecondsSince1970 version = timegm(&current) * 1000.0;
859 dataName.appendVersion(version);
860 Data data(dataName);
Yingdi Yu64206112013-12-24 11:16:32 +0800861 string content = m_localPrefix.toUri();
862 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu64206112013-12-24 11:16:32 +0800863
864 Name certificateName;
Yingdi Yueaa84e22014-01-16 10:30:26 -0800865 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu64206112013-12-24 11:16:32 +0800866
867 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800868 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu64206112013-12-24 11:16:32 +0800869 else
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800870 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
871 m_keyChain->sign(data, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800872
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800873 m_face->put(data);
Yingdi Yu64206112013-12-24 11:16:32 +0800874
Yingdi Yu42f66462013-10-31 17:38:22 -0700875 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700876}
877
878void
Yingdi Yu42f66462013-10-31 17:38:22 -0700879ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700880{
Yingdi Yu64206112013-12-24 11:16:32 +0800881 Data data(invitation.getInterestName());
882 string content("nack");
883 data.setContent((const uint8_t *)&content[0], content.size());
Yingdi Yu64206112013-12-24 11:16:32 +0800884
885 Name certificateName;
Yingdi Yueaa84e22014-01-16 10:30:26 -0800886 Name inferredIdentity = m_policy->inferSigningIdentity(data.getName());
Yingdi Yu64206112013-12-24 11:16:32 +0800887 if(inferredIdentity.getComponentCount() == 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800888 certificateName = m_keyChain->getDefaultCertificateName();
Yingdi Yu64206112013-12-24 11:16:32 +0800889 else
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800890 certificateName = m_keyChain->getDefaultCertificateNameForIdentity(inferredIdentity);
891 m_keyChain->sign(data, certificateName);
Yingdi Yu64206112013-12-24 11:16:32 +0800892
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800893 m_face->put(data);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700894}
895
Yingdi Yua50c3252013-11-03 15:05:26 -0800896void
897ContactPanel::isIntroducerChanged(int state)
898{
899 if(state == Qt::Checked)
900 {
901 ui->addScope->setEnabled(true);
902 ui->deleteScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800903 ui->trustScopeList->setEnabled(true);
904
905 string filter("contact_namespace = '");
906 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800907
908 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
909 m_trustScopeModel->setTable("TrustScope");
910 m_trustScopeModel->setFilter(filter.c_str());
911 m_trustScopeModel->select();
912 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
913 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
914 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800915
916 ui->trustScopeList->setModel(m_trustScopeModel);
917 ui->trustScopeList->setColumnHidden(0, true);
918 ui->trustScopeList->setColumnHidden(1, true);
919 ui->trustScopeList->show();
920
921 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yua50c3252013-11-03 15:05:26 -0800922 }
923 else
924 {
Yingdi Yua50c3252013-11-03 15:05:26 -0800925 ui->addScope->setEnabled(false);
926 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800927
928 string filter("contact_namespace = '");
929 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
930
931 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
932 m_trustScopeModel->setTable("TrustScope");
933 m_trustScopeModel->setFilter(filter.c_str());
934 m_trustScopeModel->select();
935 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
936 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
937 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
938
939 ui->trustScopeList->setModel(m_trustScopeModel);
940 ui->trustScopeList->setColumnHidden(0, true);
941 ui->trustScopeList->setColumnHidden(1, true);
942 ui->trustScopeList->show();
943
944 ui->trustScopeList->setEnabled(false);
945
946 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800947 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800948 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
949}
950
951void
952ContactPanel::addScopeClicked()
953{
954 int rowCount = m_trustScopeModel->rowCount();
955 QSqlRecord record;
956 QSqlField identityField("contact_namespace", QVariant::String);
957 record.append(identityField);
Yingdi Yu64206112013-12-24 11:16:32 +0800958 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800959 m_trustScopeModel->insertRow(rowCount);
960 m_trustScopeModel->setRecord(rowCount, record);
961}
962
963void
964ContactPanel::deleteScopeClicked()
965{
966 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
967 QModelIndexList indexList = selectionModel->selectedIndexes();
968
969 int i = indexList.size() - 1;
970 for(; i >= 0; i--)
971 m_trustScopeModel->removeRow(indexList[i].row());
972
973 m_trustScopeModel->submitAll();
974}
975
976void
977ContactPanel::saveScopeClicked()
Yingdi Yua0594092013-11-06 22:07:38 -0800978{ m_trustScopeModel->submitAll(); }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800979
980void
981ContactPanel::endorseButtonClicked()
982{
983 m_endorseDataModel->submitAll();
984 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yua50c3252013-11-03 15:05:26 -0800985}
Yingdi Yu100267f2013-10-21 15:01:40 -0700986
Yingdi Yu42372442013-11-06 18:43:31 -0800987void
988ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
989{
990 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yua0594092013-11-06 22:07:38 -0800991
992 ChatDialog* deletedChat = NULL;
Yingdi Yu42372442013-11-06 18:43:31 -0800993 if(it != m_chatDialogs.end())
994 {
Yingdi Yu42372442013-11-06 18:43:31 -0800995 deletedChat = it->second;
996 m_chatDialogs.erase(it);
997 }
Yingdi Yua0594092013-11-06 22:07:38 -0800998 if (deletedChat != NULL)
999 delete deletedChat;
Yingdi Yu42372442013-11-06 18:43:31 -08001000}
1001
Yingdi Yu9e0dc292013-10-10 11:39:45 -07001002#if WAF
1003#include "contactpanel.moc"
1004#include "contactpanel.cpp.moc"
1005#endif