blob: d3d26081994459f18036bd5f0c4682586a2b818b [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 Yu3b318c12013-10-15 17:54:00 -070032{
Yingdi Yu8f7325a2013-10-17 17:03:46 -070033 ui->setupUi(this);
34
Yingdi Yu4685b1b2013-10-18 17:05:02 -070035 Name defaultIdentity = contactManager->getDefaultIdentity();
Yingdi Yu8f7325a2013-10-17 17:03:46 -070036 ui->identityInput->setText(defaultIdentity.toUri().c_str());
Yingdi Yu3b318c12013-10-15 17:54:00 -070037
Yingdi Yu8f7325a2013-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 Yu3b318c12013-10-15 17:54:00 -070046
Yingdi Yu8f7325a2013-10-17 17:03:46 -070047
Yingdi Yu3b318c12013-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 Yu8f7325a2013-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 Yu3b318c12013-10-15 17:54:00 -070065 m_tableModel->insertRow(rowCount);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070066 m_tableModel->setRecord(rowCount, record);
Yingdi Yu3b318c12013-10-15 17:54:00 -070067}
68
69void
70ProfileEditor::onDeleteClicked()
71{
72 QItemSelectionModel* selectionModel = ui->profileTable->selectionModel();
Yingdi Yub2e747d2013-11-05 23:06:43 -080073 QModelIndexList indexList = selectionModel->selectedIndexes();
Yingdi Yu3b318c12013-10-15 17:54:00 -070074
75 int i = indexList.size() - 1;
76 for(; i >= 0; i--)
Yingdi Yu8f7325a2013-10-17 17:03:46 -070077 m_tableModel->removeRow(indexList[i].row());
78
Yingdi Yu3b318c12013-10-15 17:54:00 -070079 m_tableModel->submitAll();
80}
81
82void
83ProfileEditor::onOkClicked()
84{
85 m_tableModel->submitAll();
Yingdi Yu4685b1b2013-10-18 17:05:02 -070086 m_contactManager->updateProfileData(m_currentIdentity);
Yingdi Yu3b318c12013-10-15 17:54:00 -070087 this->hide();
88}
89
Yingdi Yu8f7325a2013-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("'");
Yingdi Yu7989eb22013-10-31 17:38:22 -070097 _LOG_DEBUG("filter: " << filter);
Yingdi Yu8f7325a2013-10-17 17:03:46 -070098
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 Yu7989eb22013-10-31 17:38:22 -0700107 _LOG_DEBUG("row count: " << m_tableModel->rowCount());
108
Yingdi Yu8f7325a2013-10-17 17:03:46 -0700109 ui->profileTable->setModel(m_tableModel);
110 ui->profileTable->setColumnHidden(0, true);
111 ui->profileTable->show();
112}
113
Yingdi Yu3b318c12013-10-15 17:54:00 -0700114#if WAF
115#include "profileeditor.moc"
116#include "profileeditor.cpp.moc"
117#endif