Fix resolving resources when running from build directory

* Copy wordlists to build dir share folder
* Change resource path resolution to only test the provided directory, not finding a specific file
This commit is contained in:
Jonathan White 2020-06-04 08:16:47 -04:00
parent c830f85c09
commit 6f5e13815c
5 changed files with 40 additions and 56 deletions

View File

@ -19,6 +19,9 @@ add_subdirectory(translations)
file(GLOB wordlists_files "wordlists/*.wordlist") file(GLOB wordlists_files "wordlists/*.wordlist")
install(FILES ${wordlists_files} DESTINATION ${DATA_INSTALL_DIR}/wordlists) install(FILES ${wordlists_files} DESTINATION ${DATA_INSTALL_DIR}/wordlists)
# Copy wordlists to build dir for use in tests
file(COPY "wordlists" DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
if(UNIX AND NOT APPLE AND NOT HAIKU) if(UNIX AND NOT APPLE AND NOT HAIKU)
install(DIRECTORY icons/application/ DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor install(DIRECTORY icons/application/ DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor
FILES_MATCHING PATTERN "keepassx*.png" PATTERN "keepassx*.svg" FILES_MATCHING PATTERN "keepassx*.png" PATTERN "keepassx*.svg"

View File

@ -224,19 +224,18 @@ Resources::Resources()
{ {
const QString appDirPath = QCoreApplication::applicationDirPath(); const QString appDirPath = QCoreApplication::applicationDirPath();
#if defined(Q_OS_UNIX) && !(defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE)) #if defined(Q_OS_UNIX) && !(defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE))
testResourceDir(KEEPASSX_DATA_DIR) || testResourceDir(QStringLiteral("%1/../%2").arg(appDirPath, KEEPASSX_DATA_DIR)) trySetResourceDir(KEEPASSX_DATA_DIR) || trySetResourceDir(QString("%1/../%2").arg(appDirPath, KEEPASSX_DATA_DIR))
|| testResourceDir(QStringLiteral("%1/%2").arg(KEEPASSX_PREFIX_DIR, KEEPASSX_DATA_DIR)); || trySetResourceDir(QString("%1/%2").arg(KEEPASSX_PREFIX_DIR, KEEPASSX_DATA_DIR));
#elif defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE) #elif defined(Q_OS_MACOS) && defined(WITH_APP_BUNDLE)
testResourceDir(appDirPath + QStringLiteral("/../Resources")); trySetResourceDir(appDirPath + QStringLiteral("/../Resources"));
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
testResourceDir(appDirPath + QStringLiteral("/share")); trySetResourceDir(appDirPath + QStringLiteral("/share"));
#endif #endif
if (m_dataPath.isEmpty()) { if (m_dataPath.isEmpty()) {
// Last ditch check if we are running from inside the src or test build directory // Last ditch check if we are running from inside the src or test build directory
testResourceDir(appDirPath + QStringLiteral("/../../share")) trySetResourceDir(appDirPath + QStringLiteral("/../share"))
|| testResourceDir(appDirPath + QStringLiteral("/../share")) || trySetResourceDir(appDirPath + QStringLiteral("/../../share"));
|| testResourceDir(appDirPath + QStringLiteral("/../../../share"));
} }
if (m_dataPath.isEmpty()) { if (m_dataPath.isEmpty()) {
@ -244,10 +243,11 @@ Resources::Resources()
} }
} }
bool Resources::testResourceDir(const QString& dir) bool Resources::trySetResourceDir(const QString& path)
{ {
if (QFile::exists(dir + QStringLiteral("/icons/application/256x256/apps/keepassxc.png"))) { QDir dir(path);
m_dataPath = QDir::cleanPath(dir); if (dir.exists()) {
m_dataPath = dir.canonicalPath();
return true; return true;
} }
return false; return false;

View File

@ -42,7 +42,7 @@ public:
private: private:
Resources(); Resources();
bool testResourceDir(const QString& dir); bool trySetResourceDir(const QString& path);
static Resources* m_instance; static Resources* m_instance;

View File

@ -49,22 +49,9 @@ void Translator::installTranslators()
// Always try to load english last // Always try to load english last
languages << "en_US"; languages << "en_US";
const QStringList paths = { const auto path = resources()->dataPath("translations");
#ifdef QT_DEBUG installQtTranslator(languages, path);
QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR), if (!installTranslator(languages, path)) {
#endif
resources()->dataPath("translations")};
bool translationsLoaded = false;
for (const QString& path : paths) {
installQtTranslator(languages, path);
if (installTranslator(languages, path)) {
translationsLoaded = true;
break;
}
}
if (!translationsLoaded) {
// couldn't load configured language or fallback // couldn't load configured language or fallback
qWarning("Couldn't load translations."); qWarning("Couldn't load translations.");
} }
@ -117,40 +104,31 @@ bool Translator::installQtTranslator(const QStringList& languages, const QString
*/ */
QList<QPair<QString, QString>> Translator::availableLanguages() QList<QPair<QString, QString>> Translator::availableLanguages()
{ {
const QStringList paths = {
#ifdef QT_DEBUG
QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR),
#endif
resources()->dataPath("translations")};
QList<QPair<QString, QString>> languages; QList<QPair<QString, QString>> languages;
languages.append(QPair<QString, QString>("system", "System default")); languages.append(QPair<QString, QString>("system", "System default"));
QRegularExpression regExp("^keepassx_([a-zA-Z_]+)\\.qm$", QRegularExpression::CaseInsensitiveOption); QRegularExpression regExp("^keepassx_([a-zA-Z_]+)\\.qm$", QRegularExpression::CaseInsensitiveOption);
for (const QString& path : paths) { const QStringList fileList = QDir(resources()->dataPath("translations")).entryList();
const QStringList fileList = QDir(path).entryList(); for (const QString& filename : fileList) {
for (const QString& filename : fileList) { QRegularExpressionMatch match = regExp.match(filename);
QRegularExpressionMatch match = regExp.match(filename); if (match.hasMatch()) {
if (match.hasMatch()) { QString langcode = match.captured(1);
QString langcode = match.captured(1); if (langcode == "en") {
if (langcode == "en") { continue;
continue;
}
QLocale locale(langcode);
QString languageStr = QLocale::languageToString(locale.language());
if (langcode == "la") {
// langcode "la" (Latin) is translated into "C" by QLocale::languageToString()
languageStr = "Latin";
}
QString countryStr;
if (langcode.contains("_")) {
countryStr = QString(" (%1)").arg(QLocale::countryToString(locale.country()));
}
QPair<QString, QString> language(langcode, languageStr + countryStr);
languages.append(language);
} }
QLocale locale(langcode);
QString languageStr = QLocale::languageToString(locale.language());
if (langcode == "la") {
// langcode "la" (Latin) is translated into "C" by QLocale::languageToString()
languageStr = "Latin";
}
if (langcode.contains("_")) {
languageStr += QString(" (%1)").arg(QLocale::countryToString(locale.country()));
}
QPair<QString, QString> language(langcode, languageStr);
languages.append(language);
} }
} }

View File

@ -770,6 +770,9 @@ void TestGui::testDicewareEntryEntropy()
auto* spinBoxWordCount = dicewareWidget->findChild<QSpinBox*>("spinBoxWordCount"); auto* spinBoxWordCount = dicewareWidget->findChild<QSpinBox*>("spinBoxWordCount");
spinBoxWordCount->setValue(6); spinBoxWordCount->setValue(6);
// Confirm a password was generated
QVERIFY(!pwGeneratorWidget->getGeneratedPassword().isEmpty());
// Verify entropy and strength // Verify entropy and strength
auto* entropyLabel = pwGeneratorWidget->findChild<QLabel*>("entropyLabel"); auto* entropyLabel = pwGeneratorWidget->findChild<QLabel*>("entropyLabel");
auto* strengthLabel = pwGeneratorWidget->findChild<QLabel*>("strengthLabel"); auto* strengthLabel = pwGeneratorWidget->findChild<QLabel*>("strengthLabel");