blob: 413f32bb7e42436bcbd0447350a7badca678ff1e [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 Yufa4ce792014-02-06 18:09:22 -080024#include <ndn-cpp-dev/security/validator.hpp>
Yingdi Yub6fb0302014-01-21 11:05:11 -080025#include <ndn-cpp-dev/security/signature-sha256-with-rsa.hpp>
Yingdi Yu3b318c12013-10-15 17:54:00 -070026#include <boost/filesystem.hpp>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070027#include <boost/random/random_device.hpp>
28#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu3b318c12013-10-15 17:54:00 -070029#include "logging.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070030#endif
31
32namespace fs = boost::filesystem;
33using namespace ndn;
Yingdi Yufa4ce792014-02-06 18:09:22 -080034using namespace chronos;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070035using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070036
37INIT_LOGGER("ContactPanel");
38
Yingdi Yu76dd8002013-12-24 11:16:32 +080039Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070040
Yingdi Yufa4ce792014-02-06 18:09:22 -080041ContactPanel::ContactPanel(shared_ptr<Face> face,
42 QWidget *parent)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080043 : QDialog(parent)
44 , ui(new Ui::ContactPanel)
Yingdi Yu8e135832013-11-09 20:12:31 -080045 , m_warningDialog(new WarningDialog)
Yingdi Yuec5e72a2013-11-03 15:05:26 -080046 , m_contactListModel(new QStringListModel)
47 , m_startChatDialog(new StartChatDialog)
48 , m_invitationDialog(new InvitationDialog)
49 , m_settingDialog(new SettingDialog)
Yingdi Yufa4ce792014-02-06 18:09:22 -080050 , m_keyChain(new KeyChain)
51 , m_face(face)
52 , m_ioService(face->ioService())
53 , m_contactManager(new ContactManager(m_face))
Yingdi Yu847aa862013-10-09 16:35:53 -070054{
Yingdi Yu6eabbd72013-12-27 08:44:12 +080055
Yingdi Yufa4ce792014-02-06 18:09:22 -080056 qRegisterMetaType<ndn::IdentityCertificate>("IdentityCertificate");
Yingdi Yu6eabbd72013-12-27 08:44:12 +080057
Yingdi Yu46948282013-11-06 18:43:31 -080058 createAction();
59
Yingdi Yufa4ce792014-02-06 18:09:22 -080060#ifdef WITH_SECURITY
61 m_panelValidator = make_shared<chronos::ValidatorPanel>();
62 m_invitationValidator = make_shared<chronos::ValidatorInvitation>();
63#else
64 m_panelValidator = make_shared<ndn::ValidatorNull>();
65 m_invitationValidator = make_shared<ndn::ValidatorNull>();
66#endif
Yingdi Yue35bdb82013-11-07 11:32:40 -080067
Yingdi Yu8e135832013-11-09 20:12:31 -080068 connect(&*m_contactManager, SIGNAL(noNdnConnection(const QString&)),
69 this, SLOT(showError(const QString&)));
Yingdi Yu8e135832013-11-09 20:12:31 -080070
Yingdi Yue35bdb82013-11-07 11:32:40 -080071 openDB();
Yingdi Yu46948282013-11-06 18:43:31 -080072
Yingdi Yueda39aa2013-10-23 23:07:29 -070073 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070074
Yingdi Yu76dd8002013-12-24 11:16:32 +080075 loadTrustAnchor();
Yingdi Yu46948282013-11-06 18:43:31 -080076
Yingdi Yuf8f572d2014-01-13 11:19:47 -080077 m_defaultIdentity = m_keyChain->getDefaultIdentity();
Yingdi Yufa4ce792014-02-06 18:09:22 -080078
Yingdi Yub5a0fd32013-11-10 16:26:52 -080079 if(m_defaultIdentity.size() == 0)
80 showError(QString::fromStdString("certificate of ") + QString::fromStdString(m_defaultIdentity.toUri()) + " is missing!\nHave you installed the certificate?");
Yingdi Yub5a0fd32013-11-10 16:26:52 -080081
Yingdi Yufa4ce792014-02-06 18:09:22 -080082 m_keyChain->createIdentity(m_defaultIdentity);
Yingdi Yub5a0fd32013-11-10 16:26:52 -080083
84 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu76dd8002013-12-24 11:16:32 +080085 m_nickName = m_defaultIdentity.get(-1).toEscapedString();
Yingdi Yub5a0fd32013-11-10 16:26:52 -080086 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu711f5892013-11-09 22:18:26 -080087
Yingdi Yu46948282013-11-06 18:43:31 -080088
Yingdi Yue35bdb82013-11-07 11:32:40 -080089 m_profileEditor = new ProfileEditor(m_contactManager);
90 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
91
Yingdi Yu8e135832013-11-09 20:12:31 -080092 m_addContactPanel = new AddContactPanel(m_contactManager);
93 m_browseContactDialog = new BrowseContactDialog(m_contactManager);
94 m_setAliasDialog = new SetAliasDialog(m_contactManager);
Yingdi Yue35bdb82013-11-07 11:32:40 -080095
96 ui->setupUi(this);
97
Yingdi Yu8e135832013-11-09 20:12:31 -080098
Yingdi Yua66183f2013-11-10 10:32:18 -080099 m_localPrefix = Name("/private/local");
Yingdi Yueda39aa2013-10-23 23:07:29 -0700100 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700101
Yingdi Yueda39aa2013-10-23 23:07:29 -0700102 setInvitationListener();
Yingdi Yu46948282013-11-06 18:43:31 -0800103
Yingdi Yub2e747d2013-11-05 23:06:43 -0800104 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700105
106 ui->ContactList->setModel(m_contactListModel);
107
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700108
Yingdi Yu46948282013-11-06 18:43:31 -0800109 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700110 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -0700111
Yingdi Yueda39aa2013-10-23 23:07:29 -0700112 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
113 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -0700114
Yingdi Yueda39aa2013-10-23 23:07:29 -0700115 connect(ui->EditProfileButton, SIGNAL(clicked()),
116 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -0700117
Yingdi Yu711f5892013-11-09 22:18:26 -0800118 connect(m_profileEditor, SIGNAL(noKeyOrCert(const QString&)),
Yingdi Yue433a502013-11-10 01:34:57 -0800119 this, SLOT(showWarning(const QString&)));
Yingdi Yu711f5892013-11-09 22:18:26 -0800120
Yingdi Yueda39aa2013-10-23 23:07:29 -0700121 connect(ui->AddContactButton, SIGNAL(clicked()),
Yingdi Yuae8217c2013-11-09 00:03:26 -0800122 this, SLOT(openBrowseContactDialog()));
123
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800124 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
125 this, SLOT(openAddContactPanel()));
126
Yingdi Yuae8217c2013-11-09 00:03:26 -0800127 connect(this, SIGNAL(refreshCertDirectory()),
128 m_browseContactDialog, SLOT(refreshList()));
129
130 connect(ui->DeleteContactButton, SIGNAL(clicked()),
131 this, SLOT(removeContactButton()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700132
Yingdi Yueda39aa2013-10-23 23:07:29 -0700133 connect(ui->settingButton, SIGNAL(clicked()),
134 this, SLOT(openSettingDialog()));
Yingdi Yufa4ce792014-02-06 18:09:22 -0800135
136 connect(ui->chatButton, SIGNAL(clicked()),
137 this, SLOT(openStartChatDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700138
Yingdi Yu9d7dfc22013-11-10 17:59:58 -0800139 connect(m_addContactPanel, SIGNAL(newContactAdded()),
140 this, SLOT(refreshContactList()));
Yingdi Yuae8217c2013-11-09 00:03:26 -0800141 connect(m_browseContactDialog, SIGNAL(newContactAdded()),
Yingdi Yueda39aa2013-10-23 23:07:29 -0700142 this, SLOT(refreshContactList()));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700143 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
144 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700145
Yingdi Yufa4ce792014-02-06 18:09:22 -0800146 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&)),
147 this, SLOT(startChatroom(const QString&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700148
Yingdi Yufa4ce792014-02-06 18:09:22 -0800149 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ndn::Name&)),
150 this, SLOT(acceptInvitation(const ndn::Name&)));
151 connect(m_invitationDialog, SIGNAL(invitationRejected(const ndn::Name&)),
152 this, SLOT(rejectInvitation(const ndn::Name&)));
Yingdi Yu72232692013-11-12 17:50:21 -0800153
154 connect(&*m_contactManager, SIGNAL(contactAdded(const ndn::Name&)),
Yingdi Yufa4ce792014-02-06 18:09:22 -0800155 this, SLOT(addContactIntoValidator(const ndn::Name&)));
Yingdi Yu72232692013-11-12 17:50:21 -0800156 connect(&*m_contactManager, SIGNAL(contactRemoved(const ndn::Name&)),
Yingdi Yufa4ce792014-02-06 18:09:22 -0800157 this, SLOT(removeContactFromValidator(const ndn::Name&)));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700158
Yingdi Yu46948282013-11-06 18:43:31 -0800159 connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
160 this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
Yingdi Yueda39aa2013-10-23 23:07:29 -0700161
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800162 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
163 this, SLOT(isIntroducerChanged(int)));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800164
165 connect(ui->addScope, SIGNAL(clicked()),
166 this, SLOT(addScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800167 connect(ui->deleteScope, SIGNAL(clicked()),
168 this, SLOT(deleteScopeClicked()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800169 connect(ui->saveButton, SIGNAL(clicked()),
170 this, SLOT(saveScopeClicked()));
171
172 connect(ui->endorseButton, SIGNAL(clicked()),
173 this, SLOT(endorseButtonClicked()));
Yingdi Yu847aa862013-10-09 16:35:53 -0700174}
175
176ContactPanel::~ContactPanel()
177{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700178 delete ui;
179 delete m_contactListModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800180 delete m_startChatDialog;
181 delete m_invitationDialog;
182 delete m_settingDialog;
183
Yingdi Yueda39aa2013-10-23 23:07:29 -0700184 delete m_profileEditor;
185 delete m_addContactPanel;
Yingdi Yuae8217c2013-11-09 00:03:26 -0800186 delete m_browseContactDialog;
Yingdi Yu46948282013-11-06 18:43:31 -0800187 delete m_setAliasDialog;
188
Yingdi Yub2e747d2013-11-05 23:06:43 -0800189 delete m_trustScopeModel;
190 delete m_endorseDataModel;
Yingdi Yu46948282013-11-06 18:43:31 -0800191 delete m_endorseComboBoxDelegate;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700192
Yingdi Yu46948282013-11-06 18:43:31 -0800193 delete m_menuAlias;
Yingdi Yueda39aa2013-10-23 23:07:29 -0700194
195 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
196 for(; it != m_chatDialogs.end(); it++)
197 delete it->second;
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800198}
199
200void
Yingdi Yu46948282013-11-06 18:43:31 -0800201ContactPanel::createAction()
202{
Yingdi Yu46948282013-11-06 18:43:31 -0800203 m_menuAlias = new QAction("&Set Alias", this);
204}
205
206void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700207ContactPanel::openDB()
208{
209 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
210 QString path = (QDir::home().path());
211 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
212 db.setDatabaseName(path);
213 bool ok = db.open();
Yingdi Yu46948282013-11-06 18:43:31 -0800214 _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yub2e747d2013-11-05 23:06:43 -0800215
216 m_trustScopeModel = new QSqlTableModel;
217 m_endorseDataModel = new QSqlTableModel;
218 m_endorseComboBoxDelegate = new EndorseComboBoxDelegate;
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700219}
220
221void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800222ContactPanel::loadTrustAnchor()
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700223{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800224#ifdef WITH_SECURITY
Yingdi Yu76dd8002013-12-24 11:16:32 +0800225 vector<shared_ptr<ContactItem> >::const_iterator it = m_contactList.begin();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700226 for(; it != m_contactList.end(); it++)
Yingdi Yue35bdb82013-11-07 11:32:40 -0800227 {
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800228 _LOG_DEBUG("load contact: " << (*it)->getNameSpace().toUri());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800229 m_invitationValidator->addTrustAnchor((*it)->getSelfEndorseCertificate());
230 m_panelValidator->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yue35bdb82013-11-07 11:32:40 -0800231 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800232#endif
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700233}
234
235void
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800236ContactPanel::setLocalPrefix(int retry)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700237{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800238 Name interestName("/local/ndn/prefix");
239 Interest interest(interestName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700240
Yingdi Yu76dd8002013-12-24 11:16:32 +0800241 m_face->expressInterest(interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800242 bind(&ContactPanel::onLocalPrefix, this, _1, _2),
243 bind(&ContactPanel::onLocalPrefixTimeout, this, _1, 10));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800244
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700245}
246
247void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800248ContactPanel::onLocalPrefix(const Interest& interest, Data& data)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700249{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800250 string originPrefix(reinterpret_cast<const char*>(data.getContent().value()), data.getContent().value_size());
251 string prefix = QString::fromStdString (originPrefix).trimmed ().toStdString();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700252 m_localPrefix = Name(prefix);
253}
254
255void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800256ContactPanel::onLocalPrefixTimeout(const Interest& interest, int retry)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700257{
Yingdi Yufdb8ab82013-11-10 01:38:21 -0800258 if(retry > 0)
Yingdi Yua66183f2013-11-10 10:32:18 -0800259 {
260 setLocalPrefix(retry - 1);
261 return;
262 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800263 else
Yingdi Yua66183f2013-11-10 10:32:18 -0800264 m_localPrefix = Name("/private/local");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700265}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700266
267void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700268ContactPanel::setInvitationListener()
269{
Yingdi Yu46948282013-11-06 18:43:31 -0800270 m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
271 m_inviteListenPrefix.append(m_defaultIdentity);
272 _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800273 m_invitationListenerId = m_face->setInterestFilter(m_inviteListenPrefix,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800274 bind(&ContactPanel::onInvitation, this, _1, _2),
275 bind(&ContactPanel::onInvitationRegisterFailed, this, _1, _2));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700276}
277
278void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800279ContactPanel::sendInterest(const Interest& interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800280 const OnDataValidated& onValidated,
281 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800282 const TimeoutNotify& timeoutNotify,
Yingdi Yu68de61d2014-01-16 19:13:03 -0800283 int retry /* = 1 */)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800284{
285 m_face->expressInterest(interest,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800286 bind(&ContactPanel::onTargetData,
287 this, _1, _2, onValidated, onValidationFailed),
288 bind(&ContactPanel::onTargetTimeout,
289 this, _1, retry, onValidated, onValidationFailed, timeoutNotify));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800290}
291
292void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800293ContactPanel::onTargetData(const ndn::Interest& interest,
294 Data& data,
295 const OnDataValidated& onValidated,
296 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800297{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800298 m_panelValidator->validate(data, onValidated, onValidationFailed);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800299}
300
301void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800302ContactPanel::onTargetTimeout(const ndn::Interest& interest,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800303 int retry,
Yingdi Yufa4ce792014-02-06 18:09:22 -0800304 const OnDataValidated& onValidated,
305 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu76dd8002013-12-24 11:16:32 +0800306 const TimeoutNotify& timeoutNotify)
307{
308 if(retry > 0)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800309 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, retry-1);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800310 else
311 {
Yingdi Yufa4ce792014-02-06 18:09:22 -0800312 _LOG_DEBUG("Interest: " << interest.getName() << " eventually times out!");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800313 timeoutNotify();
314 }
315}
316
317void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800318ContactPanel::onInvitationRegisterFailed(const Name& prefix, const string& msg)
Yingdi Yu76dd8002013-12-24 11:16:32 +0800319{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800320 showError(QString::fromStdString("Cannot register invitation listening prefix! " + msg));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800321}
322
323void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800324ContactPanel::onInvitation(const Name& prefix, const Interest& interest)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700325{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800326 _LOG_DEBUG("Receive invitation!" << interest.getName() << " " << prefix);
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800327
Yingdi Yufa4ce792014-02-06 18:09:22 -0800328 OnInterestValidated onValidated = bind(&ContactPanel::onInvitationValidated, this, _1);
329 OnInterestValidationFailed onValidationFailed = bind(&ContactPanel::onInvitationValidationFailed, this, _1);
330 m_invitationValidator->validate(interest, onValidated, onValidationFailed);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700331}
332
333void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800334ContactPanel::popChatInvitation(const Name& interestName)
Yingdi Yu46948282013-11-06 18:43:31 -0800335{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800336 Invitation invitation(interestName);
Yingdi Yu46948282013-11-06 18:43:31 -0800337 string alias;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800338
Yingdi Yu76dd8002013-12-24 11:16:32 +0800339 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800340 for(; it != m_contactList.end(); it++)
Yingdi Yufa4ce792014-02-06 18:09:22 -0800341 if((*it)->getNameSpace() == invitation.getInviteeNameSpace())
Yingdi Yu46948282013-11-06 18:43:31 -0800342 alias = (*it)->getAlias();
343
344 if(it != m_contactList.end())
345 return;
346
Yingdi Yufa4ce792014-02-06 18:09:22 -0800347 m_invitationDialog->setInvitation(alias, interestName);
348 m_invitationDialog->show();
Yingdi Yu46948282013-11-06 18:43:31 -0800349}
350
351void
352ContactPanel::collectEndorsement()
353{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800354#ifdef WITH_SECURITY
Yingdi Yu76dd8002013-12-24 11:16:32 +0800355 m_collectStatus = make_shared<vector<bool> >();
Yingdi Yu46948282013-11-06 18:43:31 -0800356 m_collectStatus->assign(m_contactList.size(), false);
357
Yingdi Yu76dd8002013-12-24 11:16:32 +0800358 vector<shared_ptr<ContactItem> >::iterator it = m_contactList.begin();
Yingdi Yu46948282013-11-06 18:43:31 -0800359 int count = 0;
360 for(; it != m_contactList.end(); it++, count++)
361 {
362 Name interestName = (*it)->getNameSpace();
363 interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
Yingdi Yu76dd8002013-12-24 11:16:32 +0800364 Interest interest(interestName);
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800365 interest.setInterestLifetime(1000);
Yingdi Yu46948282013-11-06 18:43:31 -0800366
Yingdi Yufa4ce792014-02-06 18:09:22 -0800367 OnDataValidated onValidated = bind(&ContactPanel::onDnsEndorseeValidated, this, _1, count);
368 OnDataValidationFailed onValidationFailed = bind(&ContactPanel::onDnsEndorseeValidationFailed, this, _1, count);
369 TimeoutNotify timeoutNotify = bind(&ContactPanel::onDnsEndorseeTimeoutNotify, this, count);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800370
Yingdi Yufa4ce792014-02-06 18:09:22 -0800371 sendInterest(interest, onValidated, onValidationFailed, timeoutNotify, 0);
Yingdi Yu46948282013-11-06 18:43:31 -0800372 }
Yingdi Yufa4ce792014-02-06 18:09:22 -0800373#endif
Yingdi Yu46948282013-11-06 18:43:31 -0800374}
375
376void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800377ContactPanel::onDnsEndorseeValidated(const shared_ptr<const Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800378{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800379 Data endorseData;
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800380 endorseData.wireDecode(Block(data->getContent().value(), data->getContent().value_size()));
Yingdi Yu76dd8002013-12-24 11:16:32 +0800381 EndorseCertificate endorseCertificate(endorseData);
Yingdi Yu46948282013-11-06 18:43:31 -0800382
383 m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
384
385 updateCollectStatus(count);
386}
387
388void
Yingdi Yu76dd8002013-12-24 11:16:32 +0800389ContactPanel::onDnsEndorseeTimeoutNotify(int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800390{ updateCollectStatus(count); }
391
392void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800393ContactPanel::onDnsEndorseeValidationFailed(const shared_ptr<const Data>& data, int count)
Yingdi Yu46948282013-11-06 18:43:31 -0800394{ updateCollectStatus(count); }
395
396void
397ContactPanel::updateCollectStatus(int count)
398{
399 m_collectStatus->at(count) = true;
400 vector<bool>::const_iterator it = m_collectStatus->begin();
401 for(; it != m_collectStatus->end(); it++)
402 if(*it == false)
403 return;
404
Yingdi Yufa4ce792014-02-06 18:09:22 -0800405 m_contactManager->publishCollectEndorsedDataInDNS(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800406}
407
408static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
409
410string
411ContactPanel::getRandomString()
412{
413 string randStr;
414 boost::random::random_device rng;
415 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
416 for (int i = 0; i < 10; i ++)
417 {
418 randStr += chars[index_dist(rng)];
419 }
420 return randStr;
421}
422
423void
Yingdi Yu8e135832013-11-09 20:12:31 -0800424ContactPanel::showError(const QString& msg){
425 QMessageBox::critical(this, tr("Chronos"), msg, QMessageBox::Ok);
426 exit(1);
427}
428
429void
Yingdi Yue433a502013-11-10 01:34:57 -0800430ContactPanel::showWarning(const QString& msg){
431 QMessageBox::information(this, tr("Chronos"), msg);
432}
433
434void
Yingdi Yu40eca752013-10-10 15:00:58 -0700435ContactPanel::updateSelection(const QItemSelection &selected,
436 const QItemSelection &deselected)
437{
438 QModelIndexList items = selected.indexes();
439 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu46948282013-11-06 18:43:31 -0800440 string alias = text.toStdString();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700441
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700442 int i = 0;
443 for(; i < m_contactList.size(); i++)
444 {
445 if(alias == m_contactList[i]->getAlias())
446 break;
447 }
448
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800449 m_currentSelectedContact = m_contactList[i];
Yingdi Yu83eae842013-11-06 22:07:38 -0800450 ui->NameData->setText(QString::fromStdString(m_currentSelectedContact->getName()));
451 ui->NameSpaceData->setText(QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
452 ui->InstitutionData->setText(QString::fromStdString(m_currentSelectedContact->getInstitution()));
Yingdi Yu595aadc2013-10-21 15:01:40 -0700453
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800454 if(m_currentSelectedContact->isIntroducer())
455 {
456 ui->isIntroducer->setChecked(true);
457 ui->addScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800458 ui->deleteScope->setEnabled(true);
459 ui->trustScopeList->setEnabled(true);
460
461 string filter("contact_namespace = '");
462 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
463
464 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
465 m_trustScopeModel->setTable("TrustScope");
466 m_trustScopeModel->setFilter(filter.c_str());
467 m_trustScopeModel->select();
468 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
469 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
470 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
471
Yingdi Yub2e747d2013-11-05 23:06:43 -0800472 ui->trustScopeList->setModel(m_trustScopeModel);
473 ui->trustScopeList->setColumnHidden(0, true);
474 ui->trustScopeList->setColumnHidden(1, true);
475 ui->trustScopeList->show();
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800476 }
477 else
478 {
479 ui->isIntroducer->setChecked(false);
480 ui->addScope->setEnabled(false);
481 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800482
483 string filter("contact_namespace = '");
484 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
485
486 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
487 m_trustScopeModel->setTable("TrustScope");
488 m_trustScopeModel->setFilter(filter.c_str());
489 m_trustScopeModel->select();
490 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
491 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
492 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
493
494 ui->trustScopeList->setModel(m_trustScopeModel);
495 ui->trustScopeList->setColumnHidden(0, true);
496 ui->trustScopeList->setColumnHidden(1, true);
497 ui->trustScopeList->show();
498
499 ui->trustScopeList->setEnabled(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800500 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800501
502 string filter("profile_identity = '");
503 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
504
505 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
506 m_endorseDataModel->setTable("ContactProfile");
507 m_endorseDataModel->setFilter(filter.c_str());
508 m_endorseDataModel->select();
509
510 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
511 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
512 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
513 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
514
515 ui->endorseList->setModel(m_endorseDataModel);
516 ui->endorseList->setColumnHidden(0, true);
517 ui->endorseList->resizeColumnToContents(1);
518 ui->endorseList->resizeColumnToContents(2);
519 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
520 ui->endorseList->show();
Yingdi Yu40eca752013-10-10 15:00:58 -0700521}
Yingdi Yu847aa862013-10-09 16:35:53 -0700522
Yingdi Yu3b318c12013-10-15 17:54:00 -0700523void
Yingdi Yu46948282013-11-06 18:43:31 -0800524ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700525{
Yingdi Yu711f5892013-11-09 22:18:26 -0800526 // _LOG_DEBUG(identity.toStdString());
Yingdi Yufa4ce792014-02-06 18:09:22 -0800527 Name defaultIdentity(identity.toStdString());
528 Name defaultCertName = m_keyChain->createIdentity(defaultIdentity);
529
Yingdi Yue433a502013-11-10 01:34:57 -0800530 m_defaultIdentity = defaultIdentity;
Yingdi Yue35bdb82013-11-07 11:32:40 -0800531 m_profileEditor->setCurrentIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800532 m_nickName = nickName.toStdString();
Yingdi Yufa4ce792014-02-06 18:09:22 -0800533
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800534 m_face->unsetInterestFilter(m_invitationListenerId);
Yingdi Yuae8217c2013-11-09 00:03:26 -0800535 m_contactManager->setDefaultIdentity(m_defaultIdentity);
Yingdi Yu46948282013-11-06 18:43:31 -0800536 setInvitationListener();
Yingdi Yub2e747d2013-11-05 23:06:43 -0800537 collectEndorsement();
Yingdi Yueda39aa2013-10-23 23:07:29 -0700538}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700539
540void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700541ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700542{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700543
Yingdi Yu0269c872013-10-16 13:16:49 -0700544void
545ContactPanel::openAddContactPanel()
546{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700547
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700548void
Yingdi Yuae8217c2013-11-09 00:03:26 -0800549ContactPanel::openBrowseContactDialog()
550{
551 m_browseContactDialog->show();
552 emit refreshCertDirectory();
553}
554
555void
556ContactPanel::removeContactButton()
557{
558 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
559 QModelIndexList selectedList = selectionModel->selectedIndexes();
560 QModelIndexList::iterator it = selectedList.begin();
561 for(; it != selectedList.end(); it++)
562 {
563 string alias = m_contactListModel->data(*it, Qt::DisplayRole).toString().toStdString();
Yingdi Yu76dd8002013-12-24 11:16:32 +0800564 vector<shared_ptr<ContactItem> >::iterator contactIt = m_contactList.begin();
Yingdi Yuae8217c2013-11-09 00:03:26 -0800565 for(; contactIt != m_contactList.end(); contactIt++)
566 {
567 if((*contactIt)->getAlias() == alias)
568 {
569 m_contactManager->getContactStorage()->removeContact((*contactIt)->getNameSpace());
570 m_contactList.erase(contactIt);
571 break;
572 }
573 }
574 }
575 refreshContactList();
576}
577
578void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800579ContactPanel::addContactIntoValidator(const Name& contactNameSpace)
Yingdi Yu72232692013-11-12 17:50:21 -0800580{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800581#ifdef WITH_SECURITY
Yingdi Yu76dd8002013-12-24 11:16:32 +0800582 shared_ptr<ContactItem> contact = m_contactManager->getContact(contactNameSpace);
Yingdi Yu93adb1a2014-01-16 10:30:26 -0800583 if(static_cast<bool>(contact))
Yingdi Yufa4ce792014-02-06 18:09:22 -0800584 {
585 m_panelValidator->addTrustAnchor(contact->getSelfEndorseCertificate());
586 m_invitationValidator->addTrustAnchor(contact->getSelfEndorseCertificate());
587 }
588#endif
Yingdi Yu72232692013-11-12 17:50:21 -0800589}
590
591void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800592ContactPanel::removeContactFromValidator(const Name& keyName)
593{
594#ifdef WITH_SECURITY
595 m_panelValidator->removeTrustAnchor(keyName);
596 m_invitationValidator->removeTrustAnchor(keyName);
597#endif
598}
Yingdi Yu72232692013-11-12 17:50:21 -0800599
600void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700601ContactPanel::refreshContactList()
602{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800603 m_contactList.clear();
604 m_contactManager->getContactItemList(m_contactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700605 QStringList contactNameList;
606 for(int i = 0; i < m_contactList.size(); i++)
Yingdi Yu83eae842013-11-06 22:07:38 -0800607 contactNameList << QString::fromStdString(m_contactList[i]->getAlias());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700608
609 m_contactListModel->setStringList(contactNameList);
610}
611
612void
613ContactPanel::showContextMenu(const QPoint& pos)
614{
615 QMenu menu(ui->ContactList);
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700616 menu.addAction(m_menuAlias);
617 connect(m_menuAlias, SIGNAL(triggered()),
618 this, SLOT(openSetAliasDialog()));
619 menu.exec(ui->ContactList->mapToGlobal(pos));
620
621}
622
623void
624ContactPanel::openSetAliasDialog()
625{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800626 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700627 m_setAliasDialog->show();
628}
629
Yingdi Yu595aadc2013-10-21 15:01:40 -0700630void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700631ContactPanel::openSettingDialog()
632{
Yingdi Yu46948282013-11-06 18:43:31 -0800633 m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700634 m_settingDialog->show();
635}
636
637void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700638ContactPanel::openStartChatDialog()
639{
Yingdi Yu595aadc2013-10-21 15:01:40 -0700640 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700641 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700642
Yingdi Yufa4ce792014-02-06 18:09:22 -0800643 m_startChatDialog->setChatroom(chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700644 m_startChatDialog->show();
645}
646
Yingdi Yu7989eb22013-10-31 17:38:22 -0700647// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700648void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800649ContactPanel::startChatroom(const QString& chatroom)
Yingdi Yu595aadc2013-10-21 15:01:40 -0700650{
Yingdi Yu76dd8002013-12-24 11:16:32 +0800651 Name chatroomName(chatroom.toStdString());
Yingdi Yu72781e52013-11-06 23:00:21 -0800652
Yingdi Yufa4ce792014-02-06 18:09:22 -0800653 ChatDialog* chatDialog = new ChatDialog(m_contactManager, m_face, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
654 m_chatDialogs[chatroomName] = chatDialog;
Yingdi Yu46948282013-11-06 18:43:31 -0800655 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
656 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800657 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
658 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800659 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
660 this, SLOT(showWarning(const QString&)));
Yingdi Yu46948282013-11-06 18:43:31 -0800661
Yingdi Yueda39aa2013-10-23 23:07:29 -0700662 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700663}
664
Yingdi Yu7989eb22013-10-31 17:38:22 -0700665// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700666void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800667ContactPanel::startChatroom2(const Name& invitationInterest)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700668{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800669 Invitation invitation(invitationInterest);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700670 Name chatroomName("/ndn/broadcast/chronos");
671 chatroomName.append(invitation.getChatroom());
Yingdi Yu72781e52013-11-06 23:00:21 -0800672
Yingdi Yufa4ce792014-02-06 18:09:22 -0800673 ChatDialog* chatDialog = new ChatDialog(m_contactManager, m_face, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
Yingdi Yu46948282013-11-06 18:43:31 -0800674 connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
675 this, SLOT(removeChatDialog(const ndn::Name&)));
Yingdi Yu8e135832013-11-09 20:12:31 -0800676 connect(chatDialog, SIGNAL(noNdnConnection(const QString&)),
677 this, SLOT(showError(const QString&)));
Yingdi Yu0aca2652013-11-10 10:47:44 -0800678 connect(chatDialog, SIGNAL(inivationRejection(const QString&)),
679 this, SLOT(showWarning(const QString&)));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700680
Yingdi Yufa4ce792014-02-06 18:09:22 -0800681 Block signatureBlock = invitationInterest.get(-1).blockFromValue();
682 Block signatureInfo = invitationInterest.get(-2).blockFromValue();
683 Signature signature(signatureInfo, signatureBlock);
Yingdi Yu7989eb22013-10-31 17:38:22 -0700684
Yingdi Yufa4ce792014-02-06 18:09:22 -0800685 SignatureSha256WithRsa sig(signature);
686 Name keyLocatorName = sig.getKeyLocator().getName();
687 Name inviter = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName).getPrefix(-1);
688
689#ifdef WITH_SECURITY
690 shared_ptr<IdentityCertificate> idCert = m_invitationValidator->getValidatedDskCertificate(keyLocatorName);
691 chatDialog->addChatDataRule(invitation.getInviterRoutingPrefix(), *idCert, true);
692 chatDialog->publishIntroCert(*idCert, true);
693 shared_ptr<ContactItem> inviterItem = m_contactManager->getContact(inviter);
694 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
695#endif
696
697 m_chatDialogs[chatroomName] = chatDialog;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700698
699 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700700}
701
702void
Yingdi Yufa4ce792014-02-06 18:09:22 -0800703ContactPanel::prepareInvitationReply(const Name& invitationInterest, const string& content)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700704{
Yingdi Yufa4ce792014-02-06 18:09:22 -0800705 Invitation invitation(invitationInterest);
706
707 Name dataName = invitationInterest;
708 dataName.appendVersion();
709
Yingdi Yu6eabbd72013-12-27 08:44:12 +0800710 Data data(dataName);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800711 data.setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size());
712 data.setFreshnessPeriod(1000);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800713
Yingdi Yufa4ce792014-02-06 18:09:22 -0800714 m_keyChain->signByIdentity(data, invitation.getInviteeNameSpace());
Yingdi Yu76dd8002013-12-24 11:16:32 +0800715
Yingdi Yuf8f572d2014-01-13 11:19:47 -0800716 m_face->put(data);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700717}
718
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800719void
720ContactPanel::isIntroducerChanged(int state)
721{
722 if(state == Qt::Checked)
723 {
724 ui->addScope->setEnabled(true);
725 ui->deleteScope->setEnabled(true);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800726 ui->trustScopeList->setEnabled(true);
727
728 string filter("contact_namespace = '");
729 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
Yingdi Yub2e747d2013-11-05 23:06:43 -0800730
731 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
732 m_trustScopeModel->setTable("TrustScope");
733 m_trustScopeModel->setFilter(filter.c_str());
734 m_trustScopeModel->select();
735 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
736 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
737 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800738
739 ui->trustScopeList->setModel(m_trustScopeModel);
740 ui->trustScopeList->setColumnHidden(0, true);
741 ui->trustScopeList->setColumnHidden(1, true);
742 ui->trustScopeList->show();
743
744 m_currentSelectedContact->setIsIntroducer(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800745 }
746 else
747 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800748 ui->addScope->setEnabled(false);
749 ui->deleteScope->setEnabled(false);
Yingdi Yub2e747d2013-11-05 23:06:43 -0800750
751 string filter("contact_namespace = '");
752 filter.append(m_currentSelectedContact->getNameSpace().toUri()).append("'");
753
754 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
755 m_trustScopeModel->setTable("TrustScope");
756 m_trustScopeModel->setFilter(filter.c_str());
757 m_trustScopeModel->select();
758 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
759 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
760 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
761
762 ui->trustScopeList->setModel(m_trustScopeModel);
763 ui->trustScopeList->setColumnHidden(0, true);
764 ui->trustScopeList->setColumnHidden(1, true);
765 ui->trustScopeList->show();
766
767 ui->trustScopeList->setEnabled(false);
768
769 m_currentSelectedContact->setIsIntroducer(false);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800770 }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800771 m_contactManager->getContactStorage()->updateIsIntroducer(m_currentSelectedContact->getNameSpace(), m_currentSelectedContact->isIntroducer());
772}
773
774void
775ContactPanel::addScopeClicked()
776{
777 int rowCount = m_trustScopeModel->rowCount();
778 QSqlRecord record;
779 QSqlField identityField("contact_namespace", QVariant::String);
780 record.append(identityField);
Yingdi Yu76dd8002013-12-24 11:16:32 +0800781 record.setValue("contact_namespace", QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri()));
Yingdi Yub2e747d2013-11-05 23:06:43 -0800782 m_trustScopeModel->insertRow(rowCount);
783 m_trustScopeModel->setRecord(rowCount, record);
784}
785
786void
787ContactPanel::deleteScopeClicked()
788{
789 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
790 QModelIndexList indexList = selectionModel->selectedIndexes();
791
792 int i = indexList.size() - 1;
793 for(; i >= 0; i--)
794 m_trustScopeModel->removeRow(indexList[i].row());
795
796 m_trustScopeModel->submitAll();
797}
798
799void
800ContactPanel::saveScopeClicked()
Yingdi Yu83eae842013-11-06 22:07:38 -0800801{ m_trustScopeModel->submitAll(); }
Yingdi Yub2e747d2013-11-05 23:06:43 -0800802
803void
804ContactPanel::endorseButtonClicked()
805{
806 m_endorseDataModel->submitAll();
807 m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800808}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700809
Yingdi Yu46948282013-11-06 18:43:31 -0800810void
811ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
812{
813 map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
Yingdi Yu83eae842013-11-06 22:07:38 -0800814
815 ChatDialog* deletedChat = NULL;
Yingdi Yu46948282013-11-06 18:43:31 -0800816 if(it != m_chatDialogs.end())
817 {
Yingdi Yu46948282013-11-06 18:43:31 -0800818 deletedChat = it->second;
819 m_chatDialogs.erase(it);
820 }
Yingdi Yu83eae842013-11-06 22:07:38 -0800821 if (deletedChat != NULL)
822 delete deletedChat;
Yingdi Yu46948282013-11-06 18:43:31 -0800823}
824
Yingdi Yu847aa862013-10-09 16:35:53 -0700825#if WAF
826#include "contactpanel.moc"
827#include "contactpanel.cpp.moc"
828#endif