blob: f0c58c22508e960a290032d252f17ca527fab175 [file] [log] [blame]
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07001#ifndef CHATDIALOG_H
2#define CHATDIALOG_H
Zhenkai Zhub45e38a2012-06-01 15:44:36 -07003#include <boost/function.hpp>
Zhenkai Zhu9036e032012-09-27 20:59:33 -07004#include <boost/thread/recursive_mutex.hpp>
Zhenkai Zhub45e38a2012-06-01 15:44:36 -07005#include <vector>
6#include "digesttreescene.h"
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -07007#include "ui_chatdialog.h"
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -07008#include "chatbuf.pb.h"
Zhenkai Zhu90ee76c2012-06-01 16:00:25 -07009#include <sync-app-socket.h>
Zhenkai Zhu85845d22012-06-01 23:10:43 -070010#include <sync-logic.h>
11#include <sync-seq-no.h>
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070012#include <QSystemTrayIcon>
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070013#include <QQueue>
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070014
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070015#define ORGANIZATION "IRL@UCLA"
Zhenkai Zhu0b3fa332012-09-27 21:58:43 -070016#define APPLICATION "CHRONOS"
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070017#define MAX_HISTORY_ENTRY 20
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070018
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070019class QAction;
20class QMenu;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -070021class QStringListModel;
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -070022class QTextTable;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070023
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -070024class ChatDialog : public QDialog, private Ui::ChatDialog
25{
26 Q_OBJECT
27
28public:
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070029 ChatDialog(QWidget *parent = 0);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070030 ~ChatDialog();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070031 void setVisible(bool visible);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070032 void appendMessage(const SyncDemo::ChatMessage msg, bool isHistory = false);
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070033 void processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo>, Sync::SyncAppSocket *);
34 void processDataWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhubb198112012-09-27 11:31:42 -070035 void processDataNoShowWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070036 void processDataHistoryWrapper(std::string, const char *buf, size_t len);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070037 void processRemoveWrapper(std::string);
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070038 void respondHistoryRequest(std::string interest);
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070039
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070040protected:
41 void closeEvent(QCloseEvent *e);
42 void changeEvent(QEvent *e);
43
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070044public slots:
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070045 void processTreeUpdate(const std::vector<Sync::MissingDataInfo>);
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070046 void processData(QString name, const char *buf, size_t len, bool show, bool isHistory);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070047 void processRemove(QString);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070048
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070049private:
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -070050 void fetchHistory(std::string name);
Zhenkai Zhu59245aa2012-09-26 16:07:04 -070051 QString getRandomString();
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070052 void formChatMessage(const QString &text, SyncDemo::ChatMessage &msg);
Zhenkai Zhub5b78462012-09-28 14:10:37 -070053 void formControlMessage(SyncDemo::ChatMessage &msg, SyncDemo::ChatMessage::ChatMessageType type);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070054 void sendMsg(SyncDemo::ChatMessage &msg);
Zhenkai Zhu82a62752012-06-04 17:11:04 -070055 bool readSettings();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070056 void writeSettings();
57 void updateLabels();
Zhenkai Zhud13acd02012-06-04 15:25:20 -070058 void resizeEvent(QResizeEvent *);
59 void showEvent(QShowEvent *);
60 void fitView();
Zhenkai Zhu21d75f92012-06-04 21:23:34 -070061 void testDraw();
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070062 void createTrayIcon();
63 void createActions();
Zhenkai Zhu560ef1b2012-09-28 14:23:33 -070064 QString formatTime(time_t);
65 void printTimeInCell(QTextTable *, time_t);
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -070066 void disableTreeDisplay();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070067
68private slots:
69 void returnPressed();
Zhenkai Zhu85845d22012-06-01 23:10:43 -070070 void buttonPressed();
Zhenkai Zhub60b7e12012-09-28 11:34:21 -070071 void treeButtonPressed();
Zhenkai Zhue837f792012-06-05 20:47:54 -070072 void checkSetting();
Zhenkai Zhu7e9b06d2012-06-02 00:44:42 -070073 void settingUpdated(QString, QString, QString);
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070074 void sendHello();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070075 void sendJoin();
Zhenkai Zhucf024442012-10-05 10:33:08 -070076 void sendLeave();
Zhenkai Zhu86df7412012-09-27 16:30:20 -070077 void replot();
Zhenkai Zhu25e33e52012-09-28 13:00:07 -070078 void updateRosterList(QStringList);
Zhenkai Zhu0e7a9b22012-10-05 17:55:17 -070079 void enableTreeDisplay();
Alexander Afanasyevebe118d2012-10-08 08:56:34 -070080 void getLocalPrefix();
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070081
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -070082 // icon related
83 void iconActivated(QSystemTrayIcon::ActivationReason reason);
84 void showMessage(QString, QString);
85 void messageClicked();
86
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070087signals:
Zhenkai Zhu24ec4722012-10-07 17:56:38 -070088 void dataReceived(QString name, const char *buf, size_t len, bool show, bool isHistory);
Zhenkai Zhu64f9ede2012-06-05 11:32:00 -070089 void treeUpdated(const std::vector<Sync::MissingDataInfo>);
Zhenkai Zhu2425b6a2012-09-26 17:11:44 -070090 void removeReceived(QString prefix);
Zhenkai Zhu10ccb5a2012-06-04 21:55:14 -070091
Zhenkai Zhu5a8d5aa2012-05-30 21:25:23 -070092private:
Zhenkai Zhu71b42cb2012-06-04 09:42:53 -070093 User m_user;
Zhenkai Zhub45e38a2012-06-01 15:44:36 -070094 Sync::SyncAppSocket *m_sock;
Zhenkai Zhu82a62752012-06-04 17:11:04 -070095 uint32_t m_session;
96 DigestTreeScene *m_scene;
Zhenkai Zhu9036e032012-09-27 20:59:33 -070097 boost::recursive_mutex m_msgMutex;
98 boost::recursive_mutex m_sceneMutex;
Zhenkai Zhu3e26bb42012-09-27 11:04:09 -070099 time_t m_lastMsgTime;
Zhenkai Zhuee5c90f2012-09-27 14:05:41 -0700100 int m_randomizedInterval;
Zhenkai Zhu86df7412012-09-27 16:30:20 -0700101 QTimer *m_timer;
Zhenkai Zhu6082ede2012-09-27 17:28:46 -0700102 QStringListModel *m_rosterModel;
Zhenkai Zhu3974a492012-09-28 14:39:45 -0700103 bool m_minimaniho;
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700104
Zhenkai Zhu6c4fc112012-10-07 17:07:43 -0700105 QQueue<SyncDemo::ChatMessage> m_history;
106 bool m_historyInitialized;
107
Zhenkai Zhu3a008fc2012-06-08 17:36:39 -0700108 // icon related
109 QAction *minimizeAction;
110 QAction *maximizeAction;
111 QAction *restoreAction;
112 QAction *quitAction;
113 QSystemTrayIcon *trayIcon;
114 QMenu *trayIconMenu;
Zhenkai Zhu6d589aa2012-05-29 17:34:35 -0700115};
116#endif