2014-05-17 09:16:27 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
|
|
|
|
|
* Copyright (C) 2014 Florian Geyer <blueice@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 "DatabaseWidgetStateSync.h"
|
|
|
|
|
|
|
|
|
|
#include "core/Config.h"
|
|
|
|
|
|
|
|
|
|
DatabaseWidgetStateSync::DatabaseWidgetStateSync(QObject* parent)
|
|
|
|
|
: QObject(parent)
|
2015-07-24 16:28:12 +00:00
|
|
|
, m_activeDbWidget(nullptr)
|
2014-05-17 16:05:02 +00:00
|
|
|
, m_blockUpdates(false)
|
2014-05-17 09:16:27 +00:00
|
|
|
{
|
2017-10-28 18:14:45 +00:00
|
|
|
m_mainSplitterSizes = variantToIntList(config()->get("GUI/SplitterState"));
|
|
|
|
|
m_detailSplitterSizes = variantToIntList(config()->get("GUI/DetailSplitterState"));
|
2017-12-23 07:40:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Load state of entry view 'Hide Usernames'/'Hide Passwords' settings
|
|
|
|
|
*/
|
|
|
|
|
m_entryHideUsernames = config()->get("GUI/EntryHideUsernames").toBool();
|
|
|
|
|
m_entryHidePasswords = config()->get("GUI/EntryHidePasswords").toBool();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Load states of entry view list/search view
|
|
|
|
|
*/
|
|
|
|
|
m_entryListViewState = config()->get("GUI/EntryListViewState").toByteArray();
|
|
|
|
|
m_entrySearchViewState = config()->get("GUI/EntrySearchViewState").toByteArray();
|
2014-05-17 09:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DatabaseWidgetStateSync::~DatabaseWidgetStateSync()
|
|
|
|
|
{
|
2017-10-28 18:14:45 +00:00
|
|
|
config()->set("GUI/SplitterState", intListToVariant(m_mainSplitterSizes));
|
|
|
|
|
config()->set("GUI/DetailSplitterState", intListToVariant(m_detailSplitterSizes));
|
2017-12-23 07:40:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Save state of entry view 'Hide Usernames'/'Hide Passwords' settings
|
|
|
|
|
*/
|
|
|
|
|
config()->set("GUI/EntryHideUsernames", m_entryHideUsernames);
|
|
|
|
|
config()->set("GUI/EntryHidePasswords", m_entryHidePasswords);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Save states of entry view list/search view
|
|
|
|
|
*/
|
|
|
|
|
config()->set("GUI/EntryListViewState", m_entryListViewState);
|
|
|
|
|
config()->set("GUI/EntrySearchViewState", m_entrySearchViewState);
|
2014-05-17 09:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget)
|
|
|
|
|
{
|
|
|
|
|
if (m_activeDbWidget) {
|
|
|
|
|
disconnect(m_activeDbWidget, 0, this, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_activeDbWidget = dbWidget;
|
|
|
|
|
|
|
|
|
|
if (m_activeDbWidget) {
|
2014-05-17 16:05:02 +00:00
|
|
|
m_blockUpdates = true;
|
|
|
|
|
|
2017-11-02 18:47:54 +00:00
|
|
|
if (!m_mainSplitterSizes.isEmpty()) {
|
2017-10-28 18:14:45 +00:00
|
|
|
m_activeDbWidget->setMainSplitterSizes(m_mainSplitterSizes);
|
2017-11-02 18:47:54 +00:00
|
|
|
}
|
2017-10-28 18:14:45 +00:00
|
|
|
|
2017-11-02 18:47:54 +00:00
|
|
|
if (!m_detailSplitterSizes.isEmpty()) {
|
2017-10-28 18:14:45 +00:00
|
|
|
m_activeDbWidget->setDetailSplitterSizes(m_detailSplitterSizes);
|
2017-11-02 18:47:54 +00:00
|
|
|
}
|
2014-05-17 09:16:27 +00:00
|
|
|
|
2017-11-02 18:47:54 +00:00
|
|
|
if (m_activeDbWidget->isInSearchMode()) {
|
2014-05-17 10:51:16 +00:00
|
|
|
restoreSearchView();
|
2017-11-02 18:47:54 +00:00
|
|
|
} else {
|
2016-11-03 01:01:02 +00:00
|
|
|
restoreListView();
|
2017-11-02 18:47:54 +00:00
|
|
|
}
|
2014-05-17 10:51:16 +00:00
|
|
|
|
2014-05-17 16:05:02 +00:00
|
|
|
m_blockUpdates = false;
|
|
|
|
|
|
2017-10-28 18:14:45 +00:00
|
|
|
connect(m_activeDbWidget, SIGNAL(mainSplitterSizesChanged()),
|
|
|
|
|
SLOT(updateSplitterSizes()));
|
|
|
|
|
connect(m_activeDbWidget, SIGNAL(detailSplitterSizesChanged()),
|
2014-05-17 09:16:27 +00:00
|
|
|
SLOT(updateSplitterSizes()));
|
2017-12-23 07:40:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Connect signal to receive state changes of entry view view
|
|
|
|
|
*/
|
|
|
|
|
connect(m_activeDbWidget, SIGNAL(entryViewStateChanged()),
|
|
|
|
|
SLOT(updateViewState()));
|
|
|
|
|
|
2014-05-17 10:51:16 +00:00
|
|
|
connect(m_activeDbWidget, SIGNAL(listModeActivated()),
|
|
|
|
|
SLOT(restoreListView()));
|
|
|
|
|
connect(m_activeDbWidget, SIGNAL(searchModeActivated()),
|
|
|
|
|
SLOT(restoreSearchView()));
|
2014-05-17 16:05:02 +00:00
|
|
|
connect(m_activeDbWidget, SIGNAL(listModeAboutToActivate()),
|
|
|
|
|
SLOT(blockUpdates()));
|
|
|
|
|
connect(m_activeDbWidget, SIGNAL(searchModeAboutToActivate()),
|
|
|
|
|
SLOT(blockUpdates()));
|
2014-05-17 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 07:40:00 +00:00
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Restore entry view list view state
|
|
|
|
|
*
|
|
|
|
|
* NOTE:
|
|
|
|
|
* States of entry view 'Hide Usernames'/'Hide Passwords' settings are considered
|
|
|
|
|
* 'global', i.e. they are the same for both list and search mode
|
|
|
|
|
*
|
|
|
|
|
* NOTE:
|
|
|
|
|
* If m_entryListViewState is empty, it is the first time after clean/invalid
|
|
|
|
|
* config that list view is activated. Thus, save its current state. Without
|
|
|
|
|
* this, m_entryListViewState would remain empty until there is an actual view
|
|
|
|
|
* state change (e.g. column is resized)
|
|
|
|
|
*/
|
2014-05-17 10:51:16 +00:00
|
|
|
void DatabaseWidgetStateSync::restoreListView()
|
|
|
|
|
{
|
2017-12-23 07:40:00 +00:00
|
|
|
m_activeDbWidget->setEntryViewHideUsernames(m_entryHideUsernames);
|
|
|
|
|
m_activeDbWidget->setEntryViewHidePasswords(m_entryHidePasswords);
|
|
|
|
|
|
|
|
|
|
if (!m_entryListViewState.isEmpty()) {
|
|
|
|
|
m_activeDbWidget->setEntryViewViewState(m_entryListViewState);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
m_entryListViewState = m_activeDbWidget->entryViewViewState();
|
2014-05-17 10:51:16 +00:00
|
|
|
}
|
2014-05-17 16:05:02 +00:00
|
|
|
|
|
|
|
|
m_blockUpdates = false;
|
2014-05-17 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-23 07:40:00 +00:00
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Restore entry view search view state
|
|
|
|
|
*
|
|
|
|
|
* NOTE:
|
|
|
|
|
* States of entry view 'Hide Usernames'/'Hide Passwords' settings are considered
|
|
|
|
|
* 'global', i.e. they are the same for both list and search mode
|
|
|
|
|
*
|
|
|
|
|
* NOTE:
|
|
|
|
|
* If m_entrySearchViewState is empty, it is the first time after clean/invalid
|
|
|
|
|
* config that search view is activated. Thus, save its current state. Without
|
|
|
|
|
* this, m_entrySearchViewState would remain empty until there is an actual view
|
|
|
|
|
* state change (e.g. column is resized)
|
|
|
|
|
*/
|
2014-05-17 10:51:16 +00:00
|
|
|
void DatabaseWidgetStateSync::restoreSearchView()
|
|
|
|
|
{
|
2017-12-23 07:40:00 +00:00
|
|
|
m_activeDbWidget->setEntryViewHideUsernames(m_entryHideUsernames);
|
|
|
|
|
m_activeDbWidget->setEntryViewHidePasswords(m_entryHidePasswords);
|
|
|
|
|
|
|
|
|
|
if (!m_entrySearchViewState.isEmpty()) {
|
|
|
|
|
m_activeDbWidget->setEntryViewViewState(m_entrySearchViewState);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
m_entrySearchViewState = m_activeDbWidget->entryViewViewState();
|
2014-05-17 09:16:27 +00:00
|
|
|
}
|
2014-05-17 16:05:02 +00:00
|
|
|
|
|
|
|
|
m_blockUpdates = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatabaseWidgetStateSync::blockUpdates()
|
|
|
|
|
{
|
|
|
|
|
m_blockUpdates = true;
|
2014-05-17 09:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DatabaseWidgetStateSync::updateSplitterSizes()
|
|
|
|
|
{
|
2014-05-17 16:05:02 +00:00
|
|
|
if (m_blockUpdates) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-28 18:14:45 +00:00
|
|
|
m_mainSplitterSizes = m_activeDbWidget->mainSplitterSizes();
|
|
|
|
|
m_detailSplitterSizes = m_activeDbWidget->detailSplitterSizes();
|
2014-05-17 09:16:27 +00:00
|
|
|
}
|
2014-05-17 10:51:16 +00:00
|
|
|
|
2017-12-23 07:40:00 +00:00
|
|
|
/**
|
|
|
|
|
* @author Fonic <https://github.com/fonic>
|
|
|
|
|
* Update entry view list/search view state (NOTE: states of entry view
|
|
|
|
|
* 'Hide Usernames'/'Hide Passwords' settings are considered 'global',
|
|
|
|
|
* i.e. they are the same for both list and search mode)
|
|
|
|
|
*/
|
|
|
|
|
void DatabaseWidgetStateSync::updateViewState()
|
2014-05-17 10:51:16 +00:00
|
|
|
{
|
2014-05-17 16:05:02 +00:00
|
|
|
if (m_blockUpdates) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 07:40:00 +00:00
|
|
|
m_entryHideUsernames = m_activeDbWidget->entryViewHideUsernames();
|
|
|
|
|
m_entryHidePasswords = m_activeDbWidget->entryViewHidePasswords();
|
|
|
|
|
|
|
|
|
|
if (m_activeDbWidget->isInSearchMode()) {
|
|
|
|
|
m_entrySearchViewState = m_activeDbWidget->entryViewViewState();
|
2014-05-17 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2017-12-23 07:40:00 +00:00
|
|
|
m_entryListViewState = m_activeDbWidget->entryViewViewState();
|
2014-05-17 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<int> DatabaseWidgetStateSync::variantToIntList(const QVariant& variant)
|
|
|
|
|
{
|
2016-09-02 17:51:51 +00:00
|
|
|
const QVariantList list = variant.toList();
|
2014-05-17 10:51:16 +00:00
|
|
|
QList<int> result;
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
for (const QVariant& var : list) {
|
2014-05-17 10:51:16 +00:00
|
|
|
bool ok;
|
|
|
|
|
int size = var.toInt(&ok);
|
|
|
|
|
if (ok) {
|
|
|
|
|
result.append(size);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
result.clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant DatabaseWidgetStateSync::intListToVariant(const QList<int>& list)
|
|
|
|
|
{
|
|
|
|
|
QVariantList result;
|
|
|
|
|
|
2016-09-02 17:51:51 +00:00
|
|
|
for (int value : list) {
|
2014-05-17 10:51:16 +00:00
|
|
|
result.append(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-01-21 16:19:05 +00:00
|
|
|
|