Adding part of exception handling
diff --git a/src/addcontactpanel.cpp b/src/addcontactpanel.cpp
index bea8d7f..c5c80e4 100644
--- a/src/addcontactpanel.cpp
+++ b/src/addcontactpanel.cpp
@@ -12,7 +12,7 @@
 #include "ui_addcontactpanel.h"
 
 #ifndef Q_MOC_RUN
-#include <cryptopp/base64.h>
+// #include <cryptopp/base64.h>
 // #include <ndn.cxx/helpers/der/der.h>
 #include <ndn.cxx/helpers/der/visitor/simple-visitor.h>
 #include "logging.h"
@@ -125,7 +125,13 @@
 AddContactPanel::onAddClicked()
 {
   ContactItem contactItem(*m_currentEndorseCertificate);
-  m_contactManager->getContactStorage()->addContact(contactItem);
+  try{
+    m_contactManager->getContactStorage()->addContact(contactItem);
+  }catch(exception& e){
+    m_warningDialog->setMsg(e.what());
+    m_warningDialog->show();
+    return;
+  }
   emit newContactAdded();
   this->close();
 }
@@ -216,11 +222,9 @@
           Ptr<ContactItem> contact = m_contactManager->getContact(signerName);
           if(contact == NULL)
             continue;
-          _LOG_DEBUG("get contact: " << signerName.toUri());
 
           if(!contact->isIntroducer() || !contact->canBeTrustedFor(m_currentEndorseCertificate->getProfileData()->getIdentityName()))
             continue;
-          _LOG_DEBUG("contact can be trusted");
           
           if(!security::PolicyManager::verifySignature(*data, contact->getSelfEndorseCertificate().getPublicKeyInfo()))
             continue;
@@ -229,15 +233,10 @@
           if(profileBlob != tmpProfileBlob)
             continue;
 
-           _LOG_DEBUG("Profile equal");
-
           const vector<string>& endorseList = endorseCert->getEndorseList();
           vector<string>::const_iterator it = endorseList.begin();
           for(; it != endorseList.end(); it++)
-            {
-              _LOG_DEBUG("Entry: " << *it);
-              endorseCount[*it] += 1;
-            }
+            endorseCount[*it] += 1;
         }
     }
 
diff --git a/src/chatdialog.cpp b/src/chatdialog.cpp
index e0304a6..1e30f87 100644
--- a/src/chatdialog.cpp
+++ b/src/chatdialog.cpp
@@ -249,7 +249,7 @@
                                             dskCertificate.getPublicKeyInfo(),
                                             (isIntroducer ? SyncIntroCertificate::INTRODUCER : SyncIntroCertificate::PRODUCER));
   ndn::Name certName = m_identityManager->getDefaultCertificateNameByIdentity(m_defaultIdentity);
-  _LOG_DEBUG("publishIntroCert: " << syncIntroCertificate.getName());
+  _LOG_DEBUG("Publish Intro Certificate: " << syncIntroCertificate.getName());
   m_identityManager->signByCertificate(syncIntroCertificate, certName);
   m_handler->putToNdnd(*syncIntroCertificate.encodeToWire());
 }
@@ -257,14 +257,13 @@
 void
 ChatDialog::invitationRejected(const ndn::Name& identity)
 {
-  _LOG_DEBUG(" " << identity.toUri() << " rejected your invitation!");
-  //TODO:
+  _LOG_DEBUG(" " << identity.toUri() << " Rejected your invitation!");
 }
 
 void
 ChatDialog::invitationAccepted(const ndn::Name& identity, ndn::Ptr<ndn::Data> data, const string& inviteePrefix, bool isIntroducer)
 {
-  _LOG_DEBUG(" " << identity.toUri() << " accepted your invitation!");
+  _LOG_DEBUG(" " << identity.toUri() << " Accepted your invitation!");
   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);
@@ -1018,7 +1017,6 @@
 ChatDialog::closeEvent(QCloseEvent *e)
 {
   hide();
-  _LOG_DEBUG("about to leave 1");
   emit closeChatDialog(m_chatroomPrefix);
 }
 
