bug+codestyle: Fix compile bugs and adjust code style

Change-Id: I008bb538441c099fa25b8b967fbf23ffce13a220
diff --git a/src/controller.cpp b/src/controller.cpp
index 7517a4e..915e3d8 100644
--- a/src/controller.cpp
+++ b/src/controller.cpp
@@ -12,34 +12,34 @@
 #include <QMessageBox>
 #include <QDir>
 #include <QTimer>
-#include "controller.h"
+#include "controller.hpp"
 
 #ifndef Q_MOC_RUN
 #include <boost/filesystem.hpp>
 #include <boost/lexical_cast.hpp>
 #include <ndn-cxx/util/random.hpp>
-#include <cryptopp/sha.h>
-#include <cryptopp/hex.h>
-#include <cryptopp/files.h>
-#include <cryptopp/filters.h>
+#include "cryptopp.hpp"
 #include "config.pb.h"
 #include "endorse-info.pb.h"
 #include "logging.h"
 #endif
 
-INIT_LOGGER("chronos.Controller");
-
-using namespace ndn;
+// INIT_LOGGER("chronos.Controller");
 
 Q_DECLARE_METATYPE(ndn::Name)
 Q_DECLARE_METATYPE(ndn::IdentityCertificate)
-Q_DECLARE_METATYPE(chronos::EndorseInfo)
+Q_DECLARE_METATYPE(Chronos::EndorseInfo)
 Q_DECLARE_METATYPE(ndn::Interest)
 Q_DECLARE_METATYPE(size_t)
 
 namespace chronos {
 
-using ndn::shared_ptr;
+using std::string;
+
+using ndn::Face;
+using ndn::IdentityCertificate;
+using ndn::OnInterestValidated;
+using ndn::OnInterestValidationFailed;
 
 static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E};
 
@@ -60,7 +60,7 @@
 {
   qRegisterMetaType<ndn::Name>("ndn.Name");
   qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate");
-  qRegisterMetaType<chronos::EndorseInfo>("chronos.EndorseInfo");
+  qRegisterMetaType<Chronos::EndorseInfo>("Chronos.EndorseInfo");
   qRegisterMetaType<ndn::Interest>("ndn.Interest");
   qRegisterMetaType<size_t>("size_t");
 
@@ -110,8 +110,8 @@
           &m_contactManager, SLOT(onFetchContactInfo(const QString&)));
   connect(m_addContactPanel, SIGNAL(addContact(const QString&)),
           &m_contactManager, SLOT(onAddFetchedContact(const QString&)));
-  connect(&m_contactManager, SIGNAL(contactEndorseInfoReady(const chronos::EndorseInfo&)),
-          m_addContactPanel, SLOT(onContactEndorseInfoReady(const chronos::EndorseInfo&)));
+  connect(&m_contactManager, SIGNAL(contactEndorseInfoReady(const Chronos::EndorseInfo&)),
+          m_addContactPanel, SLOT(onContactEndorseInfoReady(const Chronos::EndorseInfo&)));
 
 
   // Connection to BrowseContactDialog
@@ -151,8 +151,10 @@
           m_contactPanel, SLOT(onContactAliasListReady(const QStringList&)));
   connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)),
           m_contactPanel, SLOT(onContactIdListReady(const QStringList&)));
-  connect(&m_contactManager, SIGNAL(contactInfoReady(const QString&, const QString&, const QString&, bool)),
-          m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&, const QString&, bool)));
+  connect(&m_contactManager, SIGNAL(contactInfoReady(const QString&, const QString&,
+                                                     const QString&, bool)),
+          m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&,
+                                                  const QString&, bool)));
 
   initialize();
 
@@ -170,10 +172,10 @@
 
 
 // private methods
