Modernize Utility::removeFavLink and Utility::setupFavLink.

Exit setupFavLink function when SHGetKnownFolderPath fails.

Signed-off-by: Camila Ayres <hello@camilasan.com>
This commit is contained in:
Camila Ayres 2024-11-06 15:08:23 +01:00 committed by Matthieu Gallien
parent 38eefd4dc7
commit d5278c58e1

View file

@ -139,22 +139,26 @@ 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)
{