diff --git a/src/contact-manager.cpp b/src/contact-manager.cpp
index 71b9ea2..2b834c6 100644
--- a/src/contact-manager.cpp
+++ b/src/contact-manager.cpp
@@ -13,13 +13,10 @@
 #ifndef Q_MOC_RUN
 #include <ndn.cxx/wrapper/wrapper.h>
 #include <ndn.cxx/security/keychain.h>
-#include <ndn.cxx/security/identity/basic-identity-storage.h>
-#include <ndn.cxx/security/identity/osx-privatekey-storage.h>
 #include <ndn.cxx/security/policy/simple-policy-manager.h>
 #include <ndn.cxx/security/policy/identity-policy-rule.h>
-#include <ndn.cxx/security/cache/ttl-certificate-cache.h>
-#include <ndn.cxx/security/encryption/basic-encryption-manager.h>
 #include <ndn.cxx/helpers/der/der.h>
+#include <cryptopp/base64.h>
 #include <fstream>
 #include "logging.h"
 #endif
@@ -48,12 +45,10 @@
 void
 ContactManager::setKeychain()
 {
-  Ptr<OSXPrivatekeyStorage> privateStorage = Ptr<OSXPrivatekeyStorage>::Create();
-  Ptr<IdentityManager> identityManager = Ptr<IdentityManager>(new IdentityManager(Ptr<BasicIdentityStorage>::Create(), privateStorage));
-  Ptr<TTLCertificateCache> certificateCache = Ptr<TTLCertificateCache>(new TTLCertificateCache());
-  Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>(new SimplePolicyManager(10, certificateCache));
-  Ptr<EncryptionManager> encryptionManager = Ptr<EncryptionManager>(new BasicEncryptionManager(privateStorage, "/tmp/encryption.db"));
-  Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, encryptionManager));
+  Ptr<IdentityManager> identityManager = Ptr<IdentityManager>::Create();
+  Ptr<SimplePolicyManager> policyManager = Ptr<SimplePolicyManager>::Create();
+
+  Ptr<Keychain> keychain = Ptr<Keychain>(new Keychain(identityManager, policyManager, NULL));
 
   policyManager->addVerificationPolicyRule(Ptr<IdentityPolicyRule>(new IdentityPolicyRule("^([^<DNS>]*)<DNS><ENDORSED>",
                                                                                           "^([^<KEY>]*)<KEY>(<>*)[<ksk-.*><dsk-.*>]<ID-CERT>$",
@@ -75,20 +70,65 @@
                                                                                      "^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>",
                                                                                      "==", "\\1", "\\1\\2", true)));
 
-  ifstream is ("trust-anchor.data", ios::binary);
-  is.seekg (0, ios::end);
-  ifstream::pos_type size = is.tellg();
-  char * memblock = new char [size];    
-  is.seekg (0, ios::beg);
-  is.read (memblock, size);
-  is.close();
+  const string TrustAnchor("BIICqgOyEIWlKzDI2xX2hdq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");
 
-  Ptr<Blob> readBlob = Ptr<Blob>(new Blob(memblock, size));
-  Ptr<Data> readData = Data::decodeFromWire (readBlob);
-  Ptr<IdentityCertificate> anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*readData));   
+  string decoded;
+  CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(TrustAnchor.c_str()), 
+                            TrustAnchor.size(), 
+                            true,
+                            new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
+  Ptr<Blob> blob = Ptr<Blob>(new Blob(decoded.c_str(), decoded.size()));
+  Ptr<Data> data = Data::decodeFromWire(blob);
+  Ptr<IdentityCertificate>anchor = Ptr<IdentityCertificate>(new IdentityCertificate(*data));
   policyManager->addTrustAnchor(anchor);  
