keepassxc

Форк
0
/
FileDialog.cpp 
181 строка · 5.9 Кб
1
/*
2
 *  Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "FileDialog.h"
19

20
#include "core/Config.h"
21

22
#include <QProcessEnvironment>
23

24
FileDialog* FileDialog::m_instance(nullptr);
25

26
FileDialog::FileDialog() = default;
27

28
QString FileDialog::getOpenFileName(QWidget* parent,
29
                                    const QString& caption,
30
                                    const QString& dir,
31
                                    const QString& filter,
32
                                    QString* selectedFilter,
33
                                    const QFileDialog::Options options)
34
{
35
    if (!m_nextFileName.isEmpty()) {
36
        const QString result = m_nextFileName;
37
        m_nextFileName.clear();
38
        return result;
39
    } else {
40
        const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
41
        const auto result = QDir::toNativeSeparators(
42
            QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options));
43

44
#ifdef Q_OS_MACOS
45
        // on Mac OS X the focus is lost after closing the native dialog
46
        if (parent) {
47
            parent->activateWindow();
48
        }
49
#endif
50
        return result;
51
    }
52
}
53

54
QStringList FileDialog::getOpenFileNames(QWidget* parent,
55
                                         const QString& caption,
56
                                         const QString& dir,
57
                                         const QString& filter,
58
                                         QString* selectedFilter,
59
                                         const QFileDialog::Options options)
60
{
61
    if (!m_nextFileNames.isEmpty()) {
62
        const QStringList results = m_nextFileNames;
63
        m_nextFileNames.clear();
64
        return results;
65
    } else {
66
        const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
67
        auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options);
68

69
        for (auto& path : results) {
70
            path = QDir::toNativeSeparators(path);
71
        }
72

73
#ifdef Q_OS_MACOS
74
        // on Mac OS X the focus is lost after closing the native dialog
75
        if (parent) {
76
            parent->activateWindow();
77
        }
78
#endif
79
        return results;
80
    }
81
}
82

83
QString FileDialog::getSaveFileName(QWidget* parent,
84
                                    const QString& caption,
85
                                    const QString& dir,
86
                                    const QString& filter,
87
                                    QString* selectedFilter,
88
                                    const QFileDialog::Options options)
89
{
90
    if (!m_nextFileName.isEmpty()) {
91
        const QString result = m_nextFileName;
92
        m_nextFileName.clear();
93
        return result;
94
    } else {
95
        const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
96
        const auto result = QDir::toNativeSeparators(
97
            QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));
98

99
#ifdef Q_OS_MACOS
100
        // on Mac OS X the focus is lost after closing the native dialog
101
        if (parent) {
102
            parent->activateWindow();
103
        }
104
#endif
105
        return result;
106
    }
107
}
108

109
QString FileDialog::getExistingDirectory(QWidget* parent,
110
                                         const QString& caption,
111
                                         const QString& dir,
112
                                         const QFileDialog::Options options)
113
{
114
    if (!m_nextDirName.isEmpty()) {
115
        const QString result = m_nextDirName;
116
        m_nextDirName.clear();
117
        return result;
118
    } else {
119
        const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
120
        const auto result =
121
            QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options));
122

123
#ifdef Q_OS_MACOS
124
        // on Mac OS X the focus is lost after closing the native dialog
125
        if (parent) {
126
            parent->activateWindow();
127
        }
128
#endif
129
        return result;
130
    }
131
}
132

133
void FileDialog::setNextFileName(const QString& fileName)
134
{
135
    m_nextFileName = fileName;
136
}
137

138
void FileDialog::setNextDirectory(const QString& path)
139
{
140
    m_nextDirName = path;
141
}
142

143
void FileDialog::saveLastDir(const QString& role, const QString& path, bool sensitive)
144
{
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);
149
    } else {
150
        auto pathInfo = QFileInfo(path);
151
        if (!pathInfo.exists()) {
152
            lastDirs.remove(role);
153
        } else {
154
            lastDirs.insert(role, pathInfo.absolutePath());
155
        }
156
    }
157
    config()->set(Config::LastDir, lastDirs);
158
}
159

160
QString FileDialog::getLastDir(const QString& role, const QString& defaultDir)
161
{
162
    auto lastDirs = config()->get(Config::LastDir).toHash();
163
    auto fallbackDir = defaultDir;
164

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());
169
    }
170

171
    return lastDirs.value(role, fallbackDir).toString();
172
}
173

174
FileDialog* FileDialog::instance()
175
{
176
    if (!m_instance) {
177
        m_instance = new FileDialog();
178
    }
179

180
    return m_instance;
181
}
182

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.