blob: 0ce0694eaffe070850c6264f797ce378bed98571 [file] [log] [blame]
Yingdi Yu9e0dc292013-10-10 11:39:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
Yingdi Yudef90612013-10-09 22:34:42 -070011#include "contactpanel.h"
12#include "ui_contactpanel.h"
13
Yingdi Yu01a942b2013-10-10 15:00:58 -070014
15#include <QStringList>
16#include <QItemSelectionModel>
17#include <QModelIndex>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070018#include <QDir>
Yingdi Yu01a942b2013-10-10 15:00:58 -070019
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070020#ifndef Q_MOC_RUN
Yingdi Yu53eb8a72013-10-23 11:50:51 -070021#include <ndn.cxx/security/keychain.h>
22#include <ndn.cxx/security/identity/osx-privatekey-storage.h>
23#include <ndn.cxx/security/identity/identity-manager.h>
24#include <ndn.cxx/security/identity/basic-identity-storage.h>
25#include <ndn.cxx/security/cache/ttl-certificate-cache.h>
26#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
Yingdi Yu100267f2013-10-21 15:01:40 -070027#include <ndn.cxx/common.h>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070028#include <boost/filesystem.hpp>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070029#include <boost/random/random_device.hpp>
30#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu42f66462013-10-31 17:38:22 -070031#include "panel-policy-manager.h"
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070032#include "logging.h"
33#include "exception.h"
34#endif
35
36namespace fs = boost::filesystem;
37using namespace ndn;
Yingdi Yu53eb8a72013-10-23 11:50:51 -070038
Yingdi Yu79c25a22013-10-21 13:38:38 -070039using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070040
41INIT_LOGGER("ContactPanel");
42
Yingdi Yu53eb8a72013-10-23 11:50:51 -070043Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
Yingdi Yu42f66462013-10-31 17:38:22 -070044Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070045
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070046ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070047 : QDialog(parent)
48 , ui(new Ui::ContactPanel)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070049 , m_contactListModel(new QStringListModel)
Yingdi Yu100267f2013-10-21 15:01:40 -070050 , m_startChatDialog(new StartChatDialog)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070051 , m_invitationDialog(new InvitationDialog)
52 , m_settingDialog(new SettingDialog)
Yingdi Yu100267f2013-10-21 15:01:40 -070053 , m_menuInvite(new QAction("&Chat", this))
Yingdi Yu79c25a22013-10-21 13:38:38 -070054 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yudef90612013-10-09 22:34:42 -070055{
Yingdi Yu53eb8a72013-10-23 11:50:51 -070056 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu42f66462013-10-31 17:38:22 -070057 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070058
Yingdi Yu42f66462013-10-31 17:38:22 -070059 openDB();
60
61 m_contactManager = contactManager;
62 m_profileEditor = new ProfileEditor(m_contactManager);
63 m_addContactPanel = new AddContactPanel(contactManager);
64 m_setAliasDialog = new SetAliasDialog(contactManager);
65
Yingdi Yuc4d08d22013-10-23 23:07:29 -070066 ui->setupUi(this);
67 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070068
Yingdi Yuc4d08d22013-10-23 23:07:29 -070069 setKeychain();
70 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070071
Yingdi Yuc4d08d22013-10-23 23:07:29 -070072 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -070073
Yingdi Yuc4d08d22013-10-23 23:07:29 -070074 // Set Identity, TODO: through user interface
75 m_defaultIdentity = m_keychain->getDefaultIdentity();
76 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
77 setInvitationListener();
78
79 ui->ContactList->setModel(m_contactListModel);
80
81 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yuc26af3c2013-10-17 17:03:46 -070082
Yingdi Yuc4d08d22013-10-23 23:07:29 -070083 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
84 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -070085
Yingdi Yuc4d08d22013-10-23 23:07:29 -070086 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
87 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -070088
Yingdi Yuc4d08d22013-10-23 23:07:29 -070089 connect(ui->EditProfileButton, SIGNAL(clicked()),
90 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -070091
Yingdi Yuc4d08d22013-10-23 23:07:29 -070092 connect(ui->AddContactButton, SIGNAL(clicked()),
93 this, SLOT(openAddContactPanel()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070094
Yingdi Yuc4d08d22013-10-23 23:07:29 -070095 connect(ui->settingButton, SIGNAL(clicked()),
96 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070097
Yingdi Yuc4d08d22013-10-23 23:07:29 -070098 connect(m_addContactPanel, SIGNAL(newContactAdded()),
99 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700100
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700101 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
102 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700103
Yingdi Yuc4d08d22013-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 Yu53eb8a72013-10-23 11:50:51 -0700106
Yingdi Yu42f66462013-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 Yu53eb8a72013-10-23 11:50:51 -0700111
Yingdi Yuc4d08d22013-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 Yu100267f2013-10-21 15:01:40 -0700117
Yingdi Yu79c25a22013-10-21 13:38:38 -0700118
119
Yingdi Yudef90612013-10-09 22:34:42 -0700120}
121
122ContactPanel::~ContactPanel()
123{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700124 delete ui;
125 delete m_contactListModel;
126 delete m_profileEditor;
127 delete m_addContactPanel;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700128
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700129 delete m_menuInvite;
130
131 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
132 for(; it != m_chatDialogs.end(); it++)
133 delete it->second;
Yingdi Yudef90612013-10-09 22:34:42 -0700134}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700135
Yingdi Yu01a942b2013-10-10 15:00:58 -0700136void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700137ContactPanel::openDB()
138{
139 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
140 QString path = (QDir::home().path());
141 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
142 db.setDatabaseName(path);
143 bool ok = db.open();
Yingdi Yu42f66462013-10-31 17:38:22 -0700144 _LOG_DEBUG("db opened: " << std::boolalpha << ok );
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700145}
146
147void
148ContactPanel::setKeychain()
149{
Yingdi Yu42f66462013-10-31 17:38:22 -0700150 Ptr<security::IdentityManager> identityManager = Ptr<security::IdentityManager>::Create();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700151 Ptr<security::CertificateCache> certificateCache = Ptr<security::CertificateCache>(new security::TTLCertificateCache());
Yingdi Yu42f66462013-10-31 17:38:22 -0700152 Ptr<PanelPolicyManager> policyManager = Ptr<PanelPolicyManager>(new PanelPolicyManager(10, certificateCache));
153 // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700154
155 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
156 for(; it != m_contactList.end(); it++)
157 policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
158
Yingdi Yu42f66462013-10-31 17:38:22 -0700159 m_keychain = Ptr<security::Keychain>(new security::Keychain(identityManager, policyManager, NULL));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700160}
161
162void
163ContactPanel::setLocalPrefix()
164{
165 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
166 interest->setChildSelector(Interest::CHILD_RIGHT);
167
168 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
169 this,
170 _1),
171 boost::bind(&ContactPanel::onLocalPrefixTimeout,
172 this,
173 _1,
174 _2),
175 boost::bind(&ContactPanel::onLocalPrefixVerified,
176 this,
177 _1)));
178
179 m_handler->sendInterest(interest, closure);
180}
181
182void
183ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
184{
185 string originPrefix(data->content().buf(), data->content().size());
186 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
187 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700188 m_localPrefix = Name(prefix);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700189 m_localPrefix.append(randomSuffix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700190}
191
192void
193ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu42f66462013-10-31 17:38:22 -0700194{
195 string randomSuffix = getRandomString();
196 m_localPrefix = Name("/private/local");
197 m_localPrefix.append(randomSuffix);
198}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700199
200void
201ContactPanel::onUnverified(Ptr<Data> data)
202{}
203
204void
205ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
206{}
207
208void
209ContactPanel::onInvitationCertVerified(Ptr<Data> data,
210 const Name& interestName,
211 int inviterIndex)
212{
213 Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
Yingdi Yu42f66462013-10-31 17:38:22 -0700214 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
215
216 if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700217 {
218 Name keyName = certificate->getPublicKeyName();
219 Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
Yingdi Yu42f66462013-10-31 17:38:22 -0700220 popChatInvitation(invitation, inviterIndex, inviterNameSpace, certificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700221 }
222}
223
224void
Yingdi Yu42f66462013-10-31 17:38:22 -0700225ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700226 int inviterIndex,
227 const Name& inviterNameSpace,
228 Ptr<security::IdentityCertificate> certificate)
229{
Yingdi Yu42f66462013-10-31 17:38:22 -0700230 string chatroom = invitation->getChatroom().get(0).toUri();
231 string inviter = inviterNameSpace.toUri();
232
233 string alias;
234 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
235 for(; it != m_contactList.end(); it++)
236 if((*it)->getNameSpace() == inviterNameSpace)
237 alias = (*it)->getAlias();
238
239 if(it != m_contactList.end())
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700240 return;
241
Yingdi Yu42f66462013-10-31 17:38:22 -0700242 m_invitationDialog->setInvitation(alias, invitation, certificate);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700243 emit newInvitationReady();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700244}
245
246static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
247
248string
249ContactPanel::getRandomString()
250{
251 string randStr;
252 boost::random::random_device rng;
253 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
254 for (int i = 0; i < 10; i ++)
255 {
256 randStr += chars[index_dist(rng)];
257 }
258 return randStr;
259}
260
261
262void
263ContactPanel::setInvitationListener()
264{
265 Name prefix("/ndn/broadcast/chronos/invitation");
266 prefix.append(m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700267 _LOG_DEBUG("prefix: " << prefix.toUri());
268 m_inviteListenPrefix = prefix;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700269 m_handler->setInterestFilter (prefix,
270 boost::bind(&ContactPanel::onInvitation,
271 this,
272 _1));
273
274}
275
276void
277ContactPanel::onInvitation(Ptr<Interest> interest)
278{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700279 _LOG_DEBUG("receive interest!" << interest->getName().toUri());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700280 const Name& interestName = interest->getName();
281 const int end = interestName.size();
282
283 string inviter("inviter");
284 int j = end-2;
285 for(; j >= 0; j--)
286 if(interestName.get(j).toUri() == inviter)
287 break;
288
289 //No certificate name found
290 if(j < 0)
291 return;
292
293 Name certName = interestName.getSubName(j+1, end-2-j);
294 string keyString("KEY");
295 string idString("ID-CERT");
296 int m = certName.size() - 1;
297 int keyIndex = -1;
298 int idIndex = -1;
299 for(; m >= 0; m--)
300 if(certName.get(m).toUri() == idString)
301 {
302 idIndex = m;
303 break;
304 }
305
306 for(; m >=0; m--)
307 if(certName.get(m).toUri() == keyString)
308 {
309 keyIndex = m;
310 break;
311 }
312
313 //Not a qualified certificate name
314 if(keyIndex < 0 && idIndex < 0)
315 return;
316
317 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(certName));
318 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
319 this,
320 _1,
321 interestName,
322 j),
323 boost::bind(&ContactPanel::onTimeout,
324 this,
325 _1,
326 _2),
327 boost::bind(&ContactPanel::onUnverified,
328 this,
329 _1)));
330 m_handler->sendInterest(newInterest, closure);
331}
332
333void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700334ContactPanel::updateSelection(const QItemSelection &selected,
335 const QItemSelection &deselected)
336{
337 QModelIndexList items = selected.indexes();
338 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700339 string alias = text.toUtf8().constData();
340
Yingdi Yu79c25a22013-10-21 13:38:38 -0700341 int i = 0;
342 for(; i < m_contactList.size(); i++)
343 {
344 if(alias == m_contactList[i]->getAlias())
345 break;
346 }
347
348 QString name = QString::fromUtf8(m_contactList[i]->getName().c_str());
349 QString institution = QString::fromUtf8(m_contactList[i]->getInstitution().c_str());
350 QString nameSpace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str());
351 ui->NameData->setText(name);
352 ui->NameSpaceData->setText(nameSpace);
353 ui->InstitutionData->setText(institution);
Yingdi Yu100267f2013-10-21 15:01:40 -0700354
355 m_currentSelectedContactAlias = alias;
356 m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700357 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yu01a942b2013-10-10 15:00:58 -0700358}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700359
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700360void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700361ContactPanel::updateDefaultIdentity(const QString& identity)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700362{
363 m_defaultIdentity = Name(identity.toUtf8().constData());
364 Name prefix("/ndn/broadcast/chronos/invitation");
365 prefix.append(m_defaultIdentity);
366
367 _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
368
369 m_handler->clearInterestFilter(m_inviteListenPrefix);
370 m_handler->setInterestFilter(prefix,
371 boost::bind(&ContactPanel::onInvitation,
372 this,
373 _1));
374 m_inviteListenPrefix = prefix;
375}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700376
377void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700378ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700379{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700380
Yingdi Yud59b5932013-10-16 13:16:49 -0700381void
382ContactPanel::openAddContactPanel()
383{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700384
Yingdi Yu79c25a22013-10-21 13:38:38 -0700385void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700386ContactPanel::openInvitationDialog()
387{ m_invitationDialog->show(); }
388
389void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700390ContactPanel::refreshContactList()
391{
392 m_contactList = m_contactManager->getContactItemList();
393 QStringList contactNameList;
394 for(int i = 0; i < m_contactList.size(); i++)
395 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
396
397 m_contactListModel->setStringList(contactNameList);
398}
399
400void
401ContactPanel::showContextMenu(const QPoint& pos)
402{
403 QMenu menu(ui->ContactList);
404 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700405 connect(m_menuInvite, SIGNAL(triggered()),
406 this, SLOT(openStartChatDialog()));
Yingdi Yu79c25a22013-10-21 13:38:38 -0700407 menu.addAction(m_menuAlias);
408 connect(m_menuAlias, SIGNAL(triggered()),
409 this, SLOT(openSetAliasDialog()));
410 menu.exec(ui->ContactList->mapToGlobal(pos));
411
412}
413
414void
415ContactPanel::openSetAliasDialog()
416{
Yingdi Yu100267f2013-10-21 15:01:40 -0700417 m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700418 m_setAliasDialog->show();
419}
420
Yingdi Yu100267f2013-10-21 15:01:40 -0700421void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700422ContactPanel::openSettingDialog()
423{
424 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
425 m_settingDialog->show();
426}
427
428void
Yingdi Yu100267f2013-10-21 15:01:40 -0700429ContactPanel::openStartChatDialog()
430{
Yingdi Yu42f66462013-10-31 17:38:22 -0700431 // TimeInterval ti = time::NowUnixTimestamp();
432 // ostringstream oss;
433 // oss << ti.total_seconds();
Yingdi Yu100267f2013-10-21 15:01:40 -0700434
435 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700436 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700437
438 m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri());
439 m_startChatDialog->show();
440}
441
Yingdi Yu42f66462013-10-31 17:38:22 -0700442// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700443void
444ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
445{
446 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
447 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
448 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700449
Yingdi Yu42f66462013-10-31 17:38:22 -0700450 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700451
Yingdi Yu42f66462013-10-31 17:38:22 -0700452 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700453 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
454
455 //TODO: send invitation
456 Name inviteeNamespace(invitee.toUtf8().constData());
457 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
458
Yingdi Yu42f66462013-10-31 17:38:22 -0700459 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700460
461 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700462}
463
Yingdi Yu42f66462013-10-31 17:38:22 -0700464// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700465void
Yingdi Yu42f66462013-10-31 17:38:22 -0700466ContactPanel::startChatroom2(const ChronosInvitation& invitation,
467 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700468{
Yingdi Yu42f66462013-10-31 17:38:22 -0700469 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
470 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
471
472 Name chatroomName("/ndn/broadcast/chronos");
473 chatroomName.append(invitation.getChatroom());
474
475 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
476 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
477
478 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
479 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
480
481 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
482
483 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700484}
485
486void
Yingdi Yu42f66462013-10-31 17:38:22 -0700487ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
488 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700489{
490 string prefix = m_localPrefix.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700491
492 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700493 //TODO:: open chat dialog
Yingdi Yu42f66462013-10-31 17:38:22 -0700494 _LOG_DEBUG("TO open chat dialog");
495 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700496}
497
498void
Yingdi Yu42f66462013-10-31 17:38:22 -0700499ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700500{
501 string empty;
Yingdi Yu42f66462013-10-31 17:38:22 -0700502 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700503}
504
Yingdi Yu100267f2013-10-21 15:01:40 -0700505
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700506#if WAF
507#include "contactpanel.moc"
508#include "contactpanel.cpp.moc"
509#endif