blob: 56db0a7f045842ca355d8db2684d371e505e814d [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 Yu53eb8a72013-10-23 11:50:51 -070024#include <ndn.cxx/security/keychain.h>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070025#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yu100267f2013-10-21 15:01:40 -070026#include <ndn.cxx/common.h>
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 Yu0a6b6c52013-10-15 17:54:00 -070031#include "logging.h"
32#include "exception.h"
33#endif
34
35namespace fs = boost::filesystem;
36using namespace ndn;
Yingdi Yu53eb8a72013-10-23 11:50:51 -070037
Yingdi Yu79c25a22013-10-21 13:38:38 -070038using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070039
40INIT_LOGGER("ContactPanel");
41
Yingdi Yu53eb8a72013-10-23 11:50:51 -070042Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
Yingdi Yu42f66462013-10-31 17:38:22 -070043Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070044
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070045ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yua50c3252013-11-03 15:05:26 -080046 : QDialog(parent)
47 , ui(new Ui::ContactPanel)
48 , m_contactListModel(new QStringListModel)
49 , m_startChatDialog(new StartChatDialog)
50 , m_invitationDialog(new InvitationDialog)
51 , m_settingDialog(new SettingDialog)
Yingdi Yudef90612013-10-09 22:34:42 -070052{
Yingdi Yu53eb8a72013-10-23 11:50:51 -070053 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu42f66462013-10-31 17:38:22 -070054 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070055
Yingdi Yu42372442013-11-06 18:43:31 -080056 createAction();
57
Yingdi Yu42f66462013-10-31 17:38:22 -070058 m_contactManager = contactManager;
Yingdi Yub35b8652013-11-07 11:32:40 -080059
60 openDB();
Yingdi Yu42372442013-11-06 18:43:31 -080061
Yingdi Yuc4d08d22013-10-23 23:07:29 -070062 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070063
Yingdi Yuc4d08d22013-10-23 23:07:29 -070064 setKeychain();
Yingdi Yu42372442013-11-06 18:43:31 -080065
66 m_defaultIdentity = m_keychain->getDefaultIdentity();
Yingdi Yu908f8412013-11-09 00:03:26 -080067 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -080068 m_nickName = m_defaultIdentity.get(-1).toUri();
69 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
70
Yingdi Yub35b8652013-11-07 11:32:40 -080071 m_profileEditor = new ProfileEditor(m_contactManager);
72 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
73
74 m_addContactPanel = new AddContactPanel(contactManager);
Yingdi Yu908f8412013-11-09 00:03:26 -080075 m_browseContactDialog = new BrowseContactDialog(contactManager);
Yingdi Yub35b8652013-11-07 11:32:40 -080076 m_setAliasDialog = new SetAliasDialog(contactManager);
77
78 ui->setupUi(this);
79
Yingdi Yu42372442013-11-06 18:43:31 -080080 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
81
Yingdi Yuc4d08d22013-10-23 23:07:29 -070082 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -070083
Yingdi Yuc4d08d22013-10-23 23:07:29 -070084 setInvitationListener();
Yingdi Yu42372442013-11-06 18:43:31 -080085
Yingdi Yu8dacdf22013-11-05 23:06:43 -080086 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -070087
88 ui->ContactList->setModel(m_contactListModel);
89
Yingdi Yuc26af3c2013-10-17 17:03:46 -070090
Yingdi Yu42372442013-11-06 18:43:31 -080091 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yuc4d08d22013-10-23 23:07:29 -070092 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -070093
Yingdi Yuc4d08d22013-10-23 23:07:29 -070094 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
95 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -070096
Yingdi Yuc4d08d22013-10-23 23:07:29 -070097 connect(ui->EditProfileButton, SIGNAL(clicked()),
98 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -070099
Yingdi Yu908f8412013-11-09 00:03:26 -0800100 // connect(ui->AddContactButton, SIGNAL(clicked()),
101 // this, SLOT(openAddContactPanel()));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700102 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yu908f8412013-11-09 00:03:26 -0800103 this, SLOT(openBrowseContactDialog()));
104
105 connect(this, SIGNAL(refreshCertDirectory()),
106 m_browseContactDialog, SLOT(refreshList()));
107
108 connect(ui->DeleteContactButton, SIGNAL(clicked()),
109 this, SLOT(removeContactButton()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700110
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700111 connect(ui->settingButton, SIGNAL(clicked()),
112 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700113
Yingdi Yu908f8412013-11-09 00:03:26 -0800114 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700115 this, SLOT(refreshContactList()));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700116 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
117 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700118
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700119 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
120 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700121
Yingdi Yu42f66462013-10-31 17:38:22 -0700122 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
123 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
124 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
125 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700126
Yingdi Yu42372442013-11-06 18:43:31 -0800127 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
128 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700129
130 connect(this, SIGNAL(newInvitationReady()),
131 this, SLOT(openInvitationDialog()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700132
Yingdi Yua50c3252013-11-03 15:05:26 -0800133 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
134 this, SLOT(isIntroducerChanged(int)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800135
136 connect(ui->addScope, SIGNAL(clicked()),
137 this, SLOT(addScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800138 connect(ui->deleteScope, SIGNAL(clicked()),
139 this, SLOT(deleteScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800140 connect(ui->saveButton, SIGNAL(clicked()),
141 this, SLOT(saveScopeClicked()));
142
143 connect(ui->endorseButton, SIGNAL(clicked()),
144 this, SLOT(endorseButtonClicked()));
Yingdi Yudef90612013-10-09 22:34:42 -0700145}
146
147ContactPanel::~ContactPanel()
148{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700149 delete ui;
150 delete m_contactListModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800151 delete m_startChatDialog;
152 delete m_invitationDialog;
153 delete m_settingDialog;
154
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700155 delete m_profileEditor;
156 delete m_addContactPanel;
Yingdi Yu908f8412013-11-09 00:03:26 -0800157 delete m_browseContactDialog;
Yingdi Yu42372442013-11-06 18:43:31 -0800158 delete m_setAliasDialog;
159
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800160 delete m_trustScopeModel;
161 delete m_endorseDataModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800162 delete m_endorseComboBoxDelegate;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700163
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700164 delete m_menuInvite;
Yingdi Yu42372442013-11-06 18:43:31 -0800165 delete m_menuAlias;
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700166
167 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
168 for(; it != m_chatDialogs.end(); it++)
169 delete it->second;
Yingdi Yudef90612013-10-09 22:34:42 -0700170}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700171
Yingdi Yu01a942b2013-10-10 15:00:58 -0700172void
Yingdi Yu42372442013-11-06 18:43:31 -0800173ContactPanel::createAction()
174{
175 m_menuInvite = new QAction("&Chat", this);
176 m_menuAlias = new QAction("&Set Alias", this);
177}
178
179void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700180ContactPanel::openDB()
181{
182 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
183 QString path = (QDir::home().path());
184 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
185 db.setDatabaseName(path);
186 bool ok = db.open();
Yingdi Yu42372442013-11-06 18:43:31 -0800187 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800188
189 m_trustScopeModel = new QSqlTableModel;
190 m_endorseDataModel = new QSqlTableModel;
191 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700192}
193
194void
195ContactPanel::setKeychain()
196{
Yingdi Yued8cfc42013-11-01 17:37:51 -0700197 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700198
199 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
200 for(; it != m_contactList.end(); it++)
Yingdi Yub35b8652013-11-07 11:32:40 -0800201 {
202 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
203 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700204
Yingdi Yued8cfc42013-11-01 17:37:51 -0700205 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
206 m_panelPolicyManager,
207 NULL));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700208}
209
210void
211ContactPanel::setLocalPrefix()
212{
213 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
214 interest->setChildSelector(Interest::CHILD_RIGHT);
215
216 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
217 this,
218 _1),
219 boost::bind(&ContactPanel::onLocalPrefixTimeout,
220 this,
221 _1,
222 _2),
223 boost::bind(&ContactPanel::onLocalPrefixVerified,
224 this,
225 _1)));
226
227 m_handler->sendInterest(interest, closure);
228}
229
230void
231ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
232{
233 string originPrefix(data->content().buf(), data->content().size());
234 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
235 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700236 m_localPrefix = Name(prefix);
237}
238
239void
240ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu42f66462013-10-31 17:38:22 -0700241{
242 string randomSuffix = getRandomString();
243 m_localPrefix = Name("/private/local");
Yingdi Yu42f66462013-10-31 17:38:22 -0700244}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700245
246void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700247ContactPanel::setInvitationListener()
248{
Yingdi Yu42372442013-11-06 18:43:31 -0800249 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
250 m_inviteListenPrefix.append(m_defaultIdentity);
251 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
252 m_handler->setInterestFilter (m_inviteListenPrefix,
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700253 boost::bind(&ContactPanel::onInvitation,
254 this,
255 _1));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700256}
257
258void
259ContactPanel::onInvitation(Ptr<Interest> interest)
260{
Yingdi Yu42372442013-11-06 18:43:31 -0800261 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yub35b8652013-11-07 11:32:40 -0800262
263 Ptr<ChronosInvitation> invitation = NULL;
264 try{
265 invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName()));
266 }catch(exception& e){
267 _LOG_ERROR("Exception: " << e.what());
268 return;
269 }
270
Yingdi Yu42372442013-11-06 18:43:31 -0800271 Name chatroomName("/ndn/broadcast/chronos");
272 chatroomName.append(invitation->getChatroom());
273 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
274 if(it != m_chatDialogs.end())
275 {
Yingdi Yub35b8652013-11-07 11:32:40 -0800276 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu42372442013-11-06 18:43:31 -0800277 return;
278 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700279
Yingdi Yued8cfc42013-11-01 17:37:51 -0700280 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700281 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
282 this,
283 _1,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700284 invitation),
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700285 boost::bind(&ContactPanel::onTimeout,
286 this,
287 _1,
288 _2),
289 boost::bind(&ContactPanel::onUnverified,
290 this,
291 _1)));
292 m_handler->sendInterest(newInterest, closure);
293}
294
295void
Yingdi Yu42372442013-11-06 18:43:31 -0800296ContactPanel::onInvitationCertVerified(Ptr<Data> data,
297 Ptr<ChronosInvitation> invitation)
298{
299 using namespace ndn::security;
300 Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
301
302 if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
303 {
304 Name keyName = certificate->getPublicKeyName();
305 Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1);
306 popChatInvitation(invitation, inviterNameSpace, certificate);
307 }
308}
309
310void
311ContactPanel::onUnverified(Ptr<Data> data)
312{}
313
314void
315ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
316{}
317
318void
319ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
320 const Name& inviterNameSpace,
321 Ptr<security::IdentityCertificate> certificate)
322{
323 string alias;
324 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
325 for(; it != m_contactList.end(); it++)
326 if((*it)->getNameSpace() == inviterNameSpace)
327 alias = (*it)->getAlias();
328
329 if(it != m_contactList.end())
330 return;
331
332 m_invitationDialog->setInvitation(alias, invitation, certificate);
333 emit newInvitationReady();
334}
335
336void
337ContactPanel::collectEndorsement()
338{
339 m_collectStatus = Ptr<vector<bool> >::Create();
340 m_collectStatus->assign(m_contactList.size(), false);
341
342 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
343 int count = 0;
344 for(; it != m_contactList.end(); it++, count++)
345 {
346 Name interestName = (*it)->getNameSpace();
347 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
348 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
349 interest->setChildSelector(Interest::CHILD_RIGHT);
350 interest->setInterestLifetime(1);
351
352 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified,
353 this,
354 _1,
355 count),
356 boost::bind(&ContactPanel::onDnsEndorseeTimeout,
357 this,
358 _1,
359 _2,
360 count),
361 boost::bind(&ContactPanel::onDnsEndorseeUnverified,
362 this,
363 _1,
364 count)));
365
366 m_handler->sendInterest(interest, closure);
367 }
368}
369
370void
371ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count)
372{
373 Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
374 Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
375 EndorseCertificate endorseCertificate(*endorseData);
376
377 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
378
379 updateCollectStatus(count);
380}
381
382void
383ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
384{ updateCollectStatus(count); }
385
386void
387ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count)
388{ updateCollectStatus(count); }
389
390void
391ContactPanel::updateCollectStatus(int count)
392{
393 m_collectStatus->at(count) = true;
394 vector<bool>::const_iterator it = m_collectStatus->begin();
395 for(; it != m_collectStatus->end(); it++)
396 if(*it == false)
397 return;
398
399 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
400}
401
402static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
403
404string
405ContactPanel::getRandomString()
406{
407 string randStr;
408 boost::random::random_device rng;
409 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
410 for (int i = 0; i < 10; i ++)
411 {
412 randStr += chars[index_dist(rng)];
413 }
414 return randStr;
415}
416
417void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700418ContactPanel::updateSelection(const QItemSelection &selected,
419 const QItemSelection &deselected)
420{
421 QModelIndexList items = selected.indexes();
422 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu42372442013-11-06 18:43:31 -0800423 string alias = text.toStdString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700424
Yingdi Yu79c25a22013-10-21 13:38:38 -0700425 int i = 0;
426 for(; i < m_contactList.size(); i++)
427 {
428 if(alias == m_contactList[i]->getAlias())
429 break;
430 }
431
Yingdi Yua50c3252013-11-03 15:05:26 -0800432 m_currentSelectedContact = m_contactList[i];
Yingdi Yua0594092013-11-06 22:07:38 -0800433 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
434 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
435 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700436
Yingdi Yua50c3252013-11-03 15:05:26 -0800437 if(m_currentSelectedContact->isIntroducer())
438 {
439 ui->isIntroducer->setChecked(true);
440 ui->addScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800441 ui->deleteScope->setEnabled(true);
442 ui->trustScopeList->setEnabled(true);
443
444 string filter("contact_namespace = '");
445 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
446
447 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
448 m_trustScopeModel->setTable("TrustScope");
449 m_trustScopeModel->setFilter(filter.c_str());
450 m_trustScopeModel->select();
451 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
452 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
453 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
454
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800455 ui->trustScopeList->setModel(m_trustScopeModel);
456 ui->trustScopeList->setColumnHidden(0, true);
457 ui->trustScopeList->setColumnHidden(1, true);
458 ui->trustScopeList->show();
Yingdi Yua50c3252013-11-03 15:05:26 -0800459 }
460 else
461 {
462 ui->isIntroducer->setChecked(false);
463 ui->addScope->setEnabled(false);
464 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800465
466 string filter("contact_namespace = '");
467 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
468
469 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
470 m_trustScopeModel->setTable("TrustScope");
471 m_trustScopeModel->setFilter(filter.c_str());
472 m_trustScopeModel->select();
473 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
474 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
475 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
476
477 ui->trustScopeList->setModel(m_trustScopeModel);
478 ui->trustScopeList->setColumnHidden(0, true);
479 ui->trustScopeList->setColumnHidden(1, true);
480 ui->trustScopeList->show();
481
482 ui->trustScopeList->setEnabled(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800483 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800484
485 string filter("profile_identity = '");
486 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
487
488 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
489 m_endorseDataModel->setTable("ContactProfile");
490 m_endorseDataModel->setFilter(filter.c_str());
491 m_endorseDataModel->select();
492
493 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
494 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
495 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
496 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
497
498 ui->endorseList->setModel(m_endorseDataModel);
499 ui->endorseList->setColumnHidden(0, true);
500 ui->endorseList->resizeColumnToContents(1);
501 ui->endorseList->resizeColumnToContents(2);
502 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
503 ui->endorseList->show();
Yingdi Yu01a942b2013-10-10 15:00:58 -0700504}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700505
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700506void
Yingdi Yu42372442013-11-06 18:43:31 -0800507ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700508{
Yingdi Yu42372442013-11-06 18:43:31 -0800509 m_defaultIdentity = Name(identity.toStdString());
Yingdi Yub35b8652013-11-07 11:32:40 -0800510 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800511 m_nickName = nickName.toStdString();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700512 m_handler->clearInterestFilter(m_inviteListenPrefix);
Yingdi Yu908f8412013-11-09 00:03:26 -0800513 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu42372442013-11-06 18:43:31 -0800514 setInvitationListener();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800515 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700516}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700517
518void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700519ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700520{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700521
Yingdi Yud59b5932013-10-16 13:16:49 -0700522void
523ContactPanel::openAddContactPanel()
524{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700525
Yingdi Yu79c25a22013-10-21 13:38:38 -0700526void
Yingdi Yu908f8412013-11-09 00:03:26 -0800527ContactPanel::openBrowseContactDialog()
528{
529 m_browseContactDialog->show();
530 emit refreshCertDirectory();
531}
532
533void
534ContactPanel::removeContactButton()
535{
536 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
537 QModelIndexList selectedList = selectionModel->selectedIndexes();
538 QModelIndexList::iterator it = selectedList.begin();
539 for(; it != selectedList.end(); it++)
540 {
541 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
542 vector<Ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
543 for(; contactIt != m_contactList.end(); contactIt++)
544 {
545 if((*contactIt)->getAlias() == alias)
546 {
547 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
548 m_contactList.erase(contactIt);
549 break;
550 }
551 }
552 }
553 refreshContactList();
554}
555
556void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700557ContactPanel::openInvitationDialog()
558{ m_invitationDialog->show(); }
559
560void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700561ContactPanel::refreshContactList()
562{
563 m_contactList = m_contactManager->getContactItemList();
564 QStringList contactNameList;
565 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yua0594092013-11-06 22:07:38 -0800566 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700567
568 m_contactListModel->setStringList(contactNameList);
569}
570
571void
572ContactPanel::showContextMenu(const QPoint& pos)
573{
574 QMenu menu(ui->ContactList);
575 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700576 connect(m_menuInvite, SIGNAL(triggered()),
577 this, SLOT(openStartChatDialog()));
Yingdi Yua0594092013-11-06 22:07:38 -0800578 menu.addSeparator();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700579 menu.addAction(m_menuAlias);
580 connect(m_menuAlias, SIGNAL(triggered()),
581 this, SLOT(openSetAliasDialog()));
582 menu.exec(ui->ContactList->mapToGlobal(pos));
583
584}
585
586void
587ContactPanel::openSetAliasDialog()
588{
Yingdi Yua50c3252013-11-03 15:05:26 -0800589 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700590 m_setAliasDialog->show();
591}
592
Yingdi Yu100267f2013-10-21 15:01:40 -0700593void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700594ContactPanel::openSettingDialog()
595{
Yingdi Yu42372442013-11-06 18:43:31 -0800596 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700597 m_settingDialog->show();
598}
599
600void
Yingdi Yu100267f2013-10-21 15:01:40 -0700601ContactPanel::openStartChatDialog()
602{
Yingdi Yu100267f2013-10-21 15:01:40 -0700603 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700604 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700605
Yingdi Yua50c3252013-11-03 15:05:26 -0800606 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu100267f2013-10-21 15:01:40 -0700607 m_startChatDialog->show();
608}
609
Yingdi Yu42f66462013-10-31 17:38:22 -0700610// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700611void
612ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
613{
Yingdi Yu42f66462013-10-31 17:38:22 -0700614 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800615
616 Name inviteeNamespace(invitee.toStdString());
617 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
618
Yingdi Yuc90deb12013-11-06 18:51:19 -0800619 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700620 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu42372442013-11-06 18:43:31 -0800621
622 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
623 this, SLOT(removeChatDialog(const ndn::Name&)));
624
Yingdi Yuc90deb12013-11-06 18:51:19 -0800625 // send invitation
Yingdi Yu42f66462013-10-31 17:38:22 -0700626 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700627
628 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700629}
630
Yingdi Yu42f66462013-10-31 17:38:22 -0700631// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700632void
Yingdi Yu42f66462013-10-31 17:38:22 -0700633ContactPanel::startChatroom2(const ChronosInvitation& invitation,
634 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700635{
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800636 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
637
Yingdi Yu42f66462013-10-31 17:38:22 -0700638 Name chatroomName("/ndn/broadcast/chronos");
639 chatroomName.append(invitation.getChatroom());
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800640
Yingdi Yuc90deb12013-11-06 18:51:19 -0800641 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800642
Yingdi Yu42372442013-11-06 18:43:31 -0800643 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
644 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700645
Yingdi Yuc90deb12013-11-06 18:51:19 -0800646 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yua0594092013-11-06 22:07:38 -0800647 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu42f66462013-10-31 17:38:22 -0700648
Yingdi Yu42f66462013-10-31 17:38:22 -0700649 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
650
651 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
652
653 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700654}
655
656void
Yingdi Yu42f66462013-10-31 17:38:22 -0700657ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
658 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700659{
660 string prefix = m_localPrefix.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700661 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu42f66462013-10-31 17:38:22 -0700662 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700663}
664
665void
Yingdi Yu42f66462013-10-31 17:38:22 -0700666ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700667{
Yingdi Yu97936352013-11-08 14:13:42 -0800668 string empty("nack");
Yingdi Yu42f66462013-10-31 17:38:22 -0700669 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700670}
671
Yingdi Yua50c3252013-11-03 15:05:26 -0800672void
673ContactPanel::isIntroducerChanged(int state)
674{
675 if(state == Qt::Checked)
676 {
677 ui->addScope->setEnabled(true);
678 ui->deleteScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800679 ui->trustScopeList->setEnabled(true);
680
681 string filter("contact_namespace = '");
682 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800683
684 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
685 m_trustScopeModel->setTable("TrustScope");
686 m_trustScopeModel->setFilter(filter.c_str());
687 m_trustScopeModel->select();
688 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
689 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
690 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800691
692 ui->trustScopeList->setModel(m_trustScopeModel);
693 ui->trustScopeList->setColumnHidden(0, true);
694 ui->trustScopeList->setColumnHidden(1, true);
695 ui->trustScopeList->show();
696
697 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yua50c3252013-11-03 15:05:26 -0800698 }
699 else
700 {
Yingdi Yua50c3252013-11-03 15:05:26 -0800701 ui->addScope->setEnabled(false);
702 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800703
704 string filter("contact_namespace = '");
705 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
706
707 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
708 m_trustScopeModel->setTable("TrustScope");
709 m_trustScopeModel->setFilter(filter.c_str());
710 m_trustScopeModel->select();
711 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
712 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
713 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
714
715 ui->trustScopeList->setModel(m_trustScopeModel);
716 ui->trustScopeList->setColumnHidden(0, true);
717 ui->trustScopeList->setColumnHidden(1, true);
718 ui->trustScopeList->show();
719
720 ui->trustScopeList->setEnabled(false);
721
722 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800723 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800724 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
725}
726
727void
728ContactPanel::addScopeClicked()
729{
730 int rowCount = m_trustScopeModel->rowCount();
731 QSqlRecord record;
732 QSqlField identityField("contact_namespace", QVariant::String);
733 record.append(identityField);
734 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
735 m_trustScopeModel->insertRow(rowCount);
736 m_trustScopeModel->setRecord(rowCount, record);
737}
738
739void
740ContactPanel::deleteScopeClicked()
741{
742 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
743 QModelIndexList indexList = selectionModel->selectedIndexes();
744
745 int i = indexList.size() - 1;
746 for(; i >= 0; i--)
747 m_trustScopeModel->removeRow(indexList[i].row());
748
749 m_trustScopeModel->submitAll();
750}
751
752void
753ContactPanel::saveScopeClicked()
Yingdi Yua0594092013-11-06 22:07:38 -0800754{ m_trustScopeModel->submitAll(); }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800755
756void
757ContactPanel::endorseButtonClicked()
758{
759 m_endorseDataModel->submitAll();
760 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yua50c3252013-11-03 15:05:26 -0800761}
Yingdi Yu100267f2013-10-21 15:01:40 -0700762
Yingdi Yu42372442013-11-06 18:43:31 -0800763void
764ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
765{
766 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yua0594092013-11-06 22:07:38 -0800767
768 ChatDialog* deletedChat = NULL;
Yingdi Yu42372442013-11-06 18:43:31 -0800769 if(it != m_chatDialogs.end())
770 {
Yingdi Yu42372442013-11-06 18:43:31 -0800771 deletedChat = it->second;
772 m_chatDialogs.erase(it);
773 }
Yingdi Yua0594092013-11-06 22:07:38 -0800774 if (deletedChat != NULL)
775 delete deletedChat;
Yingdi Yu42372442013-11-06 18:43:31 -0800776}
777
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700778#if WAF
779#include "contactpanel.moc"
780#include "contactpanel.cpp.moc"
781#endif