Add nick name in settings
diff --git a/src/contactpanel.cpp b/src/contactpanel.cpp
index b22f460..361b7bd 100644
--- a/src/contactpanel.cpp
+++ b/src/contactpanel.cpp
@@ -49,12 +49,12 @@
   , m_startChatDialog(new StartChatDialog)
   , m_invitationDialog(new InvitationDialog)
   , m_settingDialog(new SettingDialog)
-  , m_menuInvite(new QAction("&Chat", this))
-  , m_menuAlias(new QAction("&Set Alias", this))
 {
   qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
   qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
   
+  createAction();
+
   openDB();    
 
   m_contactManager = contactManager;
@@ -63,24 +63,27 @@
   m_setAliasDialog = new SetAliasDialog(contactManager);
  
   ui->setupUi(this);
+
   refreshContactList();
 
   setKeychain();
-  m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
-   
+
+  m_defaultIdentity = m_keychain->getDefaultIdentity();
+  m_nickName = m_defaultIdentity.get(-1).toUri();
+  m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
+
+  m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));  
+
   setLocalPrefix();
     
-  // Set Identity, TODO: through user interface
-  m_defaultIdentity = m_keychain->getDefaultIdentity();
-  m_settingDialog->setIdentity(m_defaultIdentity.toUri());
   setInvitationListener();
+
   collectEndorsement();
   
   ui->ContactList->setModel(m_contactListModel);
   
-  QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
 
-  connect(selectionModel, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
+  connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
           this, SLOT(updateSelection(const QItemSelection &, const QItemSelection &)));
 
   connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
@@ -97,7 +100,6 @@
    
   connect(m_addContactPanel, SIGNAL(newContactAdded()),
           this, SLOT(refreshContactList()));
-
   connect(m_setAliasDialog, SIGNAL(aliasChanged()),
           this, SLOT(refreshContactList()));
 
@@ -109,8 +111,8 @@
   connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
           this, SLOT(rejectInvitation(const ChronosInvitation&)));
 
-  connect(m_settingDialog, SIGNAL(identitySet(const QString&)),
-          this, SLOT(updateDefaultIdentity(const QString&)));
+  connect(m_settingDialog, SIGNAL(identitySet(const QString&, const QString&)),
+          this, SLOT(updateDefaultIdentity(const QString&, const QString&)));
 
   connect(this, SIGNAL(newInvitationReady()),
           this, SLOT(openInvitationDialog()));
@@ -120,10 +122,8 @@
 
   connect(ui->addScope, SIGNAL(clicked()),
           this, SLOT(addScopeClicked()));
-
   connect(ui->deleteScope, SIGNAL(clicked()),
           this, SLOT(deleteScopeClicked()));
-
   connect(ui->saveButton, SIGNAL(clicked()),
           this, SLOT(saveScopeClicked()));
 
@@ -135,12 +135,20 @@
 {
   delete ui;
   delete m_contactListModel;
+  delete m_startChatDialog;
+  delete m_invitationDialog;
+  delete m_settingDialog;
+
   delete m_profileEditor;
   delete m_addContactPanel;
+  delete m_setAliasDialog;
+
   delete m_trustScopeModel;
   delete m_endorseDataModel;
+  delete m_endorseComboBoxDelegate;
 
   delete m_menuInvite;
+  delete m_menuAlias;
 
   map<Name, ChatDialog*>::iterator it = m_chatDialogs.begin();
   for(; it != m_chatDialogs.end(); it++)
@@ -148,6 +156,13 @@
 }
 
 void
+ContactPanel::createAction()
+{
+  m_menuInvite = new QAction("&Chat", this);
+  m_menuAlias = new QAction("&Set Alias", this);
+}
+
+void
 ContactPanel::openDB()
 {
   QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
@@ -155,7 +170,7 @@
   path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append("chronos.db");
   db.setDatabaseName(path);
   bool ok = db.open();
-  _LOG_DEBUG("db opened: " << std::boolalpha << ok );
+  _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
 
   m_trustScopeModel = new QSqlTableModel;
   m_endorseDataModel = new QSqlTableModel;
@@ -166,11 +181,10 @@
 ContactPanel::setKeychain()
 {
   m_panelPolicyManager = Ptr<PanelPolicyManager>::Create();
-  // Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
 
   vector<Ptr<ContactItem> >::const_iterator it = m_contactList.begin();
   for(; it != m_contactList.end(); it++)
-      m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
+    m_panelPolicyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
 
   m_keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), 
                                                               m_panelPolicyManager, 
@@ -216,151 +230,32 @@
 }
 
 void