-std::string
+string
 Controller::getDBName()
 {
-  std::string dbName("chronos-");
+  string dbName("chronos-");
 
   std::stringstream ss;
   {
@@ -193,11 +195,14 @@
 {
   m_db = QSqlDatabase::addDatabase("QSQLITE");
   QString path = (QDir::home().path());
-  path.append(QDir::separator()).append(".chronos").append(QDir::separator()).append(getDBName().c_str());
+  path.append(QDir::separator())
+    .append(".chronos")
+    .append(QDir::separator())
+    .append(getDBName().c_str());
   m_db.setDatabaseName(path);
   bool ok = m_db.open();
 
-  _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
+  // _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
 }
 
 void
@@ -217,22 +222,23 @@
 void
 Controller::setInvitationListener()
 {
-  if(m_invitationListenerId != 0)
+  if (m_invitationListenerId != 0)
     m_face->unsetInterestFilter(m_invitationListenerId);
 
   Name invitationPrefix;
   Name routingPrefix = getInvitationRoutingPrefix();
   size_t offset = 0;
-  if(!routingPrefix.isPrefixOf(m_identity))
-    {
-      invitationPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2);
-      offset = routingPrefix.size() + 1;
-    }
+  if (!routingPrefix.isPrefixOf(m_identity)) {
+    invitationPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2);
+    offset = routingPrefix.size() + 1;
+  }
   invitationPrefix.append(m_identity).append("CHRONOCHAT-INVITATION");
 
   m_invitationListenerId = m_face->setInterestFilter(invitationPrefix,
-                                                     bind(&Controller::onInvitationInterestWrapper, this, _1, _2, offset),
-                                                     bind(&Controller::onInvitationRegisterFailed, this, _1, _2));
+                                                     bind(&Controller::onInvitationInterestWrapper,
+                                                          this, _1, _2, offset),
+                                                     bind(&Controller::onInvitationRegisterFailed,
+                                                          this, _1, _2));
 }
 
 void
@@ -245,24 +251,22 @@
 
   std::ifstream is((chronosDir / "config").c_str ());
   ChronoChat::Conf conf;
-  if(conf.ParseFromIstream(&is))
-    {
-      m_identity.clear();
-      m_identity.append(conf.identity());
-      if(conf.has_nick())
-        m_nick = conf.nick();
-      else
-        m_nick = m_identity.get(-1).toUri();
-    }
-  else
-    {
-      m_identity.clear();
-      // TODO: change below to system default;
-      m_identity.append("chronochat-tmp-identity")
-        .append(getRandomString());
-
+  if (conf.ParseFromIstream(&is)) {
+    m_identity.clear();
+    m_identity.append(conf.identity());
+    if (conf.has_nick())
+      m_nick = conf.nick();
+    else
       m_nick = m_identity.get(-1).toUri();
-    }
+  }
+  else {
+    m_identity.clear();
+    // TODO: change below to system default;
+    m_identity.append("chronochat-tmp-identity")
+      .append(getRandomString());
+
+    m_nick = m_identity.get(-1).toUri();
+  }
 }
 
 void
@@ -276,7 +280,7 @@
   std::ofstream os((chronosDir / "config").c_str ());
   ChronoChat::Conf conf;
   conf.set_identity(m_identity.toUri());
-  if(!m_nick.empty())
+  if (!m_nick.empty())
     conf.set_nick(m_nick);
   conf.SerializeToOstream(&os);
 
@@ -358,12 +362,11 @@
   {
     ChatActionList::const_iterator it = m_chatActionList.begin();
     ChatActionList::const_iterator end = m_chatActionList.end();
-    if(it != end)
-      {
-        for(; it != end; it++)
-          menu->addAction(it->second);
-        menu->addSeparator();
-      }
+    if (it != end) {
+      for (; it != end; it++)
+        menu->addAction(it->second);
+      menu->addSeparator();
+    }
   }
   menu->addAction(m_updateLocalPrefixAction);
   menu->addSeparator();
@@ -372,15 +375,11 @@
   {
     ChatActionList::const_iterator it = m_closeActionList.begin();
     ChatActionList::const_iterator end = m_closeActionList.end();
-    if(it == end)
-      {
-        closeMenu->setEnabled(false);
-      }
+    if (it == end)
+      closeMenu->setEnabled(false);
     else
-      {
-        for(; it != end; it++)
-          closeMenu->addAction(it->second);
-      }
+      for (; it != end; it++)
+        closeMenu->addAction(it->second);
   }
   menu->addSeparator();
   menu->addAction(m_quitAction);
@@ -399,7 +398,7 @@
     .trimmed();
 
   Name localPrefix(localPrefixStr.toStdString());
-  if(m_localPrefix.empty() || m_localPrefix != localPrefix)
+  if (m_localPrefix.empty() || m_localPrefix != localPrefix)
     emit localPrefixUpdated(localPrefixStr);
 }
 
@@ -409,42 +408,47 @@
   QString localPrefixStr("/private/local");
 
   Name localPrefix(localPrefixStr.toStdString());
