src: Add chatroom discovery

Change-Id: I45e17a8d8bbcdef6dc5f93c528cde91181f3b578
diff --git a/src/controller.cpp b/src/controller.cpp
index f716ada..dce0b03 100644
--- a/src/controller.cpp
+++ b/src/controller.cpp
@@ -13,7 +13,6 @@
 #include <QDir>
 #include <QTimer>
 #include "controller.hpp"
-//#include "chatroom-discovery.h"
 
 #ifndef Q_MOC_RUN
 #include <boost/filesystem.hpp>
@@ -34,6 +33,8 @@
 Q_DECLARE_METATYPE(size_t)
 Q_DECLARE_METATYPE(chronochat::ChatroomInfo)
 Q_DECLARE_METATYPE(chronochat::Invitation)
+Q_DECLARE_METATYPE(std::string)
+Q_DECLARE_METATYPE(ndn::Name::Component)
 
 namespace chronochat {
 
@@ -43,21 +44,25 @@
 Controller::Controller(QWidget* parent)
   : QDialog(parent)
   , m_localPrefixDetected(false)
-  , m_settingDialog(new SettingDialog)
-  , m_startChatDialog(new StartChatDialog)
-  , m_profileEditor(new ProfileEditor)
-  , m_invitationDialog(new InvitationDialog)
-  , m_contactPanel(new ContactPanel)
-  , m_browseContactDialog(new BrowseContactDialog)
-  , m_addContactPanel(new AddContactPanel)
+  , m_settingDialog(new SettingDialog(this))
+  , m_startChatDialog(new StartChatDialog(this))
+  , m_profileEditor(new ProfileEditor(this))
+  , m_invitationDialog(new InvitationDialog(this))
+  , m_contactPanel(new ContactPanel(this))
+  , m_browseContactDialog(new BrowseContactDialog(this))
+  , m_addContactPanel(new AddContactPanel(this))
+  , m_discoveryPanel(new DiscoveryPanel(this))
 {
   qRegisterMetaType<ndn::Name>("ndn.Name");
   qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate");
   qRegisterMetaType<chronochat::EndorseInfo>("chronochat.EndorseInfo");
   qRegisterMetaType<ndn::Interest>("ndn.Interest");
   qRegisterMetaType<size_t>("size_t");
-  qRegisterMetaType<chronochat::ChatroomInfo>("chronos.Chatroom");
-  qRegisterMetaType<chronochat::Invitation>("chronos.Invitation");
+  qRegisterMetaType<chronochat::ChatroomInfo>("chronochat.Chatroom");
+  qRegisterMetaType<chronochat::Invitation>("chronochat.Invitation");
+  qRegisterMetaType<std::string>("std.string");
+  qRegisterMetaType<ndn::Name::Component>("ndn.Component");
+
 
   // Connection to ContactManager
   connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)),
@@ -103,7 +108,6 @@
           m_addContactPanel,
           SLOT(onContactEndorseInfoReady(const chronochat::EndorseInfo&)));
 
-
   // Connection to BrowseContactDialog
   connect(m_browseContactDialog, SIGNAL(directAddClicked()),
           this, SLOT(onDirectAdd()));
@@ -177,6 +181,43 @@
 
   initialize();
 
+  m_chatroomDiscoveryBackend
+    = new ChatroomDiscoveryBackend(m_localPrefix,
+                                   m_identity,
+                                   this);
+
+  // connect to chatroom discovery back end
+  connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
+          m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
+  connect(this, SIGNAL(localPrefixConfigured(const QString&)),
+          m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
+  connect(this, SIGNAL(newChatroomForDiscovery(Name::Component)),
+          m_chatroomDiscoveryBackend, SLOT(onNewChatroomForDiscovery(Name::Component)));
+  connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoRequest(std::string, bool)),
+          this, SLOT(onChatroomInfoRequest(std::string, bool)));
+  connect(this, SIGNAL(respondChatroomInfoRequest(ChatroomInfo, bool)),
+          m_chatroomDiscoveryBackend, SLOT(onRespondChatroomInfoRequest(ChatroomInfo, bool)));
+  connect(this, SIGNAL(shutdownDiscoveryBackend()),
+          m_chatroomDiscoveryBackend, SLOT(shutdown()));
+  connect(this, SIGNAL(identityUpdated(const QString&)),
+          m_chatroomDiscoveryBackend, SLOT(onIdentityUpdated(const QString&)));
+
+  // connect chatroom discovery back end with front end
+  connect(m_discoveryPanel, SIGNAL(waitForChatroomInfo(const QString&)),
+          m_chatroomDiscoveryBackend, SLOT(onWaitForChatroomInfo(const QString&)));
+  connect(m_discoveryPanel, SIGNAL(warning(const QString&)),
+          this, SLOT(onWarning(const QString&)));
+  connect(this, SIGNAL(identityUpdated(const QString&)),
+          m_discoveryPanel, SLOT(onIdentityUpdated(const QString&)));
+  connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomListReady(const QStringList&)),
+          m_discoveryPanel, SLOT(onChatroomListReady(const QStringList&)));
+  connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoReady(const ChatroomInfo&)),
+          m_discoveryPanel, SLOT(onChatroomInfoReady(const ChatroomInfo&)));
+  connect(m_discoveryPanel, SIGNAL(startChatroom(const QString&, bool)),
+          this, SLOT(onStartChatroom(const QString&, bool)));
+
+  m_chatroomDiscoveryBackend->start();
+
   createTrayIcon();
 
   emit updateLocalPrefix();
@@ -288,9 +329,6 @@
   m_startChatroom = new QAction(tr("Start new chat"), this);
   connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction()));
 
