/* * Copyright (C) 2010 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 "DatabaseWidget.h" #include "ui_SearchWidget.h" #include #include #include #include #include #include #include #include "core/DataPath.h" #include "core/Metadata.h" #include "core/Tools.h" #include "gui/ChangeMasterKeyWidget.h" #include "gui/DatabaseSettingsWidget.h" #include "gui/entry/EditEntryWidget.h" #include "gui/entry/EntryView.h" #include "gui/group/EditGroupWidget.h" #include "gui/group/GroupView.h" DatabaseWidget::DatabaseWidget(Database* db, QWidget* parent) : QStackedWidget(parent) , m_db(db) , m_searchUi(new Ui::SearchWidget()) , m_searchWidget(new QWidget()) , m_newGroup(0) , m_newEntry(0) , m_newParent(0) { m_searchUi->setupUi(m_searchWidget); m_searchTimer = new QTimer(this); m_searchTimer->setSingleShot(true); m_mainWidget = new QWidget(this); QLayout* layout = new QHBoxLayout(m_mainWidget); QSplitter* splitter = new QSplitter(m_mainWidget); QWidget* rightHandSideWidget = new QWidget(splitter); m_searchWidget->setParent(rightHandSideWidget); m_groupView = new GroupView(db, splitter); m_groupView->setObjectName("groupView"); m_entryView = new EntryView(rightHandSideWidget); m_entryView->setObjectName("entryView"); QSizePolicy policy; policy = m_groupView->sizePolicy(); policy.setHorizontalStretch(30); m_groupView->setSizePolicy(policy); policy = rightHandSideWidget->sizePolicy(); policy.setHorizontalStretch(70); rightHandSideWidget->setSizePolicy(policy); QAction* closeAction = new QAction(m_searchWidget); QIcon closeIcon = dataPath()->icon("action", "dialog-close"); closeAction->setIcon(closeIcon); m_searchUi->closeSearchButton->setDefaultAction(closeAction); m_searchWidget->hide(); QVBoxLayout* vLayout = new QVBoxLayout(rightHandSideWidget); vLayout->addWidget(m_searchWidget); vLayout->addWidget(m_entryView); rightHandSideWidget->setLayout(vLayout); splitter->addWidget(m_groupView); splitter->addWidget(rightHandSideWidget); layout->addWidget(splitter); m_mainWidget->setLayout(layout); m_editEntryWidget = new EditEntryWidget(); m_editEntryWidget->setObjectName("editEntryWidget"); m_historyEditEntryWidget = new EditEntryWidget(); m_editGroupWidget = new EditGroupWidget(); m_editGroupWidget->setObjectName("editGroupWidget"); m_changeMasterKeyWidget = new ChangeMasterKeyWidget(); m_changeMasterKeyWidget->headlineLabel()->setText(tr("Change master key")); QFont headlineLabelFont = m_changeMasterKeyWidget->headlineLabel()->font(); headlineLabelFont.setBold(true); headlineLabelFont.setPointSize(headlineLabelFont.pointSize() + 2); m_changeMasterKeyWidget->headlineLabel()->setFont(headlineLabelFont); m_databaseSettingsWidget = new DatabaseSettingsWidget(); addWidget(m_mainWidget); addWidget(m_editEntryWidget); addWidget(m_editGroupWidget); addWidget(m_changeMasterKeyWidget); addWidget(m_databaseSettingsWidget); addWidget(m_historyEditEntryWidget); connect(m_groupView, SIGNAL(groupChanged(Group*)), m_entryView, SLOT(setGroup(Group*))); connect(m_groupView, SIGNAL(groupChanged(Group*)), this, SLOT(clearLastGroup(Group*))); connect(m_entryView, SIGNAL(entryActivated(Entry*)), SLOT(switchToEntryEdit(Entry*))); connect(m_editEntryWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); connect(m_editEntryWidget, SIGNAL(historyEntryActivated(Entry*)), SLOT(switchToHistoryView(Entry*))); connect(m_historyEditEntryWidget, SIGNAL(editFinished(bool)), SLOT(switchBackToEntryEdit())); connect(m_editGroupWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); connect(m_changeMasterKeyWidget, SIGNAL(editFinished(bool)), SLOT(updateMasterKey(bool))); connect(m_databaseSettingsWidget, SIGNAL(editFinished(bool)), SLOT(switchToView(bool))); connect(this, SIGNAL(currentChanged(int)), this, SLOT(emitCurrentModeChanged())); connect(m_searchUi->searchEdit, SIGNAL(textChanged(QString)), this, SLOT(startSearchTimer())); connect(m_searchTimer, SIGNAL(timeout()), this, SLOT(search())); connect(closeAction, SIGNAL(triggered()), this, SLOT(closeSearch())); setCurrentIndex(0); } DatabaseWidget::~DatabaseWidget() { } DatabaseWidget::Mode DatabaseWidget::currentMode() { switch (currentIndex()) { case -1: return DatabaseWidget::None; case 0: return DatabaseWidget::ViewMode; case 1: // entry edit case 2: // group edit case 3: // change master key case 4: // database settings case 5: // entry history return DatabaseWidget::EditMode; default: Q_ASSERT(false); return DatabaseWidget::None; } } void DatabaseWidget::emitCurrentModeChanged() { Q_EMIT currentModeChanged(currentMode()); } GroupView* DatabaseWidget::groupView() { return m_groupView; } EntryView* DatabaseWidget::entryView() { return m_entryView; } void DatabaseWidget::createEntry() { m_newEntry = new Entry(); m_newEntry->setUuid(Uuid::random()); m_newEntry->setUsername(m_db->metadata()->defaultUserName()); m_newParent = m_groupView->currentGroup(); switchToEntryEdit(m_newEntry, true); } void DatabaseWidget::cloneEntry() { Entry* currentEntry = m_entryView->currentEntry(); Entry* entry = currentEntry->clone(); entry->setUuid(Uuid::random()); entry->setGroup(currentEntry->group()); m_entryView->setFocus(); m_entryView->setCurrentEntry(entry); } void DatabaseWidget::deleteEntry() { bool inRecylceBin = Tools::hasChild(m_db->metadata()->recycleBin(), m_entryView->currentEntry()); if (inRecylceBin || !m_db->metadata()->recycleBinEnabled()) { QMessageBox::StandardButton result = QMessageBox::question( this, tr("Delete entry?"), tr("Do you really want to delete the entry \"%1\" for good?") .arg(m_entryView->currentEntry()->title()), QMessageBox::Yes | QMessageBox::No); if (result == QMessageBox::Yes) { delete m_entryView->currentEntry(); } } else { m_db->recycleEntry(m_entryView->currentEntry()); } } void DatabaseWidget::createGroup() { m_newGroup = new Group(); m_newGroup->setUuid(Uuid::random()); m_newParent = m_groupView->currentGroup(); switchToGroupEdit(m_newGroup, true); } void DatabaseWidget::deleteGroup() { Q_ASSERT(canDeleteCurrentGoup()); bool inRecylceBin = Tools::hasChild(m_db->metadata()->recycleBin(), m_groupView->currentGroup()); if (inRecylceBin || !m_db->metadata()->recycleBinEnabled()) { QMessageBox::StandardButton result = QMessageBox::question( this, tr("Delete group?"), tr("Do you really want to delete the group \"%1\" for good?") .arg(m_groupView->currentGroup()->name()), QMessageBox::Yes | QMessageBox::No); if (result == QMessageBox::Yes) { delete m_groupView->currentGroup(); } } else { m_db->recycleGroup(m_groupView->currentGroup()); } } int DatabaseWidget::addWidget(QWidget* w) { w->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); int index = QStackedWidget::addWidget(w); adjustSize(); return index; } void DatabaseWidget::setCurrentIndex(int index) { if (currentWidget()) { currentWidget()->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); } QStackedWidget::setCurrentIndex(index); if (currentWidget()) { currentWidget()->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); } adjustSize(); } void DatabaseWidget::switchToView(bool accepted) { if (m_newGroup) { if (accepted) { m_newGroup->setParent(m_newParent); m_groupView->setCurrentGroup(m_newGroup); m_groupView->expandGroup(m_newParent); } else { delete m_newGroup; } m_newGroup = 0; m_newParent = 0; } else if (m_newEntry) { if (accepted) { m_newEntry->setGroup(m_newParent); m_entryView->setFocus(); m_entryView->setCurrentEntry(m_newEntry); } else { delete m_newEntry; } m_newEntry = 0; m_newParent = 0; } setCurrentIndex(0); } void DatabaseWidget::switchToHistoryView(Entry* entry) { m_historyEditEntryWidget->loadEntry(entry, false, true, "", m_db); setCurrentIndex(5); } void DatabaseWidget::switchBackToEntryEdit() { setCurrentIndex(1); } void DatabaseWidget::switchToEntryEdit(Entry* entry) { switchToEntryEdit(entry, false); } void DatabaseWidget::switchToEntryEdit(Entry* entry, bool create) { Group* group = m_groupView->currentGroup(); if (!group) { Q_ASSERT(m_entryView->inSearch()); group = m_lastGroup; } Q_ASSERT(group); m_editEntryWidget->loadEntry(entry, create, false, group->name(), m_db); setCurrentIndex(1); } void DatabaseWidget::switchToGroupEdit(Group* group, bool create) { m_editGroupWidget->loadGroup(group, create, m_db); setCurrentIndex(2); } void DatabaseWidget::updateMasterKey(bool accepted) { if (accepted) { m_db->setKey(m_changeMasterKeyWidget->newMasterKey()); } else if (!m_db->hasKey()) { Q_EMIT closeRequest(); return; } setCurrentIndex(0); } void DatabaseWidget::switchToEntryEdit() { switchToEntryEdit(m_entryView->currentEntry(), false); } void DatabaseWidget::switchToGroupEdit() { switchToGroupEdit(m_groupView->currentGroup(), false); } void DatabaseWidget::switchToMasterKeyChange() { m_changeMasterKeyWidget->clearForms(); setCurrentIndex(3); } void DatabaseWidget::switchToDatabaseSettings() { m_databaseSettingsWidget->load(m_db); setCurrentIndex(4); } void DatabaseWidget::toggleSearch() { if (m_entryView->inSearch()) { closeSearch(); } else { showSearch(); } } void DatabaseWidget::closeSearch() { Q_ASSERT(m_lastGroup); m_groupView->setCurrentGroup(m_lastGroup); } void DatabaseWidget::showSearch() { m_searchWidget->show(); m_searchUi->searchEdit->blockSignals(true); m_searchUi->searchEdit->clear(); m_searchUi->searchEdit->blockSignals(false); search(); m_searchUi->searchEdit->setFocus(); } void DatabaseWidget::search() { Group* searchGroup = m_db->rootGroup(); QList searchResult = searchGroup->search(m_searchUi->searchEdit->text(), Qt::CaseInsensitive); Group* group = m_groupView->currentGroup(); if (group) { m_lastGroup = m_groupView->currentGroup(); m_groupView->setCurrentIndex(QModelIndex()); } m_entryView->search(searchResult); } void DatabaseWidget::startSearchTimer() { if (!m_searchTimer->isActive()) { m_searchTimer->stop(); } m_searchTimer->start(100); } bool DatabaseWidget::dbHasKey() { return m_db->hasKey(); } bool DatabaseWidget::canDeleteCurrentGoup() { bool isRootGroup = m_db->rootGroup() == m_groupView->currentGroup(); bool isRecycleBin = m_db->metadata()->recycleBin() == m_groupView->currentGroup(); return !isRootGroup && !isRecycleBin; } void DatabaseWidget::truncateHistories() { QList allEntries = m_db->rootGroup()->entriesRecursive(false); Q_FOREACH (Entry* entry, allEntries) { entry->truncateHistory(); } } void DatabaseWidget::clearLastGroup(Group* group) { if (group) { m_lastGroup = 0; m_searchWidget->hide(); } }