blob: cc44a3756e20e1a4a2cce79472e53aa31050b428 [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 Yu40eca752013-10-10 15:00:58 -070019
Yingdi Yu3b318c12013-10-15 17:54:00 -070020#ifndef Q_MOC_RUN
Yingdi Yu978b3ae2013-10-23 11:50:51 -070021#include <ndn.cxx/security/keychain.h>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070022#include <ndn.cxx/security/identity/identity-manager.h>
Yingdi Yu595aadc2013-10-21 15:01:40 -070023#include <ndn.cxx/common.h>
Yingdi Yu3b318c12013-10-15 17:54:00 -070024#include <boost/filesystem.hpp>
Yingdi Yu978b3ae2013-10-23 11:50:51 -070025#include <boost/random/random_device.hpp>
26#include <boost/random/uniform_int_distribution.hpp>
Yingdi Yu7989eb22013-10-31 17:38:22 -070027#include "panel-policy-manager.h"
Yingdi Yu3b318c12013-10-15 17:54:00 -070028#include "logging.h"
29#include "exception.h"
30#endif
31
32namespace fs = boost::filesystem;
33using namespace ndn;
Yingdi Yu978b3ae2013-10-23 11:50:51 -070034
Yingdi Yu2ac40fb2013-10-21 13:38:38 -070035using namespace std;
Yingdi Yu3b318c12013-10-15 17:54:00 -070036
37INIT_LOGGER("ContactPanel");
38
Yingdi Yu978b3ae2013-10-23 11:50:51 -070039Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
Yingdi Yu7989eb22013-10-31 17:38:22 -070040Q_DECLARE_METATYPE(ChronosInvitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -070041
Yingdi Yu9236c432013-10-18 11:29:25 -070042ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent)
Yingdi Yuec5e72a2013-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 Yu847aa862013-10-09 16:35:53 -070051{
Yingdi Yu978b3ae2013-10-23 11:50:51 -070052 qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
Yingdi Yu7989eb22013-10-31 17:38:22 -070053 qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
Yingdi Yu3b318c12013-10-15 17:54:00 -070054
Yingdi Yu7989eb22013-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 Yueda39aa2013-10-23 23:07:29 -070062 ui->setupUi(this);
63 refreshContactList();
Yingdi Yu40eca752013-10-10 15:00:58 -070064
Yingdi Yueda39aa2013-10-23 23:07:29 -070065 setKeychain();
66 m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070067
Yingdi Yueda39aa2013-10-23 23:07:29 -070068 setLocalPrefix();
Yingdi Yu978b3ae2013-10-23 11:50:51 -070069
Yingdi Yueda39aa2013-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 Yu8f7325a2013-10-17 17:03:46 -070078
Yingdi Yueda39aa2013-10-23 23:07:29 -070079 connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
80 this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
Yingdi Yu40eca752013-10-10 15:00:58 -070081
Yingdi Yueda39aa2013-10-23 23:07:29 -070082 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
83 this, SLOT(showContextMenu(const QPoint&)));
Yingdi Yu0269c872013-10-16 13:16:49 -070084
Yingdi Yueda39aa2013-10-23 23:07:29 -070085 connect(ui->EditProfileButton, SIGNAL(clicked()),
86 this, SLOT(openProfileEditor()));
Yingdi Yu0269c872013-10-16 13:16:49 -070087
Yingdi Yueda39aa2013-10-23 23:07:29 -070088 connect(ui->AddContactButton, SIGNAL(clicked()),
89 this, SLOT(openAddContactPanel()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070090
Yingdi Yueda39aa2013-10-23 23:07:29 -070091 connect(ui->settingButton, SIGNAL(clicked()),
92 this, SLOT(openSettingDialog()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070093
Yingdi Yueda39aa2013-10-23 23:07:29 -070094 connect(m_addContactPanel, SIGNAL(newContactAdded()),
95 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070096
Yingdi Yueda39aa2013-10-23 23:07:29 -070097 connect(m_setAliasDialog, SIGNAL(aliasChanged()),
98 this, SLOT(refreshContactList()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -070099
Yingdi Yueda39aa2013-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 Yu978b3ae2013-10-23 11:50:51 -0700102
Yingdi Yu7989eb22013-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 Yu978b3ae2013-10-23 11:50:51 -0700107
Yingdi Yueda39aa2013-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 Yu595aadc2013-10-21 15:01:40 -0700113
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800114 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
115 this, SLOT(isIntroducerChanged(int)));
Yingdi Yu847aa862013-10-09 16:35:53 -0700116}
117
118ContactPanel::~ContactPanel()
119{
Yingdi Yueda39aa2013-10-23 23:07:29 -0700120 delete ui;
121 delete m_contactListModel;
122 delete m_profileEditor;
123 delete m_addContactPanel;
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800124 if(NULL != m_currentContactTrustScopeListModel)
125 delete m_currentContactTrustScopeListModel;
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700126
Yingdi Yueda39aa2013-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 Yu847aa862013-10-09 16:35:53 -0700132}
133
Yingdi Yu40eca752013-10-10 15:00:58 -0700134void
Yingdi Yu978b3ae2013-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 Yu7989eb22013-10-31 17:38:22 -0700142 _LOG_DEBUG("db opened: " << std::boolalpha << ok );
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700143}
144
145void
146ContactPanel::setKeychain()
147{
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700148 m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700149 // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700150
151 vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
152 for(; it != m_contactList.end(); it++)
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700153 m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700154
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700155 m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
156 m_panelPolicyManager,
157 NULL));
Yingdi Yu978b3ae2013-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 Yu978b3ae2013-10-23 11:50:51 -0700186 m_localPrefix = Name(prefix);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700187 m_localPrefix.append(randomSuffix);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700188}
189
190void
191ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
Yingdi Yu7989eb22013-10-31 17:38:22 -0700192{
193 string randomSuffix = getRandomString();
194 m_localPrefix = Name("/private/local");
195 m_localPrefix.append(randomSuffix);
196}
Yingdi Yu978b3ae2013-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 Yu9b34b1f2013-11-01 17:37:51 -0700208 Ptr<ChronosInvitation> invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700209{
210 Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
Yingdi Yu7989eb22013-10-31 17:38:22 -0700211
212 if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700213 {
214 Name keyName = certificate->getPublicKeyName();
215 Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700216 popChatInvitation(invitation, inviterNameSpace, certificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700217 }
218}
219
220void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700221ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700222 const Name& inviterNameSpace,
223 Ptr<security::IdentityCertificate> certificate)
224{
Yingdi Yu7989eb22013-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 Yu978b3ae2013-10-23 11:50:51 -0700235 return;
236
Yingdi Yu7989eb22013-10-31 17:38:22 -0700237 m_invitationDialog->setInvitation(alias, invitation, certificate);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700238 emit newInvitationReady();
Yingdi Yu978b3ae2013-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 Yueda39aa2013-10-23 23:07:29 -0700262 _LOG_DEBUG("prefix: " << prefix.toUri());
263 m_inviteListenPrefix = prefix;
Yingdi Yu978b3ae2013-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 Yueda39aa2013-10-23 23:07:29 -0700274 _LOG_DEBUG("receive interest!" << interest->getName().toUri());
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700275 const Name& interestName = interest->getName();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700276
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700277 Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700278
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700279 Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700280 Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified,
281 this,
282 _1,
Yingdi Yu9b34b1f2013-11-01 17:37:51 -0700283 invitation),
Yingdi Yu978b3ae2013-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 Yu40eca752013-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 Yu2ac40fb2013-10-21 13:38:38 -0700300 string alias = text.toUtf8().constData();
301
Yingdi Yu2ac40fb2013-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 Yuec5e72a2013-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 Yu2ac40fb2013-10-21 13:38:38 -0700313 ui->NameData->setText(name);
314 ui->NameSpaceData->setText(nameSpace);
315 ui->InstitutionData->setText(institution);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700316
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800317 // m_currentSelectedContactAlias = alias;
318 // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700319 // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
Yingdi Yuec5e72a2013-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);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800326 m_currentContactTrustScopeListModel = new QStringListModel;
327 }
328 else
329 {
330 ui->isIntroducer->setChecked(false);
331 ui->addScope->setEnabled(false);
332 ui->deleteScope->setEnabled(false);
333 delete m_currentContactTrustScopeListModel;
334 }
Yingdi Yu40eca752013-10-10 15:00:58 -0700335}
Yingdi Yu847aa862013-10-09 16:35:53 -0700336
Yingdi Yu3b318c12013-10-15 17:54:00 -0700337void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700338ContactPanel::updateDefaultIdentity(const QString& identity)
Yingdi Yueda39aa2013-10-23 23:07:29 -0700339{
340 m_defaultIdentity = Name(identity.toUtf8().constData());
341 Name prefix("/ndn/broadcast/chronos/invitation");
342 prefix.append(m_defaultIdentity);
343
344 _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
345
346 m_handler->clearInterestFilter(m_inviteListenPrefix);
347 m_handler->setInterestFilter(prefix,
348 boost::bind(&ContactPanel::onInvitation,
349 this,
350 _1));
351 m_inviteListenPrefix = prefix;
352}
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700353
354void
Yingdi Yu3b318c12013-10-15 17:54:00 -0700355ContactPanel::openProfileEditor()
Yingdi Yu0269c872013-10-16 13:16:49 -0700356{ m_profileEditor->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700357
Yingdi Yu0269c872013-10-16 13:16:49 -0700358void
359ContactPanel::openAddContactPanel()
360{ m_addContactPanel->show(); }
Yingdi Yu3b318c12013-10-15 17:54:00 -0700361
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700362void
Yingdi Yueda39aa2013-10-23 23:07:29 -0700363ContactPanel::openInvitationDialog()
364{ m_invitationDialog->show(); }
365
366void
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700367ContactPanel::refreshContactList()
368{
369 m_contactList = m_contactManager->getContactItemList();
370 QStringList contactNameList;
371 for(int i = 0; i < m_contactList.size(); i++)
372 contactNameList << QString::fromUtf8(m_contactList[i]->getAlias().c_str());
373
374 m_contactListModel->setStringList(contactNameList);
375}
376
377void
378ContactPanel::showContextMenu(const QPoint& pos)
379{
380 QMenu menu(ui->ContactList);
381 menu.addAction(m_menuInvite);
Yingdi Yu595aadc2013-10-21 15:01:40 -0700382 connect(m_menuInvite, SIGNAL(triggered()),
383 this, SLOT(openStartChatDialog()));
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700384 menu.addAction(m_menuAlias);
385 connect(m_menuAlias, SIGNAL(triggered()),
386 this, SLOT(openSetAliasDialog()));
387 menu.exec(ui->ContactList->mapToGlobal(pos));
388
389}
390
391void
392ContactPanel::openSetAliasDialog()
393{
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800394 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact->getNameSpace().toUri());
Yingdi Yu2ac40fb2013-10-21 13:38:38 -0700395 m_setAliasDialog->show();
396}
397
Yingdi Yu595aadc2013-10-21 15:01:40 -0700398void
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700399ContactPanel::openSettingDialog()
400{
401 m_settingDialog->setIdentity(m_defaultIdentity.toUri());
402 m_settingDialog->show();
403}
404
405void
Yingdi Yu595aadc2013-10-21 15:01:40 -0700406ContactPanel::openStartChatDialog()
407{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700408 // TimeInterval ti = time::NowUnixTimestamp();
409 // ostringstream oss;
410 // oss << ti.total_seconds();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700411
412 Name chatroom("/ndn/broadcast/chronos");
Yingdi Yu7989eb22013-10-31 17:38:22 -0700413 chatroom.append(string("chatroom-") + getRandomString());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700414
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800415 m_startChatDialog->setInvitee(m_currentSelectedContact->getNameSpace().toUri(), chatroom.toUri());
Yingdi Yu595aadc2013-10-21 15:01:40 -0700416 m_startChatDialog->show();
417}
418
Yingdi Yu7989eb22013-10-31 17:38:22 -0700419// For inviter
Yingdi Yu595aadc2013-10-21 15:01:40 -0700420void
421ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
422{
423 _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
424 _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
425 _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700426
Yingdi Yu7989eb22013-10-31 17:38:22 -0700427 Name chatroomName(chatroom.toUtf8().constData());
Yingdi Yueda39aa2013-10-23 23:07:29 -0700428
Yingdi Yu7989eb22013-10-31 17:38:22 -0700429 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700430 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
431
432 //TODO: send invitation
433 Name inviteeNamespace(invitee.toUtf8().constData());
434 Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
435
Yingdi Yu7989eb22013-10-31 17:38:22 -0700436 chatDialog->sendInvitation(inviteeItem, isIntroducer);
Yingdi Yueda39aa2013-10-23 23:07:29 -0700437
438 chatDialog->show();
Yingdi Yu595aadc2013-10-21 15:01:40 -0700439}
440
Yingdi Yu7989eb22013-10-31 17:38:22 -0700441// For Invitee
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700442void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700443ContactPanel::startChatroom2(const ChronosInvitation& invitation,
444 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700445{
Yingdi Yu7989eb22013-10-31 17:38:22 -0700446 _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
447 _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
448
449 Name chatroomName("/ndn/broadcast/chronos");
450 chatroomName.append(invitation.getChatroom());
451
452 ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
453 chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
454
455 Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
456 chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
457
458 m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
459
460 chatDialog->show();
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700461}
462
463void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700464ContactPanel::acceptInvitation(const ChronosInvitation& invitation,
465 const security::IdentityCertificate& identityCertificate)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700466{
467 string prefix = m_localPrefix.toUri();
Yingdi Yu7989eb22013-10-31 17:38:22 -0700468
469 m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700470 //TODO:: open chat dialog
Yingdi Yu7989eb22013-10-31 17:38:22 -0700471 _LOG_DEBUG("TO open chat dialog");
472 startChatroom2(invitation, identityCertificate);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700473}
474
475void
Yingdi Yu7989eb22013-10-31 17:38:22 -0700476ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700477{
478 string empty;
Yingdi Yu7989eb22013-10-31 17:38:22 -0700479 m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
Yingdi Yu978b3ae2013-10-23 11:50:51 -0700480}
481
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800482void
483ContactPanel::isIntroducerChanged(int state)
484{
485 if(state == Qt::Checked)
486 {
487 ui->addScope->setEnabled(true);
488 ui->deleteScope->setEnabled(true);
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800489 }
490 else
491 {
Yingdi Yuec5e72a2013-11-03 15:05:26 -0800492 ui->addScope->setEnabled(false);
493 ui->deleteScope->setEnabled(false);
494 }
495}
Yingdi Yu595aadc2013-10-21 15:01:40 -0700496
Yingdi Yu847aa862013-10-09 16:35:53 -0700497#if WAF
498#include "contactpanel.moc"
499#include "contactpanel.cpp.moc"
500#endif