blob: d9f3324f415460acbcda8803ab75cead2683c119 [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 Yu64206112013-12-24 11:16:32 +080024#include <ndn-cpp/security/identity/osx-private-key-storage.hpp>
25#include <ndn-cpp/security/identity/basic-identity-storage.hpp>
26#include <ndn-cpp/security/signature/sha256-with-rsa-handler.hpp>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070027#include <boost/filesystem.hpp>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070028#include <boost/random/random_device.hpp>
29#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu42f66462013-10-31 17:38:22 -070030#include "panel-policy-manager.h"
Yingdi Yu64206112013-12-24 11:16:32 +080031#include "null-ptrs.h"
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070032#include "logging.h"
33#include "exception.h"
34#endif
35
36namespace fs = boost::filesystem;
37using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080038using namespace ndn::ptr_lib;
Yingdi Yu53eb8a72013-10-23 11:50:51 -070039
Yingdi Yu79c25a22013-10-21 13:38:38 -070040using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070041
42INIT_LOGGER("ContactPanel");
43
Yingdi Yu64206112013-12-24 11:16:32 +080044Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu42f66462013-10-31 17:38:22 -070045Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070046
Yingdi Yub29f78c2013-11-09 20:12:31 -080047ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yua50c3252013-11-03 15:05:26 -080048 : QDialog(parent)
49 , ui(new Ui::ContactPanel)
Yingdi Yub29f78c2013-11-09 20:12:31 -080050 , m_warningDialog(new WarningDialog)
Yingdi Yua50c3252013-11-03 15:05:26 -080051 , m_contactListModel(new QStringListModel)
52 , m_startChatDialog(new StartChatDialog)
53 , m_invitationDialog(new InvitationDialog)
54 , m_settingDialog(new SettingDialog)
Yingdi Yu64206112013-12-24 11:16:32 +080055 , m_policyManager(new PanelPolicyManager())
Yingdi Yudef90612013-10-09 22:34:42 -070056{
Yingdi Yu64206112013-12-24 11:16:32 +080057 qRegisterMetaType<IdentityCertificate>("IdentityCertificate");
Yingdi Yu42f66462013-10-31 17:38:22 -070058 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yuaccbda92013-12-27 08:44:12 +080059
60 startFace();
61
Yingdi Yu42372442013-11-06 18:43:31 -080062 createAction();
63
Yingdi Yu64206112013-12-24 11:16:32 +080064 shared_ptr<BasicIdentityStorage> publicStorage = make_shared<BasicIdentityStorage>();
65 shared_ptr<OSXPrivateKeyStorage> privateStorage = make_shared<OSXPrivateKeyStorage>();
66 m_identityManager = make_shared<IdentityManager>(publicStorage, privateStorage);
67
Yingdi Yuaccbda92013-12-27 08:44:12 +080068 m_contactManager = make_shared<ContactManager>(m_identityManager, m_face, m_transport);
Yingdi Yub35b8652013-11-07 11:32:40 -080069
Yingdi Yub29f78c2013-11-09 20:12:31 -080070 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
71 this, SLOT(showError(const QString&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -080072
Yingdi Yub35b8652013-11-07 11:32:40 -080073 openDB();
Yingdi Yu42372442013-11-06 18:43:31 -080074
Yingdi Yuc4d08d22013-10-23 23:07:29 -070075 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070076
Yingdi Yu64206112013-12-24 11:16:32 +080077 loadTrustAnchor();
Yingdi Yu42372442013-11-06 18:43:31 -080078
Yingdi Yu64206112013-12-24 11:16:32 +080079 m_defaultIdentity = m_identityManager->getDefaultIdentity();
Yingdi Yu10436892013-11-10 16:26:52 -080080 if(m_defaultIdentity.size() == 0)
81 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yu64206112013-12-24 11:16:32 +080082 Name defaultCertName = m_identityManager->getDefaultCertificateNameForIdentity(m_defaultIdentity);
Yingdi Yu71ec9652013-11-09 22:18:26 -080083 if(defaultCertName.size() == 0)
Yingdi Yucf1bdcb2013-11-10 10:54:16 -080084 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yu10436892013-11-10 16:26:52 -080085
86
87 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu64206112013-12-24 11:16:32 +080088 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yu10436892013-11-10 16:26:52 -080089 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu71ec9652013-11-09 22:18:26 -080090
Yingdi Yu42372442013-11-06 18:43:31 -080091
Yingdi Yub35b8652013-11-07 11:32:40 -080092 m_profileEditor = new ProfileEditor(m_contactManager);
93 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
94
Yingdi Yub29f78c2013-11-09 20:12:31 -080095 m_addContactPanel = new AddContactPanel(m_contactManager);
96 m_browseContactDialog = new BrowseContactDialog(m_contactManager);
97 m_setAliasDialog = new SetAliasDialog(m_contactManager);
Yingdi Yub35b8652013-11-07 11:32:40 -080098
99 ui->setupUi(this);
100
Yingdi Yub29f78c2013-11-09 20:12:31 -0800101
Yingdi Yu0a953c32013-11-10 10:32:18 -0800102 m_localPrefix = Name("/private/local");
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700103 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700104
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700105 setInvitationListener();
Yingdi Yu42372442013-11-06 18:43:31 -0800106
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800107 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700108
109 ui->ContactList->setModel(m_contactListModel);
110
Yingdi Yuc26af3c2013-10-17 17:03:46 -0700111
Yingdi Yu42372442013-11-06 18:43:31 -0800112 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700113 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -0700114
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700115 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
116 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -0700117
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700118 connect(ui->EditProfileButton, SIGNAL(clicked()),
119 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -0700120
Yingdi Yu71ec9652013-11-09 22:18:26 -0800121 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800122 this, SLOT(showWarning(const QString&)));
Yingdi Yu71ec9652013-11-09 22:18:26 -0800123
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700124 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yu908f8412013-11-09 00:03:26 -0800125 this, SLOT(openBrowseContactDialog()));
126
Yingdi Yu3cd30df2013-11-10 17:59:58 -0800127 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
128 this, SLOT(openAddContactPanel()));
129
Yingdi Yu908f8412013-11-09 00:03:26 -0800130 connect(this, SIGNAL(refreshCertDirectory()),
131 m_browseContactDialog, SLOT(refreshList()));
132
133 connect(ui->DeleteContactButton, SIGNAL(clicked()),
134 this, SLOT(removeContactButton()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700135
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700136 connect(ui->settingButton, SIGNAL(clicked()),
137 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700138
Yingdi Yu3cd30df2013-11-10 17:59:58 -0800139 connect(m_addContactPanel, SIGNAL(newContactAdded()),
140 this, SLOT(refreshContactList()));
Yingdi Yu908f8412013-11-09 00:03:26 -0800141 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700142 this, SLOT(refreshContactList()));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700143 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
144 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700145
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700146 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
147 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700148
Yingdi Yu64206112013-12-24 11:16:32 +0800149 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::IdentityCertificate&)),
150 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::IdentityCertificate&)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700151 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
152 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800153
154 connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)),
155 this, SLOT(addContactIntoPanelPolicy(const ndn::Name&)));
156 connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)),
157 this, SLOT(removeContactFromPanelPolicy(const ndn::Name&)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700158
Yingdi Yu42372442013-11-06 18:43:31 -0800159 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
160 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700161
162 connect(this, SIGNAL(newInvitationReady()),
163 this, SLOT(openInvitationDialog()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700164
Yingdi Yua50c3252013-11-03 15:05:26 -0800165 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
166 this, SLOT(isIntroducerChanged(int)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800167
168 connect(ui->addScope, SIGNAL(clicked()),
169 this, SLOT(addScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800170 connect(ui->deleteScope, SIGNAL(clicked()),
171 this, SLOT(deleteScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800172 connect(ui->saveButton, SIGNAL(clicked()),
173 this, SLOT(saveScopeClicked()));
174
175 connect(ui->endorseButton, SIGNAL(clicked()),
176 this, SLOT(endorseButtonClicked()));
Yingdi Yudef90612013-10-09 22:34:42 -0700177}
178
179ContactPanel::~ContactPanel()
180{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700181 delete ui;
182 delete m_contactListModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800183 delete m_startChatDialog;
184 delete m_invitationDialog;
185 delete m_settingDialog;
186
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700187 delete m_profileEditor;
188 delete m_addContactPanel;
Yingdi Yu908f8412013-11-09 00:03:26 -0800189 delete m_browseContactDialog;
Yingdi Yu42372442013-11-06 18:43:31 -0800190 delete m_setAliasDialog;
191
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800192 delete m_trustScopeModel;
193 delete m_endorseDataModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800194 delete m_endorseComboBoxDelegate;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700195
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700196 delete m_menuInvite;
Yingdi Yu42372442013-11-06 18:43:31 -0800197 delete m_menuAlias;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700198
199 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
200 for(; it != m_chatDialogs.end(); it++)
201 delete it->second;
Yingdi Yuaccbda92013-12-27 08:44:12 +0800202
203 shutdownFace();
204}
205
206void
207ContactPanel::startFace()
208{
209 m_transport = make_shared<TcpTransport>();
210 m_face = make_shared<Face>(m_transport, make_shared<TcpTransport::ConnectionInfo>("localhost"));
211
212 connectToDaemon();
213
214 m_running = true;
215 m_thread = boost::thread (&ContactPanel::eventLoop, this);
216}
217
218void
219ContactPanel::shutdownFace()
220{
221 {
222 boost::unique_lock<boost::recursive_mutex> lock(m_mutex);
223 m_running = false;
224 }
225
226 m_thread.join();
227 m_face->shutdown();
228}
229
230void
231ContactPanel::eventLoop()
232{
233 while (m_running)
234 {
235 try{
236 m_face->processEvents();
237 usleep(100);
238 }catch(std::exception& e){
239 _LOG_DEBUG(" " << e.what() );
240 }
241 }
Yingdi Yudef90612013-10-09 22:34:42 -0700242}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700243
Yingdi Yu01a942b2013-10-10 15:00:58 -0700244void
Yingdi Yu64206112013-12-24 11:16:32 +0800245ContactPanel::connectToDaemon()
246{
247 //Hack! transport does not connect to daemon unless an interest is expressed.
248 Name name("/ndn");
249 ndn::Interest interest(name);
250 m_face->expressInterest(interest,
251 bind(&ContactPanel::onConnectionData, this, _1, _2),
252 bind(&ContactPanel::onConnectionDataTimeout, this, _1));
253}
254
255void
256ContactPanel::onConnectionData(const shared_ptr<const ndn::Interest>& interest,
257 const shared_ptr<Data>& data)
258{ _LOG_DEBUG("onConnectionData"); }
259
260void
261ContactPanel::onConnectionDataTimeout(const shared_ptr<const ndn::Interest>& interest)
262{ _LOG_DEBUG("onConnectionDataTimeout"); }
263
264void
Yingdi Yu42372442013-11-06 18:43:31 -0800265ContactPanel::createAction()
266{
267 m_menuInvite = new QAction("&Chat", this);
268 m_menuAlias = new QAction("&Set Alias", this);
269}
270
271void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700272ContactPanel::openDB()
273{
274 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
275 QString path = (QDir::home().path());
276 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
277 db.setDatabaseName(path);
278 bool ok = db.open();
Yingdi Yu42372442013-11-06 18:43:31 -0800279 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800280
281 m_trustScopeModel = new QSqlTableModel;
282 m_endorseDataModel = new QSqlTableModel;
283 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700284}
285
286void
Yingdi Yu64206112013-12-24 11:16:32 +0800287ContactPanel::loadTrustAnchor()
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700288{
Yingdi Yu64206112013-12-24 11:16:32 +0800289 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700290 for(; it != m_contactList.end(); it++)
Yingdi Yub35b8652013-11-07 11:32:40 -0800291 {
Yingdi Yuaccbda92013-12-27 08:44:12 +0800292 _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri());
Yingdi Yu64206112013-12-24 11:16:32 +0800293 m_policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yub35b8652013-11-07 11:32:40 -0800294 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700295}
296
297void
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800298ContactPanel::setLocalPrefix(int retry)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700299{
Yingdi Yu64206112013-12-24 11:16:32 +0800300 Name interestName("/local/ndn/prefix");
301 Interest interest(interestName);
302 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700303
Yingdi Yu64206112013-12-24 11:16:32 +0800304 m_face->expressInterest(interest,
305 bind(&ContactPanel::onLocalPrefix, this, _1, _2),
306 bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
307
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700308}
309
310void
Yingdi Yu64206112013-12-24 11:16:32 +0800311ContactPanel::onLocalPrefix(const shared_ptr<const Interest>& interest,
312 const shared_ptr<Data>& data)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700313{
Yingdi Yu64206112013-12-24 11:16:32 +0800314 string originPrefix((const char*)data->getContent().buf(), data->getContent().size());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700315 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
316 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700317 m_localPrefix = Name(prefix);
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800318
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700319}
320
321void
Yingdi Yu64206112013-12-24 11:16:32 +0800322ContactPanel::onLocalPrefixTimeout(const shared_ptr<const Interest>& interest,
323 int retry)
Yingdi Yu42f66462013-10-31 17:38:22 -0700324{
Yingdi Yu2ab22e72013-11-10 01:38:21 -0800325 if(retry > 0)
Yingdi Yu0a953c32013-11-10 10:32:18 -0800326 {
327 setLocalPrefix(retry - 1);
328 return;
329 }
330 else{
331 m_localPrefix = Name("/private/local");
332 }
Yingdi Yu42f66462013-10-31 17:38:22 -0700333}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700334
335void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700336ContactPanel::setInvitationListener()
337{
Yingdi Yu42372442013-11-06 18:43:31 -0800338 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
339 m_inviteListenPrefix.append(m_defaultIdentity);
340 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
Yingdi Yu64206112013-12-24 11:16:32 +0800341 m_invitationListenerId = m_face->registerPrefix(m_inviteListenPrefix,
342 boost::bind(&ContactPanel::onInvitation, this, _1, _2, _3, _4),
343 boost::bind(&ContactPanel::onInvitationRegisterFailed, this, _1));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700344}
345
346void
Yingdi Yu64206112013-12-24 11:16:32 +0800347ContactPanel::sendInterest(const Interest& interest,
348 const OnVerified& onVerified,
349 const OnVerifyFailed& onVerifyFailed,
350 const TimeoutNotify& timeoutNotify,
351 int retry /* = 1 */,
352 int stepCount /* = 0 */)
353{
354 m_face->expressInterest(interest,
355 boost::bind(&ContactPanel::onTargetData,
356 this,
357 _1,
358 _2,
359 stepCount,
360 onVerified,
361 onVerifyFailed,
362 timeoutNotify),
363 boost::bind(&ContactPanel::onTargetTimeout,
364 this,
365 _1,
366 retry,
367 stepCount,
368 onVerified,
369 onVerifyFailed,
370 timeoutNotify));
371}
372
373void
374ContactPanel::onTargetData(const shared_ptr<const ndn::Interest>& interest,
375 const shared_ptr<Data>& data,
376 int stepCount,
377 const OnVerified& onVerified,
378 const OnVerifyFailed& onVerifyFailed,
379 const TimeoutNotify& timeoutNotify)
380{
381 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(data, stepCount, onVerified, onVerifyFailed);
382
383 if (nextStep)
384 m_face->expressInterest
385 (*nextStep->interest_,
386 bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
387 bind(&ContactPanel::onCertTimeout, this, _1, onVerifyFailed, data, nextStep));
388
389}
390
391void
392ContactPanel::onTargetTimeout(const shared_ptr<const ndn::Interest>& interest,
393 int retry,
394 int stepCount,
395 const OnVerified& onVerified,
396 const OnVerifyFailed& onVerifyFailed,
397 const TimeoutNotify& timeoutNotify)
398{
399 if(retry > 0)
400 sendInterest(*interest, onVerified, onVerifyFailed, timeoutNotify, retry-1, stepCount);
401 else
402 {
403 _LOG_DEBUG("Interest: " << interest->getName().toUri() << " eventually times out!");
404 timeoutNotify();
405 }
406}
407
408void
409ContactPanel::onCertData(const shared_ptr<const ndn::Interest>& interest,
410 const shared_ptr<Data>& cert,
411 shared_ptr<ValidationRequest> previousStep)
412{
413 shared_ptr<ValidationRequest> nextStep = m_policyManager->checkVerificationPolicy(cert,
414 previousStep->stepCount_,
415 previousStep->onVerified_,
416 previousStep->onVerifyFailed_);
417
418 if (nextStep)
419 m_face->expressInterest
420 (*nextStep->interest_,
421 bind(&ContactPanel::onCertData, this, _1, _2, nextStep),
422 bind(&ContactPanel::onCertTimeout, this, _1, previousStep->onVerifyFailed_, cert, nextStep));
423}
424
425void
426ContactPanel::onCertTimeout(const shared_ptr<const ndn::Interest>& interest,
427 const OnVerifyFailed& onVerifyFailed,
428 const shared_ptr<Data>& data,
429 shared_ptr<ValidationRequest> nextStep)
430{
431 if(nextStep->retry_ > 0)
432 m_face->expressInterest(*interest,
433 bind(&ContactPanel::onCertData,
434 this,
435 _1,
436 _2,
437 nextStep),
438 bind(&ContactPanel::onCertTimeout,
439 this,
440 _1,
441 onVerifyFailed,
442 data,
443 nextStep));
444 else
445 onVerifyFailed(data);
446}
447
448void
449ContactPanel::onInvitationRegisterFailed(const shared_ptr<const Name>& prefix)
450{
451 showError(QString::fromStdString("Cannot register invitation listening prefix"));
452}
453
454void
455ContactPanel::onInvitation(const shared_ptr<const Name>& prefix,
456 const shared_ptr<const Interest>& interest,
457 Transport& transport,
458 uint64_t registeredPrefixId)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700459{
Yingdi Yu42372442013-11-06 18:43:31 -0800460 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yuaccbda92013-12-27 08:44:12 +0800461
Yingdi Yub35b8652013-11-07 11:32:40 -0800462
Yingdi Yu64206112013-12-24 11:16:32 +0800463 shared_ptr<ChronosInvitation> invitation;
Yingdi Yub35b8652013-11-07 11:32:40 -0800464 try{
Yingdi Yu64206112013-12-24 11:16:32 +0800465 invitation = make_shared<ChronosInvitation>(interest->getName());
466 }catch(std::exception& e){
Yingdi Yub35b8652013-11-07 11:32:40 -0800467 _LOG_ERROR("Exception: " << e.what());
468 return;
469 }
470
Yingdi Yu42372442013-11-06 18:43:31 -0800471 Name chatroomName("/ndn/broadcast/chronos");
472 chatroomName.append(invitation->getChatroom());
473 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
474 if(it != m_chatDialogs.end())
475 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800476 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu42372442013-11-06 18:43:31 -0800477 return;
478 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700479
Yingdi Yu64206112013-12-24 11:16:32 +0800480 shared_ptr<PublicKey> keyPtr = m_policyManager->getTrustedKey(invitation->getInviterCertificateName());
481 if(CHRONOCHAT_NULL_PUBLICKEY_PTR != keyPtr && Sha256WithRsaHandler::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), *keyPtr))
Yingdi Yu6b56f092013-11-10 11:54:02 -0800482 {
Yingdi Yu64206112013-12-24 11:16:32 +0800483 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
Yingdi Yu9f657242013-11-10 12:25:43 -0800484 // hack: incomplete certificate, we don't send it to the wire nor store it anywhere, we only use it to carry information
Yingdi Yu6b56f092013-11-10 11:54:02 -0800485 certificate->setName(invitation->getInviterCertificateName());
Yingdi Yu9f657242013-11-10 12:25:43 -0800486 bool findCert = false;
Yingdi Yu64206112013-12-24 11:16:32 +0800487 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu9f657242013-11-10 12:25:43 -0800488 for(; it != m_contactList.end(); it++)
489 {
490 if((*it)->getNameSpace() == invitation->getInviterNameSpace())
491 {
492 certificate->setNotBefore((*it)->getSelfEndorseCertificate().getNotBefore());
493 certificate->setNotAfter((*it)->getSelfEndorseCertificate().getNotAfter());
494 findCert = true;
495 break;
496 }
497 }
498 if(findCert == false)
499 {
500 _LOG_ERROR("No SelfEndorseCertificate found!");
501 return;
502 }
Yingdi Yu6b56f092013-11-10 11:54:02 -0800503 certificate->setPublicKeyInfo(*keyPtr);
504 popChatInvitation(invitation, invitation->getInviterNameSpace(), certificate);
505 return;
506 }
507
Yingdi Yuaccbda92013-12-27 08:44:12 +0800508 _LOG_DEBUG("Cannot find the inviter's key in trust anchors");
509
Yingdi Yu64206112013-12-24 11:16:32 +0800510 Interest newInterest(invitation->getInviterCertificateName());
511 OnVerified onVerified = boost::bind(&ContactPanel::onInvitationCertVerified, this, _1, invitation);
512 OnVerifyFailed onVerifyFailed = boost::bind(&ContactPanel::onInvitationCertVerifyFailed, this, _1);
513 TimeoutNotify timeoutNotify = boost::bind(&ContactPanel::onInvitationCertTimeoutNotify, this);
514
515 sendInterest(newInterest, onVerified, onVerifyFailed, timeoutNotify);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700516}
517
518void
Yingdi Yu64206112013-12-24 11:16:32 +0800519ContactPanel::onInvitationCertVerified(const shared_ptr<Data>& data,
520 shared_ptr<ChronosInvitation> invitation)
Yingdi Yu42372442013-11-06 18:43:31 -0800521{
Yingdi Yu64206112013-12-24 11:16:32 +0800522 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*data);
Yingdi Yu42372442013-11-06 18:43:31 -0800523
Yingdi Yu64206112013-12-24 11:16:32 +0800524 if(Sha256WithRsaHandler::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu42372442013-11-06 18:43:31 -0800525 {
526 Name keyName = certificate->getPublicKeyName();
Yingdi Yu64206112013-12-24 11:16:32 +0800527 Name inviterNameSpace = keyName.getPrefix(-1);
Yingdi Yu42372442013-11-06 18:43:31 -0800528 popChatInvitation(invitation, inviterNameSpace, certificate);
529 }
530}
531
532void
Yingdi Yu64206112013-12-24 11:16:32 +0800533ContactPanel::onInvitationCertVerifyFailed(const shared_ptr<Data>& data)
534{ _LOG_DEBUG("Cannot verify invitation certificate!"); }
Yingdi Yu42372442013-11-06 18:43:31 -0800535
536void
Yingdi Yu64206112013-12-24 11:16:32 +0800537ContactPanel::onInvitationCertTimeoutNotify()
538{ _LOG_DEBUG("interest for invitation certificate times out eventually!"); }
Yingdi Yu42372442013-11-06 18:43:31 -0800539
540void
Yingdi Yu64206112013-12-24 11:16:32 +0800541ContactPanel::popChatInvitation(shared_ptr<ChronosInvitation> invitation,
Yingdi Yu42372442013-11-06 18:43:31 -0800542 const Name& inviterNameSpace,
Yingdi Yu64206112013-12-24 11:16:32 +0800543 shared_ptr<IdentityCertificate> certificate)
Yingdi Yu42372442013-11-06 18:43:31 -0800544{
545 string alias;
Yingdi Yu64206112013-12-24 11:16:32 +0800546 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu42372442013-11-06 18:43:31 -0800547 for(; it != m_contactList.end(); it++)
548 if((*it)->getNameSpace() == inviterNameSpace)
549 alias = (*it)->getAlias();
550
551 if(it != m_contactList.end())
552 return;
553
554 m_invitationDialog->setInvitation(alias, invitation, certificate);
555 emit newInvitationReady();
556}
557
558void
559ContactPanel::collectEndorsement()
560{
Yingdi Yu64206112013-12-24 11:16:32 +0800561 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu42372442013-11-06 18:43:31 -0800562 m_collectStatus->assign(m_contactList.size(), false);
563
Yingdi Yu64206112013-12-24 11:16:32 +0800564 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu42372442013-11-06 18:43:31 -0800565 int count = 0;
566 for(; it != m_contactList.end(); it++, count++)
567 {
568 Name interestName = (*it)->getNameSpace();
569 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu64206112013-12-24 11:16:32 +0800570 Interest interest(interestName);
571 interest.setChildSelector(ndn_Interest_CHILD_SELECTOR_RIGHT);
572 interest.setInterestLifetimeMilliseconds(1000);
Yingdi Yu42372442013-11-06 18:43:31 -0800573
Yingdi Yu64206112013-12-24 11:16:32 +0800574 OnVerified onVerified = boost::bind(&ContactPanel::onDnsEndorseeVerified, this, _1, count);
575 OnVerifyFailed onVerifyFailed = boost::bind(&ContactPanel::onDnsEndorseeVerifyFailed, this, _1, count);
576 TimeoutNotify timeoutNotify = boost::bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
577
578 sendInterest(interest, onVerified, onVerifyFailed, timeoutNotify, 0);
Yingdi Yu42372442013-11-06 18:43:31 -0800579 }
580}
581
582void
Yingdi Yu64206112013-12-24 11:16:32 +0800583ContactPanel::onDnsEndorseeVerified(const shared_ptr<Data>& data, int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800584{
Yingdi Yu64206112013-12-24 11:16:32 +0800585 Data endorseData;
586 endorseData.wireDecode(data->getContent().buf(), data->getContent().size());
587 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu42372442013-11-06 18:43:31 -0800588
589 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
590
591 updateCollectStatus(count);
592}
593
594void
Yingdi Yu64206112013-12-24 11:16:32 +0800595ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800596{ updateCollectStatus(count); }
597
598void
Yingdi Yu64206112013-12-24 11:16:32 +0800599ContactPanel::onDnsEndorseeVerifyFailed(const shared_ptr<Data>& data, int count)
Yingdi Yu42372442013-11-06 18:43:31 -0800600{ updateCollectStatus(count); }
601
602void
603ContactPanel::updateCollectStatus(int count)
604{
605 m_collectStatus->at(count) = true;
606 vector<bool>::const_iterator it = m_collectStatus->begin();
607 for(; it != m_collectStatus->end(); it++)
608 if(*it == false)
609 return;
610
611 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
612}
613
614static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
615
616string
617ContactPanel::getRandomString()
618{
619 string randStr;
620 boost::random::random_device rng;
621 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
622 for (int i = 0; i < 10; i ++)
623 {
624 randStr += chars[index_dist(rng)];
625 }
626 return randStr;
627}
628
629void
Yingdi Yub29f78c2013-11-09 20:12:31 -0800630ContactPanel::showError(const QString& msg){
631 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
632 exit(1);
633}
634
635void
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800636ContactPanel::showWarning(const QString& msg){
637 QMessageBox::information(this, tr("Chronos"), msg);
638}
639
640void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700641ContactPanel::updateSelection(const QItemSelection &selected,
642 const QItemSelection &deselected)
643{
644 QModelIndexList items = selected.indexes();
645 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu42372442013-11-06 18:43:31 -0800646 string alias = text.toStdString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700647
Yingdi Yu79c25a22013-10-21 13:38:38 -0700648 int i = 0;
649 for(; i < m_contactList.size(); i++)
650 {
651 if(alias == m_contactList[i]->getAlias())
652 break;
653 }
654
Yingdi Yua50c3252013-11-03 15:05:26 -0800655 m_currentSelectedContact = m_contactList[i];
Yingdi Yua0594092013-11-06 22:07:38 -0800656 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
657 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
658 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700659
Yingdi Yua50c3252013-11-03 15:05:26 -0800660 if(m_currentSelectedContact->isIntroducer())
661 {
662 ui->isIntroducer->setChecked(true);
663 ui->addScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800664 ui->deleteScope->setEnabled(true);
665 ui->trustScopeList->setEnabled(true);
666
667 string filter("contact_namespace = '");
668 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
669
670 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
671 m_trustScopeModel->setTable("TrustScope");
672 m_trustScopeModel->setFilter(filter.c_str());
673 m_trustScopeModel->select();
674 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
675 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
676 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
677
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800678 ui->trustScopeList->setModel(m_trustScopeModel);
679 ui->trustScopeList->setColumnHidden(0, true);
680 ui->trustScopeList->setColumnHidden(1, true);
681 ui->trustScopeList->show();
Yingdi Yua50c3252013-11-03 15:05:26 -0800682 }
683 else
684 {
685 ui->isIntroducer->setChecked(false);
686 ui->addScope->setEnabled(false);
687 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800688
689 string filter("contact_namespace = '");
690 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
691
692 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
693 m_trustScopeModel->setTable("TrustScope");
694 m_trustScopeModel->setFilter(filter.c_str());
695 m_trustScopeModel->select();
696 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
697 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
698 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
699
700 ui->trustScopeList->setModel(m_trustScopeModel);
701 ui->trustScopeList->setColumnHidden(0, true);
702 ui->trustScopeList->setColumnHidden(1, true);
703 ui->trustScopeList->show();
704
705 ui->trustScopeList->setEnabled(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800706 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800707
708 string filter("profile_identity = '");
709 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
710
711 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
712 m_endorseDataModel->setTable("ContactProfile");
713 m_endorseDataModel->setFilter(filter.c_str());
714 m_endorseDataModel->select();
715
716 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
717 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
718 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
719 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
720
721 ui->endorseList->setModel(m_endorseDataModel);
722 ui->endorseList->setColumnHidden(0, true);
723 ui->endorseList->resizeColumnToContents(1);
724 ui->endorseList->resizeColumnToContents(2);
725 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
726 ui->endorseList->show();
Yingdi Yu01a942b2013-10-10 15:00:58 -0700727}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700728
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700729void
Yingdi Yu42372442013-11-06 18:43:31 -0800730ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700731{
Yingdi Yu71ec9652013-11-09 22:18:26 -0800732 // _LOG_DEBUG(identity.toStdString());
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800733 Name defaultIdentity = Name(identity.toStdString());
Yingdi Yu64206112013-12-24 11:16:32 +0800734 Name defaultCertName = m_identityManager->getDefaultCertificateNameForIdentity(defaultIdentity);
Yingdi Yu71ec9652013-11-09 22:18:26 -0800735 if(defaultCertName.size() == 0)
Yingdi Yueb98f7d2013-11-10 01:34:57 -0800736 {
737 showWarning(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
738 return;
739 }
740 m_defaultIdentity = defaultIdentity;
Yingdi Yub35b8652013-11-07 11:32:40 -0800741 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800742 m_nickName = nickName.toStdString();
Yingdi Yu64206112013-12-24 11:16:32 +0800743 m_face->removeRegisteredPrefix(m_invitationListenerId);
Yingdi Yu908f8412013-11-09 00:03:26 -0800744 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800745 setInvitationListener();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800746 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700747}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700748
749void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700750ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700751{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700752
Yingdi Yud59b5932013-10-16 13:16:49 -0700753void
754ContactPanel::openAddContactPanel()
755{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700756
Yingdi Yu79c25a22013-10-21 13:38:38 -0700757void
Yingdi Yu908f8412013-11-09 00:03:26 -0800758ContactPanel::openBrowseContactDialog()
759{
760 m_browseContactDialog->show();
761 emit refreshCertDirectory();
762}
763
764void
765ContactPanel::removeContactButton()
766{
767 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
768 QModelIndexList selectedList = selectionModel->selectedIndexes();
769 QModelIndexList::iterator it = selectedList.begin();
770 for(; it != selectedList.end(); it++)
771 {
772 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu64206112013-12-24 11:16:32 +0800773 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yu908f8412013-11-09 00:03:26 -0800774 for(; contactIt != m_contactList.end(); contactIt++)
775 {
776 if((*contactIt)->getAlias() == alias)
777 {
778 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
779 m_contactList.erase(contactIt);
780 break;
781 }
782 }
783 }
784 refreshContactList();
785}
786
787void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700788ContactPanel::openInvitationDialog()
789{ m_invitationDialog->show(); }
790
791void
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800792ContactPanel::addContactIntoPanelPolicy(const Name& contactNameSpace)
793{
Yingdi Yu64206112013-12-24 11:16:32 +0800794 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
795 if(contact != CHRONOCHAT_NULL_CONTACTITEM_PTR)
796 m_policyManager->addTrustAnchor(contact->getSelfEndorseCertificate());
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800797}
798
799void
800ContactPanel::removeContactFromPanelPolicy(const Name& keyName)
Yingdi Yu64206112013-12-24 11:16:32 +0800801{ m_policyManager->removeTrustAnchor(keyName); }
Yingdi Yu6ea54e42013-11-12 17:50:21 -0800802
803void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700804ContactPanel::refreshContactList()
805{
Yingdi Yu64206112013-12-24 11:16:32 +0800806 m_contactList.clear();
807 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700808 QStringList contactNameList;
809 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yua0594092013-11-06 22:07:38 -0800810 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700811
812 m_contactListModel->setStringList(contactNameList);
813}
814
815void
816ContactPanel::showContextMenu(const QPoint& pos)
817{
818 QMenu menu(ui->ContactList);
819 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700820 connect(m_menuInvite, SIGNAL(triggered()),
821 this, SLOT(openStartChatDialog()));
Yingdi Yua0594092013-11-06 22:07:38 -0800822 menu.addSeparator();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700823 menu.addAction(m_menuAlias);
824 connect(m_menuAlias, SIGNAL(triggered()),
825 this, SLOT(openSetAliasDialog()));
826 menu.exec(ui->ContactList->mapToGlobal(pos));
827
828}
829
830void
831ContactPanel::openSetAliasDialog()
832{
Yingdi Yua50c3252013-11-03 15:05:26 -0800833 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700834 m_setAliasDialog->show();
835}
836
Yingdi Yu100267f2013-10-21 15:01:40 -0700837void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700838ContactPanel::openSettingDialog()
839{
Yingdi Yu42372442013-11-06 18:43:31 -0800840 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700841 m_settingDialog->show();
842}
843
844void
Yingdi Yu100267f2013-10-21 15:01:40 -0700845ContactPanel::openStartChatDialog()
846{
Yingdi Yu100267f2013-10-21 15:01:40 -0700847 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700848 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700849
Yingdi Yua50c3252013-11-03 15:05:26 -0800850 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu100267f2013-10-21 15:01:40 -0700851 m_startChatDialog->show();
852}
853
Yingdi Yu42f66462013-10-31 17:38:22 -0700854// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700855void
856ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
857{
Yingdi Yu64206112013-12-24 11:16:32 +0800858 Name chatroomName(chatroom.toStdString());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800859
860 Name inviteeNamespace(invitee.toStdString());
Yingdi Yu64206112013-12-24 11:16:32 +0800861 shared_ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800862
Yingdi Yuaccbda92013-12-27 08:44:12 +0800863 ChatDialog* chatDialog = new ChatDialog(m_contactManager, m_identityManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700864 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu42372442013-11-06 18:43:31 -0800865
866 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
867 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -0800868 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
869 this, SLOT(showError(const QString&)));
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800870 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
871 this, SLOT(showWarning(const QString&)));
Yingdi Yu42372442013-11-06 18:43:31 -0800872
Yingdi Yuc90deb12013-11-06 18:51:19 -0800873 // send invitation
Yingdi Yu42f66462013-10-31 17:38:22 -0700874 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700875
876 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700877}
878
Yingdi Yu42f66462013-10-31 17:38:22 -0700879// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700880void
Yingdi Yu42f66462013-10-31 17:38:22 -0700881ContactPanel::startChatroom2(const ChronosInvitation& invitation,
Yingdi Yu64206112013-12-24 11:16:32 +0800882 const IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700883{
Yingdi Yu64206112013-12-24 11:16:32 +0800884 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800885
Yingdi Yu42f66462013-10-31 17:38:22 -0700886 Name chatroomName("/ndn/broadcast/chronos");
887 chatroomName.append(invitation.getChatroom());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800888
Yingdi Yuaccbda92013-12-27 08:44:12 +0800889 ChatDialog* chatDialog = new ChatDialog(m_contactManager, m_identityManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800890
Yingdi Yu42372442013-11-06 18:43:31 -0800891 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
892 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yub29f78c2013-11-09 20:12:31 -0800893 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
894 this, SLOT(showError(const QString&)));
Yingdi Yu3e87bd82013-11-10 10:47:44 -0800895 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
896 this, SLOT(showWarning(const QString&)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700897
Yingdi Yuc90deb12013-11-06 18:51:19 -0800898 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800899 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu42f66462013-10-31 17:38:22 -0700900
Yingdi Yu42f66462013-10-31 17:38:22 -0700901 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
902
903 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
904
905 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700906}
907
908void
Yingdi Yu42f66462013-10-31 17:38:22 -0700909ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
Yingdi Yu64206112013-12-24 11:16:32 +0800910 const IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700911{
Yingdi Yuaccbda92013-12-27 08:44:12 +0800912 Name dataName = invitation.getInterestName();
913 time_t nowSeconds = time(NULL);
914 struct tm current = *gmtime(&nowSeconds);
915 MillisecondsSince1970 version = timegm(&current) * 1000.0;
916 dataName.appendVersion(version);
917 Data data(dataName);
Yingdi Yu64206112013-12-24 11:16:32 +0800918 string content = m_localPrefix.toUri();
919 data.setContent((const uint8_t *)&content[0], content.size());
920 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
921
922 Name certificateName;
923 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
924
925 if(inferredIdentity.getComponentCount() == 0)
926 certificateName = m_identityManager->getDefaultCertificateName();
927 else
928 certificateName = m_identityManager->getDefaultCertificateNameForIdentity(inferredIdentity);
929 m_identityManager->signByCertificate(data, certificateName);
930
931 m_transport->send(*data.wireEncode());
932
Yingdi Yu42f66462013-10-31 17:38:22 -0700933 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700934}
935
936void
Yingdi Yu42f66462013-10-31 17:38:22 -0700937ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700938{
Yingdi Yu64206112013-12-24 11:16:32 +0800939 Data data(invitation.getInterestName());
940 string content("nack");
941 data.setContent((const uint8_t *)&content[0], content.size());
942 data.getMetaInfo().setTimestampMilliseconds(time(NULL) * 1000.0);
943
944 Name certificateName;
945 Name inferredIdentity = m_policyManager->inferSigningIdentity(data.getName());
946 if(inferredIdentity.getComponentCount() == 0)
947 certificateName = m_identityManager->getDefaultCertificateName();
948 else
949 certificateName = m_identityManager->getDefaultCertificateNameForIdentity(inferredIdentity);
950 m_identityManager->signByCertificate(data, certificateName);
951
952 m_transport->send(*data.wireEncode());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700953}
954
Yingdi Yua50c3252013-11-03 15:05:26 -0800955void
956ContactPanel::isIntroducerChanged(int state)
957{
958 if(state == Qt::Checked)
959 {
960 ui->addScope->setEnabled(true);
961 ui->deleteScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800962 ui->trustScopeList->setEnabled(true);
963
964 string filter("contact_namespace = '");
965 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800966
967 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
968 m_trustScopeModel->setTable("TrustScope");
969 m_trustScopeModel->setFilter(filter.c_str());
970 m_trustScopeModel->select();
971 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
972 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
973 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800974
975 ui->trustScopeList->setModel(m_trustScopeModel);
976 ui->trustScopeList->setColumnHidden(0, true);
977 ui->trustScopeList->setColumnHidden(1, true);
978 ui->trustScopeList->show();
979
980 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yua50c3252013-11-03 15:05:26 -0800981 }
982 else
983 {
Yingdi Yua50c3252013-11-03 15:05:26 -0800984 ui->addScope->setEnabled(false);
985 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800986
987 string filter("contact_namespace = '");
988 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
989
990 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
991 m_trustScopeModel->setTable("TrustScope");
992 m_trustScopeModel->setFilter(filter.c_str());
993 m_trustScopeModel->select();
994 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
995 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
996 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
997
998 ui->trustScopeList->setModel(m_trustScopeModel);
999 ui->trustScopeList->setColumnHidden(0, true);
1000 ui->trustScopeList->setColumnHidden(1, true);
1001 ui->trustScopeList->show();
1002
1003 ui->trustScopeList->setEnabled(false);
1004
1005 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yua50c3252013-11-03 15:05:26 -08001006 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -08001007 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
1008}
1009
1010void
1011ContactPanel::addScopeClicked()
1012{
1013 int rowCount = m_trustScopeModel->rowCount();
1014 QSqlRecord record;
1015 QSqlField identityField("contact_namespace", QVariant::String);
1016 record.append(identityField);
Yingdi Yu64206112013-12-24 11:16:32 +08001017 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -08001018 m_trustScopeModel->insertRow(rowCount);
1019 m_trustScopeModel->setRecord(rowCount, record);
1020}
1021
1022void
1023ContactPanel::deleteScopeClicked()
1024{
1025 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
1026 QModelIndexList indexList = selectionModel->selectedIndexes();
1027
1028 int i = indexList.size() - 1;
1029 for(; i >= 0; i--)
1030 m_trustScopeModel->removeRow(indexList[i].row());
1031
1032 m_trustScopeModel->submitAll();
1033}
1034
1035void
1036ContactPanel::saveScopeClicked()
Yingdi Yua0594092013-11-06 22:07:38 -08001037{ m_trustScopeModel->submitAll(); }
Yingdi Yu8dacdf22013-11-05 23:06:43 -08001038
1039void
1040ContactPanel::endorseButtonClicked()
1041{
1042 m_endorseDataModel->submitAll();
1043 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yua50c3252013-11-03 15:05:26 -08001044}
Yingdi Yu100267f2013-10-21 15:01:40 -07001045
Yingdi Yu42372442013-11-06 18:43:31 -08001046void
1047ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
1048{
1049 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yua0594092013-11-06 22:07:38 -08001050
1051 ChatDialog* deletedChat = NULL;
Yingdi Yu42372442013-11-06 18:43:31 -08001052 if(it != m_chatDialogs.end())
1053 {
Yingdi Yu42372442013-11-06 18:43:31 -08001054 deletedChat = it->second;
1055 m_chatDialogs.erase(it);
1056 }
Yingdi Yua0594092013-11-06 22:07:38 -08001057 if (deletedChat != NULL)
1058 delete deletedChat;
Yingdi Yu42372442013-11-06 18:43:31 -08001059}
1060
Yingdi Yu9e0dc292013-10-10 11:39:45 -07001061#if WAF
1062#include "contactpanel.moc"
1063#include "contactpanel.cpp.moc"
1064#endif