Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [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 | |
| 11 | #include "profileeditor.h" |
| 12 | #include "ui_profileeditor.h" |
| 13 | #include <QtSql/QSqlRecord> |
| 14 | #include <QtSql/QSqlField> |
| 15 | #include <QtSql/QSqlError> |
| 16 | |
| 17 | #ifndef Q_MOC_RUN |
| 18 | #include "logging.h" |
| 19 | #include "exception.h" |
| 20 | #endif |
| 21 | |
| 22 | using namespace ndn; |
| 23 | |
| 24 | INIT_LOGGER("ProfileEditor"); |
| 25 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 26 | ProfileEditor::ProfileEditor(Ptr<ContactManager> contactManager, |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 27 | QWidget *parent) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 28 | : QDialog(parent) |
| 29 | , ui(new Ui::ProfileEditor) |
| 30 | , m_tableModel(new QSqlTableModel()) |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 31 | , m_contactManager(contactManager) |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 32 | { |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 33 | ui->setupUi(this); |
| 34 | |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 35 | Name defaultIdentity = contactManager->getDefaultIdentity(); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 36 | ui->identityInput->setText(defaultIdentity.toUri().c_str()); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 38 | connect(ui->addRowButton, SIGNAL(clicked()), |
| 39 | this, SLOT(onAddClicked())); |
| 40 | connect(ui->deleteRowButton, SIGNAL(clicked()), |
| 41 | this, SLOT(onDeleteClicked())); |
| 42 | connect(ui->okButton, SIGNAL(clicked()), |
| 43 | this, SLOT(onOkClicked())); |
| 44 | connect(ui->getButton, SIGNAL(clicked()), |
| 45 | this, SLOT(onGetClicked())); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 46 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 48 | |
| 49 | } |
| 50 | |
| 51 | ProfileEditor::~ProfileEditor() |
| 52 | { |
| 53 | delete ui; |
| 54 | delete m_tableModel; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | ProfileEditor::onAddClicked() |
| 59 | { |
| 60 | int rowCount = m_tableModel->rowCount(); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 61 | QSqlRecord record; |
| 62 | QSqlField identityField("profile_identity", QVariant::String); |
| 63 | record.append(identityField); |
| 64 | record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str())); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 65 | m_tableModel->insertRow(rowCount); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 66 | m_tableModel->setRecord(rowCount, record); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | ProfileEditor::onDeleteClicked() |
| 71 | { |
| 72 | QItemSelectionModel* selectionModel = ui->profileTable->selectionModel(); |
Yingdi Yu | b2e747d | 2013-11-05 23:06:43 -0800 | [diff] [blame^] | 73 | QModelIndexList indexList = selectionModel->selectedIndexes(); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 74 | |
| 75 | int i = indexList.size() - 1; |
| 76 | for(; i >= 0; i--) |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 77 | m_tableModel->removeRow(indexList[i].row()); |
| 78 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 79 | m_tableModel->submitAll(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | ProfileEditor::onOkClicked() |
| 84 | { |
| 85 | m_tableModel->submitAll(); |
Yingdi Yu | 4685b1b | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 86 | m_contactManager->updateProfileData(m_currentIdentity); |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 87 | this->hide(); |
| 88 | } |
| 89 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 90 | void |
| 91 | ProfileEditor::onGetClicked() |
| 92 | { |
| 93 | QString inputIdentity = ui->identityInput->text(); |
| 94 | m_currentIdentity = Name(inputIdentity.toUtf8().constData()); |
| 95 | string filter("profile_identity = '"); |
| 96 | filter.append(m_currentIdentity.toUri()).append("'"); |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 97 | _LOG_DEBUG("filter: " << filter); |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 98 | |
| 99 | m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit); |
| 100 | m_tableModel->setTable("SelfProfile"); |
| 101 | m_tableModel->setFilter(filter.c_str()); |
| 102 | m_tableModel->select(); |
| 103 | m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity")); |
| 104 | m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type")); |
| 105 | m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value")); |
| 106 | |
Yingdi Yu | 7989eb2 | 2013-10-31 17:38:22 -0700 | [diff] [blame] | 107 | _LOG_DEBUG("row count: " << m_tableModel->rowCount()); |
| 108 | |
Yingdi Yu | 8f7325a | 2013-10-17 17:03:46 -0700 | [diff] [blame] | 109 | ui->profileTable->setModel(m_tableModel); |
| 110 | ui->profileTable->setColumnHidden(0, true); |
| 111 | ui->profileTable->show(); |
| 112 | } |
| 113 | |
Yingdi Yu | 3b318c1 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 114 | #if WAF |
| 115 | #include "profileeditor.moc" |
| 116 | #include "profileeditor.cpp.moc" |
| 117 | #endif |