blob: d29fdb09791c4ff06571a4889721bae927e99c5c [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 Yu46948282013-11-06 18:43:31 -0800411 QString name = QString::fromStdString(m_currentSelectedContact->getName());
412 QString institution = QString::fromStdString(m_currentSelectedContact->getInstitution());
413 QString nameSpace = QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700414 ui->NameData->setText(name);
415 ui->NameSpaceData->setText(nameSpace);
416 ui->InstitutionData->setText(institution);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700417
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800418 if(m_currentSelectedContact->isIntroducer())
419 {
420 ui->isIntroducer->setChecked(true);
421 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800422 ui->deleteScope->setEnabled(true);
423 ui->trustScopeList->setEnabled(true);
424
425 string filter("contact_namespace = '");
426 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
427
428 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
429 m_trustScopeModel->setTable("TrustScope");
430 m_trustScopeModel->setFilter(filter.c_str());
431 m_trustScopeModel->select();
432 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
433 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
434 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
435
Yingdi Yub2e747d2013-11-05 23:06:43 -0800436 ui->trustScopeList->setModel(m_trustScopeModel);
437 ui->trustScopeList->setColumnHidden(0, true);
438 ui->trustScopeList->setColumnHidden(1, true);
439 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800440 }
441 else
442 {
443 ui->isIntroducer->setChecked(false);
444 ui->addScope->setEnabled(false);
445 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800446
447 string filter("contact_namespace = '");
448 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
449
450 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
451 m_trustScopeModel->setTable("TrustScope");
452 m_trustScopeModel->setFilter(filter.c_str());
453 m_trustScopeModel->select();
454 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
455 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
456 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
457
458 ui->trustScopeList->setModel(m_trustScopeModel);
459 ui->trustScopeList->setColumnHidden(0, true);
460 ui->trustScopeList->setColumnHidden(1, true);
461 ui->trustScopeList->show();
462
463 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800464 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800465
466 string filter("profile_identity = '");
467 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
468
469 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
470 m_endorseDataModel->setTable("ContactProfile");
471 m_endorseDataModel->setFilter(filter.c_str());
472 m_endorseDataModel->select();
473
474 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
475 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
476 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
477 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
478
479 ui->endorseList->setModel(m_endorseDataModel);
480 ui->endorseList->setColumnHidden(0, true);
481 ui->endorseList->resizeColumnToContents(1);
482 ui->endorseList->resizeColumnToContents(2);
483 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
484 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700485}
Yingdi Yu847aa862013-10-09 16:35:53 -0700486
Yingdi Yu3b318c12013-10-15 17:54:00 -0700487void
Yingdi Yu46948282013-11-06 18:43:31 -0800488ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700489{
Yingdi Yu46948282013-11-06 18:43:31 -0800490 m_defaultIdentity = Name(identity.toStdString());
491 m_nickName = nickName.toStdString();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700492 m_handler->clearInterestFilter(m_inviteListenPrefix);
Yingdi Yu46948282013-11-06 18:43:31 -0800493 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800494 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700495}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700496
497void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700498ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700499{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700500
Yingdi Yu0269c872013-10-16 13:16:49 -0700501void
502ContactPanel::openAddContactPanel()
503{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700504
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700505void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700506ContactPanel::openInvitationDialog()
507{ m_invitationDialog->show(); }
508
509void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700510ContactPanel::refreshContactList()
511{
512 m_contactList = m_contactManager->getContactItemList();
513 QStringList contactNameList;
514 for(int i = 0; i < m_contactList.size(); i++)
515 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
516
517 m_contactListModel->setStringList(contactNameList);
518}
519
520void
521ContactPanel::showContextMenu(const QPoint& pos)
522{
523 QMenu menu(ui->ContactList);
524 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700525 connect(m_menuInvite, SIGNAL(triggered()),
526 this, SLOT(openStartChatDialog()));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700527 menu.addAction(m_menuAlias);
528 connect(m_menuAlias, SIGNAL(triggered()),
529 this, SLOT(openSetAliasDialog()));
530 menu.exec(ui->ContactList->mapToGlobal(pos));
531
532}
533
534void
535ContactPanel::openSetAliasDialog()
536{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800537 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700538 m_setAliasDialog->show();
539}
540
Yingdi Yu595aadc2013-10-21 15:01:40 -0700541void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700542ContactPanel::openSettingDialog()
543{
Yingdi Yu46948282013-11-06 18:43:31 -0800544 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700545 m_settingDialog->show();
546}
547
548void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700549ContactPanel::openStartChatDialog()
550{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700551 // TimeInterval ti = time::NowUnixTimestamp();
552 // ostringstream oss;
553 // oss << ti.total_seconds();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700554
555 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700556 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700557
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800558 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700559 m_startChatDialog->show();
560}
561
Yingdi Yu7989eb22013-10-31 17:38:22 -0700562// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700563void
564ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
565{
566 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
567 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
568 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700569
Yingdi Yu7989eb22013-10-31 17:38:22 -0700570 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yue6476cd2013-11-06 18:51:19 -0800571 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700572 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
Yingdi Yu46948282013-11-06 18:43:31 -0800573
574 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
575 this, SLOT(removeChatDialog(const ndn::Name&)));
576
Yingdi Yue6476cd2013-11-06 18:51:19 -0800577 // send invitation
Yingdi Yueda39aa2013-10-23 23:07:29 -0700578 Name inviteeNamespace(invitee.toUtf8().constData());
579 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
580
Yingdi Yu7989eb22013-10-31 17:38:22 -0700581 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700582
583 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700584}
585
Yingdi Yu7989eb22013-10-31 17:38:22 -0700586// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700587void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700588ContactPanel::startChatroom2(const ChronosInvitation& invitation,
589 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700590{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700591 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
592 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
593
594 Name chatroomName("/ndn/broadcast/chronos");
595 chatroomName.append(invitation.getChatroom());
Yingdi Yue6476cd2013-11-06 18:51:19 -0800596 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
Yingdi Yu46948282013-11-06 18:43:31 -0800597 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
598 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700599
Yingdi Yue6476cd2013-11-06 18:51:19 -0800600 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700601
602 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
603 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
604
605 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
606
Yingdi Yu46948282013-11-06 18:43:31 -0800607
608
Yingdi Yu7989eb22013-10-31 17:38:22 -0700609 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700610}
611
612void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700613ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
614 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700615{
616 string prefix = m_localPrefix.toUri();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700617
618 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700619 //TODO:: open chat dialog
Yingdi Yu7989eb22013-10-31 17:38:22 -0700620 _LOG_DEBUG("TO open chat dialog");
621 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700622}
623
624void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700625ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700626{
627 string empty;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700628 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700629}
630
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800631void
632ContactPanel::isIntroducerChanged(int state)
633{
634 if(state == Qt::Checked)
635 {
636 ui->addScope->setEnabled(true);
637 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800638 ui->trustScopeList->setEnabled(true);
639
640 string filter("contact_namespace = '");
641 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
642 _LOG_DEBUG("filter: " << filter);
643
644 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
645 m_trustScopeModel->setTable("TrustScope");
646 m_trustScopeModel->setFilter(filter.c_str());
647 m_trustScopeModel->select();
648 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
649 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
650 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
651 _LOG_DEBUG("row count: " << m_trustScopeModel->rowCount());
652
653
654 ui->trustScopeList->setModel(m_trustScopeModel);
655 ui->trustScopeList->setColumnHidden(0, true);
656 ui->trustScopeList->setColumnHidden(1, true);
657 ui->trustScopeList->show();
658
659 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800660 }
661 else
662 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800663 ui->addScope->setEnabled(false);
664 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800665
666 string filter("contact_namespace = '");
667 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
668
669 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
670 m_trustScopeModel->setTable("TrustScope");
671 m_trustScopeModel->setFilter(filter.c_str());
672 m_trustScopeModel->select();
673 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
674 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
675 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
676
677 ui->trustScopeList->setModel(m_trustScopeModel);
678 ui->trustScopeList->setColumnHidden(0, true);
679 ui->trustScopeList->setColumnHidden(1, true);
680 ui->trustScopeList->show();
681
682 ui->trustScopeList->setEnabled(false);
683
684 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800685 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800686 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
687}
688
689void
690ContactPanel::addScopeClicked()
691{
692 int rowCount = m_trustScopeModel->rowCount();
693 QSqlRecord record;
694 QSqlField identityField("contact_namespace", QVariant::String);
695 record.append(identityField);
696 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
697 m_trustScopeModel->insertRow(rowCount);
698 m_trustScopeModel->setRecord(rowCount, record);
699}
700
701void
702ContactPanel::deleteScopeClicked()
703{
704 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
705 QModelIndexList indexList = selectionModel->selectedIndexes();
706
707 int i = indexList.size() - 1;
708 for(; i >= 0; i--)
709 m_trustScopeModel->removeRow(indexList[i].row());
710
711 m_trustScopeModel->submitAll();
712}
713
714void
715ContactPanel::saveScopeClicked()
716{
717 m_trustScopeModel->submitAll();
718}
719
720void
721ContactPanel::endorseButtonClicked()
722{
723 m_endorseDataModel->submitAll();
724 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800725}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700726
Yingdi Yu46948282013-11-06 18:43:31 -0800727void
728ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
729{
730 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
731 _LOG_DEBUG("about to leave 2!");
732 ChatDialog* deletedChat;
733 if(it != m_chatDialogs.end())
734 {
735 _LOG_DEBUG("about to leave 3!");
736 deletedChat = it->second;
737 m_chatDialogs.erase(it);
738 }
739 delete deletedChat;
740}
741
Yingdi Yu847aa862013-10-09 16:35:53 -0700742#if WAF
743#include "contactpanel.moc"
744#include "contactpanel.cpp.moc"
745#endif