Skip to content

Commit

Permalink
Use 'FindMimeFromData' from Win API instead of QMimeDatabase() functi…
Browse files Browse the repository at this point in the history
…ons to get the mimetype. Prevents freeze from VFS placeholders.

Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Feb 17, 2024
1 parent 1129cb9 commit 8276351
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QElapsedTimer>
#include <QLoggingCategory>
#include <QMap>
#include <QMimeDatabase>
#include <QUrl>
#include <QUrlQuery>
#include <functional>
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/common/utility_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/common/utility_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,9 @@ void Utility::registerUriHandlerForLocalEditing()
process.waitForFinished();
}

QMimeType Utility::mimeTypeForFile(const QString &filePath)
{
return QMimeDatabase().mimeTypeForFile(filePath);
}

} // namespace OCC
19 changes: 19 additions & 0 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <shlguid.h>
#include <shlobj.h>
#include <string>
#include <urlmon.h>
#include <winbase.h>
#include <windows.h>
#include <winerror.h>
Expand All @@ -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)

Check warning on line 49 in src/common/utility_win.cpp

View workflow job for this annotation

GitHub Actions / build

src/common/utility_win.cpp:49:9 [modernize-use-trailing-return-type]

use a trailing return type for this function
{
LPWSTR mimeTypeFound = NULL;

Check warning on line 51 in src/common/utility_win.cpp

View workflow job for this annotation

GitHub Actions / build

src/common/utility_win.cpp:51:12 [cppcoreguidelines-init-variables]

variable 'mimeTypeFound' is not initialized
HRESULT hr = FindMimeFromData(NULL, filePath.toStdWString().c_str(), NULL, 0, NULL, FMFD_URLASFILENAME, &mimeTypeFound, 0);

Check warning on line 52 in src/common/utility_win.cpp

View workflow job for this annotation

GitHub Actions / build

src/common/utility_win.cpp:52:13 [cppcoreguidelines-init-variables]

variable 'hr' is not initialized

Check warning on line 52 in src/common/utility_win.cpp

View workflow job for this annotation

GitHub Actions / build

src/common/utility_win.cpp:52:13 [readability-identifier-length]

variable name 'hr' is too short, expected at least 3 characters
if (SUCCEEDED(hr)) {
const auto result = QString::fromWCharArray(mimeTypeFound);
CoTaskMemFree(mimeTypeFound);
return result;
}

return {};
}

QVector<Utility::ProcessInfosForOpenFile> Utility::queryProcessInfosKeepingFileOpen(const QString &filePath)
{
Expand Down Expand Up @@ -538,4 +552,9 @@ Utility::NtfsPermissionLookupRAII::~NtfsPermissionLookupRAII()
qt_ntfs_permission_lookup--;
}

QMimeType Utility::mimeTypeForFile(const QString &filePath)
{
return QMimeDatabase().mimeTypeForName(mimeNameForFile(filePath));
}

} // namespace OCC
2 changes: 1 addition & 1 deletion src/csync/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/gui/caseclashfilenamedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("<a href='%1'>%2</a>").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;
Expand Down

0 comments on commit 8276351

Please sign in to comment.