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