-  if(m_localPrefix.empty() || m_localPrefix != localPrefix)
+  if (m_localPrefix.empty() || m_localPrefix != localPrefix)
     emit localPrefixUpdated(localPrefixStr);
 }
 
 void
-Controller::onInvitationInterestWrapper(const Name& prefix, const Interest& interest, size_t routingPrefixOffset)
+Controller::onInvitationInterestWrapper(const Name& prefix,
+                                        const Interest& interest,
+                                        size_t routingPrefixOffset)
 {
   emit invitationInterest(prefix, interest, routingPrefixOffset);
 }
 
 void
-Controller::onInvitationRegisterFailed(const Name& prefix, const std::string& failInfo)
+Controller::onInvitationRegisterFailed(const Name& prefix, const string& failInfo)
 {
-  _LOG_DEBUG("Controller::onInvitationRegisterFailed: " << failInfo);
+  // _LOG_DEBUG("Controller::onInvitationRegisterFailed: " << failInfo);
 }
 
 void
 Controller::onInvitationValidated(const shared_ptr<const Interest>& interest)
 {
   Invitation invitation(interest->getName());
-  std::string alias = invitation.getInviterCertificate().getPublicKeyName().getPrefix(-1).toUri(); // Should be obtained via a method of ContactManager.
+  // Should be obtained via a method of ContactManager.
+  string alias = invitation.getInviterCertificate().getPublicKeyName().getPrefix(-1).toUri();
 
   m_invitationDialog->setInvitation(alias, invitation.getChatroom(), interest->getName());
   m_invitationDialog->show();
 }
 
 void
-Controller::onInvitationValidationFailed(const shared_ptr<const Interest>& interest, std::string failureInfo)
+Controller::onInvitationValidationFailed(const shared_ptr<const Interest>& interest,
+                                         string failureInfo)
 {
-  _LOG_DEBUG("Invitation: " << interest->getName() << " cannot not be validated due to: " << failureInfo);
+  // _LOG_DEBUG("Invitation: " << interest->getName() <<
+  //            " cannot not be validated due to: " << failureInfo);
 }
 
-std::string
+string
 Controller::getRandomString()
 {
-  uint32_t r = random::generateWord32();
+  uint32_t r = ndn::random::generateWord32();
   std::stringstream ss;
   {
     using namespace CryptoPP;
@@ -452,10 +456,10 @@
                  new HexEncoder(new FileSink(ss), false));
 
   }
-  // for(int i = 0; i < 8; i++)
+  // for (int i = 0; i < 8; i++)
   //   {
   //     uint32_t t = r & mask;
-  //     if(t < 10)
+  //     if (t < 10)
   //       ss << static_cast<char>(t + 0x30);
   //     else
   //       ss << static_cast<char>(t + 0x57);
@@ -501,11 +505,10 @@
 {
   Name identityName(identity.toStdString());
 
-  while(!m_chatDialogList.empty())
-    {
-      ChatDialogList::const_iterator it = m_chatDialogList.begin();
-      onRemoveChatDialog(QString::fromStdString(it->first));
-    }
+  while (!m_chatDialogList.empty()) {
+    ChatDialogList::const_iterator it = m_chatDialogList.begin();
+    onRemoveChatDialog(QString::fromStdString(it->first));
+  }
 
   m_identity = identityName;
   m_keyChain.createIdentity(m_identity);
@@ -538,10 +541,7 @@
   m_contactManager.getContactList(contactList);
   m_validator.cleanTrustAnchor();
 
-  ContactList::const_iterator it  = contactList.begin();
-  ContactList::const_iterator end = contactList.end();
-
-  for(; it != end; it++)
+  for (ContactList::const_iterator it  = contactList.begin(); it != contactList.end(); it++)
     m_validator.addTrustAnchor((*it)->getPublicKeyName(), (*it)->getPublicKey());
 
 }