-ContactPanel::onUnverified(Ptr<Data> data)
-{}
-
-void
-ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
-{}
-
-void
-ContactPanel::collectEndorsement()
-{
-  m_collectStatus = Ptr<vector<bool> >::Create();
-  m_collectStatus->assign(m_contactList.size(), false);
-  vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
-  int count = 0;
-  for(; it != m_contactList.end(); it++, count++)
-    {
-      Name interestName = (*it)->getNameSpace();
-      interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
-      Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
-      interest->setChildSelector(Interest::CHILD_RIGHT);
-  
-      Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndoreeVerified, 
-                                                                  this,
-                                                                  _1,
-                                                                  count),
-                                                      boost::bind(&ContactPanel::onDnsEndoreeTimeout,
-                                                                  this,
-                                                                  _1, 
-                                                                  _2,
-                                                                  count),
-                                                      boost::bind(&ContactPanel::onDnsEndoreeUnverified,
-                                                                  this,
-                                                                  _1,
-                                                                  count)));
-
-      m_handler->sendInterest(interest, closure);
-    }
-}
-
-void
-ContactPanel::onDnsEndoreeVerified(Ptr<Data> data, int count)
-{
-  Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
-  Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
-  EndorseCertificate endorseCertificate(*endorseData);
-
-  _LOG_DEBUG("get data: " << endorseCertificate.getName().toUri());
-
-  m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
-
-  updateCollectStatus(count);
-}
-
-void
-ContactPanel::onDnsEndoreeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
-{ updateCollectStatus(count); }
-
-void
-ContactPanel::onDnsEndoreeUnverified(Ptr<Data> data, int count)
-{ updateCollectStatus(count); }
-
-void 
-ContactPanel::updateCollectStatus(int count)
-{
-  m_collectStatus->at(count) = true;
-  vector<bool>::const_iterator it = m_collectStatus->begin();
-  for(; it != m_collectStatus->end(); it++)
-    if(*it == false)
-      return;
-
-  m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
-}
-
-void
-ContactPanel::onInvitationCertVerified(Ptr<Data> data, 
-                                       Ptr<ChronosInvitation> invitation)
-{
-  Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
-  
-  if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
-    {
-      Name keyName = certificate->getPublicKeyName();
-      Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
-      popChatInvitation(invitation, inviterNameSpace, certificate);
-    }
-}
-
-void
-ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
-                                const Name& inviterNameSpace,
-                                Ptr<security::IdentityCertificate> certificate)
-{
-  string chatroom = invitation->getChatroom().get(0).toUri();
-  string inviter = inviterNameSpace.toUri();
-
-  string alias;
-  vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
-  for(; it != m_contactList.end(); it++)
-    if((*it)->getNameSpace() == inviterNameSpace)
-      alias = (*it)->getAlias();
-
-  if(it != m_contactList.end())
-    return;
-
-  m_invitationDialog->setInvitation(alias, invitation, certificate);
-  emit newInvitationReady();
-}
-
-static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
-
-string
-ContactPanel::getRandomString()
-{
-  string randStr;
-  boost::random::random_device rng;
-  boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
-  for (int i = 0; i < 10; i ++)
-  {
-    randStr += chars[index_dist(rng)];
-  }
-  return randStr;
-}
-
-
-void
 ContactPanel::setInvitationListener()
 {
-  Name prefix("/ndn/broadcast/chronos/invitation");
-  prefix.append(m_defaultIdentity);
-  _LOG_DEBUG("prefix: " << prefix.toUri());
-  m_inviteListenPrefix = prefix;
-  m_handler->setInterestFilter (prefix, 
+  m_inviteListenPrefix = Name("/ndn/broadcast/chronos/invitation");
+  m_inviteListenPrefix.append(m_defaultIdentity);
+  _LOG_DEBUG("Listening for invitation on prefix: " << m_inviteListenPrefix.toUri());
+  m_handler->setInterestFilter (m_inviteListenPrefix, 
                                 boost::bind(&ContactPanel::onInvitation, 
                                             this,
                                             _1));
-
 }
 
 void
 ContactPanel::onInvitation(Ptr<Interest> interest)
 {
-  _LOG_DEBUG("receive interest!" << interest->getName().toUri());
-  const Name& interestName = interest->getName();
+  _LOG_DEBUG("Receive invitation!" << interest->getName().toUri());
 
-  Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
+  Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interest->getName()));
+
+  Name chatroomName("/ndn/broadcast/chronos");
+  chatroomName.append(invitation->getChatroom());
+  map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
+  if(it != m_chatDialogs.end())
+    {
+      _LOG_DEBUG("Exisiting chatroom!");
+      return;
+    }
 
   Ptr<Interest> newInterest = Ptr<Interest>(new Interest(invitation->getInviterCertificateName()));
   Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onInvitationCertVerified, 
