Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 4 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 9 | * Qiuhan Ding <qiuhanding@cs.ucla.edu> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 12 | #include "controller.hpp" |
| 13 | #include "cryptopp.hpp" |
| 14 | #include "conf.hpp" |
| 15 | #include "endorse-info.hpp" |
| 16 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 17 | #include <QApplication> |
| 18 | #include <QMessageBox> |
| 19 | #include <QDir> |
| 20 | #include <QTimer> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 21 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 22 | #include <boost/filesystem.hpp> |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 23 | #include <ndn-cxx/util/random.hpp> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | |
| 25 | Q_DECLARE_METATYPE(ndn::Name) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 26 | Q_DECLARE_METATYPE(ndn::security::Certificate) |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 27 | Q_DECLARE_METATYPE(chronochat::EndorseInfo) |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 28 | Q_DECLARE_METATYPE(ndn::Interest) |
| 29 | Q_DECLARE_METATYPE(size_t) |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 30 | Q_DECLARE_METATYPE(chronochat::ChatroomInfo) |
| 31 | Q_DECLARE_METATYPE(chronochat::Invitation) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 32 | Q_DECLARE_METATYPE(std::string) |
| 33 | Q_DECLARE_METATYPE(ndn::Name::Component) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 35 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 36 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 37 | using std::string; |
| 38 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 39 | // constructor & destructor |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 40 | Controller::Controller(QWidget* parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 41 | : QDialog(parent) |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 42 | , m_localPrefixDetected(false) |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 43 | , m_isInConnectionDetection(false) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 44 | , m_settingDialog(new SettingDialog(this)) |
| 45 | , m_startChatDialog(new StartChatDialog(this)) |
| 46 | , m_profileEditor(new ProfileEditor(this)) |
| 47 | , m_invitationDialog(new InvitationDialog(this)) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 48 | , m_invitationRequestDialog(new InvitationRequestDialog(this)) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 49 | , m_contactPanel(new ContactPanel(this)) |
| 50 | , m_browseContactDialog(new BrowseContactDialog(this)) |
| 51 | , m_addContactPanel(new AddContactPanel(this)) |
| 52 | , m_discoveryPanel(new DiscoveryPanel(this)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 53 | { |
| 54 | qRegisterMetaType<ndn::Name>("ndn.Name"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 55 | qRegisterMetaType<ndn::security::Certificate>("ndn.security.v2.Certificate"); |
| 56 | qRegisterMetaType<chronochat::EndorseInfo>("EndorseInfo"); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 57 | qRegisterMetaType<ndn::Interest>("ndn.Interest"); |
| 58 | qRegisterMetaType<size_t>("size_t"); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 59 | qRegisterMetaType<chronochat::ChatroomInfo>("chronochat.Chatroom"); |
| 60 | qRegisterMetaType<chronochat::Invitation>("chronochat.Invitation"); |
| 61 | qRegisterMetaType<std::string>("std.string"); |
| 62 | qRegisterMetaType<ndn::Name::Component>("ndn.Component"); |
| 63 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 64 | |
| 65 | // Connection to ContactManager |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 66 | connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | this, SLOT(onWarning(const QString&))); |
| 68 | connect(this, SIGNAL(refreshBrowseContact()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 69 | m_backend.getContactManager(), SLOT(onRefreshBrowseContact())); |
| 70 | connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 71 | this, SLOT(onWarning(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 72 | |
| 73 | // Connection to SettingDialog |
| 74 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 75 | m_settingDialog, SLOT(onIdentityUpdated(const QString&))); |
| 76 | connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)), |
| 77 | this, SLOT(onIdentityUpdated(const QString&))); |
| 78 | connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)), |
| 79 | this, SLOT(onNickUpdated(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 80 | connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)), |
| 81 | this, SLOT(onLocalPrefixConfigured(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 82 | |
| 83 | // Connection to ProfileEditor |
| 84 | connect(this, SIGNAL(closeDBModule()), |
| 85 | m_profileEditor, SLOT(onCloseDBModule())); |
| 86 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 87 | m_profileEditor, SLOT(onIdentityUpdated(const QString&))); |
| 88 | connect(m_profileEditor, SIGNAL(updateProfile()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 89 | m_backend.getContactManager(), SLOT(onUpdateProfile())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 90 | |
| 91 | // Connection to StartChatDialog |
| 92 | connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)), |
| 93 | this, SLOT(onStartChatroom(const QString&, bool))); |
| 94 | |
| 95 | // Connection to InvitationDialog |
| 96 | connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 97 | &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 98 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 99 | // Connection to InvitationRequestDialog |
| 100 | connect(m_invitationRequestDialog, SIGNAL(invitationRequestResponded(const ndn::Name&, bool)), |
| 101 | &m_backend, SLOT(onInvitationRequestResponded(const ndn::Name&, bool))); |
| 102 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 103 | // Connection to AddContactPanel |
| 104 | connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 105 | m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 106 | connect(m_addContactPanel, SIGNAL(addContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 107 | m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&))); |
| 108 | connect(m_backend.getContactManager(), |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 109 | SIGNAL(contactEndorseInfoReady(const EndorseInfo&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 110 | m_addContactPanel, |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 111 | SLOT(onContactEndorseInfoReady(const EndorseInfo&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 112 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 113 | // Connection to BrowseContactDialog |
| 114 | connect(m_browseContactDialog, SIGNAL(directAddClicked()), |
| 115 | this, SLOT(onDirectAdd())); |
| 116 | connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 117 | m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 118 | connect(m_browseContactDialog, SIGNAL(addContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 119 | m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&))); |
| 120 | connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 121 | m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 122 | connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 123 | m_browseContactDialog, SLOT(onNameListReady(const QStringList&))); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 124 | connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::security::Certificate&)), |
| 125 | m_browseContactDialog, SLOT(onIdCertReady(const ndn::security::Certificate&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 126 | |
| 127 | // Connection to ContactPanel |
| 128 | connect(m_contactPanel, SIGNAL(waitForContactList()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 129 | m_backend.getContactManager(), SLOT(onWaitForContactList())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 130 | connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 131 | m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 132 | connect(m_contactPanel, SIGNAL(removeContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 133 | m_backend.getContactManager(), SLOT(onRemoveContact(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 135 | m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 136 | connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 137 | m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 138 | connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 139 | m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 140 | connect(m_contactPanel, SIGNAL(warning(const QString&)), |
| 141 | this, SLOT(onWarning(const QString&))); |
| 142 | connect(this, SIGNAL(closeDBModule()), |
| 143 | m_contactPanel, SLOT(onCloseDBModule())); |
| 144 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 145 | m_contactPanel, SLOT(onIdentityUpdated(const QString&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 146 | connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 147 | m_contactPanel, SLOT(onContactAliasListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 148 | connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 149 | m_contactPanel, SLOT(onContactIdListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 150 | connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&, |
| 151 | const QString&, bool)), |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 152 | m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&, |
| 153 | const QString&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 154 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 155 | // Connection to backend thread |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 156 | connect(&m_backend, SIGNAL(nfdError()), |
| 157 | this, SLOT(onNfdError())); |
| 158 | connect(this, SIGNAL(nfdReconnect()), |
| 159 | &m_backend, SLOT(onNfdReconnect())); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 160 | connect(this, SIGNAL(shutdownBackend()), |
| 161 | &m_backend, SLOT(shutdown())); |
| 162 | connect(this, SIGNAL(updateLocalPrefix()), |
| 163 | &m_backend, SLOT(onUpdateLocalPrefixAction())); |
| 164 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 165 | &m_backend, SLOT(onIdentityChanged(const QString&))); |
| 166 | connect(this, SIGNAL(addChatroom(QString)), |
| 167 | &m_backend, SLOT(addChatroom(QString))); |
| 168 | connect(this, SIGNAL(removeChatroom(QString)), |
| 169 | &m_backend, SLOT(removeChatroom(QString))); |
| 170 | |
| 171 | // Thread notifications: |
| 172 | // on local prefix udpated: |
| 173 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
| 174 | this, SLOT(onLocalPrefixUpdated(const QString&))); |
| 175 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
| 176 | m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&))); |
| 177 | |
| 178 | // on invitation validated: |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 179 | connect(&m_backend, SIGNAL(invitationValidated(QString, QString, ndn::Name)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 180 | m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name))); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 181 | connect(&m_backend, SIGNAL(startChatroom(const QString&, bool)), |
| 182 | this, SLOT(onStartChatroom(const QString&, bool))); |
| 183 | |
| 184 | // on invitation request received |
| 185 | connect(&m_backend, SIGNAL(invitationRequestReceived(QString, QString, ndn::Name)), |
| 186 | m_invitationRequestDialog, SLOT(onInvitationRequestReceived(QString, QString, |
| 187 | ndn::Name))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 188 | |
| 189 | // on invitation accepted: |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 190 | connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronochat::Invitation, bool)), |
| 191 | this, SLOT(onStartChatroom2(chronochat::Invitation, bool))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 192 | |
| 193 | m_backend.start(); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 194 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 195 | initialize(); |
| 196 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 197 | m_chatroomDiscoveryBackend |
| 198 | = new ChatroomDiscoveryBackend(m_localPrefix, |
| 199 | m_identity, |
| 200 | this); |
| 201 | |
| 202 | // connect to chatroom discovery back end |
| 203 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
| 204 | m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&))); |
| 205 | connect(this, SIGNAL(localPrefixConfigured(const QString&)), |
| 206 | m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&))); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 207 | connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoRequest(std::string, bool)), |
| 208 | this, SLOT(onChatroomInfoRequest(std::string, bool))); |
| 209 | connect(this, SIGNAL(respondChatroomInfoRequest(ChatroomInfo, bool)), |
| 210 | m_chatroomDiscoveryBackend, SLOT(onRespondChatroomInfoRequest(ChatroomInfo, bool))); |
| 211 | connect(this, SIGNAL(shutdownDiscoveryBackend()), |
| 212 | m_chatroomDiscoveryBackend, SLOT(shutdown())); |
| 213 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 214 | m_chatroomDiscoveryBackend, SLOT(onIdentityUpdated(const QString&))); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 215 | connect(this, SIGNAL(nfdReconnect()), |
| 216 | m_chatroomDiscoveryBackend, SLOT(onNfdReconnect())); |
| 217 | connect(m_chatroomDiscoveryBackend, SIGNAL(nfdError()), |
| 218 | this, SLOT(onNfdError())); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 219 | |
| 220 | // connect chatroom discovery back end with front end |
| 221 | connect(m_discoveryPanel, SIGNAL(waitForChatroomInfo(const QString&)), |
| 222 | m_chatroomDiscoveryBackend, SLOT(onWaitForChatroomInfo(const QString&))); |
| 223 | connect(m_discoveryPanel, SIGNAL(warning(const QString&)), |
| 224 | this, SLOT(onWarning(const QString&))); |
| 225 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 226 | m_discoveryPanel, SLOT(onIdentityUpdated(const QString&))); |
| 227 | connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomListReady(const QStringList&)), |
| 228 | m_discoveryPanel, SLOT(onChatroomListReady(const QStringList&))); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 229 | connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoReady(const ChatroomInfo&, bool)), |
| 230 | m_discoveryPanel, SLOT(onChatroomInfoReady(const ChatroomInfo&, bool))); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 231 | connect(m_discoveryPanel, SIGNAL(startChatroom(const QString&, bool)), |
| 232 | this, SLOT(onStartChatroom(const QString&, bool))); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 233 | connect(m_discoveryPanel, SIGNAL(sendInvitationRequest(const QString&, const QString&)), |
| 234 | &m_backend, SLOT(onSendInvitationRequest(const QString&, const QString&))); |
| 235 | connect(&m_backend, SIGNAL(invitationRequestResult(const std::string&)), |
| 236 | m_discoveryPanel, SLOT(onInvitationRequestResult(const std::string&))); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 237 | |
| 238 | m_chatroomDiscoveryBackend->start(); |
| 239 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 240 | createTrayIcon(); |
| 241 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 242 | emit updateLocalPrefix(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 243 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 244 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 245 | Controller::~Controller() |
| 246 | { |
| 247 | saveConf(); |
| 248 | } |
| 249 | |
| 250 | // public methods |
| 251 | |
| 252 | |
| 253 | // private methods |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 254 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 255 | Controller::getDBName() |
| 256 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 257 | string dbName("chronos-"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 258 | |
| 259 | std::stringstream ss; |
| 260 | { |
| 261 | using namespace CryptoPP; |
| 262 | |
| 263 | SHA256 hash; |
| 264 | StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true, |
| 265 | new HashFilter(hash, new HexEncoder(new FileSink(ss), false))); |
| 266 | } |
| 267 | dbName.append(ss.str()).append(".db"); |
| 268 | |
| 269 | return dbName; |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | Controller::openDB() |
| 274 | { |
| 275 | m_db = QSqlDatabase::addDatabase("QSQLITE"); |
| 276 | QString path = (QDir::home().path()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 277 | path.append(QDir::separator()) |
| 278 | .append(".chronos") |
| 279 | .append(QDir::separator()) |
| 280 | .append(getDBName().c_str()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 281 | m_db.setDatabaseName(path); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 282 | |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 283 | m_db.open(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
| 287 | Controller::initialize() |
| 288 | { |
| 289 | loadConf(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 290 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 291 | openDB(); |
| 292 | |
| 293 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void |
| 297 | Controller::loadConf() |
| 298 | { |
| 299 | namespace fs = boost::filesystem; |
| 300 | |
| 301 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 302 | fs::create_directories (chronosDir); |
| 303 | |
| 304 | std::ifstream is((chronosDir / "config").c_str ()); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 305 | Conf conf; |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 306 | Block confBlock; |
| 307 | try { |
| 308 | confBlock = ndn::Block::fromStream(is); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 309 | conf.wireDecode(confBlock); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 310 | m_identity.clear(); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 311 | m_identity.append(conf.getIdentity()); |
| 312 | if (conf.getNick().length() != 0) |
| 313 | m_nick = conf.getNick(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 314 | else |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 315 | m_nick = m_identity.get(-1).toUri(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 316 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 317 | catch (const tlv::Error&) { |
Qiuhan Ding | e86f319 | 2015-04-07 17:52:59 -0700 | [diff] [blame] | 318 | try { |
| 319 | ndn::KeyChain keyChain; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 320 | m_identity = keyChain.getPib().getDefaultIdentity().getName(); |
Qiuhan Ding | e86f319 | 2015-04-07 17:52:59 -0700 | [diff] [blame] | 321 | } |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 322 | catch (const ndn::security::pib::Pib::Error&) { |
Qiuhan Ding | e86f319 | 2015-04-07 17:52:59 -0700 | [diff] [blame] | 323 | m_identity.clear(); |
| 324 | m_identity.append("chronochat-tmp-identity") |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 325 | .append(getRandomString()); |
Qiuhan Ding | e86f319 | 2015-04-07 17:52:59 -0700 | [diff] [blame] | 326 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 327 | m_nick = m_identity.get(-1).toUri(); |
| 328 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 331 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 332 | Controller::saveConf() |
| 333 | { |
| 334 | namespace fs = boost::filesystem; |
| 335 | |
| 336 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 337 | fs::create_directories (chronosDir); |
| 338 | |
| 339 | std::ofstream os((chronosDir / "config").c_str ()); |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 340 | Conf conf; |
| 341 | conf.setIdentity(m_identity); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 342 | if (!m_nick.empty()) |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 343 | conf.setNick(m_nick); |
| 344 | Block confWire = conf.wireEncode(); |
| 345 | os.write(reinterpret_cast<const char*>(confWire.wire()), confWire.size()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 346 | |
| 347 | os.close(); |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | Controller::createActions() |
| 352 | { |
| 353 | m_startChatroom = new QAction(tr("Start new chat"), this); |
| 354 | connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction())); |
| 355 | |
| 356 | m_settingsAction = new QAction(tr("Settings"), this); |
| 357 | connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction())); |
| 358 | |
| 359 | m_editProfileAction = new QAction(tr("Edit profile"), this); |
| 360 | connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction())); |
| 361 | |
| 362 | m_contactListAction = new QAction(tr("Contact List"), this); |
| 363 | connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction())); |
| 364 | |
| 365 | m_addContactAction = new QAction(tr("Add contact"), this); |
| 366 | connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction())); |
| 367 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 368 | m_chatroomDiscoveryAction = new QAction(tr("Chatroom Discovery"), this); |
| 369 | connect(m_chatroomDiscoveryAction, SIGNAL(triggered()), this, SLOT(onChatroomDiscoveryAction())); |
| 370 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 371 | m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 372 | connect(m_updateLocalPrefixAction, SIGNAL(triggered()), |
| 373 | &m_backend, SLOT(onUpdateLocalPrefixAction())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 374 | |
| 375 | m_minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 376 | connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction())); |
| 377 | |
| 378 | m_quitAction = new QAction(tr("Quit"), this); |
| 379 | connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction())); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 380 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
| 384 | Controller::createTrayIcon() |
| 385 | { |
| 386 | createActions(); |
| 387 | |
| 388 | m_trayIconMenu = new QMenu(this); |
| 389 | m_trayIconMenu->addAction(m_startChatroom); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 390 | m_trayIconMenu->addAction(m_chatroomDiscoveryAction); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 391 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 392 | m_trayIconMenu->addSeparator(); |
| 393 | m_trayIconMenu->addAction(m_settingsAction); |
| 394 | m_trayIconMenu->addAction(m_editProfileAction); |
| 395 | m_trayIconMenu->addSeparator(); |
| 396 | m_trayIconMenu->addAction(m_contactListAction); |
| 397 | m_trayIconMenu->addAction(m_addContactAction); |
| 398 | m_trayIconMenu->addSeparator(); |
| 399 | m_trayIconMenu->addAction(m_updateLocalPrefixAction); |
| 400 | m_trayIconMenu->addSeparator(); |
| 401 | m_trayIconMenu->addAction(m_minimizeAction); |
| 402 | m_closeMenu = m_trayIconMenu->addMenu("Close chatroom"); |
| 403 | m_closeMenu->setEnabled(false); |
| 404 | m_trayIconMenu->addSeparator(); |
| 405 | m_trayIconMenu->addAction(m_quitAction); |
| 406 | |
| 407 | m_trayIcon = new QSystemTrayIcon(this); |
| 408 | m_trayIcon->setContextMenu(m_trayIconMenu); |
| 409 | |
| 410 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 411 | m_trayIcon->setToolTip("ChronoChat System Tray Icon"); |
| 412 | m_trayIcon->setVisible(true); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | Controller::updateMenu() |
| 417 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 418 | m_trayIconMenu->clear(); |
| 419 | QMenu* menu = m_trayIconMenu; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 420 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 421 | menu->addAction(m_startChatroom); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 422 | menu->addAction(m_chatroomDiscoveryAction); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 423 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 424 | menu->addSeparator(); |
| 425 | menu->addAction(m_settingsAction); |
| 426 | menu->addAction(m_editProfileAction); |
| 427 | menu->addSeparator(); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 428 | menu->addAction(m_contactListAction); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 429 | menu->addAction(m_addContactAction); |
| 430 | menu->addSeparator(); |
| 431 | { |
| 432 | ChatActionList::const_iterator it = m_chatActionList.begin(); |
| 433 | ChatActionList::const_iterator end = m_chatActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 434 | if (it != end) { |
| 435 | for (; it != end; it++) |
| 436 | menu->addAction(it->second); |
| 437 | menu->addSeparator(); |
| 438 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 439 | } |
| 440 | menu->addAction(m_updateLocalPrefixAction); |
| 441 | menu->addSeparator(); |
| 442 | menu->addAction(m_minimizeAction); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 443 | m_closeMenu = menu->addMenu("Close chatroom"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 444 | { |
| 445 | ChatActionList::const_iterator it = m_closeActionList.begin(); |
| 446 | ChatActionList::const_iterator end = m_closeActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 447 | if (it == end) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 448 | m_closeMenu->setEnabled(false); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 449 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 450 | for (; it != end; it++) |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 451 | m_closeMenu->addAction(it->second); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 452 | } |
| 453 | menu->addSeparator(); |
| 454 | menu->addAction(m_quitAction); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 457 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 458 | Controller::getRandomString() |
| 459 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 460 | uint32_t r = ndn::random::generateWord32(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 461 | std::stringstream ss; |
| 462 | { |
| 463 | using namespace CryptoPP; |
| 464 | StringSource(reinterpret_cast<uint8_t*>(&r), 4, true, |
| 465 | new HexEncoder(new FileSink(ss), false)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 466 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 467 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 468 | // for (int i = 0; i < 8; i++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 469 | // { |
| 470 | // uint32_t t = r & mask; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 471 | // if (t < 10) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 472 | // ss << static_cast<char>(t + 0x30); |
| 473 | // else |
| 474 | // ss << static_cast<char>(t + 0x57); |
| 475 | // r = r >> 4; |
| 476 | // } |
| 477 | |
| 478 | return ss.str(); |
| 479 | } |
| 480 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 481 | void |
| 482 | Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog) |
| 483 | { |
| 484 | m_chatDialogList[chatroomName.toStdString()] = chatDialog; |
| 485 | connect(chatDialog, SIGNAL(closeChatDialog(const QString&)), |
| 486 | this, SLOT(onRemoveChatDialog(const QString&))); |
| 487 | connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)), |
| 488 | this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&))); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 489 | connect(chatDialog, SIGNAL(resetIcon()), |
| 490 | this, SLOT(onResetIcon())); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 491 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 492 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 493 | connect(this, SIGNAL(localPrefixConfigured(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 494 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 495 | connect(this, SIGNAL(nfdReconnect()), |
| 496 | chatDialog->getBackend(), SLOT(onNfdReconnect())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 497 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 498 | // connect chat dialog with discovery backend |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 499 | connect(chatDialog->getBackend(), SIGNAL(nfdError()), |
| 500 | this, SLOT(onNfdError())); |
| 501 | connect(chatDialog->getBackend(), SIGNAL(newChatroomForDiscovery(ndn::Name::Component)), |
| 502 | m_chatroomDiscoveryBackend, SLOT(onNewChatroomForDiscovery(ndn::Name::Component))); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 503 | connect(chatDialog->getBackend(), SIGNAL(addInRoster(ndn::Name, ndn::Name::Component)), |
| 504 | m_chatroomDiscoveryBackend, SLOT(onAddInRoster(ndn::Name, ndn::Name::Component))); |
| 505 | connect(chatDialog->getBackend(), SIGNAL(eraseInRoster(ndn::Name, ndn::Name::Component)), |
| 506 | m_chatroomDiscoveryBackend, SLOT(onEraseInRoster(ndn::Name, ndn::Name::Component))); |
| 507 | |
| 508 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 509 | QAction* chatAction = new QAction(chatroomName, this); |
| 510 | m_chatActionList[chatroomName.toStdString()] = chatAction; |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 511 | connect(chatAction, SIGNAL(triggered()), |
Yingdi Yu | e8323b6 | 2014-09-02 17:24:15 -0700 | [diff] [blame] | 512 | chatDialog, SLOT(onShow())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 513 | |
| 514 | QAction* closeAction = new QAction(chatroomName, this); |
| 515 | m_closeActionList[chatroomName.toStdString()] = closeAction; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 516 | connect(closeAction, SIGNAL(triggered()), |
| 517 | chatDialog, SLOT(shutdown())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 518 | |
| 519 | updateMenu(); |
| 520 | } |
| 521 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 522 | void |
| 523 | Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd) |
| 524 | { |
| 525 | emit discoverChatroomChanged(info, isAdd); |
| 526 | } |
| 527 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 528 | void |
| 529 | Controller::onIdentityUpdated(const QString& identity) |
| 530 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 531 | while (!m_chatDialogList.empty()) { |
| 532 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
Yingdi Yu | 1fd4e4a | 2015-02-02 13:37:50 -0800 | [diff] [blame] | 533 | it->second->shutdown(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 534 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 535 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 536 | emit closeDBModule(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 537 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 538 | Name identityName(identity.toStdString()); |
| 539 | m_identity = identityName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 540 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 541 | QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void |
| 545 | Controller::onIdentityUpdatedContinued() |
| 546 | { |
| 547 | QString connection = m_db.connectionName(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 548 | QSqlDatabase::removeDatabase(connection); |
| 549 | m_db.close(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 550 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 551 | openDB(); |
| 552 | |
| 553 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
| 554 | } |
| 555 | |
| 556 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 557 | Controller::onNickUpdated(const QString& nick) |
| 558 | { |
| 559 | m_nick = nick.toStdString(); |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | Controller::onLocalPrefixUpdated(const QString& localPrefix) |
| 564 | { |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 565 | QString privateLocalPrefix("/private/local"); |
| 566 | |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 567 | if (privateLocalPrefix != localPrefix) |
| 568 | m_localPrefixDetected = true; |
| 569 | else |
| 570 | m_localPrefixDetected = false; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 571 | |
| 572 | m_localPrefix = Name(localPrefix.toStdString()); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void |
| 576 | Controller::onLocalPrefixConfigured(const QString& prefix) |
| 577 | { |
| 578 | if (!m_localPrefixDetected) { |
| 579 | m_localPrefix = Name(prefix.toStdString()); |
| 580 | emit localPrefixConfigured(prefix); |
| 581 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void |
| 585 | Controller::onStartChatAction() |
| 586 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 587 | string chatroom = "chatroom-" + getRandomString(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 588 | |
| 589 | m_startChatDialog->setChatroom(chatroom); |
| 590 | m_startChatDialog->show(); |
| 591 | m_startChatDialog->raise(); |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | Controller::onSettingsAction() |
| 596 | { |
| 597 | m_settingDialog->setNick(QString(m_nick.c_str())); |
| 598 | m_settingDialog->show(); |
| 599 | m_settingDialog->raise(); |
| 600 | } |
| 601 | |
| 602 | void |
| 603 | Controller::onProfileEditorAction() |
| 604 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 605 | m_profileEditor->resetPanel(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 606 | m_profileEditor->show(); |
| 607 | m_profileEditor->raise(); |
| 608 | } |
| 609 | |
| 610 | void |
| 611 | Controller::onAddContactAction() |
| 612 | { |
| 613 | emit refreshBrowseContact(); |
| 614 | m_browseContactDialog->show(); |
| 615 | m_browseContactDialog->raise(); |
| 616 | } |
| 617 | |
| 618 | void |
| 619 | Controller::onContactListAction() |
| 620 | { |
| 621 | m_contactPanel->show(); |
| 622 | m_contactPanel->raise(); |
| 623 | } |
| 624 | |
| 625 | void |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 626 | Controller::onChatroomDiscoveryAction() |
| 627 | { |
| 628 | m_discoveryPanel->show(); |
| 629 | m_discoveryPanel->raise(); |
| 630 | } |
| 631 | |
| 632 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 633 | Controller::onDirectAdd() |
| 634 | { |
| 635 | m_addContactPanel->show(); |
| 636 | m_addContactPanel->raise(); |
| 637 | } |
| 638 | |
| 639 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 640 | Controller::onMinimizeAction() |
| 641 | { |
| 642 | m_settingDialog->hide(); |
| 643 | m_startChatDialog->hide(); |
| 644 | m_profileEditor->hide(); |
| 645 | m_invitationDialog->hide(); |
| 646 | m_addContactPanel->hide(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 647 | m_discoveryPanel->hide(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 648 | |
| 649 | ChatDialogList::iterator it = m_chatDialogList.begin(); |
| 650 | ChatDialogList::iterator end = m_chatDialogList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 651 | for (; it != end; it++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 652 | it->second->hide(); |
| 653 | } |
| 654 | |
| 655 | void |
| 656 | Controller::onQuitAction() |
| 657 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 658 | while (!m_chatDialogList.empty()) { |
| 659 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 660 | it->second->shutdown(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 661 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 662 | |
| 663 | delete m_settingDialog; |
| 664 | delete m_startChatDialog; |
| 665 | delete m_profileEditor; |
| 666 | delete m_invitationDialog; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 667 | delete m_browseContactDialog; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 668 | delete m_addContactPanel; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 669 | delete m_discoveryPanel; |
| 670 | if (m_chatroomDiscoveryBackend->isRunning()) { |
| 671 | emit shutdownDiscoveryBackend(); |
| 672 | m_chatroomDiscoveryBackend->wait(); |
| 673 | } |
| 674 | delete m_chatroomDiscoveryBackend; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 675 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 676 | if (m_backend.isRunning()) { |
| 677 | emit shutdownBackend(); |
| 678 | m_backend.wait(); |
| 679 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 680 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 681 | if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) { |
| 682 | emit shutdownNfdChecker(); |
| 683 | m_nfdConnectionChecker->wait(); |
| 684 | } |
| 685 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 686 | QApplication::quit(); |
| 687 | } |
| 688 | |
| 689 | void |
| 690 | Controller::onStartChatroom(const QString& chatroomName, bool secured) |
| 691 | { |
| 692 | Name chatroomPrefix; |
| 693 | chatroomPrefix.append("ndn") |
| 694 | .append("broadcast") |
| 695 | .append("ChronoChat") |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 696 | .append("Chatroom") |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 697 | .append(chatroomName.toStdString()); |
| 698 | |
| 699 | // check if the chatroom exists |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 700 | if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) { |
| 701 | QMessageBox::information(this, tr("ChronoChat"), |
| 702 | tr("You are creating an existing chatroom." |
| 703 | "You can check it in the context memu.")); |
| 704 | return; |
| 705 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 706 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 707 | // TODO: We should create a chatroom specific key/cert |
| 708 | //(which should be created in the first half of this method |
| 709 | //, but let's use the default one for now. |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 710 | Name chatPrefix; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 711 | chatPrefix.append(m_identity).append("CHRONOCHAT-CHATDATA").append(chatroomName.toStdString()); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 712 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 713 | ChatDialog* chatDialog |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 714 | = new ChatDialog(chatroomPrefix, |
| 715 | chatPrefix, |
| 716 | m_localPrefix, |
| 717 | chatroomName.toStdString(), |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 718 | m_nick, |
| 719 | true, |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 720 | m_identity, |
| 721 | this); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 722 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 723 | addChatDialog(chatroomName, chatDialog); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 724 | chatDialog->show(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 725 | |
| 726 | emit addChatroom(chatroomName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 730 | Controller::onStartChatroom2(chronochat::Invitation invitation, bool secured) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 731 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 732 | QString chatroomName = QString::fromStdString(invitation.getChatroom()); |
| 733 | onStartChatroom(chatroomName, secured); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 734 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 735 | ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 736 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 737 | BOOST_ASSERT(it != m_chatDialogList.end()); |
| 738 | it->second->addSyncAnchor(invitation); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 741 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 742 | void |
| 743 | Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data) |
| 744 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 745 | m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName), |
| 746 | QString("<%1>: %2").arg(from).arg(data), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 747 | QSystemTrayIcon::Information, 20000); |
| 748 | m_trayIcon->setIcon(QIcon(":/images/note.png")); |
| 749 | } |
| 750 | |
| 751 | void |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 752 | Controller::onResetIcon() |
| 753 | { |
| 754 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 755 | } |
| 756 | |
| 757 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 758 | Controller::onRemoveChatDialog(const QString& chatroomName) |
| 759 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 760 | emit removeChatroom(chatroomName); |
| 761 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 762 | ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 763 | if (it != m_chatDialogList.end()) { |
| 764 | ChatDialog* deletedChat = it->second; |
| 765 | if (deletedChat) |
| 766 | delete deletedChat; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 767 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 768 | m_chatDialogList.erase(it); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 769 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 770 | QAction* chatAction = m_chatActionList[chatroomName.toStdString()]; |
| 771 | QAction* closeAction = m_closeActionList[chatroomName.toStdString()]; |
| 772 | if (chatAction) |
| 773 | delete chatAction; |
| 774 | if (closeAction) |
| 775 | delete closeAction; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 776 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 777 | m_chatActionList.erase(chatroomName.toStdString()); |
| 778 | m_closeActionList.erase(chatroomName.toStdString()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 779 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 780 | updateMenu(); |
| 781 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | void |
| 785 | Controller::onWarning(const QString& msg) |
| 786 | { |
| 787 | QMessageBox::information(this, tr("ChronoChat"), msg); |
| 788 | } |
| 789 | |
| 790 | void |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 791 | Controller::onNfdError() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 792 | { |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 793 | if (m_isInConnectionDetection) |
| 794 | return; |
| 795 | // begin a thread |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 796 | |
Yingdi Yu | f340118 | 2015-02-02 20:21:07 -0800 | [diff] [blame] | 797 | m_isInConnectionDetection = true; |
| 798 | m_nfdConnectionChecker = new NfdConnectionChecker(this); |
| 799 | |
| 800 | connect(m_nfdConnectionChecker, SIGNAL(nfdConnected()), |
| 801 | this, SLOT(onNfdReconnect())); |
| 802 | connect(this, SIGNAL(shutdownNfdChecker()), |
| 803 | m_nfdConnectionChecker, SLOT(shutdown())); |
| 804 | |
| 805 | m_nfdConnectionChecker->start(); |
| 806 | QMessageBox::information(this, tr("ChronoChat"), "Nfd is not running"); |
| 807 | } |
| 808 | |
| 809 | void |
| 810 | Controller::onNfdReconnect() |
| 811 | { |
| 812 | if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) { |
| 813 | m_nfdConnectionChecker->wait(); |
| 814 | } |
| 815 | delete m_nfdConnectionChecker; |
| 816 | m_nfdConnectionChecker = nullptr; |
| 817 | m_isInConnectionDetection = false; |
| 818 | emit nfdReconnect(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 821 | void |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 822 | Controller::onChatroomInfoRequest(std::string chatroomName, bool isManager) |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 823 | { |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 824 | auto it = m_chatDialogList.find(chatroomName); |
| 825 | if (it != m_chatDialogList.end()) |
| 826 | emit respondChatroomInfoRequest(it->second->getChatroomInfo(), isManager); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 829 | } // namespace chronochat |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 830 | |
| 831 | #if WAF |
| 832 | #include "controller.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 833 | #endif |