blob: b8b2c971a45d54693188ff427cd3364eeb3a10b5 [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"
13#include "cryptopp.hpp"
14#include "conf.hpp"
15#include "endorse-info.hpp"
16
Yingdi Yu348f5ea2014-03-01 14:47:25 -080017#include <QApplication>
18#include <QMessageBox>
19#include <QDir>
20#include <QTimer>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080021
Yingdi Yu348f5ea2014-03-01 14:47:25 -080022#include <boost/filesystem.hpp>
Yingdi Yufa0b6a02014-04-30 14:26:42 -070023#include <ndn-cxx/util/random.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080024
25Q_DECLARE_METATYPE(ndn::Name)
Varun Patil3d850902020-11-23 12:19:14 +053026Q_DECLARE_METATYPE(ndn::security::Certificate)
Yingdi Yueb692ac2015-02-10 18:46:18 -080027Q_DECLARE_METATYPE(chronochat::EndorseInfo)
Yingdi Yu233a9722014-03-07 15:47:09 -080028Q_DECLARE_METATYPE(ndn::Interest)
29Q_DECLARE_METATYPE(size_t)
Yingdi Yueb692ac2015-02-10 18:46:18 -080030Q_DECLARE_METATYPE(chronochat::ChatroomInfo)
31Q_DECLARE_METATYPE(chronochat::Invitation)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080032Q_DECLARE_METATYPE(std::string)
33Q_DECLARE_METATYPE(ndn::Name::Component)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080034
Yingdi Yueb692ac2015-02-10 18:46:18 -080035namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080036
Yingdi Yu0b0a7362014-08-05 16:31:30 -070037using std::string;
38
Yingdi Yu348f5ea2014-03-01 14:47:25 -080039// constructor & destructor
Yingdi Yu2c9e7712014-10-20 11:55:05 -070040Controller::Controller(QWidget* parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080041 : QDialog(parent)
Yingdi Yubaaaec02014-09-02 22:05:32 -070042 , m_localPrefixDetected(false)
Yingdi Yuf3401182015-02-02 20:21:07 -080043 , m_isInConnectionDetection(false)
Qiuhan Ding43c8e162015-02-02 15:16:48 -080044 , m_settingDialog(new SettingDialog(this))
45 , m_startChatDialog(new StartChatDialog(this))
46 , m_profileEditor(new ProfileEditor(this))
47 , m_invitationDialog(new InvitationDialog(this))
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080048 , m_invitationRequestDialog(new InvitationRequestDialog(this))
Qiuhan Ding43c8e162015-02-02 15:16:48 -080049 , m_contactPanel(new ContactPanel(this))
50 , m_browseContactDialog(new BrowseContactDialog(this))
51 , m_addContactPanel(new AddContactPanel(this))
52 , m_discoveryPanel(new DiscoveryPanel(this))
Yingdi Yu348f5ea2014-03-01 14:47:25 -080053{
54 qRegisterMetaType<ndn::Name>("ndn.Name");
Varun Patil3d850902020-11-23 12:19:14 +053055 qRegisterMetaType<ndn::security::Certificate>("ndn.security.v2.Certificate");
56 qRegisterMetaType<chronochat::EndorseInfo>("EndorseInfo");
Yingdi Yu233a9722014-03-07 15:47:09 -080057 qRegisterMetaType<ndn::Interest>("ndn.Interest");
58 qRegisterMetaType<size_t>("size_t");
Qiuhan Ding43c8e162015-02-02 15:16:48 -080059 qRegisterMetaType<chronochat::ChatroomInfo>("chronochat.Chatroom");
60 qRegisterMetaType<chronochat::Invitation>("chronochat.Invitation");
61 qRegisterMetaType<std::string>("std.string");
62 qRegisterMetaType<ndn::Name::Component>("ndn.Component");
63
Yingdi Yu348f5ea2014-03-01 14:47:25 -080064
65 // Connection to ContactManager
Yingdi Yu2c9e7712014-10-20 11:55:05 -070066 connect(m_backend.getContactManager(), SIGNAL(warning(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067 this, SLOT(onWarning(const QString&)));
68 connect(this, SIGNAL(refreshBrowseContact()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070069 m_backend.getContactManager(), SLOT(onRefreshBrowseContact()));
70 connect(m_backend.getContactManager(), SIGNAL(contactInfoFetchFailed(const QString&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -080071 this, SLOT(onWarning(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080072
73 // Connection to SettingDialog
74 connect(this, SIGNAL(identityUpdated(const QString&)),
75 m_settingDialog, SLOT(onIdentityUpdated(const QString&)));
76 connect(m_settingDialog, SIGNAL(identityUpdated(const QString&)),
77 this, SLOT(onIdentityUpdated(const QString&)));
78 connect(m_settingDialog, SIGNAL(nickUpdated(const QString&)),
79 this, SLOT(onNickUpdated(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -070080 connect(m_settingDialog, SIGNAL(prefixUpdated(const QString&)),
81 this, SLOT(onLocalPrefixConfigured(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080082
83 // Connection to ProfileEditor
84 connect(this, SIGNAL(closeDBModule()),
85 m_profileEditor, SLOT(onCloseDBModule()));
86 connect(this, SIGNAL(identityUpdated(const QString&)),
87 m_profileEditor, SLOT(onIdentityUpdated(const QString&)));
88 connect(m_profileEditor, SIGNAL(updateProfile()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070089 m_backend.getContactManager(), SLOT(onUpdateProfile()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080090
91 // Connection to StartChatDialog
92 connect(m_startChatDialog, SIGNAL(startChatroom(const QString&, bool)),
93 this, SLOT(onStartChatroom(const QString&, bool)));
94
95 // Connection to InvitationDialog
96 connect(m_invitationDialog, SIGNAL(invitationResponded(const ndn::Name&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -070097 &m_backend, SLOT(onInvitationResponded(const ndn::Name&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080098
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080099 // Connection to InvitationRequestDialog
100 connect(m_invitationRequestDialog, SIGNAL(invitationRequestResponded(const ndn::Name&, bool)),
101 &m_backend, SLOT(onInvitationRequestResponded(const ndn::Name&, bool)));
102
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800103 // Connection to AddContactPanel
104 connect(m_addContactPanel, SIGNAL(fetchInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700105 m_backend.getContactManager(), SLOT(onFetchContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800106 connect(m_addContactPanel, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700107 m_backend.getContactManager(), SLOT(onAddFetchedContact(const QString&)));
108 connect(m_backend.getContactManager(),
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800109 SIGNAL(contactEndorseInfoReady(const EndorseInfo&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700110 m_addContactPanel,
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800111 SLOT(onContactEndorseInfoReady(const EndorseInfo&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800112
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800113 // Connection to BrowseContactDialog
114 connect(m_browseContactDialog, SIGNAL(directAddClicked()),
115 this, SLOT(onDirectAdd()));
116 connect(m_browseContactDialog, SIGNAL(fetchIdCert(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700117 m_backend.getContactManager(), SLOT(onFetchIdCert(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800118 connect(m_browseContactDialog, SIGNAL(addContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700119 m_backend.getContactManager(), SLOT(onAddFetchedContactIdCert(const QString&)));
120 connect(m_backend.getContactManager(), SIGNAL(idCertNameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800121 m_browseContactDialog, SLOT(onIdCertNameListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700122 connect(m_backend.getContactManager(), SIGNAL(nameListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800123 m_browseContactDialog, SLOT(onNameListReady(const QStringList&)));
Varun Patil3d850902020-11-23 12:19:14 +0530124 connect(m_backend.getContactManager(), SIGNAL(idCertReady(const ndn::security::Certificate&)),
125 m_browseContactDialog, SLOT(onIdCertReady(const ndn::security::Certificate&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800126
127 // Connection to ContactPanel
128 connect(m_contactPanel, SIGNAL(waitForContactList()),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700129 m_backend.getContactManager(), SLOT(onWaitForContactList()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130 connect(m_contactPanel, SIGNAL(waitForContactInfo(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700131 m_backend.getContactManager(), SLOT(onWaitForContactInfo(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800132 connect(m_contactPanel, SIGNAL(removeContact(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700133 m_backend.getContactManager(), SLOT(onRemoveContact(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134 connect(m_contactPanel, SIGNAL(updateAlias(const QString&, const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700135 m_backend.getContactManager(), SLOT(onUpdateAlias(const QString&, const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800136 connect(m_contactPanel, SIGNAL(updateIsIntroducer(const QString&, bool)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700137 m_backend.getContactManager(), SLOT(onUpdateIsIntroducer(const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800138 connect(m_contactPanel, SIGNAL(updateEndorseCertificate(const QString&)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700139 m_backend.getContactManager(), SLOT(onUpdateEndorseCertificate(const QString&)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800140 connect(m_contactPanel, SIGNAL(warning(const QString&)),
141 this, SLOT(onWarning(const QString&)));
142 connect(this, SIGNAL(closeDBModule()),
143 m_contactPanel, SLOT(onCloseDBModule()));
144 connect(this, SIGNAL(identityUpdated(const QString&)),
145 m_contactPanel, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700146 connect(m_backend.getContactManager(), SIGNAL(contactAliasListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800147 m_contactPanel, SLOT(onContactAliasListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700148 connect(m_backend.getContactManager(), SIGNAL(contactIdListReady(const QStringList&)),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800149 m_contactPanel, SLOT(onContactIdListReady(const QStringList&)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700150 connect(m_backend.getContactManager(), SIGNAL(contactInfoReady(const QString&, const QString&,
151 const QString&, bool)),
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700152 m_contactPanel, SLOT(onContactInfoReady(const QString&, const QString&,
153 const QString&, bool)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700155 // Connection to backend thread
Yingdi Yuf3401182015-02-02 20:21:07 -0800156 connect(&m_backend, SIGNAL(nfdError()),
157 this, SLOT(onNfdError()));
158 connect(this, SIGNAL(nfdReconnect()),
159 &m_backend, SLOT(onNfdReconnect()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700160 connect(this, SIGNAL(shutdownBackend()),
161 &m_backend, SLOT(shutdown()));
162 connect(this, SIGNAL(updateLocalPrefix()),
163 &m_backend, SLOT(onUpdateLocalPrefixAction()));
164 connect(this, SIGNAL(identityUpdated(const QString&)),
165 &m_backend, SLOT(onIdentityChanged(const QString&)));
166 connect(this, SIGNAL(addChatroom(QString)),
167 &m_backend, SLOT(addChatroom(QString)));
168 connect(this, SIGNAL(removeChatroom(QString)),
169 &m_backend, SLOT(removeChatroom(QString)));
170
171 // Thread notifications:
172 // on local prefix udpated:
173 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
174 this, SLOT(onLocalPrefixUpdated(const QString&)));
175 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
176 m_settingDialog, SLOT(onLocalPrefixUpdated(const QString&)));
177
178 // on invitation validated:
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800179 connect(&m_backend, SIGNAL(invitationValidated(QString, QString, ndn::Name)),
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700180 m_invitationDialog, SLOT(onInvitationReceived(QString, QString, ndn::Name)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800181 connect(&m_backend, SIGNAL(startChatroom(const QString&, bool)),
182 this, SLOT(onStartChatroom(const QString&, bool)));
183
184 // on invitation request received
185 connect(&m_backend, SIGNAL(invitationRequestReceived(QString, QString, ndn::Name)),
186 m_invitationRequestDialog, SLOT(onInvitationRequestReceived(QString, QString,
187 ndn::Name)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700188
189 // on invitation accepted:
Yingdi Yueb692ac2015-02-10 18:46:18 -0800190 connect(&m_backend, SIGNAL(startChatroomOnInvitation(chronochat::Invitation, bool)),
191 this, SLOT(onStartChatroom2(chronochat::Invitation, bool)));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700192
193 m_backend.start();
Mengjin Yan391d7242014-08-28 20:51:55 -0700194
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800195 initialize();
196
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800197 m_chatroomDiscoveryBackend
198 = new ChatroomDiscoveryBackend(m_localPrefix,
199 m_identity,
200 this);
201
202 // connect to chatroom discovery back end
203 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
204 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
205 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
206 m_chatroomDiscoveryBackend, SLOT(updateRoutingPrefix(const QString&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800207 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoRequest(std::string, bool)),
208 this, SLOT(onChatroomInfoRequest(std::string, bool)));
209 connect(this, SIGNAL(respondChatroomInfoRequest(ChatroomInfo, bool)),
210 m_chatroomDiscoveryBackend, SLOT(onRespondChatroomInfoRequest(ChatroomInfo, bool)));
211 connect(this, SIGNAL(shutdownDiscoveryBackend()),
212 m_chatroomDiscoveryBackend, SLOT(shutdown()));
213 connect(this, SIGNAL(identityUpdated(const QString&)),
214 m_chatroomDiscoveryBackend, SLOT(onIdentityUpdated(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800215 connect(this, SIGNAL(nfdReconnect()),
216 m_chatroomDiscoveryBackend, SLOT(onNfdReconnect()));
217 connect(m_chatroomDiscoveryBackend, SIGNAL(nfdError()),
218 this, SLOT(onNfdError()));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800219
220 // connect chatroom discovery back end with front end
221 connect(m_discoveryPanel, SIGNAL(waitForChatroomInfo(const QString&)),
222 m_chatroomDiscoveryBackend, SLOT(onWaitForChatroomInfo(const QString&)));
223 connect(m_discoveryPanel, SIGNAL(warning(const QString&)),
224 this, SLOT(onWarning(const QString&)));
225 connect(this, SIGNAL(identityUpdated(const QString&)),
226 m_discoveryPanel, SLOT(onIdentityUpdated(const QString&)));
227 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomListReady(const QStringList&)),
228 m_discoveryPanel, SLOT(onChatroomListReady(const QStringList&)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800229 connect(m_chatroomDiscoveryBackend, SIGNAL(chatroomInfoReady(const ChatroomInfo&, bool)),
230 m_discoveryPanel, SLOT(onChatroomInfoReady(const ChatroomInfo&, bool)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800231 connect(m_discoveryPanel, SIGNAL(startChatroom(const QString&, bool)),
232 this, SLOT(onStartChatroom(const QString&, bool)));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800233 connect(m_discoveryPanel, SIGNAL(sendInvitationRequest(const QString&, const QString&)),
234 &m_backend, SLOT(onSendInvitationRequest(const QString&, const QString&)));
235 connect(&m_backend, SIGNAL(invitationRequestResult(const std::string&)),
236 m_discoveryPanel, SLOT(onInvitationRequestResult(const std::string&)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800237
238 m_chatroomDiscoveryBackend->start();
239
Yingdi Yu233a9722014-03-07 15:47:09 -0800240 createTrayIcon();
241
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700242 emit updateLocalPrefix();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800243}
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700244
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800245Controller::~Controller()
246{
247 saveConf();
248}
249
250// public methods
251
252
253// private methods
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700254string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800255Controller::getDBName()
256{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700257 string dbName("chronos-");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800258
259 std::stringstream ss;
260 {
261 using namespace CryptoPP;
262
263 SHA256 hash;
264 StringSource(m_identity.wireEncode().wire(), m_identity.wireEncode().size(), true,
265 new HashFilter(hash, new HexEncoder(new FileSink(ss), false)));
266 }
267 dbName.append(ss.str()).append(".db");
268
269 return dbName;
270}
271
272void
273Controller::openDB()
274{
275 m_db = QSqlDatabase::addDatabase("QSQLITE");
276 QString path = (QDir::home().path());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700277 path.append(QDir::separator())
278 .append(".chronos")
279 .append(QDir::separator())
280 .append(getDBName().c_str());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800281 m_db.setDatabaseName(path);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800282
Yingdi Yu42125862014-08-07 17:04:28 -0700283 m_db.open();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800284}
285
286void
287Controller::initialize()
288{
289 loadConf();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700290
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800291 openDB();
292
293 emit identityUpdated(QString(m_identity.toUri().c_str()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800294}
295
296void
297Controller::loadConf()
298{
299 namespace fs = boost::filesystem;
300
301 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
302 fs::create_directories (chronosDir);
303
304 std::ifstream is((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800305 Conf conf;
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800306 Block confBlock;
307 try {
308 confBlock = ndn::Block::fromStream(is);
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800309 conf.wireDecode(confBlock);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700310 m_identity.clear();
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800311 m_identity.append(conf.getIdentity());
312 if (conf.getNick().length() != 0)
313 m_nick = conf.getNick();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700314 else
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800315 m_nick = m_identity.get(-1).toUri();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700316 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530317 catch (const tlv::Error&) {
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700318 try {
319 ndn::KeyChain keyChain;
Varun Patil3d850902020-11-23 12:19:14 +0530320 m_identity = keyChain.getPib().getDefaultIdentity().getName();
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700321 }
Varun Patila24bd3e2020-11-24 10:08:33 +0530322 catch (const ndn::security::pib::Pib::Error&) {
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700323 m_identity.clear();
324 m_identity.append("chronochat-tmp-identity")
Varun Patil3d850902020-11-23 12:19:14 +0530325 .append(getRandomString());
Qiuhan Dinge86f3192015-04-07 17:52:59 -0700326 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700327 m_nick = m_identity.get(-1).toUri();
328 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800329}
330
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700331void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800332Controller::saveConf()
333{
334 namespace fs = boost::filesystem;
335
336 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
337 fs::create_directories (chronosDir);
338
339 std::ofstream os((chronosDir / "config").c_str ());
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800340 Conf conf;
341 conf.setIdentity(m_identity);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700342 if (!m_nick.empty())
Qiuhan Ding0cfc1512015-02-17 17:44:11 -0800343 conf.setNick(m_nick);
344 Block confWire = conf.wireEncode();
345 os.write(reinterpret_cast<const char*>(confWire.wire()), confWire.size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800346
347 os.close();
348}
349
350void
351Controller::createActions()
352{
353 m_startChatroom = new QAction(tr("Start new chat"), this);
354 connect(m_startChatroom, SIGNAL(triggered()), this, SLOT(onStartChatAction()));
355
356 m_settingsAction = new QAction(tr("Settings"), this);
357 connect(m_settingsAction, SIGNAL(triggered()), this, SLOT(onSettingsAction()));
358
359 m_editProfileAction = new QAction(tr("Edit profile"), this);
360 connect(m_editProfileAction, SIGNAL(triggered()), this, SLOT(onProfileEditorAction()));
361
362 m_contactListAction = new QAction(tr("Contact List"), this);
363 connect(m_contactListAction, SIGNAL(triggered()), this, SLOT(onContactListAction()));
364
365 m_addContactAction = new QAction(tr("Add contact"), this);
366 connect(m_addContactAction, SIGNAL(triggered()), this, SLOT(onAddContactAction()));
367
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800368 m_chatroomDiscoveryAction = new QAction(tr("Chatroom Discovery"), this);
369 connect(m_chatroomDiscoveryAction, SIGNAL(triggered()), this, SLOT(onChatroomDiscoveryAction()));
370
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800371 m_updateLocalPrefixAction = new QAction(tr("Update local prefix"), this);
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700372 connect(m_updateLocalPrefixAction, SIGNAL(triggered()),
373 &m_backend, SLOT(onUpdateLocalPrefixAction()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800374
375 m_minimizeAction = new QAction(tr("Mi&nimize"), this);
376 connect(m_minimizeAction, SIGNAL(triggered()), this, SLOT(onMinimizeAction()));
377
378 m_quitAction = new QAction(tr("Quit"), this);
379 connect(m_quitAction, SIGNAL(triggered()), this, SLOT(onQuitAction()));
Mengjin Yan391d7242014-08-28 20:51:55 -0700380
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800381}
382
383void
384Controller::createTrayIcon()
385{
386 createActions();
387
388 m_trayIconMenu = new QMenu(this);
389 m_trayIconMenu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800390 m_trayIconMenu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700391
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800392 m_trayIconMenu->addSeparator();
393 m_trayIconMenu->addAction(m_settingsAction);
394 m_trayIconMenu->addAction(m_editProfileAction);
395 m_trayIconMenu->addSeparator();
396 m_trayIconMenu->addAction(m_contactListAction);
397 m_trayIconMenu->addAction(m_addContactAction);
398 m_trayIconMenu->addSeparator();
399 m_trayIconMenu->addAction(m_updateLocalPrefixAction);
400 m_trayIconMenu->addSeparator();
401 m_trayIconMenu->addAction(m_minimizeAction);
402 m_closeMenu = m_trayIconMenu->addMenu("Close chatroom");
403 m_closeMenu->setEnabled(false);
404 m_trayIconMenu->addSeparator();
405 m_trayIconMenu->addAction(m_quitAction);
406
407 m_trayIcon = new QSystemTrayIcon(this);
408 m_trayIcon->setContextMenu(m_trayIconMenu);
409
410 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
411 m_trayIcon->setToolTip("ChronoChat System Tray Icon");
412 m_trayIcon->setVisible(true);
413}
414
415void
416Controller::updateMenu()
417{
Varun Patil3d850902020-11-23 12:19:14 +0530418 m_trayIconMenu->clear();
419 QMenu* menu = m_trayIconMenu;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700420
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800421 menu->addAction(m_startChatroom);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800422 menu->addAction(m_chatroomDiscoveryAction);
Mengjin Yan391d7242014-08-28 20:51:55 -0700423
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800424 menu->addSeparator();
425 menu->addAction(m_settingsAction);
426 menu->addAction(m_editProfileAction);
427 menu->addSeparator();
Yingdi Yu233a9722014-03-07 15:47:09 -0800428 menu->addAction(m_contactListAction);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800429 menu->addAction(m_addContactAction);
430 menu->addSeparator();
431 {
432 ChatActionList::const_iterator it = m_chatActionList.begin();
433 ChatActionList::const_iterator end = m_chatActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700434 if (it != end) {
435 for (; it != end; it++)
436 menu->addAction(it->second);
437 menu->addSeparator();
438 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800439 }
440 menu->addAction(m_updateLocalPrefixAction);
441 menu->addSeparator();
442 menu->addAction(m_minimizeAction);
Varun Patil3d850902020-11-23 12:19:14 +0530443 m_closeMenu = menu->addMenu("Close chatroom");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800444 {
445 ChatActionList::const_iterator it = m_closeActionList.begin();
446 ChatActionList::const_iterator end = m_closeActionList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700447 if (it == end)
Varun Patil3d850902020-11-23 12:19:14 +0530448 m_closeMenu->setEnabled(false);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800449 else
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700450 for (; it != end; it++)
Varun Patil3d850902020-11-23 12:19:14 +0530451 m_closeMenu->addAction(it->second);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800452 }
453 menu->addSeparator();
454 menu->addAction(m_quitAction);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800455}
456
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700457string
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800458Controller::getRandomString()
459{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700460 uint32_t r = ndn::random::generateWord32();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800461 std::stringstream ss;
462 {
463 using namespace CryptoPP;
464 StringSource(reinterpret_cast<uint8_t*>(&r), 4, true,
465 new HexEncoder(new FileSink(ss), false));
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700466
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800467 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700468 // for (int i = 0; i < 8; i++)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800469 // {
470 // uint32_t t = r & mask;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700471 // if (t < 10)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800472 // ss << static_cast<char>(t + 0x30);
473 // else
474 // ss << static_cast<char>(t + 0x57);
475 // r = r >> 4;
476 // }
477
478 return ss.str();
479}
480
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800481void
482Controller::addChatDialog(const QString& chatroomName, ChatDialog* chatDialog)
483{
484 m_chatDialogList[chatroomName.toStdString()] = chatDialog;
485 connect(chatDialog, SIGNAL(closeChatDialog(const QString&)),
486 this, SLOT(onRemoveChatDialog(const QString&)));
487 connect(chatDialog, SIGNAL(showChatMessage(const QString&, const QString&, const QString&)),
488 this, SLOT(onShowChatMessage(const QString&, const QString&, const QString&)));
Yingdi Yu233a9722014-03-07 15:47:09 -0800489 connect(chatDialog, SIGNAL(resetIcon()),
490 this, SLOT(onResetIcon()));
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700491 connect(&m_backend, SIGNAL(localPrefixUpdated(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700492 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yubaaaec02014-09-02 22:05:32 -0700493 connect(this, SIGNAL(localPrefixConfigured(const QString&)),
Yingdi Yud45777b2014-10-16 23:54:11 -0700494 chatDialog->getBackend(), SLOT(updateRoutingPrefix(const QString&)));
Yingdi Yuf3401182015-02-02 20:21:07 -0800495 connect(this, SIGNAL(nfdReconnect()),
496 chatDialog->getBackend(), SLOT(onNfdReconnect()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800497
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800498 // connect chat dialog with discovery backend
Yingdi Yuf3401182015-02-02 20:21:07 -0800499 connect(chatDialog->getBackend(), SIGNAL(nfdError()),
500 this, SLOT(onNfdError()));
501 connect(chatDialog->getBackend(), SIGNAL(newChatroomForDiscovery(ndn::Name::Component)),
502 m_chatroomDiscoveryBackend, SLOT(onNewChatroomForDiscovery(ndn::Name::Component)));
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800503 connect(chatDialog->getBackend(), SIGNAL(addInRoster(ndn::Name, ndn::Name::Component)),
504 m_chatroomDiscoveryBackend, SLOT(onAddInRoster(ndn::Name, ndn::Name::Component)));
505 connect(chatDialog->getBackend(), SIGNAL(eraseInRoster(ndn::Name, ndn::Name::Component)),
506 m_chatroomDiscoveryBackend, SLOT(onEraseInRoster(ndn::Name, ndn::Name::Component)));
507
508
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800509 QAction* chatAction = new QAction(chatroomName, this);
510 m_chatActionList[chatroomName.toStdString()] = chatAction;
Mengjin Yan391d7242014-08-28 20:51:55 -0700511 connect(chatAction, SIGNAL(triggered()),
Yingdi Yue8323b62014-09-02 17:24:15 -0700512 chatDialog, SLOT(onShow()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800513
514 QAction* closeAction = new QAction(chatroomName, this);
515 m_closeActionList[chatroomName.toStdString()] = closeAction;
Yingdi Yud45777b2014-10-16 23:54:11 -0700516 connect(closeAction, SIGNAL(triggered()),
517 chatDialog, SLOT(shutdown()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800518
519 updateMenu();
520}
521
Mengjin Yan391d7242014-08-28 20:51:55 -0700522void
523Controller::updateDiscoveryList(const ChatroomInfo& info, bool isAdd)
524{
525 emit discoverChatroomChanged(info, isAdd);
526}
527
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800528void
529Controller::onIdentityUpdated(const QString& identity)
530{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700531 while (!m_chatDialogList.empty()) {
532 ChatDialogList::const_iterator it = m_chatDialogList.begin();
Yingdi Yu1fd4e4a2015-02-02 13:37:50 -0800533 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700534 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800535
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800536 emit closeDBModule();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700537
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700538 Name identityName(identity.toStdString());
539 m_identity = identityName;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800540
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700541 QTimer::singleShot(500, this, SLOT(onIdentityUpdatedContinued()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800542}
543
544void
545Controller::onIdentityUpdatedContinued()
546{
547 QString connection = m_db.connectionName();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800548 QSqlDatabase::removeDatabase(connection);
549 m_db.close();
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700550
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800551 openDB();
552
553 emit identityUpdated(QString(m_identity.toUri().c_str()));
554}
555
556void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800557Controller::onNickUpdated(const QString& nick)
558{
559 m_nick = nick.toStdString();
560}
561
562void
563Controller::onLocalPrefixUpdated(const QString& localPrefix)
564{
Yingdi Yubaaaec02014-09-02 22:05:32 -0700565 QString privateLocalPrefix("/private/local");
566
Yingdi Yubaaaec02014-09-02 22:05:32 -0700567 if (privateLocalPrefix != localPrefix)
568 m_localPrefixDetected = true;
569 else
570 m_localPrefixDetected = false;
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700571
572 m_localPrefix = Name(localPrefix.toStdString());
Yingdi Yubaaaec02014-09-02 22:05:32 -0700573}
574
575void
576Controller::onLocalPrefixConfigured(const QString& prefix)
577{
578 if (!m_localPrefixDetected) {
579 m_localPrefix = Name(prefix.toStdString());
580 emit localPrefixConfigured(prefix);
581 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800582}
583
584void
585Controller::onStartChatAction()
586{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700587 string chatroom = "chatroom-" + getRandomString();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800588
589 m_startChatDialog->setChatroom(chatroom);
590 m_startChatDialog->show();
591 m_startChatDialog->raise();
592}
593
594void
595Controller::onSettingsAction()
596{
597 m_settingDialog->setNick(QString(m_nick.c_str()));
598 m_settingDialog->show();
599 m_settingDialog->raise();
600}
601
602void
603Controller::onProfileEditorAction()
604{
Varun Patil3d850902020-11-23 12:19:14 +0530605 m_profileEditor->resetPanel();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800606 m_profileEditor->show();
607 m_profileEditor->raise();
608}
609
610void
611Controller::onAddContactAction()
612{
613 emit refreshBrowseContact();
614 m_browseContactDialog->show();
615 m_browseContactDialog->raise();
616}
617
618void
619Controller::onContactListAction()
620{
621 m_contactPanel->show();
622 m_contactPanel->raise();
623}
624
625void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800626Controller::onChatroomDiscoveryAction()
627{
628 m_discoveryPanel->show();
629 m_discoveryPanel->raise();
630}
631
632void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800633Controller::onDirectAdd()
634{
635 m_addContactPanel->show();
636 m_addContactPanel->raise();
637}
638
639void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800640Controller::onMinimizeAction()
641{
642 m_settingDialog->hide();
643 m_startChatDialog->hide();
644 m_profileEditor->hide();
645 m_invitationDialog->hide();
646 m_addContactPanel->hide();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800647 m_discoveryPanel->hide();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800648
649 ChatDialogList::iterator it = m_chatDialogList.begin();
650 ChatDialogList::iterator end = m_chatDialogList.end();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700651 for (; it != end; it++)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800652 it->second->hide();
653}
654
655void
656Controller::onQuitAction()
657{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700658 while (!m_chatDialogList.empty()) {
659 ChatDialogList::const_iterator it = m_chatDialogList.begin();
Yingdi Yud45777b2014-10-16 23:54:11 -0700660 it->second->shutdown();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700661 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800662
663 delete m_settingDialog;
664 delete m_startChatDialog;
665 delete m_profileEditor;
666 delete m_invitationDialog;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700667 delete m_browseContactDialog;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800668 delete m_addContactPanel;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800669 delete m_discoveryPanel;
670 if (m_chatroomDiscoveryBackend->isRunning()) {
671 emit shutdownDiscoveryBackend();
672 m_chatroomDiscoveryBackend->wait();
673 }
674 delete m_chatroomDiscoveryBackend;
Yingdi Yuf4aaa8b2014-03-10 11:24:31 -0700675
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700676 if (m_backend.isRunning()) {
677 emit shutdownBackend();
678 m_backend.wait();
679 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800680
Yingdi Yuf3401182015-02-02 20:21:07 -0800681 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
682 emit shutdownNfdChecker();
683 m_nfdConnectionChecker->wait();
684 }
685
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800686 QApplication::quit();
687}
688
689void
690Controller::onStartChatroom(const QString& chatroomName, bool secured)
691{
692 Name chatroomPrefix;
693 chatroomPrefix.append("ndn")
694 .append("broadcast")
695 .append("ChronoChat")
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800696 .append("Chatroom")
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800697 .append(chatroomName.toStdString());
698
699 // check if the chatroom exists
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700700 if (m_chatDialogList.find(chatroomName.toStdString()) != m_chatDialogList.end()) {
701 QMessageBox::information(this, tr("ChronoChat"),
702 tr("You are creating an existing chatroom."
703 "You can check it in the context memu."));
704 return;
705 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800706
Mengjin Yan391d7242014-08-28 20:51:55 -0700707 // TODO: We should create a chatroom specific key/cert
708 //(which should be created in the first half of this method
709 //, but let's use the default one for now.
Yingdi Yud45777b2014-10-16 23:54:11 -0700710 Name chatPrefix;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800711 chatPrefix.append(m_identity).append("CHRONOCHAT-CHATDATA").append(chatroomName.toStdString());
Yingdi Yud45777b2014-10-16 23:54:11 -0700712
Mengjin Yan391d7242014-08-28 20:51:55 -0700713 ChatDialog* chatDialog
Yingdi Yud45777b2014-10-16 23:54:11 -0700714 = new ChatDialog(chatroomPrefix,
715 chatPrefix,
716 m_localPrefix,
717 chatroomName.toStdString(),
Yingdi Yu45da92a2015-02-02 13:17:03 -0800718 m_nick,
719 true,
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800720 m_identity,
721 this);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800722
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700723 addChatDialog(chatroomName, chatDialog);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800724 chatDialog->show();
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700725
726 emit addChatroom(chatroomName);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800727}
728
729void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800730Controller::onStartChatroom2(chronochat::Invitation invitation, bool secured)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800731{
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700732 QString chatroomName = QString::fromStdString(invitation.getChatroom());
733 onStartChatroom(chatroomName, secured);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800734
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700735 ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800736
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700737 BOOST_ASSERT(it != m_chatDialogList.end());
738 it->second->addSyncAnchor(invitation);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800739}
740
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700741
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800742void
743Controller::onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data)
744{
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700745 m_trayIcon->showMessage(QString("Chatroom %1 has a new message").arg(chatroomName),
746 QString("<%1>: %2").arg(from).arg(data),
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800747 QSystemTrayIcon::Information, 20000);
748 m_trayIcon->setIcon(QIcon(":/images/note.png"));
749}
750
751void
Yingdi Yu233a9722014-03-07 15:47:09 -0800752Controller::onResetIcon()
753{
754 m_trayIcon->setIcon(QIcon(":/images/icon_small.png"));
755}
756
757void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800758Controller::onRemoveChatDialog(const QString& chatroomName)
759{
Varun Patil3d850902020-11-23 12:19:14 +0530760 emit removeChatroom(chatroomName);
761
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800762 ChatDialogList::iterator it = m_chatDialogList.find(chatroomName.toStdString());
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700763 if (it != m_chatDialogList.end()) {
764 ChatDialog* deletedChat = it->second;
765 if (deletedChat)
766 delete deletedChat;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800767
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700768 m_chatDialogList.erase(it);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800769
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700770 QAction* chatAction = m_chatActionList[chatroomName.toStdString()];
771 QAction* closeAction = m_closeActionList[chatroomName.toStdString()];
772 if (chatAction)
773 delete chatAction;
774 if (closeAction)
775 delete closeAction;
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700776
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700777 m_chatActionList.erase(chatroomName.toStdString());
778 m_closeActionList.erase(chatroomName.toStdString());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700779
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700780 updateMenu();
781 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800782}
783
784void
785Controller::onWarning(const QString& msg)
786{
787 QMessageBox::information(this, tr("ChronoChat"), msg);
788}
789
790void
Yingdi Yuf3401182015-02-02 20:21:07 -0800791Controller::onNfdError()
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800792{
Yingdi Yuf3401182015-02-02 20:21:07 -0800793 if (m_isInConnectionDetection)
794 return;
795 // begin a thread
Varun Patil3d850902020-11-23 12:19:14 +0530796
Yingdi Yuf3401182015-02-02 20:21:07 -0800797 m_isInConnectionDetection = true;
798 m_nfdConnectionChecker = new NfdConnectionChecker(this);
799
800 connect(m_nfdConnectionChecker, SIGNAL(nfdConnected()),
801 this, SLOT(onNfdReconnect()));
802 connect(this, SIGNAL(shutdownNfdChecker()),
803 m_nfdConnectionChecker, SLOT(shutdown()));
804
805 m_nfdConnectionChecker->start();
806 QMessageBox::information(this, tr("ChronoChat"), "Nfd is not running");
807}
808
809void
810Controller::onNfdReconnect()
811{
812 if (m_nfdConnectionChecker != nullptr && m_nfdConnectionChecker->isRunning()) {
813 m_nfdConnectionChecker->wait();
814 }
815 delete m_nfdConnectionChecker;
816 m_nfdConnectionChecker = nullptr;
817 m_isInConnectionDetection = false;
818 emit nfdReconnect();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800819}
820
Yingdi Yu233a9722014-03-07 15:47:09 -0800821void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800822Controller::onChatroomInfoRequest(std::string chatroomName, bool isManager)
Mengjin Yan391d7242014-08-28 20:51:55 -0700823{
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800824 auto it = m_chatDialogList.find(chatroomName);
825 if (it != m_chatDialogList.end())
826 emit respondChatroomInfoRequest(it->second->getChatroomInfo(), isManager);
Mengjin Yan391d7242014-08-28 20:51:55 -0700827}
828
Yingdi Yueb692ac2015-02-10 18:46:18 -0800829} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800830
831#if WAF
832#include "controller.moc"
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800833#endif