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