Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 1 | #include <QtGui> |
| 2 | #include "chatdialog.h" |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 3 | #include "settingdialog.h" |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 4 | #include <ctime> |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 5 | #include <iostream> |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 6 | #include <QTimer> |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 7 | #include <QMetaType> |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 8 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 9 | #define BROADCAST_PREFIX_FOR_SYNC_DEMO "/ndn/broadcast/sync-demo" |
| 10 | |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 11 | void |
| 12 | ChatDialog::testDraw() |
| 13 | { |
| 14 | std::string prefix[5] = {"/ndn/1", "/ndn/2", "/ndn/3", "/ndn/4", "/ndn/5"}; |
| 15 | std::string nick[5] = {"tom", "jerry", "jason", "michael", "hurry"}; |
| 16 | std::vector<Sync::MissingDataInfo> v; |
| 17 | for (int i = 0; i < 5; i++) |
| 18 | { |
| 19 | Sync::MissingDataInfo mdi = {prefix[i], Sync::SeqNo(0), Sync::SeqNo(i * (2 << i) )}; |
| 20 | v.push_back(mdi); |
| 21 | } |
| 22 | |
| 23 | m_scene->processUpdate(v, "12341234@!#%!@"); |
| 24 | |
| 25 | for (int i = 0; i < 5; i++) |
| 26 | { |
| 27 | m_scene-> msgReceived(prefix[i].c_str(), nick[i].c_str()); |
| 28 | } |
| 29 | |
| 30 | fitView(); |
| 31 | } |
| 32 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 33 | ChatDialog::ChatDialog(QWidget *parent) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 34 | : QDialog(parent), m_sock(NULL) |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 35 | { |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 36 | // have to register this, otherwise |
| 37 | // the signal-slot system won't recognize this type |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 38 | qRegisterMetaType<std::vector<Sync::MissingDataInfo> >("std::vector<Sync::MissingDataInfo>"); |
| 39 | qRegisterMetaType<size_t>("size_t"); |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 40 | setupUi(this); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 41 | m_session = time(NULL); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 42 | |
| 43 | readSettings(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 44 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 45 | updateLabels(); |
| 46 | |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 47 | lineEdit->setFocusPolicy(Qt::StrongFocus); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 48 | m_scene = new DigestTreeScene(this); |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 49 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 50 | treeViewer->setScene(m_scene); |
| 51 | m_scene->plot("Empty"); |
| 52 | QRectF rect = m_scene->itemsBoundingRect(); |
| 53 | m_scene->setSceneRect(rect); |
| 54 | |
| 55 | // create sync socket |
| 56 | if(!m_user.getChatroom().isEmpty()) { |
| 57 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 58 | syncPrefix += "/"; |
| 59 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 60 | try |
| 61 | { |
| 62 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 63 | } |
| 64 | catch (Sync::CcnxOperationException ex) |
| 65 | { |
| 66 | QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok); |
| 67 | std::exit(1); |
| 68 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 69 | } |
Zhenkai Zhu | b45e38a | 2012-06-01 15:44:36 -0700 | [diff] [blame] | 70 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 71 | connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 72 | connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed())); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 73 | connect(this, SIGNAL(dataReceived(QString, const char *, size_t)), this, SLOT(processData(QString, const char *, size_t))); |
| 74 | connect(this, SIGNAL(treeUpdated(const std::vector<Sync::MissingDataInfo>)), this, SLOT(processTreeUpdate(const std::vector<Sync::MissingDataInfo>))); |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 75 | //testDraw(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 78 | ChatDialog::~ChatDialog() |
| 79 | { |
| 80 | if (m_sock != NULL) |
| 81 | { |
| 82 | delete m_sock; |
| 83 | m_sock = NULL; |
| 84 | } |
| 85 | } |
| 86 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 87 | void |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 88 | ChatDialog::appendMessage(const SyncDemo::ChatMessage msg) |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 89 | { |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 90 | |
| 91 | if (msg.type() != SyncDemo::ChatMessage::CHAT) { |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 92 | return; |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | if (!msg.has_data()) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (msg.from().empty() || msg.data().empty()) { |
| 100 | return; |
| 101 | } |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 102 | |
| 103 | QTextCursor cursor(textEdit->textCursor()); |
| 104 | cursor.movePosition(QTextCursor::End); |
| 105 | QTextTableFormat tableFormat; |
| 106 | tableFormat.setBorder(0); |
| 107 | QTextTable *table = cursor.insertTable(1, 2, tableFormat); |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 108 | QString from = QString("<%1>: ").arg(msg.from().c_str()); |
| 109 | table->cellAt(0, 0).firstCursorPosition().insertText(from); |
| 110 | table->cellAt(0, 1).firstCursorPosition().insertText(msg.data().c_str()); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 111 | QScrollBar *bar = textEdit->verticalScrollBar(); |
| 112 | bar->setValue(bar->maximum()); |
| 113 | } |
| 114 | |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 115 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 116 | ChatDialog::processTreeUpdateWrapper(const std::vector<Sync::MissingDataInfo> v, Sync::SyncAppSocket *sock) |
| 117 | { |
| 118 | emit treeUpdated(v); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 119 | #ifdef __DEBUG |
| 120 | std::cout << "<<< Tree update signal emitted" << std::endl; |
| 121 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void |
| 125 | ChatDialog::processTreeUpdate(const std::vector<Sync::MissingDataInfo> v) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 126 | { |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 127 | #ifdef __DEBUG |
| 128 | std::cout << "<<< processing Tree Update" << std::endl; |
| 129 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 130 | if (v.empty()) |
| 131 | { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // reflect the changes on digest tree |
| 136 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 137 | |
| 138 | int n = v.size(); |
| 139 | int totalMissingPackets = 0; |
| 140 | for (int i = 0; i < n; i++) |
| 141 | { |
| 142 | totalMissingPackets += v[i].high.getSeq() - v[i].low.getSeq() + 1; |
| 143 | } |
| 144 | |
| 145 | if (totalMissingPackets < 10) { |
| 146 | for (int i = 0; i < n; i++) |
| 147 | { |
| 148 | for (Sync::SeqNo seq = v[i].low; seq <= v[i].high; ++seq) |
| 149 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 150 | m_sock->fetchRaw(v[i].prefix, seq, bind(&ChatDialog::processDataWrapper, this, _1, _2, _3), 2); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 151 | #ifdef __DEBUG |
| 152 | std::cout << "<<< Fetching " << v[i].prefix << "/" <<seq.getSession() <<"/" << seq.getSeq() << std::endl; |
| 153 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | // too bad; too many missing packets |
| 160 | // we may just join a new chatroom |
| 161 | // or some network patition just healed |
| 162 | // we don't try to fetch any data in this case (for now) |
| 163 | } |
| 164 | |
| 165 | // adjust the view |
| 166 | fitView(); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | void |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 171 | ChatDialog::processDataWrapper(std::string name, const char *buf, size_t len) |
| 172 | { |
| 173 | emit dataReceived(name.c_str(), buf, len); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 174 | #ifdef __DEBUG |
| 175 | std::cout <<"<<< " << name << " fetched" << std::endl; |
| 176 | #endif |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void |
| 180 | ChatDialog::processData(QString name, const char *buf, size_t len) |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 181 | { |
| 182 | SyncDemo::ChatMessage msg; |
| 183 | if (!msg.ParseFromArray(buf, len)) |
| 184 | { |
| 185 | std::cerr << "Errrrr.. Can not parse msg at "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 186 | abort(); |
| 187 | } |
| 188 | |
Zhenkai Zhu | 10ccb5a | 2012-06-04 21:55:14 -0700 | [diff] [blame] | 189 | // display msg received from network |
| 190 | // we have to do so; this function is called by ccnd thread |
| 191 | // so if we call appendMsg directly |
| 192 | // Qt crash as "QObject: Cannot create children for a parent that is in a different thread" |
| 193 | // the "cannonical" way to is use signal-slot |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 194 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 195 | |
| 196 | // update the tree view |
Zhenkai Zhu | 097bfe7 | 2012-06-05 14:30:17 -0700 | [diff] [blame] | 197 | std::string stdStrName = name.toStdString(); |
| 198 | std::string stdStrNameWithoutSeq = stdStrName.substr(0, stdStrName.find_last_of('/')); |
| 199 | std::string prefix = stdStrNameWithoutSeq.substr(0, stdStrNameWithoutSeq.find_last_of('/')); |
| 200 | #ifdef __DEBUG |
| 201 | std::cout <<"<<< updating scene for" << prefix << ": " << msg.from() << std::endl; |
| 202 | #endif |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 203 | m_scene->msgReceived(prefix.c_str(), msg.from().c_str()); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 204 | fitView(); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void |
| 208 | ChatDialog::processRemove(std::string prefix) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 213 | ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 214 | msg.set_from(m_user.getNick().toStdString()); |
| 215 | msg.set_to(m_user.getChatroom().toStdString()); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 216 | msg.set_data(text.toStdString()); |
| 217 | time_t seconds = time(NULL); |
| 218 | msg.set_timestamp(seconds); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 219 | msg.set_type(SyncDemo::ChatMessage::CHAT); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 222 | bool |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 223 | ChatDialog::readSettings() |
| 224 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 225 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 226 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 227 | QString nick = s.value("nick", "").toString(); |
| 228 | QString chatroom = s.value("chatroom", "").toString(); |
| 229 | QString prefix = s.value("prefix", "").toString(); |
| 230 | if (nick == "" || chatroom == "" || prefix == "") { |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 231 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 232 | return false; |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 233 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 234 | else { |
| 235 | m_user.setNick(nick); |
| 236 | m_user.setChatroom(chatroom); |
| 237 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 238 | return true; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 239 | } |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 240 | #else |
| 241 | QTimer::singleShot(500, this, SLOT(buttonPressed())); |
| 242 | return false; |
| 243 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
| 247 | ChatDialog::writeSettings() |
| 248 | { |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 249 | #ifndef __DEBUG |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 250 | QSettings s(ORGANIZATION, APPLICATION); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 251 | s.setValue("nick", m_user.getNick()); |
| 252 | s.setValue("chatroom", m_user.getChatroom()); |
| 253 | s.setValue("prefix", m_user.getPrefix()); |
Zhenkai Zhu | 64f9ede | 2012-06-05 11:32:00 -0700 | [diff] [blame] | 254 | #endif |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void |
| 258 | ChatDialog::updateLabels() |
| 259 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 260 | QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 261 | infoLabel->setText(settingDisp); |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 262 | QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 263 | prefixLabel->setText(prefixDisp); |
| 264 | } |
| 265 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 266 | void |
| 267 | ChatDialog::returnPressed() |
| 268 | { |
| 269 | QString text = lineEdit->text(); |
| 270 | if (text.isEmpty()) |
| 271 | return; |
| 272 | |
Zhenkai Zhu | b633882 | 2012-05-31 13:27:24 -0700 | [diff] [blame] | 273 | lineEdit->clear(); |
| 274 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 275 | SyncDemo::ChatMessage msg; |
| 276 | formChatMessage(text, msg); |
| 277 | |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 278 | appendMessage(msg); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 279 | |
| 280 | // send msg |
| 281 | size_t size = msg.ByteSize(); |
| 282 | char *buf = new char[size]; |
| 283 | msg.SerializeToArray(buf, size); |
| 284 | if (!msg.IsInitialized()) |
| 285 | { |
| 286 | std::cerr << "Errrrr.. msg was not probally initialized "<<__FILE__ <<":"<<__LINE__<<". what is happening?" << std::endl; |
| 287 | abort(); |
| 288 | } |
| 289 | m_sock->publishRaw(m_user.getPrefix().toStdString(), m_session, buf, size, 60); |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 290 | |
| 291 | int nextSequence = m_sock->getNextSeq(m_user.getPrefix().toStdString(), m_session); |
Zhenkai Zhu | d1c5a97 | 2012-06-05 14:07:41 -0700 | [diff] [blame] | 292 | Sync::MissingDataInfo mdi = {m_user.getPrefix().toStdString(), Sync::SeqNo(0), Sync::SeqNo(nextSequence - 1)}; |
Zhenkai Zhu | c547061 | 2012-06-05 12:28:59 -0700 | [diff] [blame] | 293 | std::vector<Sync::MissingDataInfo> v; |
| 294 | v.push_back(mdi); |
| 295 | m_scene->processUpdate(v, m_sock->getRootDigest().c_str()); |
| 296 | m_scene->msgReceived(m_user.getPrefix(), m_user.getNick()); |
| 297 | fitView(); |
Zhenkai Zhu | 5a8d5aa | 2012-05-30 21:25:23 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 301 | ChatDialog::buttonPressed() |
| 302 | { |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 303 | SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix()); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 304 | connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString))); |
Zhenkai Zhu | 85845d2 | 2012-06-01 23:10:43 -0700 | [diff] [blame] | 305 | dialog.exec(); |
Zhenkai Zhu | e837f79 | 2012-06-05 20:47:54 -0700 | [diff] [blame] | 306 | QTimer::singleShot(100, this, SLOT(checkSetting())); |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | ChatDialog::checkSetting() |
| 311 | { |
| 312 | if (m_user.getPrefix().isEmpty() || m_user.getNick().isEmpty() || m_user.getChatroom().isEmpty()) |
| 313 | { |
| 314 | buttonPressed(); |
| 315 | } |
Zhenkai Zhu | 6d589aa | 2012-05-29 17:34:35 -0700 | [diff] [blame] | 316 | } |
Zhenkai Zhu | e08afe0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 317 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 318 | void |
| 319 | ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix) |
| 320 | { |
| 321 | bool needWrite = false; |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 322 | if (!nick.isEmpty() && nick != m_user.getNick()) { |
| 323 | m_user.setNick(nick); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 324 | needWrite = true; |
| 325 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 326 | if (!prefix.isEmpty() && prefix != m_user.getPrefix()) { |
| 327 | m_user.setPrefix(prefix); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 328 | needWrite = true; |
| 329 | // TODO: set the previous prefix as left? |
| 330 | } |
Zhenkai Zhu | 71b42cb | 2012-06-04 09:42:53 -0700 | [diff] [blame] | 331 | if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) { |
| 332 | m_user.setChatroom(chatroom); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 333 | needWrite = true; |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 334 | |
| 335 | m_scene->clearAll(); |
| 336 | m_scene->plot("Empty"); |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 337 | // TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 338 | if (m_sock != NULL) |
| 339 | { |
| 340 | delete m_sock; |
| 341 | m_sock = NULL; |
| 342 | } |
| 343 | std::string syncPrefix = BROADCAST_PREFIX_FOR_SYNC_DEMO; |
| 344 | syncPrefix += "/"; |
| 345 | syncPrefix += m_user.getChatroom().toStdString(); |
Zhenkai Zhu | a4fb124 | 2012-06-05 20:26:05 -0700 | [diff] [blame] | 346 | try |
| 347 | { |
| 348 | m_sock = new Sync::SyncAppSocket(syncPrefix, bind(&ChatDialog::processTreeUpdateWrapper, this, _1, _2), bind(&ChatDialog::processRemove, this, _1)); |
| 349 | } |
| 350 | catch (Sync::CcnxOperationException ex) |
| 351 | { |
| 352 | QMessageBox::critical(this, tr("Sync-Demo"), tr("Canno connect to ccnd.\n Have you started your ccnd?"), QMessageBox::Ok); |
| 353 | std::exit(1); |
| 354 | } |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 355 | |
| 356 | fitView(); |
| 357 | |
Zhenkai Zhu | 7e9b06d | 2012-06-02 00:44:42 -0700 | [diff] [blame] | 358 | } |
| 359 | if (needWrite) { |
| 360 | writeSettings(); |
| 361 | updateLabels(); |
| 362 | } |
| 363 | } |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 364 | |
| 365 | void |
| 366 | ChatDialog::resizeEvent(QResizeEvent *e) |
| 367 | { |
| 368 | fitView(); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | ChatDialog::showEvent(QShowEvent *e) |
| 373 | { |
| 374 | fitView(); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | ChatDialog::fitView() |
| 379 | { |
Zhenkai Zhu | 21d75f9 | 2012-06-04 21:23:34 -0700 | [diff] [blame] | 380 | QRectF rect = m_scene->itemsBoundingRect(); |
| 381 | m_scene->setSceneRect(rect); |
Zhenkai Zhu | 82a6275 | 2012-06-04 17:11:04 -0700 | [diff] [blame] | 382 | treeViewer->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio); |
Zhenkai Zhu | d13acd0 | 2012-06-04 15:25:20 -0700 | [diff] [blame] | 383 | } |