Add nick name in settings
diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp
index 4ecb7db..17ec238 100644
--- a/src/chatdialog.cpp
+++ b/src/chatdialog.cpp
@@ -37,6 +37,8 @@
                        const ndn::Name& chatroomPrefix,
 		       const ndn::Name& localPrefix,
                        const ndn::Name& defaultIdentity,
+                       const std::string& nick,
+                       bool trial,
 		       QWidget *parent) 
 : QDialog(parent)
   , ui(new Ui::ChatDialog)
@@ -45,6 +47,7 @@
   , m_localPrefix(localPrefix)
   , m_defaultIdentity(defaultIdentity)
   , m_invitationPolicyManager(ndn::Ptr<InvitationPolicyManager>(new InvitationPolicyManager(m_chatroomPrefix.get(-1).toUri())))
+  , m_nick(nick)
   , m_sock(NULL)
   , m_lastMsgTime(0)
   // , m_historyInitialized(false)
@@ -57,7 +60,7 @@
   ui->setupUi(this);
 
   m_localChatPrefix = m_localPrefix;
-  m_localChatPrefix.append("FH").append(m_defaultIdentity);
+  m_localChatPrefix.append("%F0.").append(m_defaultIdentity);
   m_localChatPrefix.append("chronos").append(m_chatroomPrefix.get(-1));
 
   m_session = time(NULL);
@@ -77,7 +80,7 @@
 
   m_timer = new QTimer(this);
 
-  setWrapper();
+  setWrapper(trial);
 
   connect(ui->inviteButton, SIGNAL(clicked()),
           this, SLOT(openInviteListDialog()));
@@ -188,13 +191,18 @@
 
 ChatDialog::~ChatDialog()
 {
-  delete ui;
-  sendLeave();
-  m_handler->shutdown();
+  _LOG_DEBUG("about to leave 4!");
+  if(m_sock != NULL)
+    {
+      sendLeave();
+      delete m_sock;
+      m_sock = NULL;
+    }
+  // m_handler->shutdown();
 }
 
 void
