From f90831b8095a51aca87be9dfea5c75520f3baa58 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Mon, 26 Dec 2011 01:21:29 +0100 Subject: Use an own FileDialog class instead of QFileDialog. --- src/CMakeLists.txt | 1 + src/gui/DatabaseManager.cpp | 10 +++--- src/gui/FileDialog.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ src/gui/FileDialog.h | 46 ++++++++++++++++++++++++ src/gui/KeyOpenDialog.cpp | 4 +-- 5 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 src/gui/FileDialog.cpp create mode 100644 src/gui/FileDialog.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c888f9538..d8630f0db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -51,6 +51,7 @@ set(keepassx_SOURCES gui/EditEntryWidget.cpp gui/EntryModel.cpp gui/EntryView.cpp + gui/FileDialog.cpp gui/GroupModel.cpp gui/GroupView.cpp gui/KeyOpenDialog.cpp diff --git a/src/gui/DatabaseManager.cpp b/src/gui/DatabaseManager.cpp index 0f6ac0f6c..e187d4f26 100644 --- a/src/gui/DatabaseManager.cpp +++ b/src/gui/DatabaseManager.cpp @@ -18,13 +18,13 @@ #include "DatabaseManager.h" #include -#include #include #include "core/Database.h" #include "core/Metadata.h" #include "format/KeePass2XmlReader.h" #include "gui/DatabaseWidget.h" +#include "gui/FileDialog.h" #include "gui/KeyOpenDialog.h" DatabaseManagerStruct::DatabaseManagerStruct() @@ -53,8 +53,8 @@ void DatabaseManager::newDatabase() void DatabaseManager::openDatabase() { - QString fileName = QFileDialog::getOpenFileName(m_window, tr("Open database"), QString(), - tr("KeePass 2 Database").append(" (*.kdbx)")); + QString fileName = fileDialog()->getOpenFileName(m_window, tr("Open database"), QString(), + tr("KeePass 2 Database").append(" (*.kdbx)")); if (!fileName.isEmpty()) { openDatabase(fileName); } @@ -134,8 +134,8 @@ void DatabaseManager::saveDatabase(Database* db) void DatabaseManager::saveDatabaseAs(Database* db) { - QString fileName = QFileDialog::getSaveFileName(m_window, tr("Save database as"), - QString(), tr("KeePass 2 Database").append(" (*.kdbx)")); + QString fileName = fileDialog()->getSaveFileName(m_window, tr("Save database as"), + QString(), tr("KeePass 2 Database").append(" (*.kdbx)")); if (!fileName.isEmpty()) { DatabaseManagerStruct& dbStruct = m_dbList[db]; diff --git a/src/gui/FileDialog.cpp b/src/gui/FileDialog.cpp new file mode 100644 index 000000000..aa18aeb79 --- /dev/null +++ b/src/gui/FileDialog.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2011 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 "FileDialog.h" + +#include "core/Config.h" + +QString FileDialog::getOpenFileName(QWidget* parent, const QString& caption, QString dir, + const QString& filter, QString* selectedFilter, QFileDialog::Options options) +{ + if (!m_nextFileName.isEmpty()) { + QString result = m_nextFileName; + m_nextFileName = ""; + return result; + } + else { + if (dir.isEmpty()) { + dir = config()->get("LastDir").toString(); + } + + QString result = QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options); + + if (!result.isEmpty()) { + config()->set("LastDir", QFileInfo(result).absolutePath()); + } + + return result; + } +} + +QString FileDialog::getSaveFileName(QWidget* parent, const QString& caption, QString dir, + const QString& filter, QString* selectedFilter, QFileDialog::Options options) +{ + if (!m_nextFileName.isEmpty()) { + QString result = m_nextFileName; + m_nextFileName = ""; + return result; + } + else { + if (dir.isEmpty()) { + dir = config()->get("LastDir").toString(); + } + + QString result = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options); + + if (!result.isEmpty()) { + config()->set("LastDir", QFileInfo(result).absolutePath()); + } + + return result; + } +} + +void FileDialog::setNextFileName(const QString& fileName) +{ + m_nextFileName = fileName; +} + +FileDialog::FileDialog() +{ +} + +FileDialog* fileDialog() +{ + static FileDialog* instance(0); + + if (!instance) { + instance = new FileDialog(); + } + + return instance; +} diff --git a/src/gui/FileDialog.h b/src/gui/FileDialog.h new file mode 100644 index 000000000..5e0acd458 --- /dev/null +++ b/src/gui/FileDialog.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2011 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_FILEDIALOG_H +#define KEEPASSX_FILEDIALOG_H + +#include + +class FileDialog +{ +public: + QString getOpenFileName(QWidget* parent = 0, const QString& caption = QString(), QString dir = QString(), + const QString& filter = QString(), QString* selectedFilter = 0, QFileDialog::Options options = 0); + QString getSaveFileName(QWidget* parent = 0, const QString& caption = QString(), QString dir = QString(), + const QString& filter = QString(), QString* selectedFilter = 0, QFileDialog::Options options = 0); + + /** + * Sets the result of the next get* method call. + * Use only for testing. + */ + void setNextFileName(const QString& fileName); + +private: + FileDialog(); + QString m_nextFileName; + + friend FileDialog* fileDialog(); +}; + +FileDialog* fileDialog(); + +#endif // KEEPASSX_FILEDIALOG_H diff --git a/src/gui/KeyOpenDialog.cpp b/src/gui/KeyOpenDialog.cpp index c28ee825b..95631c3a4 100644 --- a/src/gui/KeyOpenDialog.cpp +++ b/src/gui/KeyOpenDialog.cpp @@ -18,10 +18,10 @@ #include "KeyOpenDialog.h" #include "ui_KeyOpenDialog.h" -#include #include #include "core/Config.h" +#include "gui/FileDialog.h" #include "keys/FileKey.h" #include "keys/PasswordKey.h" @@ -116,7 +116,7 @@ void KeyOpenDialog::setOkButtonEnabled() void KeyOpenDialog::browseKeyFile() { QString filters = QString("%1 (*);;%2 (*.key)").arg(tr("All files"), tr("Key files")); - QString filename = QFileDialog::getOpenFileName(this, tr("Select key file"), QString(), filters); + QString filename = fileDialog()->getOpenFileName(this, tr("Select key file"), QString(), filters); if (!filename.isEmpty()) { m_ui->comboKeyFile->lineEdit()->setText(filename); -- cgit v1.2.3