blob: b7c0ed628698650e1ddd34e03cf9c537453ddb8f [file] [log] [blame]
Qiuhan Ding43c8e162015-02-02 15:16:48 -08001/* -*- 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 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
24static const time::seconds REFRESH_INTERVAL(60);
Qiuhan Dingba3e57a2015-01-08 19:07:39 -080025static const ndn::Name::Component ROUTING_HINT_SEPARATOR =
26 ndn::name::Component::fromEscapedString("%F0%2E");
Qiuhan Ding43c8e162015-02-02 15:16:48 -080027
28DiscoveryPanel::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 Dingba3e57a2015-01-08 19:07:39 -080048 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 Ding0b21e292015-03-12 14:18:18 -070054 m_isParticipant = false;
Qiuhan Ding43c8e162015-02-02 15:16:48 -080055}
56
57DiscoveryPanel::~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
67void
68DiscoveryPanel::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 Dingba3e57a2015-01-08 19:07:39 -080085 ui->join->setEnabled(false);
86 ui->requestInvitation->setEnabled(false);
87 ui->InChatroomWarning->clear();
Qiuhan Ding43c8e162015-02-02 15:16:48 -080088}
89
90// public slots
91void
92DiscoveryPanel::onIdentityUpdated(const QString& identity)
93{
94 resetPanel();
95}
96
97void
98DiscoveryPanel::onChatroomListReady(const QStringList& list)
99{
100 m_chatroomList = list;
101 m_chatroomListModel->setStringList(m_chatroomList);
102}
103
104void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800105DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800106{
107 ui->NameData->setText(QString::fromStdString(info.getName().toUri()));
108 ui->NameSpaceData->setText(QString::fromStdString(info.getSyncPrefix().toUri()));
109
110 switch(info.getTrustModel()) {
111 case 2:
112 {
113 ui->TrustModelData->setText(QString("Hierarchical"));
114 ui->join->setEnabled(false);
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700115 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800116 break;
117 }
118 case 1:
119 {
120 ui->TrustModelData->setText(QString("Web Of Trust"));
121 ui->join->setEnabled(false);
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700122 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800123 break;
124 }
125 case 0:
126 {
127 ui->TrustModelData->setText(QString("None"));
128 ui->join->setEnabled(true);
129 ui->requestInvitation->setEnabled(false);
130 break;
131 }
132 default:
133 {
134 ui->TrustModelData->setText(QString("Unrecognized"));
135 ui->join->setEnabled(false);
136 ui->requestInvitation->setEnabled(false);
137 }
138 }
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800139 ui->InChatroomWarning->clear();
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700140 m_isParticipant = isParticipant;
141 if (m_isParticipant) {
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800142 ui->join->setEnabled(false);
143 ui->requestInvitation->setEnabled(false);
144 ui->InChatroomWarning->setText(QString("You are already in this chatroom"));
145 }
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800146
147 std::list<Name>roster = info.getParticipants();
148 m_rosterList.clear();
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800149 Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800150 for (const auto& participant : roster) {
151 size_t i;
152 for (i = 0; i < participant.size(); ++i) {
153 if (routingHint == participant.at(i))
154 break;
155 }
156 if (i == participant.size())
157 m_rosterList << QString::fromStdString(participant.toUri());
158 else
159 m_rosterList << QString::fromStdString(participant.getSubName(i + 1).toUri());
160 }
161 m_rosterListModel->setStringList(m_rosterList);
162 ui->RosterList->setModel(m_rosterListModel);
163}
164
165// private slots
166void
167DiscoveryPanel::onSelectedChatroomChanged(const QItemSelection &selected,
168 const QItemSelection &deselected)
169{
170 QModelIndexList items = selected.indexes();
171 QString chatroomName = m_chatroomListModel->data(items.first(), Qt::DisplayRole).toString();
172
173 bool chatroomFound = false;
174 for (int i = 0; i < m_chatroomList.size(); i++) {
175 if (chatroomName == m_chatroomList[i]) {
176 chatroomFound = true;
177 m_chatroom = m_chatroomList[i];
178 m_participant.clear();
179 break;
180 }
181 }
182
183 if (!chatroomFound) {
184 emit warning("This should not happen: DiscoveryPanel::onSelectedChatroomChanged");
185 return;
186 }
187
188 emit waitForChatroomInfo(m_chatroom);
189}
190
191void
192DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection &selected,
193 const QItemSelection &deselected)
194{
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700195 if (m_isParticipant)
196 return;
197
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800198 QModelIndexList items = selected.indexes();
199 QString participant = m_rosterListModel->data(items.first(), Qt::DisplayRole).toString();
200
201 bool participantFound = false;
202 for (int i = 0; i < m_rosterList.size(); i++) {
203 if (participant == m_rosterList[i]) {
204 participantFound = true;
205 m_participant = m_rosterList[i];
206 break;
207 }
208 }
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700209 ui->requestInvitation->setEnabled(true);
210 BOOST_ASSERT(participantFound);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800211}
212
213void
214DiscoveryPanel::onJoinClicked()
215{
216 emit startChatroom(m_chatroom, false);
217}
218
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800219void
220DiscoveryPanel::onRequestInvitation()
221{
222 emit sendInvitationRequest(m_chatroom, m_participant);
223}
224
225void
226DiscoveryPanel::onInvitationRequestResult(const std::string& message)
227{
228 QMessageBox::information(this, tr("Chatroom Discovery"),
229 tr(message.c_str()));
230}
231
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800232} // namespace chronochat
233
234#if WAF
235#include "discovery-panel.moc"
236// #include "discovery-panel.cpp.moc"
237#endif