-ChatDialog::setWrapper()
+ChatDialog::setWrapper(bool trial)
 {
   m_identityManager = ndn::Ptr<ndn::security::IdentityManager>::Create();
   // ndn::Ptr<ndn::security::EncryptionManager> encryptionManager = ndn::Ptr<ndn::security::EncryptionManager>(new ndn::security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
@@ -205,13 +213,30 @@
   m_keychain = ndn::Ptr<ndn::security::Keychain>(new ndn::security::Keychain(m_identityManager, m_invitationPolicyManager, NULL));
 
   m_handler = ndn::Ptr<ndn::Wrapper>(new ndn::Wrapper(m_keychain));
+
+  if(trial == true)
+    {
+      ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(ndn::Name("/local/ndn/prefix")));
+      ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onUnverified,
+                                                                                           this,
+                                                                                           _1),
+                                                                               boost::bind(&ChatDialog::onTimeout,
+                                                                                           this,
+                                                                                           _1,
+                                                                                           _2),
+                                                                               boost::bind(&ChatDialog::onUnverified,
+                                                                                           this,
+                                                                                           _1)));
+
+      m_handler->sendInterest(interest, closure);
+    }
 }
 
 void
 ChatDialog::initializeSetting()
 {
   // TODO: nick name may be changed.
-  m_user.setNick(QString::fromStdString(m_defaultIdentity.get(-1).toUri()));
+  m_user.setNick(QString::fromStdString(m_nick));
   m_user.setChatroom(QString::fromStdString(m_chatroomPrefix.get(-1).toUri()));
   m_user.setOriginPrefix(QString::fromStdString(m_localPrefix.toUri()));
   m_user.setPrefix(QString::fromStdString(m_localChatPrefix.toUri()));
@@ -261,6 +286,8 @@
   const ndn::Blob& sigBits = sha256sig->getSignatureBits();
 
   interestName.append(sigBits.buf(), sigBits.size());
+  interestName.appendVersion();
+
 
   ndn::Ptr<ndn::Interest> interest = ndn::Ptr<ndn::Interest>(new ndn::Interest(interestName));
   ndn::Ptr<ndn::Closure> closure = ndn::Ptr<ndn::Closure>(new ndn::Closure(boost::bind(&ChatDialog::onInviteReplyVerified,
@@ -321,7 +348,10 @@
   ndn::Ptr<const ndn::signature::Sha256WithRsa> sha256sig = boost::dynamic_pointer_cast<const ndn::signature::Sha256WithRsa> (data->getSignature());
   const ndn::Name & keyLocatorName = sha256sig->getKeyLocator().getKeyName();
   ndn::Ptr<ndn::security::IdentityCertificate> dskCertificate = m_invitationPolicyManager->getValidatedDskCertificate(keyLocatorName);
-  m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
+  ndn::Name tmpPrefix(inviteePrefix);
+  ndn::Name prefix = tmpPrefix.getPrefix(tmpPrefix.size()-1);
+  m_syncPolicyManager->addChatDataRule(prefix, *dskCertificate, isIntroducer);
+  // m_syncPolicyManager->addChatDataRule(inviteePrefix, *dskCertificate, isIntroducer);
   publishIntroCert(dskCertificate, isIntroducer);
 }
 
@@ -361,6 +391,11 @@
 {}
 
 void
+ChatDialog::onTimeout(ndn::Ptr<ndn::Closure> closure, 
+                      ndn::Ptr<ndn::Interest> interest)
+{}
+
+void
 ChatDialog::initializeSync()
 {
   
@@ -970,6 +1005,16 @@
   sendInvitation(inviteeItem, isIntroducer);
 }
 
+void
+ChatDialog::closeEvent(QCloseEvent *e)
+{
+  hide();
+  _LOG_DEBUG("about to leave 1");
+  emit closeChatDialog(m_chatroomPrefix);
+}
+
+
+
 
 #if WAF
 #include "chatdialog.moc"
diff --git a/src/chatdialog.h b/src/chatdialog.h
index a4792bc..951edbb 100644
--- a/src/chatdialog.h
+++ b/src/chatdialog.h
@@ -48,6 +48,8 @@
                       const ndn::Name& chatroomPrefix,
                       const ndn::Name& localPrefix,
                       const ndn::Name& defaultIdentity,
+                      const std::string& nick,
+                      bool trial = false,
                       QWidget *parent = 0);
 
   // explicit ChatDialog(const ndn::Name& chatroomPrefix,
@@ -92,6 +94,9 @@
   void 
   processRemoveWrapper(std::string);
 
+protected:
+  void closeEvent(QCloseEvent *e);
+
 private:
 
   void
@@ -101,7 +106,7 @@
   updateLabels();
 
   void
-  setWrapper();
+  setWrapper(bool trial);
 
   void
   initializeSync();
@@ -130,6 +135,9 @@
   void
   onUnverified(ndn::Ptr<ndn::Data> data);
 
+  void
+  onTimeout(ndn::Ptr<ndn::Closure> closure, 
+            ndn::Ptr<ndn::Interest> interest);
 
 
   // void 
@@ -172,6 +180,9 @@
   void 
   removeReceived(QString prefix);
 
+  void
+  closeChatDialog(const ndn::Name& chatroomPrefix);
+
 public slots:
   void 
   processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
@@ -236,6 +247,7 @@
   ndn::Ptr<ndn::Wrapper> m_handler;
 
   User m_user; 
+  std::string m_nick;
   Sync::SyncSocket *m_sock;
   uint32_t m_session;
   DigestTreeScene *m_scene;
diff --git a/src/chronos-invitation.cpp b/src/chronos-invitation.cpp
index a36634b..5a392a1 100644
--- a/src/chronos-invitation.cpp
+++ b/src/chronos-invitation.cpp
@@ -19,9 +19,10 @@
 
 INIT_LOGGER("ChronosInvitation");
 
-ChronosInvitation::ChronosInvitation(const ndn::Name& interestName)
-  : m_interestName(interestName)
+ChronosInvitation::ChronosInvitation(const ndn::Name& originalInterestName)
+  : m_interestName(originalInterestName)
 {
+  Name interestName = originalInterestName.getPrefix(originalInterestName.size()-1);
   if(interestName.get(0).toUri() != string("ndn")
      || interestName.get(1).toUri() != string("broadcast")
      || interestName.get(2).toUri() != string("chronos")
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"
diff --git a/src/contactpanel.h b/src/contactpanel.h
index 222bf0b..fd76b0b 100644
--- a/src/contactpanel.h
+++ b/src/contactpanel.h
@@ -41,12 +41,16 @@
   Q_OBJECT
 
 public:
-  explicit ContactPanel(ndn::Ptr<ContactManager> contactManager, QWidget *parent = 0);
+  explicit ContactPanel(ndn::Ptr<ContactManager> contactManager, 
+                        QWidget *parent = 0);
 
   ~ContactPanel();
 
 private:
   void
+  createAction();
+
+  void
   openDB();
 
   void
@@ -59,37 +63,44 @@
   onLocalPrefixVerified(ndn::Ptr<ndn::Data> data);
   
   void
-  onLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest);
+  onLocalPrefixTimeout(ndn::Ptr<ndn::Closure> closure, 
+                       ndn::Ptr<ndn::Interest> interest);
 
   void
-  onUnverified(ndn::Ptr<ndn::Data> data);
-  
-  void
-  onTimeout(ndn::Ptr<ndn::Closure> closure, ndn::Ptr<ndn::Interest> interest);
-    
-  void
   setInvitationListener();
 
   void
   onInvitation(ndn::Ptr<ndn::Interest> interest);
 
   void
+  onUnverified(ndn::Ptr<ndn::Data> data);
+  
+  void
+  onTimeout(ndn::Ptr<ndn::Closure> closure, 
+            ndn::Ptr<ndn::Interest> interest);
+    
+  void
   onInvitationCertVerified(ndn::Ptr<ndn::Data> data,
                            ndn::Ptr<ChronosInvitation> invitation);
 
   void
+  popChatInvitation(ndn::Ptr<ChronosInvitation> invitation,
+                    const ndn::Name& inviterNameSpace,
+                    ndn::Ptr<ndn::security::IdentityCertificate> certificate);
+
+  void
   collectEndorsement();
 
   void
-  onDnsEndoreeVerified(ndn::Ptr<ndn::Data> data, int count);
+  onDnsEndorseeVerified(ndn::Ptr<ndn::Data> data, int count);
 
   void
-  onDnsEndoreeTimeout(ndn::Ptr<ndn::Closure> closure, 
+  onDnsEndorseeTimeout(ndn::Ptr<ndn::Closure> closure, 
                       ndn::Ptr<ndn::Interest> interest, 
                       int count);
   
   void
-  onDnsEndoreeUnverified(ndn::Ptr<ndn::Data> data, int count);
+  onDnsEndorseeUnverified(ndn::Ptr<ndn::Data> data, int count);
 
   void 
   updateCollectStatus(int count);
@@ -97,11 +108,6 @@
   std::string
   getRandomString();
 
-  void
-  popChatInvitation(ndn::Ptr<ChronosInvitation> invitation,
-                    const ndn::Name& inviterNameSpace,
-                    ndn::Ptr<ndn::security::IdentityCertificate> certificate);
-
 signals:
   void
   newInvitationReady();
@@ -112,7 +118,8 @@
                   const QItemSelection &deselected);
 
   void
-  updateDefaultIdentity(const QString& identity);
+  updateDefaultIdentity(const QString& identity,
+                        const QString& nickName);
 
   void
   openProfileEditor();
@@ -139,7 +146,9 @@
   showContextMenu(const QPoint& pos);
 
   void
-  startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer);
+  startChatroom(const QString& chatroom, 
+                const QString& invitee, 
+                bool isIntroducer);
 
   void 
   startChatroom2(const ChronosInvitation& invitation, 
@@ -166,6 +175,9 @@
 
   void
   endorseButtonClicked();
+  
+  void
+  removeChatDialog(const ndn::Name& chatroomName);
 
   
 
@@ -190,17 +202,14 @@
   ndn::Ptr<ndn::Wrapper> m_handler;
 
   ndn::Name m_defaultIdentity;
+  std::string m_nickName;
   ndn::Name m_localPrefix;
   ndn::Name m_inviteListenPrefix;
 
-  // std::string m_currentSelectedContactAlias;
-  // std::string m_currentSelectedContactNamespace;
   ndn::Ptr<ContactItem> m_currentSelectedContact;
   QSqlTableModel* m_trustScopeModel;
   QSqlTableModel* m_endorseDataModel;
   EndorseComboBoxDelegate* m_endorseComboBoxDelegate;
-
-
 };
 
 #endif // CONTACTPANEL_H
diff --git a/src/main.cpp b/src/main.cpp
index 0ae3da1..4e58e77 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -16,10 +16,13 @@
 #include "contact-storage.h"
 #include "dns-storage.h"
 #include "contact-manager.h"
+#include "logging.h"
 #include <ndn.cxx/security/identity/identity-manager.h>
 #include <ndn.cxx/security/identity/osx-privatekey-storage.h>
 #include <ndn.cxx/security/identity/basic-identity-storage.h>
 
+INIT_LOGGER("MAIN");
+
 using namespace ndn;
 
 class NewApp : public QApplication
@@ -27,7 +30,7 @@
 public:
   NewApp(int & argc, char ** argv)
     : QApplication(argc, argv)
-  {}
+  { }
 
   bool notify(QObject * receiver, QEvent * event) 
   {
@@ -48,14 +51,10 @@
 {
   NewApp app(argc, argv);
 
+  // app.setWindowIcon(QIcon(":/demo.icns"));
 
-//   app.setWindowIcon(QIcon("/Users/yuyingdi/Develop/QT/demo.icns"));
-// // #else
-// // 	app.setWindowIcon(QIcon("/Users/yuyingdi/Develop/QT/images/icon_large.png"));
-// // #endif
-
-  Ptr<ContactStorage> contactStorage = Ptr<ContactStorage>(new ContactStorage());
-  Ptr<DnsStorage> dnsStorage = Ptr<DnsStorage>(new DnsStorage());
+  Ptr<ContactStorage> contactStorage = Ptr<ContactStorage>::Create();
+  Ptr<DnsStorage> dnsStorage = Ptr<DnsStorage>::Create();
   Ptr<ContactManager> contactManager = Ptr<ContactManager>(new ContactManager(contactStorage, dnsStorage));
 
   ContactPanel contactPanel(contactManager);
diff --git a/src/settingdialog.cpp b/src/settingdialog.cpp
index a8dca16..ed9a1bd 100644
--- a/src/settingdialog.cpp
+++ b/src/settingdialog.cpp
@@ -29,21 +29,28 @@
 }
 
 void
-SettingDialog::setIdentity(const std::string& identity)
+SettingDialog::setIdentity(const std::string& identity,
+                           const std::string& nickName)
 { 
   m_identity = identity;
-  ui->identityLine->setText(QString::fromUtf8(m_identity.c_str()));
+  ui->identityLine->setText(QString::fromStdString(m_identity));
+  m_nickName = nickName;
+  ui->nickLine->setText(QString::fromStdString(m_nickName));
 }
 
 void
 SettingDialog::onOkClicked()
 {
-  QString text = ui->identityLine->text();
-  string identity = text.toUtf8().constData();
-  if(identity != m_identity)
+  QString qIdentity = ui->identityLine->text();
+  string identity = qIdentity.toStdString();
+  QString qNick = ui->nickLine->text();
+  string nick = qNick.toStdString();
+
+  if(identity != m_identity || nick != m_nickName)
     {
       m_identity = identity;
-      emit identitySet(text);
+      m_nickName = nick;
+      emit identitySet(qIdentity, qNick);
     }
   this->close();
 }
diff --git a/src/settingdialog.h b/src/settingdialog.h
index a06046d..9ee6865 100644
--- a/src/settingdialog.h
+++ b/src/settingdialog.h
@@ -26,11 +26,13 @@
   ~SettingDialog();
 
   void
-  setIdentity(const std::string& identity);
+  setIdentity(const std::string& identity,
+              const std::string& nickName);
 
 signals:
   void
-  identitySet(const QString& identity);
+  identitySet(const QString& identity,
+              const QString& nickName);
 
 private slots:
   void
@@ -39,6 +41,7 @@
 private:
   Ui::SettingDialog *ui;
   std::string m_identity;
+  std::string m_nickName;
 };
 
 #endif // SETTINGDIALOG_H
diff --git a/src/settingdialog.ui b/src/settingdialog.ui
index 5d3c04d..271ef23 100644
--- a/src/settingdialog.ui
+++ b/src/settingdialog.ui
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>300</width>
-    <height>140</height>
+    <height>150</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -17,7 +17,7 @@
    <property name="geometry">
     <rect>
      <x>100</x>
-     <y>90</y>
+     <y>110</y>
      <width>100</width>
      <height>32</height>
     </rect>
@@ -29,8 +29,8 @@
   <widget class="QLabel" name="msgLabel">
    <property name="geometry">
     <rect>
-     <x>20</x>
-     <y>20</y>
+     <x>10</x>
+     <y>10</y>
      <width>150</width>
      <height>16</height>
     </rect>
@@ -48,9 +48,38 @@
   <widget class="QLineEdit" name="identityLine">
    <property name="geometry">
     <rect>
-     <x>20</x>
-     <y>50</y>
-     <width>260</width>
+     <x>10</x>
+     <y>30</y>
+     <width>280</width>
+     <height>21</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QLabel" name="nickLabel">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>60</y>
+     <width>150</width>
+     <height>16</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <weight>75</weight>
+     <bold>true</bold>
+    </font>
+   </property>
+   <property name="text">
+    <string>Nick Name:</string>
+   </property>
+  </widget>
+  <widget class="QLineEdit" name="nickLine">
+   <property name="geometry">
+    <rect>
+     <x>10</x>
+     <y>80</y>
+     <width>280</width>
      <height>21</height>
     </rect>
    </property>