blob: ca92c82ab61d5a41c61b30c8c01e71f073123e7e [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
Yingdi Yueb692ac2015-02-10 18:46:18 -080012#ifndef CHRONOCHAT_CONTROLLER_HPP
13#define CHRONOCHAT_CONTROLLER_HPP
Yingdi Yu348f5ea2014-03-01 14:47:25 -080014
15#include <QDialog>
16#include <QMenu>
17#include <QSystemTrayIcon>
18#include <QtSql/QSqlDatabase>
19
Yingdi Yu0b0a7362014-08-05 16:31:30 -070020#include "setting-dialog.hpp"
21#include "start-chat-dialog.hpp"
22#include "profile-editor.hpp"
23#include "invitation-dialog.hpp"
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080024#include "invitation-request-dialog.hpp"
Yingdi Yu0b0a7362014-08-05 16:31:30 -070025#include "contact-panel.hpp"
26#include "browse-contact-dialog.hpp"
27#include "add-contact-panel.hpp"
28#include "chat-dialog.hpp"
Qiuhan Ding43c8e162015-02-02 15:16:48 -080029#include "chatroom-discovery-backend.hpp"
30#include "discovery-panel.hpp"
Yingdi Yuf3401182015-02-02 20:21:07 -080031#include "nfd-connection-checker.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080032
33#ifndef Q_MOC_RUN
Yingdi Yu0b0a7362014-08-05 16:31:30 -070034#include "common.hpp"
Yingdi Yu2c9e7712014-10-20 11:55:05 -070035#include "invitation.hpp"
36#include "controller-backend.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080037#endif
38
Yingdi Yueb692ac2015-02-10 18:46:18 -080039namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080040
41class Controller : public QDialog
42{
43 Q_OBJECT
44
45public: // public methods
Yingdi Yuf3401182015-02-02 20:21:07 -080046 Controller(QWidget* parent = nullptr);
Yingdi Yufa0b6a02014-04-30 14:26:42 -070047
Yingdi Yu348f5ea2014-03-01 14:47:25 -080048 virtual
49 ~Controller();
50
51private: // private methods
52 std::string
53 getDBName();
54
55 void
56 openDB();
57
58 void
59 initialize();
60
61 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080062 loadConf();
63
64 void
65 saveConf();
66
Yingdi Yufa0b6a02014-04-30 14:26:42 -070067 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080068 createActions();
69
70 void
71 createTrayIcon();
72
73 void
74 updateMenu();
75
Yingdi Yu348f5ea2014-03-01 14:47:25 -080076 std::string
77 getRandomString();
78
Yingdi Yu348f5ea2014-03-01 14:47:25 -080079 void
80 addChatDialog(const QString& chatroomName, ChatDialog* chatDialog);
81
Mengjin Yan391d7242014-08-28 20:51:55 -070082 void
Yingdi Yueb692ac2015-02-10 18:46:18 -080083 updateDiscoveryList(const chronochat::ChatroomInfo& chatroomName, bool isAdd);
Mengjin Yan391d7242014-08-28 20:51:55 -070084
Yingdi Yu348f5ea2014-03-01 14:47:25 -080085signals:
86 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070087 shutdownBackend();
88
89 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -080090 shutdownDiscoveryBackend();
91
92 void
Yingdi Yu2c9e7712014-10-20 11:55:05 -070093 updateLocalPrefix();
94
95 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -080096 closeDBModule();
97
98 void
99 localPrefixUpdated(const QString& localPrefix);
100
101 void
Yingdi Yubaaaec02014-09-02 22:05:32 -0700102 localPrefixConfigured(const QString& prefix);
103
104 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800105 identityUpdated(const QString& identity);
106
107 void
108 refreshBrowseContact();
109
Yingdi Yu233a9722014-03-07 15:47:09 -0800110 void
Mengjin Yan391d7242014-08-28 20:51:55 -0700111 invitationInterest(const ndn::Name& prefix, const ndn::Interest& interest,
112 size_t routingPrefixOffset);
113
114 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800115 discoverChatroomChanged(const chronochat::ChatroomInfo& chatroomInfo, bool isAdd);
Yingdi Yu233a9722014-03-07 15:47:09 -0800116
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700117 void
118 addChatroom(QString chatroomName);
119
120 void
121 removeChatroom(QString chatroomName);
122
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800123 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800124 respondChatroomInfoRequest(ChatroomInfo chatroomInfo, bool isManager);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800125
126 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800127 nfdReconnect();
128
129 void
130 shutdownNfdChecker();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800131
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800132private slots:
133 void
134 onIdentityUpdated(const QString& identity);
135
136 void
137 onIdentityUpdatedContinued();
138
139 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800140 onNickUpdated(const QString& nick);
141
142 void
143 onLocalPrefixUpdated(const QString& localPrefix);
144
145 void
Yingdi Yubaaaec02014-09-02 22:05:32 -0700146 onLocalPrefixConfigured(const QString& prefix);
147
148 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800149 onStartChatAction();
150
151 void
152 onSettingsAction();
153
154 void
155 onProfileEditorAction();
156
157 void
158 onAddContactAction();
159
160 void
161 onContactListAction();
162
163 void
164 onDirectAdd();
165
166 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800167 onMinimizeAction();
168
169 void
170 onQuitAction();
171
172 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800173 onChatroomDiscoveryAction();
174
175 void
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800176 onStartChatroom(const QString& chatroom, bool secured);
177
178 void
Yingdi Yueb692ac2015-02-10 18:46:18 -0800179 onStartChatroom2(chronochat::Invitation invitation, bool secured);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800180
181 void
182 onShowChatMessage(const QString& chatroomName, const QString& from, const QString& data);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700183
Yingdi Yu233a9722014-03-07 15:47:09 -0800184 void
185 onResetIcon();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800186
187 void
188 onRemoveChatDialog(const QString& chatroom);
189
190 void
191 onWarning(const QString& msg);
192
193 void
Yingdi Yuf3401182015-02-02 20:21:07 -0800194 onNfdError();
195
196 void
197 onNfdReconnect();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800198
Yingdi Yu233a9722014-03-07 15:47:09 -0800199 void
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800200 onChatroomInfoRequest(std::string chatroomName, bool isManager);
Mengjin Yan391d7242014-08-28 20:51:55 -0700201
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800202private: // private member
203 typedef std::map<std::string, QAction*> ChatActionList;
204 typedef std::map<std::string, ChatDialog*> ChatDialogList;
205
206 // Communication
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700207 Name m_localPrefix;
Yingdi Yubaaaec02014-09-02 22:05:32 -0700208 bool m_localPrefixDetected;
Yingdi Yuf3401182015-02-02 20:21:07 -0800209 bool m_isInConnectionDetection;
Mengjin Yan391d7242014-08-28 20:51:55 -0700210
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800211 // Tray
212 QAction* m_startChatroom;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800213 //QAction* m_discoveryAction;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800214 QAction* m_minimizeAction;
215 QAction* m_settingsAction;
216 QAction* m_editProfileAction;
217 QAction* m_contactListAction;
218 QAction* m_addContactAction;
219 QAction* m_updateLocalPrefixAction;
220 QAction* m_quitAction;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800221 QAction* m_chatroomDiscoveryAction;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800222 QMenu* m_trayIconMenu;
223 QMenu* m_closeMenu;
224 QSystemTrayIcon* m_trayIcon;
225 ChatActionList m_chatActionList;
226 ChatActionList m_closeActionList;
227
228 // Dialogs
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800229 SettingDialog* m_settingDialog;
230 StartChatDialog* m_startChatDialog;
231 ProfileEditor* m_profileEditor;
232 InvitationDialog* m_invitationDialog;
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800233 InvitationRequestDialog* m_invitationRequestDialog;
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800234 ContactPanel* m_contactPanel;
235 BrowseContactDialog* m_browseContactDialog;
236 AddContactPanel* m_addContactPanel;
237 ChatDialogList m_chatDialogList;
238 DiscoveryPanel* m_discoveryPanel;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800239
240 // Conf
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700241 Name m_identity;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800242 std::string m_nick;
243 QSqlDatabase m_db;
244
Yingdi Yu2c9e7712014-10-20 11:55:05 -0700245 // Backend
Yingdi Yuf3401182015-02-02 20:21:07 -0800246 ControllerBackend m_backend;
247 ChatroomDiscoveryBackend* m_chatroomDiscoveryBackend;
248 NfdConnectionChecker* m_nfdConnectionChecker;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800249};
250
Yingdi Yueb692ac2015-02-10 18:46:18 -0800251} // namespace chronochat
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800252
Yingdi Yueb692ac2015-02-10 18:46:18 -0800253#endif // CHRONOCHAT_CONTROLLER_HPP