Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -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 | * |
| 5 | * BSD license, See the LICENSE file for more information |
| 6 | * |
| 7 | * Author: Qiuhan Ding <qiuhanding@cs.ucla.edu> |
| 8 | * Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "discovery-panel.hpp" |
| 12 | #include "ui_discovery-panel.h" |
| 13 | |
| 14 | #include <QItemSelectionModel> |
| 15 | #include <QModelIndex> |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 16 | #include <QMessageBox> |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 17 | |
| 18 | #ifndef Q_MOC_RUN |
| 19 | #endif |
| 20 | |
| 21 | |
| 22 | namespace chronochat { |
| 23 | |
| 24 | static const time::seconds REFRESH_INTERVAL(60); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 25 | static const ndn::Name::Component ROUTING_HINT_SEPARATOR = |
| 26 | ndn::name::Component::fromEscapedString("%F0%2E"); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 27 | |
| 28 | DiscoveryPanel::DiscoveryPanel(QWidget *parent) |
| 29 | : QDialog(parent) |
| 30 | , ui(new Ui::DiscoveryPanel) |
| 31 | , m_chatroomListModel(new QStringListModel) |
| 32 | , m_rosterListModel(new QStringListModel) |
| 33 | { |
| 34 | ui->setupUi(this); |
| 35 | ui->ChatroomList->setModel(m_chatroomListModel); |
| 36 | ui->RosterList->setModel(m_rosterListModel); |
| 37 | |
| 38 | connect(ui->ChatroomList->selectionModel(), |
| 39 | SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 40 | this, |
| 41 | SLOT(onSelectedChatroomChanged(const QItemSelection &, const QItemSelection &))); |
| 42 | connect(ui->RosterList->selectionModel(), |
| 43 | SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 44 | this, |
| 45 | SLOT(onSelectedParticipantChanged(const QItemSelection &, const QItemSelection &))); |
| 46 | connect(ui->join, SIGNAL(clicked()), |
| 47 | this, SLOT(onJoinClicked())); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 48 | connect(ui->requestInvitation, SIGNAL(clicked()), |
| 49 | this, SLOT(onRequestInvitation())); |
| 50 | |
| 51 | ui->join->setEnabled(false); |
| 52 | ui->requestInvitation->setEnabled(false); |
| 53 | ui->InChatroomWarning->clear(); |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 54 | m_isParticipant = false; |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | DiscoveryPanel::~DiscoveryPanel() |
| 58 | { |
| 59 | if (m_chatroomListModel) |
| 60 | delete m_chatroomListModel; |
| 61 | if (m_rosterListModel) |
| 62 | delete m_rosterListModel; |
| 63 | delete ui; |
| 64 | } |
| 65 | |
| 66 | //private methods |
| 67 | void |
| 68 | DiscoveryPanel::resetPanel() |
| 69 | { |
| 70 | // Clean up General tag. |
| 71 | ui->NameData->clear(); |
| 72 | ui->NameSpaceData->clear(); |
| 73 | ui->TrustModelData->clear(); |
| 74 | |
| 75 | // Clean up Roster tag. |
| 76 | m_rosterList.clear(); |
| 77 | m_participant.clear(); |
| 78 | m_rosterListModel->setStringList(m_rosterList); |
| 79 | |
| 80 | // Clean up chatroom list. |
| 81 | m_chatroomList.clear(); |
| 82 | m_chatroom.clear(); |
| 83 | m_chatroomListModel->setStringList(m_chatroomList); |
| 84 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 85 | ui->join->setEnabled(false); |
| 86 | ui->requestInvitation->setEnabled(false); |
| 87 | ui->InChatroomWarning->clear(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // public slots |
| 91 | void |
| 92 | DiscoveryPanel::onIdentityUpdated(const QString& identity) |
| 93 | { |
| 94 | resetPanel(); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | DiscoveryPanel::onChatroomListReady(const QStringList& list) |
| 99 | { |
| 100 | m_chatroomList = list; |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 101 | resetPanel(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 102 | m_chatroomListModel->setStringList(m_chatroomList); |
| 103 | } |
| 104 | |
| 105 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 106 | DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 107 | { |
| 108 | ui->NameData->setText(QString::fromStdString(info.getName().toUri())); |
| 109 | ui->NameSpaceData->setText(QString::fromStdString(info.getSyncPrefix().toUri())); |
| 110 | |
| 111 | switch(info.getTrustModel()) { |
| 112 | case 2: |
| 113 | { |
| 114 | ui->TrustModelData->setText(QString("Hierarchical")); |
| 115 | ui->join->setEnabled(false); |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 116 | ui->requestInvitation->setEnabled(false); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | case 1: |
| 120 | { |
| 121 | ui->TrustModelData->setText(QString("Web Of Trust")); |
| 122 | ui->join->setEnabled(false); |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 123 | ui->requestInvitation->setEnabled(false); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | case 0: |
| 127 | { |
| 128 | ui->TrustModelData->setText(QString("None")); |
| 129 | ui->join->setEnabled(true); |
| 130 | ui->requestInvitation->setEnabled(false); |
| 131 | break; |
| 132 | } |
| 133 | default: |
| 134 | { |
| 135 | ui->TrustModelData->setText(QString("Unrecognized")); |
| 136 | ui->join->setEnabled(false); |
| 137 | ui->requestInvitation->setEnabled(false); |
| 138 | } |
| 139 | } |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 140 | ui->InChatroomWarning->clear(); |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 141 | m_isParticipant = isParticipant; |
| 142 | if (m_isParticipant) { |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 143 | ui->join->setEnabled(false); |
| 144 | ui->requestInvitation->setEnabled(false); |
| 145 | ui->InChatroomWarning->setText(QString("You are already in this chatroom")); |
| 146 | } |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 147 | |
| 148 | std::list<Name>roster = info.getParticipants(); |
| 149 | m_rosterList.clear(); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 150 | Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 151 | for (const auto& participant : roster) { |
| 152 | size_t i; |
| 153 | for (i = 0; i < participant.size(); ++i) { |
| 154 | if (routingHint == participant.at(i)) |
| 155 | break; |
| 156 | } |
| 157 | if (i == participant.size()) |
| 158 | m_rosterList << QString::fromStdString(participant.toUri()); |
| 159 | else |
| 160 | m_rosterList << QString::fromStdString(participant.getSubName(i + 1).toUri()); |
| 161 | } |
| 162 | m_rosterListModel->setStringList(m_rosterList); |
| 163 | ui->RosterList->setModel(m_rosterListModel); |
| 164 | } |
| 165 | |
| 166 | // private slots |
| 167 | void |
| 168 | DiscoveryPanel::onSelectedChatroomChanged(const QItemSelection &selected, |
| 169 | const QItemSelection &deselected) |
| 170 | { |
| 171 | QModelIndexList items = selected.indexes(); |
| 172 | QString chatroomName = m_chatroomListModel->data(items.first(), Qt::DisplayRole).toString(); |
| 173 | |
| 174 | bool chatroomFound = false; |
| 175 | for (int i = 0; i < m_chatroomList.size(); i++) { |
| 176 | if (chatroomName == m_chatroomList[i]) { |
| 177 | chatroomFound = true; |
| 178 | m_chatroom = m_chatroomList[i]; |
| 179 | m_participant.clear(); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (!chatroomFound) { |
| 185 | emit warning("This should not happen: DiscoveryPanel::onSelectedChatroomChanged"); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | emit waitForChatroomInfo(m_chatroom); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection &selected, |
| 194 | const QItemSelection &deselected) |
| 195 | { |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 196 | if (m_isParticipant) |
| 197 | return; |
| 198 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 199 | QModelIndexList items = selected.indexes(); |
| 200 | QString participant = m_rosterListModel->data(items.first(), Qt::DisplayRole).toString(); |
| 201 | |
| 202 | bool participantFound = false; |
| 203 | for (int i = 0; i < m_rosterList.size(); i++) { |
| 204 | if (participant == m_rosterList[i]) { |
| 205 | participantFound = true; |
| 206 | m_participant = m_rosterList[i]; |
| 207 | break; |
| 208 | } |
| 209 | } |
Qiuhan Ding | 0b21e29 | 2015-03-12 14:18:18 -0700 | [diff] [blame^] | 210 | ui->requestInvitation->setEnabled(true); |
| 211 | BOOST_ASSERT(participantFound); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
| 215 | DiscoveryPanel::onJoinClicked() |
| 216 | { |
| 217 | emit startChatroom(m_chatroom, false); |
| 218 | } |
| 219 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame] | 220 | void |
| 221 | DiscoveryPanel::onRequestInvitation() |
| 222 | { |
| 223 | emit sendInvitationRequest(m_chatroom, m_participant); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | DiscoveryPanel::onInvitationRequestResult(const std::string& message) |
| 228 | { |
| 229 | QMessageBox::information(this, tr("Chatroom Discovery"), |
| 230 | tr(message.c_str())); |
| 231 | } |
| 232 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 233 | } // namespace chronochat |
| 234 | |
| 235 | #if WAF |
| 236 | #include "discovery-panel.moc" |
| 237 | // #include "discovery-panel.cpp.moc" |
| 238 | #endif |