keepassxc/src/gui/DatabaseTabWidget.cpp

468 lines
12 KiB
C++
Raw Normal View History

2011-11-13 08:55:20 -05:00
/*
* Copyright (C) 2011 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 "DatabaseTabWidget.h"
2011-11-13 08:55:20 -05:00
#include <QtCore/QFileInfo>
#include <QtGui/QTabWidget>
#include <QtGui/QMessageBox>
2011-11-13 08:55:20 -05:00
#include "core/Database.h"
#include "core/Group.h"
2011-11-13 08:55:20 -05:00
#include "core/Metadata.h"
#include "gui/DatabaseOpenDialog.h"
2011-11-13 08:55:20 -05:00
#include "gui/DatabaseWidget.h"
2011-12-29 13:01:58 -05:00
#include "gui/EntryView.h"
#include "gui/FileDialog.h"
#include "gui/GroupView.h"
#include "gui/KeePass1OpenDialog.h"
2011-11-13 08:55:20 -05:00
DatabaseManagerStruct::DatabaseManagerStruct()
: file(0)
, dbWidget(0)
, modified(false)
, readOnly(false)
{
}
DatabaseTabWidget::DatabaseTabWidget(QWidget* parent)
: QTabWidget(parent)
, m_window(parent->window())
2011-11-13 08:55:20 -05:00
{
connect(this, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int)));
connect(this, SIGNAL(currentChanged(int)), SLOT(emitEntrySelectionChanged()));
2011-11-13 08:55:20 -05:00
}
void DatabaseTabWidget::toggleTabbar() {
if (count() > 1) {
if (!tabBar()->isVisible()) {
tabBar()->show();
}
}
else {
if (tabBar()->isVisible()) {
tabBar()->hide();
}
}
}
void DatabaseTabWidget::newDatabase()
{
DatabaseManagerStruct dbStruct;
Database* db = new Database();
db->rootGroup()->setName(tr("Root"));
dbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, dbStruct);
dbStruct.dbWidget->switchToMasterKeyChange();
}
void DatabaseTabWidget::openDatabase()
2011-11-13 08:55:20 -05:00
{
QString fileName = fileDialog()->getOpenFileName(m_window, tr("Open database"), QString(),
tr("KeePass 2 Database").append(" (*.kdbx)"));
2011-11-13 08:55:20 -05:00
if (!fileName.isEmpty()) {
openDatabase(fileName);
}
}
void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw,
const QString& keyFile)
2011-11-13 08:55:20 -05:00
{
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
2011-11-13 08:55:20 -05:00
if (!file->open(QIODevice::ReadWrite)) {
if (!file->open(QIODevice::ReadOnly)) {
// can't open
return;
}
else {
// can only open read-only
m_curDbStruct.readOnly = true;
}
2011-11-13 08:55:20 -05:00
}
m_curDbStruct.file = file.take();
m_curDbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
2011-11-13 08:55:20 -05:00
openDatabaseDialog(pw, keyFile);
}
2011-11-13 08:55:20 -05:00
void DatabaseTabWidget::openDatabaseDialog(const QString& pw, const QString& keyFile)
{
m_curKeyDialog = new DatabaseOpenDialog(m_curDbStruct.file, m_curDbStruct.fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup()));
m_curKeyDialog->setModal(true);
m_curKeyDialog->show();
if (!pw.isNull() || !keyFile.isEmpty()) {
m_curKeyDialog->enterKey(pw, keyFile);
}
}
2011-11-13 08:55:20 -05:00
void DatabaseTabWidget::importKeePass1Database()
{
QString fileName = fileDialog()->getOpenFileName(this, tr("Open KeePass 1 database"), QString(),
tr("KeePass 1 database") + " (*.kdb);;" + tr("All files (*)"));
if (fileName.isEmpty()) {
return;
}
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
if (!file->open(QIODevice::ReadOnly)) {
return;
}
m_curDbStruct.modified = true;
m_curKeyDialog = new KeePass1OpenDialog(file.take(), fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup()));
m_curKeyDialog->setModal(true);
m_curKeyDialog->show();
}
void DatabaseTabWidget::openDatabaseRead()
{
Database* db = m_curKeyDialog->database();
delete m_curKeyDialog;
m_curKeyDialog = 0;
2011-11-13 08:55:20 -05:00
m_curDbStruct.dbWidget = new DatabaseWidget(db, this);
insertDatabase(db, m_curDbStruct);
m_curDbStruct = DatabaseManagerStruct();
}
void DatabaseTabWidget::openDatabaseCleanup()
{
delete m_curKeyDialog;
m_curKeyDialog = 0;
if (m_curDbStruct.file) {
delete m_curDbStruct.file;
}
m_curDbStruct = DatabaseManagerStruct();
2011-11-13 08:55:20 -05:00
}
void DatabaseTabWidget::emitEntrySelectionChanged()
2011-12-29 13:01:58 -05:00
{
DatabaseWidget* dbWidget = currentDatabaseWidget();
2011-12-29 13:01:58 -05:00
bool isSingleEntrySelected = false;
if (dbWidget) {
isSingleEntrySelected = dbWidget->entryView()->isSingleEntrySelected();
2011-12-29 13:01:58 -05:00
}
Q_EMIT entrySelectionChanged(isSingleEntrySelected);
}
bool DatabaseTabWidget::closeDatabase(Database* db)
2011-11-13 08:55:20 -05:00
{
Q_ASSERT(db);
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
int index = databaseIndex(db);
Q_ASSERT(index != -1);
QString dbName = tabText(index);
if (dbName.right(1) == "*") {
dbName.chop(1);
}
if (dbStruct.dbWidget->currentMode() == DatabaseWidget::EditMode && db->hasKey()) {
QMessageBox::StandardButton result =
QMessageBox::question(
this, tr("Close?"),
tr("\"%1\" is in edit mode.\nClose anyway?").arg(dbName),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (result == QMessageBox::No) {
return false;
}
}
if (dbStruct.modified) {
QMessageBox::StandardButton result =
QMessageBox::question(
this, tr("Save changes?"),
tr("\"%1\" was modified.\nSave changes?").arg(dbName),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes);
if (result == QMessageBox::Yes) {
saveDatabase(db);
}
else if (result == QMessageBox::Cancel) {
return false;
}
}
2011-11-13 08:55:20 -05:00
removeTab(index);
toggleTabbar();
m_dbList.remove(db);
delete dbStruct.file;
2011-11-13 08:55:20 -05:00
delete dbStruct.dbWidget;
delete db;
return true;
}
bool DatabaseTabWidget::closeAllDatabases() {
while (!m_dbList.isEmpty()) {
if (!closeDatabase()) {
return false;
}
}
return true;
2011-11-13 08:55:20 -05:00
}
void DatabaseTabWidget::saveDatabase(Database* db)
{
DatabaseManagerStruct& dbStruct = m_dbList[db];
// TODO: ensure that the data is actually written to disk
2011-12-24 13:22:42 -05:00
if (dbStruct.file) {
dbStruct.file->reset();
m_writer.writeDatabase(dbStruct.file, db);
dbStruct.file->resize(dbStruct.file->pos());
dbStruct.file->flush();
2011-12-24 13:22:42 -05:00
dbStruct.modified = false;
updateTabName(db);
}
else {
saveDatabaseAs(db);
}
}
void DatabaseTabWidget::saveDatabaseAs(Database* db)
2011-12-24 13:22:42 -05:00
{
DatabaseManagerStruct& dbStruct = m_dbList[db];
2011-12-28 13:05:31 -05:00
QString oldFileName;
if (dbStruct.file) {
oldFileName = dbStruct.fileName;
2011-12-28 13:05:31 -05:00
}
QString fileName = fileDialog()->getSaveFileName(m_window, tr("Save database as"),
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"));
2011-12-24 13:22:42 -05:00
if (!fileName.isEmpty()) {
QFile* oldFile = dbStruct.file;
2011-12-24 13:22:42 -05:00
QScopedPointer<QFile> file(new QFile(fileName));
// TODO: error handling
2011-12-24 13:22:42 -05:00
if (!file->open(QIODevice::ReadWrite)) {
return;
}
dbStruct.file = file.take();
// TODO: ensure that the data is actually written to disk
2011-12-24 13:22:42 -05:00
m_writer.writeDatabase(dbStruct.file, db);
dbStruct.file->flush();
delete oldFile;
2011-12-24 13:22:42 -05:00
dbStruct.modified = false;
dbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
2011-12-24 13:22:42 -05:00
updateTabName(db);
}
}
bool DatabaseTabWidget::closeDatabase(int index)
2011-11-13 08:55:20 -05:00
{
if (index == -1) {
index = currentIndex();
}
setCurrentIndex(index);
return closeDatabase(indexDatabase(index));
}
void DatabaseTabWidget::closeDatabaseFromSender()
{
Q_ASSERT(sender());
DatabaseWidget* dbWidget = static_cast<DatabaseWidget*>(sender());
Database* db = databaseFromDatabaseWidget(dbWidget);
int index = databaseIndex(db);
setCurrentIndex(index);
closeDatabase(db);
2011-11-13 08:55:20 -05:00
}
void DatabaseTabWidget::saveDatabase(int index)
{
if (index == -1) {
index = currentIndex();
}
saveDatabase(indexDatabase(index));
}
void DatabaseTabWidget::saveDatabaseAs(int index)
2011-12-24 13:22:42 -05:00
{
if (index == -1) {
index = currentIndex();
2011-12-24 13:22:42 -05:00
}
saveDatabaseAs(indexDatabase(index));
}
void DatabaseTabWidget::changeMasterKey()
{
currentDatabaseWidget()->switchToMasterKeyChange();
}
void DatabaseTabWidget::changeDatabaseSettings()
{
currentDatabaseWidget()->switchToDatabaseSettings();
}
void DatabaseTabWidget::createEntry()
2011-12-27 10:04:59 -05:00
{
currentDatabaseWidget()->createEntry();
2011-12-27 10:04:59 -05:00
}
void DatabaseTabWidget::editEntry()
2011-12-29 13:01:58 -05:00
{
currentDatabaseWidget()->switchToEntryEdit();
2011-12-29 13:01:58 -05:00
}
2012-04-18 14:08:54 -04:00
void DatabaseTabWidget::deleteEntry()
{
currentDatabaseWidget()->deleteEntry();
}
void DatabaseTabWidget::createGroup()
{
currentDatabaseWidget()->createGroup();
}
void DatabaseTabWidget::editGroup()
{
currentDatabaseWidget()->switchToGroupEdit();
}
2012-04-21 13:06:28 -04:00
void DatabaseTabWidget::deleteGroup()
{
currentDatabaseWidget()->deleteGroup();
}
void DatabaseTabWidget::updateTabName(Database* db)
2011-11-13 08:55:20 -05:00
{
int index = databaseIndex(db);
Q_ASSERT(index != -1);
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
QString tabName;
if (dbStruct.file) {
2011-12-25 13:47:41 -05:00
QFileInfo fileInfo(*dbStruct.file);
if (db->metadata()->name().isEmpty()) {
2011-12-25 13:47:41 -05:00
tabName = fileInfo.fileName();
}
else {
2011-12-25 13:47:41 -05:00
tabName = db->metadata()->name();
}
2011-12-25 13:47:41 -05:00
setTabToolTip(index, dbStruct.fileName);
2011-11-13 08:55:20 -05:00
}
else {
if (db->metadata()->name().isEmpty()) {
tabName = tr("New database");
}
else {
2011-12-25 13:47:41 -05:00
tabName = QString("%1 [%2]").arg(db->metadata()->name(), tr("New database"));
}
2011-11-13 08:55:20 -05:00
}
if (dbStruct.modified) {
tabName.append("*");
}
setTabText(index, tabName);
Q_EMIT tabNameChanged();
2011-11-13 08:55:20 -05:00
}
int DatabaseTabWidget::databaseIndex(Database* db)
2011-11-13 08:55:20 -05:00
{
QWidget* dbWidget = m_dbList.value(db).dbWidget;
return indexOf(dbWidget);
2011-11-13 08:55:20 -05:00
}
Database* DatabaseTabWidget::indexDatabase(int index)
2011-11-13 08:55:20 -05:00
{
QWidget* dbWidget = widget(index);
2011-11-13 08:55:20 -05:00
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
while (i.hasNext()) {
i.next();
if (i.value().dbWidget == dbWidget) {
return i.key();
}
}
return 0;
}
Database* DatabaseTabWidget::databaseFromDatabaseWidget(DatabaseWidget* dbWidget)
{
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
while (i.hasNext()) {
i.next();
if (i.value().dbWidget == dbWidget) {
return i.key();
}
}
return 0;
}
void DatabaseTabWidget::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
{
m_dbList.insert(db, dbStruct);
addTab(dbStruct.dbWidget, "");
toggleTabbar();
updateTabName(db);
int index = databaseIndex(db);
setCurrentIndex(index);
2012-04-25 14:22:55 -04:00
connect(db->metadata(), SIGNAL(nameTextChanged(Database*)),
SLOT(updateTabName(Database*)));
connect(dbStruct.dbWidget->entryView(), SIGNAL(entrySelectionChanged()),
SLOT(emitEntrySelectionChanged()));
connect(dbStruct.dbWidget, SIGNAL(closeRequest()), SLOT(closeDatabaseFromSender()));
connect(db, SIGNAL(modified()), SLOT(modified()));
2012-04-25 14:22:55 -04:00
connect(dbStruct.dbWidget, SIGNAL(currentModeChanged(DatabaseWidget::Mode)),
SIGNAL(currentWidgetModeChanged(DatabaseWidget::Mode)));
}
DatabaseWidget* DatabaseTabWidget::currentDatabaseWidget()
{
Database* db = indexDatabase(currentIndex());
if (db) {
return m_dbList[db].dbWidget;
}
else {
return 0;
}
}
void DatabaseTabWidget::modified()
{
Q_ASSERT(sender());
Database* db = static_cast<Database*>(sender());
DatabaseManagerStruct& dbStruct = m_dbList[db];
if (!dbStruct.modified) {
dbStruct.modified = true;
updateTabName(db);
}
}