blob: 04b68d5d11c7027d6f8522f266260c0310fb3857 [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -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
Yingdi Yu348f5ea2014-03-01 14:47:25 -08004 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
Yingdi Yu0b0a7362014-08-05 16:31:30 -070011#include "contact-panel.hpp"
Yingdi Yu348f5ea2014-03-01 14:47:25 -080012#include "ui_contact-panel.h"
13
14#include <QMenu>
15#include <QItemSelectionModel>
16#include <QModelIndex>
17#include <QtSql/QSqlRecord>
18#include <QtSql/QSqlField>
19#include <QtSql/QSqlError>
20
Yingdi Yueb692ac2015-02-10 18:46:18 -080021namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080022
Yingdi Yufa0b6a02014-04-30 14:26:42 -070023ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080024 : QDialog(parent)
25 , ui(new Ui::ContactPanel)
26 , m_setAliasDialog(new SetAliasDialog)
27 , m_contactListModel(new QStringListModel)
28 , m_trustScopeModel(0)
29 , m_endorseDataModel(0)
30 , m_endorseComboBoxDelegate(new EndorseComboBoxDelegate)
31{
32 ui->setupUi(this);
33 ui->ContactList->setModel(m_contactListModel);
34 m_menuAlias = new QAction("Set Alias", this);
35 m_menuDelete = new QAction("Delete", this);
36
Yingdi Yu0b0a7362014-08-05 16:31:30 -070037 connect(ui->ContactList->selectionModel(),
38 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
39 this,
40 SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080041 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
42 this, SLOT(onContextMenuRequested(const QPoint&)));
43 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
44 this, SLOT(onIsIntroducerChanged(int)));
45 connect(ui->addScope, SIGNAL(clicked()),
46 this, SLOT(onAddScopeClicked()));
47 connect(ui->deleteScope, SIGNAL(clicked()),
48 this, SLOT(onDeleteScopeClicked()));
49 connect(ui->saveButton, SIGNAL(clicked()),
50 this, SLOT(onSaveScopeClicked()));
51 connect(ui->endorseButton, SIGNAL(clicked()),
52 this, SLOT(onEndorseButtonClicked()));
53 connect(m_setAliasDialog, SIGNAL(aliasChanged(const QString&, const QString&)),
54 this, SLOT(onAliasChanged(const QString&, const QString&)));
55}
56
57ContactPanel::~ContactPanel()
58{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070059 if (m_contactListModel)
60 delete m_contactListModel;
61 if (m_trustScopeModel)
62 delete m_trustScopeModel;
63 if (m_endorseDataModel)
64 delete m_endorseDataModel;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080065
66 delete m_setAliasDialog;
67 delete m_endorseComboBoxDelegate;
68
69 delete m_menuAlias;
70 delete m_menuDelete;
71
72 delete ui;
73}
74
75//private methods
76void
77ContactPanel::resetPanel()
78{
79 // Clean up General tag.
80 ui->NameData->clear();
81 ui->NameSpaceData->clear();
82 ui->InstitutionData->clear();
83
84 // Clean up TrustScope tag.
85 ui->isIntroducer->setChecked(false);
86 ui->addScope->setEnabled(false);
87 ui->deleteScope->setEnabled(false);
88
89 m_trustScopeModel = new QSqlTableModel;
90 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
91 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
92 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
93
94 ui->trustScopeList->setModel(m_trustScopeModel);
95 ui->trustScopeList->setColumnHidden(0, true);
96 ui->trustScopeList->setColumnHidden(1, true);
97 ui->trustScopeList->show();
98 ui->trustScopeList->setEnabled(false);
99
100 // Clean up Endorse tag.
101 m_endorseDataModel = new QSqlTableModel;
102 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
103 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
104 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
105 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
106
107 ui->endorseList->setModel(m_endorseDataModel);
108 ui->endorseList->setColumnHidden(0, true);
109 ui->endorseList->resizeColumnToContents(1);
110 ui->endorseList->resizeColumnToContents(2);
111 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
112 ui->endorseList->show();
113 ui->endorseList->setEnabled(false);
114
115 // Clean up contact list.
116 m_contactAliasList.clear();
117 m_contactIdList.clear();
118 m_currentSelectedContact.clear();
119 m_contactListModel->setStringList(m_contactAliasList);
120}
121
122// public slots
123void
124ContactPanel::onCloseDBModule()
125{
Varun Patila24bd3e2020-11-24 10:08:33 +0530126 if (m_trustScopeModel)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700127 delete m_trustScopeModel;
Varun Patila24bd3e2020-11-24 10:08:33 +0530128 if (m_endorseDataModel)
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700129 delete m_endorseDataModel;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800130}
131
132void
133ContactPanel::onIdentityUpdated(const QString& identity)
134{
135 resetPanel();
136
137 emit waitForContactList(); // Re-load contact list;
138}
139
140void
141ContactPanel::onContactAliasListReady(const QStringList& aliasList)
142{
143 m_contactAliasList = aliasList;
144 m_contactListModel->setStringList(m_contactAliasList);
145}
146
147void
148ContactPanel::onContactIdListReady(const QStringList& idList)
149{
150 m_currentSelectedContact.clear();
151 m_contactIdList = idList;
152}
153
154void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700155ContactPanel::onContactInfoReady(const QString& identity,
156 const QString& name,
157 const QString& institute,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800158 bool isIntro)
159{
160 ui->NameData->setText(name);
161 ui->NameSpaceData->setText(identity);
162 ui->InstitutionData->setText(institute);
163
164 QString filter = QString("contact_namespace = '%1'").arg(identity);
165 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
166 m_trustScopeModel->setTable("TrustScope");
167 m_trustScopeModel->setFilter(filter);
168 m_trustScopeModel->select();
169 ui->trustScopeList->setModel(m_trustScopeModel);
170 ui->trustScopeList->setColumnHidden(0, true);
171 ui->trustScopeList->setColumnHidden(1, true);
172 ui->trustScopeList->show();
173
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700174 if (isIntro) {
175 ui->isIntroducer->setChecked(true);
176 ui->addScope->setEnabled(true);
177 ui->deleteScope->setEnabled(true);
178 ui->trustScopeList->setEnabled(true);
179 }
180 else {
181 ui->isIntroducer->setChecked(false);
182 ui->addScope->setEnabled(false);
183 ui->deleteScope->setEnabled(false);
184 ui->trustScopeList->setEnabled(false);
185 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800186
187 QString filter2 = QString("profile_identity = '%1'").arg(identity);
188 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
189 m_endorseDataModel->setTable("ContactProfile");
190 m_endorseDataModel->setFilter(filter2);
191 m_endorseDataModel->select();
192 ui->endorseList->setModel(m_endorseDataModel);
193 ui->endorseList->setColumnHidden(0, true);
194 ui->endorseList->resizeColumnToContents(1);
195 ui->endorseList->resizeColumnToContents(2);
196 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
197 ui->endorseList->show();
198 ui->endorseList->setEnabled(true);
199}
200
201// private slots
202void
Varun Patila24bd3e2020-11-24 10:08:33 +0530203ContactPanel::onSelectionChanged(const QItemSelection& selected,
204 const QItemSelection& deselected)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800205{
206 QModelIndexList items = selected.indexes();
207 QString alias = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
208
209 bool contactFound = false;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700210 for (int i = 0; i < m_contactAliasList.size(); i++) {
211 if (alias == m_contactAliasList[i]) {
212 contactFound = true;
213 m_currentSelectedContact = m_contactIdList[i];
214 break;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800215 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700216 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800217
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700218 if (!contactFound) {
219 emit warning("This should not happen: ContactPanel::updateSelection #1");
220 return;
221 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800222
223 emit waitForContactInfo(m_currentSelectedContact);
224}
225
226void
227ContactPanel::onContextMenuRequested(const QPoint& pos)
228{
229 QMenu menu(ui->ContactList);
230
231 menu.addAction(m_menuAlias);
232 connect(m_menuAlias, SIGNAL(triggered()),
233 this, SLOT(onSetAliasDialogRequested()));
234
235 menu.addAction(m_menuDelete);
236 connect(m_menuDelete, SIGNAL(triggered()),
237 this, SLOT(onContactDeletionRequested()));
238
239 menu.exec(ui->ContactList->mapToGlobal(pos));
240}
241
242void
243ContactPanel::onSetAliasDialogRequested()
244{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700245 for (int i = 0; i < m_contactIdList.size(); i++)
246 if (m_contactIdList[i] == m_currentSelectedContact) {
247 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact, m_contactAliasList[i]);
248 m_setAliasDialog->show();
249 return;
250 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800251}
252
253void
254ContactPanel::onContactDeletionRequested()
255{
256 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
257 QModelIndexList selectedList = selectionModel->selectedIndexes();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700258
259 for (QModelIndexList::iterator it = selectedList.begin(); it != selectedList.end(); it++) {
260 QString alias = m_contactListModel->data(*it, Qt::DisplayRole).toString();
261 for (int i = 0; i < m_contactAliasList.size(); i++)
262 if (m_contactAliasList[i] == alias) {
263 emit removeContact(m_contactIdList[i]);
264 return;
265 }
266 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800267}
268
269void
270ContactPanel::onIsIntroducerChanged(int state)
271{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700272 if (state == Qt::Checked) {
273 ui->addScope->setEnabled(true);
274 ui->deleteScope->setEnabled(true);
275 ui->trustScopeList->setEnabled(true);
276 emit updateIsIntroducer(m_currentSelectedContact, true);
277 }
278 else {
279 ui->addScope->setEnabled(false);
280 ui->deleteScope->setEnabled(false);
281 ui->trustScopeList->setEnabled(false);
282 emit updateIsIntroducer(m_currentSelectedContact, false);
283 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800284}
285
286void
287ContactPanel::onAddScopeClicked()
288{
289 int rowCount = m_trustScopeModel->rowCount();
290 QSqlRecord record;
291 QSqlField identityField("contact_namespace", QVariant::String);
292 record.append(identityField);
293 record.setValue("contact_namespace", m_currentSelectedContact);
294 m_trustScopeModel->insertRow(rowCount);
295 m_trustScopeModel->setRecord(rowCount, record);
296}
297
298void
299ContactPanel::onDeleteScopeClicked()
300{
301 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
302 QModelIndexList indexList = selectionModel->selectedIndexes();
303
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700304 for (int i = indexList.size() - 1; i >= 0; i--)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800305 m_trustScopeModel->removeRow(indexList[i].row());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700306
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800307 m_trustScopeModel->submitAll();
308}
309
310void
311ContactPanel::onSaveScopeClicked()
312{
313 m_trustScopeModel->submitAll();
314}
315
316void
317ContactPanel::onEndorseButtonClicked()
318{
319 m_endorseDataModel->submitAll();
320 emit updateEndorseCertificate(m_currentSelectedContact);
321}
322
323void
324ContactPanel::onAliasChanged(const QString& identity, const QString& alias)
325{
326 emit updateAlias(identity, alias);
327}
328
Yingdi Yueb692ac2015-02-10 18:46:18 -0800329} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700330
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800331#if WAF
332#include "contact-panel.moc"
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800333#endif