blob: 323667b4c4949ed41d05a502402c384c8e10af1a [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
11#include "contact-panel.h"
12#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
25INIT_LOGGER("ContactPanel");
26
Yingdi Yufa0b6a02014-04-30 14:26:42 -070027ContactPanel::ContactPanel(QWidget *parent)
Yingdi Yu348f5ea2014-03-01 14:47:25 -080028 : QDialog(parent)
29 , ui(new Ui::ContactPanel)
30 , m_setAliasDialog(new SetAliasDialog)
31 , m_contactListModel(new QStringListModel)
32 , m_trustScopeModel(0)
33 , m_endorseDataModel(0)
34 , m_endorseComboBoxDelegate(new EndorseComboBoxDelegate)
35{
36 ui->setupUi(this);
37 ui->ContactList->setModel(m_contactListModel);
38 m_menuAlias = new QAction("Set Alias", this);
39 m_menuDelete = new QAction("Delete", this);
40
41 connect(ui->ContactList->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
42 this, SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &)));
43 connect(ui->ContactList, SIGNAL(customContextMenuRequested(const QPoint&)),
44 this, SLOT(onContextMenuRequested(const QPoint&)));
45 connect(ui->isIntroducer, SIGNAL(stateChanged(int)),
46 this, SLOT(onIsIntroducerChanged(int)));
47 connect(ui->addScope, SIGNAL(clicked()),
48 this, SLOT(onAddScopeClicked()));
49 connect(ui->deleteScope, SIGNAL(clicked()),
50 this, SLOT(onDeleteScopeClicked()));
51 connect(ui->saveButton, SIGNAL(clicked()),
52 this, SLOT(onSaveScopeClicked()));
53 connect(ui->endorseButton, SIGNAL(clicked()),
54 this, SLOT(onEndorseButtonClicked()));
55 connect(m_setAliasDialog, SIGNAL(aliasChanged(const QString&, const QString&)),
56 this, SLOT(onAliasChanged(const QString&, const QString&)));
57}
58
59ContactPanel::~ContactPanel()
60{
61 if(m_contactListModel) delete m_contactListModel;
62 if(m_trustScopeModel) delete m_trustScopeModel;
63 if(m_endorseDataModel) delete m_endorseDataModel;
64
65 delete m_setAliasDialog;
66 delete m_endorseComboBoxDelegate;
67
68 delete m_menuAlias;
69 delete m_menuDelete;
70
71 delete ui;
72}
73
74//private methods
75void
76ContactPanel::resetPanel()
77{
78 // Clean up General tag.
79 ui->NameData->clear();
80 ui->NameSpaceData->clear();
81 ui->InstitutionData->clear();
82
83 // Clean up TrustScope tag.
84 ui->isIntroducer->setChecked(false);
85 ui->addScope->setEnabled(false);
86 ui->deleteScope->setEnabled(false);
87
88 m_trustScopeModel = new QSqlTableModel;
89 m_trustScopeModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
90 m_trustScopeModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Contact"));
91 m_trustScopeModel->setHeaderData(2, Qt::Horizontal, QObject::tr("TrustScope"));
92
93 ui->trustScopeList->setModel(m_trustScopeModel);
94 ui->trustScopeList->setColumnHidden(0, true);
95 ui->trustScopeList->setColumnHidden(1, true);
96 ui->trustScopeList->show();
97 ui->trustScopeList->setEnabled(false);
98
99 // Clean up Endorse tag.
100 m_endorseDataModel = new QSqlTableModel;
101 m_endorseDataModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
102 m_endorseDataModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
103 m_endorseDataModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
104 m_endorseDataModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Endorse"));
105
106 ui->endorseList->setModel(m_endorseDataModel);
107 ui->endorseList->setColumnHidden(0, true);
108 ui->endorseList->resizeColumnToContents(1);
109 ui->endorseList->resizeColumnToContents(2);
110 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
111 ui->endorseList->show();
112 ui->endorseList->setEnabled(false);
113
114 // Clean up contact list.
115 m_contactAliasList.clear();
116 m_contactIdList.clear();
117 m_currentSelectedContact.clear();
118 m_contactListModel->setStringList(m_contactAliasList);
119}
120
121// public slots
122void
123ContactPanel::onCloseDBModule()
124{
125 _LOG_DEBUG("close db module");
126 if(m_trustScopeModel)
127 {
128 delete m_trustScopeModel;
129 _LOG_DEBUG("trustScopeModel closed");
130 }
131 if(m_endorseDataModel)
132 {
133 delete m_endorseDataModel;
134 _LOG_DEBUG("endorseDataModel closed");
135 }
136}
137
138void
139ContactPanel::onIdentityUpdated(const QString& identity)
140{
141 resetPanel();
142
143 emit waitForContactList(); // Re-load contact list;
144}
145
146void
147ContactPanel::onContactAliasListReady(const QStringList& aliasList)
148{
149 m_contactAliasList = aliasList;
150 m_contactListModel->setStringList(m_contactAliasList);
151}
152
153void
154ContactPanel::onContactIdListReady(const QStringList& idList)
155{
156 m_currentSelectedContact.clear();
157 m_contactIdList = idList;
158}
159
160void
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700161ContactPanel::onContactInfoReady(const QString& identity,
162 const QString& name,
163 const QString& institute,
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800164 bool isIntro)
165{
166 ui->NameData->setText(name);
167 ui->NameSpaceData->setText(identity);
168 ui->InstitutionData->setText(institute);
169
170 QString filter = QString("contact_namespace = '%1'").arg(identity);
171 m_trustScopeModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
172 m_trustScopeModel->setTable("TrustScope");
173 m_trustScopeModel->setFilter(filter);
174 m_trustScopeModel->select();
175 ui->trustScopeList->setModel(m_trustScopeModel);
176 ui->trustScopeList->setColumnHidden(0, true);
177 ui->trustScopeList->setColumnHidden(1, true);
178 ui->trustScopeList->show();
179
180 if(isIntro)
181 {
182 ui->isIntroducer->setChecked(true);
183 ui->addScope->setEnabled(true);
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700184 ui->deleteScope->setEnabled(true);
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800185 ui->trustScopeList->setEnabled(true);
186 }
187 else
188 {
189 ui->isIntroducer->setChecked(false);
190 ui->addScope->setEnabled(false);
191 ui->deleteScope->setEnabled(false);
192 ui->trustScopeList->setEnabled(false);
193 }
194
195 QString filter2 = QString("profile_identity = '%1'").arg(identity);
196 m_endorseDataModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
197 m_endorseDataModel->setTable("ContactProfile");
198 m_endorseDataModel->setFilter(filter2);
199 m_endorseDataModel->select();
200 ui->endorseList->setModel(m_endorseDataModel);
201 ui->endorseList->setColumnHidden(0, true);
202 ui->endorseList->resizeColumnToContents(1);
203 ui->endorseList->resizeColumnToContents(2);
204 ui->endorseList->setItemDelegateForColumn(3, m_endorseComboBoxDelegate);
205 ui->endorseList->show();
206 ui->endorseList->setEnabled(true);
207}
208
209// private slots
210void
211ContactPanel::onSelectionChanged(const QItemSelection &selected,
212 const QItemSelection &deselected)
213{
214 QModelIndexList items = selected.indexes();
215 QString alias = m_contactListModel->data(items.first(), Qt::DisplayRole).toString();
216
217 bool contactFound = false;
218 for(int i = 0; i < m_contactAliasList.size(); i++)
219 {
220 if(alias == m_contactAliasList[i])
221 {
222 contactFound = true;
223 m_currentSelectedContact = m_contactIdList[i];
224 break;
225 }
226 }
227
228 if(!contactFound)
229 {
230 emit warning("This should not happen: ContactPanel::updateSelection #1");
231 return;
232 }
233
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{
256 for(int i = 0; i < m_contactIdList.size(); i++)
257 if(m_contactIdList[i] == m_currentSelectedContact)
258 {
259 m_setAliasDialog->setTargetIdentity(m_currentSelectedContact, m_contactAliasList[i]);
260 m_setAliasDialog->show();
261 return;
262 }
263}
264
265void
266ContactPanel::onContactDeletionRequested()
267{
268 QItemSelectionModel* selectionModel = ui->ContactList->selectionModel();
269 QModelIndexList selectedList = selectionModel->selectedIndexes();
270 QModelIndexList::iterator it = selectedList.begin();
271 for(; it != selectedList.end(); it++)
272 {
273 QString alias = m_contactListModel->data(*it, Qt::DisplayRole).toString();
274 for(int i = 0; i < m_contactAliasList.size(); i++)
275 if(m_contactAliasList[i] == alias)
276 {
277 emit removeContact(m_contactIdList[i]);
278 return;
279 }
280 }
281}
282
283void
284ContactPanel::onIsIntroducerChanged(int state)
285{
286 if(state == Qt::Checked)
287 {
288 ui->addScope->setEnabled(true);
289 ui->deleteScope->setEnabled(true);
290 ui->trustScopeList->setEnabled(true);
291 emit updateIsIntroducer(m_currentSelectedContact, true);
292 }
293 else
294 {
295 ui->addScope->setEnabled(false);
296 ui->deleteScope->setEnabled(false);
297 ui->trustScopeList->setEnabled(false);
298 emit updateIsIntroducer(m_currentSelectedContact, false);
299 }
300}
301
302void
303ContactPanel::onAddScopeClicked()
304{
305 int rowCount = m_trustScopeModel->rowCount();
306 QSqlRecord record;
307 QSqlField identityField("contact_namespace", QVariant::String);
308 record.append(identityField);
309 record.setValue("contact_namespace", m_currentSelectedContact);
310 m_trustScopeModel->insertRow(rowCount);
311 m_trustScopeModel->setRecord(rowCount, record);
312}
313
314void
315ContactPanel::onDeleteScopeClicked()
316{
317 QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel();
318 QModelIndexList indexList = selectionModel->selectedIndexes();
319
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700320 int i = indexList.size() - 1;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800321 for(; i >= 0; i--)
322 m_trustScopeModel->removeRow(indexList[i].row());
Yingdi Yufa0b6a02014-04-30 14:26:42 -0700323
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800324 m_trustScopeModel->submitAll();
325}
326
327void
328ContactPanel::onSaveScopeClicked()
329{
330 m_trustScopeModel->submitAll();
331}
332
333void
334ContactPanel::onEndorseButtonClicked()
335{
336 m_endorseDataModel->submitAll();
337 emit updateEndorseCertificate(m_currentSelectedContact);
338}
339
340void
341ContactPanel::onAliasChanged(const QString& identity, const QString& alias)
342{
343 emit updateAlias(identity, alias);
344}
345
346#if WAF
347#include "contact-panel.moc"
348#include "contact-panel.cpp.moc"
349#endif