blob: 7014794be75aec505059118481af42ccd6fb69e8 [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"
19#include "exception.h"
20#endif
21
22using namespace ndn;
23
24INIT_LOGGER("ProfileEditor");
25
Yingdi Yuaa8d7692013-10-18 17:05:02 -070026ProfileEditor::ProfileEditor(Ptr<ContactManager> contactManager,
Yingdi Yuc26af3c2013-10-17 17:03:46 -070027 QWidget *parent)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070028 : QDialog(parent)
29 , ui(new Ui::ProfileEditor)
30 , m_tableModel(new QSqlTableModel())
Yingdi Yuaa8d7692013-10-18 17:05:02 -070031 , m_contactManager(contactManager)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070032{
Yingdi Yuc26af3c2013-10-17 17:03:46 -070033 ui->setupUi(this);
34
Yingdi Yub35b8652013-11-07 11:32:40 -080035 m_currentIdentity = contactManager->getDefaultIdentity();
36 ui->identityInput->setText(m_currentIdentity.toUri().c_str());
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070037
Yingdi Yuc26af3c2013-10-17 17:03:46 -070038 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 Yu0a6b6c52013-10-15 17:54:00 -070046
Yingdi Yuc26af3c2013-10-17 17:03:46 -070047
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070048
49}
50
51ProfileEditor::~ProfileEditor()
52{
53 delete ui;
54 delete m_tableModel;
55}
56
57void
58ProfileEditor::onAddClicked()
59{
60 int rowCount = m_tableModel->rowCount();
Yingdi Yuc26af3c2013-10-17 17:03:46 -070061 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 Yu0a6b6c52013-10-15 17:54:00 -070065 m_tableModel->insertRow(rowCount);
Yingdi Yuc26af3c2013-10-17 17:03:46 -070066 m_tableModel->setRecord(rowCount, record);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070067}
68
69void
70ProfileEditor::onDeleteClicked()
71{
72 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
Yingdi Yu8dacdf22013-11-05 23:06:43 -080073 QModelIndexList indexList = selectionModel->selectedIndexes();
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070074
75 int i = indexList.size() - 1;
76 for(; i >= 0; i--)
Yingdi Yuc26af3c2013-10-17 17:03:46 -070077 m_tableModel->removeRow(indexList[i].row());
78
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070079 m_tableModel->submitAll();
80}
81
82void
83ProfileEditor::onOkClicked()
84{
85 m_tableModel->submitAll();
Yingdi Yuaa8d7692013-10-18 17:05:02 -070086 m_contactManager->updateProfileData(m_currentIdentity);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070087 this->hide();
88}
89
Yingdi Yuc26af3c2013-10-17 17:03:46 -070090void
91ProfileEditor::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("'");
97
98 m_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
99 m_tableModel->setTable("SelfProfile");
100 m_tableModel->setFilter(filter.c_str());
101 m_tableModel->select();
102 m_tableModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Identity"));
103 m_tableModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Type"));
104 m_tableModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Value"));
105
106 ui->profileTable->setModel(m_tableModel);
107 ui->profileTable->setColumnHidden(0, true);
108 ui->profileTable->show();
109}
110
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700111#if WAF
112#include "profileeditor.moc"
113#include "profileeditor.cpp.moc"
114#endif