blob: 8841d5dd28d2e3f2b98eca12e3435f343530a2dd [file] [log] [blame]
Yingdi Yu348f5ea2014-03-01 14:47:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * 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
21#ifndef Q_MOC_RUN
22#include "logging.h"
23#endif
24
Yingdi Yu0b0a7362014-08-05 16:31:30 -070025// INIT_LOGGER("ContactPanel");
26
Yingdi Yueb692ac2015-02-10 18:46:18 -080027namespace chronochat {
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028
Yingdi Yufa0b6a02014-04-30 14:26:42 -070029ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080030 : QDialog(parent)
31 , ui(new Ui::ContactPanel)
32 , m_setAliasDialog(new SetAliasDialog)
33 , m_contactListModel(new QStringListModel)
34 , m_trustScopeModel(0)
35 , m_endorseDataModel(0)
36 , m_endorseComboBoxDelegate(new EndorseComboBoxDelegate)
37{
38 ui->setupUi(this);
39 ui->ContactList->setModel(m_contactListModel);
40 m_menuAlias = new QAction("Set Alias", this);
41 m_menuDelete = new QAction("Delete", this);
42
Yingdi Yu0b0a7362014-08-05 16:31:30 -070043 connect(ui->ContactList->selectionModel(),
44 SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
45 this,
46 SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
Yingdi Yu348f5ea2014-03-01 14:47:25 -080047 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
48 this, SLOT(onContextMenuRequested(const QPoint&)));
49 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
50 this, SLOT(onIsIntroducerChanged(int)));
51 connect(ui->addScope, SIGNAL(clicked()),
52 this, SLOT(onAddScopeClicked()));
53 connect(ui->deleteScope, SIGNAL(clicked()),
54 this, SLOT(onDeleteScopeClicked()));
55 connect(ui->saveButton, SIGNAL(clicked()),
56 this, SLOT(onSaveScopeClicked()));
57 connect(ui->endorseButton, SIGNAL(clicked()),
58 this, SLOT(onEndorseButtonClicked()));
59 connect(m_setAliasDialog, SIGNAL(aliasChanged(const QString&, const QString&)),
60 this, SLOT(onAliasChanged(const QString&, const QString&)));
61}
62
63ContactPanel::~ContactPanel()
64{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070065 if (m_contactListModel)
66 delete m_contactListModel;
67 if (m_trustScopeModel)
68 delete m_trustScopeModel;
69 if (m_endorseDataModel)
70 delete m_endorseDataModel;
Yingdi Yu348f5ea2014-03-01 14:47:25 -080071
72 delete m_setAliasDialog;
73 delete m_endorseComboBoxDelegate;
74
75 delete m_menuAlias;
76 delete m_menuDelete;
77
78 delete ui;
79}
80
81//private methods
82void
83ContactPanel::resetPanel()
84{
85 // Clean up General tag.
86 ui->NameData->clear();
87 ui->NameSpaceData->clear();
88 ui->InstitutionData->clear();
89
90 // Clean up TrustScope tag.
91 ui->isIntroducer->setChecked(false);
92 ui->addScope->setEnabled(false);
93 ui->deleteScope->setEnabled(false);
94
95 m_trustScopeModel = new QSqlTableModel;
96 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
97 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
98 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
99
100 ui->trustScopeList->setModel(m_trustScopeModel);
101 ui->trustScopeList->setColumnHidden(0, true);
102 ui->trustScopeList->setColumnHidden(1, true);
103 ui->trustScopeList->show();
104 ui->trustScopeList->setEnabled(false);
105
106 // Clean up Endorse tag.
107 m_endorseDataModel = new QSqlTableModel;
108 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
109 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
110 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
111 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
112
113 ui->endorseList->setModel(m_endorseDataModel);
114 ui->endorseList->setColumnHidden(0, true);
115 ui->endorseList->resizeColumnToContents(1);
116 ui->endorseList->resizeColumnToContents(2);
117 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
118 ui->endorseList->show();
119 ui->endorseList->setEnabled(false);
120
121 // Clean up contact list.
122 m_contactAliasList.clear();
123 m_contactIdList.clear();
124 m_currentSelectedContact.clear();
125 m_contactListModel->setStringList(m_contactAliasList);
126}
127
128// public slots
129void
130ContactPanel::onCloseDBModule()
131{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700132 // _LOG_DEBUG("close db module");
133 if (m_trustScopeModel) {
134 delete m_trustScopeModel;
135 // _LOG_DEBUG("trustScopeModel closed");
136 }
137 if (m_endorseDataModel) {
138 delete m_endorseDataModel;
139 // _LOG_DEBUG("endorseDataModel closed");
140 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141}
142
143void
144ContactPanel::onIdentityUpdated(const QString& identity)
145{
146 resetPanel();
147
148 emit waitForContactList(); // Re-load contact list;
149}
150
151void
152ContactPanel::onContactAliasListReady(const QStringList& aliasList)
153{
154 m_contactAliasList = aliasList;
155 m_contactListModel->setStringList(m_contactAliasList);
156}
157
158void
159ContactPanel::onContactIdListReady(const QStringList& idList)
160{
161 m_currentSelectedContact.clear();
162 m_contactIdList = idList;
163}
164
165void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700166ContactPanel::onContactInfoReady(const QString& identity,
167 const QString& name,
168 const QString& institute,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800169 bool isIntro)
170{
171 ui->NameData->setText(name);
172 ui->NameSpaceData->setText(identity);
173 ui->InstitutionData->setText(institute);
174
175 QString filter = QString("contact_namespace = '%1'").arg(identity);
176 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
177 m_trustScopeModel->setTable("TrustScope");
178 m_trustScopeModel->setFilter(filter);
179 m_trustScopeModel->select();
180 ui->trustScopeList->setModel(m_trustScopeModel);
181 ui->trustScopeList->setColumnHidden(0, true);
182 ui->trustScopeList->setColumnHidden(1, true);
183 ui->trustScopeList->show();
184
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700185 if (isIntro) {
186 ui->isIntroducer->setChecked(true);
187 ui->addScope->setEnabled(true);
188 ui->deleteScope->setEnabled(true);
189 ui->trustScopeList->setEnabled(true);
190 }
191 else {
192 ui->isIntroducer->setChecked(false);
193 ui->addScope->setEnabled(false);
194 ui->deleteScope->setEnabled(false);
195 ui->trustScopeList->setEnabled(false);
196 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800197
198 QString filter2 = QString("profile_identity = '%1'").arg(identity);
199 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
200 m_endorseDataModel->setTable("ContactProfile");
201 m_endorseDataModel->setFilter(filter2);
202 m_endorseDataModel->select();
203 ui->endorseList->setModel(m_endorseDataModel);
204 ui->endorseList->setColumnHidden(0, true);
205 ui->endorseList->resizeColumnToContents(1);
206 ui->endorseList->resizeColumnToContents(2);
207 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
208 ui->endorseList->show();
209 ui->endorseList->setEnabled(true);
210}
211
212// private slots
213void
214ContactPanel::onSelectionChanged(const QItemSelection &selected,
215 const QItemSelection &deselected)
216{
217 QModelIndexList items = selected.indexes();
218 QString alias = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
219
220 bool contactFound = false;
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700221 for (int i = 0; i < m_contactAliasList.size(); i++) {
222 if (alias == m_contactAliasList[i]) {
223 contactFound = true;
224 m_currentSelectedContact = m_contactIdList[i];
225 break;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800226 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700227 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800228
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700229 if (!contactFound) {
230 emit warning("This should not happen: ContactPanel::updateSelection #1");
231 return;
232 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800233
234 emit waitForContactInfo(m_currentSelectedContact);
235}
236
237void
238ContactPanel::onContextMenuRequested(const QPoint& pos)
239{
240 QMenu menu(ui->ContactList);
241
242 menu.addAction(m_menuAlias);
243 connect(m_menuAlias, SIGNAL(triggered()),
244 this, SLOT(onSetAliasDialogRequested()));
245
246 menu.addAction(m_menuDelete);
247 connect(m_menuDelete, SIGNAL(triggered()),
248 this, SLOT(onContactDeletionRequested()));
249
250 menu.exec(ui->ContactList->mapToGlobal(pos));
251}
252
253void
254ContactPanel::onSetAliasDialogRequested()
255{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700256 for (int i = 0; i < m_contactIdList.size(); i++)
257 if (m_contactIdList[i] == m_currentSelectedContact) {
258 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact, m_contactAliasList[i]);
259 m_setAliasDialog->show();
260 return;
261 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800262}
263
264void
265ContactPanel::onContactDeletionRequested()
266{
267 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
268 QModelIndexList selectedList = selectionModel->selectedIndexes();
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700269
270 for (QModelIndexList::iterator it = selectedList.begin(); it != selectedList.end(); it++) {
271 QString alias = m_contactListModel->data(*it, Qt::DisplayRole).toString();
272 for (int i = 0; i < m_contactAliasList.size(); i++)
273 if (m_contactAliasList[i] == alias) {
274 emit removeContact(m_contactIdList[i]);
275 return;
276 }
277 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800278}
279
280void
281ContactPanel::onIsIntroducerChanged(int state)
282{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700283 if (state == Qt::Checked) {
284 ui->addScope->setEnabled(true);
285 ui->deleteScope->setEnabled(true);
286 ui->trustScopeList->setEnabled(true);
287 emit updateIsIntroducer(m_currentSelectedContact, true);
288 }
289 else {
290 ui->addScope->setEnabled(false);
291 ui->deleteScope->setEnabled(false);
292 ui->trustScopeList->setEnabled(false);
293 emit updateIsIntroducer(m_currentSelectedContact, false);
294 }
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800295}
296
297void
298ContactPanel::onAddScopeClicked()
299{
300 int rowCount = m_trustScopeModel->rowCount();
301 QSqlRecord record;
302 QSqlField identityField("contact_namespace", QVariant::String);
303 record.append(identityField);
304 record.setValue("contact_namespace", m_currentSelectedContact);
305 m_trustScopeModel->insertRow(rowCount);
306 m_trustScopeModel->setRecord(rowCount, record);
307}
308
309void
310ContactPanel::onDeleteScopeClicked()
311{
312 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
313 QModelIndexList indexList = selectionModel->selectedIndexes();
314
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700315 for (int i = indexList.size() - 1; i >= 0; i--)
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800316 m_trustScopeModel->removeRow(indexList[i].row());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700317
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800318 m_trustScopeModel->submitAll();
319}
320
321void
322ContactPanel::onSaveScopeClicked()
323{
324 m_trustScopeModel->submitAll();
325}
326
327void
328ContactPanel::onEndorseButtonClicked()
329{
330 m_endorseDataModel->submitAll();
331 emit updateEndorseCertificate(m_currentSelectedContact);
332}
333
334void
335ContactPanel::onAliasChanged(const QString& identity, const QString& alias)
336{
337 emit updateAlias(identity, alias);
338}
339
Yingdi Yueb692ac2015-02-10 18:46:18 -0800340} // namespace chronochat
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700341
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800342#if WAF
343#include "contact-panel.moc"
Yingdi Yu42125862014-08-07 17:04:28 -0700344// #include "contact-panel.cpp.moc"
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800345#endif