@@ -561,7 +561,7 @@
 void
 Controller::onStartChatAction()
 {
-  std::string chatroom = "chatroom-" + getRandomString();
+  string chatroom = "chatroom-" + getRandomString();
 
   m_startChatDialog->setChatroom(chatroom);
   m_startChatDialog->show();
@@ -629,20 +629,19 @@
 
   ChatDialogList::iterator it = m_chatDialogList.begin();
   ChatDialogList::iterator end = m_chatDialogList.end();
-  for(; it != end; it++)
+  for (; it != end; it++)
     it->second->hide();
 }
 
 void
 Controller::onQuitAction()
 {
-  while(!m_chatDialogList.empty())
-    {
-      ChatDialogList::const_iterator it = m_chatDialogList.begin();
-      onRemoveChatDialog(QString::fromStdString(it->first));
-    }
+  while (!m_chatDialogList.empty()) {
+    ChatDialogList::const_iterator it = m_chatDialogList.begin();
+    onRemoveChatDialog(QString::fromStdString(it->first));
+  }
 
-  if(m_invitationListenerId != 0)
+  if (m_invitationListenerId != 0)
     m_face->unsetInterestFilter(m_invitationListenerId);
 
   delete m_settingDialog;
@@ -652,7 +651,7 @@
   delete m_browseContactDialog;
   delete m_addContactPanel;
 
-  m_face->ioService()->stop();
+  m_face->getIoService().stop();
 
   QApplication::quit();
 }
@@ -667,13 +666,12 @@
     .append(chatroomName.toStdString());
 
   // check if the chatroom exists
-  if(m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end())
-    {
-      QMessageBox::information(this, tr("ChronoChat"),
-                               tr("You are creating an existing chatroom."
-                                  "You can check it in the context memu."));
-      return;
-    }
+  if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) {
+    QMessageBox::information(this, tr("ChronoChat"),
+                             tr("You are creating an existing chatroom."
+                                "You can check it in the context memu."));
+    return;
+  }
 
   // TODO: We should create a chatroom specific key/cert (which should be created in the first half of this method, but let's use the default one for now.
   shared_ptr<IdentityCertificate> idCert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
@@ -690,67 +688,62 @@
   shared_ptr<IdentityCertificate> chatroomCert;
 
   // generate reply;
-  if(accepted)
-    {
-      Name responseName = invitationName;
-      responseName.append(m_localPrefix.wireEncode());
+  if (accepted) {
+    Name responseName = invitationName;
+    responseName.append(m_localPrefix.wireEncode());
 
-      response.setName(responseName);
+    response.setName(responseName);
 
-      // We should create a particular certificate for this chatroom, but let's use default one for now.
-      chatroomCert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
+    // We should create a particular certificate for this chatroom, but let's use default one for now.
+    chatroomCert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
 
-      response.setContent(chatroomCert->wireEncode());
-      response.setFreshnessPeriod(time::milliseconds(1000));
-    }
-  else
-    {
-      response.setName(invitationName);
-      response.setFreshnessPeriod(time::milliseconds(1000));
-    }
+    response.setContent(chatroomCert->wireEncode());
+    response.setFreshnessPeriod(time::milliseconds(1000));
+  }
+  else {
+    response.setName(invitationName);
+    response.setFreshnessPeriod(time::milliseconds(1000));
+  }
+
   m_keyChain.signByIdentity(response, m_identity);
 
   // Check if we need a wrapper
   Name invitationRoutingPrefix = getInvitationRoutingPrefix();
-  if(invitationRoutingPrefix.isPrefixOf(m_identity))
-    {
-      m_face->put(response);
-    }
-  else
-    {
-      Name wrappedName;
-      wrappedName.append(invitationRoutingPrefix)
-        .append(ROUTING_PREFIX_SEPARATOR, 2)
-        .append(response.getName());
+  if (invitationRoutingPrefix.isPrefixOf(m_identity))
+    m_face->put(response);
+  else {
+    Name wrappedName;
+    wrappedName.append(invitationRoutingPrefix)
+      .append(ROUTING_PREFIX_SEPARATOR, 2)
+      .append(response.getName());
 
-      _LOG_DEBUG("onInvitationResponded: prepare reply " << wrappedName);
+    // _LOG_DEBUG("onInvitationResponded: prepare reply " << wrappedName);
 
-      Data wrappedData(wrappedName);
-      wrappedData.setContent(response.wireEncode());
-      wrappedData.setFreshnessPeriod(time::milliseconds(1000));
+    Data wrappedData(wrappedName);
+    wrappedData.setContent(response.wireEncode());
+    wrappedData.setFreshnessPeriod(time::milliseconds(1000));
 
-      m_keyChain.signByIdentity(wrappedData, m_identity);
-      m_face->put(wrappedData);
-    }
+    m_keyChain.signByIdentity(wrappedData, m_identity);
+    m_face->put(wrappedData);
+  }
 
   // create chatroom
