blob: b7edf20d65bcaaf89b453f590ada55fb6d39d92a [file] [log] [blame]
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07001#include <QtGui>
2#include "chatdialog.h"
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -07003#include "settingdialog.h"
Zhenkai Zhuc71da772012-05-30 21:25:23 -07004#include <ctime>
Zhenkai Zhu2c55b382012-05-31 13:27:24 -07005#include <iostream>
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -07006#include <QTimer>
Zhenkai Zhufd52ab72012-05-29 17:34:35 -07007
8ChatDialog::ChatDialog(QWidget *parent)
9 : QDialog(parent)
10{
11 setupUi(this);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070012
13 readSettings();
14 updateLabels();
15
Zhenkai Zhufd52ab72012-05-29 17:34:35 -070016 lineEdit->setFocusPolicy(Qt::StrongFocus);
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070017 DigestTreeScene *scene = new DigestTreeScene();
18
19 treeViewer->setScene(scene);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070020 //scene->plot();
Zhenkai Zhu9ec8f412012-06-01 15:44:36 -070021
Zhenkai Zhuc71da772012-05-30 21:25:23 -070022 connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070023 connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
Zhenkai Zhuc71da772012-05-30 21:25:23 -070024}
25
26void
27ChatDialog::appendMessage(const SyncDemo::ChatMessage &msg)
28{
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070029
30 if (msg.type() != SyncDemo::ChatMessage::CHAT) {
Zhenkai Zhuc71da772012-05-30 21:25:23 -070031 return;
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070032 }
33
34 if (!msg.has_data()) {
35 return;
36 }
37
38 if (msg.from().empty() || msg.data().empty()) {
39 return;
40 }
Zhenkai Zhuc71da772012-05-30 21:25:23 -070041
42 QTextCursor cursor(textEdit->textCursor());
43 cursor.movePosition(QTextCursor::End);
44 QTextTableFormat tableFormat;
45 tableFormat.setBorder(0);
46 QTextTable *table = cursor.insertTable(1, 2, tableFormat);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -070047 QString from = QString("<%1>: ").arg(msg.from().c_str());
48 table->cellAt(0, 0).firstCursorPosition().insertText(from);
49 table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str());
Zhenkai Zhuc71da772012-05-30 21:25:23 -070050 QScrollBar *bar = textEdit->verticalScrollBar();
51 bar->setValue(bar->maximum());
52}
53
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070054void
55ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
Zhenkai Zhu56a88592012-06-04 09:42:53 -070056 msg.set_from(m_user.getNick().toStdString());
57 msg.set_to(m_user.getChatroom().toStdString());
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -070058 msg.set_data(text.toStdString());
59 time_t seconds = time(NULL);
60 msg.set_timestamp(seconds);
Zhenkai Zhuc71da772012-05-30 21:25:23 -070061}
62
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070063void
64ChatDialog::readSettings()
65{
66 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070067 QString nick = s.value("nick", "").toString();
68 QString chatroom = s.value("chatroom", "").toString();
69 QString prefix = s.value("prefix", "").toString();
70 if (nick == "" || chatroom == "" || prefix == "") {
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070071 QTimer::singleShot(500, this, SLOT(buttonPressed()));
72 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -070073 else {
74 m_user.setNick(nick);
75 m_user.setChatroom(chatroom);
76 m_user.setPrefix(prefix);
77 }
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070078}
79
80void
81ChatDialog::writeSettings()
82{
83 QSettings s(ORGANIZATION, APPLICATION);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070084 s.setValue("nick", m_user.getNick());
85 s.setValue("chatroom", m_user.getChatroom());
86 s.setValue("prefix", m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070087}
88
89void
90ChatDialog::updateLabels()
91{
Zhenkai Zhu56a88592012-06-04 09:42:53 -070092 QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070093 infoLabel->setText(settingDisp);
Zhenkai Zhu56a88592012-06-04 09:42:53 -070094 QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -070095 prefixLabel->setText(prefixDisp);
96}
97
Zhenkai Zhuc71da772012-05-30 21:25:23 -070098void
99ChatDialog::returnPressed()
100{
101 QString text = lineEdit->text();
102 if (text.isEmpty())
103 return;
104
Zhenkai Zhu2c55b382012-05-31 13:27:24 -0700105 lineEdit->clear();
106
Zhenkai Zhuc71da772012-05-30 21:25:23 -0700107 SyncDemo::ChatMessage msg;
108 formChatMessage(text, msg);
109
110 // TODO:
111 // send message
112 appendMessage(msg);
Zhenkai Zhu2c55b382012-05-31 13:27:24 -0700113
Zhenkai Zhuc71da772012-05-30 21:25:23 -0700114}
115
116void
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -0700117ChatDialog::buttonPressed()
118{
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700119 SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700120 connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
Zhenkai Zhu275ba3a2012-06-01 23:10:43 -0700121 dialog.exec();
Zhenkai Zhufd52ab72012-05-29 17:34:35 -0700122}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700123
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700124void
125ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
126{
127 bool needWrite = false;
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700128 if (!nick.isEmpty() && nick != m_user.getNick()) {
129 m_user.setNick(nick);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700130 needWrite = true;
131 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700132 if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
133 m_user.setPrefix(prefix);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700134 needWrite = true;
135 // TODO: set the previous prefix as left?
136 }
Zhenkai Zhu56a88592012-06-04 09:42:53 -0700137 if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
138 m_user.setChatroom(chatroom);
Zhenkai Zhu3472e3f2012-06-02 00:44:42 -0700139 needWrite = true;
140 // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
141 }
142 if (needWrite) {
143 writeSettings();
144 updateLabels();
145 }
146}