partially working new chronochat
diff --git a/src/contactpanel.cpp b/src/contactpanel.cpp
index 5c4cfc3..0ce0694 100644
--- a/src/contactpanel.cpp
+++ b/src/contactpanel.cpp
@@ -28,7 +28,7 @@
 #include <boost/filesystem.hpp>
 #include <boost/random/random_device.hpp>
 #include <boost/random/uniform_int_distribution.hpp>
-#include "invitation-policy-manager.h"
+#include "panel-policy-manager.h"
 #include "logging.h"
 #include "exception.h"
 #endif
@@ -41,15 +41,12 @@
 INIT_LOGGER("ContactPanel");
 
 Q_DECLARE_METATYPE(ndn::security::IdentityCertificate)
+Q_DECLARE_METATYPE(ChronosInvitation)
 
 ContactPanel::ContactPanel(Ptr<ContactManager> contactManager, QWidget *parent) 
     : QDialog(parent)
     , ui(new Ui::ContactPanel)
-    , m_contactManager(contactManager)
     , m_contactListModel(new QStringListModel)
-    , m_profileEditor(new ProfileEditor(m_contactManager))
-    , m_addContactPanel(new AddContactPanel(contactManager))
-    , m_setAliasDialog(new SetAliasDialog(contactManager))
     , m_startChatDialog(new StartChatDialog)
     , m_invitationDialog(new InvitationDialog)
     , m_settingDialog(new SettingDialog)
@@ -57,12 +54,18 @@
     , m_menuAlias(new QAction("&Set Alias", this))
 {
   qRegisterMetaType<ndn::security::IdentityCertificate>("IdentityCertificate");
+  qRegisterMetaType<ChronosInvitation>("ChronosInvitation");
   
+  openDB();    
+
+  m_contactManager = contactManager;
+  m_profileEditor = new ProfileEditor(m_contactManager);
+  m_addContactPanel = new AddContactPanel(contactManager);
+  m_setAliasDialog = new SetAliasDialog(contactManager);
+ 
   ui->setupUi(this);
   refreshContactList();
 
-  openDB();    
-
   setKeychain();
   m_handler = Ptr<Wrapper>(new Wrapper(m_keychain));
    
@@ -101,10 +104,10 @@
   connect(m_startChatDialog, SIGNAL(chatroomConfirmed(const QString&, const QString&, bool)),
           this, SLOT(startChatroom(const QString&, const QString&, bool)));
 
-  connect(m_invitationDialog, SIGNAL(invitationAccepted(const ndn::Name&, const ndn::security::IdentityCertificate&, QString, QString)),
-          this, SLOT(acceptInvitation(const ndn::Name&, const ndn::security::IdentityCertificate&, QString, QString)));
-  connect(m_invitationDialog, SIGNAL(invitationRejected(const ndn::Name&)),
-          this, SLOT(rejectInvitation(const ndn::Name&)));
+  connect(m_invitationDialog, SIGNAL(invitationAccepted(const ChronosInvitation&, const ndn::security::IdentityCertificate&)),
+          this, SLOT(acceptInvitation(const ChronosInvitation&, const ndn::security::IdentityCertificate&)));
+  connect(m_invitationDialog, SIGNAL(invitationRejected(const ChronosInvitation&)),
+          this, SLOT(rejectInvitation(const ChronosInvitation&)));
 
   connect(m_settingDialog, SIGNAL(identitySet(const QString&)),
           this, SLOT(updateDefaultIdentity(const QString&)));
@@ -138,22 +141,22 @@
   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 );
 }
 
 void 
 ContactPanel::setKeychain()
 {
-  Ptr<security::OSXPrivatekeyStorage> privateStorage = Ptr<security::OSXPrivatekeyStorage>::Create();
-  Ptr<security::IdentityManager> identityManager = Ptr<security::IdentityManager>(new security::IdentityManager(Ptr<security::BasicIdentityStorage>::Create(), privateStorage));
+  Ptr<security::IdentityManager> identityManager = Ptr<security::IdentityManager>::Create();
   Ptr<security::CertificateCache> certificateCache = Ptr<security::CertificateCache>(new security::TTLCertificateCache());
-  Ptr<InvitationPolicyManager> policyManager = Ptr<InvitationPolicyManager>(new InvitationPolicyManager(10, certificateCache));
-  Ptr<security::EncryptionManager> encryptionManager = Ptr<security::EncryptionManager>(new security::BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
+  Ptr<PanelPolicyManager> policyManager = Ptr<PanelPolicyManager>(new PanelPolicyManager(10, certificateCache));
+  // 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++)
       policyManager->addTrustAnchor((*it)->getSelfEndorseCertificate());
 
-  m_keychain = Ptr<security::Keychain>(new security::Keychain(identityManager, policyManager, encryptionManager));
+  m_keychain = Ptr<security::Keychain>(new security::Keychain(identityManager, policyManager, NULL));
 }
 
 void
@@ -188,7 +191,11 @@
 
 void
 ContactPanel::onLocalPrefixTimeout(Ptr<Closure> closure, Ptr<Interest> interest)
-{ throw LnException("No local prefix is found!"); }
+{ 
+  string randomSuffix = getRandomString();
+  m_localPrefix = Name("/private/local"); 
+  m_localPrefix.append(randomSuffix);
+}
 
 void
 ContactPanel::onUnverified(Ptr<Data> data)
@@ -204,41 +211,35 @@
                                        int inviterIndex)
 {
   Ptr<security::IdentityCertificate> certificate = Ptr<security::IdentityCertificate>(new security::IdentityCertificate(*data));
-
-  const int end = interestName.size();
-
-  string signature = interestName.get(end-1).toBlob();
-  Blob signatureBlob(signature.c_str(), signature.size());
-  string signedName = interestName.getSubName(0, end - 1).toUri();
-  Blob signedBlob(signedName.c_str(), signedName.size());
-
-  if(security::PolicyManager::verifySignature(signedBlob, signatureBlob, certificate->getPublicKeyInfo()))
+  Ptr<ChronosInvitation> invitation = Ptr<ChronosInvitation>(new ChronosInvitation(interestName));
+  
+  if(security::PolicyManager::verifySignature(invitation->getSignedBlob(), invitation->getSignatureBits(), certificate->getPublicKeyInfo()))
     {
       Name keyName = certificate->getPublicKeyName();
       Name inviterNameSpace = keyName.getSubName(0, keyName.size() - 1);
-      popChatInvitation(interestName, inviterIndex, inviterNameSpace, certificate);
+      popChatInvitation(invitation, inviterIndex, inviterNameSpace, certificate);
     }
 }
 
 void