-  
-  delete memblock;
+
+#ifdef _DEBUG
+
+  const string FakeAnchor("BIICqgOyEIVAaoHnQZIx5osAuY2fKte4HBSrxyam7MY6/kp+w47O1bGdd2KjeZKV\
+zZzQd3EQorDC3KUPbB6ql30jYfspvo4OPSlIuDrkyROaoZ+MSKyzQYpB6CZcTjBa\
+qcWYFOfwUlcWvkbd00X4bkc5PkcWpVdRrx+NCTiq9EXes//hOHpEJHMNsJUi45O+\
+6M4OE6/sNEqs/ryHn2w1vCqwPpG8xzcd0prQUdCH2MGE77F+H0XFDuWp8mrT37Uw\
+DUy7Ltm+7nDTHSQy2J3Zk4Q+0tjxCzSw4owEpwOHr+afdkuE3v9aB2NRQBBDCEmL\
+Ykz4sYX3XE8MVFqRn1HHWCkszjDg+F0UAADy+p1uZG4A+p1LRVkA+vVrc2stMTM4\
+MjkzNDE5OAD6vUlELUNFUlQA+s39/////95rc7MAAAGiA+IChaK1eVvzlkg6BJAw\
+qiOpxRoezQ0hAHOBbPRLeBllxMN7AAK6tQUm3mtztQAB4gHq8vqdbmRuAPqdS0VZ\
+APr1a3NrLTEzODI5MzQxOTgA+r1JRC1DRVJUAAAAAAABmhblMIIBaDAiGA8yMDEz\
+MTAyODAwMDAwMFoYDzIwMzMxMDI4MDAwMDAwWjAcMBoGA1UEKRMTL25kbi9rc2st\
+MTM4MjkzNDE5ODCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2htIFF\
+/PH+SJsGOA6jhpFT74xfLJlgZNJOnKzl27HI2gupE0mainWj/HqVzdGxD6jOOReI\
+sul+eQyEyBYq4e35pLmdJGlux/+UPQ51DD8jg04GrUPewV7+iGm6usp/7xEGHbah\
+H2Grv/bsGrt6aRA8cKmdIc+rehxZCVFtiwSEHTnOWzn3lfZR5xnjF9aGX+uGo1hA\
+gMwu1ECxg4H3O4z1tbTzji5+WH0RDsPRlgzQX6wAQH8btlQyoFJfljEA3QaOtDaB\
+OcfegIlClzutmgJnK9i5ZLz2Mjvx49dlCWAVKg65vOXMLC/33jD9F+V8urwsBlOb\
+F7Wh5ayeo8NBKDsCAwEAAQAA");
+
+  string decoded2;
+  CryptoPP::StringSource ss2(reinterpret_cast<const unsigned char *>(FakeAnchor.c_str()), 
+                            FakeAnchor.size(), 
+                            true,
+                            new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded2)));
+  Ptr<Blob> blob2 = Ptr<Blob>(new Blob(decoded2.c_str(), decoded2.size()));
+  Ptr<Data> data2 = Data::decodeFromWire(blob2);
+  Ptr<IdentityCertificate>anchor2 = Ptr<IdentityCertificate>(new IdentityCertificate(*data2));
+  policyManager->addTrustAnchor(anchor2);  
+
+#endif
 
   m_keychain = keychain;
 }
