Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 1 | /* -*- 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 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 | |
| 21 | #ifndef Q_MOC_RUN |
| 22 | #include "logging.h" |
| 23 | #endif |
| 24 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 25 | // INIT_LOGGER("ContactPanel"); |
| 26 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 27 | namespace chronochat { |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 29 | ContactPanel::ContactPanel(QWidget *parent) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 30 | : 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 43 | connect(ui->ContactList->selectionModel(), |
| 44 | SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), |
| 45 | this, |
| 46 | SLOT(onSelectionChanged(const QItemSelection &, const QItemSelection &))); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 47 | 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 | |
| 63 | ContactPanel::~ContactPanel() |
| 64 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 65 | if (m_contactListModel) |
| 66 | delete m_contactListModel; |
| 67 | if (m_trustScopeModel) |
| 68 | delete m_trustScopeModel; |
| 69 | if (m_endorseDataModel) |
| 70 | delete m_endorseDataModel; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 71 | |
| 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 |
| 82 | void |
| 83 | ContactPanel::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 |
| 129 | void |
| 130 | ContactPanel::onCloseDBModule() |
| 131 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 132 | // _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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | ContactPanel::onIdentityUpdated(const QString& identity) |
| 145 | { |
| 146 | resetPanel(); |
| 147 | |
| 148 | emit waitForContactList(); // Re-load contact list; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | ContactPanel::onContactAliasListReady(const QStringList& aliasList) |
| 153 | { |
| 154 | m_contactAliasList = aliasList; |
| 155 | m_contactListModel->setStringList(m_contactAliasList); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | ContactPanel::onContactIdListReady(const QStringList& idList) |
| 160 | { |
| 161 | m_currentSelectedContact.clear(); |
| 162 | m_contactIdList = idList; |
| 163 | } |
| 164 | |
| 165 | void |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 166 | ContactPanel::onContactInfoReady(const QString& identity, |
| 167 | const QString& name, |
| 168 | const QString& institute, |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 169 | 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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 185 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 197 | |
| 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 |
| 213 | void |
| 214 | ContactPanel::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 Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 221 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 226 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 227 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 228 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 229 | if (!contactFound) { |
| 230 | emit warning("This should not happen: ContactPanel::updateSelection #1"); |
| 231 | return; |
| 232 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 233 | |
| 234 | emit waitForContactInfo(m_currentSelectedContact); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | ContactPanel::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 | |
| 253 | void |
| 254 | ContactPanel::onSetAliasDialogRequested() |
| 255 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 256 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
| 265 | ContactPanel::onContactDeletionRequested() |
| 266 | { |
| 267 | QItemSelectionModel* selectionModel = ui->ContactList->selectionModel(); |
| 268 | QModelIndexList selectedList = selectionModel->selectedIndexes(); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 269 | |
| 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void |
| 281 | ContactPanel::onIsIntroducerChanged(int state) |
| 282 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 283 | 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 Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void |
| 298 | ContactPanel::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 | |
| 309 | void |
| 310 | ContactPanel::onDeleteScopeClicked() |
| 311 | { |
| 312 | QItemSelectionModel* selectionModel = ui->trustScopeList->selectionModel(); |
| 313 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
| 314 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 315 | for (int i = indexList.size() - 1; i >= 0; i--) |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 316 | m_trustScopeModel->removeRow(indexList[i].row()); |
Yingdi Yu | fa0b6a0 | 2014-04-30 14:26:42 -0700 | [diff] [blame] | 317 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 318 | m_trustScopeModel->submitAll(); |
| 319 | } |
| 320 | |
| 321 | void |
| 322 | ContactPanel::onSaveScopeClicked() |
| 323 | { |
| 324 | m_trustScopeModel->submitAll(); |
| 325 | } |
| 326 | |
| 327 | void |
| 328 | ContactPanel::onEndorseButtonClicked() |
| 329 | { |
| 330 | m_endorseDataModel->submitAll(); |
| 331 | emit updateEndorseCertificate(m_currentSelectedContact); |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | ContactPanel::onAliasChanged(const QString& identity, const QString& alias) |
| 336 | { |
| 337 | emit updateAlias(identity, alias); |
| 338 | } |
| 339 | |
Yingdi Yu | eb692ac | 2015-02-10 18:46:18 -0800 | [diff] [blame] | 340 | } // namespace chronochat |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 341 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 342 | #if WAF |
| 343 | #include "contact-panel.moc" |
Yingdi Yu | 4212586 | 2014-08-07 17:04:28 -0700 | [diff] [blame] | 344 | // #include "contact-panel.cpp.moc" |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 345 | #endif |