separate ChatDialog frontend & backend

Change-Id: I824e58579a9aaac0264561cc7ae3d4977d98a3bf
diff --git a/src/chat-dialog.hpp b/src/chat-dialog.hpp
index 7c63b30..7f1a726 100644
--- a/src/chat-dialog.hpp
+++ b/src/chat-dialog.hpp
@@ -20,30 +20,16 @@
 #include <QTimer>
 
 #ifndef Q_MOC_RUN
-#include "common.hpp"
-#include "contact-manager.hpp"
 #include "invitation.hpp"
-#include "contact.hpp"
-#include "chatbuf.pb.h"
-#include "intro-cert-list.pb.h"
+
 #include "digest-tree-scene.hpp"
 #include "trust-tree-scene.hpp"
 #include "trust-tree-node.hpp"
-#include "validator-invitation.hpp"
-#include <sync-socket.h>
-#include <sync-seq-no.h>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <boost/thread/locks.hpp>
-#include <boost/thread/recursive_mutex.hpp>
-#include <boost/thread/thread.hpp>
+#include "chat-dialog-backend.hpp"
+
 #include "chatroom-info.hpp"
 #endif
 
-#include "invite-list-dialog.hpp"
-
-
-#define MAX_HISTORY_ENTRY   20
-
 namespace Ui {
 class ChatDialog;
 }
@@ -56,37 +42,17 @@
 
 public:
   explicit
-  ChatDialog(ContactManager* contactManager,
-             shared_ptr<ndn::Face> face,
-             const ndn::IdentityCertificate& myCertificate,
-             const Name& chatroomPrefix,
-             const Name& localPrefix,
+  ChatDialog(const Name& chatroomPrefix,
+             const Name& userChatPrefix,
+             const Name& routingPrefix,
+             const std::string& chatroomName,
              const std::string& nick,
-             bool witSecurity,
+             bool isSecured = false,
              QWidget* parent = 0);
 
   ~ChatDialog();
 
   void
-  addSyncAnchor(const Invitation& invitation);
-
-  void
-  processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>&, Sync::SyncSocket *);
-
-  void
-  processDataWrapper(const shared_ptr<const Data>& data);
-
-  void
-  processDataNoShowWrapper(const shared_ptr<const Data>& data);
-
-  void
-  processRemoveWrapper(const std::string& prefix);
-
-  //ymj
-  shared_ptr<ChatroomInfo>
-  getChatroomInfo() const;
-
-  void
   closeEvent(QCloseEvent* e);
 
   void
@@ -98,91 +64,29 @@
   void
   showEvent(QShowEvent* e);
 
+  ChatDialogBackend*
+  getBackend()
+  {
+    return &m_backend;
+  }
+
+  void
+  addSyncAnchor(const Invitation& invitation)
+  {
+  }
+
+  shared_ptr<ChatroomInfo>
+  getChatroomInfo();
+
 private:
   void
-  updatePrefix();
-
-  void
-  updateLabels();
-
-  void
-  initializeSync();
-
-  void
-  sendInvitation(shared_ptr<Contact> contact, bool isIntroducer);
-
-  void
-  replyWrapper(const Interest& interest, Data& data,
-               size_t routablePrefixOffset, bool isIntroducer);
-
-  void
-  replyTimeoutWrapper(const Interest& interest,
-                      size_t routablePrefixOffset);
-
-  void
-  onReplyValidated(const ndn::shared_ptr<const ndn::Data>& data,
-                   size_t inviteeRoutablePrefixOffset,
-                   bool isIntroduce);
-
-  void
-  onReplyValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
-                          const std::string& failureInfo);
-
-  void
-  invitationRejected(const Name& identity);
-
-  void
-  invitationAccepted(const ndn::IdentityCertificate& inviteeCert,
-                     const Name& inviteePrefix, bool isIntroducer);
-
-  void
-  fetchIntroCert(const Name& identity, const Name& prefix);
-
-  void
-  onIntroCertList(const Interest& interest, const Data& data);
-
-  void
-  onIntroCertListTimeout(const Interest& interest, int retry, const std::string& msg);
-
-  void
-  introCertWrapper(const Interest& interest, Data& data);
-
-  void
-  introCertTimeoutWrapper(const Interest& interest, int retry, const QString& msg);
-
-  void
-  onCertListInterest(const ndn::Name& prefix, const ndn::Interest& interest);
-
-  void
-  onCertListRegisterFailed(const ndn::Name& prefix, const std::string& msg);
-
-  void
-  onCertSingleInterest(const Name& prefix, const Interest& interest);
-
-  void
-  onCertSingleRegisterFailed(const Name& prefix, const std::string& msg);
-
-
-  void
-  sendMsg(SyncDemo::ChatMessage& msg);
-
-  void
   disableSyncTreeDisplay();
 
   void
-  appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
+  appendChatMessage(const QString& nick, const QString& text, time_t timestamp);
 
   void
-  processRemove(QString prefix);
-
-  ndn::Name
-  getInviteeRoutablePrefix(const Name& invitee);
-
-  void
-  formChatMessage(const QString& text, SyncDemo::ChatMessage& msg);
-
-  void
-  formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
+  appendControlMessage(const QString& nick, const QString& action, time_t timestamp);
 
   QString
   formatTime(time_t timestamp);
@@ -190,61 +94,29 @@
   void
   printTimeInCell(QTextTable* table, time_t timestamp);
 
