blob: ba6c6c8aac27f3004eec4358035f8134d071359f [file] [log] [blame]
Alexander Afanasyevb4b92292013-07-09 13:54:59 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * GNU v3.0 license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070013#ifndef CHATDIALOG_H
14#define CHATDIALOG_H
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070015
16#ifndef Q_MOC_RUN
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070017#include <boost/function.hpp>
Zhenkai Zhu9036e032012-09-27 20:59:33 -070018#include <boost/thread/recursive_mutex.hpp>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070019
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070020#include <vector>
21#include "digesttreescene.h"
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070022#include "ui_chatdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070023#include "chatbuf.pb.h"
Alexander Afanasyev69b0d202013-07-14 11:47:23 -070024#include <ccnx/sync-app-socket.h>
Zhenkai Zhu85845d22012-06-01 23:10:43 -070025#include <sync-logic.h>
26#include <sync-seq-no.h>
Alexander Afanasyevf829f4d2013-05-07 15:59:36 -070027#endif
28
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070029#include <QSystemTrayIcon>
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070030#include <QMainWindow>
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070031#include <QQueue>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070032
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070033#define ORGANIZATION "irl.cs.ucla.edu"
34#define APPLICATION "ChronoChat"
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070035#define MAX_HISTORY_ENTRY 20
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070036
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070037class QAction;
38class QMenu;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070039class QStringListModel;
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -070040class QTextTable;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070041
Alexander Afanasyevb4b92292013-07-09 13:54:59 -070042class ChatDialog : public QDialog, private Ui::ChatDialog
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070043{
44 Q_OBJECT
45
46public:
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070047 ChatDialog(QWidget *parent = 0);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070048 ~ChatDialog();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070049 void setVisible(bool visible);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070050 void appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070051 void processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncAppSocket *);
52 void processDataWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhubb198112012-09-27 11:31:42 -070053 void processDataNoShowWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070054 void processDataHistoryWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070055 void processRemoveWrapper(std::string);
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070056 void respondHistoryRequest(std::string interest);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070057
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070058protected:
59 void closeEvent(QCloseEvent *e);
60 void changeEvent(QEvent *e);
61
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070062public slots:
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070063 void processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070064 void processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070065 void processRemove(QString);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070066
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070067private:
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070068 void fetchHistory(std::string name);
Zhenkai Zhu59245aa2012-09-26 16:07:04 -070069 QString getRandomString();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070070 void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
Zhenkai Zhub5b78462012-09-28 14:10:37 -070071 void formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070072 void sendMsg(SyncDemo::ChatMessage &msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070073 bool readSettings();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070074 void writeSettings();
75 void updateLabels();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070076 void resizeEvent(QResizeEvent *);
77 void showEvent(QShowEvent *);
78 void fitView();
Zhenkai Zhu21d75f92012-06-04 21:23:34 -070079 void testDraw();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070080 void createTrayIcon();
81 void createActions();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -070082 QString formatTime(time_t);
83 void printTimeInCell(QTextTable *, time_t);
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -070084 void disableTreeDisplay();
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -070085 bool getLocalPrefix();
Zhenkai Zhud616b582012-10-10 00:04:07 -070086 void initializeSync();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070087
88private slots:
89 void returnPressed();
Zhenkai Zhu85845d22012-06-01 23:10:43 -070090 void buttonPressed();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -070091 void treeButtonPressed();
Zhenkai Zhue837f792012-06-05 20:47:54 -070092 void checkSetting();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070093 void settingUpdated(QString, QString, QString);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070094 void sendHello();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070095 void sendJoin();
Zhenkai Zhucf024442012-10-05 10:33:08 -070096 void sendLeave();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070097 void replot();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070098 void updateRosterList(QStringList);
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -070099 void enableTreeDisplay();
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700100 void updateLocalPrefix();
Zhenkai Zhu7f52e1b2012-10-09 11:45:36 -0700101 void reap();
102 void summonReaper();
Zhenkai Zhu86581412012-10-08 16:58:39 -0700103
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700104 // icon related
105 void iconActivated(QSystemTrayIcon::ActivationReason reason);
106 void showMessage(QString, QString);
107 void messageClicked();
108
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700109signals:
Zhenkai Zhu24ec4722012-10-07 17:56:38 -0700110 void dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -0700111 void treeUpdated(const std::vector<Sync::MissingDataInfo>);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -0700112 void removeReceived(QString prefix);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -0700113
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -0700114private:
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -0700115 User m_user;
Zhenkai Zhub45e38a2012-06-01 15:44:36 -0700116 Sync::SyncAppSocket *m_sock;
Zhenkai Zhu82a62752012-06-04 17:11:04 -0700117 uint32_t m_session;
118 DigestTreeScene *m_scene;
Zhenkai Zhu9036e032012-09-27 20:59:33 -0700119 boost::recursive_mutex m_msgMutex;
120 boost::recursive_mutex m_sceneMutex;
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -0700121 time_t m_lastMsgTime;
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700122 int m_randomizedInterval;
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700123 QTimer *m_timer;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700124 QStringListModel *m_rosterModel;
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700125 bool m_minimaniho;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700126
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700127 QQueue<SyncDemo::ChatMessage> m_history;
128 bool m_historyInitialized;
Zhenkai Zhu0fe2b8c2012-10-09 04:49:28 -0700129 bool m_joined;
Zhenkai Zhu7f52e1b2012-10-09 11:45:36 -0700130
131 QList<QString> m_zombieList;
132 int m_zombieIndex;
Zhenkai Zhud616b582012-10-10 00:04:07 -0700133
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700134 // icon related
135 QAction *minimizeAction;
136 QAction *maximizeAction;
137 QAction *restoreAction;
Alexander Afanasyevb4b92292013-07-09 13:54:59 -0700138 QAction *settingsAction;
139 QAction *updateLocalPrefixAction;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700140 QAction *quitAction;
141 QSystemTrayIcon *trayIcon;
Alexander Afanasyevb4b92292013-07-09 13:54:59 -0700142 // QMenu *menuBar;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700143 QMenu *trayIconMenu;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700144};
145#endif