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 | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 28 | INIT_LOGGER("chronochat.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 | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 32 | Q_DECLARE_METATYPE(chronochat::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) |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 35 | Q_DECLARE_METATYPE(chronochat::ChatroomInfo) |
| 36 | Q_DECLARE_METATYPE(chronochat::Invitation) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 37 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 38 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 39 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 40 | using std::string; |
| 41 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 42 | // constructor & destructor |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 43 | Controller::Controller(QWidget* parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 44 | : QDialog(parent) |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 45 | , m_localPrefixDetected(false) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 46 | , m_settingDialog(new SettingDialog) |
| 47 | , m_startChatDialog(new StartChatDialog) |
| 48 | , m_profileEditor(new ProfileEditor) |
| 49 | , m_invitationDialog(new InvitationDialog) |
| 50 | , m_contactPanel(new ContactPanel) |
| 51 | , m_browseContactDialog(new BrowseContactDialog) |
| 52 | , m_addContactPanel(new AddContactPanel) |
| 53 | { |
| 54 | qRegisterMetaType<ndn::Name>("ndn.Name"); |
| 55 | qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate"); |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 56 | qRegisterMetaType<chronochat::EndorseInfo>("chronochat.EndorseInfo"); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 57 | qRegisterMetaType<ndn::Interest>("ndn.Interest"); |
| 58 | qRegisterMetaType<size_t>("size_t"); |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 59 | qRegisterMetaType<chronochat::ChatroomInfo>("chronos.Chatroom"); |
| 60 | qRegisterMetaType<chronochat::Invitation>("chronos.Invitation"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | |
| 62 | // Connection to ContactManager |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 63 | connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 64 | this, SLOT(onWarning(const QString&))); |
| 65 | connect(this, SIGNAL(refreshBrowseContact()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 66 | m_backend.getContactManager(), SLOT(onRefreshBrowseContact())); |
| 67 | connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 68 | this, SLOT(onWarning(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 69 | |
| 70 | // Connection to SettingDialog |
| 71 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 72 | m_settingDialog, SLOT(onIdentityUpdated(const QString&))); |
| 73 | connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)), |
| 74 | this, SLOT(onIdentityUpdated(const QString&))); |
| 75 | connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)), |
| 76 | this, SLOT(onNickUpdated(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 77 | connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)), |
| 78 | this, SLOT(onLocalPrefixConfigured(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 79 | |
| 80 | // Connection to ProfileEditor |
| 81 | connect(this, SIGNAL(closeDBModule()), |
| 82 | m_profileEditor, SLOT(onCloseDBModule())); |
| 83 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 84 | m_profileEditor, SLOT(onIdentityUpdated(const QString&))); |
| 85 | connect(m_profileEditor, SIGNAL(updateProfile()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 86 | m_backend.getContactManager(), SLOT(onUpdateProfile())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 87 | |
| 88 | // Connection to StartChatDialog |
| 89 | connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)), |
| 90 | this, SLOT(onStartChatroom(const QString&, bool))); |
| 91 | |
| 92 | // Connection to InvitationDialog |
| 93 | connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 94 | &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 95 | |
| 96 | // Connection to AddContactPanel |
| 97 | connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 98 | m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 99 | connect(m_addContactPanel, SIGNAL(addContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 100 | m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&))); |
| 101 | connect(m_backend.getContactManager(), |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 102 | SIGNAL(contactEndorseInfoReady(const chronochat::EndorseInfo&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 103 | m_addContactPanel, |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 104 | SLOT(onContactEndorseInfoReady(const chronochat::EndorseInfo&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 105 | |
| 106 | |
| 107 | // Connection to BrowseContactDialog |
| 108 | connect(m_browseContactDialog, SIGNAL(directAddClicked()), |
| 109 | this, SLOT(onDirectAdd())); |
| 110 | connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 111 | m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 112 | connect(m_browseContactDialog, SIGNAL(addContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 113 | m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&))); |
| 114 | connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 115 | m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 116 | connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 117 | m_browseContactDialog, SLOT(onNameListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 118 | connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::IdentityCertificate&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 119 | m_browseContactDialog, SLOT(onIdCertReady(const ndn::IdentityCertificate&))); |
| 120 | |
| 121 | // Connection to ContactPanel |
| 122 | connect(m_contactPanel, SIGNAL(waitForContactList()), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 123 | m_backend.getContactManager(), SLOT(onWaitForContactList())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 124 | connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 125 | m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 126 | connect(m_contactPanel, SIGNAL(removeContact(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 127 | m_backend.getContactManager(), SLOT(onRemoveContact(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 128 | connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 129 | m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 130 | connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 131 | m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 132 | connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)), |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 133 | m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 134 | connect(m_contactPanel, SIGNAL(warning(const QString&)), |
| 135 | this, SLOT(onWarning(const QString&))); |
| 136 | connect(this, SIGNAL(closeDBModule()), |
| 137 | m_contactPanel, SLOT(onCloseDBModule())); |
| 138 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 139 | m_contactPanel, SLOT(onIdentityUpdated(const QString&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 140 | connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | m_contactPanel, SLOT(onContactAliasListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 142 | connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 143 | m_contactPanel, SLOT(onContactIdListReady(const QStringList&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 144 | connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&, |
| 145 | const QString&, bool)), |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 146 | m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&, |
| 147 | const QString&, bool))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 148 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 149 | // Connection to backend thread |
| 150 | connect(this, SIGNAL(shutdownBackend()), |
| 151 | &m_backend, SLOT(shutdown())); |
| 152 | connect(this, SIGNAL(updateLocalPrefix()), |
| 153 | &m_backend, SLOT(onUpdateLocalPrefixAction())); |
| 154 | connect(this, SIGNAL(identityUpdated(const QString&)), |
| 155 | &m_backend, SLOT(onIdentityChanged(const QString&))); |
| 156 | connect(this, SIGNAL(addChatroom(QString)), |
| 157 | &m_backend, SLOT(addChatroom(QString))); |
| 158 | connect(this, SIGNAL(removeChatroom(QString)), |
| 159 | &m_backend, SLOT(removeChatroom(QString))); |
| 160 | |
| 161 | // Thread notifications: |
| 162 | // on local prefix udpated: |
| 163 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
| 164 | this, SLOT(onLocalPrefixUpdated(const QString&))); |
| 165 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
| 166 | m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&))); |
| 167 | |
| 168 | // on invitation validated: |
| 169 | connect(&m_backend, SIGNAL(invitaionValidated(QString, QString, ndn::Name)), |
| 170 | m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name))); |
| 171 | |
| 172 | // on invitation accepted: |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 173 | connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronochat::Invitation, bool)), |
| 174 | this, SLOT(onStartChatroom2(chronochat::Invitation, bool))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 175 | |
| 176 | m_backend.start(); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 177 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 178 | initialize(); |
| 179 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 180 | createTrayIcon(); |
| 181 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 182 | emit updateLocalPrefix(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 183 | } |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 184 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 185 | Controller::~Controller() |
| 186 | { |
| 187 | saveConf(); |
| 188 | } |
| 189 | |
| 190 | // public methods |
| 191 | |
| 192 | |
| 193 | // private methods |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 194 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 195 | Controller::getDBName() |
| 196 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 197 | string dbName("chronos-"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 198 | |
| 199 | std::stringstream ss; |
| 200 | { |
| 201 | using namespace CryptoPP; |
| 202 | |
| 203 | SHA256 hash; |
| 204 | StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true, |
| 205 | new HashFilter(hash, new HexEncoder(new FileSink(ss), false))); |
| 206 | } |
| 207 | dbName.append(ss.str()).append(".db"); |
| 208 | |
| 209 | return dbName; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | Controller::openDB() |
| 214 | { |
| 215 | m_db = QSqlDatabase::addDatabase("QSQLITE"); |
| 216 | QString path = (QDir::home().path()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 217 | path.append(QDir::separator()) |
| 218 | .append(".chronos") |
| 219 | .append(QDir::separator()) |
| 220 | .append(getDBName().c_str()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 221 | m_db.setDatabaseName(path); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 222 | |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 223 | m_db.open(); |
| 224 | |
| 225 | // bool ok = m_db.open(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 226 | // _LOG_DEBUG("DB opened: " << std::boolalpha << ok ); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void |
| 230 | Controller::initialize() |
| 231 | { |
| 232 | loadConf(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 233 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 234 | openDB(); |
| 235 | |
| 236 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void |
| 240 | Controller::loadConf() |
| 241 | { |
| 242 | namespace fs = boost::filesystem; |
| 243 | |
| 244 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 245 | fs::create_directories (chronosDir); |
| 246 | |
| 247 | std::ifstream is((chronosDir / "config").c_str ()); |
| 248 | ChronoChat::Conf conf; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 249 | if (conf.ParseFromIstream(&is)) { |
| 250 | m_identity.clear(); |
| 251 | m_identity.append(conf.identity()); |
| 252 | if (conf.has_nick()) |
| 253 | m_nick = conf.nick(); |
| 254 | else |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 255 | m_nick = m_identity.get(-1).toUri(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 256 | } |
| 257 | else { |
| 258 | m_identity.clear(); |
| 259 | // TODO: change below to system default; |
| 260 | m_identity.append("chronochat-tmp-identity") |
| 261 | .append(getRandomString()); |
| 262 | |
| 263 | m_nick = m_identity.get(-1).toUri(); |
| 264 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 267 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 268 | Controller::saveConf() |
| 269 | { |
| 270 | namespace fs = boost::filesystem; |
| 271 | |
| 272 | fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos"; |
| 273 | fs::create_directories (chronosDir); |
| 274 | |
| 275 | std::ofstream os((chronosDir / "config").c_str ()); |
| 276 | ChronoChat::Conf conf; |
| 277 | conf.set_identity(m_identity.toUri()); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 278 | if (!m_nick.empty()) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 279 | conf.set_nick(m_nick); |
| 280 | conf.SerializeToOstream(&os); |
| 281 | |
| 282 | os.close(); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | Controller::createActions() |
| 287 | { |
| 288 | m_startChatroom = new QAction(tr("Start new chat"), this); |
| 289 | connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction())); |
| 290 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 291 | m_discoveryAction = new QAction(tr("Ongoing Chatrooms"), this); |
| 292 | connect(m_discoveryAction, SIGNAL(triggered()), this, SLOT(onDiscoveryAction())); |
| 293 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 294 | m_settingsAction = new QAction(tr("Settings"), this); |
| 295 | connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction())); |
| 296 | |
| 297 | m_editProfileAction = new QAction(tr("Edit profile"), this); |
| 298 | connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction())); |
| 299 | |
| 300 | m_contactListAction = new QAction(tr("Contact List"), this); |
| 301 | connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction())); |
| 302 | |
| 303 | m_addContactAction = new QAction(tr("Add contact"), this); |
| 304 | connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction())); |
| 305 | |
| 306 | m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 307 | connect(m_updateLocalPrefixAction, SIGNAL(triggered()), |
| 308 | &m_backend, SLOT(onUpdateLocalPrefixAction())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 309 | |
| 310 | m_minimizeAction = new QAction(tr("Mi&nimize"), this); |
| 311 | connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction())); |
| 312 | |
| 313 | m_quitAction = new QAction(tr("Quit"), this); |
| 314 | connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction())); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 315 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void |
| 319 | Controller::createTrayIcon() |
| 320 | { |
| 321 | createActions(); |
| 322 | |
| 323 | m_trayIconMenu = new QMenu(this); |
| 324 | m_trayIconMenu->addAction(m_startChatroom); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 325 | // m_trayIconMenu->addAction(m_discoveryAction); // disable discovery temporarily |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 326 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 327 | m_trayIconMenu->addSeparator(); |
| 328 | m_trayIconMenu->addAction(m_settingsAction); |
| 329 | m_trayIconMenu->addAction(m_editProfileAction); |
| 330 | m_trayIconMenu->addSeparator(); |
| 331 | m_trayIconMenu->addAction(m_contactListAction); |
| 332 | m_trayIconMenu->addAction(m_addContactAction); |
| 333 | m_trayIconMenu->addSeparator(); |
| 334 | m_trayIconMenu->addAction(m_updateLocalPrefixAction); |
| 335 | m_trayIconMenu->addSeparator(); |
| 336 | m_trayIconMenu->addAction(m_minimizeAction); |
| 337 | m_closeMenu = m_trayIconMenu->addMenu("Close chatroom"); |
| 338 | m_closeMenu->setEnabled(false); |
| 339 | m_trayIconMenu->addSeparator(); |
| 340 | m_trayIconMenu->addAction(m_quitAction); |
| 341 | |
| 342 | m_trayIcon = new QSystemTrayIcon(this); |
| 343 | m_trayIcon->setContextMenu(m_trayIconMenu); |
| 344 | |
| 345 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 346 | m_trayIcon->setToolTip("ChronoChat System Tray Icon"); |
| 347 | m_trayIcon->setVisible(true); |
| 348 | } |
| 349 | |
| 350 | void |
| 351 | Controller::updateMenu() |
| 352 | { |
| 353 | QMenu* menu = new QMenu(this); |
| 354 | QMenu* closeMenu = 0; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 355 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 356 | menu->addAction(m_startChatroom); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 357 | // menu->addAction(m_discoveryAction); |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 358 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 359 | menu->addSeparator(); |
| 360 | menu->addAction(m_settingsAction); |
| 361 | menu->addAction(m_editProfileAction); |
| 362 | menu->addSeparator(); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 363 | menu->addAction(m_contactListAction); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 364 | menu->addAction(m_addContactAction); |
| 365 | menu->addSeparator(); |
| 366 | { |
| 367 | ChatActionList::const_iterator it = m_chatActionList.begin(); |
| 368 | ChatActionList::const_iterator end = m_chatActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 369 | if (it != end) { |
| 370 | for (; it != end; it++) |
| 371 | menu->addAction(it->second); |
| 372 | menu->addSeparator(); |
| 373 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 374 | } |
| 375 | menu->addAction(m_updateLocalPrefixAction); |
| 376 | menu->addSeparator(); |
| 377 | menu->addAction(m_minimizeAction); |
| 378 | closeMenu = menu->addMenu("Close chatroom"); |
| 379 | { |
| 380 | ChatActionList::const_iterator it = m_closeActionList.begin(); |
| 381 | ChatActionList::const_iterator end = m_closeActionList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 382 | if (it == end) |
| 383 | closeMenu->setEnabled(false); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 384 | else |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 385 | for (; it != end; it++) |
| 386 | closeMenu->addAction(it->second); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 387 | } |
| 388 | menu->addSeparator(); |
| 389 | menu->addAction(m_quitAction); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 390 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 391 | m_trayIcon->setContextMenu(menu); |
| 392 | delete m_trayIconMenu; |
| 393 | m_trayIconMenu = menu; |
| 394 | m_closeMenu = closeMenu; |
| 395 | } |
| 396 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 397 | string |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 398 | Controller::getRandomString() |
| 399 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 400 | uint32_t r = ndn::random::generateWord32(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 401 | std::stringstream ss; |
| 402 | { |
| 403 | using namespace CryptoPP; |
| 404 | StringSource(reinterpret_cast<uint8_t*>(&r), 4, true, |
| 405 | new HexEncoder(new FileSink(ss), false)); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 406 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 407 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 408 | // for (int i = 0; i < 8; i++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 409 | // { |
| 410 | // uint32_t t = r & mask; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 411 | // if (t < 10) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 412 | // ss << static_cast<char>(t + 0x30); |
| 413 | // else |
| 414 | // ss << static_cast<char>(t + 0x57); |
| 415 | // r = r >> 4; |
| 416 | // } |
| 417 | |
| 418 | return ss.str(); |
| 419 | } |
| 420 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 421 | void |
| 422 | Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog) |
| 423 | { |
| 424 | m_chatDialogList[chatroomName.toStdString()] = chatDialog; |
| 425 | connect(chatDialog, SIGNAL(closeChatDialog(const QString&)), |
| 426 | this, SLOT(onRemoveChatDialog(const QString&))); |
| 427 | connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)), |
| 428 | this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&))); |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 429 | connect(chatDialog, SIGNAL(resetIcon()), |
| 430 | this, SLOT(onResetIcon())); |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 431 | connect(chatDialog, SIGNAL(rosterChanged(const chronochat::ChatroomInfo&)), |
| 432 | this, SLOT(onRosterChanged(const chronochat::ChatroomInfo&))); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 433 | connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 434 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 435 | connect(this, SIGNAL(localPrefixConfigured(const QString&)), |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 436 | chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 437 | |
| 438 | QAction* chatAction = new QAction(chatroomName, this); |
| 439 | m_chatActionList[chatroomName.toStdString()] = chatAction; |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 440 | connect(chatAction, SIGNAL(triggered()), |
Yingdi Yu | e8323b6 | 2014-09-02 17:24:15 -0700 | [diff] [blame] | 441 | chatDialog, SLOT(onShow())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 442 | |
| 443 | QAction* closeAction = new QAction(chatroomName, this); |
| 444 | m_closeActionList[chatroomName.toStdString()] = closeAction; |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 445 | connect(closeAction, SIGNAL(triggered()), |
| 446 | chatDialog, SLOT(shutdown())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 447 | |
| 448 | updateMenu(); |
| 449 | } |
| 450 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 451 | void |
| 452 | Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd) |
| 453 | { |
| 454 | emit discoverChatroomChanged(info, isAdd); |
| 455 | } |
| 456 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 457 | void |
| 458 | Controller::onIdentityUpdated(const QString& identity) |
| 459 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 460 | while (!m_chatDialogList.empty()) { |
| 461 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
Yingdi Yu | 1fd4e4a | 2015-02-02 13:37:50 -0800 | [diff] [blame^] | 462 | it->second->shutdown(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 463 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 464 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 465 | emit closeDBModule(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 466 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 467 | Name identityName(identity.toStdString()); |
| 468 | m_identity = identityName; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 469 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 470 | QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void |
| 474 | Controller::onIdentityUpdatedContinued() |
| 475 | { |
| 476 | QString connection = m_db.connectionName(); |
| 477 | // _LOG_DEBUG("connection name: " << connection.toStdString()); |
| 478 | QSqlDatabase::removeDatabase(connection); |
| 479 | m_db.close(); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 480 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 481 | openDB(); |
| 482 | |
| 483 | emit identityUpdated(QString(m_identity.toUri().c_str())); |
| 484 | } |
| 485 | |
| 486 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 487 | Controller::onNickUpdated(const QString& nick) |
| 488 | { |
| 489 | m_nick = nick.toStdString(); |
| 490 | } |
| 491 | |
| 492 | void |
| 493 | Controller::onLocalPrefixUpdated(const QString& localPrefix) |
| 494 | { |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 495 | QString privateLocalPrefix("/private/local"); |
| 496 | |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 497 | if (privateLocalPrefix != localPrefix) |
| 498 | m_localPrefixDetected = true; |
| 499 | else |
| 500 | m_localPrefixDetected = false; |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 501 | |
| 502 | m_localPrefix = Name(localPrefix.toStdString()); |
Yingdi Yu | baaaec0 | 2014-09-02 22:05:32 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void |
| 506 | Controller::onLocalPrefixConfigured(const QString& prefix) |
| 507 | { |
| 508 | if (!m_localPrefixDetected) { |
| 509 | m_localPrefix = Name(prefix.toStdString()); |
| 510 | emit localPrefixConfigured(prefix); |
| 511 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void |
| 515 | Controller::onStartChatAction() |
| 516 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 517 | string chatroom = "chatroom-" + getRandomString(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 518 | |
| 519 | m_startChatDialog->setChatroom(chatroom); |
| 520 | m_startChatDialog->show(); |
| 521 | m_startChatDialog->raise(); |
| 522 | } |
| 523 | |
| 524 | void |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 525 | Controller::onDiscoveryAction() |
| 526 | { |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 531 | Controller::onSettingsAction() |
| 532 | { |
| 533 | m_settingDialog->setNick(QString(m_nick.c_str())); |
| 534 | m_settingDialog->show(); |
| 535 | m_settingDialog->raise(); |
| 536 | } |
| 537 | |
| 538 | void |
| 539 | Controller::onProfileEditorAction() |
| 540 | { |
| 541 | m_profileEditor->show(); |
| 542 | m_profileEditor->raise(); |
| 543 | } |
| 544 | |
| 545 | void |
| 546 | Controller::onAddContactAction() |
| 547 | { |
| 548 | emit refreshBrowseContact(); |
| 549 | m_browseContactDialog->show(); |
| 550 | m_browseContactDialog->raise(); |
| 551 | } |
| 552 | |
| 553 | void |
| 554 | Controller::onContactListAction() |
| 555 | { |
| 556 | m_contactPanel->show(); |
| 557 | m_contactPanel->raise(); |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | Controller::onDirectAdd() |
| 562 | { |
| 563 | m_addContactPanel->show(); |
| 564 | m_addContactPanel->raise(); |
| 565 | } |
| 566 | |
| 567 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 568 | Controller::onMinimizeAction() |
| 569 | { |
| 570 | m_settingDialog->hide(); |
| 571 | m_startChatDialog->hide(); |
| 572 | m_profileEditor->hide(); |
| 573 | m_invitationDialog->hide(); |
| 574 | m_addContactPanel->hide(); |
| 575 | |
| 576 | ChatDialogList::iterator it = m_chatDialogList.begin(); |
| 577 | ChatDialogList::iterator end = m_chatDialogList.end(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 578 | for (; it != end; it++) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 579 | it->second->hide(); |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | Controller::onQuitAction() |
| 584 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 585 | while (!m_chatDialogList.empty()) { |
| 586 | ChatDialogList::const_iterator it = m_chatDialogList.begin(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 587 | it->second->shutdown(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 588 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 589 | |
| 590 | delete m_settingDialog; |
| 591 | delete m_startChatDialog; |
| 592 | delete m_profileEditor; |
| 593 | delete m_invitationDialog; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 594 | delete m_browseContactDialog; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 595 | delete m_addContactPanel; |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 596 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 597 | if (m_backend.isRunning()) { |
| 598 | emit shutdownBackend(); |
| 599 | m_backend.wait(); |
| 600 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 601 | |
| 602 | QApplication::quit(); |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | Controller::onStartChatroom(const QString& chatroomName, bool secured) |
| 607 | { |
| 608 | Name chatroomPrefix; |
| 609 | chatroomPrefix.append("ndn") |
| 610 | .append("broadcast") |
| 611 | .append("ChronoChat") |
| 612 | .append(chatroomName.toStdString()); |
| 613 | |
| 614 | // check if the chatroom exists |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 615 | if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) { |
| 616 | QMessageBox::information(this, tr("ChronoChat"), |
| 617 | tr("You are creating an existing chatroom." |
| 618 | "You can check it in the context memu.")); |
| 619 | return; |
| 620 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 621 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 622 | // TODO: We should create a chatroom specific key/cert |
| 623 | //(which should be created in the first half of this method |
| 624 | //, but let's use the default one for now. |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 625 | Name chatPrefix; |
| 626 | chatPrefix.append(m_identity).append("CHRONOCHAT-DATA").append(chatroomName.toStdString()); |
| 627 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 628 | ChatDialog* chatDialog |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 629 | = new ChatDialog(chatroomPrefix, |
| 630 | chatPrefix, |
| 631 | m_localPrefix, |
| 632 | chatroomName.toStdString(), |
| 633 | m_nick); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 634 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 635 | addChatDialog(chatroomName, chatDialog); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 636 | chatDialog->show(); |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 637 | |
| 638 | emit addChatroom(chatroomName); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 642 | Controller::onStartChatroom2(chronochat::Invitation invitation, bool secured) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 643 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 644 | QString chatroomName = QString::fromStdString(invitation.getChatroom()); |
| 645 | onStartChatroom(chatroomName, secured); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 646 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 647 | ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 648 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 649 | BOOST_ASSERT(it != m_chatDialogList.end()); |
| 650 | it->second->addSyncAnchor(invitation); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 653 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 654 | void |
| 655 | Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data) |
| 656 | { |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 657 | m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName), |
| 658 | QString("<%1>: %2").arg(from).arg(data), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 659 | QSystemTrayIcon::Information, 20000); |
| 660 | m_trayIcon->setIcon(QIcon(":/images/note.png")); |
| 661 | } |
| 662 | |
| 663 | void |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 664 | Controller::onResetIcon() |
| 665 | { |
| 666 | m_trayIcon->setIcon(QIcon(":/images/icon_small.png")); |
| 667 | } |
| 668 | |
| 669 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 670 | Controller::onRemoveChatDialog(const QString& chatroomName) |
| 671 | { |
| 672 | ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString()); |
| 673 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 674 | if (it != m_chatDialogList.end()) { |
| 675 | ChatDialog* deletedChat = it->second; |
| 676 | if (deletedChat) |
| 677 | delete deletedChat; |
| 678 | m_chatDialogList.erase(it); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 679 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 680 | QAction* chatAction = m_chatActionList[chatroomName.toStdString()]; |
| 681 | QAction* closeAction = m_closeActionList[chatroomName.toStdString()]; |
| 682 | if (chatAction) |
| 683 | delete chatAction; |
| 684 | if (closeAction) |
| 685 | delete closeAction; |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 686 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 687 | m_chatActionList.erase(chatroomName.toStdString()); |
| 688 | m_closeActionList.erase(chatroomName.toStdString()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 689 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 690 | updateMenu(); |
| 691 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | void |
| 695 | Controller::onWarning(const QString& msg) |
| 696 | { |
| 697 | QMessageBox::information(this, tr("ChronoChat"), msg); |
| 698 | } |
| 699 | |
| 700 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 701 | Controller::onError(const QString& msg) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 702 | { |
| 703 | QMessageBox::critical(this, tr("ChronoChat"), msg, QMessageBox::Ok); |
| 704 | exit(1); |
| 705 | } |
| 706 | |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 707 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 708 | Controller::onRosterChanged(const chronochat::ChatroomInfo& info) |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 709 | { |
Yingdi Yu | 2c9e771 | 2014-10-20 11:55:05 -0700 | [diff] [blame] | 710 | |
Mengjin Yan | 391d724 | 2014-08-28 20:51:55 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 713 | } // namespace chronochat |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 714 | |
| 715 | #if WAF |
| 716 | #include "controller.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 717 | // #include "controller.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 718 | #endif |