blob: 4abad5a3614fcfc97cf94f30c21dc4edc166e85e [file] [log] [blame]
Yingdi Yu3b318c12013-10-15 17:54:00 -07001/* -*- 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
22using namespace ndn;
Yingdi Yu76dd8002013-12-24 11:16:32 +080023using namespace std;
24using namespace ndn::ptr_lib;
Yingdi Yu3b318c12013-10-15 17:54:00 -070025
26INIT_LOGGER("ProfileEditor");
27
Yingdi Yu76dd8002013-12-24 11:16:32 +080028ProfileEditor::ProfileEditor(shared_ptr<ContactManager> contactManager,
Yingdi Yu8f7325a2013-10-17 17:03:46 -070029 QWidget *parent)
Yingdi Yu3b318c12013-10-15 17:54:00 -070030 : QDialog(parent)
31 , ui(new Ui::ProfileEditor)
32 , m_tableModel(new QSqlTableModel())
Yingdi Yu4685b1b2013-10-18 17:05:02 -070033 , m_contactManager(contactManager)
Yingdi Yu76dd8002013-12-24 11:16:32 +080034 , m_identityManager(contactManager->getIdentityManager())
Yingdi Yu3b318c12013-10-15 17:54:00 -070035{
Yingdi Yu8f7325a2013-10-17 17:03:46 -070036 ui->setupUi(this);
37
Yingdi Yue35bdb82013-11-07 11:32:40 -080038 m_currentIdentity = contactManager->getDefaultIdentity();
39 ui->identityInput->setText(m_currentIdentity.toUri().c_str());
Yingdi Yu3b318c12013-10-15 17:54:00 -070040
Yingdi Yu8f7325a2013-10-17 17:03:46 -070041 connect(ui->addRowButton, SIGNAL(clicked()),
42 this, SLOT(onAddClicked()));
43 connect(ui->deleteRowButton, SIGNAL(clicked()),
44 this, SLOT(onDeleteClicked()));
45 connect(ui->okButton, SIGNAL(clicked()),
46 this, SLOT(onOkClicked()));
47 connect(ui->getButton, SIGNAL(clicked()),
48 this, SLOT(onGetClicked()));
Yingdi Yu3b318c12013-10-15 17:54:00 -070049
Yingdi Yu8f7325a2013-10-17 17:03:46 -070050
Yingdi Yu3b318c12013-10-15 17:54:00 -070051
52}
53
54ProfileEditor::~ProfileEditor()
55{
56 delete ui;
57 delete m_tableModel;
58}
59
60void
61ProfileEditor::onAddClicked()
62{
63 int rowCount = m_tableModel->rowCount();
Yingdi Yu8f7325a2013-10-17 17:03:46 -070064 QSqlRecord record;
65 QSqlField identityField("profile_identity", QVariant::String);
66 record.append(identityField);
67 record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str()));
Yingdi Yu3b318c12013-10-15 17:54:00 -070068 m_tableModel->insertRow(rowCount);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070069 m_tableModel->setRecord(rowCount, record);
Yingdi Yu3b318c12013-10-15 17:54:00 -070070}
71
72void
73ProfileEditor::onDeleteClicked()
74{
75 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
Yingdi Yub2e747d2013-11-05 23:06:43 -080076 QModelIndexList indexList = selectionModel->selectedIndexes();
Yingdi Yu3b318c12013-10-15 17:54:00 -070077
78 int i = indexList.size() - 1;
79 for(; i >= 0; i--)
Yingdi Yu8f7325a2013-10-17 17:03:46 -070080 m_tableModel->removeRow(indexList[i].row());
81
Yingdi Yu3b318c12013-10-15 17:54:00 -070082 m_tableModel->submitAll();
83}
84
85void
86ProfileEditor::onOkClicked()
87{
Yingdi Yu76dd8002013-12-24 11:16:32 +080088 Name defaultCertName = m_identityManager->getDefaultCertificateNameForIdentity(m_currentIdentity);
Yingdi Yu711f5892013-11-09 22:18:26 -080089 if(defaultCertName.size() == 0)
Yingdi Yue433a502013-11-10 01:34:57 -080090 {
91 emit noKeyOrCert(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
92 return;
93 }
Yingdi Yu711f5892013-11-09 22:18:26 -080094
Yingdi Yu3b318c12013-10-15 17:54:00 -070095 m_tableModel->submitAll();
Yingdi Yu4685b1b2013-10-18 17:05:02 -070096 m_contactManager->updateProfileData(m_currentIdentity);
Yingdi Yu3b318c12013-10-15 17:54:00 -070097 this->hide();
98}
99
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700100void
101ProfileEditor::onGetClicked()
102{
103 QString inputIdentity = ui->identityInput->text();
104 m_currentIdentity = Name(inputIdentity.toUtf8().constData());
105 string filter("profile_identity = '");
106 filter.append(m_currentIdentity.toUri()).append("'");
107
108 m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
109 m_tableModel->setTable("SelfProfile");
110 m_tableModel->setFilter(filter.c_str());
111 m_tableModel->select();
112 m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
113 m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
114 m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
115
116 ui->profileTable->setModel(m_tableModel);
117 ui->profileTable->setColumnHidden(0, true);
118 ui->profileTable->show();
119}
120
Yingdi Yu3b318c12013-10-15 17:54:00 -0700121#if WAF
122#include "profileeditor.moc"
123#include "profileeditor.cpp.moc"
124#endif