blob: aa91028952893a61ba07555c86be365f4dbb4773 [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;
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700101 resetPanel();
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800102 m_chatroomListModel->setStringList(m_chatroomList);
103}
104
105void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800106DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800107{
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 Ding0b21e292015-03-12 14:18:18 -0700116 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800117 break;
118 }
119 case 1:
120 {
121 ui->TrustModelData->setText(QString("Web Of Trust"));
122 ui->join->setEnabled(false);
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700123 ui->requestInvitation->setEnabled(false);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800124 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 Dingba3e57a2015-01-08 19:07:39 -0800140 ui->InChatroomWarning->clear();
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700141 m_isParticipant = isParticipant;
142 if (m_isParticipant) {
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800143 ui->join->setEnabled(false);
144 ui->requestInvitation->setEnabled(false);
145 ui->InChatroomWarning->setText(QString("You are already in this chatroom"));
146 }
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800147
148 std::list<Name>roster = info.getParticipants();
149 m_rosterList.clear();
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800150 Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800151 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
167void
168DiscoveryPanel::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
192void
193DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection &selected,
194 const QItemSelection &deselected)
195{
Qiuhan Ding0b21e292015-03-12 14:18:18 -0700196 if (m_isParticipant)
197 return;
198
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800199 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 Ding0b21e292015-03-12 14:18:18 -0700210 ui->requestInvitation->setEnabled(true);
211 BOOST_ASSERT(participantFound);
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800212}
213
214void
215DiscoveryPanel::onJoinClicked()
216{
217 emit startChatroom(m_chatroom, false);
218}
219
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800220void
221DiscoveryPanel::onRequestInvitation()
222{
223 emit sendInvitationRequest(m_chatroom, m_participant);
224}
225
226void
227DiscoveryPanel::onInvitationRequestResult(const std::string& message)
228{
229 QMessageBox::information(this, tr("Chatroom Discovery"),
230 tr(message.c_str()));
231}
232
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800233} // namespace chronochat
234
235#if WAF
236#include "discovery-panel.moc"
237// #include "discovery-panel.cpp.moc"
238#endif