2
* Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "FileDialog.h"
20
#include "core/Config.h"
22
#include <QProcessEnvironment>
24
FileDialog* FileDialog::m_instance(nullptr);
26
FileDialog::FileDialog() = default;
28
QString FileDialog::getOpenFileName(QWidget* parent,
29
const QString& caption,
31
const QString& filter,
32
QString* selectedFilter,
33
const QFileDialog::Options options)
35
if (!m_nextFileName.isEmpty()) {
36
const QString result = m_nextFileName;
37
m_nextFileName.clear();
40
const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
41
const auto result = QDir::toNativeSeparators(
42
QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options));
45
// on Mac OS X the focus is lost after closing the native dialog
47
parent->activateWindow();
54
QStringList FileDialog::getOpenFileNames(QWidget* parent,
55
const QString& caption,
57
const QString& filter,
58
QString* selectedFilter,
59
const QFileDialog::Options options)
61
if (!m_nextFileNames.isEmpty()) {
62
const QStringList results = m_nextFileNames;
63
m_nextFileNames.clear();
66
const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
67
auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options);
69
for (auto& path : results) {
70
path = QDir::toNativeSeparators(path);
74
// on Mac OS X the focus is lost after closing the native dialog
76
parent->activateWindow();
83
QString FileDialog::getSaveFileName(QWidget* parent,
84
const QString& caption,
86
const QString& filter,
87
QString* selectedFilter,
88
const QFileDialog::Options options)
90
if (!m_nextFileName.isEmpty()) {
91
const QString result = m_nextFileName;
92
m_nextFileName.clear();
95
const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
96
const auto result = QDir::toNativeSeparators(
97
QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));
100
// on Mac OS X the focus is lost after closing the native dialog
102
parent->activateWindow();
109
QString FileDialog::getExistingDirectory(QWidget* parent,
110
const QString& caption,
112
const QFileDialog::Options options)
114
if (!m_nextDirName.isEmpty()) {
115
const QString result = m_nextDirName;
116
m_nextDirName.clear();
119
const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
121
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options));
124
// on Mac OS X the focus is lost after closing the native dialog
126
parent->activateWindow();
133
void FileDialog::setNextFileName(const QString& fileName)
135
m_nextFileName = fileName;
138
void FileDialog::setNextDirectory(const QString& path)
140
m_nextDirName = path;
143
void FileDialog::saveLastDir(const QString& role, const QString& path, bool sensitive)
145
auto lastDirs = config()->get(Config::LastDir).toHash();
146
if (sensitive && !config()->get(Config::RememberLastDatabases).toBool()) {
147
// Ensure this role is forgotten
148
lastDirs.remove(role);
150
auto pathInfo = QFileInfo(path);
151
if (!pathInfo.exists()) {
152
lastDirs.remove(role);
154
lastDirs.insert(role, pathInfo.absolutePath());
157
config()->set(Config::LastDir, lastDirs);
160
QString FileDialog::getLastDir(const QString& role, const QString& defaultDir)
162
auto lastDirs = config()->get(Config::LastDir).toHash();
163
auto fallbackDir = defaultDir;
165
if (fallbackDir.isEmpty()) {
166
// Fallback to the environment variable, if it exists, otherwise use the home directory
167
const auto& env = QProcessEnvironment::systemEnvironment();
168
fallbackDir = env.value("KPXC_INITIAL_DIR", QDir::homePath());
171
return lastDirs.value(role, fallbackDir).toString();
174
FileDialog* FileDialog::instance()
177
m_instance = new FileDialog();