From 82763517bf48c598b681402e982b34191b64ca6f Mon Sep 17 00:00:00 2001 From: alex-z Date: Sat, 17 Feb 2024 21:04:27 +0100 Subject: [PATCH] Use 'FindMimeFromData' from Win API instead of QMimeDatabase() functions to get the mimetype. Prevents freeze from VFS placeholders. Signed-off-by: alex-z --- src/common/utility.h | 3 +++ src/common/utility_mac.mm | 5 +++++ src/common/utility_unix.cpp | 5 +++++ src/common/utility_win.cpp | 19 +++++++++++++++++++ src/csync/ConfigureChecks.cmake | 2 +- src/gui/caseclashfilenamedialog.cpp | 7 +++++-- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/common/utility.h b/src/common/utility.h index a76527c1caa81..fbc3653a7b242 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -183,6 +184,8 @@ namespace Utility { */ OCSYNC_EXPORT QString timeAgoInWords(const QDateTime &dt, const QDateTime &from = QDateTime()); + OCSYNC_EXPORT QMimeType mimeTypeForFile(const QString &filePath); + class OCSYNC_EXPORT StopWatch { private: diff --git a/src/common/utility_mac.mm b/src/common/utility_mac.mm index b7e74c7130522..4b4d925d5ec69 100644 --- a/src/common/utility_mac.mm +++ b/src/common/utility_mac.mm @@ -266,4 +266,9 @@ void Utility::registerUriHandlerForLocalEditing() { /* URI handler is registered via MacOSXBundleInfo.plist.in */ } +QMimeType Utility::mimeTypeForFile(const QString &filePath) +{ + return QMimeDatabase().mimeTypeForFile(filePath); +} + } // namespace OCC diff --git a/src/common/utility_unix.cpp b/src/common/utility_unix.cpp index 091f16ab6548e..3b9381a227377 100644 --- a/src/common/utility_unix.cpp +++ b/src/common/utility_unix.cpp @@ -150,4 +150,9 @@ void Utility::registerUriHandlerForLocalEditing() process.waitForFinished(); } +QMimeType Utility::mimeTypeForFile(const QString &filePath) +{ + return QMimeDatabase().mimeTypeForFile(filePath); +} + } // namespace OCC diff --git a/src/common/utility_win.cpp b/src/common/utility_win.cpp index ba4d2ba859156..6d63672a72c18 100644 --- a/src/common/utility_win.cpp +++ b/src/common/utility_win.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +45,19 @@ static const char systemRunPathC[] = R"(HKEY_LOCAL_MACHINE\Software\Microsoft\Wi static const char runPathC[] = R"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run)"; namespace OCC { + +QString mimeNameForFile(const QString &filePath) +{ + LPWSTR mimeTypeFound = NULL; + HRESULT hr = FindMimeFromData(NULL, filePath.toStdWString().c_str(), NULL, 0, NULL, FMFD_URLASFILENAME, &mimeTypeFound, 0); + if (SUCCEEDED(hr)) { + const auto result = QString::fromWCharArray(mimeTypeFound); + CoTaskMemFree(mimeTypeFound); + return result; + } + + return {}; +} QVector Utility::queryProcessInfosKeepingFileOpen(const QString &filePath) { @@ -538,4 +552,9 @@ Utility::NtfsPermissionLookupRAII::~NtfsPermissionLookupRAII() qt_ntfs_permission_lookup--; } +QMimeType Utility::mimeTypeForFile(const QString &filePath) +{ + return QMimeDatabase().mimeTypeForName(mimeNameForFile(filePath)); +} + } // namespace OCC diff --git a/src/csync/ConfigureChecks.cmake b/src/csync/ConfigureChecks.cmake index bd1a74be11f02..7347c255a26a3 100644 --- a/src/csync/ConfigureChecks.cmake +++ b/src/csync/ConfigureChecks.cmake @@ -31,7 +31,7 @@ if (NOT LINUX) endif (NOT LINUX) if(WIN32) - set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} psapi kernel32 Rstrtmgr) + set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} psapi kernel32 Rstrtmgr urlmon) endif() check_function_exists(utimes HAVE_UTIMES) diff --git a/src/gui/caseclashfilenamedialog.cpp b/src/gui/caseclashfilenamedialog.cpp index cfba3660f1e7b..cc031db38755f 100644 --- a/src/gui/caseclashfilenamedialog.cpp +++ b/src/gui/caseclashfilenamedialog.cpp @@ -88,7 +88,10 @@ CaseClashFilenameDialog::CaseClashFilenameDialog(AccountPtr account, _relativeFilePath = filePathFileInfo.path() + QStringLiteral("/"); _relativeFilePath = _relativeFilePath.replace(folder->path(), QLatin1String()); - _relativeFilePath = _relativeFilePath.isEmpty() ? QString() : _relativeFilePath + QStringLiteral("/"); + _relativeFilePath = _relativeFilePath.isEmpty() ? QString() : _relativeFilePath; + if (!_relativeFilePath.isEmpty() && !_relativeFilePath.endsWith(QStringLiteral("/"))) { + _relativeFilePath += QStringLiteral("/"); + } _originalFileName = _relativeFilePath + conflictFileName; @@ -208,7 +211,7 @@ void CaseClashFilenameDialog::updateFileWidgetGroup(const QString &filePath, const auto fileSizeString = locale().formattedDataSize(filePathFileInfo.size()); const auto fileUrl = QUrl::fromLocalFile(filePath).toString(); const auto linkString = QStringLiteral("%2").arg(fileUrl, linkText); - const auto mime = QMimeDatabase().mimeTypeForFile(_filePath); + const auto mime = Utility::mimeTypeForFile(_filePath); QIcon fileTypeIcon; qCDebug(lcCaseClashConflictFialog) << filePath << filePathFileInfo.exists() << filename << lastModifiedString << fileSizeString << fileUrl << linkString << mime;