blob: 7940145617093921e9ec0553bc3dc2c25a1ef7e0 [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 Yua50c3252013-11-03 15:05:26 -080043 : QDialog(parent)
44 , ui(new Ui::ContactPanel)
45 , m_contactListModel(new QStringListModel)
46 , m_startChatDialog(new StartChatDialog)
47 , m_invitationDialog(new InvitationDialog)
48 , m_settingDialog(new SettingDialog)
49 , m_menuInvite(new QAction("&Chat", this))
50 , 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 Yua50c3252013-11-03 15:05:26 -0800114 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
115 this, SLOT(isIntroducerChanged(int)));
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 Yua50c3252013-11-03 15:05:26 -0800124 if(NULL != m_currentContactTrustScopeListModel)
125 delete m_currentContactTrustScopeListModel;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700126
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700127 delete m_menuInvite;
128
129 map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
130 for(; it != m_chatDialogs.end(); it++)
131 delete it->second;
Yingdi Yudef90612013-10-09 22:34:42 -0700132}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700133
Yingdi Yu01a942b2013-10-10 15:00:58 -0700134void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700135ContactPanel::openDB()
136{
137 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
138 QString path = (QDir::home().path());
139 path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
140 db.setDatabaseName(path);
141 bool ok = db.open();
Yingdi Yu42f66462013-10-31 17:38:22 -0700142 _LOG_DEBUG("db opened: " << std::boolalpha << ok );
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700143}
144
145void
146ContactPanel::setKeychain()
147{
Yingdi Yued8cfc42013-11-01 17:37:51 -0700148 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu42f66462013-10-31 17:38:22 -0700149 // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700150
151 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
152 for(; it != m_contactList.end(); it++)
Yingdi Yued8cfc42013-11-01 17:37:51 -0700153 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700154
Yingdi Yued8cfc42013-11-01 17:37:51 -0700155 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
156 m_panelPolicyManager,
157 NULL));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700158}
159
160void
161ContactPanel::setLocalPrefix()
162{
163 Ptr<Interest> interest = Ptr<Interest>(new Interest(Name("/local/ndn/prefix")));
164 interest->setChildSelector(Interest::CHILD_RIGHT);
165
166 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onLocalPrefixVerified,
167 this,
168 _1),
169 boost::bind(&ContactPanel::onLocalPrefixTimeout,
170 this,
171 _1,
172 _2),
173 boost::bind(&ContactPanel::onLocalPrefixVerified,
174 this,
175 _1)));
176
177 m_handler->sendInterest(interest, closure);
178}
179
180void
181ContactPanel::onLocalPrefixVerified(Ptr<Data> data)
182{
183 string originPrefix(data->content().buf(), data->content().size());
184 string prefix = QString::fromStdString (originPrefix).trimmed ().toUtf8().constData();
185 string randomSuffix = getRandomString();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700186 m_localPrefix = Name(prefix);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700187 m_localPrefix.append(randomSuffix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700188}
189
190void
191ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu42f66462013-10-31 17:38:22 -0700192{
193 string randomSuffix = getRandomString();
194 m_localPrefix = Name("/private/local");
195 m_localPrefix.append(randomSuffix);
196}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700197
198void
199ContactPanel::onUnverified(Ptr<Data> data)
200{}
201
202void
203ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
204{}
205
206void
207ContactPanel::onInvitationCertVerified(Ptr<Data> data,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700208 Ptr<ChronosInvitation> invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700209{
210 Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
Yingdi Yu42f66462013-10-31 17:38:22 -0700211
212 if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700213 {
214 Name keyName = certificate->getPublicKeyName();
215 Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
Yingdi Yued8cfc42013-11-01 17:37:51 -0700216 popChatInvitation(invitation, inviterNameSpace, certificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700217 }
218}
219
220void
Yingdi Yu42f66462013-10-31 17:38:22 -0700221ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700222 const Name& inviterNameSpace,
223 Ptr<security::IdentityCertificate> certificate)
224{
Yingdi Yu42f66462013-10-31 17:38:22 -0700225 string chatroom = invitation->getChatroom().get(0).toUri();
226 string inviter = inviterNameSpace.toUri();
227
228 string alias;
229 vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
230 for(; it != m_contactList.end(); it++)
231 if((*it)->getNameSpace() == inviterNameSpace)
232 alias = (*it)->getAlias();
233
234 if(it != m_contactList.end())
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700235 return;
236
Yingdi Yu42f66462013-10-31 17:38:22 -0700237 m_invitationDialog->setInvitation(alias, invitation, certificate);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700238 emit newInvitationReady();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700239}
240
241static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
242
243string
244ContactPanel::getRandomString()
245{
246 string randStr;
247 boost::random::random_device rng;
248 boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
249 for (int i = 0; i < 10; i ++)
250 {
251 randStr += chars[index_dist(rng)];
252 }
253 return randStr;
254}
255
256
257void
258ContactPanel::setInvitationListener()
259{
260 Name prefix("/ndn/broadcast/chronos/invitation");
261 prefix.append(m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700262 _LOG_DEBUG("prefix: " << prefix.toUri());
263 m_inviteListenPrefix = prefix;
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700264 m_handler->setInterestFilter (prefix,
265 boost::bind(&ContactPanel::onInvitation,
266 this,
267 _1));
268
269}
270
271void
272ContactPanel::onInvitation(Ptr<Interest> interest)
273{
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700274 _LOG_DEBUG("receive interest!" << interest->getName().toUri());
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700275 const Name& interestName = interest->getName();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700276
Yingdi Yued8cfc42013-11-01 17:37:51 -0700277 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700278
Yingdi Yued8cfc42013-11-01 17:37:51 -0700279 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700280 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
281 this,
282 _1,
Yingdi Yued8cfc42013-11-01 17:37:51 -0700283 invitation),
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700284 boost::bind(&ContactPanel::onTimeout,
285 this,
286 _1,
287 _2),
288 boost::bind(&ContactPanel::onUnverified,
289 this,
290 _1)));
291 m_handler->sendInterest(newInterest, closure);
292}
293
294void
Yingdi Yu01a942b2013-10-10 15:00:58 -0700295ContactPanel::updateSelection(const QItemSelection &selected,
296 const QItemSelection &deselected)
297{
298 QModelIndexList items = selected.indexes();
299 QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700300 string alias = text.toUtf8().constData();
301
Yingdi Yu79c25a22013-10-21 13:38:38 -0700302 int i = 0;
303 for(; i < m_contactList.size(); i++)
304 {
305 if(alias == m_contactList[i]->getAlias())
306 break;
307 }
308
Yingdi Yua50c3252013-11-03 15:05:26 -0800309 m_currentSelectedContact = m_contactList[i];
310 QString name = QString::fromUtf8(m_currentSelectedContact->getName().c_str());
311 QString institution = QString::fromUtf8(m_currentSelectedContact->getInstitution().c_str());
312 QString nameSpace = QString::fromUtf8(m_currentSelectedContact->getNameSpace().toUri().c_str());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700313 ui->NameData->setText(name);
314 ui->NameSpaceData->setText(nameSpace);
315 ui->InstitutionData->setText(institution);
Yingdi Yu100267f2013-10-21 15:01:40 -0700316
Yingdi Yua50c3252013-11-03 15:05:26 -0800317 // m_currentSelectedContactAlias = alias;
318 // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu79c25a22013-10-21 13:38:38 -0700319 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yua50c3252013-11-03 15:05:26 -0800320
321 if(m_currentSelectedContact->isIntroducer())
322 {
323 ui->isIntroducer->setChecked(true);
324 ui->addScope->setEnabled(true);
325 ui->deleteScope->setEnabled(true);
326 Ptr<TrustedContact> trustedContact = boost::dynamic_pointer_cast<TrustedContact>(m_currentSelectedContact);
327 m_currentContactTrustScopeListModel = new QStringListModel;
328 }
329 else
330 {
331 ui->isIntroducer->setChecked(false);
332 ui->addScope->setEnabled(false);
333 ui->deleteScope->setEnabled(false);
334 delete m_currentContactTrustScopeListModel;
335 }
Yingdi Yu01a942b2013-10-10 15:00:58 -0700336}
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700337
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700338void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700339ContactPanel::updateDefaultIdentity(const QString& identity)
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700340{
341 m_defaultIdentity = Name(identity.toUtf8().constData());
342 Name prefix("/ndn/broadcast/chronos/invitation");
343 prefix.append(m_defaultIdentity);
344
345 _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
346
347 m_handler->clearInterestFilter(m_inviteListenPrefix);
348 m_handler->setInterestFilter(prefix,
349 boost::bind(&ContactPanel::onInvitation,
350 this,
351 _1));
352 m_inviteListenPrefix = prefix;
353}
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700354
355void
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700356ContactPanel::openProfileEditor()
Yingdi Yud59b5932013-10-16 13:16:49 -0700357{ m_profileEditor->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700358
Yingdi Yud59b5932013-10-16 13:16:49 -0700359void
360ContactPanel::openAddContactPanel()
361{ m_addContactPanel->show(); }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700362
Yingdi Yu79c25a22013-10-21 13:38:38 -0700363void
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700364ContactPanel::openInvitationDialog()
365{ m_invitationDialog->show(); }
366
367void
Yingdi Yu79c25a22013-10-21 13:38:38 -0700368ContactPanel::refreshContactList()
369{
370 m_contactList = m_contactManager->getContactItemList();
371 QStringList contactNameList;
372 for(int i = 0; i < m_contactList.size(); i++)
373 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
374
375 m_contactListModel->setStringList(contactNameList);
376}
377
378void
379ContactPanel::showContextMenu(const QPoint& pos)
380{
381 QMenu menu(ui->ContactList);
382 menu.addAction(m_menuInvite);
Yingdi Yu100267f2013-10-21 15:01:40 -0700383 connect(m_menuInvite, SIGNAL(triggered()),
384 this, SLOT(openStartChatDialog()));
Yingdi Yu79c25a22013-10-21 13:38:38 -0700385 menu.addAction(m_menuAlias);
386 connect(m_menuAlias, SIGNAL(triggered()),
387 this, SLOT(openSetAliasDialog()));
388 menu.exec(ui->ContactList->mapToGlobal(pos));
389
390}
391
392void
393ContactPanel::openSetAliasDialog()
394{
Yingdi Yua50c3252013-11-03 15:05:26 -0800395 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu79c25a22013-10-21 13:38:38 -0700396 m_setAliasDialog->show();
397}
398
Yingdi Yu100267f2013-10-21 15:01:40 -0700399void
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700400ContactPanel::openSettingDialog()
401{
402 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
403 m_settingDialog->show();
404}
405
406void
Yingdi Yu100267f2013-10-21 15:01:40 -0700407ContactPanel::openStartChatDialog()
408{
Yingdi Yu42f66462013-10-31 17:38:22 -0700409 // TimeInterval ti = time::NowUnixTimestamp();
410 // ostringstream oss;
411 // oss << ti.total_seconds();
Yingdi Yu100267f2013-10-21 15:01:40 -0700412
413 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu42f66462013-10-31 17:38:22 -0700414 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu100267f2013-10-21 15:01:40 -0700415
Yingdi Yua50c3252013-11-03 15:05:26 -0800416 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu100267f2013-10-21 15:01:40 -0700417 m_startChatDialog->show();
418}
419
Yingdi Yu42f66462013-10-31 17:38:22 -0700420// For inviter
Yingdi Yu100267f2013-10-21 15:01:40 -0700421void
422ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
423{
424 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
425 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
426 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700427
Yingdi Yu42f66462013-10-31 17:38:22 -0700428 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700429
Yingdi Yu42f66462013-10-31 17:38:22 -0700430 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700431 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
432
433 //TODO: send invitation
434 Name inviteeNamespace(invitee.toUtf8().constData());
435 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
436
Yingdi Yu42f66462013-10-31 17:38:22 -0700437 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yuc4d08d22013-10-23 23:07:29 -0700438
439 chatDialog->show();
Yingdi Yu100267f2013-10-21 15:01:40 -0700440}
441
Yingdi Yu42f66462013-10-31 17:38:22 -0700442// For Invitee
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700443void
Yingdi Yu42f66462013-10-31 17:38:22 -0700444ContactPanel::startChatroom2(const ChronosInvitation& invitation,
445 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700446{
Yingdi Yu42f66462013-10-31 17:38:22 -0700447 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
448 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
449
450 Name chatroomName("/ndn/broadcast/chronos");
451 chatroomName.append(invitation.getChatroom());
452
453 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
454 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
455
456 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
457 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
458
459 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
460
461 chatDialog->show();
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700462}
463
464void
Yingdi Yu42f66462013-10-31 17:38:22 -0700465ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
466 const security::IdentityCertificate& identityCertificate)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700467{
468 string prefix = m_localPrefix.toUri();
Yingdi Yu42f66462013-10-31 17:38:22 -0700469
470 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700471 //TODO:: open chat dialog
Yingdi Yu42f66462013-10-31 17:38:22 -0700472 _LOG_DEBUG("TO open chat dialog");
473 startChatroom2(invitation, identityCertificate);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700474}
475
476void
Yingdi Yu42f66462013-10-31 17:38:22 -0700477ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700478{
479 string empty;
Yingdi Yu42f66462013-10-31 17:38:22 -0700480 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu53eb8a72013-10-23 11:50:51 -0700481}
482
Yingdi Yua50c3252013-11-03 15:05:26 -0800483void
484ContactPanel::isIntroducerChanged(int state)
485{
486 if(state == Qt::Checked)
487 {
488 ui->addScope->setEnabled(true);
489 ui->deleteScope->setEnabled(true);
490 Ptr<TrustedContact> trustedContact = boost::dynamic_pointer_cast<TrustedContact>(m_currentSelectedContact);
491 }
492 else
493 {
494 ui->isIntroducer->setChecked(false);
495 ui->addScope->setEnabled(false);
496 ui->deleteScope->setEnabled(false);
497 }
498}
Yingdi Yu100267f2013-10-21 15:01:40 -0700499
Yingdi Yu9e0dc292013-10-10 11:39:45 -0700500#if WAF
501#include "contactpanel.moc"
502#include "contactpanel.cpp.moc"
503#endif