-  m_discoveryAction = new QAction(tr("Ongoing Chatrooms"), this);
-  connect(m_discoveryAction, SIGNAL(triggered()), this, SLOT(onDiscoveryAction()));
-
   m_settingsAction = new QAction(tr("Settings"), this);
   connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction()));
 
@@ -303,6 +341,9 @@
   m_addContactAction = new QAction(tr("Add contact"), this);
   connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction()));
 
+  m_chatroomDiscoveryAction = new QAction(tr("Chatroom Discovery"), this);
+  connect(m_chatroomDiscoveryAction, SIGNAL(triggered()), this, SLOT(onChatroomDiscoveryAction()));
+
   m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
   connect(m_updateLocalPrefixAction, SIGNAL(triggered()),
           &m_backend, SLOT(onUpdateLocalPrefixAction()));
@@ -322,7 +363,7 @@
 
   m_trayIconMenu = new QMenu(this);
   m_trayIconMenu->addAction(m_startChatroom);
-  // m_trayIconMenu->addAction(m_discoveryAction); // disable discovery temporarily
+  m_trayIconMenu->addAction(m_chatroomDiscoveryAction);
 
   m_trayIconMenu->addSeparator();
   m_trayIconMenu->addAction(m_settingsAction);
@@ -354,7 +395,7 @@
   QMenu* closeMenu = 0;
 
   menu->addAction(m_startChatroom);
-  // menu->addAction(m_discoveryAction);
+  menu->addAction(m_chatroomDiscoveryAction);
 
   menu->addSeparator();
   menu->addAction(m_settingsAction);
@@ -428,13 +469,18 @@
           this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&)));
   connect(chatDialog, SIGNAL(resetIcon()),
           this, SLOT(onResetIcon()));
-  connect(chatDialog, SIGNAL(rosterChanged(const chronochat::ChatroomInfo&)),
-          this, SLOT(onRosterChanged(const chronochat::ChatroomInfo&)));
   connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
           chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
   connect(this, SIGNAL(localPrefixConfigured(const QString&)),
           chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
 
+  // connect chat dialog with discovery backend
+  connect(chatDialog->getBackend(), SIGNAL(addInRoster(ndn::Name, ndn::Name::Component)),
+          m_chatroomDiscoveryBackend, SLOT(onAddInRoster(ndn::Name, ndn::Name::Component)));
+  connect(chatDialog->getBackend(), SIGNAL(eraseInRoster(ndn::Name, ndn::Name::Component)),
+          m_chatroomDiscoveryBackend, SLOT(onEraseInRoster(ndn::Name, ndn::Name::Component)));
+
+
   QAction* chatAction = new QAction(chatroomName, this);
   m_chatActionList[chatroomName.toStdString()] = chatAction;
   connect(chatAction, SIGNAL(triggered()),
@@ -446,6 +492,7 @@
           chatDialog, SLOT(shutdown()));
 
   updateMenu();
+  emit newChatroomForDiscovery(Name::Component(chatroomName.toStdString()));
 }
 
 void
@@ -522,12 +569,6 @@
 }
 
 void
-Controller::onDiscoveryAction()
-{
-}
-
-
-void
 Controller::onSettingsAction()
 {
   m_settingDialog->setNick(QString(m_nick.c_str()));
@@ -558,6 +599,13 @@
 }
 
 void
+Controller::onChatroomDiscoveryAction()
+{
+  m_discoveryPanel->show();
+  m_discoveryPanel->raise();
+}
+
+void
 Controller::onDirectAdd()
 {
   m_addContactPanel->show();
@@ -572,6 +620,7 @@
   m_profileEditor->hide();
   m_invitationDialog->hide();
   m_addContactPanel->hide();
+  m_discoveryPanel->hide();
 
   ChatDialogList::iterator it = m_chatDialogList.begin();
   ChatDialogList::iterator end = m_chatDialogList.end();
@@ -593,6 +642,12 @@
   delete m_invitationDialog;
   delete m_browseContactDialog;
   delete m_addContactPanel;
+  delete m_discoveryPanel;
+  if (m_chatroomDiscoveryBackend->isRunning()) {
+    emit shutdownDiscoveryBackend();
+    m_chatroomDiscoveryBackend->wait();
+  }
+  delete m_chatroomDiscoveryBackend;
 
   if (m_backend.isRunning()) {
     emit shutdownBackend();
@@ -609,6 +664,7 @@
   chatroomPrefix.append("ndn")
     .append("broadcast")
     .append("ChronoChat")
+    .append("Chatroom")
     .append(chatroomName.toStdString());
 
   // check if the chatroom exists
@@ -623,7 +679,7 @@
   //(which should be created in the first half of this method
   //, but let's use the default one for now.
   Name chatPrefix;
-  chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(chatroomName.toStdString());
+  chatPrefix.append(m_identity).append("CHRONOCHAT-CHATDATA").append(chatroomName.toStdString());
 
   ChatDialog* chatDialog
     = new ChatDialog(chatroomPrefix,
@@ -632,7 +688,8 @@
                      chatroomName.toStdString(),
                      m_nick,
                      true,
-                     m_identity);
+                     m_identity,
+                     this);
 
   addChatDialog(chatroomName, chatDialog);
   chatDialog->show();
@@ -672,11 +729,11 @@
 Controller::onRemoveChatDialog(const QString& chatroomName)
 {
   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);
 
     QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
@@ -707,9 +764,11 @@
 }
 
 void
-Controller::onRosterChanged(const chronochat::ChatroomInfo& info)
+Controller::onChatroomInfoRequest(std::string chatroomName, bool isManager)
 {
-
+  auto it = m_chatDialogList.find(chatroomName);
+  if (it != m_chatDialogList.end())
+    emit respondChatroomInfoRequest(it->second->getChatroomInfo(), isManager);
 }
 
 } // namespace chronochat