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