blob: b45e1ddea49f83c1c6e605ab68ca2a1abf082b03 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- 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>
Yingdi Yuf3401182015-02-02 20:21:07 -08009 * Qiuhan Ding <qiuhanding@cs.ucla.edu>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080010 */
11
12#include <QApplication>
13#include <QMessageBox>
14#include <QDir>
15#include <QTimer>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070016#include "controller.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080017
18#ifndef Q_MOC_RUN
19#include <boost/filesystem.hpp>
20#include <boost/lexical_cast.hpp>
Yingdi Yufa0b6a02014-04-30 14:26:42 -070021#include <ndn-cxx/util/random.hpp>
Yingdi Yu0b0a7362014-08-05 16:31:30 -070022#include "cryptopp.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080023#include "logging.h"
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080024#include "conf.hpp"
25#include "endorse-info.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080026#endif
27
Yingdi Yueb692ac2015-02-10 18:46:18 -080028INIT_LOGGER("chronochat.Controller");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029
30Q_DECLARE_METATYPE(ndn::Name)
31Q_DECLARE_METATYPE(ndn::IdentityCertificate)
Yingdi Yueb692ac2015-02-10 18:46:18 -080032Q_DECLARE_METATYPE(chronochat::EndorseInfo)
Yingdi Yu233a9722014-03-07 15:47:09 -080033Q_DECLARE_METATYPE(ndn::Interest)
34Q_DECLARE_METATYPE(size_t)
Yingdi Yueb692ac2015-02-10 18:46:18 -080035Q_DECLARE_METATYPE(chronochat::ChatroomInfo)
36Q_DECLARE_METATYPE(chronochat::Invitation)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080037Q_DECLARE_METATYPE(std::string)
38Q_DECLARE_METATYPE(ndn::Name::Component)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080039
Yingdi Yueb692ac2015-02-10 18:46:18 -080040namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080041
Yingdi Yu0b0a7362014-08-05 16:31:30 -070042using std::string;
43
Yingdi Yu348f5ea2014-03-01 14:47:25 -080044// constructor & destructor
Yingdi Yu2c9e7712014-10-20 11:55:05 -070045Controller::Controller(QWidget* parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080046 : QDialog(parent)
Yingdi Yubaaaec02014-09-02 22:05:32 -070047 , m_localPrefixDetected(false)
Yingdi Yuf3401182015-02-02 20:21:07 -080048 , m_isInConnectionDetection(false)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080049 , m_settingDialog(new SettingDialog(this))
50 , m_startChatDialog(new StartChatDialog(this))
51 , m_profileEditor(new ProfileEditor(this))
52 , m_invitationDialog(new InvitationDialog(this))
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080053 , m_invitationRequestDialog(new InvitationRequestDialog(this))
Qiuhan Ding43c8e162015-02-02 15:16:48 -080054 , m_contactPanel(new ContactPanel(this))
55 , m_browseContactDialog(new BrowseContactDialog(this))
56 , m_addContactPanel(new AddContactPanel(this))
57 , m_discoveryPanel(new DiscoveryPanel(this))
Yingdi Yu348f5ea2014-03-01 14:47:25 -080058{
59 qRegisterMetaType<ndn::Name>("ndn.Name");
60 qRegisterMetaType<ndn::IdentityCertificate>("ndn.IdentityCertificate");
Yingdi Yueb692ac2015-02-10 18:46:18 -080061 qRegisterMetaType<chronochat::EndorseInfo>("chronochat.EndorseInfo");
Yingdi Yu233a9722014-03-07 15:47:09 -080062 qRegisterMetaType<ndn::Interest>("ndn.Interest");
63 qRegisterMetaType<size_t>("size_t");
Qiuhan Ding43c8e162015-02-02 15:16:48 -080064 qRegisterMetaType<chronochat::ChatroomInfo>("chronochat.Chatroom");
65 qRegisterMetaType<chronochat::Invitation>("chronochat.Invitation");
66 qRegisterMetaType<std::string>("std.string");
67 qRegisterMetaType<ndn::Name::Component>("ndn.Component");
68
Yingdi Yu348f5ea2014-03-01 14:47:25 -080069
70 // Connection to ContactManager
Yingdi Yu2c9e7712014-10-20 11:55:05 -070071 connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080072 this, SLOT(onWarning(const QString&)));
73 connect(this, SIGNAL(refreshBrowseContact()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070074 m_backend.getContactManager(), SLOT(onRefreshBrowseContact()));
75 connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080076 this, SLOT(onWarning(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080077
78 // Connection to SettingDialog
79 connect(this, SIGNAL(identityUpdated(const QString&)),
80 m_settingDialog, SLOT(onIdentityUpdated(const QString&)));
81 connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)),
82 this, SLOT(onIdentityUpdated(const QString&)));
83 connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)),
84 this, SLOT(onNickUpdated(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -070085 connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)),
86 this, SLOT(onLocalPrefixConfigured(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080087
88 // Connection to ProfileEditor
89 connect(this, SIGNAL(closeDBModule()),
90 m_profileEditor, SLOT(onCloseDBModule()));
91 connect(this, SIGNAL(identityUpdated(const QString&)),
92 m_profileEditor, SLOT(onIdentityUpdated(const QString&)));
93 connect(m_profileEditor, SIGNAL(updateProfile()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070094 m_backend.getContactManager(), SLOT(onUpdateProfile()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095
96 // Connection to StartChatDialog
97 connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)),
98 this, SLOT(onStartChatroom(const QString&, bool)));
99
100 // Connection to InvitationDialog
101 connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700102 &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800103
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800104 // Connection to InvitationRequestDialog
105 connect(m_invitationRequestDialog, SIGNAL(invitationRequestResponded(const ndn::Name&, bool)),
106 &m_backend, SLOT(onInvitationRequestResponded(const ndn::Name&, bool)));
107
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800108 // Connection to AddContactPanel
109 connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700110 m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800111 connect(m_addContactPanel, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700112 m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&)));
113 connect(m_backend.getContactManager(),
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800114 SIGNAL(contactEndorseInfoReady(const EndorseInfo&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700115 m_addContactPanel,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800116 SLOT(onContactEndorseInfoReady(const EndorseInfo&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800117
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800118 // Connection to BrowseContactDialog
119 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
120 this, SLOT(onDirectAdd()));
121 connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700122 m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800123 connect(m_browseContactDialog, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700124 m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&)));
125 connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800126 m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700127 connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800128 m_browseContactDialog, SLOT(onNameListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700129 connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::IdentityCertificate&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130 m_browseContactDialog, SLOT(onIdCertReady(const ndn::IdentityCertificate&)));
131
132 // Connection to ContactPanel
133 connect(m_contactPanel, SIGNAL(waitForContactList()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700134 m_backend.getContactManager(), SLOT(onWaitForContactList()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800135 connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700136 m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800137 connect(m_contactPanel, SIGNAL(removeContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700138 m_backend.getContactManager(), SLOT(onRemoveContact(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800139 connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700140 m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700142 m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800143 connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700144 m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800145 connect(m_contactPanel, SIGNAL(warning(const QString&)),
146 this, SLOT(onWarning(const QString&)));
147 connect(this, SIGNAL(closeDBModule()),
148 m_contactPanel, SLOT(onCloseDBModule()));
149 connect(this, SIGNAL(identityUpdated(const QString&)),
150 m_contactPanel, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700151 connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800152 m_contactPanel, SLOT(onContactAliasListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700153 connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154 m_contactPanel, SLOT(onContactIdListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700155 connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&,
156 const QString&, bool)),
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700157 m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&,
158 const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800159
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700160 // Connection to backend thread
Yingdi Yuf3401182015-02-02 20:21:07 -0800161 connect(&m_backend, SIGNAL(nfdError()),
162 this, SLOT(onNfdError()));
163 connect(this, SIGNAL(nfdReconnect()),
164 &m_backend, SLOT(onNfdReconnect()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700165 connect(this, SIGNAL(shutdownBackend()),
166 &m_backend, SLOT(shutdown()));
167 connect(this, SIGNAL(updateLocalPrefix()),
168 &m_backend, SLOT(onUpdateLocalPrefixAction()));
169 connect(this, SIGNAL(identityUpdated(const QString&)),
170 &m_backend, SLOT(onIdentityChanged(const QString&)));
171 connect(this, SIGNAL(addChatroom(QString)),
172 &m_backend, SLOT(addChatroom(QString)));
173 connect(this, SIGNAL(removeChatroom(QString)),
174 &m_backend, SLOT(removeChatroom(QString)));
175
176 // Thread notifications:
177 // on local prefix udpated:
178 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
179 this, SLOT(onLocalPrefixUpdated(const QString&)));
180 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
181 m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&)));
182
183 // on invitation validated:
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800184 connect(&m_backend, SIGNAL(invitationValidated(QString, QString, ndn::Name)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700185 m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800186 connect(&m_backend, SIGNAL(startChatroom(const QString&, bool)),
187 this, SLOT(onStartChatroom(const QString&, bool)));
188
189 // on invitation request received
190 connect(&m_backend, SIGNAL(invitationRequestReceived(QString, QString, ndn::Name)),
191 m_invitationRequestDialog, SLOT(onInvitationRequestReceived(QString, QString,
192 ndn::Name)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700193
194 // on invitation accepted:
Yingdi Yueb692ac2015-02-10 18:46:18 -0800195 connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronochat::Invitation, bool)),
196 this, SLOT(onStartChatroom2(chronochat::Invitation, bool)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700197
198 m_backend.start();
Mengjin Yan391d7242014-08-28 20:51:55 -0700199
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800200 initialize();
201
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800202 m_chatroomDiscoveryBackend
203 = new ChatroomDiscoveryBackend(m_localPrefix,
204 m_identity,
205 this);
206
207 // connect to chatroom discovery back end
208 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
209 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
210 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
211 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800212 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoRequest(std::string, bool)),
213 this, SLOT(onChatroomInfoRequest(std::string, bool)));
214 connect(this, SIGNAL(respondChatroomInfoRequest(ChatroomInfo, bool)),
215 m_chatroomDiscoveryBackend, SLOT(onRespondChatroomInfoRequest(ChatroomInfo, bool)));
216 connect(this, SIGNAL(shutdownDiscoveryBackend()),
217 m_chatroomDiscoveryBackend, SLOT(shutdown()));
218 connect(this, SIGNAL(identityUpdated(const QString&)),
219 m_chatroomDiscoveryBackend, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800220 connect(this, SIGNAL(nfdReconnect()),
221 m_chatroomDiscoveryBackend, SLOT(onNfdReconnect()));
222 connect(m_chatroomDiscoveryBackend, SIGNAL(nfdError()),
223 this, SLOT(onNfdError()));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800224
225 // connect chatroom discovery back end with front end
226 connect(m_discoveryPanel, SIGNAL(waitForChatroomInfo(const QString&)),
227 m_chatroomDiscoveryBackend, SLOT(onWaitForChatroomInfo(const QString&)));
228 connect(m_discoveryPanel, SIGNAL(warning(const QString&)),
229 this, SLOT(onWarning(const QString&)));
230 connect(this, SIGNAL(identityUpdated(const QString&)),
231 m_discoveryPanel, SLOT(onIdentityUpdated(const QString&)));
232 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomListReady(const QStringList&)),
233 m_discoveryPanel, SLOT(onChatroomListReady(const QStringList&)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800234 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoReady(const ChatroomInfo&, bool)),
235 m_discoveryPanel, SLOT(onChatroomInfoReady(const ChatroomInfo&, bool)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800236 connect(m_discoveryPanel, SIGNAL(startChatroom(const QString&, bool)),
237 this, SLOT(onStartChatroom(const QString&, bool)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800238 connect(m_discoveryPanel, SIGNAL(sendInvitationRequest(const QString&, const QString&)),
239 &m_backend, SLOT(onSendInvitationRequest(const QString&, const QString&)));
240 connect(&m_backend, SIGNAL(invitationRequestResult(const std::string&)),
241 m_discoveryPanel, SLOT(onInvitationRequestResult(const std::string&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800242
243 m_chatroomDiscoveryBackend->start();
244
Yingdi Yu233a9722014-03-07 15:47:09 -0800245 createTrayIcon();
246
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700247 emit updateLocalPrefix();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800248}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700249
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800250Controller::~Controller()
251{
252 saveConf();
253}
254
255// public methods
256
257
258// private methods
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700259string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800260Controller::getDBName()
261{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700262 string dbName("chronos-");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800263
264 std::stringstream ss;
265 {
266 using namespace CryptoPP;
267
268 SHA256 hash;
269 StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true,
270 new HashFilter(hash, new HexEncoder(new FileSink(ss), false)));
271 }
272 dbName.append(ss.str()).append(".db");
273
274 return dbName;
275}
276
277void
278Controller::openDB()
279{
280 m_db = QSqlDatabase::addDatabase("QSQLITE");
281 QString path = (QDir::home().path());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700282 path.append(QDir::separator())
283 .append(".chronos")
284 .append(QDir::separator())
285 .append(getDBName().c_str());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800286 m_db.setDatabaseName(path);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800287
Yingdi Yu42125862014-08-07 17:04:28 -0700288 m_db.open();
289
290 // bool ok = m_db.open();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700291 // _LOG_DEBUG("DB opened: " << std::boolalpha << ok );
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800292}
293
294void
295Controller::initialize()
296{
297 loadConf();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700298
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800299 openDB();
300
301 emit identityUpdated(QString(m_identity.toUri().c_str()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800302}
303
304void
305Controller::loadConf()
306{
307 namespace fs = boost::filesystem;
308
309 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
310 fs::create_directories (chronosDir);
311
312 std::ifstream is((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800313 Conf conf;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800314 Block confBlock;
315 try {
316 confBlock = ndn::Block::fromStream(is);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800317 conf.wireDecode(confBlock);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700318 m_identity.clear();
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800319 m_identity.append(conf.getIdentity());
320 if (conf.getNick().length() != 0)
321 m_nick = conf.getNick();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700322 else
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800323 m_nick = m_identity.get(-1).toUri();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700324 }
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700325 catch (tlv::Error) {
326 try {
327 ndn::KeyChain keyChain;
328 m_identity = keyChain.getDefaultIdentity();
329 }
330 catch (ndn::KeyChain::Error) {
331 m_identity.clear();
332 m_identity.append("chronochat-tmp-identity")
333 .append(getRandomString());
334 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700335 m_nick = m_identity.get(-1).toUri();
336 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800337}
338
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700339void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800340Controller::saveConf()
341{
342 namespace fs = boost::filesystem;
343
344 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
345 fs::create_directories (chronosDir);
346
347 std::ofstream os((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800348 Conf conf;
349 conf.setIdentity(m_identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700350 if (!m_nick.empty())
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800351 conf.setNick(m_nick);
352 Block confWire = conf.wireEncode();
353 os.write(reinterpret_cast<const char*>(confWire.wire()), confWire.size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800354
355 os.close();
356}
357
358void
359Controller::createActions()
360{
361 m_startChatroom = new QAction(tr("Start new chat"), this);
362 connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction()));
363
364 m_settingsAction = new QAction(tr("Settings"), this);
365 connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction()));
366
367 m_editProfileAction = new QAction(tr("Edit profile"), this);
368 connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction()));
369
370 m_contactListAction = new QAction(tr("Contact List"), this);
371 connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction()));
372
373 m_addContactAction = new QAction(tr("Add contact"), this);
374 connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction()));
375
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800376 m_chatroomDiscoveryAction = new QAction(tr("Chatroom Discovery"), this);
377 connect(m_chatroomDiscoveryAction, SIGNAL(triggered()), this, SLOT(onChatroomDiscoveryAction()));
378
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800379 m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700380 connect(m_updateLocalPrefixAction, SIGNAL(triggered()),
381 &m_backend, SLOT(onUpdateLocalPrefixAction()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800382
383 m_minimizeAction = new QAction(tr("Mi&nimize"), this);
384 connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction()));
385
386 m_quitAction = new QAction(tr("Quit"), this);
387 connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction()));
Mengjin Yan391d7242014-08-28 20:51:55 -0700388
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800389}
390
391void
392Controller::createTrayIcon()
393{
394 createActions();
395
396 m_trayIconMenu = new QMenu(this);
397 m_trayIconMenu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800398 m_trayIconMenu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700399
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800400 m_trayIconMenu->addSeparator();
401 m_trayIconMenu->addAction(m_settingsAction);
402 m_trayIconMenu->addAction(m_editProfileAction);
403 m_trayIconMenu->addSeparator();
404 m_trayIconMenu->addAction(m_contactListAction);
405 m_trayIconMenu->addAction(m_addContactAction);
406 m_trayIconMenu->addSeparator();
407 m_trayIconMenu->addAction(m_updateLocalPrefixAction);
408 m_trayIconMenu->addSeparator();
409 m_trayIconMenu->addAction(m_minimizeAction);
410 m_closeMenu = m_trayIconMenu->addMenu("Close chatroom");
411 m_closeMenu->setEnabled(false);
412 m_trayIconMenu->addSeparator();
413 m_trayIconMenu->addAction(m_quitAction);
414
415 m_trayIcon = new QSystemTrayIcon(this);
416 m_trayIcon->setContextMenu(m_trayIconMenu);
417
418 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
419 m_trayIcon->setToolTip("ChronoChat System Tray Icon");
420 m_trayIcon->setVisible(true);
421}
422
423void
424Controller::updateMenu()
425{
426 QMenu* menu = new QMenu(this);
427 QMenu* closeMenu = 0;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700428
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800429 menu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800430 menu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700431
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800432 menu->addSeparator();
433 menu->addAction(m_settingsAction);
434 menu->addAction(m_editProfileAction);
435 menu->addSeparator();
Yingdi Yu233a9722014-03-07 15:47:09 -0800436 menu->addAction(m_contactListAction);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800437 menu->addAction(m_addContactAction);
438 menu->addSeparator();
439 {
440 ChatActionList::const_iterator it = m_chatActionList.begin();
441 ChatActionList::const_iterator end = m_chatActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700442 if (it != end) {
443 for (; it != end; it++)
444 menu->addAction(it->second);
445 menu->addSeparator();
446 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800447 }
448 menu->addAction(m_updateLocalPrefixAction);
449 menu->addSeparator();
450 menu->addAction(m_minimizeAction);
451 closeMenu = menu->addMenu("Close chatroom");
452 {
453 ChatActionList::const_iterator it = m_closeActionList.begin();
454 ChatActionList::const_iterator end = m_closeActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700455 if (it == end)
456 closeMenu->setEnabled(false);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800457 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700458 for (; it != end; it++)
459 closeMenu->addAction(it->second);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800460 }
461 menu->addSeparator();
462 menu->addAction(m_quitAction);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700463
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800464 m_trayIcon->setContextMenu(menu);
465 delete m_trayIconMenu;
466 m_trayIconMenu = menu;
467 m_closeMenu = closeMenu;
468}
469
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700470string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800471Controller::getRandomString()
472{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700473 uint32_t r = ndn::random::generateWord32();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800474 std::stringstream ss;
475 {
476 using namespace CryptoPP;
477 StringSource(reinterpret_cast<uint8_t*>(&r), 4, true,
478 new HexEncoder(new FileSink(ss), false));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700479
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800480 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700481 // for (int i = 0; i < 8; i++)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800482 // {
483 // uint32_t t = r & mask;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700484 // if (t < 10)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800485 // ss << static_cast<char>(t + 0x30);
486 // else
487 // ss << static_cast<char>(t + 0x57);
488 // r = r >> 4;
489 // }
490
491 return ss.str();
492}
493
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800494void
495Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog)
496{
497 m_chatDialogList[chatroomName.toStdString()] = chatDialog;
498 connect(chatDialog, SIGNAL(closeChatDialog(const QString&)),
499 this, SLOT(onRemoveChatDialog(const QString&)));
500 connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)),
501 this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&)));
Yingdi Yu233a9722014-03-07 15:47:09 -0800502 connect(chatDialog, SIGNAL(resetIcon()),
503 this, SLOT(onResetIcon()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700504 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700505 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -0700506 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700507 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800508 connect(this, SIGNAL(nfdReconnect()),
509 chatDialog->getBackend(), SLOT(onNfdReconnect()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800510
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800511 // connect chat dialog with discovery backend
Yingdi Yuf3401182015-02-02 20:21:07 -0800512 connect(chatDialog->getBackend(), SIGNAL(nfdError()),
513 this, SLOT(onNfdError()));
514 connect(chatDialog->getBackend(), SIGNAL(newChatroomForDiscovery(ndn::Name::Component)),
515 m_chatroomDiscoveryBackend, SLOT(onNewChatroomForDiscovery(ndn::Name::Component)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800516 connect(chatDialog->getBackend(), SIGNAL(addInRoster(ndn::Name, ndn::Name::Component)),
517 m_chatroomDiscoveryBackend, SLOT(onAddInRoster(ndn::Name, ndn::Name::Component)));
518 connect(chatDialog->getBackend(), SIGNAL(eraseInRoster(ndn::Name, ndn::Name::Component)),
519 m_chatroomDiscoveryBackend, SLOT(onEraseInRoster(ndn::Name, ndn::Name::Component)));
520
521
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800522 QAction* chatAction = new QAction(chatroomName, this);
523 m_chatActionList[chatroomName.toStdString()] = chatAction;
Mengjin Yan391d7242014-08-28 20:51:55 -0700524 connect(chatAction, SIGNAL(triggered()),
Yingdi Yue8323b62014-09-02 17:24:15 -0700525 chatDialog, SLOT(onShow()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800526
527 QAction* closeAction = new QAction(chatroomName, this);
528 m_closeActionList[chatroomName.toStdString()] = closeAction;
Yingdi Yud45777b2014-10-16 23:54:11 -0700529 connect(closeAction, SIGNAL(triggered()),
530 chatDialog, SLOT(shutdown()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800531
532 updateMenu();
533}
534
Mengjin Yan391d7242014-08-28 20:51:55 -0700535void
536Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd)
537{
538 emit discoverChatroomChanged(info, isAdd);
539}
540
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800541void
542Controller::onIdentityUpdated(const QString& identity)
543{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700544 while (!m_chatDialogList.empty()) {
545 ChatDialogList::const_iterator it = m_chatDialogList.begin();
Yingdi Yu1fd4e4a2015-02-02 13:37:50 -0800546 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700547 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800548
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800549 emit closeDBModule();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700550
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700551 Name identityName(identity.toStdString());
552 m_identity = identityName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800553
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700554 QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800555}
556
557void
558Controller::onIdentityUpdatedContinued()
559{
560 QString connection = m_db.connectionName();
561 // _LOG_DEBUG("connection name: " << connection.toStdString());
562 QSqlDatabase::removeDatabase(connection);
563 m_db.close();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700564
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800565 openDB();
566
567 emit identityUpdated(QString(m_identity.toUri().c_str()));
568}
569
570void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800571Controller::onNickUpdated(const QString& nick)
572{
573 m_nick = nick.toStdString();
574}
575
576void
577Controller::onLocalPrefixUpdated(const QString& localPrefix)
578{
Yingdi Yubaaaec02014-09-02 22:05:32 -0700579 QString privateLocalPrefix("/private/local");
580
Yingdi Yubaaaec02014-09-02 22:05:32 -0700581 if (privateLocalPrefix != localPrefix)
582 m_localPrefixDetected = true;
583 else
584 m_localPrefixDetected = false;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700585
586 m_localPrefix = Name(localPrefix.toStdString());
Yingdi Yubaaaec02014-09-02 22:05:32 -0700587}
588
589void
590Controller::onLocalPrefixConfigured(const QString& prefix)
591{
592 if (!m_localPrefixDetected) {
593 m_localPrefix = Name(prefix.toStdString());
594 emit localPrefixConfigured(prefix);
595 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800596}
597
598void
599Controller::onStartChatAction()
600{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700601 string chatroom = "chatroom-" + getRandomString();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800602
603 m_startChatDialog->setChatroom(chatroom);
604 m_startChatDialog->show();
605 m_startChatDialog->raise();
606}
607
608void
609Controller::onSettingsAction()
610{
611 m_settingDialog->setNick(QString(m_nick.c_str()));
612 m_settingDialog->show();
613 m_settingDialog->raise();
614}
615
616void
617Controller::onProfileEditorAction()
618{
619 m_profileEditor->show();
620 m_profileEditor->raise();
621}
622
623void
624Controller::onAddContactAction()
625{
626 emit refreshBrowseContact();
627 m_browseContactDialog->show();
628 m_browseContactDialog->raise();
629}
630
631void
632Controller::onContactListAction()
633{
634 m_contactPanel->show();
635 m_contactPanel->raise();
636}
637
638void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800639Controller::onChatroomDiscoveryAction()
640{
641 m_discoveryPanel->show();
642 m_discoveryPanel->raise();
643}
644
645void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800646Controller::onDirectAdd()
647{
648 m_addContactPanel->show();
649 m_addContactPanel->raise();
650}
651
652void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800653Controller::onMinimizeAction()
654{
655 m_settingDialog->hide();
656 m_startChatDialog->hide();
657 m_profileEditor->hide();
658 m_invitationDialog->hide();
659 m_addContactPanel->hide();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800660 m_discoveryPanel->hide();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800661
662 ChatDialogList::iterator it = m_chatDialogList.begin();
663 ChatDialogList::iterator end = m_chatDialogList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700664 for (; it != end; it++)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800665 it->second->hide();
666}
667
668void
669Controller::onQuitAction()
670{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700671 while (!m_chatDialogList.empty()) {
672 ChatDialogList::const_iterator it = m_chatDialogList.begin();
Yingdi Yud45777b2014-10-16 23:54:11 -0700673 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700674 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800675
676 delete m_settingDialog;
677 delete m_startChatDialog;
678 delete m_profileEditor;
679 delete m_invitationDialog;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700680 delete m_browseContactDialog;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800681 delete m_addContactPanel;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800682 delete m_discoveryPanel;
683 if (m_chatroomDiscoveryBackend->isRunning()) {
684 emit shutdownDiscoveryBackend();
685 m_chatroomDiscoveryBackend->wait();
686 }
687 delete m_chatroomDiscoveryBackend;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700688
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700689 if (m_backend.isRunning()) {
690 emit shutdownBackend();
691 m_backend.wait();
692 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800693
Yingdi Yuf3401182015-02-02 20:21:07 -0800694 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
695 emit shutdownNfdChecker();
696 m_nfdConnectionChecker->wait();
697 }
698
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800699 QApplication::quit();
700}
701
702void
703Controller::onStartChatroom(const QString& chatroomName, bool secured)
704{
705 Name chatroomPrefix;
706 chatroomPrefix.append("ndn")
707 .append("broadcast")
708 .append("ChronoChat")
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800709 .append("Chatroom")
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800710 .append(chatroomName.toStdString());
711
712 // check if the chatroom exists
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700713 if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) {
714 QMessageBox::information(this, tr("ChronoChat"),
715 tr("You are creating an existing chatroom."
716 "You can check it in the context memu."));
717 return;
718 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800719
Mengjin Yan391d7242014-08-28 20:51:55 -0700720 // TODO: We should create a chatroom specific key/cert
721 //(which should be created in the first half of this method
722 //, but let's use the default one for now.
Yingdi Yud45777b2014-10-16 23:54:11 -0700723 Name chatPrefix;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800724 chatPrefix.append(m_identity).append("CHRONOCHAT-CHATDATA").append(chatroomName.toStdString());
Yingdi Yud45777b2014-10-16 23:54:11 -0700725
Mengjin Yan391d7242014-08-28 20:51:55 -0700726 ChatDialog* chatDialog
Yingdi Yud45777b2014-10-16 23:54:11 -0700727 = new ChatDialog(chatroomPrefix,
728 chatPrefix,
729 m_localPrefix,
730 chatroomName.toStdString(),
Yingdi Yu45da92a2015-02-02 13:17:03 -0800731 m_nick,
732 true,
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800733 m_identity,
734 this);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800735
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700736 addChatDialog(chatroomName, chatDialog);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800737 chatDialog->show();
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700738
739 emit addChatroom(chatroomName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800740}
741
742void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800743Controller::onStartChatroom2(chronochat::Invitation invitation, bool secured)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800744{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700745 QString chatroomName = QString::fromStdString(invitation.getChatroom());
746 onStartChatroom(chatroomName, secured);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800747
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700748 ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800749
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700750 BOOST_ASSERT(it != m_chatDialogList.end());
751 it->second->addSyncAnchor(invitation);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800752}
753
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700754
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800755void
756Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data)
757{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700758 m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName),
759 QString("<%1>: %2").arg(from).arg(data),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800760 QSystemTrayIcon::Information, 20000);
761 m_trayIcon->setIcon(QIcon(":/images/note.png"));
762}
763
764void
Yingdi Yu233a9722014-03-07 15:47:09 -0800765Controller::onResetIcon()
766{
767 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
768}
769
770void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800771Controller::onRemoveChatDialog(const QString& chatroomName)
772{
773 ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700774 if (it != m_chatDialogList.end()) {
775 ChatDialog* deletedChat = it->second;
776 if (deletedChat)
777 delete deletedChat;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800778
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700779 m_chatDialogList.erase(it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800780
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700781 QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
782 QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
783 if (chatAction)
784 delete chatAction;
785 if (closeAction)
786 delete closeAction;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700787
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700788 m_chatActionList.erase(chatroomName.toStdString());
789 m_closeActionList.erase(chatroomName.toStdString());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700790
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700791 updateMenu();
792 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800793}
794
795void
796Controller::onWarning(const QString& msg)
797{
798 QMessageBox::information(this, tr("ChronoChat"), msg);
799}
800
801void
Yingdi Yuf3401182015-02-02 20:21:07 -0800802Controller::onNfdError()
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800803{
Yingdi Yuf3401182015-02-02 20:21:07 -0800804 if (m_isInConnectionDetection)
805 return;
806 // begin a thread
807
808 m_isInConnectionDetection = true;
809 m_nfdConnectionChecker = new NfdConnectionChecker(this);
810
811 connect(m_nfdConnectionChecker, SIGNAL(nfdConnected()),
812 this, SLOT(onNfdReconnect()));
813 connect(this, SIGNAL(shutdownNfdChecker()),
814 m_nfdConnectionChecker, SLOT(shutdown()));
815
816 m_nfdConnectionChecker->start();
817 QMessageBox::information(this, tr("ChronoChat"), "Nfd is not running");
818}
819
820void
821Controller::onNfdReconnect()
822{
823 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
824 m_nfdConnectionChecker->wait();
825 }
826 delete m_nfdConnectionChecker;
827 m_nfdConnectionChecker = nullptr;
828 m_isInConnectionDetection = false;
829 emit nfdReconnect();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800830}
831
Yingdi Yu233a9722014-03-07 15:47:09 -0800832void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800833Controller::onChatroomInfoRequest(std::string chatroomName, bool isManager)
Mengjin Yan391d7242014-08-28 20:51:55 -0700834{
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800835 auto it = m_chatDialogList.find(chatroomName);
836 if (it != m_chatDialogList.end())
837 emit respondChatroomInfoRequest(it->second->getChatroomInfo(), isManager);
Mengjin Yan391d7242014-08-28 20:51:55 -0700838}
839
Yingdi Yueb692ac2015-02-10 18:46:18 -0800840} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800841
842#if WAF
843#include "controller.moc"
Yingdi Yu42125862014-08-07 17:04:28 -0700844// #include "controller.cpp.moc"
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800845#endif