keepassxc

Форк
0
/
Resources.cpp 
141 строка · 4.5 Кб
1
/*
2
 *  Copyright (C) 2020 KeePassXC Team <team@keepassxc.org>
3
 *  Copyright (C) 2011 Felix Geyer <debfx@fobos.de>
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 2 or (at your option)
8
 *  version 3 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "Resources.h"
20

21
#include <QCoreApplication>
22
#include <QDir>
23
#include <QLibrary>
24

25
#include "config-keepassx.h"
26
#include "core/Config.h"
27
#include "core/Global.h"
28

29
Resources* Resources::m_instance(nullptr);
30

31
QString Resources::dataPath(const QString& name) const
32
{
33
    if (name.isEmpty() || name.startsWith('/')) {
34
        return m_dataPath + name;
35
    }
36
    return m_dataPath + "/" + name;
37
}
38

39
QString Resources::pluginPath(const QString& name) const
40
{
41
    QStringList pluginPaths;
42

43
    QDir buildDir(QCoreApplication::applicationDirPath() + "/autotype");
44
    const QStringList buildDirEntryList = buildDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
45
    for (const QString& dir : buildDirEntryList) {
46
        pluginPaths << QCoreApplication::applicationDirPath() + "/autotype/" + dir;
47
    }
48

49
    // for TestAutoType
50
    pluginPaths << QCoreApplication::applicationDirPath() + "/../src/autotype/test";
51

52
#if defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE)
53
    pluginPaths << QCoreApplication::applicationDirPath() + "/../PlugIns";
54
#endif
55

56
    pluginPaths << QCoreApplication::applicationDirPath();
57

58
    QString configuredPluginDir = KEEPASSX_PLUGIN_DIR;
59
    if (configuredPluginDir != ".") {
60
        if (QDir(configuredPluginDir).isAbsolute()) {
61
            pluginPaths << configuredPluginDir;
62
        } else {
63
            QString relativePluginDir =
64
                QStringLiteral("%1/../%2").arg(QCoreApplication::applicationDirPath(), configuredPluginDir);
65
            pluginPaths << QDir(relativePluginDir).canonicalPath();
66

67
            QString absolutePluginDir = QStringLiteral("%1/%2").arg(KEEPASSX_PREFIX_DIR, configuredPluginDir);
68
            pluginPaths << QDir(absolutePluginDir).canonicalPath();
69
        }
70
    }
71

72
    QStringList dirFilter;
73
    dirFilter << QStringLiteral("*%1*").arg(name);
74

75
    for (const QString& path : asConst(pluginPaths)) {
76
        const QStringList fileCandidates = QDir(path).entryList(dirFilter, QDir::Files);
77

78
        for (const QString& file : fileCandidates) {
79
            QString filePath = path + "/" + file;
80

81
            if (QLibrary::isLibrary(filePath)) {
82
                return filePath;
83
            }
84
        }
85
    }
86

87
    return {};
88
}
89

90
QString Resources::wordlistPath(const QString& name) const
91
{
92
    return dataPath(QStringLiteral("wordlists/%1").arg(name));
93
}
94

95
QString Resources::userWordlistPath(const QString& name) const
96
{
97
    QString configPath = QFileInfo(config()->getFileName()).absolutePath();
98
    return configPath + QStringLiteral("/wordlists/%1").arg(name);
99
}
100

101
Resources::Resources()
102
{
103
    const QString appDirPath = QCoreApplication::applicationDirPath();
104
#if defined(Q_OS_UNIX) && !(defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE))
105
    trySetResourceDir(KEEPASSX_DATA_DIR) || trySetResourceDir(QString("%1/../%2").arg(appDirPath, KEEPASSX_DATA_DIR))
106
        || trySetResourceDir(QString("%1/%2").arg(KEEPASSX_PREFIX_DIR, KEEPASSX_DATA_DIR));
107
#elif defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE)
108
    trySetResourceDir(appDirPath + QStringLiteral("/../Resources"));
109
#elif defined(Q_OS_WIN)
110
    trySetResourceDir(appDirPath + QStringLiteral("/share"));
111
#endif
112

113
    if (m_dataPath.isEmpty()) {
114
        // Last ditch check if we are running from inside the src or test build directory
115
        trySetResourceDir(appDirPath + QStringLiteral("/../share"))
116
            || trySetResourceDir(appDirPath + QStringLiteral("/../../share"));
117
    }
118

119
    if (m_dataPath.isEmpty()) {
120
        qWarning("Resources::DataPath: can't find data dir");
121
    }
122
}
123

124
bool Resources::trySetResourceDir(const QString& path)
125
{
126
    QDir dir(path);
127
    if (dir.exists()) {
128
        m_dataPath = dir.canonicalPath();
129
        return true;
130
    }
131
    return false;
132
}
133

134
Resources* Resources::instance()
135
{
136
    if (!m_instance) {
137
        m_instance = new Resources();
138
    }
139

140
    return m_instance;
141
}
142

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

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

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

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