blob: 4c5dcf7c0ad3df3ba21aee6b8ecf172117857538 [file] [log] [blame]
Qiuhan Ding43c8e162015-02-02 15:16:48 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (c) 2020, Regents of the University of California
Qiuhan Ding43c8e162015-02-02 15:16:48 -08004 *
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 Dingba3e57a2015-01-08 19:07:39 -080016#include <QMessageBox>
Qiuhan Ding43c8e162015-02-02 15:16:48 -080017
18#ifndef Q_MOC_RUN
19#endif
20
21
22namespace chronochat {
23
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080024static const ndn::Name::Component ROUTING_HINT_SEPARATOR =
25 ndn::name::Component::fromEscapedString("%F0%2E");
Qiuhan Ding43c8e162015-02-02 15:16:48 -080026
27DiscoveryPanel::DiscoveryPanel(QWidget *parent)
28 : QDialog(parent)
29 , ui(new Ui::DiscoveryPanel)
30 , m_chatroomListModel(new QStringListModel)
31 , m_rosterListModel(new QStringListModel)
32{
33 ui->setupUi(this);
34 ui->ChatroomList->setModel(m_chatroomListModel);
35 ui->RosterList->setModel(m_rosterListModel);
36
37 connect(ui->ChatroomList->selectionModel(),
38 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
39 this,
40 SLOT(onSelectedChatroomChanged(const QItemSelection &, const QItemSelection &)));
41 connect(ui->RosterList->selectionModel(),
42 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
43 this,
44 SLOT(onSelectedParticipantChanged(const QItemSelection &, const QItemSelection &)));
45 connect(ui->join, SIGNAL(clicked()),
46 this, SLOT(onJoinClicked()));
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080047 connect(ui->requestInvitation, SIGNAL(clicked()),
48 this, SLOT(onRequestInvitation()));
49
50 ui->join->setEnabled(false);
51 ui->requestInvitation->setEnabled(false);
52 ui->InChatroomWarning->clear();
Qiuhan Ding0b21e292015-03-12 14:18:18 -070053 m_isParticipant = false;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080054}
55
56DiscoveryPanel::~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
66void
67DiscoveryPanel::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 Dingba3e57a2015-01-08 19:07:39 -080084 ui->join->setEnabled(false);
85 ui->requestInvitation->setEnabled(false);
86 ui->InChatroomWarning->clear();
Qiuhan Ding43c8e162015-02-02 15:16:48 -080087}
88
89// public slots
90void
91DiscoveryPanel::onIdentityUpdated(const QString& identity)
92{
Qiuhan Ding43c8e162015-02-02 15:16:48 -080093}
94
95void
96DiscoveryPanel::onChatroomListReady(const QStringList& list)
97{
98 m_chatroomList = list;
99 m_chatroomListModel->setStringList(m_chatroomList);
100}
101
102void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800103DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800104{
105 ui->NameData->setText(QString::fromStdString(info.getName().toUri()));
106 ui->NameSpaceData->setText(QString::fromStdString(info.getSyncPrefix().toUri()));
107
Varun Patila24bd3e2020-11-24 10:08:33 +0530108 switch (info.getTrustModel()) {
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800109 case 2:
110 {
111 ui->TrustModelData->setText(QString("Hierarchical"));
112 ui->join->setEnabled(false);
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700113 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800114 break;
115 }
116 case 1:
117 {
118 ui->TrustModelData->setText(QString("Web Of Trust"));
119 ui->join->setEnabled(false);
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700120 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800121 break;
122 }
123 case 0:
124 {
125 ui->TrustModelData->setText(QString("None"));
126 ui->join->setEnabled(true);
127 ui->requestInvitation->setEnabled(false);
128 break;
129 }
130 default:
131 {
132 ui->TrustModelData->setText(QString("Unrecognized"));
133 ui->join->setEnabled(false);
134 ui->requestInvitation->setEnabled(false);
135 }
136 }
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800137 ui->InChatroomWarning->clear();
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700138 m_isParticipant = isParticipant;
139 if (m_isParticipant) {
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800140 ui->join->setEnabled(false);
141 ui->requestInvitation->setEnabled(false);
142 ui->InChatroomWarning->setText(QString("You are already in this chatroom"));
143 }
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800144
145 std::list<Name>roster = info.getParticipants();
146 m_rosterList.clear();
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800147 Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800148 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
164void
Varun Patila24bd3e2020-11-24 10:08:33 +0530165DiscoveryPanel::onSelectedChatroomChanged(const QItemSelection& selected,
166 const QItemSelection& deselected)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800167{
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
189void
Varun Patila24bd3e2020-11-24 10:08:33 +0530190DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection& selected,
191 const QItemSelection& deselected)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800192{
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700193 if (m_isParticipant)
194 return;
195
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800196 QModelIndexList items = selected.indexes();
197 QString participant = m_rosterListModel->data(items.first(), Qt::DisplayRole).toString();
198
199 bool participantFound = false;
200 for (int i = 0; i < m_rosterList.size(); i++) {
201 if (participant == m_rosterList[i]) {
202 participantFound = true;
203 m_participant = m_rosterList[i];
204 break;
205 }
206 }
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700207 ui->requestInvitation->setEnabled(true);
208 BOOST_ASSERT(participantFound);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800209}
210
211void
212DiscoveryPanel::onJoinClicked()
213{
214 emit startChatroom(m_chatroom, false);
215}
216
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800217void
218DiscoveryPanel::onRequestInvitation()
219{
220 emit sendInvitationRequest(m_chatroom, m_participant);
221}
222
223void
224DiscoveryPanel::onInvitationRequestResult(const std::string& message)
225{
226 QMessageBox::information(this, tr("Chatroom Discovery"),
227 tr(message.c_str()));
228}
229
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800230} // namespace chronochat
231
232#if WAF
233#include "discovery-panel.moc"
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800234#endif