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 | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | DiscoveryPanel::~DiscoveryPanel() |
| 57 | { |
| 58 | if (m_chatroomListModel) |
| 59 | delete m_chatroomListModel; |
| 60 | if (m_rosterListModel) |
| 61 | delete m_rosterListModel; |
| 62 | delete ui; |
| 63 | } |
| 64 | |
| 65 | //private methods |
| 66 | void |
| 67 | DiscoveryPanel::resetPanel() |
| 68 | { |
| 69 | // Clean up General tag. |
| 70 | ui->NameData->clear(); |
| 71 | ui->NameSpaceData->clear(); |
| 72 | ui->TrustModelData->clear(); |
| 73 | |
| 74 | // Clean up Roster tag. |
| 75 | m_rosterList.clear(); |
| 76 | m_participant.clear(); |
| 77 | m_rosterListModel->setStringList(m_rosterList); |
| 78 | |
| 79 | // Clean up chatroom list. |
| 80 | m_chatroomList.clear(); |
| 81 | m_chatroom.clear(); |
| 82 | m_chatroomListModel->setStringList(m_chatroomList); |
| 83 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame^] | 84 | ui->join->setEnabled(false); |
| 85 | ui->requestInvitation->setEnabled(false); |
| 86 | ui->InChatroomWarning->clear(); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // public slots |
| 90 | void |
| 91 | DiscoveryPanel::onIdentityUpdated(const QString& identity) |
| 92 | { |
| 93 | resetPanel(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | DiscoveryPanel::onChatroomListReady(const QStringList& list) |
| 98 | { |
| 99 | m_chatroomList = list; |
| 100 | m_chatroomListModel->setStringList(m_chatroomList); |
| 101 | } |
| 102 | |
| 103 | void |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame^] | 104 | DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant) |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 105 | { |
| 106 | ui->NameData->setText(QString::fromStdString(info.getName().toUri())); |
| 107 | ui->NameSpaceData->setText(QString::fromStdString(info.getSyncPrefix().toUri())); |
| 108 | |
| 109 | switch(info.getTrustModel()) { |
| 110 | case 2: |
| 111 | { |
| 112 | ui->TrustModelData->setText(QString("Hierarchical")); |
| 113 | ui->join->setEnabled(false); |
| 114 | ui->requestInvitation->setEnabled(true); |
| 115 | break; |
| 116 | } |
| 117 | case 1: |
| 118 | { |
| 119 | ui->TrustModelData->setText(QString("Web Of Trust")); |
| 120 | ui->join->setEnabled(false); |
| 121 | ui->requestInvitation->setEnabled(true); |
| 122 | break; |
| 123 | } |
| 124 | case 0: |
| 125 | { |
| 126 | ui->TrustModelData->setText(QString("None")); |
| 127 | ui->join->setEnabled(true); |
| 128 | ui->requestInvitation->setEnabled(false); |
| 129 | break; |
| 130 | } |
| 131 | default: |
| 132 | { |
| 133 | ui->TrustModelData->setText(QString("Unrecognized")); |
| 134 | ui->join->setEnabled(false); |
| 135 | ui->requestInvitation->setEnabled(false); |
| 136 | } |
| 137 | } |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame^] | 138 | ui->InChatroomWarning->clear(); |
| 139 | if (isParticipant) { |
| 140 | ui->join->setEnabled(false); |
| 141 | ui->requestInvitation->setEnabled(false); |
| 142 | ui->InChatroomWarning->setText(QString("You are already in this chatroom")); |
| 143 | } |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 144 | |
| 145 | std::list<Name>roster = info.getParticipants(); |
| 146 | m_rosterList.clear(); |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame^] | 147 | Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR); |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 148 | for (const auto& participant : roster) { |
| 149 | size_t i; |
| 150 | for (i = 0; i < participant.size(); ++i) { |
| 151 | if (routingHint == participant.at(i)) |
| 152 | break; |
| 153 | } |
| 154 | if (i == participant.size()) |
| 155 | m_rosterList << QString::fromStdString(participant.toUri()); |
| 156 | else |
| 157 | m_rosterList << QString::fromStdString(participant.getSubName(i + 1).toUri()); |
| 158 | } |
| 159 | m_rosterListModel->setStringList(m_rosterList); |
| 160 | ui->RosterList->setModel(m_rosterListModel); |
| 161 | } |
| 162 | |
| 163 | // private slots |
| 164 | void |
| 165 | DiscoveryPanel::onSelectedChatroomChanged(const QItemSelection &selected, |
| 166 | const QItemSelection &deselected) |
| 167 | { |
| 168 | QModelIndexList items = selected.indexes(); |
| 169 | QString chatroomName = m_chatroomListModel->data(items.first(), Qt::DisplayRole).toString(); |
| 170 | |
| 171 | bool chatroomFound = false; |
| 172 | for (int i = 0; i < m_chatroomList.size(); i++) { |
| 173 | if (chatroomName == m_chatroomList[i]) { |
| 174 | chatroomFound = true; |
| 175 | m_chatroom = m_chatroomList[i]; |
| 176 | m_participant.clear(); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!chatroomFound) { |
| 182 | emit warning("This should not happen: DiscoveryPanel::onSelectedChatroomChanged"); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | emit waitForChatroomInfo(m_chatroom); |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection &selected, |
| 191 | const QItemSelection &deselected) |
| 192 | { |
| 193 | QModelIndexList items = selected.indexes(); |
| 194 | QString participant = m_rosterListModel->data(items.first(), Qt::DisplayRole).toString(); |
| 195 | |
| 196 | bool participantFound = false; |
| 197 | for (int i = 0; i < m_rosterList.size(); i++) { |
| 198 | if (participant == m_rosterList[i]) { |
| 199 | participantFound = true; |
| 200 | m_participant = m_rosterList[i]; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | if (!participantFound) { |
| 205 | emit warning("This should not happen: DiscoveryPanel::onSelectedParticipantChangeds #1"); |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | DiscoveryPanel::onJoinClicked() |
| 212 | { |
| 213 | emit startChatroom(m_chatroom, false); |
| 214 | } |
| 215 | |
Qiuhan Ding | ba3e57a | 2015-01-08 19:07:39 -0800 | [diff] [blame^] | 216 | void |
| 217 | DiscoveryPanel::onRequestInvitation() |
| 218 | { |
| 219 | emit sendInvitationRequest(m_chatroom, m_participant); |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | DiscoveryPanel::onInvitationRequestResult(const std::string& message) |
| 224 | { |
| 225 | QMessageBox::information(this, tr("Chatroom Discovery"), |
| 226 | tr(message.c_str())); |
| 227 | } |
| 228 | |
Qiuhan Ding | 43c8e16 | 2015-02-02 15:16:48 -0800 | [diff] [blame] | 229 | } // namespace chronochat |
| 230 | |
| 231 | #if WAF |
| 232 | #include "discovery-panel.moc" |
| 233 | // #include "discovery-panel.cpp.moc" |
| 234 | #endif |