@@ -378,12 +273,134 @@
 }
 
 void
+ContactPanel::onInvitationCertVerified(Ptr<Data> data, 
+                                       Ptr<ChronosInvitation> invitation)
+{
+  using namespace ndn::security;
+  Ptr<IdentityCertificate> certificate = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
+  
+  if(PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
+    {
+      Name keyName = certificate->getPublicKeyName();
+      Name inviterNameSpace = keyName.getPrefix(keyName.size() - 1);
+      popChatInvitation(invitation, inviterNameSpace, certificate);
+    }
+}
+
+void
+ContactPanel::onUnverified(Ptr<Data> data)
+{}
+
+void
+ContactPanel::onTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
+{}
+
+void
+ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
+                                const Name& inviterNameSpace,
+                                Ptr<security::IdentityCertificate> certificate)
+{
+  string alias;
+  vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
+  for(; it != m_contactList.end(); it++)
+    if((*it)->getNameSpace() == inviterNameSpace)
+      alias = (*it)->getAlias();
+
+  if(it != m_contactList.end())
+    return;
+
+  m_invitationDialog->setInvitation(alias, invitation, certificate);
+  emit newInvitationReady();
+}
+
+void
+ContactPanel::collectEndorsement()
+{
+  m_collectStatus = Ptr<vector<bool> >::Create();
+  m_collectStatus->assign(m_contactList.size(), false);
+
+  vector<Ptr<ContactItem> >::iterator it = m_contactList.begin();
+  int count = 0;
+  for(; it != m_contactList.end(); it++, count++)
+    {
+      Name interestName = (*it)->getNameSpace();
+      interestName.append("DNS").append(m_defaultIdentity).append("ENDORSEE");
+      Ptr<Interest> interest = Ptr<Interest>(new Interest(interestName));
+      interest->setChildSelector(Interest::CHILD_RIGHT);
+      interest->setInterestLifetime(1);
+  
+      Ptr<Closure> closure = Ptr<Closure>(new Closure(boost::bind(&ContactPanel::onDnsEndorseeVerified, 
+                                                                  this,
+                                                                  _1,
+                                                                  count),
+                                                      boost::bind(&ContactPanel::onDnsEndorseeTimeout,
+                                                                  this,
+                                                                  _1, 
+                                                                  _2,
+                                                                  count),
+                                                      boost::bind(&ContactPanel::onDnsEndorseeUnverified,
+                                                                  this,
+                                                                  _1,
+                                                                  count)));
+
+      m_handler->sendInterest(interest, closure);
+    }
+}
+
+void
+ContactPanel::onDnsEndorseeVerified(Ptr<Data> data, int count)
+{
+  Ptr<Blob> contentBlob = Ptr<Blob>(new Blob(data->content().buf(), data->content().size()));
+  Ptr<Data> endorseData = Data::decodeFromWire(contentBlob);
+  EndorseCertificate endorseCertificate(*endorseData);
+
+  m_contactManager->getContactStorage()->updateCollectEndorse(endorseCertificate);
+
+  updateCollectStatus(count);
+}
+
+void
+ContactPanel::onDnsEndorseeTimeout(Ptr<Closure> closure, Ptr<Interest> interest, int count)
+{ updateCollectStatus(count); }
+
+void
+ContactPanel::onDnsEndorseeUnverified(Ptr<Data> data, int count)
+{ updateCollectStatus(count); }
+
+void 
+ContactPanel::updateCollectStatus(int count)
+{
+  m_collectStatus->at(count) = true;
+  vector<bool>::const_iterator it = m_collectStatus->begin();
+  for(; it != m_collectStatus->end(); it++)
+    if(*it == false)
+      return;
+
+  m_contactManager->publishEndorsedDataInDns(m_defaultIdentity);
+}
+
+static std::string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789");
+
+string
+ContactPanel::getRandomString()
+{
+  string randStr;
+  boost::random::random_device rng;
+  boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
+  for (int i = 0; i < 10; i ++)
+  {
+    randStr += chars[index_dist(rng)];
+  }
+  return randStr;
+}
+
+void
 ContactPanel::updateSelection(const QItemSelection &selected,
 			      const QItemSelection &deselected)
 {
   QModelIndexList items = selected.indexes();
   QString text = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
-  string alias = text.toUtf8().constData();
+  string alias = text.toStdString();
 
   int i = 0;
   for(; i < m_contactList.size(); i++)
@@ -393,17 +410,13 @@
     }
   
   m_currentSelectedContact = m_contactList[i];