-ContactPanel::popChatInvitation(const Name& interestName,
+ContactPanel::popChatInvitation(Ptr<ChronosInvitation> invitation,
                                 int inviterIndex,
                                 const Name& inviterNameSpace,
                                 Ptr<security::IdentityCertificate> certificate)
 {
-  string chatroomTag("chatroom");
-  int i = 0;
-  for(; i < inviterIndex; i++)
-    if(interestName.get(i).toUri() == chatroomTag)
-      break;
-  if(i+1 >= inviterIndex)
+  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;
 
-  string chatroom = interestName.get(i+1).toUri();
-  string inviter = inviterNameSpace.toUri();
-  m_invitationDialog->setMsg(inviter, chatroom);
-  m_invitationDialog->setIdentityCertificate(certificate);
-  m_invitationDialog->setInterestName(interestName);
+  m_invitationDialog->setInvitation(alias, invitation, certificate);
   emit newInvitationReady();
 }
 
@@ -427,17 +428,18 @@
 void
 ContactPanel::openStartChatDialog()
 {
-  TimeInterval ti = time::NowUnixTimestamp();
-  ostringstream oss;
-  oss << ti.total_seconds();
+  // TimeInterval ti = time::NowUnixTimestamp();
+  // ostringstream oss;
+  // oss << ti.total_seconds();
 
   Name chatroom("/ndn/broadcast/chronos");
-  chatroom.append(string("chatroom-") + oss.str());
+  chatroom.append(string("chatroom-") + getRandomString());
 
   m_startChatDialog->setInvitee(m_currentSelectedContactNamespace, chatroom.toUri());
   m_startChatDialog->show();
 }
 
+// For inviter
 void
 ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
 {
@@ -445,48 +447,59 @@
   _LOG_DEBUG("invitee: " << invitee.toUtf8().constData());
   _LOG_DEBUG("introducer: " << std::boolalpha << isIntroducer);
 
-  Name chatroomName("/ndn/broadcast/chronos");
-  chatroomName.append(chatroom.toUtf8().constData());
+  Name chatroomName(chatroom.toUtf8().constData());
   
-  ChatDialog* chatDialog = new ChatDialog(chatroomName, m_localPrefix, m_defaultIdentity);
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
   
   //TODO: send invitation
   Name inviteeNamespace(invitee.toUtf8().constData());
   Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
 
-  chatDialog->sendInvitation(inviteeItem); 
+  chatDialog->sendInvitation(inviteeItem, isIntroducer); 
   
   chatDialog->show();
 }
 
+// For Invitee
 void
-ContactPanel::startChatroom2(const QString& chatroom, const QString& inviter)
+ContactPanel::startChatroom2(const ChronosInvitation& invitation, 
+                             const security::IdentityCertificate& identityCertificate)
 {
-  _LOG_DEBUG("room: " << chatroom.toUtf8().constData());
-  _LOG_DEBUG("inviter: " << inviter.toUtf8().constData());
+  _LOG_DEBUG("room: " << invitation.getChatroom().toUri());
+  _LOG_DEBUG("inviter: " << invitation.getInviterNameSpace().toUri());
+
+  Name chatroomName("/ndn/broadcast/chronos");
+  chatroomName.append(invitation.getChatroom());
+
+  ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity);
+  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();
 }
 
 void
-ContactPanel::acceptInvitation(const Name& interestName, 
-                               const security::IdentityCertificate& identityCertificate, 
-                               QString inviter, 
-                               QString chatroom)
+ContactPanel::acceptInvitation(const ChronosInvitation& invitation, 
+                               const security::IdentityCertificate& identityCertificate)
 {
   string prefix = m_localPrefix.toUri();
-  _LOG_DEBUG("interestName " << interestName);
-  _LOG_DEBUG("prefix " << prefix);
-  m_handler->publishDataByIdentity (interestName, prefix);
+
+  m_handler->publishDataByIdentity (invitation.getInterestName(), prefix);
   //TODO:: open chat dialog
-  _LOG_DEBUG("ok");
-  startChatroom2(chatroom, inviter);
+  _LOG_DEBUG("TO open chat dialog");
+  startChatroom2(invitation, identityCertificate);
 }
 
 void
-ContactPanel::rejectInvitation(const ndn::Name& interestName)
+ContactPanel::rejectInvitation(const ChronosInvitation& invitation)
 {
   string empty;
-  m_handler->publishDataByIdentity (interestName, empty);
+  m_handler->publishDataByIdentity (invitation.getInterestName(), empty);
 }