Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- 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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 9 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 11 | */ |
| 12 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 13 | #include "chat-dialog.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 14 | #include "ui_chat-dialog.h" |
| 15 | |
| 16 | #include <QScrollBar> |
| 17 | #include <QMessageBox> |
| 18 | #include <QCloseEvent> |
| 19 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 20 | Q_DECLARE_METATYPE(ndn::Name) |
| 21 | Q_DECLARE_METATYPE(time_t) |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 22 | Q_DECLARE_METATYPE(std::vector<chronochat::NodeInfo>) |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 23 | Q_DECLARE_METATYPE(uint64_t) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 25 | namespace chronochat { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 26 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 27 | static const Name PRIVATE_PREFIX("/private/local"); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 28 | static const ndn::Name::Component ROUTING_HINT_SEPARATOR = |
| 29 | ndn::name::Component::fromEscapedString("%F0%2E"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 31 | ChatDialog::ChatDialog(const Name& chatroomPrefix, |
| 32 | const Name& userChatPrefix, |
| 33 | const Name& routingPrefix, |
| 34 | const std::string& chatroomName, |
Yingdi Yu | 17032f8 | 2014-03-25 15:48:23 -0700 | [diff] [blame] | 35 | const std::string& nick, |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 36 | bool isSecured, |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 37 | const Name& signingId, |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 38 | QWidget* parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 39 | : QDialog(parent) |
| 40 | , ui(new Ui::ChatDialog) |
Yingdi Yu | 45da92a | 2015-02-02 13:17:03 -0800 | [diff] [blame] | 41 | , m_backend(chatroomPrefix, userChatPrefix, routingPrefix, chatroomName, nick, signingId) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 42 | , m_chatroomName(chatroomName) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 43 | , m_chatroomPrefix(chatroomPrefix) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 44 | , m_nick(nick.c_str()) |
| 45 | , m_isSecured(isSecured) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 46 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 47 | qRegisterMetaType<ndn::Name>("ndn::Name"); |
| 48 | qRegisterMetaType<time_t>("time_t"); |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 49 | qRegisterMetaType<std::vector<chronochat::NodeInfo> >("std::vector<chronochat::NodeInfo>"); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 50 | qRegisterMetaType<uint64_t>("uint64_t"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 51 | |
| 52 | m_scene = new DigestTreeScene(this); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 53 | m_trustScene = new TrustTreeScene(this); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 54 | m_rosterModel = new QStringListModel(this); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 55 | |
| 56 | ui->setupUi(this); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 58 | ui->syncTreeViewer->setScene(m_scene); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 59 | m_scene->setSceneRect(m_scene->itemsBoundingRect()); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 60 | ui->syncTreeViewer->hide(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 61 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 62 | ui->trustTreeViewer->setScene(m_trustScene); |
| 63 | m_trustScene->setSceneRect(m_trustScene->itemsBoundingRect()); |
| 64 | ui->trustTreeViewer->hide(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 65 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 66 | ui->listView->setModel(m_rosterModel); |
| 67 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 68 | Name routablePrefix; |
Yingdi Yu | d85e29f | 2014-09-09 17:20:04 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 70 | if (routingPrefix.isPrefixOf(userChatPrefix)) |
| 71 | routablePrefix = userChatPrefix; |
| 72 | else |
| 73 | routablePrefix.append(routingPrefix) |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 74 | .append(ROUTING_HINT_SEPARATOR) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 75 | .append(userChatPrefix); |
Yingdi Yu | d85e29f | 2014-09-09 17:20:04 -0700 | [diff] [blame] | 76 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 77 | updateLabels(routablePrefix); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 78 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 79 | QStringList roster; |
| 80 | roster << "- " + m_nick; |
| 81 | m_rosterModel->setStringList(roster); |
| 82 | |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 83 | ui->syncTreeButton->setText("Hide ChronoSync Tree"); |
| 84 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 85 | // When backend receives a sync update, notify frontend to update sync tree |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 86 | connect(&m_backend, SIGNAL(syncTreeUpdated(std::vector<chronochat::NodeInfo>, QString)), |
| 87 | this, SLOT(updateSyncTree(std::vector<chronochat::NodeInfo>, QString))); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 88 | |
| 89 | // When backend receives a new chat message, notify frontent to print it out. |
| 90 | connect(&m_backend, SIGNAL(chatMessageReceived(QString, QString, time_t)), |
| 91 | this, SLOT(receiveChatMessage(QString, QString, time_t))); |
| 92 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 93 | // When backend detects a deleted session, notify frontend to print the message. |
| 94 | connect(&m_backend, SIGNAL(sessionRemoved(QString, QString, time_t)), |
| 95 | this, SLOT(removeSession(QString, QString, time_t))); |
| 96 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 97 | // When backend receives a new message, notify frontend to print notification |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 98 | connect(&m_backend, SIGNAL(messageReceived(QString, QString, uint64_t, time_t, bool)), |
| 99 | this, SLOT(receiveMessage(QString, QString, uint64_t, time_t, bool))); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 100 | |
| 101 | // When backend updates prefix, notify frontend to update labels. |
| 102 | connect(&m_backend, SIGNAL(chatPrefixChanged(ndn::Name)), |
| 103 | this, SLOT(updateLabels(ndn::Name))); |
| 104 | |
| 105 | // When frontend gets a message to send, notify backend. |
| 106 | connect(this, SIGNAL(msgToSent(QString, time_t)), |
| 107 | &m_backend, SLOT(sendChatMessage(QString, time_t))); |
| 108 | |
| 109 | // When frontend gets a shutdown command, notify backend. |
| 110 | connect(this, SIGNAL(shutdownBackend()), |
| 111 | &m_backend, SLOT(shutdown())); |
| 112 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 113 | m_scene->plot("Empty"); |
| 114 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 115 | connect(ui->lineEdit, SIGNAL(returnPressed()), |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 116 | this, SLOT(onReturnPressed())); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 117 | connect(ui->syncTreeButton, SIGNAL(pressed()), |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 118 | this, SLOT(onSyncTreeButtonPressed())); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 119 | connect(ui->trustTreeButton, SIGNAL(pressed()), |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 120 | this, SLOT(onTrustTreeButtonPressed())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 121 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 122 | disableSyncTreeDisplay(); |
| 123 | QTimer::singleShot(2200, this, SLOT(enableSyncTreeDisplay())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 124 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 125 | m_backend.start(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
| 129 | ChatDialog::~ChatDialog() |
| 130 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 133 | void |
| 134 | ChatDialog::closeEvent(QCloseEvent *e) |
| 135 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 136 | // When close button is clicked, do not close the dialog immediately. |
| 137 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 138 | QMessageBox::information(this, tr("ChronoChat"), |
| 139 | tr("The chatroom will keep running in the " |
| 140 | "system tray. To close the chatroom, " |
| 141 | "choose <b>Close chatroom</b> in the " |
| 142 | "context memu of the system tray entry.")); |
Yingdi Yu | e8323b6 | 2014-09-02 17:24:15 -0700 | [diff] [blame] | 143 | hide(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 144 | e->ignore(); |
| 145 | } |
| 146 | |
| 147 | void |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 148 | ChatDialog::changeEvent(QEvent *e) |
| 149 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 150 | switch(e->type()) { |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 151 | case QEvent::ActivationChange: |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 152 | if (isActiveWindow()) { |
Yingdi Yu | 233a972 | 2014-03-07 15:47:09 -0800 | [diff] [blame] | 153 | emit resetIcon(); |
| 154 | } |
| 155 | break; |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 162 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 163 | { |
| 164 | fitView(); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | ChatDialog::showEvent(QShowEvent *e) |
| 169 | { |
| 170 | fitView(); |
| 171 | } |
| 172 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 173 | ChatroomInfo |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 174 | ChatDialog::getChatroomInfo() |
| 175 | { |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 176 | ChatroomInfo chatroomInfo; |
| 177 | chatroomInfo.setName(Name::Component(m_chatroomName)); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 178 | QStringList prefixList = m_scene->getRosterPrefixList(); |
| 179 | for(QStringList::iterator it = prefixList.begin(); |
| 180 | it != prefixList.end(); ++it ) { |
| 181 | Name participant = Name(it->toStdString()).getPrefix(-3); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 182 | chatroomInfo.addParticipant(participant); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 185 | chatroomInfo.setSyncPrefix(m_chatroomPrefix); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 186 | if (m_isSecured) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 187 | chatroomInfo.setTrustModel(ChatroomInfo::TRUST_MODEL_HIERARCHICAL); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 188 | else |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 189 | chatroomInfo.setTrustModel(ChatroomInfo::TRUST_MODEL_NONE); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 190 | return chatroomInfo; |
| 191 | } |
| 192 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 193 | // private methods: |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 194 | void ChatDialog::disableSyncTreeDisplay() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 195 | { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 196 | ui->syncTreeButton->setEnabled(false); |
| 197 | ui->syncTreeViewer->hide(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 198 | fitView(); |
| 199 | } |
| 200 | |
| 201 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 202 | ChatDialog::appendChatMessage(const QString& nick, const QString& text, time_t timestamp) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 203 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 204 | QTextCharFormat nickFormat; |
| 205 | nickFormat.setForeground(Qt::darkGreen); |
| 206 | nickFormat.setFontWeight(QFont::Bold); |
| 207 | nickFormat.setFontUnderline(true); |
| 208 | nickFormat.setUnderlineColor(Qt::gray); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 209 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 210 | // Print who & when |
| 211 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 212 | cursor.movePosition(QTextCursor::End); |
| 213 | QTextTableFormat tableFormat; |
| 214 | tableFormat.setBorder(0); |
| 215 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
| 216 | QString from = QString("%1 ").arg(nick); |
| 217 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 218 | fromCell.setFormat(nickFormat); |
| 219 | fromCell.firstCursorPosition().insertText(from); |
| 220 | printTimeInCell(table, timestamp); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 221 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 222 | // Print what |
| 223 | QTextCursor nextCursor(ui->textEdit->textCursor()); |
| 224 | nextCursor.movePosition(QTextCursor::End); |
| 225 | table = nextCursor.insertTable(1, 1, tableFormat); |
| 226 | table->cellAt(0, 0).firstCursorPosition().insertText(text); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 227 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 228 | // Popup notification |
| 229 | showMessage(from, text); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 230 | |
| 231 | QScrollBar *bar = ui->textEdit->verticalScrollBar(); |
| 232 | bar->setValue(bar->maximum()); |
| 233 | } |
| 234 | |
| 235 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 236 | ChatDialog::appendControlMessage(const QString& nick, |
| 237 | const QString& action, |
| 238 | time_t timestamp) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 239 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 240 | QTextCharFormat nickFormat; |
| 241 | nickFormat.setForeground(Qt::gray); |
| 242 | nickFormat.setFontWeight(QFont::Bold); |
| 243 | nickFormat.setFontUnderline(true); |
| 244 | nickFormat.setUnderlineColor(Qt::gray); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 245 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 246 | QTextCursor cursor(ui->textEdit->textCursor()); |
| 247 | cursor.movePosition(QTextCursor::End); |
| 248 | QTextTableFormat tableFormat; |
| 249 | tableFormat.setBorder(0); |
| 250 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 251 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 252 | QString controlMsg = QString("%1 %2 ").arg(nick).arg(action); |
| 253 | QTextTableCell fromCell = table->cellAt(0, 0); |
| 254 | fromCell.setFormat(nickFormat); |
| 255 | fromCell.firstCursorPosition().insertText(controlMsg); |
| 256 | printTimeInCell(table, timestamp); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | QString |
| 260 | ChatDialog::formatTime(time_t timestamp) |
| 261 | { |
Qiuhan Ding | 4b8cfaa | 2015-03-10 17:03:48 -0700 | [diff] [blame] | 262 | struct tm* localTime = localtime(×tamp); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 263 | |
Qiuhan Ding | 4b8cfaa | 2015-03-10 17:03:48 -0700 | [diff] [blame] | 264 | return QString("%1:%2:%3") |
| 265 | .arg(localTime->tm_hour, 2, 10, QChar('0')) |
| 266 | .arg(localTime->tm_min, 2, 10, QChar('0')) |
| 267 | .arg(localTime->tm_sec, 2, 10, QChar('0')); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void |
| 271 | ChatDialog::printTimeInCell(QTextTable *table, time_t timestamp) |
| 272 | { |
| 273 | QTextCharFormat timeFormat; |
| 274 | timeFormat.setForeground(Qt::gray); |
| 275 | timeFormat.setFontUnderline(true); |
| 276 | timeFormat.setUnderlineColor(Qt::gray); |
| 277 | QTextTableCell timeCell = table->cellAt(0, 1); |
| 278 | timeCell.setFormat(timeFormat); |
| 279 | timeCell.firstCursorPosition().insertText(formatTime(timestamp)); |
| 280 | } |
| 281 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 282 | void |
| 283 | ChatDialog::showMessage(const QString& from, const QString& data) |
| 284 | { |
| 285 | if (!isActiveWindow()) |
| 286 | emit showChatMessage(QString::fromStdString(m_chatroomName), |
| 287 | from, data); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | ChatDialog::fitView() |
| 292 | { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 293 | QRectF rect = m_scene->itemsBoundingRect(); |
| 294 | m_scene->setSceneRect(rect); |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 295 | ui->syncTreeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
| 296 | |
| 297 | QRectF trustRect = m_trustScene->itemsBoundingRect(); |
| 298 | m_trustScene->setSceneRect(trustRect); |
| 299 | ui->trustTreeViewer->fitInView(m_trustScene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 302 | // public slots: |
| 303 | void |
Yingdi Yu | e8323b6 | 2014-09-02 17:24:15 -0700 | [diff] [blame] | 304 | ChatDialog::onShow() |
| 305 | { |
| 306 | this->show(); |
| 307 | this->raise(); |
| 308 | this->activateWindow(); |
| 309 | } |
| 310 | |
| 311 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 312 | ChatDialog::shutdown() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 313 | { |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 314 | if (m_backend.isRunning()) { |
| 315 | emit shutdownBackend(); |
| 316 | m_backend.wait(); |
| 317 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 318 | hide(); |
| 319 | emit closeChatDialog(QString::fromStdString(m_chatroomName)); |
| 320 | } |
| 321 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 322 | // private slots: |
| 323 | void |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 324 | ChatDialog::updateSyncTree(std::vector<chronochat::NodeInfo> updates, QString rootDigest) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 325 | { |
| 326 | m_scene->processSyncUpdate(updates, rootDigest); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | ChatDialog::receiveChatMessage(QString nick, QString text, time_t timestamp) |
| 331 | { |
| 332 | appendChatMessage(nick, text, timestamp); |
| 333 | } |
| 334 | |
| 335 | void |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 336 | ChatDialog::removeSession(QString sessionPrefix, QString nick, time_t timestamp) |
| 337 | { |
| 338 | appendControlMessage(nick, "leaves room", timestamp); |
| 339 | m_scene->removeNode(sessionPrefix); |
| 340 | m_rosterModel->setStringList(m_scene->getRosterList()); |
Qiuhan Ding | 112ee48 | 2015-03-11 11:54:11 -0700 | [diff] [blame] | 341 | fitView(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 345 | ChatDialog::receiveMessage(QString sessionPrefix, QString nick, uint64_t seqNo, time_t timestamp, |
| 346 | bool addSession) |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 347 | { |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 348 | m_scene->updateNode(sessionPrefix, nick, seqNo); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 349 | m_scene->messageReceived(sessionPrefix); |
Qiuhan Ding | 7a4e7ef | 2015-02-03 20:25:50 -0800 | [diff] [blame] | 350 | if (addSession) { |
| 351 | appendControlMessage(nick, "enters room", timestamp); |
| 352 | m_rosterModel->setStringList(m_scene->getRosterList()); |
| 353 | } |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void |
| 357 | ChatDialog::updateLabels(Name newChatPrefix) |
| 358 | { |
| 359 | // Reset DigestTree |
| 360 | m_scene->clearAll(); |
| 361 | m_scene->plot("Empty"); |
| 362 | |
| 363 | // Display chatroom name |
| 364 | QString chatroomName = QString("Chatroom: %1").arg(QString::fromStdString(m_chatroomName)); |
| 365 | ui->infoLabel->setStyleSheet("QLabel {color: #630; font-size: 16px; font: bold \"Verdana\";}"); |
| 366 | ui->infoLabel->setText(chatroomName); |
| 367 | |
| 368 | // Display chat message prefix |
| 369 | QString chatPrefix; |
| 370 | if (PRIVATE_PREFIX.isPrefixOf(newChatPrefix)) { |
| 371 | chatPrefix = |
| 372 | QString("<Warning: no connection to hub or hub does not support prefix autoconfig.>\n" |
| 373 | "<Prefix = %1>") |
| 374 | .arg(QString::fromStdString(newChatPrefix.toUri())); |
| 375 | ui->prefixLabel->setStyleSheet( |
| 376 | "QLabel {color: red; font-size: 12px; font: bold \"Verdana\";}"); |
| 377 | } |
| 378 | else { |
| 379 | chatPrefix = QString("<Prefix = %1>") |
| 380 | .arg(QString::fromStdString(newChatPrefix.toUri())); |
| 381 | ui->prefixLabel->setStyleSheet( |
| 382 | "QLabel {color: Green; font-size: 12px; font: bold \"Verdana\";}"); |
| 383 | } |
| 384 | ui->prefixLabel->setText(chatPrefix); |
Qiuhan Ding | 112ee48 | 2015-03-11 11:54:11 -0700 | [diff] [blame] | 385 | fitView(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 389 | ChatDialog::onReturnPressed() |
| 390 | { |
| 391 | QString text = ui->lineEdit->text(); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 392 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 393 | if (text.isEmpty()) |
| 394 | return; |
| 395 | |
| 396 | ui->lineEdit->clear(); |
| 397 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 398 | time_t timestamp = |
| 399 | static_cast<time_t>(time::toUnixTimestamp(time::system_clock::now()).count() / 1000); |
| 400 | // appendChatMessage(m_nick, text, timestamp); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 401 | |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 402 | emit msgToSent(text, timestamp); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 403 | |
| 404 | fitView(); |
| 405 | } |
| 406 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 407 | void |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 408 | ChatDialog::onSyncTreeButtonPressed() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 409 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 410 | if (ui->syncTreeViewer->isVisible()) { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 411 | ui->syncTreeViewer->hide(); |
| 412 | ui->syncTreeButton->setText("Show ChronoSync Tree"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 413 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 414 | else { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 415 | ui->syncTreeViewer->show(); |
| 416 | ui->syncTreeButton->setText("Hide ChronoSync Tree"); |
| 417 | } |
| 418 | |
| 419 | fitView(); |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | ChatDialog::onTrustTreeButtonPressed() |
| 424 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 425 | if (ui->trustTreeViewer->isVisible()) { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 426 | ui->trustTreeViewer->hide(); |
| 427 | ui->trustTreeButton->setText("Show Trust Tree"); |
| 428 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 429 | else { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 430 | ui->trustTreeViewer->show(); |
| 431 | ui->trustTreeButton->setText("Hide Trust Tree"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | fitView(); |
| 435 | } |
| 436 | |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 437 | void ChatDialog::enableSyncTreeDisplay() |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 438 | { |
Yingdi Yu | f4aaa8b | 2014-03-10 11:24:31 -0700 | [diff] [blame] | 439 | ui->syncTreeButton->setEnabled(true); |
Yingdi Yu | d45777b | 2014-10-16 23:54:11 -0700 | [diff] [blame] | 440 | ui->syncTreeViewer->show(); |
| 441 | fitView(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 444 | } // namespace chronochat |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 445 | |
Mengjin Yan | aec7074 | 2014-08-25 10:37:45 -0700 | [diff] [blame] | 446 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 447 | #if WAF |
| 448 | #include "chat-dialog.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 449 | // #include "chat-dialog.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 450 | #endif |