-  std::string
-  getRandomString();
-
   void
   showMessage(const QString&, const QString&);
 
   void
   fitView();
 
-  void
-  summonReaper();
-
-  void
-  getTree(TrustTreeNodeList& nodeList);
-
-  void
-  plotTrustTree();
-
 signals:
   void
-  processData(const ndn::shared_ptr<const ndn::Data>& data,
-              bool show, bool isHistory);
+  shutdownBackend();
 
   void
-  processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
+  msgToSent(QString text, time_t timestamp);
 
   void
   closeChatDialog(const QString& chatroomName);
 
   void
-  inivationRejection(const QString& msg);
-
-  void
   showChatMessage(const QString& chatroomName, const QString& from, const QString& data);
 
   void
   resetIcon();
 
   void
-  reply(const ndn::Interest& interest, const ndn::shared_ptr<const ndn::Data>& data,
-        size_t routablePrefixOffset, bool isIntroducer);
-
-  void
-  replyTimeout(const ndn::Interest& interest, size_t routablePrefixOffset);
-
-  void
-  introCert(const ndn::Interest& interest, const ndn::shared_ptr<const ndn::Data>& data);
-
-  void
-  introCertTimeout(const ndn::Interest& interest, int retry, const QString& msg);
-
-  void
-  waitForContactList();
-
-  void
   rosterChanged(const chronos::ChatroomInfo& info);
 
 public slots:
@@ -252,13 +124,31 @@
   onShow();
 
   void
-  onLocalPrefixUpdated(const QString& localPrefix);
-
-  void
-  onClose();
+  shutdown();
 
 private slots:
   void
+  updateSyncTree(std::vector<chronos::NodeInfo> updates, QString rootDigest);
+
+  void
+  receiveChatMessage(QString nick, QString text, time_t timestamp);
+
+  void
+  addSession(QString sessionPrefix, QString nick, time_t timestamp);
+
+  void
+  removeSession(QString sessionPrefix, QString nick, time_t timestamp);
+
+  void
+  updateNick(QString sessionPrefix, QString nick);
+
+  void
+  receiveMessage(QString sessionPrefix);
+
+  void
+  updateLabels(ndn::Name newChatPrefix);
+
+  void
   onReturnPressed();
 
   void
@@ -268,97 +158,21 @@
   onTrustTreeButtonPressed();
 
   void
-  onProcessData(const ndn::shared_ptr<const ndn::Data>& data, bool show, bool isHistory);
-
-  void
-  onProcessTreeUpdate(const std::vector<Sync::MissingDataInfo>&);
-
-  void
-  onReplot();
-
-  void
-  onRosterChanged(QStringList staleUserList);
-
-  void
-  onInviteListDialogRequested();
-
-  void
-  sendJoin();
-
-  void
-  sendHello();
-
-  void
-  sendLeave();
-
-  void
   enableSyncTreeDisplay();
 
-  void
-  reap();
-
-  void
-  onSendInvitation(QString invitee);
-
-  void
-  onReply(const ndn::Interest& interest, const ndn::shared_ptr<const ndn::Data>& data,
-          size_t routablePrefixOffset, bool isIntroducer);
-
-  void
-  onReplyTimeout(const ndn::Interest& interest, size_t routablePrefixOffset);
-
-  void
-  onIntroCert(const ndn::Interest& interest, const ndn::shared_ptr<const ndn::Data>& data);
-
-  void
-  onIntroCertTimeout(const ndn::Interest& interest, int retry, const QString& msg);
-
 private:
-  Ui::ChatDialog *ui;
-  ndn::KeyChain m_keyChain;
+  Ui::ChatDialog* ui;
 
-  ContactManager* m_contactManager;
-  shared_ptr<ndn::Face> m_face;
-
-  ndn::IdentityCertificate m_myCertificate;
-  Name m_identity;
-
-  Name m_certListPrefix;
-  const ndn::RegisteredPrefixId* m_certListPrefixId;
-  Name m_certSinglePrefix;
-  const ndn::RegisteredPrefixId* m_certSinglePrefixId;
+  ChatDialogBackend m_backend;
 
   std::string m_chatroomName;
-  Name m_chatroomPrefix;
-  Name m_localPrefix;
-  bool m_useRoutablePrefix;
-  Name m_chatPrefix;
-  Name m_localChatPrefix;
-  std::string m_nick;
-  DigestTreeScene *m_scene;
-  TrustTreeScene *m_trustScene;
-  QStringListModel *m_rosterModel;
-  QTimer* m_timer;
+  QString m_nick;
+  bool m_isSecured;
 
 
-  int64_t m_lastMsgTime;
-  int m_randomizedInterval;
-  bool m_joined;
-
-  Sync::SyncSocket *m_sock;
-  uint64_t m_session;
-  shared_ptr<ndn::SecRuleRelative> m_dataRule;
-
-  InviteListDialog* m_inviteListDialog;
-  shared_ptr<ValidatorInvitation> m_invitationValidator;
-
-  boost::recursive_mutex m_msgMutex;
-  boost::recursive_mutex m_sceneMutex;
-  QList<QString> m_zombieList;
-  int m_zombieIndex;
-
-  //ymj
-  ChatroomInfo::TrustModel m_trustModel;
+  DigestTreeScene* m_scene;
+  TrustTreeScene* m_trustScene;
+  QStringListModel* m_rosterModel;
 };
 
 } // namespace chronos