blob: f0f92d218c9e5f5565c7310f68b83b9c2e7399fd [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;
23
24INIT_LOGGER("ProfileEditor");
25
Yingdi Yu4685b1b2013-10-18 17:05:02 -070026ProfileEditor::ProfileEditor(Ptr<ContactManager> contactManager,
Yingdi Yu8f7325a2013-10-17 17:03:46 -070027 QWidget *parent)
Yingdi Yu3b318c12013-10-15 17:54:00 -070028 : QDialog(parent)
29 , ui(new Ui::ProfileEditor)
30 , m_tableModel(new QSqlTableModel())
Yingdi Yu4685b1b2013-10-18 17:05:02 -070031 , m_contactManager(contactManager)
Yingdi Yu711f5892013-11-09 22:18:26 -080032 , m_identityManager(ndn::Ptr<ndn::security::IdentityManager>::Create())
Yingdi Yu3b318c12013-10-15 17:54:00 -070033{
Yingdi Yu8f7325a2013-10-17 17:03:46 -070034 ui->setupUi(this);
35
Yingdi Yue35bdb82013-11-07 11:32:40 -080036 m_currentIdentity = contactManager->getDefaultIdentity();
37 ui->identityInput->setText(m_currentIdentity.toUri().c_str());
Yingdi Yu3b318c12013-10-15 17:54:00 -070038
Yingdi Yu8f7325a2013-10-17 17:03:46 -070039 connect(ui->addRowButton, SIGNAL(clicked()),
40 this, SLOT(onAddClicked()));
41 connect(ui->deleteRowButton, SIGNAL(clicked()),
42 this, SLOT(onDeleteClicked()));
43 connect(ui->okButton, SIGNAL(clicked()),
44 this, SLOT(onOkClicked()));
45 connect(ui->getButton, SIGNAL(clicked()),
46 this, SLOT(onGetClicked()));
Yingdi Yu3b318c12013-10-15 17:54:00 -070047
Yingdi Yu8f7325a2013-10-17 17:03:46 -070048
Yingdi Yu3b318c12013-10-15 17:54:00 -070049
50}
51
52ProfileEditor::~ProfileEditor()
53{
54 delete ui;
55 delete m_tableModel;
56}
57
58void
59ProfileEditor::onAddClicked()
60{
61 int rowCount = m_tableModel->rowCount();
Yingdi Yu8f7325a2013-10-17 17:03:46 -070062 QSqlRecord record;
63 QSqlField identityField("profile_identity", QVariant::String);
64 record.append(identityField);
65 record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str()));
Yingdi Yu3b318c12013-10-15 17:54:00 -070066 m_tableModel->insertRow(rowCount);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070067 m_tableModel->setRecord(rowCount, record);
Yingdi Yu3b318c12013-10-15 17:54:00 -070068}
69
70void
71ProfileEditor::onDeleteClicked()
72{
73 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
Yingdi Yub2e747d2013-11-05 23:06:43 -080074 QModelIndexList indexList = selectionModel->selectedIndexes();
Yingdi Yu3b318c12013-10-15 17:54:00 -070075
76 int i = indexList.size() - 1;
77 for(; i >= 0; i--)
Yingdi Yu8f7325a2013-10-17 17:03:46 -070078 m_tableModel->removeRow(indexList[i].row());
79
Yingdi Yu3b318c12013-10-15 17:54:00 -070080 m_tableModel->submitAll();
81}
82
83void
84ProfileEditor::onOkClicked()
85{
Yingdi Yu711f5892013-11-09 22:18:26 -080086 Name defaultKeyName = m_identityManager->getPublicStorage()->getDefaultKeyNameForIdentity(m_currentIdentity);
87 if(defaultKeyName.size() == 0)
88 emit noKeyOrCert(QString::fromStdString("Corresponding key is missing!\nHave you created the key?"));
89 Name defaultCertName = m_identityManager->getPublicStorage()->getDefaultCertificateNameForKey(defaultKeyName);
90 if(defaultCertName.size() == 0)
91 emit noKeyOrCert(QString::fromStdString("Corresponding certificate is missing!\nHave you installed the certificate?"));
92
93
Yingdi Yu3b318c12013-10-15 17:54:00 -070094 m_tableModel->submitAll();
Yingdi Yu4685b1b2013-10-18 17:05:02 -070095 m_contactManager->updateProfileData(m_currentIdentity);
Yingdi Yu3b318c12013-10-15 17:54:00 -070096 this->hide();
97}
98
Yingdi Yu8f7325a2013-10-17 17:03:46 -070099void
100ProfileEditor::onGetClicked()
101{
102 QString inputIdentity = ui->identityInput->text();
103 m_currentIdentity = Name(inputIdentity.toUtf8().constData());
104 string filter("profile_identity = '");
105 filter.append(m_currentIdentity.toUri()).append("'");
106
107 m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
108 m_tableModel->setTable("SelfProfile");
109 m_tableModel->setFilter(filter.c_str());
110 m_tableModel->select();
111 m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
112 m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
113 m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
114
115 ui->profileTable->setModel(m_tableModel);
116 ui->profileTable->setColumnHidden(0, true);
117 ui->profileTable->show();
118}
119
Yingdi Yu3b318c12013-10-15 17:54:00 -0700120#if WAF
121#include "profileeditor.moc"
122#include "profileeditor.cpp.moc"
123#endif