blob: de35f51452c155e781ed5b192af16599b4116c2c [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 openDB();
59
60 m_contactManager = contactManager;
61 m_profileEditor = new ProfileEditor(m_contactManager);
62 m_addContactPanel = new AddContactPanel(contactManager);
63 m_setAliasDialog = new SetAliasDialog(contactManager);
64
Yingdi Yueda39aa2013-10-23 23:07:29 -070065 ui->setupUi(this);
Yingdi Yu46948282013-11-06 18:43:31 -080066
Yingdi Yueda39aa2013-10-23 23:07:29 -070067 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070068
Yingdi Yueda39aa2013-10-23 23:07:29 -070069 setKeychain();
Yingdi Yu46948282013-11-06 18:43:31 -080070
71 m_defaultIdentity = m_keychain->getDefaultIdentity();
72 m_nickName = m_defaultIdentity.get(-1).toUri();
73 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
74
75 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
76
Yingdi Yueda39aa2013-10-23 23:07:29 -070077 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -070078
Yingdi Yueda39aa2013-10-23 23:07:29 -070079 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -080080
Yingdi Yub2e747d2013-11-05 23:06:43 -080081 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -070082
83 ui->ContactList->setModel(m_contactListModel);
84
Yingdi Yu8f7325a2013-10-17 17:03:46 -070085
Yingdi Yu46948282013-11-06 18:43:31 -080086 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -070087 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -070088
Yingdi Yueda39aa2013-10-23 23:07:29 -070089 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
90 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -070091
Yingdi Yueda39aa2013-10-23 23:07:29 -070092 connect(ui->EditProfileButton, SIGNAL(clicked()),
93 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070094
Yingdi Yueda39aa2013-10-23 23:07:29 -070095 connect(ui->AddContactButton, SIGNAL(clicked()),
96 this, SLOT(openAddContactPanel()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070097
Yingdi Yueda39aa2013-10-23 23:07:29 -070098 connect(ui->settingButton, SIGNAL(clicked()),
99 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700100
Yingdi Yueda39aa2013-10-23 23:07:29 -0700101 connect(m_addContactPanel, SIGNAL(newContactAdded()),
102 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700103 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
104 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700105
Yingdi Yueda39aa2013-10-23 23:07:29 -0700106 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
107 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700108
Yingdi Yu7989eb22013-10-31 17:38:22 -0700109 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
110 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
111 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
112 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700113
Yingdi Yu46948282013-11-06 18:43:31 -0800114 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
115 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700116
117 connect(this, SIGNAL(newInvitationReady()),
118 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700119
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800120 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
121 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800122
123 connect(ui->addScope, SIGNAL(clicked()),
124 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800125 connect(ui->deleteScope, SIGNAL(clicked()),
126 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800127 connect(ui->saveButton, SIGNAL(clicked()),
128 this, SLOT(saveScopeClicked()));
129
130 connect(ui->endorseButton, SIGNAL(clicked()),
131 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700132}
133
134ContactPanel::~ContactPanel()
135{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700136 delete ui;
137 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800138 delete m_startChatDialog;
139 delete m_invitationDialog;
140 delete m_settingDialog;
141
Yingdi Yueda39aa2013-10-23 23:07:29 -0700142 delete m_profileEditor;
143 delete m_addContactPanel;
Yingdi Yu46948282013-11-06 18:43:31 -0800144 delete m_setAliasDialog;
145
Yingdi Yub2e747d2013-11-05 23:06:43 -0800146 delete m_trustScopeModel;
147 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800148 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700149
Yingdi Yueda39aa2013-10-23 23:07:29 -0700150 delete m_menuInvite;
Yingdi Yu46948282013-11-06 18:43:31 -0800151 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700152
153 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
154 for(; it != m_chatDialogs.end(); it++)
155 delete it->second;
Yingdi Yu847aa862013-10-09 16:35:53 -0700156}
157
Yingdi Yu40eca752013-10-10 15:00:58 -0700158void
Yingdi Yu46948282013-11-06 18:43:31 -0800159ContactPanel::createAction()
160{
161 m_menuInvite = new QAction("&Chat", this);
162 m_menuAlias = new QAction("&Set Alias", this);
163}
164
165void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700166ContactPanel::openDB()
167{
168 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
169 QString path = (QDir::home().path());
170 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
171 db.setDatabaseName(path);
172 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800173 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800174
175 m_trustScopeModel = new QSqlTableModel;
176 m_endorseDataModel = new QSqlTableModel;
177 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700178}
179
180void
181ContactPanel::setKeychain()
182{
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700183 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700184
185 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
186 for(; it != m_contactList.end(); it++)
Yingdi Yu46948282013-11-06 18:43:31 -0800187 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700188
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700189 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
190 m_panelPolicyManager,
191 NULL));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700192}
193
194void
195ContactPanel::setLocalPrefix()
196{
197 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
198 interest->setChildSelector(Interest::CHILD_RIGHT);
199
200 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
201 this,
202 _1),
203 boost::bind(&ContactPanel::onLocalPrefixTimeout,
204 this,
205 _1,
206 _2),
207 boost::bind(&ContactPanel::onLocalPrefixVerified,
208 this,
209 _1)));
210
211 m_handler->sendInterest(interest, closure);
212}
213
214void
215ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
216{
217 string originPrefix(data->content().buf(), data->content().size());
218 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
219 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700220 m_localPrefix = Name(prefix);
221}
222
223void
224ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700225{
226 string randomSuffix = getRandomString();
227 m_localPrefix = Name("/private/local");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700228}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700229
230void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700231ContactPanel::setInvitationListener()
232{
Yingdi Yu46948282013-11-06 18:43:31 -0800233 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
234 m_inviteListenPrefix.append(m_defaultIdentity);
235 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
236 m_handler->setInterestFilter (m_inviteListenPrefix,
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700237 boost::bind(&ContactPanel::onInvitation,
238 this,
239 _1));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700240}
241
242void
243ContactPanel::onInvitation(Ptr<Interest> interest)
244{
Yingdi Yu46948282013-11-06 18:43:31 -0800245 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700246
Yingdi Yu46948282013-11-06 18:43:31 -0800247 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName()));
248
249 Name chatroomName("/ndn/broadcast/chronos");
250 chatroomName.append(invitation->getChatroom());
251 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
252 if(it != m_chatDialogs.end())
253 {
254 _LOG_DEBUG("Exisiting chatroom!");
255 return;
256 }
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700257
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700258 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700259 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
260 this,
261 _1,
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700262 invitation),
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700263 boost::bind(&ContactPanel::onTimeout,
264 this,
265 _1,
266 _2),
267 boost::bind(&ContactPanel::onUnverified,
268 this,
269 _1)));
270 m_handler->sendInterest(newInterest, closure);
271}
272
273void
Yingdi Yu46948282013-11-06 18:43:31 -0800274ContactPanel::onInvitationCertVerified(Ptr<Data> data,
275 Ptr<ChronosInvitation> invitation)
276{
277 using namespace ndn::security;
278 Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
279
280 if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
281 {
282 Name keyName = certificate->getPublicKeyName();
283 Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1);
284 popChatInvitation(invitation, inviterNameSpace, certificate);
285 }
286}
287
288void
289ContactPanel::onUnverified(Ptr<Data> data)
290{}
291
292void
293ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
294{}
295
296void
297ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
298 const Name& inviterNameSpace,
299 Ptr<security::IdentityCertificate> certificate)
300{
301 string alias;
302 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
303 for(; it != m_contactList.end(); it++)
304 if((*it)->getNameSpace() == inviterNameSpace)
305 alias = (*it)->getAlias();
306
307 if(it != m_contactList.end())
308 return;
309
310 m_invitationDialog->setInvitation(alias, invitation, certificate);
311 emit newInvitationReady();
312}
313
314void
315ContactPanel::collectEndorsement()
316{
317 m_collectStatus = Ptr<vector<bool> >::Create();
318 m_collectStatus->assign(m_contactList.size(), false);
319
320 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
321 int count = 0;
322 for(; it != m_contactList.end(); it++, count++)
323 {
324 Name interestName = (*it)->getNameSpace();
325 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
326 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
327 interest->setChildSelector(Interest::CHILD_RIGHT);
328 interest->setInterestLifetime(1);
329
330 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified,
331 this,
332 _1,
333 count),
334 boost::bind(&ContactPanel::onDnsEndorseeTimeout,
335 this,
336 _1,
337 _2,
338 count),
339 boost::bind(&ContactPanel::onDnsEndorseeUnverified,
340 this,
341 _1,
342 count)));
343
344 m_handler->sendInterest(interest, closure);
345 }
346}
347
348void
349ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count)
350{
351 Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
352 Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
353 EndorseCertificate endorseCertificate(*endorseData);
354
355 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
356
357 updateCollectStatus(count);
358}
359
360void
361ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
362{ updateCollectStatus(count); }
363
364void
365ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count)
366{ updateCollectStatus(count); }
367
368void
369ContactPanel::updateCollectStatus(int count)
370{
371 m_collectStatus->at(count) = true;
372 vector<bool>::const_iterator it = m_collectStatus->begin();
373 for(; it != m_collectStatus->end(); it++)
374 if(*it == false)
375 return;
376
377 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
378}
379
380static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
381
382string
383ContactPanel::getRandomString()
384{
385 string randStr;
386 boost::random::random_device rng;
387 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
388 for (int i = 0; i < 10; i ++)
389 {
390 randStr += chars[index_dist(rng)];
391 }
392 return randStr;
393}
394
395void
Yingdi Yu40eca752013-10-10 15:00:58 -0700396ContactPanel::updateSelection(const QItemSelection &selected,
397 const QItemSelection &deselected)
398{
399 QModelIndexList items = selected.indexes();
400 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800401 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700402
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700403 int i = 0;
404 for(; i < m_contactList.size(); i++)
405 {
406 if(alias == m_contactList[i]->getAlias())
407 break;
408 }
409
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800410 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800411 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
412 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
413 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700414
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800415 if(m_currentSelectedContact->isIntroducer())
416 {
417 ui->isIntroducer->setChecked(true);
418 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800419 ui->deleteScope->setEnabled(true);
420 ui->trustScopeList->setEnabled(true);
421
422 string filter("contact_namespace = '");
423 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
424
425 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
426 m_trustScopeModel->setTable("TrustScope");
427 m_trustScopeModel->setFilter(filter.c_str());
428 m_trustScopeModel->select();
429 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
430 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
431 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
432
Yingdi Yub2e747d2013-11-05 23:06:43 -0800433 ui->trustScopeList->setModel(m_trustScopeModel);
434 ui->trustScopeList->setColumnHidden(0, true);
435 ui->trustScopeList->setColumnHidden(1, true);
436 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800437 }
438 else
439 {
440 ui->isIntroducer->setChecked(false);
441 ui->addScope->setEnabled(false);
442 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800443
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
455 ui->trustScopeList->setModel(m_trustScopeModel);
456 ui->trustScopeList->setColumnHidden(0, true);
457 ui->trustScopeList->setColumnHidden(1, true);
458 ui->trustScopeList->show();
459
460 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800461 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800462
463 string filter("profile_identity = '");
464 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
465
466 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
467 m_endorseDataModel->setTable("ContactProfile");
468 m_endorseDataModel->setFilter(filter.c_str());
469 m_endorseDataModel->select();
470
471 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
472 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
473 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
474 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
475
476 ui->endorseList->setModel(m_endorseDataModel);
477 ui->endorseList->setColumnHidden(0, true);
478 ui->endorseList->resizeColumnToContents(1);
479 ui->endorseList->resizeColumnToContents(2);
480 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
481 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700482}
Yingdi Yu847aa862013-10-09 16:35:53 -0700483
Yingdi Yu3b318c12013-10-15 17:54:00 -0700484void
Yingdi Yu46948282013-11-06 18:43:31 -0800485ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700486{
Yingdi Yu46948282013-11-06 18:43:31 -0800487 m_defaultIdentity = Name(identity.toStdString());
488 m_nickName = nickName.toStdString();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700489 m_handler->clearInterestFilter(m_inviteListenPrefix);
Yingdi Yu46948282013-11-06 18:43:31 -0800490 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800491 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700492}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700493
494void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700495ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700496{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700497
Yingdi Yu0269c872013-10-16 13:16:49 -0700498void
499ContactPanel::openAddContactPanel()
500{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700501
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700502void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700503ContactPanel::openInvitationDialog()
504{ m_invitationDialog->show(); }
505
506void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700507ContactPanel::refreshContactList()
508{
509 m_contactList = m_contactManager->getContactItemList();
510 QStringList contactNameList;
511 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800512 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700513
514 m_contactListModel->setStringList(contactNameList);
515}
516
517void
518ContactPanel::showContextMenu(const QPoint& pos)
519{
520 QMenu menu(ui->ContactList);
521 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700522 connect(m_menuInvite, SIGNAL(triggered()),
523 this, SLOT(openStartChatDialog()));
Yingdi Yu83eae842013-11-06 22:07:38 -0800524 menu.addSeparator();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700525 menu.addAction(m_menuAlias);
526 connect(m_menuAlias, SIGNAL(triggered()),
527 this, SLOT(openSetAliasDialog()));
528 menu.exec(ui->ContactList->mapToGlobal(pos));
529
530}
531
532void
533ContactPanel::openSetAliasDialog()
534{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800535 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700536 m_setAliasDialog->show();
537}
538
Yingdi Yu595aadc2013-10-21 15:01:40 -0700539void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700540ContactPanel::openSettingDialog()
541{
Yingdi Yu46948282013-11-06 18:43:31 -0800542 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700543 m_settingDialog->show();
544}
545
546void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700547ContactPanel::openStartChatDialog()
548{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700549 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700550 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700551
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800552 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700553 m_startChatDialog->show();
554}
555
Yingdi Yu7989eb22013-10-31 17:38:22 -0700556// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700557void
558ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
559{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700560 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yu72781e52013-11-06 23:00:21 -0800561
562 Name inviteeNamespace(invitee.toStdString());
563 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
564
Yingdi Yue6476cd2013-11-06 18:51:19 -0800565 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700566 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800567
568 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
569 this, SLOT(removeChatDialog(const ndn::Name&)));
570
Yingdi Yue6476cd2013-11-06 18:51:19 -0800571 // send invitation
Yingdi Yu7989eb22013-10-31 17:38:22 -0700572 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700573
574 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700575}
576
Yingdi Yu7989eb22013-10-31 17:38:22 -0700577// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700578void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700579ContactPanel::startChatroom2(const ChronosInvitation& invitation,
580 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700581{
Yingdi Yu72781e52013-11-06 23:00:21 -0800582 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
583
Yingdi Yu7989eb22013-10-31 17:38:22 -0700584 Name chatroomName("/ndn/broadcast/chronos");
585 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800586
Yingdi Yue6476cd2013-11-06 18:51:19 -0800587 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800588
Yingdi Yu46948282013-11-06 18:43:31 -0800589 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
590 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700591
Yingdi Yue6476cd2013-11-06 18:51:19 -0800592 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu83eae842013-11-06 22:07:38 -0800593 chatDialog->publishIntroCert(identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700594
Yingdi Yu7989eb22013-10-31 17:38:22 -0700595 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
596
597 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
598
599 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700600}
601
602void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700603ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
604 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700605{
606 string prefix = m_localPrefix.toUri();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700607 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700608 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700609}
610
611void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700612ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700613{
614 string empty;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700615 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700616}
617
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800618void
619ContactPanel::isIntroducerChanged(int state)
620{
621 if(state == Qt::Checked)
622 {
623 ui->addScope->setEnabled(true);
624 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800625 ui->trustScopeList->setEnabled(true);
626
627 string filter("contact_namespace = '");
628 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800629
630 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
631 m_trustScopeModel->setTable("TrustScope");
632 m_trustScopeModel->setFilter(filter.c_str());
633 m_trustScopeModel->select();
634 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
635 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
636 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800637
638 ui->trustScopeList->setModel(m_trustScopeModel);
639 ui->trustScopeList->setColumnHidden(0, true);
640 ui->trustScopeList->setColumnHidden(1, true);
641 ui->trustScopeList->show();
642
643 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800644 }
645 else
646 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800647 ui->addScope->setEnabled(false);
648 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800649
650 string filter("contact_namespace = '");
651 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
652
653 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
654 m_trustScopeModel->setTable("TrustScope");
655 m_trustScopeModel->setFilter(filter.c_str());
656 m_trustScopeModel->select();
657 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
658 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
659 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
660
661 ui->trustScopeList->setModel(m_trustScopeModel);
662 ui->trustScopeList->setColumnHidden(0, true);
663 ui->trustScopeList->setColumnHidden(1, true);
664 ui->trustScopeList->show();
665
666 ui->trustScopeList->setEnabled(false);
667
668 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800669 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800670 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
671}
672
673void
674ContactPanel::addScopeClicked()
675{
676 int rowCount = m_trustScopeModel->rowCount();
677 QSqlRecord record;
678 QSqlField identityField("contact_namespace", QVariant::String);
679 record.append(identityField);
680 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
681 m_trustScopeModel->insertRow(rowCount);
682 m_trustScopeModel->setRecord(rowCount, record);
683}
684
685void
686ContactPanel::deleteScopeClicked()
687{
688 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
689 QModelIndexList indexList = selectionModel->selectedIndexes();
690
691 int i = indexList.size() - 1;
692 for(; i >= 0; i--)
693 m_trustScopeModel->removeRow(indexList[i].row());
694
695 m_trustScopeModel->submitAll();
696}
697
698void
699ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -0800700{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800701
702void
703ContactPanel::endorseButtonClicked()
704{
705 m_endorseDataModel->submitAll();
706 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800707}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700708
Yingdi Yu46948282013-11-06 18:43:31 -0800709void
710ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
711{
712 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -0800713
714 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -0800715 if(it != m_chatDialogs.end())
716 {
Yingdi Yu46948282013-11-06 18:43:31 -0800717 deletedChat = it->second;
718 m_chatDialogs.erase(it);
719 }
Yingdi Yu83eae842013-11-06 22:07:38 -0800720 if (deletedChat != NULL)
721 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -0800722}
723
Yingdi Yu847aa862013-10-09 16:35:53 -0700724#if WAF
725#include "contactpanel.moc"
726#include "contactpanel.cpp.moc"
727#endif