-  if(accepted)
-    {
-      Invitation invitation(invitationName);
-      Name chatroomPrefix;
-      chatroomPrefix.append("ndn")
-        .append("broadcast")
-        .append("ChronoChat")
-        .append(invitation.getChatroom());
+  if (accepted) {
+    Invitation invitation(invitationName);
+    Name chatroomPrefix;
+    chatroomPrefix.append("ndn")
+      .append("broadcast")
+      .append("ChronoChat")
+      .append(invitation.getChatroom());
 
-      //We should create a chatroom specific key/cert (which should be created in the first half of this method, but let's use the default one for now.
-      shared_ptr<IdentityCertificate> idCert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
-      ChatDialog* chatDialog = new ChatDialog(&m_contactManager, m_face, *idCert, chatroomPrefix, m_localPrefix, m_nick, true);
-      chatDialog->addSyncAnchor(invitation);
+    //We should create a chatroom specific key/cert (which should be created in the first half of this method, but let's use the default one for now.
+    shared_ptr<IdentityCertificate> idCert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity));
+    ChatDialog* chatDialog = new ChatDialog(&m_contactManager, m_face, *idCert, chatroomPrefix, m_localPrefix, m_nick, true);
+    chatDialog->addSyncAnchor(invitation);
 
-      addChatDialog(QString::fromStdString(invitation.getChatroom()), chatDialog);
-      chatDialog->show();
-    }
+    addChatDialog(QString::fromStdString(invitation.getChatroom()), chatDialog);
+    chatDialog->show();
+  }
 }
 
 void
@@ -773,25 +766,24 @@
 {
   ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
 
-  if(it != m_chatDialogList.end())
-    {
-      ChatDialog* deletedChat = it->second;
-      if(deletedChat)
-        delete deletedChat;
-      m_chatDialogList.erase(it);
+  if (it != m_chatDialogList.end()) {
+    ChatDialog* deletedChat = it->second;
+    if (deletedChat)
+      delete deletedChat;
+    m_chatDialogList.erase(it);
 
-      QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
-      QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
-      if(chatAction)
-        delete chatAction;
-      if(closeAction)
-        delete closeAction;
+    QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
+    QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
+    if (chatAction)
+      delete chatAction;
+    if (closeAction)
+      delete closeAction;
 
-      m_chatActionList.erase(chatroomName.toStdString());
-      m_closeActionList.erase(chatroomName.toStdString());
+    m_chatActionList.erase(chatroomName.toStdString());
+    m_closeActionList.erase(chatroomName.toStdString());
 
-      updateMenu();
-    }
+    updateMenu();
+  }
 }
 
 void
@@ -808,26 +800,28 @@
 }
 
 void
-Controller::onInvitationInterest(const Name& prefix, const Interest& interest, size_t routingPrefixOffset)
+Controller::onInvitationInterest(const Name& prefix,
+                                 const Interest& interest,
+                                 size_t routingPrefixOffset)
 {
-  _LOG_DEBUG("onInvitationInterest: " << interest.getName());
-  shared_ptr<Interest> invitationInterest = make_shared<Interest>(boost::cref(interest.getName().getSubName(routingPrefixOffset)));
+  // _LOG_DEBUG("onInvitationInterest: " << interest.getName());
+  shared_ptr<Interest> invitationInterest =
+    make_shared<Interest>(boost::cref(interest.getName().getSubName(routingPrefixOffset)));
 
   // check if the chatroom already exists;
-  try
-    {
+  try {
       Invitation invitation(invitationInterest->getName());
-      if(m_chatDialogList.find(invitation.getChatroom()) != m_chatDialogList.end())
+      if (m_chatDialogList.find(invitation.getChatroom()) != m_chatDialogList.end())
         return;
-    }
-  catch(Invitation::Error& e)
-    {
-      // Cannot parse the invitation;
-      return;
-    }
+  }
+  catch (Invitation::Error& e) {
+    // Cannot parse the invitation;
+    return;
+  }
 
   OnInterestValidated onValidated = bind(&Controller::onInvitationValidated, this, _1);
-  OnInterestValidationFailed onValidationFailed = bind(&Controller::onInvitationValidationFailed, this, _1, _2);
+  OnInterestValidationFailed onValidationFailed = bind(&Controller::onInvitationValidationFailed,
+                                                       this, _1, _2);
   m_validator.validate(*invitationInterest, onValidated, onValidationFailed);
 }