From de1a94ff614e8d618ba26a6410611606ab3a5cbb Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Wed, 15 Aug 2012 22:47:41 +0200 Subject: [PATCH] Add presets for expire date. Closes #44 --- src/CMakeLists.txt | 1 + src/core/TimeDelta.cpp | 70 ++++++++++++++++++++++++++++ src/core/TimeDelta.h | 50 ++++++++++++++++++++ src/gui/entry/EditEntryWidget.cpp | 32 +++++++++++++ src/gui/entry/EditEntryWidget.h | 5 ++ src/gui/entry/EditEntryWidgetMain.ui | 32 +++++++++---- 6 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 src/core/TimeDelta.cpp create mode 100644 src/core/TimeDelta.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cb021b329..d45464943 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,6 +39,7 @@ set(keepassx_SOURCES core/Metadata.cpp core/qsavefile.cpp core/SignalMultiplexer.cpp + core/TimeDelta.cpp core/TimeInfo.cpp core/Tools.cpp core/Uuid.cpp diff --git a/src/core/TimeDelta.cpp b/src/core/TimeDelta.cpp new file mode 100644 index 000000000..989d744b8 --- /dev/null +++ b/src/core/TimeDelta.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 . + */ + +#include "TimeDelta.h" + +#include + +QDateTime operator+(const QDateTime& dateTime, const TimeDelta& delta) { + return dateTime.addDays(delta.getDays()) + .addMonths(delta.getMonths()) + .addYears(delta.getYears()); +} + +TimeDelta TimeDelta::fromDays(int days) +{ + return TimeDelta(days, 0, 0); +} + +TimeDelta TimeDelta::fromMonths(int months) +{ + return TimeDelta(0, months, 0); +} + +TimeDelta TimeDelta::fromYears(int years) +{ + return TimeDelta(0, 0, years); +} + +TimeDelta::TimeDelta() + : m_days(0) + , m_months(0) + , m_years(0) +{ +} + +TimeDelta::TimeDelta(int days, int months, int years) + : m_days(days) + , m_months(months) + , m_years(years) +{ +} + +int TimeDelta::getDays() const +{ + return m_days; +} + +int TimeDelta::getMonths() const +{ + return m_months; +} + +int TimeDelta::getYears() const +{ + return m_years; +} diff --git a/src/core/TimeDelta.h b/src/core/TimeDelta.h new file mode 100644 index 000000000..c8fa9fd0c --- /dev/null +++ b/src/core/TimeDelta.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2012 Felix Geyer + * + * 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 . + */ + +#ifndef KEEPASSX_TIMEDELTA_H +#define KEEPASSX_TIMEDELTA_H + +#include + +class QDateTime; +class TimeDelta; + +QDateTime operator+(const QDateTime& dateTime, const TimeDelta& delta); + +class TimeDelta +{ +public: + static TimeDelta fromDays(int days); + static TimeDelta fromMonths(int months); + static TimeDelta fromYears(int years); + + TimeDelta(); + TimeDelta(int days, int months, int years); + + int getDays() const; + int getMonths() const; + int getYears() const; + +private: + int m_days; + int m_months; + int m_years; +}; + +Q_DECLARE_METATYPE(TimeDelta) + +#endif // KEEPASSX_TIMEDELTA_H diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 48f2f4fc1..77bc95e9f 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -26,12 +26,14 @@ #include #include +#include #include #include #include "core/Database.h" #include "core/Entry.h" #include "core/Metadata.h" +#include "core/TimeDelta.h" #include "core/Tools.h" #include "gui/EditWidgetIcons.h" #include "gui/FileDialog.h" @@ -150,6 +152,9 @@ EditEntryWidget::EditEntryWidget(QWidget* parent) connect(m_historyUi->deleteButton, SIGNAL(clicked()), SLOT(deleteHistoryEntry())); connect(m_historyUi->deleteAllButton, SIGNAL(clicked()), SLOT(deleteAllHistoryEntries())); + m_mainUi->expirePresets->setMenu(createPresetsMenu()); + connect(m_mainUi->expirePresets->menu(), SIGNAL(triggered(QAction*)), this, SLOT(useExpiryPreset(QAction*))); + connect(this, SIGNAL(accepted()), SLOT(saveEntry())); connect(this, SIGNAL(rejected()), SLOT(cancel())); } @@ -195,6 +200,15 @@ void EditEntryWidget::updateHistoryButtons(const QModelIndex& current, const QMo } } +void EditEntryWidget::useExpiryPreset(QAction* action) +{ + m_mainUi->expireCheck->setChecked(true); + TimeDelta delta = action->data().value(); + QDateTime now = Tools::currentDateTimeUtc().toLocalTime(); + QDateTime expiryDateTime = now + delta; + m_mainUi->expireDatePicker->setDateTime(expiryDateTime); +} + void EditEntryWidget::loadEntry(Entry* entry, bool create, bool history, const QString& groupName, Database* database) { @@ -259,6 +273,7 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) setPasswordCheckColors(); m_mainUi->expireCheck->setChecked(entry->timeInfo().expires()); m_mainUi->expireDatePicker->setDateTime(entry->timeInfo().expiryTime().toLocalTime()); + m_mainUi->expirePresets->setEnabled(!m_history); m_mainUi->togglePasswordButton->setChecked(true); m_notesUi->notesEdit->setPlainText(entry->notes()); @@ -722,3 +737,20 @@ void EditEntryWidget::deleteAllHistoryEntries() m_historyUi->deleteAllButton->setEnabled(false); } } + +QMenu* EditEntryWidget::createPresetsMenu() +{ + QMenu* expirePresetsMenu = new QMenu(); + expirePresetsMenu->addAction(tr("Tomorrow"))->setData(QVariant::fromValue(TimeDelta::fromDays(1))); + expirePresetsMenu->addSeparator(); + expirePresetsMenu->addAction(tr("1 week"))->setData(QVariant::fromValue(TimeDelta::fromDays(7))); + expirePresetsMenu->addAction(tr("2 weeks"))->setData(QVariant::fromValue(TimeDelta::fromDays(14))); + expirePresetsMenu->addAction(tr("3 weeks"))->setData(QVariant::fromValue(TimeDelta::fromDays(21))); + expirePresetsMenu->addSeparator(); + expirePresetsMenu->addAction(tr("1 month"))->setData(QVariant::fromValue(TimeDelta::fromMonths(1))); + expirePresetsMenu->addAction(tr("3 months"))->setData(QVariant::fromValue(TimeDelta::fromMonths(3))); + expirePresetsMenu->addAction(tr("6 months"))->setData(QVariant::fromValue(TimeDelta::fromMonths(6))); + expirePresetsMenu->addSeparator(); + expirePresetsMenu->addAction(tr("1 year"))->setData(QVariant::fromValue(TimeDelta::fromYears(1))); + return expirePresetsMenu; +} diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index 2b5340543..9507c3b3e 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -34,6 +34,7 @@ class EntryAttributes; class EntryAttributesModel; class EntryHistoryModel; class QButtonGroup; +class QMenu; class QSortFilterProxyModel; class QStackedLayout; @@ -60,6 +61,7 @@ public: static const QColor CorrectSoFarColor; static const QColor ErrorColor; + void createPresetsMenu(QMenu* expirePresetsMenu); Q_SIGNALS: void editFinished(bool accepted); void historyEntryActivated(Entry* entry); @@ -89,10 +91,12 @@ private Q_SLOTS: void emitHistoryEntryActivated(const QModelIndex& index); void histEntryActivated(const QModelIndex& index); void updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous); + void useExpiryPreset(QAction* action); private: bool passwordsEqual(); void setForms(const Entry* entry, bool restore = false); + QMenu *createPresetsMenu(); Entry* m_entry; Database* m_database; @@ -121,6 +125,7 @@ private: AutoTypeAssociationsModel* const m_autoTypeAssocModel; QButtonGroup* const m_autoTypeDefaultSequenceGroup; QButtonGroup* const m_autoTypeWindowSequenceGroup; + QMenu* m_expirePresetsMenu; Q_DISABLE_COPY(EditEntryWidget) }; diff --git a/src/gui/entry/EditEntryWidgetMain.ui b/src/gui/entry/EditEntryWidgetMain.ui index b84f2d262..1e3c135e3 100644 --- a/src/gui/entry/EditEntryWidgetMain.ui +++ b/src/gui/entry/EditEntryWidgetMain.ui @@ -89,6 +89,27 @@ + + + + + + false + + + true + + + + + + + Presets + + + + + @@ -96,16 +117,6 @@ - - - - false - - - true - - - @@ -117,6 +128,7 @@ togglePasswordButton expireCheck expireDatePicker + expirePresets