@@ -210,7 +250,7 @@
 void
 ContactManager::updateProfileData(const Name& identity)
 {
-  _LOG_DEBUG("updateProfileData: " << identity.toUri());
+  // _LOG_DEBUG("updateProfileData: " << identity.toUri());
   // Get current profile;
   Ptr<Profile> newProfile = m_contactStorage->getSelfProfile(identity);
   if(NULL == newProfile)
@@ -233,7 +273,7 @@
       // _LOG_DEBUG("Signing DONE!");
       if(NULL == newEndorseCertificate)
         return;
-      _LOG_DEBUG("About to update");
+      // _LOG_DEBUG("About to update");
       m_contactStorage->updateSelfEndorseCertificate(newEndorseCertificate, identity);
 
       publishSelfEndorseCertificateInDNS(newEndorseCertificate);
@@ -244,7 +284,7 @@
       // _LOG_DEBUG("Signing DONE!");
       if(NULL == newEndorseCertificate)
         return;
-      _LOG_DEBUG("About to Insert");
+      // _LOG_DEBUG("About to Insert");
       m_contactStorage->addSelfEndorseCertificate(newEndorseCertificate, identity);
 
       publishSelfEndorseCertificateInDNS(newEndorseCertificate);
@@ -278,6 +318,8 @@
 ContactManager::generateEndorseCertificate(const Name& identity, const Name& signerIdentity)
 {
   Ptr<ContactItem> contact = getContact(identity);
+  if(contact == NULL)
+    return NULL;
 
   Ptr<IdentityManager> identityManager = m_keychain->getIdentityManager();
   Name signerKeyName = identityManager->getDefaultKeyNameForIdentity(signerIdentity);
@@ -312,6 +354,9 @@
   identityManager->signByCertificate(*profileData, certificateName);
 
   Ptr<security::IdentityCertificate> signingCert = identityManager->getCertificate(certificateName);
+  if(NULL == signingCert)
+    return NULL;
+
   Name signingKeyName = security::IdentityCertificate::certificateNameToPublicKeyName(signingCert->getName(), true);
 
   Ptr<security::IdentityCertificate> kskCert;
@@ -319,20 +364,21 @@
     {
       Ptr<const signature::Sha256WithRsa> dskCertSig = DynamicCast<const signature::Sha256WithRsa>(signingCert->getSignature());
       // HACK! KSK certificate should be retrieved from network.
-      _LOG_DEBUG("keyLocator: " << dskCertSig->getKeyLocator().getKeyName());
       Name keyName = security::IdentityCertificate::certificateNameToPublicKeyName(dskCertSig->getKeyLocator().getKeyName());
-      _LOG_DEBUG("keyName: " << keyName.toUri());
+
       Name kskCertName = identityManager->getPublicStorage()->getDefaultCertificateNameForKey(keyName);
-      _LOG_DEBUG("ksk cert name: " << kskCertName);
+
       kskCert = identityManager->getCertificate(kskCertName);
 
     }
   else
     {
       kskCert = signingCert;
-      _LOG_DEBUG("ksk cert name: " << kskCert->getName().toUri());
     }
 
+  if(NULL == kskCert)
+    return NULL;
+
   vector<string> endorseList;
   Profile::const_iterator it = profile.begin();
   for(; it != profile.end(); it++)
@@ -359,18 +405,9 @@
   const security::Publickey& ksk = selfEndorseCertificate->getPublicKeyInfo();
 
   if(security::PolicyManager::verifySignature(*plainData, ksk))
-    {
-      // Profile profile = selfEndorseCertificate->getProfileData()->getProfile();
-      // Profile::const_iterator it = profile.getEntries().begin();
-      // it++;
-      // _LOG_DEBUG("Entry Size: " << it->first);
-
-      emit contactFetched (*selfEndorseCertificate); 
-    }
+    emit contactFetched (*selfEndorseCertificate); 
   else
-    {
-      emit contactFetchFailed (identity);
-    }
+    emit contactFetchFailed (identity);
 }
 
 void
@@ -396,11 +433,6 @@
   data->setName(dnsName);
   Ptr<Blob> blob = selfEndorseCertificate->encodeToWire();
 
-  // string encoded;
-  // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(blob->buf()), blob->size(), true,
-  //       		    new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), false));
-
-  // Content content(encoded.c_str(), encoded.size());
   Content content(blob->buf(), blob->size());
   data->setContent(content);
 
diff --git a/src/contact-storage.cpp b/src/contact-storage.cpp
index 903f210..b3ee977 100644
--- a/src/contact-storage.cpp
+++ b/src/contact-storage.cpp
@@ -288,7 +288,6 @@
 Ptr<Profile>
 ContactStorage::getSelfProfile(const Name& identity)
 {
-  _LOG_DEBUG("getSelfProfile " << identity.toUri());
   sqlite3_stmt *stmt;
   Ptr<Profile> profile = Ptr<Profile>(new Profile(identity));
   
@@ -297,13 +296,14 @@
 
   while(sqlite3_step (stmt) == SQLITE_ROW)
     {
-      _LOG_DEBUG("entry");
       string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
       Blob profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
 
       profile->setProfileEntry(profileType, profileValue );
     }
 
+  sqlite3_finalize(stmt);
+
   return profile;
 }
   
@@ -329,7 +329,6 @@
   sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
 
   int res = sqlite3_step (stmt);
-  // _LOG_DEBUG("res " << res);
   sqlite3_finalize (stmt);
 
   Ptr<ProfileData> profileData = contact.getSelfEndorseCertificate().getProfileData();
@@ -509,6 +508,8 @@
       profile->setProfileEntry(profileType, profileValue);
     }
 
