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 "DatabaseManager.h"
|
|
|
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
#include <QtGui/QTabWidget>
|
|
|
|
|
|
|
|
#include "core/Database.h"
|
|
|
|
#include "core/Metadata.h"
|
|
|
|
#include "format/KeePass2XmlReader.h"
|
|
|
|
#include "gui/DatabaseWidget.h"
|
2011-12-25 19:21:29 -05:00
|
|
|
#include "gui/FileDialog.h"
|
2011-12-29 13:01:58 -05:00
|
|
|
#include "gui/EntryView.h"
|
2011-12-27 09:49:06 -05:00
|
|
|
#include "gui/GroupView.h"
|
2011-11-13 08:55:20 -05:00
|
|
|
#include "gui/KeyOpenDialog.h"
|
|
|
|
|
|
|
|
DatabaseManagerStruct::DatabaseManagerStruct()
|
|
|
|
: file(0)
|
|
|
|
, dbWidget(0)
|
|
|
|
, modified(false)
|
|
|
|
, readOnly(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DatabaseManager::DatabaseManager(QTabWidget* tabWidget)
|
|
|
|
: m_tabWidget(tabWidget)
|
|
|
|
, m_window(tabWidget->window())
|
|
|
|
{
|
|
|
|
connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), SLOT(closeDatabase(int)));
|
2011-12-29 13:01:58 -05:00
|
|
|
connect(m_tabWidget, SIGNAL(currentChanged(int)), SLOT(emitEntrySelectionChanged()));
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
2011-11-16 12:47:17 -05:00
|
|
|
void DatabaseManager::newDatabase()
|
|
|
|
{
|
|
|
|
DatabaseManagerStruct dbStruct;
|
|
|
|
Database* db = new Database();
|
|
|
|
dbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget);
|
|
|
|
|
|
|
|
insertDatabase(db, dbStruct);
|
|
|
|
}
|
|
|
|
|
2011-11-13 08:55:20 -05:00
|
|
|
void DatabaseManager::openDatabase()
|
|
|
|
{
|
2011-12-25 19:21:29 -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 DatabaseManager::openDatabase(const QString& fileName)
|
|
|
|
{
|
|
|
|
QScopedPointer<QFile> file(new QFile(fileName));
|
2011-12-24 13:22:42 -05:00
|
|
|
// TODO error handling
|
2011-11-13 08:55:20 -05:00
|
|
|
if (!file->open(QIODevice::ReadWrite)) {
|
|
|
|
if (!file->open(QIODevice::ReadOnly)) {
|
|
|
|
// can only open read-only
|
2011-12-26 13:17:11 -05:00
|
|
|
m_curDbStruct.readOnly = true;
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// can't open
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
m_curDbStruct.file = file.take();
|
|
|
|
m_curDbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
openDatabaseDialog();
|
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
void DatabaseManager::openDatabaseDialog()
|
|
|
|
{
|
|
|
|
m_curKeyDialog = new KeyOpenDialog(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();
|
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
void DatabaseManager::openDatabaseRead()
|
|
|
|
{
|
|
|
|
m_curDbStruct.file->reset();
|
|
|
|
Database* db = m_reader.readDatabase(m_curDbStruct.file, m_curKeyDialog->key());
|
|
|
|
delete m_curKeyDialog;
|
|
|
|
m_curKeyDialog = 0;
|
2011-11-13 08:55:20 -05:00
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
if (!db) {
|
|
|
|
openDatabaseDialog();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_curDbStruct.dbWidget = new DatabaseWidget(db, m_tabWidget);
|
|
|
|
insertDatabase(db, m_curDbStruct);
|
|
|
|
m_curDbStruct = DatabaseManagerStruct();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::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
|
|
|
}
|
|
|
|
|
2011-12-29 13:01:58 -05:00
|
|
|
void DatabaseManager::emitEntrySelectionChanged()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(m_tabWidget->currentIndex());
|
|
|
|
|
|
|
|
bool isSingleEntrySelected = false;
|
|
|
|
if (db) {
|
|
|
|
isSingleEntrySelected = m_dbList[db].dbWidget->entryView()->isSingleEntrySelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT entrySelectionChanged(isSingleEntrySelected);
|
|
|
|
}
|
|
|
|
|
2011-11-13 08:55:20 -05:00
|
|
|
void DatabaseManager::closeDatabase(Database* db)
|
|
|
|
{
|
|
|
|
Q_ASSERT(db);
|
|
|
|
|
|
|
|
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
|
|
|
|
|
|
|
|
if (dbStruct.modified) {
|
|
|
|
// TODO message box
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = databaseIndex(db);
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
|
|
|
|
m_tabWidget->removeTab(index);
|
|
|
|
delete dbStruct.dbWidget;
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2011-11-16 12:47:17 -05:00
|
|
|
void DatabaseManager::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-11-16 12:47:17 -05:00
|
|
|
|
2011-12-24 13:22:42 -05:00
|
|
|
dbStruct.modified = false;
|
|
|
|
updateTabName(db);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
saveDatabaseAs(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::saveDatabaseAs(Database* db)
|
|
|
|
{
|
2011-12-26 14:08:26 -05:00
|
|
|
DatabaseManagerStruct& dbStruct = m_dbList[db];
|
2011-12-28 13:05:31 -05:00
|
|
|
QString oldFileName;
|
|
|
|
if (dbStruct.file) {
|
2011-12-26 14:08:26 -05:00
|
|
|
oldFileName = dbStruct.fileName;
|
2011-12-28 13:05:31 -05:00
|
|
|
}
|
2011-12-25 19:21:29 -05:00
|
|
|
QString fileName = fileDialog()->getSaveFileName(m_window, tr("Save database as"),
|
2011-12-26 14:08:26 -05:00
|
|
|
oldFileName, tr("KeePass 2 Database").append(" (*.kdbx)"));
|
2011-12-24 13:22:42 -05:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
delete dbStruct.file;
|
|
|
|
QScopedPointer<QFile> file(new QFile(fileName));
|
|
|
|
// TODO error handling
|
|
|
|
if (!file->open(QIODevice::ReadWrite)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dbStruct.file = file.take();
|
|
|
|
// TODO ensure that the data is actually written to disk
|
|
|
|
m_writer.writeDatabase(dbStruct.file, db);
|
|
|
|
dbStruct.file->flush();
|
|
|
|
|
|
|
|
dbStruct.modified = false;
|
2011-12-26 13:17:11 -05:00
|
|
|
dbStruct.fileName = QFileInfo(fileName).absoluteFilePath();
|
2011-12-24 13:22:42 -05:00
|
|
|
updateTabName(db);
|
|
|
|
}
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
|
2011-11-13 08:55:20 -05:00
|
|
|
void DatabaseManager::closeDatabase(int index)
|
|
|
|
{
|
2011-11-16 12:47:17 -05:00
|
|
|
if (index == -1) {
|
|
|
|
index = m_tabWidget->currentIndex();
|
|
|
|
}
|
|
|
|
|
2011-11-13 08:55:20 -05:00
|
|
|
closeDatabase(indexDatabase(index));
|
|
|
|
}
|
|
|
|
|
2011-11-16 12:47:17 -05:00
|
|
|
void DatabaseManager::saveDatabase(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = m_tabWidget->currentIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
saveDatabase(indexDatabase(index));
|
|
|
|
}
|
|
|
|
|
2011-12-24 13:22:42 -05:00
|
|
|
void DatabaseManager::saveDatabaseAs(int index)
|
|
|
|
{
|
|
|
|
if (index == -1) {
|
|
|
|
index = m_tabWidget->currentIndex();
|
|
|
|
}
|
|
|
|
saveDatabaseAs(indexDatabase(index));
|
|
|
|
}
|
|
|
|
|
2011-12-27 10:04:59 -05:00
|
|
|
void DatabaseManager::createEntry()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(m_tabWidget->currentIndex());
|
|
|
|
DatabaseWidget* dbWidget = m_dbList[db].dbWidget;
|
|
|
|
dbWidget->createEntry();
|
|
|
|
}
|
|
|
|
|
2011-12-29 13:01:58 -05:00
|
|
|
void DatabaseManager::editEntry()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(m_tabWidget->currentIndex());
|
|
|
|
DatabaseWidget* dbWidget = m_dbList[db].dbWidget;
|
|
|
|
dbWidget->switchToEntryEdit();
|
|
|
|
}
|
|
|
|
|
2011-12-27 09:49:06 -05:00
|
|
|
void DatabaseManager::createGroup()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(m_tabWidget->currentIndex());
|
|
|
|
DatabaseWidget* dbWidget = m_dbList[db].dbWidget;
|
|
|
|
dbWidget->createGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatabaseManager::editGroup()
|
|
|
|
{
|
|
|
|
Database* db = indexDatabase(m_tabWidget->currentIndex());
|
|
|
|
DatabaseWidget* dbWidget = m_dbList[db].dbWidget;
|
|
|
|
dbWidget->switchToGroupEdit();
|
|
|
|
}
|
|
|
|
|
2011-11-13 08:55:20 -05:00
|
|
|
void DatabaseManager::updateTabName(Database* db)
|
|
|
|
{
|
|
|
|
int index = databaseIndex(db);
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
|
|
|
|
const DatabaseManagerStruct& dbStruct = m_dbList.value(db);
|
|
|
|
|
|
|
|
QString tabName;
|
2011-11-16 12:47:17 -05:00
|
|
|
|
|
|
|
if (dbStruct.file) {
|
2011-12-25 13:47:41 -05:00
|
|
|
QFileInfo fileInfo(*dbStruct.file);
|
2011-11-16 12:47:17 -05:00
|
|
|
|
|
|
|
if (db->metadata()->name().isEmpty()) {
|
2011-12-25 13:47:41 -05:00
|
|
|
tabName = fileInfo.fileName();
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
|
|
|
else {
|
2011-12-25 13:47:41 -05:00
|
|
|
tabName = db->metadata()->name();
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
2011-12-25 13:47:41 -05:00
|
|
|
|
2011-12-26 13:17:11 -05:00
|
|
|
m_tabWidget->setTabToolTip(index, dbStruct.fileName);
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
else {
|
2011-11-16 12:47:17 -05:00
|
|
|
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-16 12:47:17 -05:00
|
|
|
}
|
2011-11-13 08:55:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m_tabWidget->setTabText(index, tabName);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DatabaseManager::databaseIndex(Database* db)
|
|
|
|
{
|
|
|
|
QWidget* dbWidget = m_dbList.value(db).dbWidget;
|
|
|
|
return m_tabWidget->indexOf(dbWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
Database* DatabaseManager::indexDatabase(int index)
|
|
|
|
{
|
|
|
|
QWidget* dbWidget = m_tabWidget->widget(index);
|
|
|
|
|
|
|
|
QHashIterator<Database*, DatabaseManagerStruct> i(m_dbList);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
if (i.value().dbWidget == dbWidget) {
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-16 12:47:17 -05:00
|
|
|
|
|
|
|
void DatabaseManager::insertDatabase(Database* db, const DatabaseManagerStruct& dbStruct)
|
|
|
|
{
|
|
|
|
m_dbList.insert(db, dbStruct);
|
|
|
|
|
|
|
|
m_tabWidget->addTab(dbStruct.dbWidget, "");
|
|
|
|
updateTabName(db);
|
2011-12-24 18:56:04 -05:00
|
|
|
int index = databaseIndex(db);
|
|
|
|
m_tabWidget->setCurrentIndex(index);
|
2011-11-16 12:47:17 -05:00
|
|
|
|
|
|
|
connect(db->metadata(), SIGNAL(nameTextChanged(Database*)), SLOT(updateTabName(Database*)));
|
2011-12-29 13:01:58 -05:00
|
|
|
connect(dbStruct.dbWidget->entryView(), SIGNAL(entrySelectionChanged()), SLOT(emitEntrySelectionChanged()));
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|