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