blob: 9e794b95889a02710ee8f83ae3c78a7c6729b4a3 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Yingdi Yu348f5ea2014-03-01 14:47:25 -08004 * 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
Varun Patila24bd3e2020-11-24 10:08:33 +053012#include "controller.hpp"
Varun Patila24bd3e2020-11-24 10:08:33 +053013#include "conf.hpp"
14#include "endorse-info.hpp"
15
Yingdi Yu348f5ea2014-03-01 14:47:25 -080016#include <QApplication>
17#include <QMessageBox>
18#include <QDir>
19#include <QTimer>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080020
Davide Pesavento7676b562020-12-14 00:41:26 -050021#include <ndn-cxx/security/transform/buffer-source.hpp>
22#include <ndn-cxx/security/transform/digest-filter.hpp>
23#include <ndn-cxx/security/transform/hex-encode.hpp>
24#include <ndn-cxx/security/transform/stream-sink.hpp>
Yingdi Yufa0b6a02014-04-30 14:26:42 -070025#include <ndn-cxx/util/random.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080026
Davide Pesavento7676b562020-12-14 00:41:26 -050027#include <boost/filesystem.hpp>
28
Yingdi Yu348f5ea2014-03-01 14:47:25 -080029Q_DECLARE_METATYPE(ndn::Name)
Varun Patil3d850902020-11-23 12:19:14 +053030Q_DECLARE_METATYPE(ndn::security::Certificate)
Yingdi Yueb692ac2015-02-10 18:46:18 -080031Q_DECLARE_METATYPE(chronochat::EndorseInfo)
Yingdi Yu233a9722014-03-07 15:47:09 -080032Q_DECLARE_METATYPE(ndn::Interest)
33Q_DECLARE_METATYPE(size_t)
Yingdi Yueb692ac2015-02-10 18:46:18 -080034Q_DECLARE_METATYPE(chronochat::ChatroomInfo)
35Q_DECLARE_METATYPE(chronochat::Invitation)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080036Q_DECLARE_METATYPE(std::string)
37Q_DECLARE_METATYPE(ndn::Name::Component)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080038
Yingdi Yueb692ac2015-02-10 18:46:18 -080039namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080040
Yingdi Yu0b0a7362014-08-05 16:31:30 -070041using std::string;
42
Yingdi Yu348f5ea2014-03-01 14:47:25 -080043// constructor & destructor
Yingdi Yu2c9e7712014-10-20 11:55:05 -070044Controller::Controller(QWidget* parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080045 : QDialog(parent)
Yingdi Yubaaaec02014-09-02 22:05:32 -070046 , m_localPrefixDetected(false)
Yingdi Yuf3401182015-02-02 20:21:07 -080047 , m_isInConnectionDetection(false)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080048 , m_settingDialog(new SettingDialog(this))
49 , m_startChatDialog(new StartChatDialog(this))
50 , m_profileEditor(new ProfileEditor(this))
51 , m_invitationDialog(new InvitationDialog(this))
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080052 , m_invitationRequestDialog(new InvitationRequestDialog(this))
Qiuhan Ding43c8e162015-02-02 15:16:48 -080053 , m_contactPanel(new ContactPanel(this))
54 , m_browseContactDialog(new BrowseContactDialog(this))
55 , m_addContactPanel(new AddContactPanel(this))
56 , m_discoveryPanel(new DiscoveryPanel(this))
Yingdi Yu348f5ea2014-03-01 14:47:25 -080057{
58 qRegisterMetaType<ndn::Name>("ndn.Name");
Varun Patil3d850902020-11-23 12:19:14 +053059 qRegisterMetaType<ndn::security::Certificate>("ndn.security.v2.Certificate");
60 qRegisterMetaType<chronochat::EndorseInfo>("EndorseInfo");
Yingdi Yu233a9722014-03-07 15:47:09 -080061 qRegisterMetaType<ndn::Interest>("ndn.Interest");
62 qRegisterMetaType<size_t>("size_t");
Qiuhan Ding43c8e162015-02-02 15:16:48 -080063 qRegisterMetaType<chronochat::ChatroomInfo>("chronochat.Chatroom");
64 qRegisterMetaType<chronochat::Invitation>("chronochat.Invitation");
65 qRegisterMetaType<std::string>("std.string");
66 qRegisterMetaType<ndn::Name::Component>("ndn.Component");
67
Yingdi Yu348f5ea2014-03-01 14:47:25 -080068
69 // Connection to ContactManager
Yingdi Yu2c9e7712014-10-20 11:55:05 -070070 connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080071 this, SLOT(onWarning(const QString&)));
72 connect(this, SIGNAL(refreshBrowseContact()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070073 m_backend.getContactManager(), SLOT(onRefreshBrowseContact()));
74 connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080075 this, SLOT(onWarning(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080076
77 // Connection to SettingDialog
78 connect(this, SIGNAL(identityUpdated(const QString&)),
79 m_settingDialog, SLOT(onIdentityUpdated(const QString&)));
80 connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)),
81 this, SLOT(onIdentityUpdated(const QString&)));
82 connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)),
83 this, SLOT(onNickUpdated(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -070084 connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)),
85 this, SLOT(onLocalPrefixConfigured(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080086
87 // Connection to ProfileEditor
88 connect(this, SIGNAL(closeDBModule()),
89 m_profileEditor, SLOT(onCloseDBModule()));
90 connect(this, SIGNAL(identityUpdated(const QString&)),
91 m_profileEditor, SLOT(onIdentityUpdated(const QString&)));
92 connect(m_profileEditor, SIGNAL(updateProfile()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070093 m_backend.getContactManager(), SLOT(onUpdateProfile()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080094
95 // Connection to StartChatDialog
96 connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)),
97 this, SLOT(onStartChatroom(const QString&, bool)));
98
99 // Connection to InvitationDialog
100 connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700101 &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800102
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800103 // Connection to InvitationRequestDialog
104 connect(m_invitationRequestDialog, SIGNAL(invitationRequestResponded(const ndn::Name&, bool)),
105 &m_backend, SLOT(onInvitationRequestResponded(const ndn::Name&, bool)));
106
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800107 // Connection to AddContactPanel
108 connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700109 m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800110 connect(m_addContactPanel, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700111 m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&)));
112 connect(m_backend.getContactManager(),
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800113 SIGNAL(contactEndorseInfoReady(const EndorseInfo&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700114 m_addContactPanel,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800115 SLOT(onContactEndorseInfoReady(const EndorseInfo&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800116
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800117 // Connection to BrowseContactDialog
118 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
119 this, SLOT(onDirectAdd()));
120 connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700121 m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800122 connect(m_browseContactDialog, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700123 m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&)));
124 connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800125 m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700126 connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800127 m_browseContactDialog, SLOT(onNameListReady(const QStringList&)));
Varun Patil3d850902020-11-23 12:19:14 +0530128 connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::security::Certificate&)),
129 m_browseContactDialog, SLOT(onIdCertReady(const ndn::security::Certificate&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130
131 // Connection to ContactPanel
132 connect(m_contactPanel, SIGNAL(waitForContactList()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700133 m_backend.getContactManager(), SLOT(onWaitForContactList()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700135 m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800136 connect(m_contactPanel, SIGNAL(removeContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700137 m_backend.getContactManager(), SLOT(onRemoveContact(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800138 connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700139 m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800140 connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700141 m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800142 connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700143 m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800144 connect(m_contactPanel, SIGNAL(warning(const QString&)),
145 this, SLOT(onWarning(const QString&)));
146 connect(this, SIGNAL(closeDBModule()),
147 m_contactPanel, SLOT(onCloseDBModule()));
148 connect(this, SIGNAL(identityUpdated(const QString&)),
149 m_contactPanel, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700150 connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800151 m_contactPanel, SLOT(onContactAliasListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700152 connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800153 m_contactPanel, SLOT(onContactIdListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700154 connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&,
155 const QString&, bool)),
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700156 m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&,
157 const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800158
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700159 // Connection to backend thread
Yingdi Yuf3401182015-02-02 20:21:07 -0800160 connect(&m_backend, SIGNAL(nfdError()),
161 this, SLOT(onNfdError()));
162 connect(this, SIGNAL(nfdReconnect()),
163 &m_backend, SLOT(onNfdReconnect()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700164 connect(this, SIGNAL(shutdownBackend()),
165 &m_backend, SLOT(shutdown()));
166 connect(this, SIGNAL(updateLocalPrefix()),
167 &m_backend, SLOT(onUpdateLocalPrefixAction()));
168 connect(this, SIGNAL(identityUpdated(const QString&)),
169 &m_backend, SLOT(onIdentityChanged(const QString&)));
170 connect(this, SIGNAL(addChatroom(QString)),
171 &m_backend, SLOT(addChatroom(QString)));
172 connect(this, SIGNAL(removeChatroom(QString)),
173 &m_backend, SLOT(removeChatroom(QString)));
174
175 // Thread notifications:
176 // on local prefix udpated:
177 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
178 this, SLOT(onLocalPrefixUpdated(const QString&)));
179 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
180 m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&)));
181
182 // on invitation validated:
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800183 connect(&m_backend, SIGNAL(invitationValidated(QString, QString, ndn::Name)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700184 m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800185 connect(&m_backend, SIGNAL(startChatroom(const QString&, bool)),
186 this, SLOT(onStartChatroom(const QString&, bool)));
187
188 // on invitation request received
189 connect(&m_backend, SIGNAL(invitationRequestReceived(QString, QString, ndn::Name)),
190 m_invitationRequestDialog, SLOT(onInvitationRequestReceived(QString, QString,
191 ndn::Name)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700192
193 // on invitation accepted:
Yingdi Yueb692ac2015-02-10 18:46:18 -0800194 connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronochat::Invitation, bool)),
195 this, SLOT(onStartChatroom2(chronochat::Invitation, bool)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700196
197 m_backend.start();
Mengjin Yan391d7242014-08-28 20:51:55 -0700198
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800199 initialize();
200
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800201 m_chatroomDiscoveryBackend
202 = new ChatroomDiscoveryBackend(m_localPrefix,
203 m_identity,
204 this);
205
206 // connect to chatroom discovery back end
207 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
208 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
209 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
210 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800211 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoRequest(std::string, bool)),
212 this, SLOT(onChatroomInfoRequest(std::string, bool)));
213 connect(this, SIGNAL(respondChatroomInfoRequest(ChatroomInfo, bool)),
214 m_chatroomDiscoveryBackend, SLOT(onRespondChatroomInfoRequest(ChatroomInfo, bool)));
215 connect(this, SIGNAL(shutdownDiscoveryBackend()),
216 m_chatroomDiscoveryBackend, SLOT(shutdown()));
217 connect(this, SIGNAL(identityUpdated(const QString&)),
218 m_chatroomDiscoveryBackend, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800219 connect(this, SIGNAL(nfdReconnect()),
220 m_chatroomDiscoveryBackend, SLOT(onNfdReconnect()));
221 connect(m_chatroomDiscoveryBackend, SIGNAL(nfdError()),
222 this, SLOT(onNfdError()));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800223
224 // connect chatroom discovery back end with front end
225 connect(m_discoveryPanel, SIGNAL(waitForChatroomInfo(const QString&)),
226 m_chatroomDiscoveryBackend, SLOT(onWaitForChatroomInfo(const QString&)));
227 connect(m_discoveryPanel, SIGNAL(warning(const QString&)),
228 this, SLOT(onWarning(const QString&)));
229 connect(this, SIGNAL(identityUpdated(const QString&)),
230 m_discoveryPanel, SLOT(onIdentityUpdated(const QString&)));
231 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomListReady(const QStringList&)),
232 m_discoveryPanel, SLOT(onChatroomListReady(const QStringList&)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800233 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoReady(const ChatroomInfo&, bool)),
234 m_discoveryPanel, SLOT(onChatroomInfoReady(const ChatroomInfo&, bool)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800235 connect(m_discoveryPanel, SIGNAL(startChatroom(const QString&, bool)),
236 this, SLOT(onStartChatroom(const QString&, bool)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800237 connect(m_discoveryPanel, SIGNAL(sendInvitationRequest(const QString&, const QString&)),
238 &m_backend, SLOT(onSendInvitationRequest(const QString&, const QString&)));
239 connect(&m_backend, SIGNAL(invitationRequestResult(const std::string&)),
240 m_discoveryPanel, SLOT(onInvitationRequestResult(const std::string&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800241
242 m_chatroomDiscoveryBackend->start();
243
Yingdi Yu233a9722014-03-07 15:47:09 -0800244 createTrayIcon();
245
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700246 emit updateLocalPrefix();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800247}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700248
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800249Controller::~Controller()
250{
251 saveConf();
252}
253
Davide Pesavento7676b562020-12-14 00:41:26 -0500254static string
255getRandomString()
256{
257 uint32_t r = ndn::random::generateWord32();
258 std::ostringstream ss;
259 {
260 using namespace ndn::security::transform;
261 bufferSource(reinterpret_cast<const uint8_t*>(&r), sizeof(r))
262 >> hexEncode(false)
263 >> streamSink(ss);
264 }
265 return ss.str();
266}
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800267
268
269// private methods
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700270string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800271Controller::getDBName()
272{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700273 string dbName("chronos-");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800274
Davide Pesavento7676b562020-12-14 00:41:26 -0500275 std::ostringstream ss;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800276 {
Davide Pesavento7676b562020-12-14 00:41:26 -0500277 using namespace ndn::security::transform;
278 bufferSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size())
279 >> digestFilter(ndn::DigestAlgorithm::SHA256)
280 >> hexEncode(false)
281 >> streamSink(ss);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800282 }
283 dbName.append(ss.str()).append(".db");
284
285 return dbName;
286}
287
288void
289Controller::openDB()
290{
291 m_db = QSqlDatabase::addDatabase("QSQLITE");
292 QString path = (QDir::home().path());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700293 path.append(QDir::separator())
294 .append(".chronos")
295 .append(QDir::separator())
296 .append(getDBName().c_str());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800297 m_db.setDatabaseName(path);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800298
Yingdi Yu42125862014-08-07 17:04:28 -0700299 m_db.open();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800300}
301
302void
303Controller::initialize()
304{
305 loadConf();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700306
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800307 openDB();
308
309 emit identityUpdated(QString(m_identity.toUri().c_str()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800310}
311
312void
313Controller::loadConf()
314{
315 namespace fs = boost::filesystem;
316
317 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
318 fs::create_directories (chronosDir);
319
320 std::ifstream is((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800321 Conf conf;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800322 Block confBlock;
323 try {
324 confBlock = ndn::Block::fromStream(is);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800325 conf.wireDecode(confBlock);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700326 m_identity.clear();
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800327 m_identity.append(conf.getIdentity());
328 if (conf.getNick().length() != 0)
329 m_nick = conf.getNick();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700330 else
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800331 m_nick = m_identity.get(-1).toUri();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700332 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530333 catch (const tlv::Error&) {
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700334 try {
335 ndn::KeyChain keyChain;
Varun Patil3d850902020-11-23 12:19:14 +0530336 m_identity = keyChain.getPib().getDefaultIdentity().getName();
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700337 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530338 catch (const ndn::security::pib::Pib::Error&) {
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700339 m_identity.clear();
340 m_identity.append("chronochat-tmp-identity")
Varun Patil3d850902020-11-23 12:19:14 +0530341 .append(getRandomString());
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700342 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700343 m_nick = m_identity.get(-1).toUri();
344 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800345}
346
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700347void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800348Controller::saveConf()
349{
350 namespace fs = boost::filesystem;
351
352 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
353 fs::create_directories (chronosDir);
354
355 std::ofstream os((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800356 Conf conf;
357 conf.setIdentity(m_identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700358 if (!m_nick.empty())
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800359 conf.setNick(m_nick);
360 Block confWire = conf.wireEncode();
361 os.write(reinterpret_cast<const char*>(confWire.wire()), confWire.size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800362
363 os.close();
364}
365
366void
367Controller::createActions()
368{
369 m_startChatroom = new QAction(tr("Start new chat"), this);
370 connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction()));
371
372 m_settingsAction = new QAction(tr("Settings"), this);
373 connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction()));
374
375 m_editProfileAction = new QAction(tr("Edit profile"), this);
376 connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction()));
377
378 m_contactListAction = new QAction(tr("Contact List"), this);
379 connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction()));
380
381 m_addContactAction = new QAction(tr("Add contact"), this);
382 connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction()));
383
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800384 m_chatroomDiscoveryAction = new QAction(tr("Chatroom Discovery"), this);
385 connect(m_chatroomDiscoveryAction, SIGNAL(triggered()), this, SLOT(onChatroomDiscoveryAction()));
386
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800387 m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700388 connect(m_updateLocalPrefixAction, SIGNAL(triggered()),
389 &m_backend, SLOT(onUpdateLocalPrefixAction()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800390
391 m_minimizeAction = new QAction(tr("Mi&nimize"), this);
392 connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction()));
393
394 m_quitAction = new QAction(tr("Quit"), this);
395 connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction()));
Mengjin Yan391d7242014-08-28 20:51:55 -0700396
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800397}
398
399void
400Controller::createTrayIcon()
401{
402 createActions();
403
404 m_trayIconMenu = new QMenu(this);
405 m_trayIconMenu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800406 m_trayIconMenu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700407
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800408 m_trayIconMenu->addSeparator();
409 m_trayIconMenu->addAction(m_settingsAction);
410 m_trayIconMenu->addAction(m_editProfileAction);
411 m_trayIconMenu->addSeparator();
412 m_trayIconMenu->addAction(m_contactListAction);
413 m_trayIconMenu->addAction(m_addContactAction);
414 m_trayIconMenu->addSeparator();
415 m_trayIconMenu->addAction(m_updateLocalPrefixAction);
416 m_trayIconMenu->addSeparator();
417 m_trayIconMenu->addAction(m_minimizeAction);
418 m_closeMenu = m_trayIconMenu->addMenu("Close chatroom");
419 m_closeMenu->setEnabled(false);
420 m_trayIconMenu->addSeparator();
421 m_trayIconMenu->addAction(m_quitAction);
422
423 m_trayIcon = new QSystemTrayIcon(this);
424 m_trayIcon->setContextMenu(m_trayIconMenu);
425
426 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
427 m_trayIcon->setToolTip("ChronoChat System Tray Icon");
428 m_trayIcon->setVisible(true);
429}
430
431void
432Controller::updateMenu()
433{
Varun Patil3d850902020-11-23 12:19:14 +0530434 m_trayIconMenu->clear();
435 QMenu* menu = m_trayIconMenu;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700436
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800437 menu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800438 menu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700439
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800440 menu->addSeparator();
441 menu->addAction(m_settingsAction);
442 menu->addAction(m_editProfileAction);
443 menu->addSeparator();
Yingdi Yu233a9722014-03-07 15:47:09 -0800444 menu->addAction(m_contactListAction);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800445 menu->addAction(m_addContactAction);
446 menu->addSeparator();
447 {
Davide Pesavento7676b562020-12-14 00:41:26 -0500448 auto it = m_chatActionList.begin();
449 auto end = m_chatActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700450 if (it != end) {
451 for (; it != end; it++)
452 menu->addAction(it->second);
453 menu->addSeparator();
454 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800455 }
456 menu->addAction(m_updateLocalPrefixAction);
457 menu->addSeparator();
458 menu->addAction(m_minimizeAction);
Varun Patil3d850902020-11-23 12:19:14 +0530459 m_closeMenu = menu->addMenu("Close chatroom");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800460 {
Davide Pesavento7676b562020-12-14 00:41:26 -0500461 auto it = m_closeActionList.begin();
462 auto end = m_closeActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700463 if (it == end)
Varun Patil3d850902020-11-23 12:19:14 +0530464 m_closeMenu->setEnabled(false);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800465 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700466 for (; it != end; it++)
Varun Patil3d850902020-11-23 12:19:14 +0530467 m_closeMenu->addAction(it->second);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800468 }
469 menu->addSeparator();
470 menu->addAction(m_quitAction);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800471}
472
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800473void
474Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog)
475{
476 m_chatDialogList[chatroomName.toStdString()] = chatDialog;
477 connect(chatDialog, SIGNAL(closeChatDialog(const QString&)),
478 this, SLOT(onRemoveChatDialog(const QString&)));
479 connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)),
480 this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&)));
Yingdi Yu233a9722014-03-07 15:47:09 -0800481 connect(chatDialog, SIGNAL(resetIcon()),
482 this, SLOT(onResetIcon()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700483 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700484 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -0700485 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700486 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800487 connect(this, SIGNAL(nfdReconnect()),
488 chatDialog->getBackend(), SLOT(onNfdReconnect()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800489
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800490 // connect chat dialog with discovery backend
Yingdi Yuf3401182015-02-02 20:21:07 -0800491 connect(chatDialog->getBackend(), SIGNAL(nfdError()),
492 this, SLOT(onNfdError()));
493 connect(chatDialog->getBackend(), SIGNAL(newChatroomForDiscovery(ndn::Name::Component)),
494 m_chatroomDiscoveryBackend, SLOT(onNewChatroomForDiscovery(ndn::Name::Component)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800495 connect(chatDialog->getBackend(), SIGNAL(addInRoster(ndn::Name, ndn::Name::Component)),
496 m_chatroomDiscoveryBackend, SLOT(onAddInRoster(ndn::Name, ndn::Name::Component)));
497 connect(chatDialog->getBackend(), SIGNAL(eraseInRoster(ndn::Name, ndn::Name::Component)),
498 m_chatroomDiscoveryBackend, SLOT(onEraseInRoster(ndn::Name, ndn::Name::Component)));
499
500
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800501 QAction* chatAction = new QAction(chatroomName, this);
502 m_chatActionList[chatroomName.toStdString()] = chatAction;
Mengjin Yan391d7242014-08-28 20:51:55 -0700503 connect(chatAction, SIGNAL(triggered()),
Yingdi Yue8323b62014-09-02 17:24:15 -0700504 chatDialog, SLOT(onShow()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800505
506 QAction* closeAction = new QAction(chatroomName, this);
507 m_closeActionList[chatroomName.toStdString()] = closeAction;
Yingdi Yud45777b2014-10-16 23:54:11 -0700508 connect(closeAction, SIGNAL(triggered()),
509 chatDialog, SLOT(shutdown()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800510
511 updateMenu();
512}
513
Mengjin Yan391d7242014-08-28 20:51:55 -0700514void
515Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd)
516{
517 emit discoverChatroomChanged(info, isAdd);
518}
519
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800520void
521Controller::onIdentityUpdated(const QString& identity)
522{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700523 while (!m_chatDialogList.empty()) {
Davide Pesavento7676b562020-12-14 00:41:26 -0500524 auto it = m_chatDialogList.begin();
Yingdi Yu1fd4e4a2015-02-02 13:37:50 -0800525 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700526 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800527
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800528 emit closeDBModule();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700529
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700530 Name identityName(identity.toStdString());
531 m_identity = identityName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800532
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700533 QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800534}
535
536void
537Controller::onIdentityUpdatedContinued()
538{
539 QString connection = m_db.connectionName();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800540 QSqlDatabase::removeDatabase(connection);
541 m_db.close();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700542
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800543 openDB();
544
545 emit identityUpdated(QString(m_identity.toUri().c_str()));
546}
547
548void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800549Controller::onNickUpdated(const QString& nick)
550{
551 m_nick = nick.toStdString();
552}
553
554void
555Controller::onLocalPrefixUpdated(const QString& localPrefix)
556{
Yingdi Yubaaaec02014-09-02 22:05:32 -0700557 QString privateLocalPrefix("/private/local");
558
Yingdi Yubaaaec02014-09-02 22:05:32 -0700559 if (privateLocalPrefix != localPrefix)
560 m_localPrefixDetected = true;
561 else
562 m_localPrefixDetected = false;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700563
564 m_localPrefix = Name(localPrefix.toStdString());
Yingdi Yubaaaec02014-09-02 22:05:32 -0700565}
566
567void
568Controller::onLocalPrefixConfigured(const QString& prefix)
569{
570 if (!m_localPrefixDetected) {
571 m_localPrefix = Name(prefix.toStdString());
572 emit localPrefixConfigured(prefix);
573 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800574}
575
576void
577Controller::onStartChatAction()
578{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700579 string chatroom = "chatroom-" + getRandomString();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800580
581 m_startChatDialog->setChatroom(chatroom);
582 m_startChatDialog->show();
583 m_startChatDialog->raise();
584}
585
586void
587Controller::onSettingsAction()
588{
589 m_settingDialog->setNick(QString(m_nick.c_str()));
590 m_settingDialog->show();
591 m_settingDialog->raise();
592}
593
594void
595Controller::onProfileEditorAction()
596{
Varun Patil3d850902020-11-23 12:19:14 +0530597 m_profileEditor->resetPanel();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800598 m_profileEditor->show();
599 m_profileEditor->raise();
600}
601
602void
603Controller::onAddContactAction()
604{
605 emit refreshBrowseContact();
606 m_browseContactDialog->show();
607 m_browseContactDialog->raise();
608}
609
610void
611Controller::onContactListAction()
612{
613 m_contactPanel->show();
614 m_contactPanel->raise();
615}
616
617void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800618Controller::onChatroomDiscoveryAction()
619{
620 m_discoveryPanel->show();
621 m_discoveryPanel->raise();
622}
623
624void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800625Controller::onDirectAdd()
626{
627 m_addContactPanel->show();
628 m_addContactPanel->raise();
629}
630
631void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800632Controller::onMinimizeAction()
633{
634 m_settingDialog->hide();
635 m_startChatDialog->hide();
636 m_profileEditor->hide();
637 m_invitationDialog->hide();
638 m_addContactPanel->hide();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800639 m_discoveryPanel->hide();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800640
Davide Pesavento7676b562020-12-14 00:41:26 -0500641 auto it = m_chatDialogList.begin();
642 auto end = m_chatDialogList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700643 for (; it != end; it++)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800644 it->second->hide();
645}
646
647void
648Controller::onQuitAction()
649{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700650 while (!m_chatDialogList.empty()) {
Davide Pesavento7676b562020-12-14 00:41:26 -0500651 auto it = m_chatDialogList.begin();
Yingdi Yud45777b2014-10-16 23:54:11 -0700652 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700653 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800654
655 delete m_settingDialog;
656 delete m_startChatDialog;
657 delete m_profileEditor;
658 delete m_invitationDialog;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700659 delete m_browseContactDialog;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800660 delete m_addContactPanel;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800661 delete m_discoveryPanel;
662 if (m_chatroomDiscoveryBackend->isRunning()) {
663 emit shutdownDiscoveryBackend();
664 m_chatroomDiscoveryBackend->wait();
665 }
666 delete m_chatroomDiscoveryBackend;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700667
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700668 if (m_backend.isRunning()) {
669 emit shutdownBackend();
670 m_backend.wait();
671 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800672
Yingdi Yuf3401182015-02-02 20:21:07 -0800673 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
674 emit shutdownNfdChecker();
675 m_nfdConnectionChecker->wait();
676 }
677
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800678 QApplication::quit();
679}
680
681void
682Controller::onStartChatroom(const QString& chatroomName, bool secured)
683{
684 Name chatroomPrefix;
685 chatroomPrefix.append("ndn")
686 .append("broadcast")
687 .append("ChronoChat")
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800688 .append("Chatroom")
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800689 .append(chatroomName.toStdString());
690
691 // check if the chatroom exists
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700692 if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) {
693 QMessageBox::information(this, tr("ChronoChat"),
694 tr("You are creating an existing chatroom."
695 "You can check it in the context memu."));
696 return;
697 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800698
Mengjin Yan391d7242014-08-28 20:51:55 -0700699 // TODO: We should create a chatroom specific key/cert
700 //(which should be created in the first half of this method
701 //, but let's use the default one for now.
Yingdi Yud45777b2014-10-16 23:54:11 -0700702 Name chatPrefix;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800703 chatPrefix.append(m_identity).append("CHRONOCHAT-CHATDATA").append(chatroomName.toStdString());
Yingdi Yud45777b2014-10-16 23:54:11 -0700704
Mengjin Yan391d7242014-08-28 20:51:55 -0700705 ChatDialog* chatDialog
Yingdi Yud45777b2014-10-16 23:54:11 -0700706 = new ChatDialog(chatroomPrefix,
707 chatPrefix,
708 m_localPrefix,
709 chatroomName.toStdString(),
Yingdi Yu45da92a2015-02-02 13:17:03 -0800710 m_nick,
711 true,
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800712 m_identity,
713 this);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800714
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700715 addChatDialog(chatroomName, chatDialog);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800716 chatDialog->show();
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700717
718 emit addChatroom(chatroomName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800719}
720
721void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800722Controller::onStartChatroom2(chronochat::Invitation invitation, bool secured)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800723{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700724 QString chatroomName = QString::fromStdString(invitation.getChatroom());
725 onStartChatroom(chatroomName, secured);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800726
Davide Pesavento7676b562020-12-14 00:41:26 -0500727 auto it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700728 BOOST_ASSERT(it != m_chatDialogList.end());
729 it->second->addSyncAnchor(invitation);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800730}
731
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700732
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800733void
734Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data)
735{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700736 m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName),
737 QString("<%1>: %2").arg(from).arg(data),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800738 QSystemTrayIcon::Information, 20000);
739 m_trayIcon->setIcon(QIcon(":/images/note.png"));
740}
741
742void
Yingdi Yu233a9722014-03-07 15:47:09 -0800743Controller::onResetIcon()
744{
745 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
746}
747
748void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800749Controller::onRemoveChatDialog(const QString& chatroomName)
750{
Varun Patil3d850902020-11-23 12:19:14 +0530751 emit removeChatroom(chatroomName);
752
Davide Pesavento7676b562020-12-14 00:41:26 -0500753 auto it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700754 if (it != m_chatDialogList.end()) {
755 ChatDialog* deletedChat = it->second;
756 if (deletedChat)
757 delete deletedChat;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800758
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700759 m_chatDialogList.erase(it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800760
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700761 QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
762 QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
763 if (chatAction)
764 delete chatAction;
765 if (closeAction)
766 delete closeAction;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700767
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700768 m_chatActionList.erase(chatroomName.toStdString());
769 m_closeActionList.erase(chatroomName.toStdString());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700770
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700771 updateMenu();
772 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800773}
774
775void
776Controller::onWarning(const QString& msg)
777{
778 QMessageBox::information(this, tr("ChronoChat"), msg);
779}
780
781void
Yingdi Yuf3401182015-02-02 20:21:07 -0800782Controller::onNfdError()
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800783{
Yingdi Yuf3401182015-02-02 20:21:07 -0800784 if (m_isInConnectionDetection)
785 return;
786 // begin a thread
Varun Patil3d850902020-11-23 12:19:14 +0530787
Yingdi Yuf3401182015-02-02 20:21:07 -0800788 m_isInConnectionDetection = true;
789 m_nfdConnectionChecker = new NfdConnectionChecker(this);
790
791 connect(m_nfdConnectionChecker, SIGNAL(nfdConnected()),
792 this, SLOT(onNfdReconnect()));
793 connect(this, SIGNAL(shutdownNfdChecker()),
794 m_nfdConnectionChecker, SLOT(shutdown()));
795
796 m_nfdConnectionChecker->start();
797 QMessageBox::information(this, tr("ChronoChat"), "Nfd is not running");
798}
799
800void
801Controller::onNfdReconnect()
802{
803 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
804 m_nfdConnectionChecker->wait();
805 }
806 delete m_nfdConnectionChecker;
807 m_nfdConnectionChecker = nullptr;
808 m_isInConnectionDetection = false;
809 emit nfdReconnect();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800810}
811
Yingdi Yu233a9722014-03-07 15:47:09 -0800812void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800813Controller::onChatroomInfoRequest(std::string chatroomName, bool isManager)
Mengjin Yan391d7242014-08-28 20:51:55 -0700814{
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800815 auto it = m_chatDialogList.find(chatroomName);
816 if (it != m_chatDialogList.end())
817 emit respondChatroomInfoRequest(it->second->getChatroomInfo(), isManager);
Mengjin Yan391d7242014-08-28 20:51:55 -0700818}
819
Yingdi Yueb692ac2015-02-10 18:46:18 -0800820} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800821
822#if WAF
823#include "controller.moc"
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800824#endif