blob: 361b7bdb88375f90e5cbb84411fe26fa0abe7a16 [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 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 Yuc4d08d22013-10-23 23:07:29 -070065 ui->setupUi(this);
Yingdi Yu42372442013-11-06 18:43:31 -080066
Yingdi Yuc4d08d22013-10-23 23:07:29 -070067 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070068
Yingdi Yuc4d08d22013-10-23 23:07:29 -070069 setKeychain();
Yingdi Yu42372442013-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 Yuc4d08d22013-10-23 23:07:29 -070077 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -070078
Yingdi Yuc4d08d22013-10-23 23:07:29 -070079 setInvitationListener();
Yingdi Yu42372442013-11-06 18:43:31 -080080
Yingdi Yu8dacdf22013-11-05 23:06:43 -080081 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -070082
83 ui->ContactList->setModel(m_contactListModel);
84
Yingdi Yuc26af3c2013-10-17 17:03:46 -070085
Yingdi Yu42372442013-11-06 18:43:31 -080086 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yuc4d08d22013-10-23 23:07:29 -070087 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -070088
Yingdi Yuc4d08d22013-10-23 23:07:29 -070089 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
90 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -070091
Yingdi Yuc4d08d22013-10-23 23:07:29 -070092 connect(ui->EditProfileButton, SIGNAL(clicked()),
93 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -070094
Yingdi Yuc4d08d22013-10-23 23:07:29 -070095 connect(ui->AddContactButton, SIGNAL(clicked()),
96 this, SLOT(openAddContactPanel()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070097
Yingdi Yuc4d08d22013-10-23 23:07:29 -070098 connect(ui->settingButton, SIGNAL(clicked()),
99 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700100
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700101 connect(m_addContactPanel, SIGNAL(newContactAdded()),
102 this, SLOT(refreshContactList()));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700103 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
104 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700105
Yingdi Yuc4d08d22013-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 Yu53eb8a72013-10-23 11:50:51 -0700108
Yingdi Yu42f66462013-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 Yu53eb8a72013-10-23 11:50:51 -0700113
Yingdi Yu42372442013-11-06 18:43:31 -0800114 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
115 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700116
117 connect(this, SIGNAL(newInvitationReady()),
118 this, SLOT(openInvitationDialog()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700119
Yingdi Yua50c3252013-11-03 15:05:26 -0800120 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
121 this, SLOT(isIntroducerChanged(int)));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800122
123 connect(ui->addScope, SIGNAL(clicked()),
124 this, SLOT(addScopeClicked()));
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800125 connect(ui->deleteScope, SIGNAL(clicked()),
126 this, SLOT(deleteScopeClicked()));
Yingdi Yu8dacdf22013-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 Yudef90612013-10-09 22:34:42 -0700132}
133
134ContactPanel::~ContactPanel()
135{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700136 delete ui;
137 delete m_contactListModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800138 delete m_startChatDialog;
139 delete m_invitationDialog;
140 delete m_settingDialog;
141
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700142 delete m_profileEditor;
143 delete m_addContactPanel;
Yingdi Yu42372442013-11-06 18:43:31 -0800144 delete m_setAliasDialog;
145
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800146 delete m_trustScopeModel;
147 delete m_endorseDataModel;
Yingdi Yu42372442013-11-06 18:43:31 -0800148 delete m_endorseComboBoxDelegate;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700149
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700150 delete m_menuInvite;
Yingdi Yu42372442013-11-06 18:43:31 -0800151 delete m_menuAlias;
Yingdi Yuc4d08d22013-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 Yudef90612013-10-09 22:34:42 -0700156}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700157
Yingdi Yu01a942b2013-10-10 15:00:58 -0700158void
Yingdi Yu42372442013-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 Yu53eb8a72013-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 Yu42372442013-11-06 18:43:31 -0800173 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800174
175 m_trustScopeModel = new QSqlTableModel;
176 m_endorseDataModel = new QSqlTableModel;
177 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700178}
179
180void
181ContactPanel::setKeychain()
182{
Yingdi Yued8cfc42013-11-01 17:37:51 -0700183 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700184
185 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
186 for(; it != m_contactList.end(); it++)
Yingdi Yu42372442013-11-06 18:43:31 -0800187 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700188
Yingdi Yued8cfc42013-11-01 17:37:51 -0700189 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
190 m_panelPolicyManager,
191 NULL));
Yingdi Yu53eb8a72013-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 Yu53eb8a72013-10-23 11:50:51 -0700220 m_localPrefix = Name(prefix);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700221 m_localPrefix.append(randomSuffix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700222}
223
224void
225ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu42f66462013-10-31 17:38:22 -0700226{
227 string randomSuffix = getRandomString();
228 m_localPrefix = Name("/private/local");
229 m_localPrefix.append(randomSuffix);
230}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700231
232void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700233ContactPanel::setInvitationListener()
234{
Yingdi Yu42372442013-11-06 18:43:31 -0800235 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
236 m_inviteListenPrefix.append(m_defaultIdentity);
237 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
238 m_handler->setInterestFilter (m_inviteListenPrefix,
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700239 boost::bind(&ContactPanel::onInvitation,
240 this,
241 _1));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700242}
243
244void
245ContactPanel::onInvitation(Ptr<Interest> interest)
246{
Yingdi Yu42372442013-11-06 18:43:31 -0800247 _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700248
Yingdi Yu42372442013-11-06 18:43:31 -0800249 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName()));
250
251 Name chatroomName("/ndn/broadcast/chronos");
252 chatroomName.append(invitation->getChatroom());
253 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
254 if(it != m_chatDialogs.end())
255 {
256 _LOG_DEBUG("Exisiting chatroom!");
257 return;
258 }
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700259
Yingdi Yued8cfc42013-11-01 17:37:51 -0700260 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700261 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
262 this,
263 _1,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700264 invitation),
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700265 boost::bind(&ContactPanel::onTimeout,
266 this,
267 _1,
268 _2),
269 boost::bind(&ContactPanel::onUnverified,
270 this,
271 _1)));
272 m_handler->sendInterest(newInterest, closure);
273}
274
275void
Yingdi Yu42372442013-11-06 18:43:31 -0800276ContactPanel::onInvitationCertVerified(Ptr<Data> data,
277 Ptr<ChronosInvitation> invitation)
278{
279 using namespace ndn::security;
280 Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
281
282 if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
283 {
284 Name keyName = certificate->getPublicKeyName();
285 Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1);
286 popChatInvitation(invitation, inviterNameSpace, certificate);
287 }
288}
289
290void
291ContactPanel::onUnverified(Ptr<Data> data)
292{}
293
294void
295ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
296{}
297
298void
299ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
300 const Name& inviterNameSpace,
301 Ptr<security::IdentityCertificate> certificate)
302{
303 string alias;
304 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
305 for(; it != m_contactList.end(); it++)
306 if((*it)->getNameSpace() == inviterNameSpace)
307 alias = (*it)->getAlias();
308
309 if(it != m_contactList.end())
310 return;
311
312 m_invitationDialog->setInvitation(alias, invitation, certificate);
313 emit newInvitationReady();
314}
315
316void
317ContactPanel::collectEndorsement()
318{
319 m_collectStatus = Ptr<vector<bool> >::Create();
320 m_collectStatus->assign(m_contactList.size(), false);
321
322 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
323 int count = 0;
324 for(; it != m_contactList.end(); it++, count++)
325 {
326 Name interestName = (*it)->getNameSpace();
327 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
328 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
329 interest->setChildSelector(Interest::CHILD_RIGHT);
330 interest->setInterestLifetime(1);
331
332 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified,
333 this,
334 _1,
335 count),
336 boost::bind(&ContactPanel::onDnsEndorseeTimeout,
337 this,
338 _1,
339 _2,
340 count),
341 boost::bind(&ContactPanel::onDnsEndorseeUnverified,
342 this,
343 _1,
344 count)));
345
346 m_handler->sendInterest(interest, closure);
347 }
348}
349
350void
351ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count)
352{
353 Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
354 Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
355 EndorseCertificate endorseCertificate(*endorseData);
356
357 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
358
359 updateCollectStatus(count);
360}
361
362void
363ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
364{ updateCollectStatus(count); }
365
366void
367ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count)
368{ updateCollectStatus(count); }
369
370void
371ContactPanel::updateCollectStatus(int count)
372{
373 m_collectStatus->at(count) = true;
374 vector<bool>::const_iterator it = m_collectStatus->begin();
375 for(; it != m_collectStatus->end(); it++)
376 if(*it == false)
377 return;
378
379 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
380}
381
382static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
383
384string
385ContactPanel::getRandomString()
386{
387 string randStr;
388 boost::random::random_device rng;
389 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
390 for (int i = 0; i < 10; i ++)
391 {
392 randStr += chars[index_dist(rng)];
393 }
394 return randStr;
395}
396
397void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700398ContactPanel::updateSelection(const QItemSelection &selected,
399 const QItemSelection &deselected)
400{
401 QModelIndexList items = selected.indexes();
402 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu42372442013-11-06 18:43:31 -0800403 string alias = text.toStdString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700404
Yingdi Yu79c25a22013-10-21 13:38:38 -0700405 int i = 0;
406 for(; i < m_contactList.size(); i++)
407 {
408 if(alias == m_contactList[i]->getAlias())
409 break;
410 }
411
Yingdi Yua50c3252013-11-03 15:05:26 -0800412 m_currentSelectedContact = m_contactList[i];
Yingdi Yu42372442013-11-06 18:43:31 -0800413 QString name = QString::fromStdString(m_currentSelectedContact->getName());
414 QString institution = QString::fromStdString(m_currentSelectedContact->getInstitution());
415 QString nameSpace = QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700416 ui->NameData->setText(name);
417 ui->NameSpaceData->setText(nameSpace);
418 ui->InstitutionData->setText(institution);
Yingdi Yu100267f2013-10-21 15:01:40 -0700419
Yingdi Yua50c3252013-11-03 15:05:26 -0800420 if(m_currentSelectedContact->isIntroducer())
421 {
422 ui->isIntroducer->setChecked(true);
423 ui->addScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800424 ui->deleteScope->setEnabled(true);
425 ui->trustScopeList->setEnabled(true);
426
427 string filter("contact_namespace = '");
428 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
429
430 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
431 m_trustScopeModel->setTable("TrustScope");
432 m_trustScopeModel->setFilter(filter.c_str());
433 m_trustScopeModel->select();
434 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
435 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
436 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
437
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800438 ui->trustScopeList->setModel(m_trustScopeModel);
439 ui->trustScopeList->setColumnHidden(0, true);
440 ui->trustScopeList->setColumnHidden(1, true);
441 ui->trustScopeList->show();
Yingdi Yua50c3252013-11-03 15:05:26 -0800442 }
443 else
444 {
445 ui->isIntroducer->setChecked(false);
446 ui->addScope->setEnabled(false);
447 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800448
449 string filter("contact_namespace = '");
450 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
451
452 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
453 m_trustScopeModel->setTable("TrustScope");
454 m_trustScopeModel->setFilter(filter.c_str());
455 m_trustScopeModel->select();
456 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
457 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
458 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
459
460 ui->trustScopeList->setModel(m_trustScopeModel);
461 ui->trustScopeList->setColumnHidden(0, true);
462 ui->trustScopeList->setColumnHidden(1, true);
463 ui->trustScopeList->show();
464
465 ui->trustScopeList->setEnabled(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800466 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800467
468 string filter("profile_identity = '");
469 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
470
471 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
472 m_endorseDataModel->setTable("ContactProfile");
473 m_endorseDataModel->setFilter(filter.c_str());
474 m_endorseDataModel->select();
475
476 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
477 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
478 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
479 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
480
481 ui->endorseList->setModel(m_endorseDataModel);
482 ui->endorseList->setColumnHidden(0, true);
483 ui->endorseList->resizeColumnToContents(1);
484 ui->endorseList->resizeColumnToContents(2);
485 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
486 ui->endorseList->show();
Yingdi Yu01a942b2013-10-10 15:00:58 -0700487}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700488
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700489void
Yingdi Yu42372442013-11-06 18:43:31 -0800490ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700491{
Yingdi Yu42372442013-11-06 18:43:31 -0800492 m_defaultIdentity = Name(identity.toStdString());
493 m_nickName = nickName.toStdString();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700494 m_handler->clearInterestFilter(m_inviteListenPrefix);
Yingdi Yu42372442013-11-06 18:43:31 -0800495 setInvitationListener();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800496 collectEndorsement();
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700497}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700498
499void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700500ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700501{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700502
Yingdi Yud59b5932013-10-16 13:16:49 -0700503void
504ContactPanel::openAddContactPanel()
505{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700506
Yingdi Yu79c25a22013-10-21 13:38:38 -0700507void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700508ContactPanel::openInvitationDialog()
509{ m_invitationDialog->show(); }
510
511void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700512ContactPanel::refreshContactList()
513{
514 m_contactList = m_contactManager->getContactItemList();
515 QStringList contactNameList;
516 for(int i = 0; i < m_contactList.size(); i++)
517 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
518
519 m_contactListModel->setStringList(contactNameList);
520}
521
522void
523ContactPanel::showContextMenu(const QPoint& pos)
524{
525 QMenu menu(ui->ContactList);
526 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700527 connect(m_menuInvite, SIGNAL(triggered()),
528 this, SLOT(openStartChatDialog()));
Yingdi Yu79c25a22013-10-21 13:38:38 -0700529 menu.addAction(m_menuAlias);
530 connect(m_menuAlias, SIGNAL(triggered()),
531 this, SLOT(openSetAliasDialog()));
532 menu.exec(ui->ContactList->mapToGlobal(pos));
533
534}
535
536void
537ContactPanel::openSetAliasDialog()
538{
Yingdi Yua50c3252013-11-03 15:05:26 -0800539 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700540 m_setAliasDialog->show();
541}
542
Yingdi Yu100267f2013-10-21 15:01:40 -0700543void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700544ContactPanel::openSettingDialog()
545{
Yingdi Yu42372442013-11-06 18:43:31 -0800546 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700547 m_settingDialog->show();
548}
549
550void
Yingdi Yu100267f2013-10-21 15:01:40 -0700551ContactPanel::openStartChatDialog()
552{
Yingdi Yu42f66462013-10-31 17:38:22 -0700553 // TimeInterval ti = time::NowUnixTimestamp();
554 // ostringstream oss;
555 // oss << ti.total_seconds();
Yingdi Yu100267f2013-10-21 15:01:40 -0700556
557 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700558 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700559
Yingdi Yua50c3252013-11-03 15:05:26 -0800560 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu100267f2013-10-21 15:01:40 -0700561 m_startChatDialog->show();
562}
563
Yingdi Yu42f66462013-10-31 17:38:22 -0700564// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700565void
566ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
567{
568 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
569 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
570 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700571
Yingdi Yu42f66462013-10-31 17:38:22 -0700572 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yu42372442013-11-06 18:43:31 -0800573 Name prefix = m_localPrefix.getPrefix(m_localPrefix.size()-1);
574 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, prefix, m_defaultIdentity, m_nickName);
575 // ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700576 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu42372442013-11-06 18:43:31 -0800577
578 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
579 this, SLOT(removeChatDialog(const ndn::Name&)));
580
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700581 //TODO: send invitation
582 Name inviteeNamespace(invitee.toUtf8().constData());
583 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
584
Yingdi Yu42f66462013-10-31 17:38:22 -0700585 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700586
587 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700588}
589
Yingdi Yu42f66462013-10-31 17:38:22 -0700590// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700591void
Yingdi Yu42f66462013-10-31 17:38:22 -0700592ContactPanel::startChatroom2(const ChronosInvitation& invitation,
593 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700594{
Yingdi Yu42f66462013-10-31 17:38:22 -0700595 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
596 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
597
598 Name chatroomName("/ndn/broadcast/chronos");
599 chatroomName.append(invitation.getChatroom());
Yingdi Yu42372442013-11-06 18:43:31 -0800600 Name prefix = m_localPrefix.getPrefix(m_localPrefix.size()-1);
601 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, prefix, m_defaultIdentity, m_nickName, true);
602 // ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
603 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
604 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu42f66462013-10-31 17:38:22 -0700605
Yingdi Yu42372442013-11-06 18:43:31 -0800606 Name inviterPrefix = invitation.getInviterPrefix().getPrefix(invitation.getInviterPrefix().size()-1);
607 chatDialog->addChatDataRule(inviterPrefix, identityCertificate, true);
608 // chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu42f66462013-10-31 17:38:22 -0700609
610 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
611 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
612
613 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
614
Yingdi Yu42372442013-11-06 18:43:31 -0800615
616
Yingdi Yu42f66462013-10-31 17:38:22 -0700617 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700618}
619
620void
Yingdi Yu42f66462013-10-31 17:38:22 -0700621ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
622 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700623{
624 string prefix = m_localPrefix.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700625
626 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700627 //TODO:: open chat dialog
Yingdi Yu42f66462013-10-31 17:38:22 -0700628 _LOG_DEBUG("TO open chat dialog");
629 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700630}
631
632void
Yingdi Yu42f66462013-10-31 17:38:22 -0700633ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700634{
635 string empty;
Yingdi Yu42f66462013-10-31 17:38:22 -0700636 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700637}
638
Yingdi Yua50c3252013-11-03 15:05:26 -0800639void
640ContactPanel::isIntroducerChanged(int state)
641{
642 if(state == Qt::Checked)
643 {
644 ui->addScope->setEnabled(true);
645 ui->deleteScope->setEnabled(true);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800646 ui->trustScopeList->setEnabled(true);
647
648 string filter("contact_namespace = '");
649 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
650 _LOG_DEBUG("filter: " << filter);
651
652 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
653 m_trustScopeModel->setTable("TrustScope");
654 m_trustScopeModel->setFilter(filter.c_str());
655 m_trustScopeModel->select();
656 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
657 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
658 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
659 _LOG_DEBUG("row count: " << m_trustScopeModel->rowCount());
660
661
662 ui->trustScopeList->setModel(m_trustScopeModel);
663 ui->trustScopeList->setColumnHidden(0, true);
664 ui->trustScopeList->setColumnHidden(1, true);
665 ui->trustScopeList->show();
666
667 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yua50c3252013-11-03 15:05:26 -0800668 }
669 else
670 {
Yingdi Yua50c3252013-11-03 15:05:26 -0800671 ui->addScope->setEnabled(false);
672 ui->deleteScope->setEnabled(false);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800673
674 string filter("contact_namespace = '");
675 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
676
677 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
678 m_trustScopeModel->setTable("TrustScope");
679 m_trustScopeModel->setFilter(filter.c_str());
680 m_trustScopeModel->select();
681 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
682 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
683 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
684
685 ui->trustScopeList->setModel(m_trustScopeModel);
686 ui->trustScopeList->setColumnHidden(0, true);
687 ui->trustScopeList->setColumnHidden(1, true);
688 ui->trustScopeList->show();
689
690 ui->trustScopeList->setEnabled(false);
691
692 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yua50c3252013-11-03 15:05:26 -0800693 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800694 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
695}
696
697void
698ContactPanel::addScopeClicked()
699{
700 int rowCount = m_trustScopeModel->rowCount();
701 QSqlRecord record;
702 QSqlField identityField("contact_namespace", QVariant::String);
703 record.append(identityField);
704 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
705 m_trustScopeModel->insertRow(rowCount);
706 m_trustScopeModel->setRecord(rowCount, record);
707}
708
709void
710ContactPanel::deleteScopeClicked()
711{
712 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
713 QModelIndexList indexList = selectionModel->selectedIndexes();
714
715 int i = indexList.size() - 1;
716 for(; i >= 0; i--)
717 m_trustScopeModel->removeRow(indexList[i].row());
718
719 m_trustScopeModel->submitAll();
720}
721
722void
723ContactPanel::saveScopeClicked()
724{
725 m_trustScopeModel->submitAll();
726}
727
728void
729ContactPanel::endorseButtonClicked()
730{
731 m_endorseDataModel->submitAll();
732 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yua50c3252013-11-03 15:05:26 -0800733}
Yingdi Yu100267f2013-10-21 15:01:40 -0700734
Yingdi Yu42372442013-11-06 18:43:31 -0800735void
736ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
737{
738 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
739 _LOG_DEBUG("about to leave 2!");
740 ChatDialog* deletedChat;
741 if(it != m_chatDialogs.end())
742 {
743 _LOG_DEBUG("about to leave 3!");
744 deletedChat = it->second;
745 m_chatDialogs.erase(it);
746 }
747 delete deletedChat;
748}
749
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700750#if WAF
751#include "contactpanel.moc"
752#include "contactpanel.cpp.moc"
753#endif