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" |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 16 | //#include "chatroom-discovery.h" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 17 | |
| 18 | #ifndef Q_MOC_RUN |
| 19 | #include <boost/filesystem.hpp> |
| 20 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 21 | #include <ndn-cxx/util/random.hpp> |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 22 | #include "invitation.hpp" |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 23 | #include "cryptopp.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | #include "config.pb.h" |
| 25 | #include "endorse-info.pb.h" |
| 26 | #include "logging.h" |
| 27 | #endif |
| 28 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 29 | // INIT_LOGGER("chronos.Controller"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 30 | |
| 31 | Q_DECLARE_METATYPE(ndn::Name) |
| 32 | Q_DECLARE_METATYPE(ndn::IdentityCertificate) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 33 | Q_DECLARE_METATYPE(Chronos::EndorseInfo) |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 34 | Q_DECLARE_METATYPE(ndn::Interest) |
| 35 | Q_DECLARE_METATYPE(size_t) |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 36 | Q_DECLARE_METATYPE(chronos::ChatroomInfo) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 37 | |
| 38 | namespace chronos { |
| 39 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | |
| 42 | using ndn::Face; |
| 43 | using ndn::IdentityCertificate; |
| 44 | using ndn::OnInterestValidated; |
| 45 | using ndn::OnInterestValidationFailed; |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 46 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 47 | static const uint8_t ROUTING_PREFIX_SEPARATOR[2] = {0xF0, 0x2E}; |
| 48 | |
| 49 | // constructor & destructor |
| 50 | Controller::Controller(shared_ptr<Face> face, |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 51 | QWidget* parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | : QDialog(parent) |
| 53 | , m_face(face) |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 54 | , m_localPrefixDetected(false) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 55 | , m_invitationListenerId(0) |
| 56 | , m_contactManager(m_face) |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 57 | , m_discoveryLogic(m_face, |
| 58 | bind(&Controller::updateDiscoveryList, this, _1, _2)) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 59 | , m_settingDialog(new SettingDialog) |
| 60 | , m_startChatDialog(new StartChatDialog) |
| 61 | , m_profileEditor(new ProfileEditor) |
| 62 | , m_invitationDialog(new InvitationDialog) |
| 63 | , m_contactPanel(new ContactPanel) |
| 64 | , m_browseContactDialog(new BrowseContactDialog) |
| 65 | , m_addContactPanel(new AddContactPanel) |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 66 | , m_chatroomDiscoveryDialog(new ChatroomDiscoveryDialog) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | { |
| 68 | qRegisterMetaType<ndn::Name>("ndn.Name"); |
| 69 | qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 70 | qRegisterMetaType<Chronos::EndorseInfo>("Chronos.EndorseInfo"); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 71 | qRegisterMetaType<ndn::Interest>("ndn.Interest"); |
| 72 | qRegisterMetaType<size_t>("size_t"); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 73 | qRegisterMetaType<chronos::ChatroomInfo>("chronos.Chatroom"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 74 | |
| 75 | connect(this, SIGNAL(localPrefixUpdated(const QString&)), |
| 76 | this, SLOT(onLocalPrefixUpdated(const QString&))); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 77 | connect(this, SIGNAL(invitationInterest(const ndn::Name&, const ndn::Interest&, size_t)), |
| 78 | this, SLOT(onInvitationInterest(const ndn::Name&, const ndn::Interest&, size_t))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 79 | |
| 80 | // Connection to ContactManager |
| 81 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 82 | &m_contactManager, SLOT(onIdentityUpdated(const QString&))); |
| 83 | connect(&m_contactManager, SIGNAL(warning(const QString&)), |
| 84 | this, SLOT(onWarning(const QString&))); |
| 85 | connect(this, SIGNAL(refreshBrowseContact()), |
| 86 | &m_contactManager, SLOT(onRefreshBrowseContact())); |
| 87 | connect(&m_contactManager, SIGNAL(contactInfoFetchFailed(const QString&)), |
| 88 | this, SLOT(onWarning(const QString&))); |
| 89 | connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)), |
| 90 | this, SLOT(onContactIdListReady(const QStringList&))); |
| 91 | |
| 92 | // Connection to SettingDialog |
| 93 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 94 | m_settingDialog, SLOT(onIdentityUpdated(const QString&))); |
| 95 | connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)), |
| 96 | this, SLOT(onIdentityUpdated(const QString&))); |
| 97 | connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)), |
| 98 | this, SLOT(onNickUpdated(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 99 | connect(this, SIGNAL(localPrefixUpdated(const QString&)), |
| 100 | m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&))); |
| 101 | connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)), |
| 102 | this, SLOT(onLocalPrefixConfigured(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 103 | |
| 104 | // Connection to ProfileEditor |
| 105 | connect(this, SIGNAL(closeDBModule()), |
| 106 | m_profileEditor, SLOT(onCloseDBModule())); |
| 107 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 108 | m_profileEditor, SLOT(onIdentityUpdated(const QString&))); |
| 109 | connect(m_profileEditor, SIGNAL(updateProfile()), |
| 110 | &m_contactManager, SLOT(onUpdateProfile())); |
| 111 | |
| 112 | // Connection to StartChatDialog |
| 113 | connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)), |
| 114 | this, SLOT(onStartChatroom(const QString&, bool))); |
| 115 | |
| 116 | // Connection to InvitationDialog |
| 117 | connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)), |
| 118 | this, SLOT(onInvitationResponded(const ndn::Name&, bool))); |
| 119 | |
| 120 | // Connection to AddContactPanel |
| 121 | connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)), |
| 122 | &m_contactManager, SLOT(onFetchContactInfo(const QString&))); |
| 123 | connect(m_addContactPanel, SIGNAL(addContact(const QString&)), |
| 124 | &m_contactManager, SLOT(onAddFetchedContact(const QString&))); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 125 | connect(&m_contactManager, SIGNAL(contactEndorseInfoReady(const Chronos::EndorseInfo&)), |
| 126 | m_addContactPanel, SLOT(onContactEndorseInfoReady(const Chronos::EndorseInfo&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 127 | |
| 128 | |
| 129 | // Connection to BrowseContactDialog |
| 130 | connect(m_browseContactDialog, SIGNAL(directAddClicked()), |
| 131 | this, SLOT(onDirectAdd())); |
| 132 | connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)), |
| 133 | &m_contactManager, SLOT(onFetchIdCert(const QString&))); |
| 134 | connect(m_browseContactDialog, SIGNAL(addContact(const QString&)), |
| 135 | &m_contactManager, SLOT(onAddFetchedContactIdCert(const QString&))); |
| 136 | connect(&m_contactManager, SIGNAL(idCertNameListReady(const QStringList&)), |
| 137 | m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&))); |
| 138 | connect(&m_contactManager, SIGNAL(nameListReady(const QStringList&)), |
| 139 | m_browseContactDialog, SLOT(onNameListReady(const QStringList&))); |
| 140 | connect(&m_contactManager, SIGNAL(idCertReady(const ndn::IdentityCertificate&)), |
| 141 | m_browseContactDialog, SLOT(onIdCertReady(const ndn::IdentityCertificate&))); |
| 142 | |
| 143 | // Connection to ContactPanel |
| 144 | connect(m_contactPanel, SIGNAL(waitForContactList()), |
| 145 | &m_contactManager, SLOT(onWaitForContactList())); |
| 146 | connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)), |
| 147 | &m_contactManager, SLOT(onWaitForContactInfo(const QString&))); |
| 148 | connect(m_contactPanel, SIGNAL(removeContact(const QString&)), |
| 149 | &m_contactManager, SLOT(onRemoveContact(const QString&))); |
| 150 | connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)), |
| 151 | &m_contactManager, SLOT(onUpdateAlias(const QString&, const QString&))); |
| 152 | connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)), |
| 153 | &m_contactManager, SLOT(onUpdateIsIntroducer(const QString&, bool))); |
| 154 | connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)), |
| 155 | &m_contactManager, SLOT(onUpdateEndorseCertificate(const QString&))); |
| 156 | connect(m_contactPanel, SIGNAL(warning(const QString&)), |
| 157 | this, SLOT(onWarning(const QString&))); |
| 158 | connect(this, SIGNAL(closeDBModule()), |
| 159 | m_contactPanel, SLOT(onCloseDBModule())); |
| 160 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 161 | m_contactPanel, SLOT(onIdentityUpdated(const QString&))); |
| 162 | connect(&m_contactManager, SIGNAL(contactAliasListReady(const QStringList&)), |
| 163 | m_contactPanel, SLOT(onContactAliasListReady(const QStringList&))); |
| 164 | connect(&m_contactManager, SIGNAL(contactIdListReady(const QStringList&)), |
| 165 | m_contactPanel, SLOT(onContactIdListReady(const QStringList&))); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 166 | connect(&m_contactManager, SIGNAL(contactInfoReady(const QString&, const QString&, |
| 167 | const QString&, bool)), |
| 168 | m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&, |
| 169 | const QString&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 170 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 171 | // Connection to DiscoveryDialog |
| 172 | connect(this, |
| 173 | SIGNAL(discoverChatroomChanged(const chronos::ChatroomInfo&, bool)), |
| 174 | m_chatroomDiscoveryDialog, |
| 175 | SLOT(onDiscoverChatroomChanged(const chronos::ChatroomInfo&, bool))); |
| 176 | connect(m_chatroomDiscoveryDialog, SIGNAL(startChatroom(const QString&, bool)), |
| 177 | this, SLOT(onStartChatroom(const QString&, bool))); |
| 178 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 179 | initialize(); |
| 180 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 181 | createTrayIcon(); |
| 182 | |
| 183 | onUpdateLocalPrefixAction(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 184 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 185 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 186 | Controller::~Controller() |
| 187 | { |
| 188 | saveConf(); |
| 189 | } |
| 190 | |
| 191 | // public methods |
| 192 | |
| 193 | |
| 194 | // private methods |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 195 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 196 | Controller::getDBName() |
| 197 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 198 | string dbName("chronos-"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 199 | |
| 200 | std::stringstream ss; |
| 201 | { |
| 202 | using namespace CryptoPP; |
| 203 | |
| 204 | SHA256 hash; |
| 205 | StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true, |
| 206 | new HashFilter(hash, new HexEncoder(new FileSink(ss), false))); |
| 207 | } |
| 208 | dbName.append(ss.str()).append(".db"); |
| 209 | |
| 210 | return dbName; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | Controller::openDB() |
| 215 | { |
| 216 | m_db = QSqlDatabase::addDatabase("QSQLITE"); |
| 217 | QString path = (QDir::home().path()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 218 | path.append(QDir::separator()) |
| 219 | .append(".chronos") |
| 220 | .append(QDir::separator()) |
| 221 | .append(getDBName().c_str()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 222 | m_db.setDatabaseName(path); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 223 | |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 224 | m_db.open(); |
| 225 | |
| 226 | // bool ok = m_db.open(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 227 | // _LOG_DEBUG("DB opened: " << std::boolalpha << ok ); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
| 231 | Controller::initialize() |
| 232 | { |
| 233 | loadConf(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 234 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 235 | m_keyChain.createIdentity(m_identity); |
| 236 | |
| 237 | openDB(); |
| 238 | |
| 239 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
| 240 | |
| 241 | setInvitationListener(); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 242 | |
| 243 | m_discoveryLogic.sendDiscoveryInterest(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | Controller::setInvitationListener() |
| 248 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 249 | if (m_invitationListenerId != 0) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 250 | m_face->unsetInterestFilter(m_invitationListenerId); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 251 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 252 | Name invitationPrefix; |
| 253 | Name routingPrefix = getInvitationRoutingPrefix(); |
| 254 | size_t offset = 0; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 255 | if (!routingPrefix.isPrefixOf(m_identity)) { |
| 256 | invitationPrefix.append(routingPrefix).append(ROUTING_PREFIX_SEPARATOR, 2); |
| 257 | offset = routingPrefix.size() + 1; |
| 258 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 259 | invitationPrefix.append(m_identity).append("CHRONOCHAT-INVITATION"); |
| 260 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 261 | m_invitationListenerId = m_face->setInterestFilter(invitationPrefix, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 262 | bind(&Controller::onInvitationInterestWrapper, |
| 263 | this, _1, _2, offset), |
| 264 | bind(&Controller::onInvitationRegisterFailed, |
| 265 | this, _1, _2)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void |
| 269 | Controller::loadConf() |
| 270 | { |
| 271 | namespace fs = boost::filesystem; |
| 272 | |
| 273 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 274 | fs::create_directories (chronosDir); |
| 275 | |
| 276 | std::ifstream is((chronosDir / "config").c_str ()); |
| 277 | ChronoChat::Conf conf; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 278 | if (conf.ParseFromIstream(&is)) { |
| 279 | m_identity.clear(); |
| 280 | m_identity.append(conf.identity()); |
| 281 | if (conf.has_nick()) |
| 282 | m_nick = conf.nick(); |
| 283 | else |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 284 | m_nick = m_identity.get(-1).toUri(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 285 | } |
| 286 | else { |
| 287 | m_identity.clear(); |
| 288 | // TODO: change below to system default; |
| 289 | m_identity.append("chronochat-tmp-identity") |
| 290 | .append(getRandomString()); |
| 291 | |
| 292 | m_nick = m_identity.get(-1).toUri(); |
| 293 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 296 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 297 | Controller::saveConf() |
| 298 | { |
| 299 | namespace fs = boost::filesystem; |
| 300 | |
| 301 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 302 | fs::create_directories (chronosDir); |
| 303 | |
| 304 | std::ofstream os((chronosDir / "config").c_str ()); |
| 305 | ChronoChat::Conf conf; |
| 306 | conf.set_identity(m_identity.toUri()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 307 | if (!m_nick.empty()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 308 | conf.set_nick(m_nick); |
| 309 | conf.SerializeToOstream(&os); |
| 310 | |
| 311 | os.close(); |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | Controller::createActions() |
| 316 | { |
| 317 | m_startChatroom = new QAction(tr("Start new chat"), this); |
| 318 | connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction())); |
| 319 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 320 | m_discoveryAction = new QAction(tr("Ongoing Chatrooms"), this); |
| 321 | connect(m_discoveryAction, SIGNAL(triggered()), this, SLOT(onDiscoveryAction())); |
| 322 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 323 | m_settingsAction = new QAction(tr("Settings"), this); |
| 324 | connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction())); |
| 325 | |
| 326 | m_editProfileAction = new QAction(tr("Edit profile"), this); |
| 327 | connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction())); |
| 328 | |
| 329 | m_contactListAction = new QAction(tr("Contact List"), this); |
| 330 | connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction())); |
| 331 | |
| 332 | m_addContactAction = new QAction(tr("Add contact"), this); |
| 333 | connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction())); |
| 334 | |
| 335 | m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this); |
| 336 | connect(m_updateLocalPrefixAction, SIGNAL(triggered()), this, SLOT(onUpdateLocalPrefixAction())); |
| 337 | |
| 338 | m_minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 339 | connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction())); |
| 340 | |
| 341 | m_quitAction = new QAction(tr("Quit"), this); |
| 342 | connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction())); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 343 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void |
| 347 | Controller::createTrayIcon() |
| 348 | { |
| 349 | createActions(); |
| 350 | |
| 351 | m_trayIconMenu = new QMenu(this); |
| 352 | m_trayIconMenu->addAction(m_startChatroom); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 353 | m_trayIconMenu->addAction(m_discoveryAction); |
| 354 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 355 | m_trayIconMenu->addSeparator(); |
| 356 | m_trayIconMenu->addAction(m_settingsAction); |
| 357 | m_trayIconMenu->addAction(m_editProfileAction); |
| 358 | m_trayIconMenu->addSeparator(); |
| 359 | m_trayIconMenu->addAction(m_contactListAction); |
| 360 | m_trayIconMenu->addAction(m_addContactAction); |
| 361 | m_trayIconMenu->addSeparator(); |
| 362 | m_trayIconMenu->addAction(m_updateLocalPrefixAction); |
| 363 | m_trayIconMenu->addSeparator(); |
| 364 | m_trayIconMenu->addAction(m_minimizeAction); |
| 365 | m_closeMenu = m_trayIconMenu->addMenu("Close chatroom"); |
| 366 | m_closeMenu->setEnabled(false); |
| 367 | m_trayIconMenu->addSeparator(); |
| 368 | m_trayIconMenu->addAction(m_quitAction); |
| 369 | |
| 370 | m_trayIcon = new QSystemTrayIcon(this); |
| 371 | m_trayIcon->setContextMenu(m_trayIconMenu); |
| 372 | |
| 373 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 374 | m_trayIcon->setToolTip("ChronoChat System Tray Icon"); |
| 375 | m_trayIcon->setVisible(true); |
| 376 | } |
| 377 | |
| 378 | void |
| 379 | Controller::updateMenu() |
| 380 | { |
| 381 | QMenu* menu = new QMenu(this); |
| 382 | QMenu* closeMenu = 0; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 383 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 384 | menu->addAction(m_startChatroom); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 385 | menu->addAction(m_discoveryAction); |
| 386 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 387 | menu->addSeparator(); |
| 388 | menu->addAction(m_settingsAction); |
| 389 | menu->addAction(m_editProfileAction); |
| 390 | menu->addSeparator(); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 391 | menu->addAction(m_contactListAction); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 392 | menu->addAction(m_addContactAction); |
| 393 | menu->addSeparator(); |
| 394 | { |
| 395 | ChatActionList::const_iterator it = m_chatActionList.begin(); |
| 396 | ChatActionList::const_iterator end = m_chatActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 397 | if (it != end) { |
| 398 | for (; it != end; it++) |
| 399 | menu->addAction(it->second); |
| 400 | menu->addSeparator(); |
| 401 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 402 | } |
| 403 | menu->addAction(m_updateLocalPrefixAction); |
| 404 | menu->addSeparator(); |
| 405 | menu->addAction(m_minimizeAction); |
| 406 | closeMenu = menu->addMenu("Close chatroom"); |
| 407 | { |
| 408 | ChatActionList::const_iterator it = m_closeActionList.begin(); |
| 409 | ChatActionList::const_iterator end = m_closeActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 410 | if (it == end) |
| 411 | closeMenu->setEnabled(false); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 412 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 413 | for (; it != end; it++) |
| 414 | closeMenu->addAction(it->second); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 415 | } |
| 416 | menu->addSeparator(); |
| 417 | menu->addAction(m_quitAction); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 418 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 419 | m_trayIcon->setContextMenu(menu); |
| 420 | delete m_trayIconMenu; |
| 421 | m_trayIconMenu = menu; |
| 422 | m_closeMenu = closeMenu; |
| 423 | } |
| 424 | |
| 425 | void |
| 426 | Controller::onLocalPrefix(const Interest& interest, Data& data) |
| 427 | { |
Yingdi Yu | 106be86 | 2014-08-29 09:03:18 -0700 | [diff] [blame] | 428 | QString localPrefixStr("/private/local"); |
| 429 | Name prefix; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 430 | |
Yingdi Yu | 106be86 | 2014-08-29 09:03:18 -0700 | [diff] [blame] | 431 | Block contentBlock = data.getContent(); |
| 432 | try { |
| 433 | contentBlock.parse(); |
| 434 | |
| 435 | for (Block::element_const_iterator it = contentBlock.elements_begin(); |
| 436 | it != contentBlock.elements_end(); it++) { |
| 437 | Name candidate; |
| 438 | candidate.wireDecode(*it); |
| 439 | if (candidate.isPrefixOf(m_identity)) { |
| 440 | prefix = candidate; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (prefix.empty()) { |
| 446 | if (contentBlock.elements_begin() != contentBlock.elements_end()) |
| 447 | prefix.wireDecode(*contentBlock.elements_begin()); |
| 448 | else |
| 449 | prefix = Name("/private/local"); |
| 450 | } |
| 451 | |
| 452 | localPrefixStr = QString::fromStdString(prefix.toUri()); |
| 453 | } |
| 454 | catch (Block::Error& e) { |
| 455 | prefix = Name("/private/local"); |
| 456 | } |
| 457 | |
| 458 | if (m_localPrefix.empty() || m_localPrefix != prefix) { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 459 | emit localPrefixUpdated(localPrefixStr); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 460 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void |
| 464 | Controller::onLocalPrefixTimeout(const Interest& interest) |
| 465 | { |
| 466 | QString localPrefixStr("/private/local"); |
| 467 | |
| 468 | Name localPrefix(localPrefixStr.toStdString()); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 469 | if (m_localPrefix.empty() || m_localPrefix != localPrefix) { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 470 | emit localPrefixUpdated(localPrefixStr); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 471 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 475 | Controller::onInvitationInterestWrapper(const Name& prefix, |
| 476 | const Interest& interest, |
| 477 | size_t routingPrefixOffset) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 478 | { |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 479 | emit invitationInterest(prefix, interest, routingPrefixOffset); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 483 | Controller::onInvitationRegisterFailed(const Name& prefix, const string& failInfo) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 484 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 485 | // _LOG_DEBUG("Controller::onInvitationRegisterFailed: " << failInfo); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void |
| 489 | Controller::onInvitationValidated(const shared_ptr<const Interest>& interest) |
| 490 | { |
| 491 | Invitation invitation(interest->getName()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 492 | // Should be obtained via a method of ContactManager. |
| 493 | string alias = invitation.getInviterCertificate().getPublicKeyName().getPrefix(-1).toUri(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 494 | |
| 495 | m_invitationDialog->setInvitation(alias, invitation.getChatroom(), interest->getName()); |
| 496 | m_invitationDialog->show(); |
| 497 | } |
| 498 | |
| 499 | void |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 500 | Controller::onInvitationValidationFailed(const shared_ptr<const Interest>& interest, |
| 501 | string failureInfo) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 502 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 503 | // _LOG_DEBUG("Invitation: " << interest->getName() << |
| 504 | // " cannot not be validated due to: " << failureInfo); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 507 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 508 | Controller::getRandomString() |
| 509 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 510 | uint32_t r = ndn::random::generateWord32(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 511 | std::stringstream ss; |
| 512 | { |
| 513 | using namespace CryptoPP; |
| 514 | StringSource(reinterpret_cast<uint8_t*>(&r), 4, true, |
| 515 | new HexEncoder(new FileSink(ss), false)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 516 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 517 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 518 | // for (int i = 0; i < 8; i++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 519 | // { |
| 520 | // uint32_t t = r & mask; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 521 | // if (t < 10) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 522 | // ss << static_cast<char>(t + 0x30); |
| 523 | // else |
| 524 | // ss << static_cast<char>(t + 0x57); |
| 525 | // r = r >> 4; |
| 526 | // } |
| 527 | |
| 528 | return ss.str(); |
| 529 | } |
| 530 | |
| 531 | ndn::Name |
| 532 | Controller::getInvitationRoutingPrefix() |
| 533 | { |
| 534 | return Name("/ndn/broadcast"); |
| 535 | } |
| 536 | |
| 537 | void |
| 538 | Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog) |
| 539 | { |
| 540 | m_chatDialogList[chatroomName.toStdString()] = chatDialog; |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 541 | m_discoveryLogic.addLocalChatroom(*chatDialog->getChatroomInfo()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 542 | connect(chatDialog, SIGNAL(closeChatDialog(const QString&)), |
| 543 | this, SLOT(onRemoveChatDialog(const QString&))); |
| 544 | connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)), |
| 545 | this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&))); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 546 | connect(chatDialog, SIGNAL(resetIcon()), |
| 547 | this, SLOT(onResetIcon())); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 548 | connect(chatDialog, SIGNAL(rosterChanged(const chronos::ChatroomInfo&)), |
| 549 | this, SLOT(onRosterChanged(const chronos::ChatroomInfo&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 550 | connect(this, SIGNAL(localPrefixUpdated(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 551 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 552 | connect(this, SIGNAL(localPrefixConfigured(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 553 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 554 | |
| 555 | QAction* chatAction = new QAction(chatroomName, this); |
| 556 | m_chatActionList[chatroomName.toStdString()] = chatAction; |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 557 | connect(chatAction, SIGNAL(triggered()), |
Yingdi Yu | e8323b6 | 2014-09-02 17:24:15 -0700 | [diff] [blame] | 558 | chatDialog, SLOT(onShow())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 559 | |
| 560 | QAction* closeAction = new QAction(chatroomName, this); |
| 561 | m_closeActionList[chatroomName.toStdString()] = closeAction; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 562 | connect(closeAction, SIGNAL(triggered()), |
| 563 | chatDialog, SLOT(shutdown())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 564 | |
| 565 | updateMenu(); |
| 566 | } |
| 567 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 568 | void |
| 569 | Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd) |
| 570 | { |
| 571 | emit discoverChatroomChanged(info, isAdd); |
| 572 | } |
| 573 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 574 | void |
| 575 | Controller::onIdentityUpdated(const QString& identity) |
| 576 | { |
| 577 | Name identityName(identity.toStdString()); |
| 578 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 579 | while (!m_chatDialogList.empty()) { |
| 580 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
| 581 | onRemoveChatDialog(QString::fromStdString(it->first)); |
| 582 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 583 | |
| 584 | m_identity = identityName; |
| 585 | m_keyChain.createIdentity(m_identity); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 586 | setInvitationListener(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 587 | |
| 588 | emit closeDBModule(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 589 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 590 | QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued())); |
| 591 | |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | Controller::onIdentityUpdatedContinued() |
| 596 | { |
| 597 | QString connection = m_db.connectionName(); |
| 598 | // _LOG_DEBUG("connection name: " << connection.toStdString()); |
| 599 | QSqlDatabase::removeDatabase(connection); |
| 600 | m_db.close(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 601 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 602 | openDB(); |
| 603 | |
| 604 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
| 605 | } |
| 606 | |
| 607 | void |
| 608 | Controller::onContactIdListReady(const QStringList& list) |
| 609 | { |
| 610 | ContactList contactList; |
| 611 | |
| 612 | m_contactManager.getContactList(contactList); |
| 613 | m_validator.cleanTrustAnchor(); |
| 614 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 615 | for (ContactList::const_iterator it = contactList.begin(); it != contactList.end(); it++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 616 | m_validator.addTrustAnchor((*it)->getPublicKeyName(), (*it)->getPublicKey()); |
| 617 | |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | Controller::onNickUpdated(const QString& nick) |
| 622 | { |
| 623 | m_nick = nick.toStdString(); |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | Controller::onLocalPrefixUpdated(const QString& localPrefix) |
| 628 | { |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 629 | QString privateLocalPrefix("/private/local"); |
| 630 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 631 | m_localPrefix = Name(localPrefix.toStdString()); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 632 | |
| 633 | if (privateLocalPrefix != localPrefix) |
| 634 | m_localPrefixDetected = true; |
| 635 | else |
| 636 | m_localPrefixDetected = false; |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | Controller::onLocalPrefixConfigured(const QString& prefix) |
| 641 | { |
| 642 | if (!m_localPrefixDetected) { |
| 643 | m_localPrefix = Name(prefix.toStdString()); |
| 644 | emit localPrefixConfigured(prefix); |
| 645 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | void |
| 649 | Controller::onStartChatAction() |
| 650 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 651 | string chatroom = "chatroom-" + getRandomString(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 652 | |
| 653 | m_startChatDialog->setChatroom(chatroom); |
| 654 | m_startChatDialog->show(); |
| 655 | m_startChatDialog->raise(); |
| 656 | } |
| 657 | |
| 658 | void |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 659 | Controller::onDiscoveryAction() |
| 660 | { |
| 661 | m_discoveryLogic.sendDiscoveryInterest(); |
| 662 | |
| 663 | m_chatroomDiscoveryDialog->updateChatroomList(); |
| 664 | m_chatroomDiscoveryDialog->show(); |
| 665 | m_chatroomDiscoveryDialog->raise(); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 670 | Controller::onSettingsAction() |
| 671 | { |
| 672 | m_settingDialog->setNick(QString(m_nick.c_str())); |
| 673 | m_settingDialog->show(); |
| 674 | m_settingDialog->raise(); |
| 675 | } |
| 676 | |
| 677 | void |
| 678 | Controller::onProfileEditorAction() |
| 679 | { |
| 680 | m_profileEditor->show(); |
| 681 | m_profileEditor->raise(); |
| 682 | } |
| 683 | |
| 684 | void |
| 685 | Controller::onAddContactAction() |
| 686 | { |
| 687 | emit refreshBrowseContact(); |
| 688 | m_browseContactDialog->show(); |
| 689 | m_browseContactDialog->raise(); |
| 690 | } |
| 691 | |
| 692 | void |
| 693 | Controller::onContactListAction() |
| 694 | { |
| 695 | m_contactPanel->show(); |
| 696 | m_contactPanel->raise(); |
| 697 | } |
| 698 | |
| 699 | void |
| 700 | Controller::onDirectAdd() |
| 701 | { |
| 702 | m_addContactPanel->show(); |
| 703 | m_addContactPanel->raise(); |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | Controller::onUpdateLocalPrefixAction() |
| 708 | { |
| 709 | // Name interestName(); |
Yingdi Yu | 106be86 | 2014-08-29 09:03:18 -0700 | [diff] [blame] | 710 | Interest interest("/localhop/ndn-autoconf/routable-prefixes"); |
Yingdi Yu | a787672 | 2014-03-25 14:46:55 -0700 | [diff] [blame] | 711 | interest.setInterestLifetime(time::milliseconds(1000)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 712 | interest.setMustBeFresh(true); |
| 713 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 714 | m_face->expressInterest(interest, |
| 715 | bind(&Controller::onLocalPrefix, this, _1, _2), |
| 716 | bind(&Controller::onLocalPrefixTimeout, this, _1)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | void |
| 720 | Controller::onMinimizeAction() |
| 721 | { |
| 722 | m_settingDialog->hide(); |
| 723 | m_startChatDialog->hide(); |
| 724 | m_profileEditor->hide(); |
| 725 | m_invitationDialog->hide(); |
| 726 | m_addContactPanel->hide(); |
| 727 | |
| 728 | ChatDialogList::iterator it = m_chatDialogList.begin(); |
| 729 | ChatDialogList::iterator end = m_chatDialogList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 730 | for (; it != end; it++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 731 | it->second->hide(); |
| 732 | } |
| 733 | |
| 734 | void |
| 735 | Controller::onQuitAction() |
| 736 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 737 | while (!m_chatDialogList.empty()) { |
| 738 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 739 | it->second->shutdown(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 740 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 741 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 742 | if (m_invitationListenerId != 0) |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 743 | m_face->unsetInterestFilter(m_invitationListenerId); |
| 744 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 745 | delete m_settingDialog; |
| 746 | delete m_startChatDialog; |
| 747 | delete m_profileEditor; |
| 748 | delete m_invitationDialog; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 749 | delete m_browseContactDialog; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 750 | delete m_addContactPanel; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 751 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 752 | m_face->getIoService().stop(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 753 | |
| 754 | QApplication::quit(); |
| 755 | } |
| 756 | |
| 757 | void |
| 758 | Controller::onStartChatroom(const QString& chatroomName, bool secured) |
| 759 | { |
| 760 | Name chatroomPrefix; |
| 761 | chatroomPrefix.append("ndn") |
| 762 | .append("broadcast") |
| 763 | .append("ChronoChat") |
| 764 | .append(chatroomName.toStdString()); |
| 765 | |
| 766 | // check if the chatroom exists |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 767 | if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) { |
| 768 | QMessageBox::information(this, tr("ChronoChat"), |
| 769 | tr("You are creating an existing chatroom." |
| 770 | "You can check it in the context memu.")); |
| 771 | return; |
| 772 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 773 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 774 | // TODO: We should create a chatroom specific key/cert |
| 775 | //(which should be created in the first half of this method |
| 776 | //, but let's use the default one for now. |
| 777 | // std::cout << "start chat room localprefix: " << m_localPrefix.toUri() << std::endl; |
| 778 | shared_ptr<IdentityCertificate> idCert |
| 779 | = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 780 | |
| 781 | Name chatPrefix; |
| 782 | chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(chatroomName.toStdString()); |
| 783 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 784 | ChatDialog* chatDialog |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 785 | = new ChatDialog(chatroomPrefix, |
| 786 | chatPrefix, |
| 787 | m_localPrefix, |
| 788 | chatroomName.toStdString(), |
| 789 | m_nick); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 790 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 791 | addChatDialog(chatroomName, chatDialog); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 792 | chatDialog->show(); |
| 793 | } |
| 794 | |
| 795 | void |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 796 | Controller::onInvitationResponded(const ndn::Name& invitationName, bool accepted) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 797 | { |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 798 | shared_ptr<Data> response = make_shared<Data>(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 799 | shared_ptr<IdentityCertificate> chatroomCert; |
| 800 | |
| 801 | // generate reply; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 802 | if (accepted) { |
| 803 | Name responseName = invitationName; |
| 804 | responseName.append(m_localPrefix.wireEncode()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 805 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 806 | response->setName(responseName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 807 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 808 | // We should create a particular certificate for this chatroom, |
| 809 | //but let's use default one for now. |
| 810 | chatroomCert |
| 811 | = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 812 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 813 | response->setContent(chatroomCert->wireEncode()); |
| 814 | response->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 815 | } |
| 816 | else { |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 817 | response->setName(invitationName); |
| 818 | response->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 821 | m_keyChain.signByIdentity(*response, m_identity); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 822 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 823 | // Check if we need a wrapper |
| 824 | Name invitationRoutingPrefix = getInvitationRoutingPrefix(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 825 | if (invitationRoutingPrefix.isPrefixOf(m_identity)) |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 826 | m_face->put(*response); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 827 | else { |
| 828 | Name wrappedName; |
| 829 | wrappedName.append(invitationRoutingPrefix) |
| 830 | .append(ROUTING_PREFIX_SEPARATOR, 2) |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 831 | .append(response->getName()); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 832 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 833 | // _LOG_DEBUG("onInvitationResponded: prepare reply " << wrappedName); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 834 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 835 | shared_ptr<Data> wrappedData = make_shared<Data>(wrappedName); |
| 836 | wrappedData->setContent(response->wireEncode()); |
| 837 | wrappedData->setFreshnessPeriod(time::milliseconds(1000)); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 838 | |
Yingdi Yu | 6c3c596 | 2014-09-09 16:52:38 -0700 | [diff] [blame] | 839 | m_keyChain.signByIdentity(*wrappedData, m_identity); |
| 840 | m_face->put(*wrappedData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 841 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 842 | |
| 843 | // create chatroom |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 844 | if (accepted) { |
| 845 | Invitation invitation(invitationName); |
| 846 | Name chatroomPrefix; |
| 847 | chatroomPrefix.append("ndn") |
| 848 | .append("broadcast") |
| 849 | .append("ChronoChat") |
| 850 | .append(invitation.getChatroom()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 851 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 852 | //We should create a chatroom specific key/cert |
| 853 | //(which should be created in the first half of this method, |
| 854 | //but let's use the default one for now. |
| 855 | shared_ptr<IdentityCertificate> idCert |
| 856 | = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(m_identity)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 857 | |
| 858 | Name chatPrefix; |
| 859 | chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(invitation.getChatroom()); |
| 860 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 861 | ChatDialog* chatDialog |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame^] | 862 | = new ChatDialog(chatroomPrefix, |
| 863 | chatPrefix, |
| 864 | m_localPrefix, |
| 865 | invitation.getChatroom(), |
| 866 | m_nick, |
| 867 | true); |
| 868 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 869 | chatDialog->addSyncAnchor(invitation); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 870 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 871 | addChatDialog(QString::fromStdString(invitation.getChatroom()), chatDialog); |
| 872 | chatDialog->show(); |
| 873 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | void |
| 877 | Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data) |
| 878 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 879 | m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName), |
| 880 | QString("<%1>: %2").arg(from).arg(data), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 881 | QSystemTrayIcon::Information, 20000); |
| 882 | m_trayIcon->setIcon(QIcon(":/images/note.png")); |
| 883 | } |
| 884 | |
| 885 | void |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 886 | Controller::onResetIcon() |
| 887 | { |
| 888 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 889 | } |
| 890 | |
| 891 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 892 | Controller::onRemoveChatDialog(const QString& chatroomName) |
| 893 | { |
| 894 | ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString()); |
| 895 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 896 | if (it != m_chatDialogList.end()) { |
| 897 | ChatDialog* deletedChat = it->second; |
| 898 | if (deletedChat) |
| 899 | delete deletedChat; |
| 900 | m_chatDialogList.erase(it); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 901 | m_discoveryLogic.removeLocalChatroom(Name::Component(chatroomName.toStdString())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 902 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 903 | QAction* chatAction = m_chatActionList[chatroomName.toStdString()]; |
| 904 | QAction* closeAction = m_closeActionList[chatroomName.toStdString()]; |
| 905 | if (chatAction) |
| 906 | delete chatAction; |
| 907 | if (closeAction) |
| 908 | delete closeAction; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 909 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 910 | m_chatActionList.erase(chatroomName.toStdString()); |
| 911 | m_closeActionList.erase(chatroomName.toStdString()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 912 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 913 | updateMenu(); |
| 914 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | void |
| 918 | Controller::onWarning(const QString& msg) |
| 919 | { |
| 920 | QMessageBox::information(this, tr("ChronoChat"), msg); |
| 921 | } |
| 922 | |
| 923 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 924 | Controller::onError(const QString& msg) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 925 | { |
| 926 | QMessageBox::critical(this, tr("ChronoChat"), msg, QMessageBox::Ok); |
| 927 | exit(1); |
| 928 | } |
| 929 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 930 | void |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 931 | Controller::onInvitationInterest(const ndn::Name& prefix, |
| 932 | const ndn::Interest& interest, |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 933 | size_t routingPrefixOffset) |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 934 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 935 | // _LOG_DEBUG("onInvitationInterest: " << interest.getName()); |
| 936 | shared_ptr<Interest> invitationInterest = |
| 937 | make_shared<Interest>(boost::cref(interest.getName().getSubName(routingPrefixOffset))); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 938 | |
| 939 | // check if the chatroom already exists; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 940 | try { |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 941 | Invitation invitation(invitationInterest->getName()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 942 | if (m_chatDialogList.find(invitation.getChatroom()) != m_chatDialogList.end()) |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 943 | return; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 944 | } |
| 945 | catch (Invitation::Error& e) { |
| 946 | // Cannot parse the invitation; |
| 947 | return; |
| 948 | } |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 949 | |
| 950 | OnInterestValidated onValidated = bind(&Controller::onInvitationValidated, this, _1); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 951 | OnInterestValidationFailed onValidationFailed = bind(&Controller::onInvitationValidationFailed, |
| 952 | this, _1, _2); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 953 | m_validator.validate(*invitationInterest, onValidated, onValidationFailed); |
| 954 | } |
| 955 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 956 | void |
| 957 | Controller::onRosterChanged(const chronos::ChatroomInfo& info) |
| 958 | { |
| 959 | m_discoveryLogic.addLocalChatroom(info); |
| 960 | } |
| 961 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 962 | } // namespace chronos |
| 963 | |
| 964 | #if WAF |
| 965 | #include "controller.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 966 | // #include "controller.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 967 | #endif |