blob: 0407d8f88ffa4c809ada8a7b844f401868cce4e2 [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>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070022#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yu100267f2013-10-21 15:01:40 -070023#include <ndn.cxx/common.h>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070024#include <boost/filesystem.hpp>
Yingdi Yu53eb8a72013-10-23 11:50:51 -070025#include <boost/random/random_device.hpp>
26#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu42f66462013-10-31 17:38:22 -070027#include "panel-policy-manager.h"
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070028#include "logging.h"
29#include "exception.h"
30#endif
31
32namespace fs = boost::filesystem;
33using namespace ndn;
Yingdi Yu53eb8a72013-10-23 11:50:51 -070034
Yingdi Yu79c25a22013-10-21 13:38:38 -070035using namespace std;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070036
37INIT_LOGGER("ContactPanel");
38
Yingdi Yu53eb8a72013-10-23 11:50:51 -070039Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
Yingdi Yu42f66462013-10-31 17:38:22 -070040Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070041
Yingdi Yu0b82a4e2013-10-18 11:29:25 -070042ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070043 : QDialog(parent)
44 , ui(new Ui::ContactPanel)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070045 , m_contactListModel(new QStringListModel)
Yingdi Yu100267f2013-10-21 15:01:40 -070046 , m_startChatDialog(new StartChatDialog)
Yingdi Yu53eb8a72013-10-23 11:50:51 -070047 , m_invitationDialog(new InvitationDialog)
48 , m_settingDialog(new SettingDialog)
Yingdi Yu100267f2013-10-21 15:01:40 -070049 , m_menuInvite(new QAction("&Chat", this))
Yingdi Yu79c25a22013-10-21 13:38:38 -070050 , m_menuAlias(new QAction("&Set Alias", this))
Yingdi Yudef90612013-10-09 22:34:42 -070051{
Yingdi Yu53eb8a72013-10-23 11:50:51 -070052 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu42f66462013-10-31 17:38:22 -070053 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070054
Yingdi Yu42f66462013-10-31 17:38:22 -070055 openDB();
56
57 m_contactManager = contactManager;
58 m_profileEditor = new ProfileEditor(m_contactManager);
59 m_addContactPanel = new AddContactPanel(contactManager);
60 m_setAliasDialog = new SetAliasDialog(contactManager);
61
Yingdi Yuc4d08d22013-10-23 23:07:29 -070062 ui->setupUi(this);
63 refreshContactList();
Yingdi Yu01a942b2013-10-10 15:00:58 -070064
Yingdi Yuc4d08d22013-10-23 23:07:29 -070065 setKeychain();
66 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070067
Yingdi Yuc4d08d22013-10-23 23:07:29 -070068 setLocalPrefix();
Yingdi Yu53eb8a72013-10-23 11:50:51 -070069
Yingdi Yuc4d08d22013-10-23 23:07:29 -070070 // Set Identity, TODO: through user interface
71 m_defaultIdentity = m_keychain->getDefaultIdentity();
72 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
73 setInvitationListener();
74
75 ui->ContactList->setModel(m_contactListModel);
76
77 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
Yingdi Yuc26af3c2013-10-17 17:03:46 -070078
Yingdi Yuc4d08d22013-10-23 23:07:29 -070079 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
80 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu01a942b2013-10-10 15:00:58 -070081
Yingdi Yuc4d08d22013-10-23 23:07:29 -070082 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
83 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yud59b5932013-10-16 13:16:49 -070084
Yingdi Yuc4d08d22013-10-23 23:07:29 -070085 connect(ui->EditProfileButton, SIGNAL(clicked()),
86 this, SLOT(openProfileEditor()));
Yingdi Yud59b5932013-10-16 13:16:49 -070087
Yingdi Yuc4d08d22013-10-23 23:07:29 -070088 connect(ui->AddContactButton, SIGNAL(clicked()),
89 this, SLOT(openAddContactPanel()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070090
Yingdi Yuc4d08d22013-10-23 23:07:29 -070091 connect(ui->settingButton, SIGNAL(clicked()),
92 this, SLOT(openSettingDialog()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070093
Yingdi Yuc4d08d22013-10-23 23:07:29 -070094 connect(m_addContactPanel, SIGNAL(newContactAdded()),
95 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070096
Yingdi Yuc4d08d22013-10-23 23:07:29 -070097 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
98 this, SLOT(refreshContactList()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -070099
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700100 connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
101 this, SLOT(startChatroom(const QString&, const QString&, bool)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700102
Yingdi Yu42f66462013-10-31 17:38:22 -0700103 connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
104 this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
105 connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
106 this, SLOT(rejectInvitation(const ChronosInvitation&)));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700107
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700108 connect(m_settingDialog, SIGNAL(identitySet(const QString&)),
109 this, SLOT(updateDefaultIdentity(const QString&)));
110
111 connect(this, SIGNAL(newInvitationReady()),
112 this, SLOT(openInvitationDialog()));
Yingdi Yu100267f2013-10-21 15:01:40 -0700113
Yingdi Yu79c25a22013-10-21 13:38:38 -0700114
115
Yingdi Yudef90612013-10-09 22:34:42 -0700116}
117
118ContactPanel::~ContactPanel()
119{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700120 delete ui;
121 delete m_contactListModel;
122 delete m_profileEditor;
123 delete m_addContactPanel;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700124
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700125 delete m_menuInvite;
126
127 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
128 for(; it != m_chatDialogs.end(); it++)
129 delete it->second;
Yingdi Yudef90612013-10-09 22:34:42 -0700130}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700131
Yingdi Yu01a942b2013-10-10 15:00:58 -0700132void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700133ContactPanel::openDB()
134{
135 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
136 QString path = (QDir::home().path());
137 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
138 db.setDatabaseName(path);
139 bool ok = db.open();
Yingdi Yu42f66462013-10-31 17:38:22 -0700140 _LOG_DEBUG("db opened: " << std::boolalpha << ok );
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700141}
142
143void
144ContactPanel::setKeychain()
145{
Yingdi Yued8cfc42013-11-01 17:37:51 -0700146 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu42f66462013-10-31 17:38:22 -0700147 // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700148
149 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
150 for(; it != m_contactList.end(); it++)
Yingdi Yued8cfc42013-11-01 17:37:51 -0700151 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700152
Yingdi Yued8cfc42013-11-01 17:37:51 -0700153 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
154 m_panelPolicyManager,
155 NULL));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700156}
157
158void
159ContactPanel::setLocalPrefix()
160{
161 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
162 interest->setChildSelector(Interest::CHILD_RIGHT);
163
164 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
165 this,
166 _1),
167 boost::bind(&ContactPanel::onLocalPrefixTimeout,
168 this,
169 _1,
170 _2),
171 boost::bind(&ContactPanel::onLocalPrefixVerified,
172 this,
173 _1)));
174
175 m_handler->sendInterest(interest, closure);
176}
177
178void
179ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
180{
181 string originPrefix(data->content().buf(), data->content().size());
182 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
183 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700184 m_localPrefix = Name(prefix);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700185 m_localPrefix.append(randomSuffix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700186}
187
188void
189ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu42f66462013-10-31 17:38:22 -0700190{
191 string randomSuffix = getRandomString();
192 m_localPrefix = Name("/private/local");
193 m_localPrefix.append(randomSuffix);
194}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700195
196void
197ContactPanel::onUnverified(Ptr<Data> data)
198{}
199
200void
201ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
202{}
203
204void
205ContactPanel::onInvitationCertVerified(Ptr<Data> data,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700206 Ptr<ChronosInvitation> invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700207{
208 Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
Yingdi Yu42f66462013-10-31 17:38:22 -0700209
210 if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700211 {
212 Name keyName = certificate->getPublicKeyName();
213 Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
Yingdi Yued8cfc42013-11-01 17:37:51 -0700214 popChatInvitation(invitation, inviterNameSpace, certificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700215 }
216}
217
218void
Yingdi Yu42f66462013-10-31 17:38:22 -0700219ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700220 const Name& inviterNameSpace,
221 Ptr<security::IdentityCertificate> certificate)
222{
Yingdi Yu42f66462013-10-31 17:38:22 -0700223 string chatroom = invitation->getChatroom().get(0).toUri();
224 string inviter = inviterNameSpace.toUri();
225
226 string alias;
227 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
228 for(; it != m_contactList.end(); it++)
229 if((*it)->getNameSpace() == inviterNameSpace)
230 alias = (*it)->getAlias();
231
232 if(it != m_contactList.end())
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700233 return;
234
Yingdi Yu42f66462013-10-31 17:38:22 -0700235 m_invitationDialog->setInvitation(alias, invitation, certificate);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700236 emit newInvitationReady();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700237}
238
239static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
240
241string
242ContactPanel::getRandomString()
243{
244 string randStr;
245 boost::random::random_device rng;
246 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
247 for (int i = 0; i < 10; i ++)
248 {
249 randStr += chars[index_dist(rng)];
250 }
251 return randStr;
252}
253
254
255void
256ContactPanel::setInvitationListener()
257{
258 Name prefix("/ndn/broadcast/chronos/invitation");
259 prefix.append(m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700260 _LOG_DEBUG("prefix: " << prefix.toUri());
261 m_inviteListenPrefix = prefix;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700262 m_handler->setInterestFilter (prefix,
263 boost::bind(&ContactPanel::onInvitation,
264 this,
265 _1));
266
267}
268
269void
270ContactPanel::onInvitation(Ptr<Interest> interest)
271{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700272 _LOG_DEBUG("receive interest!" << interest->getName().toUri());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700273 const Name& interestName = interest->getName();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700274
Yingdi Yued8cfc42013-11-01 17:37:51 -0700275 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700276
Yingdi Yued8cfc42013-11-01 17:37:51 -0700277 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700278 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
279 this,
280 _1,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700281 invitation),
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700282 boost::bind(&ContactPanel::onTimeout,
283 this,
284 _1,
285 _2),
286 boost::bind(&ContactPanel::onUnverified,
287 this,
288 _1)));
289 m_handler->sendInterest(newInterest, closure);
290}
291
292void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700293ContactPanel::updateSelection(const QItemSelection &selected,
294 const QItemSelection &deselected)
295{
296 QModelIndexList items = selected.indexes();
297 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700298 string alias = text.toUtf8().constData();
299
Yingdi Yu79c25a22013-10-21 13:38:38 -0700300 int i = 0;
301 for(; i < m_contactList.size(); i++)
302 {
303 if(alias == m_contactList[i]->getAlias())
304 break;
305 }
306
307 QString name = QString::fromUtf8(m_contactList[i]->getName().c_str());
308 QString institution = QString::fromUtf8(m_contactList[i]->getInstitution().c_str());
309 QString nameSpace = QString::fromUtf8(m_contactList[i]->getNameSpace().toUri().c_str());
310 ui->NameData->setText(name);
311 ui->NameSpaceData->setText(nameSpace);
312 ui->InstitutionData->setText(institution);
Yingdi Yu100267f2013-10-21 15:01:40 -0700313
314 m_currentSelectedContactAlias = alias;
315 m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700316 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yu01a942b2013-10-10 15:00:58 -0700317}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700318
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700319void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700320ContactPanel::updateDefaultIdentity(const QString& identity)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700321{
322 m_defaultIdentity = Name(identity.toUtf8().constData());
323 Name prefix("/ndn/broadcast/chronos/invitation");
324 prefix.append(m_defaultIdentity);
325
326 _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
327
328 m_handler->clearInterestFilter(m_inviteListenPrefix);
329 m_handler->setInterestFilter(prefix,
330 boost::bind(&ContactPanel::onInvitation,
331 this,
332 _1));
333 m_inviteListenPrefix = prefix;
334}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700335
336void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700337ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700338{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700339
Yingdi Yud59b5932013-10-16 13:16:49 -0700340void
341ContactPanel::openAddContactPanel()
342{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700343
Yingdi Yu79c25a22013-10-21 13:38:38 -0700344void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700345ContactPanel::openInvitationDialog()
346{ m_invitationDialog->show(); }
347
348void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700349ContactPanel::refreshContactList()
350{
351 m_contactList = m_contactManager->getContactItemList();
352 QStringList contactNameList;
353 for(int i = 0; i < m_contactList.size(); i++)
354 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
355
356 m_contactListModel->setStringList(contactNameList);
357}
358
359void
360ContactPanel::showContextMenu(const QPoint& pos)
361{
362 QMenu menu(ui->ContactList);
363 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700364 connect(m_menuInvite, SIGNAL(triggered()),
365 this, SLOT(openStartChatDialog()));
Yingdi Yu79c25a22013-10-21 13:38:38 -0700366 menu.addAction(m_menuAlias);
367 connect(m_menuAlias, SIGNAL(triggered()),
368 this, SLOT(openSetAliasDialog()));
369 menu.exec(ui->ContactList->mapToGlobal(pos));
370
371}
372
373void
374ContactPanel::openSetAliasDialog()
375{
Yingdi Yu100267f2013-10-21 15:01:40 -0700376 m_setAliasDialog->setTargetIdentity(m_currentSelectedContactNamespace);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700377 m_setAliasDialog->show();
378}
379
Yingdi Yu100267f2013-10-21 15:01:40 -0700380void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700381ContactPanel::openSettingDialog()
382{
383 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
384 m_settingDialog->show();
385}
386
387void
Yingdi Yu100267f2013-10-21 15:01:40 -0700388ContactPanel::openStartChatDialog()
389{
Yingdi Yu42f66462013-10-31 17:38:22 -0700390 // TimeInterval ti = time::NowUnixTimestamp();
391 // ostringstream oss;
392 // oss << ti.total_seconds();
Yingdi Yu100267f2013-10-21 15:01:40 -0700393
394 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700395 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700396
397 m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri());
398 m_startChatDialog->show();
399}
400
Yingdi Yu42f66462013-10-31 17:38:22 -0700401// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700402void
403ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
404{
405 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
406 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
407 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700408
Yingdi Yu42f66462013-10-31 17:38:22 -0700409 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700410
Yingdi Yu42f66462013-10-31 17:38:22 -0700411 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700412 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
413
414 //TODO: send invitation
415 Name inviteeNamespace(invitee.toUtf8().constData());
416 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
417
Yingdi Yu42f66462013-10-31 17:38:22 -0700418 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700419
420 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700421}
422
Yingdi Yu42f66462013-10-31 17:38:22 -0700423// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700424void
Yingdi Yu42f66462013-10-31 17:38:22 -0700425ContactPanel::startChatroom2(const ChronosInvitation& invitation,
426 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700427{
Yingdi Yu42f66462013-10-31 17:38:22 -0700428 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
429 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
430
431 Name chatroomName("/ndn/broadcast/chronos");
432 chatroomName.append(invitation.getChatroom());
433
434 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
435 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
436
437 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
438 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
439
440 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
441
442 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700443}
444
445void
Yingdi Yu42f66462013-10-31 17:38:22 -0700446ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
447 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700448{
449 string prefix = m_localPrefix.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700450
451 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700452 //TODO:: open chat dialog
Yingdi Yu42f66462013-10-31 17:38:22 -0700453 _LOG_DEBUG("TO open chat dialog");
454 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700455}
456
457void
Yingdi Yu42f66462013-10-31 17:38:22 -0700458ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700459{
460 string empty;
Yingdi Yu42f66462013-10-31 17:38:22 -0700461 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700462}
463
Yingdi Yu100267f2013-10-21 15:01:40 -0700464
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700465#if WAF
466#include "contactpanel.moc"
467#include "contactpanel.cpp.moc"
468#endif