blob: cbc98e070f15a96e8970f0ea74974a01d4f46545 [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>
16
17#ifndef Q_MOC_RUN
18#endif
19
20
21namespace chronochat {
22
23static const time::seconds REFRESH_INTERVAL(60);
24static const uint8_t ROUTING_HINT_SEPARATOR[2] = {0xF0, 0x2E};
25
26DiscoveryPanel::DiscoveryPanel(QWidget *parent)
27 : QDialog(parent)
28 , ui(new Ui::DiscoveryPanel)
29 , m_chatroomListModel(new QStringListModel)
30 , m_rosterListModel(new QStringListModel)
31{
32 ui->setupUi(this);
33 ui->ChatroomList->setModel(m_chatroomListModel);
34 ui->RosterList->setModel(m_rosterListModel);
35
36 connect(ui->ChatroomList->selectionModel(),
37 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
38 this,
39 SLOT(onSelectedChatroomChanged(const QItemSelection &, const QItemSelection &)));
40 connect(ui->RosterList->selectionModel(),
41 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
42 this,
43 SLOT(onSelectedParticipantChanged(const QItemSelection &, const QItemSelection &)));
44 connect(ui->join, SIGNAL(clicked()),
45 this, SLOT(onJoinClicked()));
46}
47
48DiscoveryPanel::~DiscoveryPanel()
49{
50 if (m_chatroomListModel)
51 delete m_chatroomListModel;
52 if (m_rosterListModel)
53 delete m_rosterListModel;
54 delete ui;
55}
56
57//private methods
58void
59DiscoveryPanel::resetPanel()
60{
61 // Clean up General tag.
62 ui->NameData->clear();
63 ui->NameSpaceData->clear();
64 ui->TrustModelData->clear();
65
66 // Clean up Roster tag.
67 m_rosterList.clear();
68 m_participant.clear();
69 m_rosterListModel->setStringList(m_rosterList);
70
71 // Clean up chatroom list.
72 m_chatroomList.clear();
73 m_chatroom.clear();
74 m_chatroomListModel->setStringList(m_chatroomList);
75
76}
77
78// public slots
79void
80DiscoveryPanel::onIdentityUpdated(const QString& identity)
81{
82 resetPanel();
83}
84
85void
86DiscoveryPanel::onChatroomListReady(const QStringList& list)
87{
88 m_chatroomList = list;
89 m_chatroomListModel->setStringList(m_chatroomList);
90}
91
92void
93DiscoveryPanel::onChatroomInfoReady(const ChatroomInfo& info)
94{
95 ui->NameData->setText(QString::fromStdString(info.getName().toUri()));
96 ui->NameSpaceData->setText(QString::fromStdString(info.getSyncPrefix().toUri()));
97
98 switch(info.getTrustModel()) {
99 case 2:
100 {
101 ui->TrustModelData->setText(QString("Hierarchical"));
102 ui->join->setEnabled(false);
103 ui->requestInvitation->setEnabled(true);
104 break;
105 }
106 case 1:
107 {
108 ui->TrustModelData->setText(QString("Web Of Trust"));
109 ui->join->setEnabled(false);
110 ui->requestInvitation->setEnabled(true);
111 break;
112 }
113 case 0:
114 {
115 ui->TrustModelData->setText(QString("None"));
116 ui->join->setEnabled(true);
117 ui->requestInvitation->setEnabled(false);
118 break;
119 }
120 default:
121 {
122 ui->TrustModelData->setText(QString("Unrecognized"));
123 ui->join->setEnabled(false);
124 ui->requestInvitation->setEnabled(false);
125 }
126 }
127
128 std::list<Name>roster = info.getParticipants();
129 m_rosterList.clear();
130 Name::Component routingHint = Name::Component(ROUTING_HINT_SEPARATOR, 2);
131 for (const auto& participant : roster) {
132 size_t i;
133 for (i = 0; i < participant.size(); ++i) {
134 if (routingHint == participant.at(i))
135 break;
136 }
137 if (i == participant.size())
138 m_rosterList << QString::fromStdString(participant.toUri());
139 else
140 m_rosterList << QString::fromStdString(participant.getSubName(i + 1).toUri());
141 }
142 m_rosterListModel->setStringList(m_rosterList);
143 ui->RosterList->setModel(m_rosterListModel);
144}
145
146// private slots
147void
148DiscoveryPanel::onSelectedChatroomChanged(const QItemSelection &selected,
149 const QItemSelection &deselected)
150{
151 QModelIndexList items = selected.indexes();
152 QString chatroomName = m_chatroomListModel->data(items.first(), Qt::DisplayRole).toString();
153
154 bool chatroomFound = false;
155 for (int i = 0; i < m_chatroomList.size(); i++) {
156 if (chatroomName == m_chatroomList[i]) {
157 chatroomFound = true;
158 m_chatroom = m_chatroomList[i];
159 m_participant.clear();
160 break;
161 }
162 }
163
164 if (!chatroomFound) {
165 emit warning("This should not happen: DiscoveryPanel::onSelectedChatroomChanged");
166 return;
167 }
168
169 emit waitForChatroomInfo(m_chatroom);
170}
171
172void
173DiscoveryPanel::onSelectedParticipantChanged(const QItemSelection &selected,
174 const QItemSelection &deselected)
175{
176 QModelIndexList items = selected.indexes();
177 QString participant = m_rosterListModel->data(items.first(), Qt::DisplayRole).toString();
178
179 bool participantFound = false;
180 for (int i = 0; i < m_rosterList.size(); i++) {
181 if (participant == m_rosterList[i]) {
182 participantFound = true;
183 m_participant = m_rosterList[i];
184 break;
185 }
186 }
187 if (!participantFound) {
188 emit warning("This should not happen: DiscoveryPanel::onSelectedParticipantChangeds #1");
189 return;
190 }
191}
192
193void
194DiscoveryPanel::onJoinClicked()
195{
196 emit startChatroom(m_chatroom, false);
197}
198
199} // namespace chronochat
200
201#if WAF
202#include "discovery-panel.moc"
203// #include "discovery-panel.cpp.moc"
204#endif