blob: 17931d0229ef3eb4cc53e3df2ca6224cefe65042 [file] [log] [blame]
Yingdi Yu0a6b6c52013-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"
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070019#endif
20
21using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080022using namespace std;
Yingdi Yua1a688f2014-02-06 18:09:22 -080023using namespace chronos;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070024
25INIT_LOGGER("ProfileEditor");
26
Yingdi Yu64206112013-12-24 11:16:32 +080027ProfileEditor::ProfileEditor(shared_ptr<ContactManager> contactManager,
Yingdi Yuc26af3c2013-10-17 17:03:46 -070028 QWidget *parent)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070029 : QDialog(parent)
30 , ui(new Ui::ProfileEditor)
31 , m_tableModel(new QSqlTableModel())
Yingdi Yuaa8d7692013-10-18 17:05:02 -070032 , m_contactManager(contactManager)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -080033 , m_keyChain(new KeyChain())
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070034{
Yingdi Yuc26af3c2013-10-17 17:03:46 -070035 ui->setupUi(this);
36
Yingdi Yub35b8652013-11-07 11:32:40 -080037 m_currentIdentity = contactManager->getDefaultIdentity();
38 ui->identityInput->setText(m_currentIdentity.toUri().c_str());
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070039
Yingdi Yuc26af3c2013-10-17 17:03:46 -070040 connect(ui->addRowButton, SIGNAL(clicked()),
41 this, SLOT(onAddClicked()));
42 connect(ui->deleteRowButton, SIGNAL(clicked()),
43 this, SLOT(onDeleteClicked()));
44 connect(ui->okButton, SIGNAL(clicked()),
45 this, SLOT(onOkClicked()));
46 connect(ui->getButton, SIGNAL(clicked()),
47 this, SLOT(onGetClicked()));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070048
Yingdi Yuc26af3c2013-10-17 17:03:46 -070049
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070050
51}
52
53ProfileEditor::~ProfileEditor()
54{
55 delete ui;
56 delete m_tableModel;
57}
58
59void
60ProfileEditor::onAddClicked()
61{
62 int rowCount = m_tableModel->rowCount();
Yingdi Yuc26af3c2013-10-17 17:03:46 -070063 QSqlRecord record;
64 QSqlField identityField("profile_identity", QVariant::String);
65 record.append(identityField);
66 record.setValue("profile_identity", QString(m_currentIdentity.toUri().c_str()));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070067 m_tableModel->insertRow(rowCount);
Yingdi Yuc26af3c2013-10-17 17:03:46 -070068 m_tableModel->setRecord(rowCount, record);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070069}
70
71void
72ProfileEditor::onDeleteClicked()
73{
74 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
Yingdi Yu8dacdf22013-11-05 23:06:43 -080075 QModelIndexList indexList = selectionModel->selectedIndexes();
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070076
77 int i = indexList.size() - 1;
78 for(; i >= 0; i--)
Yingdi Yuc26af3c2013-10-17 17:03:46 -070079 m_tableModel->removeRow(indexList[i].row());
80
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070081 m_tableModel->submitAll();
82}
83
84void
85ProfileEditor::onOkClicked()
86{
87 m_tableModel->submitAll();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070088 m_contactManager->updateProfileData(m_currentIdentity);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070089 this->hide();
90}
91
Yingdi Yuc26af3c2013-10-17 17:03:46 -070092void
93ProfileEditor::onGetClicked()
94{
95 QString inputIdentity = ui->identityInput->text();
96 m_currentIdentity = Name(inputIdentity.toUtf8().constData());
97 string filter("profile_identity = '");
98 filter.append(m_currentIdentity.toUri()).append("'");
99
100 m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
101 m_tableModel->setTable("SelfProfile");
102 m_tableModel->setFilter(filter.c_str());
103 m_tableModel->select();
104 m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
105 m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
106 m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
107
108 ui->profileTable->setModel(m_tableModel);
109 ui->profileTable->setColumnHidden(0, true);
110 ui->profileTable->show();
111}
112
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700113#if WAF
114#include "profileeditor.moc"
115#include "profileeditor.cpp.moc"
116#endif