+  sqlite3_finalize(stmt);
+
   return profile;
 }
 
@@ -653,8 +654,6 @@
       Ptr<Blob> blob = endorseCertificate.encodeToWire();
       sqlite3_bind_text(stmt, 4, blob->buf(), blob->size(), SQLITE_TRANSIENT);
       int res = sqlite3_step (stmt);
-      // if(res != SQLITE_OK)
-      //   _LOG_DEBUG("Insert CollectEndorse Failure: " << getCertName.toUri());
       sqlite3_finalize (stmt); 
       return;
     }
@@ -667,8 +666,6 @@
       sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
       sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
       int res = sqlite3_step (stmt);
-      // if(res != SQLITE_OK)
-      //   _LOG_DEBUG("Insert CollectEndorse Failure: " << getCertName.toUri());
       sqlite3_finalize (stmt); 
       return;
     }
@@ -689,5 +686,7 @@
       collectEndorseList->push_back(blob);
     }
 
+  sqlite3_finalize (stmt);
+
   return collectEndorseList;
 }
diff --git a/src/contactpanel.cpp b/src/contactpanel.cpp
index 8775ae5..de35f51 100644
--- a/src/contactpanel.cpp
+++ b/src/contactpanel.cpp
@@ -558,6 +558,10 @@
 ContactPanel::startChatroom(const QString& chatroom, const QString& invitee, bool isIntroducer)
 {
   Name chatroomName(chatroom.toUtf8().constData());
+
+  Name inviteeNamespace(invitee.toStdString());
+  Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
+
   ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName);
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
 
@@ -565,9 +569,6 @@
           this, SLOT(removeChatDialog(const ndn::Name&)));
 
   // send invitation
-  Name inviteeNamespace(invitee.toUtf8().constData());
-  Ptr<ContactItem> inviteeItem = m_contactManager->getContact(inviteeNamespace);
-
   chatDialog->sendInvitation(inviteeItem, isIntroducer); 
   
   chatDialog->show();
@@ -578,8 +579,11 @@
 ContactPanel::startChatroom2(const ChronosInvitation& invitation, 
                              const security::IdentityCertificate& identityCertificate)
 {
+  Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
+
   Name chatroomName("/ndn/broadcast/chronos");
   chatroomName.append(invitation.getChatroom());
+
   ChatDialog* chatDialog = new ChatDialog(m_contactManager, chatroomName, m_localPrefix, m_defaultIdentity, m_nickName, true);
 
   connect(chatDialog, SIGNAL(closeChatDialog(const ndn::Name&)),
@@ -588,7 +592,6 @@
   chatDialog->addChatDataRule(invitation.getInviterPrefix(), identityCertificate, true);
   chatDialog->publishIntroCert(identityCertificate, true);
 
-  Ptr<ContactItem> inviterItem = m_contactManager->getContact(invitation.getInviterNameSpace());
   chatDialog->addTrustAnchor(inviterItem->getSelfEndorseCertificate());
   
   m_chatDialogs.insert(pair <Name, ChatDialog*> (chatroomName, chatDialog));
diff --git a/src/main.cpp b/src/main.cpp
index 4e58e77..44a67ee 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -53,10 +53,16 @@
 
   // app.setWindowIcon(QIcon(":/demo.icns"));
 
-  Ptr<ContactStorage> contactStorage = Ptr<ContactStorage>::Create();
+  Ptr<ContactStorage> contactStorage = NULL;
+  try{
+    contactStorage = Ptr<ContactStorage>::Create();
+  }catch(std::exception& e){
+    std::cerr << e.what() << std::endl;
+    exit(1);
+  }
   Ptr<DnsStorage> dnsStorage = Ptr<DnsStorage>::Create();
   Ptr<ContactManager> contactManager = Ptr<ContactManager>(new ContactManager(contactStorage, dnsStorage));
-
+  
   ContactPanel contactPanel(contactManager);
 
   contactPanel.show ();