Use an own FileDialog class instead of QFileDialog.

This commit is contained in:
Felix Geyer 2011-12-26 01:21:29 +01:00
parent dc0de8caf2
commit f90831b809
5 changed files with 140 additions and 7 deletions

View File

@ -51,6 +51,7 @@ set(keepassx_SOURCES
gui/EditEntryWidget.cpp gui/EditEntryWidget.cpp
gui/EntryModel.cpp gui/EntryModel.cpp
gui/EntryView.cpp gui/EntryView.cpp
gui/FileDialog.cpp
gui/GroupModel.cpp gui/GroupModel.cpp
gui/GroupView.cpp gui/GroupView.cpp
gui/KeyOpenDialog.cpp gui/KeyOpenDialog.cpp

View File

@ -18,13 +18,13 @@
#include "DatabaseManager.h" #include "DatabaseManager.h"
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtGui/QFileDialog>
#include <QtGui/QTabWidget> #include <QtGui/QTabWidget>
#include "core/Database.h" #include "core/Database.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "format/KeePass2XmlReader.h" #include "format/KeePass2XmlReader.h"
#include "gui/DatabaseWidget.h" #include "gui/DatabaseWidget.h"
#include "gui/FileDialog.h"
#include "gui/KeyOpenDialog.h" #include "gui/KeyOpenDialog.h"
DatabaseManagerStruct::DatabaseManagerStruct() DatabaseManagerStruct::DatabaseManagerStruct()
@ -53,7 +53,7 @@ void DatabaseManager::newDatabase()
void DatabaseManager::openDatabase() void DatabaseManager::openDatabase()
{ {
QString fileName = QFileDialog::getOpenFileName(m_window, tr("Open database"), QString(), QString fileName = fileDialog()->getOpenFileName(m_window, tr("Open database"), QString(),
tr("KeePass 2 Database").append(" (*.kdbx)")); tr("KeePass 2 Database").append(" (*.kdbx)"));
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
openDatabase(fileName); openDatabase(fileName);
@ -134,7 +134,7 @@ void DatabaseManager::saveDatabase(Database* db)
void DatabaseManager::saveDatabaseAs(Database* db) void DatabaseManager::saveDatabaseAs(Database* db)
{ {
QString fileName = QFileDialog::getSaveFileName(m_window, tr("Save database as"), QString fileName = fileDialog()->getSaveFileName(m_window, tr("Save database as"),
QString(), tr("KeePass 2 Database").append(" (*.kdbx)")); QString(), tr("KeePass 2 Database").append(" (*.kdbx)"));
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
DatabaseManagerStruct& dbStruct = m_dbList[db]; DatabaseManagerStruct& dbStruct = m_dbList[db];

86
src/gui/FileDialog.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
* 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 "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;
}

46
src/gui/FileDialog.h Normal file
View File

@ -0,0 +1,46 @@
/*
* 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/>.
*/
#ifndef KEEPASSX_FILEDIALOG_H
#define KEEPASSX_FILEDIALOG_H
#include <QtGui/QFileDialog>
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

View File

@ -18,10 +18,10 @@
#include "KeyOpenDialog.h" #include "KeyOpenDialog.h"
#include "ui_KeyOpenDialog.h" #include "ui_KeyOpenDialog.h"
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox> #include <QtGui/QMessageBox>
#include "core/Config.h" #include "core/Config.h"
#include "gui/FileDialog.h"
#include "keys/FileKey.h" #include "keys/FileKey.h"
#include "keys/PasswordKey.h" #include "keys/PasswordKey.h"
@ -116,7 +116,7 @@ void KeyOpenDialog::setOkButtonEnabled()
void KeyOpenDialog::browseKeyFile() void KeyOpenDialog::browseKeyFile()
{ {
QString filters = QString("%1 (*);;%2 (*.key)").arg(tr("All files"), tr("Key files")); 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()) { if (!filename.isEmpty()) {
m_ui->comboKeyFile->lineEdit()->setText(filename); m_ui->comboKeyFile->lineEdit()->setText(filename);