2010-10-06 13:40:50 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EditEntryWidget.h"
|
|
|
|
#include "ui_EditEntryWidget.h"
|
2012-04-14 09:43:41 -04:00
|
|
|
#include "ui_EditEntryWidgetAdvanced.h"
|
2010-10-06 13:40:50 -04:00
|
|
|
#include "ui_EditEntryWidgetMain.h"
|
|
|
|
#include "ui_EditEntryWidgetNotes.h"
|
|
|
|
|
|
|
|
#include <QtGui/QListWidget>
|
|
|
|
#include <QtGui/QStackedLayout>
|
2012-04-17 10:04:49 -04:00
|
|
|
#include <QtGui/QMessageBox>
|
2010-10-06 13:40:50 -04:00
|
|
|
|
|
|
|
#include "core/Entry.h"
|
2010-10-06 16:54:07 -04:00
|
|
|
#include "core/Group.h"
|
2012-04-14 09:43:41 -04:00
|
|
|
#include "gui/EntryAttachmentsModel.h"
|
|
|
|
#include "gui/EntryAttributesModel.h"
|
2010-10-06 13:40:50 -04:00
|
|
|
|
|
|
|
EditEntryWidget::EditEntryWidget(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, m_entry(0)
|
|
|
|
, m_ui(new Ui::EditEntryWidget())
|
|
|
|
, m_mainUi(new Ui::EditEntryWidgetMain())
|
|
|
|
, m_notesUi(new Ui::EditEntryWidgetNotes())
|
2012-04-14 09:43:41 -04:00
|
|
|
, m_advancedUi(new Ui::EditEntryWidgetAdvanced())
|
2010-10-06 13:40:50 -04:00
|
|
|
, m_mainWidget(new QWidget(this))
|
|
|
|
, m_notesWidget(new QWidget(this))
|
2012-04-14 09:43:41 -04:00
|
|
|
, m_advancedWidget(new QWidget(this))
|
2010-10-06 13:40:50 -04:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
|
2010-10-06 16:54:07 -04:00
|
|
|
QFont headerLabelFont = m_ui->headerLabel->font();
|
|
|
|
headerLabelFont.setBold(true);
|
|
|
|
headerLabelFont.setPointSize(headerLabelFont.pointSize() + 2);
|
|
|
|
m_ui->headerLabel->setFont(headerLabelFont);
|
|
|
|
|
2010-10-06 13:40:50 -04:00
|
|
|
m_ui->categoryList->addItem(tr("Entry"));
|
|
|
|
m_ui->categoryList->addItem(tr("Description"));
|
2012-04-14 09:43:41 -04:00
|
|
|
m_ui->categoryList->addItem(tr("Advanced"));
|
2010-10-06 13:40:50 -04:00
|
|
|
|
|
|
|
m_mainUi->setupUi(m_mainWidget);
|
|
|
|
m_ui->stackedWidget->addWidget(m_mainWidget);
|
|
|
|
|
|
|
|
m_notesUi->setupUi(m_notesWidget);
|
|
|
|
m_ui->stackedWidget->addWidget(m_notesWidget);
|
|
|
|
|
2012-04-14 09:43:41 -04:00
|
|
|
m_advancedUi->setupUi(m_advancedWidget);
|
|
|
|
m_ui->stackedWidget->addWidget(m_advancedWidget);
|
|
|
|
|
|
|
|
m_attachmentsModel = new EntryAttachmentsModel(m_advancedWidget);
|
|
|
|
m_advancedUi->attachmentsView->setModel(m_attachmentsModel);
|
|
|
|
m_entryAttributes = new EntryAttributes(this);
|
|
|
|
|
|
|
|
m_attributesModel = new EntryAttributesModel(m_advancedWidget);
|
|
|
|
m_advancedUi->attributesView->setModel(m_attributesModel);
|
2012-04-14 12:45:32 -04:00
|
|
|
m_entryAttachments = new EntryAttachments(this);
|
2012-04-14 09:43:41 -04:00
|
|
|
|
2010-10-06 13:40:50 -04:00
|
|
|
Q_ASSERT(m_ui->categoryList->model()->rowCount() == m_ui->stackedWidget->count());
|
|
|
|
|
|
|
|
connect(m_ui->categoryList, SIGNAL(currentRowChanged(int)), m_ui->stackedWidget, SLOT(setCurrentIndex(int)));
|
2012-01-07 10:18:21 -05:00
|
|
|
|
|
|
|
connect(m_mainUi->togglePasswordButton, SIGNAL(toggled(bool)), SLOT(togglePassword(bool)));
|
|
|
|
connect(m_mainUi->expireCheck, SIGNAL(toggled(bool)), m_mainUi->expireDatePicker, SLOT(setEnabled(bool)));
|
2012-04-17 10:04:49 -04:00
|
|
|
connect(m_mainUi->passwordEdit, SIGNAL(textEdited(QString)), SLOT(setPasswordCheckColors()));
|
|
|
|
connect(m_mainUi->passwordRepeatEdit, SIGNAL(textEdited(QString)), SLOT(setPasswordCheckColors()));
|
2012-01-07 10:18:21 -05:00
|
|
|
|
2010-10-06 13:40:50 -04:00
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(saveEntry()));
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(cancel()));
|
|
|
|
}
|
|
|
|
|
|
|
|
EditEntryWidget::~EditEntryWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-17 10:04:49 -04:00
|
|
|
const QColor EditEntryWidget::normalColor = Qt::white;
|
|
|
|
const QColor EditEntryWidget::correctSoFarColor = QColor(255, 205, 15);
|
|
|
|
const QColor EditEntryWidget::errorColor = QColor(255, 125, 125);
|
|
|
|
|
2011-12-27 10:04:59 -05:00
|
|
|
void EditEntryWidget::loadEntry(Entry* entry, bool create, const QString& groupName)
|
2010-10-06 13:40:50 -04:00
|
|
|
{
|
|
|
|
m_entry = entry;
|
|
|
|
|
2011-12-27 10:04:59 -05:00
|
|
|
if (create) {
|
|
|
|
m_ui->headerLabel->setText(groupName+" > "+tr("Add entry"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_ui->headerLabel->setText(groupName+" > "+tr("Edit entry"));
|
|
|
|
}
|
2010-10-06 16:54:07 -04:00
|
|
|
|
2010-10-06 13:40:50 -04:00
|
|
|
m_mainUi->titleEdit->setText(entry->title());
|
|
|
|
m_mainUi->usernameEdit->setText(entry->username());
|
|
|
|
m_mainUi->urlEdit->setText(entry->url());
|
2012-01-07 10:18:21 -05:00
|
|
|
m_mainUi->passwordEdit->setText(entry->password());
|
|
|
|
m_mainUi->passwordRepeatEdit->setText(entry->password());
|
2012-04-17 10:04:49 -04:00
|
|
|
setPasswordCheckColors();
|
2012-01-07 10:18:21 -05:00
|
|
|
m_mainUi->expireCheck->setChecked(entry->timeInfo().expires());
|
2012-04-18 09:29:04 -04:00
|
|
|
m_mainUi->expireDatePicker->setDateTime(entry->timeInfo().expiryTime().toLocalTime());
|
2012-01-07 10:18:21 -05:00
|
|
|
m_mainUi->togglePasswordButton->setChecked(true);
|
2010-10-06 13:40:50 -04:00
|
|
|
|
|
|
|
m_notesUi->notesEdit->setPlainText(entry->notes());
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
m_entryAttributes->copyCustomKeysFrom(entry->attributes());
|
2012-04-14 09:43:41 -04:00
|
|
|
m_attributesModel->setEntryAttributes(m_entryAttributes);
|
|
|
|
m_entryAttachments->copyFrom(entry->attachments());
|
|
|
|
m_attachmentsModel->setEntryAttachments(m_entryAttachments);
|
|
|
|
|
2010-10-06 13:40:50 -04:00
|
|
|
m_ui->categoryList->setCurrentRow(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditEntryWidget::saveEntry()
|
|
|
|
{
|
2012-04-17 10:04:49 -04:00
|
|
|
if (!passwordsEqual()) {
|
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Different passwords supplied."));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-18 09:23:46 -04:00
|
|
|
m_entry->setTitle(m_mainUi->titleEdit->text());
|
|
|
|
m_entry->setUsername(m_mainUi->usernameEdit->text());
|
|
|
|
m_entry->setUrl(m_mainUi->urlEdit->text());
|
2012-01-07 10:18:21 -05:00
|
|
|
m_entry->setPassword(m_mainUi->passwordEdit->text());
|
2012-04-16 20:27:38 -04:00
|
|
|
m_entry->setExpires(m_mainUi->expireCheck->isChecked());
|
2012-04-18 09:29:04 -04:00
|
|
|
m_entry->setExpiryTime(m_mainUi->expireDatePicker->dateTime().toUTC());
|
2010-10-06 13:40:50 -04:00
|
|
|
|
|
|
|
m_entry->setNotes(m_notesUi->notesEdit->toPlainText());
|
|
|
|
|
2012-04-14 13:38:45 -04:00
|
|
|
m_entry->attributes()->copyCustomKeysFrom(m_entryAttributes);
|
2012-04-14 09:43:41 -04:00
|
|
|
m_entryAttributes->clear();
|
|
|
|
m_entry->attachments()->copyFrom(m_entryAttachments);
|
|
|
|
m_entryAttachments->clear();
|
|
|
|
|
2011-12-27 09:49:06 -05:00
|
|
|
m_entry = 0;
|
|
|
|
Q_EMIT editFinished(true);
|
2010-10-06 13:40:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditEntryWidget::cancel()
|
|
|
|
{
|
|
|
|
m_entry = 0;
|
2011-12-27 09:49:06 -05:00
|
|
|
Q_EMIT editFinished(false);
|
2010-10-06 13:40:50 -04:00
|
|
|
}
|
2012-01-07 10:18:21 -05:00
|
|
|
|
|
|
|
void EditEntryWidget::togglePassword(bool checked)
|
|
|
|
{
|
|
|
|
m_mainUi->passwordEdit->setEchoMode(checked ? QLineEdit::Password : QLineEdit::Normal);
|
|
|
|
m_mainUi->passwordRepeatEdit->setEchoMode(checked ? QLineEdit::Password : QLineEdit::Normal);
|
|
|
|
}
|
2012-04-17 10:04:49 -04:00
|
|
|
|
|
|
|
bool EditEntryWidget::passwordsEqual()
|
|
|
|
{
|
|
|
|
return m_mainUi->passwordEdit->text() == m_mainUi->passwordRepeatEdit->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditEntryWidget::setPasswordCheckColors()
|
|
|
|
{
|
|
|
|
QPalette pal;
|
|
|
|
if (passwordsEqual()) {
|
|
|
|
pal.setColor(QPalette::Base, normalColor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (m_mainUi->passwordEdit->text().startsWith(m_mainUi->passwordRepeatEdit->text())) {
|
|
|
|
pal.setColor(QPalette::Base, correctSoFarColor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pal.setColor(QPalette::Base, errorColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_mainUi->passwordRepeatEdit->setPalette(pal);
|
|
|
|
}
|