blob: 20e36ab5a88986edf004e6327aeac8c02568f55e [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 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{
93 resetPanel();
94}
95
96void
97DiscoveryPanel::onChatroomListReady(const QStringList& list)
98{
99 m_chatroomList = list;
100 m_chatroomListModel->setStringList(m_chatroomList);
101}
102
103void
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800104DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info, bool isParticipant)
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800105{
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 Dingba3e57a2015-01-08 19:07:39 -0800138 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 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
165DiscoveryPanel::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
189void
190DiscoveryPanel::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
210void
211DiscoveryPanel::onJoinClicked()
212{
213 emit startChatroom(m_chatroom, false);
214}
215
Qiuhan Dingba3e57a2015-01-08 19:07:39 -0800216void
217DiscoveryPanel::onRequestInvitation()
218{
219 emit sendInvitationRequest(m_chatroom, m_participant);
220}
221
222void
223DiscoveryPanel::onInvitationRequestResult(const std::string& message)
224{
225 QMessageBox::information(this, tr("Chatroom Discovery"),
226 tr(message.c_str()));
227}
228
Qiuhan Ding43c8e162015-02-02 15:16:48 -0800229} // namespace chronochat
230
231#if WAF
232#include "discovery-panel.moc"
233// #include "discovery-panel.cpp.moc"
234#endif