-  QString name = QString::fromUtf8(m_currentSelectedContact->getName().c_str());
-  QString institution = QString::fromUtf8(m_currentSelectedContact->getInstitution().c_str());
-  QString nameSpace = QString::fromUtf8(m_currentSelectedContact->getNameSpace().toUri().c_str());
+  QString name = QString::fromStdString(m_currentSelectedContact->getName());
+  QString institution = QString::fromStdString(m_currentSelectedContact->getInstitution());
+  QString nameSpace = QString::fromStdString(m_currentSelectedContact->getNameSpace().toUri());
   ui->NameData->setText(name);
   ui->NameSpaceData->setText(nameSpace);
   ui->InstitutionData->setText(institution);
 
-  // m_currentSelectedContactAlias = alias;
-  // m_currentSelectedContactNamespace = m_contactList[i]->getNameSpace().toUri();
-  // _LOG_DEBUG("current Alias: " << m_currentSelectedContact);
-
   if(m_currentSelectedContact->isIntroducer())
     {
       ui->isIntroducer->setChecked(true);
@@ -422,8 +435,6 @@
       m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
       m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
 
-      // _LOG_DEBUG("row count: " << m_trustScopeModel->rowCount());
-
       ui->trustScopeList->setModel(m_trustScopeModel);
       ui->trustScopeList->setColumnHidden(0, true);
       ui->trustScopeList->setColumnHidden(1, true);
@@ -473,25 +484,15 @@
   ui->endorseList->resizeColumnToContents(2);
   ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
   ui->endorseList->show();
-
 }
 
 void
-ContactPanel::updateDefaultIdentity(const QString& identity)
+ContactPanel::updateDefaultIdentity(const QString& identity, const QString& nickName)
 { 
-  m_defaultIdentity = Name(identity.toUtf8().constData()); 
-  Name prefix("/ndn/broadcast/chronos/invitation");
-  prefix.append(m_defaultIdentity);
-
-  _LOG_DEBUG("reset invite listen prefix: " << prefix.toUri());
-  
+  m_defaultIdentity = Name(identity.toStdString());
+  m_nickName = nickName.toStdString();
   m_handler->clearInterestFilter(m_inviteListenPrefix);
-  m_handler->setInterestFilter(prefix,
-                               boost::bind(&ContactPanel::onInvitation, 
-                                           this,
-                                           _1));
-  m_inviteListenPrefix = prefix;
-
+  setInvitationListener();
   collectEndorsement();
 }
 
@@ -542,7 +543,7 @@
 void
 ContactPanel::openSettingDialog()
 {
-  m_settingDialog->setIdentity(m_defaultIdentity.toUri());
+  m_settingDialog->setIdentity(m_defaultIdentity.toUri(), m_nickName);
   m_settingDialog->show();
 }
 
@@ -569,10 +570,14 @@
   _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
 
   Name chatroomName(chatroom.toUtf8().constData());
-  
-  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
+  Name prefix = m_localPrefix.getPrefix(m_localPrefix.size()-1);
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, prefix, m_defaultIdentity, m_nickName);
+  // ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
-  
+
+  connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
+          this, SLOT(removeChatDialog(const ndn::Name&)));
+
   //TODO: send invitation
   Name inviteeNamespace(invitee.toUtf8().constData());
   Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
@@ -592,15 +597,23 @@
 
   Name chatroomName("/ndn/broadcast/chronos");
   chatroomName.append(invitation.getChatroom());
+  Name prefix = m_localPrefix.getPrefix(m_localPrefix.size()-1);
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, prefix, m_defaultIdentity, m_nickName, true);
+  // ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
+  connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
+          this, SLOT(removeChatDialog(const ndn::Name&)));
 
-  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
-  chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
+  Name inviterPrefix = invitation.getInviterPrefix().getPrefix(invitation.getInviterPrefix().size()-1);
+  chatDialog->addChatDataRule(inviterPrefix, identityCertificate, true);
+  // chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
 
   Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
   chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
   
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
 
+
+
   chatDialog->show();
 }
 
@@ -719,6 +732,21 @@
   m_contactManager->updateEndorseCertificate(m_currentSelectedContact->getNameSpace(), m_defaultIdentity);
 }
 
+void
+ContactPanel::removeChatDialog(const ndn::Name& chatroomName)
+{
+  map<Name, ChatDialog*>::iterator it = m_chatDialogs.find(chatroomName);
+  _LOG_DEBUG("about to leave 2!");
+  ChatDialog* deletedChat;
+  if(it != m_chatDialogs.end())
+    {
+      _LOG_DEBUG("about to leave 3!");
+      deletedChat = it->second;
+      m_chatDialogs.erase(it);      
+    }
+  delete deletedChat;
+}
+
 #if WAF
 #include "contactpanel.moc"
 #include "contactpanel.cpp.moc"