@@ -8,8 +8,8 @@ public static class DesktopShortcutsDatabase
88 public enum Status
99 {
1010 Maintain , // The user has explicitly requested this shortcut not be deleted
11- Delete , // The user has allowed the shortcut to be deleted
1211 Unknown , // The user has not said whether they want this shortcut to be deleted
12+ Delete , // The user has allowed the shortcut to be deleted
1313 }
1414
1515 private static readonly List < string > UnknownShortcuts = [ ] ;
@@ -28,10 +28,13 @@ public static void ResetDatabase()
2828 /// Adds a desktop shortcut to the deletable desktop shortcuts database
2929 /// </summary>
3030 /// <param name="shortcutPath">The path of the shortcut to delete</param>
31- /// <param name="deletable">Whether or not to mark this entry as deletable in the database. Defaults to true </param>
32- public static void AddToDatabase ( string shortcutPath , bool deletable = true )
31+ /// <param name="shortcutStatus">The status to set </param>
32+ public static void AddToDatabase ( string shortcutPath , Status shortcutStatus )
3333 {
34- Settings . SetDictionaryItem ( "DeletableDesktopShortcuts" , shortcutPath , deletable ) ;
34+ if ( shortcutStatus is Status . Unknown )
35+ Settings . RemoveDictionaryKey < string , bool > ( "DeletableDesktopShortcuts" , shortcutPath ) ;
36+ else
37+ Settings . SetDictionaryItem < string , bool > ( "DeletableDesktopShortcuts" , shortcutPath , shortcutStatus is Status . Delete ) ;
3538 }
3639
3740 /// <summary>
@@ -54,28 +57,6 @@ public static bool Remove(string shortcutPath)
5457 return false ;
5558 }
5659
57- /// <summary>
58- /// Attempts to reset the configuration of a given shortcut path from the database.
59- /// This will make it so the user is asked about it the next time it is discovered.
60- /// Different from `Remove` as Remove simply marks it as non-deletable, whereas this removes the configuration entirely.
61- /// </summary>
62- /// <param name="shortcutPath">The path of the shortcut to delete</param>
63- /// <returns>True if the shortcut was completely removed, false if it was not there from the beginning</returns>
64- public static bool ResetShortcut ( string shortcutPath )
65- {
66- // Remove the entry if present
67- if ( Settings . DictionaryContainsKey < string , bool > ( "DeletableDesktopShortcuts" , shortcutPath ) )
68- {
69- // Remove the entry and propagate changes to disk
70- Settings . RemoveDictionaryKey < string , bool > ( "DeletableDesktopShortcuts" , shortcutPath ) ;
71- return true ;
72- }
73-
74- // Do nothing if the entry was not there
75- Logger . Warn ( $ "Attempted to reset a deletable desktop shortcut {{shortcutPath={ shortcutPath } }} that was not found there") ;
76- return false ;
77- }
78-
7960 /// <summary>
8061 /// Attempts to delete the given shortcut path off the disk
8162 /// </summary>
@@ -103,7 +84,7 @@ public static bool DeleteFromDisk(string shortcutPath)
10384 /// until a choice is given to the user and they explicitly request that it be deleted.
10485 /// </summary>
10586 /// <param name="shortcutPath">The path of the shortcut to be deleted</param>
106- /// <returns>True if the package is ignored, false otherwhise </returns>
87+ /// <returns>The status of a shortcut </returns>
10788 public static Status GetStatus ( string shortcutPath )
10889 {
10990 // Check if the package is ignored
@@ -120,7 +101,7 @@ public static Status GetStatus(string shortcutPath)
120101 /// Get a list of shortcuts (.lnk files only) currently on the user's desktop
121102 /// </summary>
122103 /// <returns>A list of desktop shortcut paths</returns>
123- public static List < string > GetShortcuts ( )
104+ public static List < string > GetShortcutsOnDisk ( )
124105 {
125106 List < string > shortcuts = [ ] ;
126107 string UserDesktop = Environment . GetFolderPath ( Environment . SpecialFolder . DesktopDirectory ) ;
@@ -130,6 +111,23 @@ public static List<string> GetShortcuts()
130111 return shortcuts ;
131112 }
132113
114+ /// <summary>
115+ /// Gets all the shortcuts exist on disk or/and on the database
116+ /// </summary>
117+ /// <returns></returns>
118+ public static List < string > GetAllShortcuts ( )
119+ {
120+ var shortcuts = GetShortcutsOnDisk ( ) ;
121+
122+ foreach ( var item in Settings . GetDictionary < string , bool > ( "DeletableDesktopShortcuts" ) )
123+ {
124+ if ( ! shortcuts . Contains ( item . Key ) )
125+ shortcuts . Add ( item . Key ) ;
126+ }
127+
128+ return shortcuts ;
129+ }
130+
133131 /// <summary>
134132 /// Remove a shortcut from the list of shortcuts whose deletion verdicts are unknown (as in, the user needs to be asked about deleting them when their operations finish)
135133 /// </summary>
@@ -150,29 +148,52 @@ public static List<string> GetUnknownShortcuts()
150148 }
151149
152150 /// <summary>
153- /// Will attempt to remove new desktop shortcuts , if applicable.
151+ /// Will handle the removal , if applicable, of any shortcut that is not present on the given PreviousShortCutList .
154152 /// </summary>
155- /// <param name="PreviousShortCutList"></param>
156- public static void TryRemoveNewShortcuts ( IReadOnlyList < string > PreviousShortCutList )
153+ /// <param name="PreviousShortCutList">The shortcuts that already existed </param>
154+ public static void HandleNewShortcuts ( IReadOnlyList < string > PreviousShortCutList )
157155 {
158- HashSet < string > ShortcutSet = PreviousShortCutList . ToHashSet ( ) ;
159- List < string > CurrentShortcutList = DesktopShortcutsDatabase . GetShortcuts ( ) ;
160- foreach ( string shortcut in CurrentShortcutList )
156+ bool DeleteUnknownShortcuts = Settings . Get ( "RemoveAllDesktopShortcuts" ) ;
157+ HashSet < string > PreviousShortcuts = [ .. PreviousShortCutList ] ;
158+ List < string > CurrentShortcuts = GetShortcutsOnDisk ( ) ;
159+
160+ foreach ( string shortcut in CurrentShortcuts )
161161 {
162- if ( ShortcutSet . Contains ( shortcut ) ) continue ;
163- switch ( DesktopShortcutsDatabase . GetStatus ( shortcut ) )
162+ var status = GetStatus ( shortcut ) ;
163+ if ( status is Status . Maintain )
164+ {
165+ // Don't delete this shortcut, it has been set to be kept
166+ }
167+ else if ( status is Status . Delete )
168+ {
169+ // If a shortcut is set to be deleted, delete it,
170+ // even when it was not created during an UniGetUI operation
171+ DeleteFromDisk ( shortcut ) ;
172+ }
173+ else if ( status is Status . Unknown )
164174 {
165- case Status . Delete :
166- DesktopShortcutsDatabase . DeleteFromDisk ( shortcut ) ;
167- break ;
168- case Status . Maintain :
169- Logger . Debug ( "Refraining from deleting new shortcut " + shortcut + ": user disabled its deletion" ) ;
170- break ;
171- case Status . Unknown :
172- if ( UnknownShortcuts . Contains ( shortcut ) ) continue ;
173- Logger . Info ( "Marking the shortcut " + shortcut + " to be asked to be deleted" ) ;
174- UnknownShortcuts . Add ( shortcut ) ;
175- break ;
175+ if ( DeleteUnknownShortcuts )
176+ {
177+ // If a shortcut has not been detected yet, and it
178+ // existed before an operation started, then do nothing.
179+ if ( PreviousShortcuts . Contains ( shortcut ) )
180+ continue ;
181+
182+ // If the shortcut was created during an operation
183+ // and autodelete is enabled, delete that icon
184+ Logger . Warn ( $ "New shortcut { shortcut } will be set for deletion (this shortcut was never seen before)") ;
185+ AddToDatabase ( shortcut , Status . Delete ) ;
186+ DeleteFromDisk ( shortcut ) ;
187+ }
188+ else
189+ {
190+ // Mark the shortcut as unknown and prompt the user.
191+ if ( ! UnknownShortcuts . Contains ( shortcut ) )
192+ {
193+ Logger . Info ( $ "Marking the shortcut { shortcut } to be asked to be deleted") ;
194+ UnknownShortcuts . Add ( shortcut ) ;
195+ }
196+ }
176197 }
177198 }
178199 }
0 commit comments