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