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