From 54d2ade5ea228d3c75039a8ffc76f4ab395c9965 Mon Sep 17 00:00:00 2001 From: Camila Ayres Date: Wed, 6 Nov 2024 15:08:23 +0100 Subject: [PATCH] Modernize Utility::removeFavLink and Utility::setupFavLink. Exit setupFavLink function when SHGetKnownFolderPath fails. Signed-off-by: Camila Ayres --- src/common/utility_win.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/common/utility_win.cpp b/src/common/utility_win.cpp index ba4d2ba859156..e2b917028fd11 100644 --- a/src/common/utility_win.cpp +++ b/src/common/utility_win.cpp @@ -139,21 +139,25 @@ void Utility::setupFavLink(const QString &folder) SetFileAttributesW((wchar_t *)desktopIni.fileName().utf16(), FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM); } - // Windows Explorer: Place under "Favorites" (Links) - QString linkName; - QDir folderDir(QDir::fromNativeSeparators(folder)); - /* Use new WINAPI functions */ PWSTR path; - - if (SHGetKnownFolderPath(FOLDERID_Links, 0, nullptr, &path) == S_OK) { - QString links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); - linkName = QDir(links).filePath(folderDir.dirName() + QLatin1String(".lnk")); - CoTaskMemFree(path); + if (!SHGetKnownFolderPath(FOLDERID_Links, 0, nullptr, &path) == S_OK) { + qCWarning(lcUtility) << "SHGetKnownFolderPath for " << folder << "has failed."; + return; } + + // Windows Explorer: Place under "Favorites" (Links) + const auto links = QDir::fromNativeSeparators(QString::fromWCharArray(path)); + CoTaskMemFree(path); + + const QDir folderDir(QDir::fromNativeSeparators(folder)); + const QString filePath = folderDir.dirName() + QLatin1String(".lnk"); + const auto linkName = QDir(links).filePath(filePath); + qCInfo(lcUtility) << "Creating favorite link from" << folder << "to" << linkName; - if (!QFile::link(folder, linkName)) + if (!QFile::link(folder, linkName)) { qCWarning(lcUtility) << "linking" << folder << "to" << linkName << "failed!"; + } } void Utility::removeFavLink(const QString &folder)