Skip to content

Commit

Permalink
Modernize Utility::removeFavLink and Utility::setupFavLink.
Browse files Browse the repository at this point in the history
Exit setupFavLink function when SHGetKnownFolderPath fails.

Signed-off-by: Camila Ayres <[email protected]>
  • Loading branch information
camilasan authored and mgallien committed Nov 15, 2024
1 parent 38eefd4 commit d5278c5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d5278c5

Please sign in to comment.