From d5278c58e14c89fe9c61a6f760d7ff9662ee639a 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)