Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 3 | * Copyright (c) 2020, Regents of the University of California |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 11 | #include "contact-panel.hpp" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 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 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 21 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 22 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 23 | ContactPanel::ContactPanel(QWidget *parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 24 | : 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 37 | connect(ui->ContactList->selectionModel(), |
| 38 | SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 39 | this, |
| 40 | SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 41 | 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 | |
| 57 | ContactPanel::~ContactPanel() |
| 58 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 59 | if (m_contactListModel) |
| 60 | delete m_contactListModel; |
| 61 | if (m_trustScopeModel) |
| 62 | delete m_trustScopeModel; |
| 63 | if (m_endorseDataModel) |
| 64 | delete m_endorseDataModel; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 65 | |
| 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 |
| 76 | void |
| 77 | ContactPanel::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 |
| 123 | void |
| 124 | ContactPanel::onCloseDBModule() |
| 125 | { |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 126 | if (m_trustScopeModel) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 127 | delete m_trustScopeModel; |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 128 | if (m_endorseDataModel) |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 129 | delete m_endorseDataModel; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
| 133 | ContactPanel::onIdentityUpdated(const QString& identity) |
| 134 | { |
| 135 | resetPanel(); |
| 136 | |
| 137 | emit waitForContactList(); // Re-load contact list; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | ContactPanel::onContactAliasListReady(const QStringList& aliasList) |
| 142 | { |
| 143 | m_contactAliasList = aliasList; |
| 144 | m_contactListModel->setStringList(m_contactAliasList); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | ContactPanel::onContactIdListReady(const QStringList& idList) |
| 149 | { |
| 150 | m_currentSelectedContact.clear(); |
| 151 | m_contactIdList = idList; |
| 152 | } |
| 153 | |
| 154 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 155 | ContactPanel::onContactInfoReady(const QString& identity, |
| 156 | const QString& name, |
| 157 | const QString& institute, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 158 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 174 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 186 | |
| 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 |
| 202 | void |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame] | 203 | ContactPanel::onSelectionChanged(const QItemSelection& selected, |
| 204 | const QItemSelection& deselected) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 205 | { |
| 206 | QModelIndexList items = selected.indexes(); |
| 207 | QString alias = m_contactListModel->data(items.first(), Qt::DisplayRole).toString(); |
| 208 | |
| 209 | bool contactFound = false; |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 210 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 215 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 216 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 217 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 218 | if (!contactFound) { |
| 219 | emit warning("This should not happen: ContactPanel::updateSelection #1"); |
| 220 | return; |
| 221 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 222 | |
| 223 | emit waitForContactInfo(m_currentSelectedContact); |
| 224 | } |
| 225 | |
| 226 | void |
| 227 | ContactPanel::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 | |
| 242 | void |
| 243 | ContactPanel::onSetAliasDialogRequested() |
| 244 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 245 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | ContactPanel::onContactDeletionRequested() |
| 255 | { |
| 256 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 257 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 258 | |
| 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void |
| 270 | ContactPanel::onIsIntroducerChanged(int state) |
| 271 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 272 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void |
| 287 | ContactPanel::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 | |
| 298 | void |
| 299 | ContactPanel::onDeleteScopeClicked() |
| 300 | { |
| 301 | QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel(); |
| 302 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 303 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 304 | for (int i = indexList.size() - 1; i >= 0; i--) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 305 | m_trustScopeModel->removeRow(indexList[i].row()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 306 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 307 | m_trustScopeModel->submitAll(); |
| 308 | } |
| 309 | |
| 310 | void |
| 311 | ContactPanel::onSaveScopeClicked() |
| 312 | { |
| 313 | m_trustScopeModel->submitAll(); |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | ContactPanel::onEndorseButtonClicked() |
| 318 | { |
| 319 | m_endorseDataModel->submitAll(); |
| 320 | emit updateEndorseCertificate(m_currentSelectedContact); |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | ContactPanel::onAliasChanged(const QString& identity, const QString& alias) |
| 325 | { |
| 326 | emit updateAlias(identity, alias); |
| 327 | } |
| 328 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 329 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 330 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 331 | #if WAF |
| 332 | #include "contact-panel.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 333 | #endif |