Cleanup system bindings from Windows when removing a local sync folder
authoralex-z <blackslayer4@gmail.com>
Fri, 19 Nov 2021 14:03:09 +0000 (16:03 +0200)
committerMatthieu Gallien (Rebase PR Action) <matthieu_gallien@yahoo.fr>
Tue, 23 Nov 2021 08:32:55 +0000 (08:32 +0000)
Signed-off-by: alex-z <blackslayer4@gmail.com>
src/common/utility.cpp
src/common/utility.h
src/common/utility_mac.cpp
src/common/utility_unix.cpp
src/common/utility_win.cpp
src/gui/accountsettings.cpp

index bd9394b8c3e6e32eae57886ed904191986637a69..3adb9a4dec27be22b932502877feba1b85b42482 100644 (file)
@@ -109,6 +109,11 @@ void Utility::setupFavLink(const QString &folder)
     setupFavLink_private(folder);
 }
 
+void Utility::removeFavLink(const QString &folder)
+{
+    removeFavLink_private(folder);
+}
+
 QString Utility::octetsToString(qint64 octets)
 {
 #define THE_FACTOR 1024
index 9522d188e7ddd4afd1dc4c33d372544dc5ee4f1e..349791644ef3b66df8e7784c2904b4fe6b8ba90a 100644 (file)
@@ -55,6 +55,7 @@ namespace Utility {
     OCSYNC_EXPORT void usleep(int usec);
     OCSYNC_EXPORT QString formatFingerprint(const QByteArray &, bool colonSeparated = true);
     OCSYNC_EXPORT void setupFavLink(const QString &folder);
+    OCSYNC_EXPORT void removeFavLink(const QString &folder);
     OCSYNC_EXPORT bool writeRandomFile(const QString &fname, int size = -1);
     OCSYNC_EXPORT QString octetsToString(qint64 octets);
     OCSYNC_EXPORT QByteArray userAgentString();
index c093d8805ed853b0bafdff3fa9b916c1266592bd..f11cf0b8f010ada2e7cca7c0ba1e5991918881df 100644 (file)
@@ -41,6 +41,11 @@ static void setupFavLink_private(const QString &folder)
     CFRelease(urlRef);
 }
 
+static void removeFavLink_private(const QString &folder)
+{
+    Q_UNUSED(folder)
+}
+
 bool hasLaunchOnStartup_private(const QString &)
 {
     // this is quite some duplicate code with setLaunchOnStartup, at some point we should fix this FIXME.
index 087254e6006083ed310eec1c1b0c4d414868e448..d994757383bdbe1c72f867af49468f835fbbf103 100644 (file)
@@ -37,6 +37,11 @@ static void setupFavLink_private(const QString &folder)
     }
 }
 
+static void removeFavLink_private(const QString &folder)
+{
+    Q_UNUSED(folder)
+}
+
 // returns the autostart directory the linux way
 // and respects the XDG_CONFIG_HOME env variable
 QString getUserAutostartDir_private()
index c6f9c00667c2e45d45f2acae0a4710b8a4d0dd38..2c14ccb1fc8cb8ac26c51c5663f82cf7291463dc 100644 (file)
@@ -74,6 +74,40 @@ static void setupFavLink_private(const QString &folder)
         qCWarning(lcUtility) << "linking" << folder << "to" << linkName << "failed!";
 }
 
+static void removeFavLink_private(const QString &folder)
+{
+    const QDir folderDir(folder);
+
+    // #1 Remove the Desktop.ini to reset the folder icon
+    if (!QFile::remove(folderDir.absoluteFilePath(QLatin1String("Desktop.ini")))) {
+        qCWarning(lcUtility) << "Remove Desktop.ini from" << folder
+                             << " has failed. Make sure it exists and is not locked by another process.";
+    }
+
+    // #2 Remove the system file attribute
+    const auto folderAttrs = GetFileAttributesW(folder.toStdWString().c_str());
+    if (!SetFileAttributesW(folder.toStdWString().c_str(), folderAttrs & ~FILE_ATTRIBUTE_SYSTEM)) {
+        qCWarning(lcUtility) << "Remove system file attribute failed for:" << folder;
+    }
+
+    // #3 Remove the link to this folder
+    PWSTR path;
+    if (!SHGetKnownFolderPath(FOLDERID_Links, 0, nullptr, &path) == S_OK) {
+        qCWarning(lcUtility) << "SHGetKnownFolderPath for " << folder << "has failed.";
+        return;
+    }
+
+    const QDir links(QString::fromWCharArray(path));
+    CoTaskMemFree(path);
+
+    const auto linkName = QDir(links).absoluteFilePath(folderDir.dirName() + QLatin1String(".lnk"));
+
+    qCInfo(lcUtility) << "Removing favorite link from" << folder << "to" << linkName;
+    if (!QFile::remove(linkName)) {
+        qCWarning(lcUtility) << "Removing a favorite link from" << folder << "to" << linkName << "failed.";
+    }
+}
+
 bool hasSystemLaunchOnStartup_private(const QString &appName)
 {
     QString runPath = QLatin1String(systemRunPathC);
index 76cf0cc39f28ec8a8d07fffd35659eaf1bbd6b63..a0ef19f1cdade6b3ad367b6841629cfb88d10f55 100644 (file)
@@ -761,6 +761,7 @@ void AccountSettings::slotRemoveCurrentFolder()
         messageBox->addButton(tr("Cancel"), QMessageBox::NoRole);
         connect(messageBox, &QMessageBox::finished, this, [messageBox, yesButton, folder, row, this]{
             if (messageBox->clickedButton() == yesButton) {
+                Utility::removeFavLink(folder->path());
                 FolderMan::instance()->removeFolder(folder);
                 _model->removeRow(row);