blob: b22f46030c480c41b2decaaab62b8040a5d72284 [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)
52 , m_menuInvite(new QAction("&Chat", this))
53 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yu847aa862013-10-09 16:35:53 -070054{
Yingdi Yu978b3ae2013-10-23 11:50:51 -070055 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070056 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu3b318c12013-10-15 17:54:00 -070057
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);
66 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070067
Yingdi Yueda39aa2013-10-23 23:07:29 -070068 setKeychain();
69 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070070
Yingdi Yueda39aa2013-10-23 23:07:29 -070071 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -070072
Yingdi Yueda39aa2013-10-23 23:07:29 -070073 // Set Identity, TODO: through user interface
74 m_defaultIdentity = m_keychain->getDefaultIdentity();
75 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
76 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -080077 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -070078
79 ui->ContactList->setModel(m_contactListModel);
80
81 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yu8f7325a2013-10-17 17:03:46 -070082
Yingdi Yueda39aa2013-10-23 23:07:29 -070083 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
84 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -070085
Yingdi Yueda39aa2013-10-23 23:07:29 -070086 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
87 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -070088
Yingdi Yueda39aa2013-10-23 23:07:29 -070089 connect(ui->EditProfileButton, SIGNAL(clicked()),
90 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070091
Yingdi Yueda39aa2013-10-23 23:07:29 -070092 connect(ui->AddContactButton, SIGNAL(clicked()),
93 this, SLOT(openAddContactPanel()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070094
Yingdi Yueda39aa2013-10-23 23:07:29 -070095 connect(ui->settingButton, SIGNAL(clicked()),
96 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070097
Yingdi Yueda39aa2013-10-23 23:07:29 -070098 connect(m_addContactPanel, SIGNAL(newContactAdded()),
99 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700100
Yingdi Yueda39aa2013-10-23 23:07:29 -0700101 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
102 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700103
Yingdi Yueda39aa2013-10-23 23:07:29 -0700104 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
105 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700106
Yingdi Yu7989eb22013-10-31 17:38:22 -0700107 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
108 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
109 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
110 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700111
Yingdi Yueda39aa2013-10-23 23:07:29 -0700112 connect(m_settingDialog, SIGNAL(identitySet(const QString&)),
113 this, SLOT(updateDefaultIdentity(const QString&)));
114
115 connect(this, SIGNAL(newInvitationReady()),
116 this, SLOT(openInvitationDialog()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700117
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800118 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
119 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800120
121 connect(ui->addScope, SIGNAL(clicked()),
122 this, SLOT(addScopeClicked()));
123
124 connect(ui->deleteScope, SIGNAL(clicked()),
125 this, SLOT(deleteScopeClicked()));
126
127 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;
138 delete m_profileEditor;
139 delete m_addContactPanel;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800140 delete m_trustScopeModel;
141 delete m_endorseDataModel;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700142
Yingdi Yueda39aa2013-10-23 23:07:29 -0700143 delete m_menuInvite;
144
145 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
146 for(; it != m_chatDialogs.end(); it++)
147 delete it->second;
Yingdi Yu847aa862013-10-09 16:35:53 -0700148}
149
Yingdi Yu40eca752013-10-10 15:00:58 -0700150void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700151ContactPanel::openDB()
152{
153 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
154 QString path = (QDir::home().path());
155 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
156 db.setDatabaseName(path);
157 bool ok = db.open();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700158 _LOG_DEBUG("db opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800159
160 m_trustScopeModel = new QSqlTableModel;
161 m_endorseDataModel = new QSqlTableModel;
162 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700163}
164
165void
166ContactPanel::setKeychain()
167{
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700168 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700169 // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700170
171 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
172 for(; it != m_contactList.end(); it++)
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700173 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700174
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700175 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
176 m_panelPolicyManager,
177 NULL));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700178}
179
180void
181ContactPanel::setLocalPrefix()
182{
183 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
184 interest->setChildSelector(Interest::CHILD_RIGHT);
185
186 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
187 this,
188 _1),
189 boost::bind(&ContactPanel::onLocalPrefixTimeout,
190 this,
191 _1,
192 _2),
193 boost::bind(&ContactPanel::onLocalPrefixVerified,
194 this,
195 _1)));
196
197 m_handler->sendInterest(interest, closure);
198}
199
200void
201ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
202{
203 string originPrefix(data->content().buf(), data->content().size());
204 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
205 string randomSuffix = getRandomString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700206 m_localPrefix = Name(prefix);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700207 m_localPrefix.append(randomSuffix);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700208}
209
210void
211ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700212{
213 string randomSuffix = getRandomString();
214 m_localPrefix = Name("/private/local");
215 m_localPrefix.append(randomSuffix);
216}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700217
218void
219ContactPanel::onUnverified(Ptr<Data> data)
220{}
221
222void
223ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
224{}
225
226void
Yingdi Yub2e747d2013-11-05 23:06:43 -0800227ContactPanel::collectEndorsement()
228{
229 m_collectStatus = Ptr<vector<bool> >::Create();
230 m_collectStatus->assign(m_contactList.size(), false);
231 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
232 int count = 0;
233 for(; it != m_contactList.end(); it++, count++)
234 {
235 Name interestName = (*it)->getNameSpace();
236 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
237 Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
238 interest->setChildSelector(Interest::CHILD_RIGHT);
239
240 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndoreeVerified,
241 this,
242 _1,
243 count),
244 boost::bind(&ContactPanel::onDnsEndoreeTimeout,
245 this,
246 _1,
247 _2,
248 count),
249 boost::bind(&ContactPanel::onDnsEndoreeUnverified,
250 this,
251 _1,
252 count)));
253
254 m_handler->sendInterest(interest, closure);
255 }
256}
257
258void
259ContactPanel::onDnsEndoreeVerified(Ptr<Data> data, int count)
260{
261 Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
262 Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
263 EndorseCertificate endorseCertificate(*endorseData);
264
265 _LOG_DEBUG("get data: " << endorseCertificate.getName().toUri());
266
267 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
268
269 updateCollectStatus(count);
270}
271
272void
273ContactPanel::onDnsEndoreeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
274{ updateCollectStatus(count); }
275
276void
277ContactPanel::onDnsEndoreeUnverified(Ptr<Data> data, int count)
278{ updateCollectStatus(count); }
279
280void
281ContactPanel::updateCollectStatus(int count)
282{
283 m_collectStatus->at(count) = true;
284 vector<bool>::const_iterator it = m_collectStatus->begin();
285 for(; it != m_collectStatus->end(); it++)
286 if(*it == false)
287 return;
288
289 m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
290}
291
292void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700293ContactPanel::onInvitationCertVerified(Ptr<Data> data,
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700294 Ptr<ChronosInvitation> invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700295{
296 Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700297
298 if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700299 {
300 Name keyName = certificate->getPublicKeyName();
301 Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700302 popChatInvitation(invitation, inviterNameSpace, certificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700303 }
304}
305
306void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700307ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700308 const Name& inviterNameSpace,
309 Ptr<security::IdentityCertificate> certificate)
310{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700311 string chatroom = invitation->getChatroom().get(0).toUri();
312 string inviter = inviterNameSpace.toUri();
313
314 string alias;
315 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
316 for(; it != m_contactList.end(); it++)
317 if((*it)->getNameSpace() == inviterNameSpace)
318 alias = (*it)->getAlias();
319
320 if(it != m_contactList.end())
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700321 return;
322
Yingdi Yu7989eb22013-10-31 17:38:22 -0700323 m_invitationDialog->setInvitation(alias, invitation, certificate);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700324 emit newInvitationReady();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700325}
326
327static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
328
329string
330ContactPanel::getRandomString()
331{
332 string randStr;
333 boost::random::random_device rng;
334 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
335 for (int i = 0; i < 10; i ++)
336 {
337 randStr += chars[index_dist(rng)];
338 }
339 return randStr;
340}
341
342
343void
344ContactPanel::setInvitationListener()
345{
346 Name prefix("/ndn/broadcast/chronos/invitation");
347 prefix.append(m_defaultIdentity);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700348 _LOG_DEBUG("prefix: " << prefix.toUri());
349 m_inviteListenPrefix = prefix;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700350 m_handler->setInterestFilter (prefix,
351 boost::bind(&ContactPanel::onInvitation,
352 this,
353 _1));
354
355}
356
357void
358ContactPanel::onInvitation(Ptr<Interest> interest)
359{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700360 _LOG_DEBUG("receive interest!" << interest->getName().toUri());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700361 const Name& interestName = interest->getName();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700362
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700363 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700364
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700365 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700366 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
367 this,
368 _1,
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700369 invitation),
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700370 boost::bind(&ContactPanel::onTimeout,
371 this,
372 _1,
373 _2),
374 boost::bind(&ContactPanel::onUnverified,
375 this,
376 _1)));
377 m_handler->sendInterest(newInterest, closure);
378}
379
380void
Yingdi Yu40eca752013-10-10 15:00:58 -0700381ContactPanel::updateSelection(const QItemSelection &selected,
382 const QItemSelection &deselected)
383{
384 QModelIndexList items = selected.indexes();
385 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700386 string alias = text.toUtf8().constData();
387
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700388 int i = 0;
389 for(; i < m_contactList.size(); i++)
390 {
391 if(alias == m_contactList[i]->getAlias())
392 break;
393 }
394
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800395 m_currentSelectedContact = m_contactList[i];
396 QString name = QString::fromUtf8(m_currentSelectedContact->getName().c_str());
397 QString institution = QString::fromUtf8(m_currentSelectedContact->getInstitution().c_str());
398 QString nameSpace = QString::fromUtf8(m_currentSelectedContact->getNameSpace().toUri().c_str());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700399 ui->NameData->setText(name);
400 ui->NameSpaceData->setText(nameSpace);
401 ui->InstitutionData->setText(institution);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700402
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800403 // m_currentSelectedContactAlias = alias;
404 // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700405 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800406
407 if(m_currentSelectedContact->isIntroducer())
408 {
409 ui->isIntroducer->setChecked(true);
410 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800411 ui->deleteScope->setEnabled(true);
412 ui->trustScopeList->setEnabled(true);
413
414 string filter("contact_namespace = '");
415 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
416
417 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
418 m_trustScopeModel->setTable("TrustScope");
419 m_trustScopeModel->setFilter(filter.c_str());
420 m_trustScopeModel->select();
421 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
422 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
423 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
424
425 // _LOG_DEBUG("row count: " << m_trustScopeModel->rowCount());
426
427 ui->trustScopeList->setModel(m_trustScopeModel);
428 ui->trustScopeList->setColumnHidden(0, true);
429 ui->trustScopeList->setColumnHidden(1, true);
430 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800431 }
432 else
433 {
434 ui->isIntroducer->setChecked(false);
435 ui->addScope->setEnabled(false);
436 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800437
438 string filter("contact_namespace = '");
439 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
440
441 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
442 m_trustScopeModel->setTable("TrustScope");
443 m_trustScopeModel->setFilter(filter.c_str());
444 m_trustScopeModel->select();
445 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
446 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
447 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
448
449 ui->trustScopeList->setModel(m_trustScopeModel);
450 ui->trustScopeList->setColumnHidden(0, true);
451 ui->trustScopeList->setColumnHidden(1, true);
452 ui->trustScopeList->show();
453
454 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800455 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800456
457 string filter("profile_identity = '");
458 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
459
460 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
461 m_endorseDataModel->setTable("ContactProfile");
462 m_endorseDataModel->setFilter(filter.c_str());
463 m_endorseDataModel->select();
464
465 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
466 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
467 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
468 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
469
470 ui->endorseList->setModel(m_endorseDataModel);
471 ui->endorseList->setColumnHidden(0, true);
472 ui->endorseList->resizeColumnToContents(1);
473 ui->endorseList->resizeColumnToContents(2);
474 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
475 ui->endorseList->show();
476
Yingdi Yu40eca752013-10-10 15:00:58 -0700477}
Yingdi Yu847aa862013-10-09 16:35:53 -0700478
Yingdi Yu3b318c12013-10-15 17:54:00 -0700479void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700480ContactPanel::updateDefaultIdentity(const QString& identity)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700481{
482 m_defaultIdentity = Name(identity.toUtf8().constData());
483 Name prefix("/ndn/broadcast/chronos/invitation");
484 prefix.append(m_defaultIdentity);
485
486 _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
487
488 m_handler->clearInterestFilter(m_inviteListenPrefix);
489 m_handler->setInterestFilter(prefix,
490 boost::bind(&ContactPanel::onInvitation,
491 this,
492 _1));
493 m_inviteListenPrefix = prefix;
Yingdi Yub2e747d2013-11-05 23:06:43 -0800494
495 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700496}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700497
498void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700499ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700500{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700501
Yingdi Yu0269c872013-10-16 13:16:49 -0700502void
503ContactPanel::openAddContactPanel()
504{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700505
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700506void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700507ContactPanel::openInvitationDialog()
508{ m_invitationDialog->show(); }
509
510void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700511ContactPanel::refreshContactList()
512{
513 m_contactList = m_contactManager->getContactItemList();
514 QStringList contactNameList;
515 for(int i = 0; i < m_contactList.size(); i++)
516 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
517
518 m_contactListModel->setStringList(contactNameList);
519}
520
521void
522ContactPanel::showContextMenu(const QPoint& pos)
523{
524 QMenu menu(ui->ContactList);
525 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700526 connect(m_menuInvite, SIGNAL(triggered()),
527 this, SLOT(openStartChatDialog()));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700528 menu.addAction(m_menuAlias);
529 connect(m_menuAlias, SIGNAL(triggered()),
530 this, SLOT(openSetAliasDialog()));
531 menu.exec(ui->ContactList->mapToGlobal(pos));
532
533}
534
535void
536ContactPanel::openSetAliasDialog()
537{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800538 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700539 m_setAliasDialog->show();
540}
541
Yingdi Yu595aadc2013-10-21 15:01:40 -0700542void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700543ContactPanel::openSettingDialog()
544{
545 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
546 m_settingDialog->show();
547}
548
549void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700550ContactPanel::openStartChatDialog()
551{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700552 // TimeInterval ti = time::NowUnixTimestamp();
553 // ostringstream oss;
554 // oss << ti.total_seconds();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700555
556 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700557 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700558
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800559 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700560 m_startChatDialog->show();
561}
562
Yingdi Yu7989eb22013-10-31 17:38:22 -0700563// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700564void
565ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
566{
567 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
568 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
569 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700570
Yingdi Yu7989eb22013-10-31 17:38:22 -0700571 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yueda39aa2013-10-23 23:07:29 -0700572
Yingdi Yu7989eb22013-10-31 17:38:22 -0700573 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700574 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
575
576 //TODO: send invitation
577 Name inviteeNamespace(invitee.toUtf8().constData());
578 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
579
Yingdi Yu7989eb22013-10-31 17:38:22 -0700580 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700581
582 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700583}
584
Yingdi Yu7989eb22013-10-31 17:38:22 -0700585// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700586void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700587ContactPanel::startChatroom2(const ChronosInvitation& invitation,
588 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700589{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700590 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
591 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
592
593 Name chatroomName("/ndn/broadcast/chronos");
594 chatroomName.append(invitation.getChatroom());
595
596 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
597 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
598
599 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
600 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
601
602 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
603
604 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700605}
606
607void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700608ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
609 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700610{
611 string prefix = m_localPrefix.toUri();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700612
613 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700614 //TODO:: open chat dialog
Yingdi Yu7989eb22013-10-31 17:38:22 -0700615 _LOG_DEBUG("TO open chat dialog");
616 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700617}
618
619void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700620ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700621{
622 string empty;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700623 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700624}
625
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800626void
627ContactPanel::isIntroducerChanged(int state)
628{
629 if(state == Qt::Checked)
630 {
631 ui->addScope->setEnabled(true);
632 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800633 ui->trustScopeList->setEnabled(true);
634
635 string filter("contact_namespace = '");
636 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
637 _LOG_DEBUG("filter: " << filter);
638
639 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
640 m_trustScopeModel->setTable("TrustScope");
641 m_trustScopeModel->setFilter(filter.c_str());
642 m_trustScopeModel->select();
643 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
644 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
645 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
646 _LOG_DEBUG("row count: " << m_trustScopeModel->rowCount());
647
648
649 ui->trustScopeList->setModel(m_trustScopeModel);
650 ui->trustScopeList->setColumnHidden(0, true);
651 ui->trustScopeList->setColumnHidden(1, true);
652 ui->trustScopeList->show();
653
654 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800655 }
656 else
657 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800658 ui->addScope->setEnabled(false);
659 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800660
661 string filter("contact_namespace = '");
662 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
663
664 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
665 m_trustScopeModel->setTable("TrustScope");
666 m_trustScopeModel->setFilter(filter.c_str());
667 m_trustScopeModel->select();
668 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
669 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
670 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
671
672 ui->trustScopeList->setModel(m_trustScopeModel);
673 ui->trustScopeList->setColumnHidden(0, true);
674 ui->trustScopeList->setColumnHidden(1, true);
675 ui->trustScopeList->show();
676
677 ui->trustScopeList->setEnabled(false);
678
679 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800680 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800681 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
682}
683
684void
685ContactPanel::addScopeClicked()
686{
687 int rowCount = m_trustScopeModel->rowCount();
688 QSqlRecord record;
689 QSqlField identityField("contact_namespace", QVariant::String);
690 record.append(identityField);
691 record.setValue("contact_namespace", QString(m_currentSelectedContact->getNameSpace().toUri().c_str()));
692 m_trustScopeModel->insertRow(rowCount);
693 m_trustScopeModel->setRecord(rowCount, record);
694}
695
696void
697ContactPanel::deleteScopeClicked()
698{
699 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
700 QModelIndexList indexList = selectionModel->selectedIndexes();
701
702 int i = indexList.size() - 1;
703 for(; i >= 0; i--)
704 m_trustScopeModel->removeRow(indexList[i].row());
705
706 m_trustScopeModel->submitAll();
707}
708
709void
710ContactPanel::saveScopeClicked()
711{
712 m_trustScopeModel->submitAll();
713}
714
715void
716ContactPanel::endorseButtonClicked()
717{
718 m_endorseDataModel->submitAll();
719 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800720}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700721
Yingdi Yu847aa862013-10-09 16:35:53 -0700722#if WAF
723#include "contactpanel.moc"
724#include "contactpanel.cpp.moc"
725#endif