blob: 4d281d6e4d63677440379c54f679f7284fd2dbf6 [file] [log] [blame]
Yingdi Yu847aa862013-10-09 16:35:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu40eca752013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu3b318c12013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yub2e747d2013-11-05 23:06:43 -080019#include <QtSql/QSqlRecord>
20#include <QtSql/QSqlField>
21#include <QtSql/QSqlError>
Yingdi Yu40eca752013-10-10 15:00:58 -070022
Yingdi Yu3b318c12013-10-15 17:54:00 -070023#ifndef Q_MOC_RUN
Yingdi Yu978b3ae2013-10-23 11:50:51 -070024#include <ndn.cxx/security/keychain.h>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070025#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yu595aadc2013-10-21 15:01:40 -070026#include <ndn.cxx/common.h>
Yingdi Yu3b318c12013-10-15 17:54:00 -070027#include <boost/filesystem.hpp>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070028#include <boost/random/random_device.hpp>
29#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu7989eb22013-10-31 17:38:22 -070030#include "panel-policy-manager.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070031#include "logging.h"
32#include "exception.h"
33#endif
34
35namespace fs = boost::filesystem;
36using namespace ndn;
Yingdi Yu978b3ae2013-10-23 11:50:51 -070037
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070038using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070039
40INIT_LOGGER("ContactPanel");
41
Yingdi Yu978b3ae2013-10-23 11:50:51 -070042Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
Yingdi Yu7989eb22013-10-31 17:38:22 -070043Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070044
Yingdi Yu9236c432013-10-18 11:29:25 -070045ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yuec5e72a2013-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 Yu847aa862013-10-09 16:35:53 -070052{
Yingdi Yu978b3ae2013-10-23 11:50:51 -070053 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070054 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu3b318c12013-10-15 17:54:00 -070055
Yingdi Yu46948282013-11-06 18:43:31 -080056 createAction();
57
Yingdi Yu7989eb22013-10-31 17:38:22 -070058 m_contactManager = contactManager;
Yingdi Yue35bdb82013-11-07 11:32:40 -080059
60 openDB();
Yingdi Yu46948282013-11-06 18:43:31 -080061
Yingdi Yueda39aa2013-10-23 23:07:29 -070062 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070063
Yingdi Yueda39aa2013-10-23 23:07:29 -070064 setKeychain();
Yingdi Yu46948282013-11-06 18:43:31 -080065
66 m_defaultIdentity = m_keychain->getDefaultIdentity();
67 m_nickName = m_defaultIdentity.get(-1).toUri();
68 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
69
Yingdi Yue35bdb82013-11-07 11:32:40 -080070 m_profileEditor = new ProfileEditor(m_contactManager);
71 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
72
73 m_addContactPanel = new AddContactPanel(contactManager);
74 m_setAliasDialog = new SetAliasDialog(contactManager);
75
76 ui->setupUi(this);
77
Yingdi Yu46948282013-11-06 18:43:31 -080078 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
79
Yingdi Yueda39aa2013-10-23 23:07:29 -070080 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -070081
Yingdi Yueda39aa2013-10-23 23:07:29 -070082 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -080083
Yingdi Yub2e747d2013-11-05 23:06:43 -080084 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -070085
86 ui->ContactList->setModel(m_contactListModel);
87
Yingdi Yu8f7325a2013-10-17 17:03:46 -070088
Yingdi Yu46948282013-11-06 18:43:31 -080089 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -070090 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -070091
Yingdi Yueda39aa2013-10-23 23:07:29 -070092 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
93 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -070094
Yingdi Yueda39aa2013-10-23 23:07:29 -070095 connect(ui->EditProfileButton, SIGNAL(clicked()),
96 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070097
Yingdi Yueda39aa2013-10-23 23:07:29 -070098 connect(ui->AddContactButton, SIGNAL(clicked()),
99 this, SLOT(openAddContactPanel()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700100
Yingdi Yueda39aa2013-10-23 23:07:29 -0700101 connect(ui->settingButton, SIGNAL(clicked()),
102 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700103
Yingdi Yueda39aa2013-10-23 23:07:29 -0700104 connect(m_addContactPanel, SIGNAL(newContactAdded()),
105 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700106 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
107 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700108
Yingdi Yueda39aa2013-10-23 23:07:29 -0700109 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
110 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700111
Yingdi Yu7989eb22013-10-31 17:38:22 -0700112 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
113 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
114 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
115 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700116
Yingdi Yu46948282013-11-06 18:43:31 -0800117 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
118 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700119
120 connect(this, SIGNAL(newInvitationReady()),
121 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700122
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800123 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
124 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800125
126 connect(ui->addScope, SIGNAL(clicked()),
127 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800128 connect(ui->deleteScope, SIGNAL(clicked()),
129 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800130 connect(ui->saveButton, SIGNAL(clicked()),
131 this, SLOT(saveScopeClicked()));
132
133 connect(ui->endorseButton, SIGNAL(clicked()),
134 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700135}
136
137ContactPanel::~ContactPanel()
138{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700139 delete ui;
140 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800141 delete m_startChatDialog;
142 delete m_invitationDialog;
143 delete m_settingDialog;
144
Yingdi Yueda39aa2013-10-23 23:07:29 -0700145 delete m_profileEditor;
146 delete m_addContactPanel;
Yingdi Yu46948282013-11-06 18:43:31 -0800147 delete m_setAliasDialog;
148
Yingdi Yub2e747d2013-11-05 23:06:43 -0800149 delete m_trustScopeModel;
150 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800151 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700152
Yingdi Yueda39aa2013-10-23 23:07:29 -0700153 delete m_menuInvite;
Yingdi Yu46948282013-11-06 18:43:31 -0800154 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700155
156 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
157 for(; it != m_chatDialogs.end(); it++)
158 delete it->second;
Yingdi Yu847aa862013-10-09 16:35:53 -0700159}
160
Yingdi Yu40eca752013-10-10 15:00:58 -0700161void
Yingdi Yu46948282013-11-06 18:43:31 -0800162ContactPanel::createAction()
163{
164 m_menuInvite = new QAction("&Chat", this);
165 m_menuAlias = new QAction("&Set Alias", this);
166}
167
168void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700169ContactPanel::openDB()
170{
171 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
172 QString path = (QDir::home().path());
173 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
174 db.setDatabaseName(path);
175 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800176 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800177
178 m_trustScopeModel = new QSqlTableModel;
179 m_endorseDataModel = new QSqlTableModel;
180 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700181}
182
183void
184ContactPanel::setKeychain()
185{
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700186 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700187
188 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
189 for(; it != m_contactList.end(); it++)
Yingdi Yue35bdb82013-11-07 11:32:40 -0800190 {
191 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
192 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700193
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700194 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
195 m_panelPolicyManager,
196 NULL));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700197}
198
199void
200ContactPanel::setLocalPrefix()
201{
202 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
203 interest->setChildSelector(Interest::CHILD_RIGHT);
204
205 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
206 this,
207 _1),
208 boost::bind(&ContactPanel::onLocalPrefixTimeout,
209 this,
210 _1,
211 _2),
212 boost::bind(&ContactPanel::onLocalPrefixVerified,
213 this,
214 _1)));
215
216 m_handler->sendInterest(interest, closure);
217}
218
219void
220ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
221{
222 string originPrefix(data->content().buf(), data->content().size());
223 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
224 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700225 m_localPrefix = Name(prefix);
226}
227
228void
229ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700230{
231 string randomSuffix = getRandomString();
232 m_localPrefix = Name("/private/local");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700233}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700234
235void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700236ContactPanel::setInvitationListener()
237{
Yingdi Yu46948282013-11-06 18:43:31 -0800238 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
239 m_inviteListenPrefix.append(m_defaultIdentity);
240 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
241 m_handler->setInterestFilter (m_inviteListenPrefix,
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700242 boost::bind(&ContactPanel::onInvitation,
243 this,
244 _1));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700245}
246
247void
248ContactPanel::onInvitation(Ptr<Interest> interest)
249{
Yingdi Yu46948282013-11-06 18:43:31 -0800250 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800251
252 Ptr<ChronosInvitation> invitation = NULL;
253 try{
254 invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName()));
255 }catch(exception& e){
256 _LOG_ERROR("Exception: " << e.what());
257 return;
258 }
259
Yingdi Yu46948282013-11-06 18:43:31 -0800260 Name chatroomName("/ndn/broadcast/chronos");
261 chatroomName.append(invitation->getChatroom());
262 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
263 if(it != m_chatDialogs.end())
264 {
Yingdi Yue35bdb82013-11-07 11:32:40 -0800265 _LOG_ERROR("Exisiting chatroom!");
Yingdi Yu46948282013-11-06 18:43:31 -0800266 return;
267 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700268
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700269 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700270 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
271 this,
272 _1,
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700273 invitation),
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700274 boost::bind(&ContactPanel::onTimeout,
275 this,
276 _1,
277 _2),
278 boost::bind(&ContactPanel::onUnverified,
279 this,
280 _1)));
281 m_handler->sendInterest(newInterest, closure);
282}
283
284void
Yingdi Yu46948282013-11-06 18:43:31 -0800285ContactPanel::onInvitationCertVerified(Ptr<Data> data,
286 Ptr<ChronosInvitation> invitation)
287{
288 using namespace ndn::security;
289 Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
290
291 if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
292 {
293 Name keyName = certificate->getPublicKeyName();
294 Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1);
295 popChatInvitation(invitation, inviterNameSpace, certificate);
296 }
297}
298
299void
300ContactPanel::onUnverified(Ptr<Data> data)
301{}
302
303void
304ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
305{}
306
307void
308ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
309 const Name& inviterNameSpace,
310 Ptr<security::IdentityCertificate> certificate)
311{
312 string alias;
313 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
314 for(; it != m_contactList.end(); it++)
315 if((*it)->getNameSpace() == inviterNameSpace)
316 alias = (*it)->getAlias();
317
318 if(it != m_contactList.end())
319 return;
320
321 m_invitationDialog->setInvitation(alias, invitation, certificate);
322 emit newInvitationReady();
323}
324
325void
326ContactPanel::collectEndorsement()
327{
328 m_collectStatus = Ptr<vector<bool> >::Create();
329 m_collectStatus->assign(m_contactList.size(), false);
330
331 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
332 int count = 0;
333 for(; it != m_contactList.end(); it++, count++)
334 {
335 Name interestName = (*it)->getNameSpace();
336 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
337 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
338 interest->setChildSelector(Interest::CHILD_RIGHT);
339 interest->setInterestLifetime(1);
340
341 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified,
342 this,
343 _1,
344 count),
345 boost::bind(&ContactPanel::onDnsEndorseeTimeout,
346 this,
347 _1,
348 _2,
349 count),
350 boost::bind(&ContactPanel::onDnsEndorseeUnverified,
351 this,
352 _1,
353 count)));
354
355 m_handler->sendInterest(interest, closure);
356 }
357}
358
359void
360ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count)
361{
362 Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
363 Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
364 EndorseCertificate endorseCertificate(*endorseData);
365
366 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
367
368 updateCollectStatus(count);
369}
370
371void
372ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
373{ updateCollectStatus(count); }
374
375void
376ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count)
377{ updateCollectStatus(count); }
378
379void
380ContactPanel::updateCollectStatus(int count)
381{
382 m_collectStatus->at(count) = true;
383 vector<bool>::const_iterator it = m_collectStatus->begin();
384 for(; it != m_collectStatus->end(); it++)
385 if(*it == false)
386 return;
387
388 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
389}
390
391static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
392
393string
394ContactPanel::getRandomString()
395{
396 string randStr;
397 boost::random::random_device rng;
398 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
399 for (int i = 0; i < 10; i ++)
400 {
401 randStr += chars[index_dist(rng)];
402 }
403 return randStr;
404}
405
406void
Yingdi Yu40eca752013-10-10 15:00:58 -0700407ContactPanel::updateSelection(const QItemSelection &selected,
408 const QItemSelection &deselected)
409{
410 QModelIndexList items = selected.indexes();
411 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800412 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700413
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700414 int i = 0;
415 for(; i < m_contactList.size(); i++)
416 {
417 if(alias == m_contactList[i]->getAlias())
418 break;
419 }
420
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800421 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800422 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
423 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
424 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700425
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800426 if(m_currentSelectedContact->isIntroducer())
427 {
428 ui->isIntroducer->setChecked(true);
429 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800430 ui->deleteScope->setEnabled(true);
431 ui->trustScopeList->setEnabled(true);
432
433 string filter("contact_namespace = '");
434 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
435
436 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
437 m_trustScopeModel->setTable("TrustScope");
438 m_trustScopeModel->setFilter(filter.c_str());
439 m_trustScopeModel->select();
440 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
441 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
442 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
443
Yingdi Yub2e747d2013-11-05 23:06:43 -0800444 ui->trustScopeList->setModel(m_trustScopeModel);
445 ui->trustScopeList->setColumnHidden(0, true);
446 ui->trustScopeList->setColumnHidden(1, true);
447 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800448 }
449 else
450 {
451 ui->isIntroducer->setChecked(false);
452 ui->addScope->setEnabled(false);
453 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800454
455 string filter("contact_namespace = '");
456 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
457
458 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
459 m_trustScopeModel->setTable("TrustScope");
460 m_trustScopeModel->setFilter(filter.c_str());
461 m_trustScopeModel->select();
462 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
463 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
464 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
465
466 ui->trustScopeList->setModel(m_trustScopeModel);
467 ui->trustScopeList->setColumnHidden(0, true);
468 ui->trustScopeList->setColumnHidden(1, true);
469 ui->trustScopeList->show();
470
471 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800472 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800473
474 string filter("profile_identity = '");
475 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
476
477 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
478 m_endorseDataModel->setTable("ContactProfile");
479 m_endorseDataModel->setFilter(filter.c_str());
480 m_endorseDataModel->select();
481
482 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
483 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
484 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
485 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
486
487 ui->endorseList->setModel(m_endorseDataModel);
488 ui->endorseList->setColumnHidden(0, true);
489 ui->endorseList->resizeColumnToContents(1);
490 ui->endorseList->resizeColumnToContents(2);
491 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
492 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700493}
Yingdi Yu847aa862013-10-09 16:35:53 -0700494
Yingdi Yu3b318c12013-10-15 17:54:00 -0700495void
Yingdi Yu46948282013-11-06 18:43:31 -0800496ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700497{
Yingdi Yu46948282013-11-06 18:43:31 -0800498 m_defaultIdentity = Name(identity.toStdString());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800499 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800500 m_nickName = nickName.toStdString();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700501 m_handler->clearInterestFilter(m_inviteListenPrefix);
Yingdi Yu46948282013-11-06 18:43:31 -0800502 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800503 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700504}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700505
506void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700507ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700508{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700509
Yingdi Yu0269c872013-10-16 13:16:49 -0700510void
511ContactPanel::openAddContactPanel()
512{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700513
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700514void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700515ContactPanel::openInvitationDialog()
516{ m_invitationDialog->show(); }
517
518void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700519ContactPanel::refreshContactList()
520{
521 m_contactList = m_contactManager->getContactItemList();
522 QStringList contactNameList;
523 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800524 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700525
526 m_contactListModel->setStringList(contactNameList);
527}
528
529void
530ContactPanel::showContextMenu(const QPoint& pos)
531{
532 QMenu menu(ui->ContactList);
533 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700534 connect(m_menuInvite, SIGNAL(triggered()),
535 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800536 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700537 menu.addAction(m_menuAlias);
538 connect(m_menuAlias, SIGNAL(triggered()),
539 this, SLOT(openSetAliasDialog()));
540 menu.exec(ui->ContactList->mapToGlobal(pos));
541
542}
543
544void
545ContactPanel::openSetAliasDialog()
546{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800547 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700548 m_setAliasDialog->show();
549}
550
Yingdi Yu595aadc2013-10-21 15:01:40 -0700551void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700552ContactPanel::openSettingDialog()
553{
Yingdi Yu46948282013-11-06 18:43:31 -0800554 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700555 m_settingDialog->show();
556}
557
558void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700559ContactPanel::openStartChatDialog()
560{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700561 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700562 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700563
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800564 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700565 m_startChatDialog->show();
566}
567
Yingdi Yu7989eb22013-10-31 17:38:22 -0700568// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700569void
570ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
571{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700572 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yu72781e52013-11-06 23:00:21 -0800573
574 Name inviteeNamespace(invitee.toStdString());
575 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
576
Yingdi Yue6476cd2013-11-06 18:51:19 -0800577 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700578 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800579
580 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
581 this, SLOT(removeChatDialog(const ndn::Name&)));
582
Yingdi Yue6476cd2013-11-06 18:51:19 -0800583 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700584 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700585
586 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700587}
588
Yingdi Yu7989eb22013-10-31 17:38:22 -0700589// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700590void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700591ContactPanel::startChatroom2(const ChronosInvitation& invitation,
592 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700593{
Yingdi Yu72781e52013-11-06 23:00:21 -0800594 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
595
Yingdi Yu7989eb22013-10-31 17:38:22 -0700596 Name chatroomName("/ndn/broadcast/chronos");
597 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800598
Yingdi Yue6476cd2013-11-06 18:51:19 -0800599 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800600
Yingdi Yu46948282013-11-06 18:43:31 -0800601 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
602 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700603
Yingdi Yue6476cd2013-11-06 18:51:19 -0800604 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800605 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700606
Yingdi Yu7989eb22013-10-31 17:38:22 -0700607 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
608
609 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
610
611 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700612}
613
614void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700615ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
616 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700617{
618 string prefix = m_localPrefix.toUri();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700619 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700620 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700621}
622
623void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700624ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700625{
Yingdi Yue9195fd2013-11-08 14:13:42 -0800626 string empty("nack");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700627 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700628}
629
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800630void
631ContactPanel::isIntroducerChanged(int state)
632{
633 if(state == Qt::Checked)
634 {
635 ui->addScope->setEnabled(true);
636 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800637 ui->trustScopeList->setEnabled(true);
638
639 string filter("contact_namespace = '");
640 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800641
642 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
643 m_trustScopeModel->setTable("TrustScope");
644 m_trustScopeModel->setFilter(filter.c_str());
645 m_trustScopeModel->select();
646 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
647 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
648 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800649
650 ui->trustScopeList->setModel(m_trustScopeModel);
651 ui->trustScopeList->setColumnHidden(0, true);
652 ui->trustScopeList->setColumnHidden(1, true);
653 ui->trustScopeList->show();
654
655 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800656 }
657 else
658 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800659 ui->addScope->setEnabled(false);
660 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800661
662 string filter("contact_namespace = '");
663 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
664
665 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
666 m_trustScopeModel->setTable("TrustScope");
667 m_trustScopeModel->setFilter(filter.c_str());
668 m_trustScopeModel->select();
669 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
670 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
671 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
672
673 ui->trustScopeList->setModel(m_trustScopeModel);
674 ui->trustScopeList->setColumnHidden(0, true);
675 ui->trustScopeList->setColumnHidden(1, true);
676 ui->trustScopeList->show();
677
678 ui->trustScopeList->setEnabled(false);
679
680 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800681 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800682 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
683}
684
685void
686ContactPanel::addScopeClicked()
687{
688 int rowCount = m_trustScopeModel->rowCount();
689 QSqlRecord record;
690 QSqlField identityField("contact_namespace", QVariant::String);
691 record.append(identityField);
692 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
693 m_trustScopeModel->insertRow(rowCount);
694 m_trustScopeModel->setRecord(rowCount, record);
695}
696
697void
698ContactPanel::deleteScopeClicked()
699{
700 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
701 QModelIndexList indexList = selectionModel->selectedIndexes();
702
703 int i = indexList.size() - 1;
704 for(; i >= 0; i--)
705 m_trustScopeModel->removeRow(indexList[i].row());
706
707 m_trustScopeModel->submitAll();
708}
709
710void
711ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -0800712{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800713
714void
715ContactPanel::endorseButtonClicked()
716{
717 m_endorseDataModel->submitAll();
718 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800719}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700720
Yingdi Yu46948282013-11-06 18:43:31 -0800721void
722ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
723{
724 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -0800725
726 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -0800727 if(it != m_chatDialogs.end())
728 {
Yingdi Yu46948282013-11-06 18:43:31 -0800729 deletedChat = it->second;
730 m_chatDialogs.erase(it);
731 }
Yingdi Yu83eae842013-11-06 22:07:38 -0800732 if (deletedChat != NULL)
733 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -0800734}
735
Yingdi Yu847aa862013-10-09 16:35:53 -0700736#if WAF
737#include "contactpanel.moc"
738#include "contactpanel.cpp.moc"
739#endif