void GenerateSidecarFilesImpl(DirectoryInfo current, IconReference parentIcon, bool previewMode, bool recursive = true) { var icon = DesktopIniParser.GetIcon(current.FullName); var infoFile = new FolderInfoFile(current); var info = infoFile.Object; var iconInfo = infoFile.Object.Icon; //IconReference finalIcon; //var isNew = GetCurrentIconFromIni(current, icon, parentIcon, iconInfo, out finalIcon); var finalIcon = icon; var isNew = (icon.Resource != parentIcon.Resource && !icon.IsEmpty); if (isNew) { ReportProgress(0, $"Setting Main Icon To <<LINK:{finalIcon.Info}::500>> for <<LINK:{current.FullName}::800>> [<<LINK:{parentIcon.Info}>>]"); //ReportStatus($"Setting Main Icon To [LINK:{icon.Info}::500] for [LINK:{current.FullName}::800] [Parent: [LINK:{parentIcon.Info}]]"); if (!previewMode) { info.Icon.Main = finalIcon.Resource; infoFile.Save(); } } if (!recursive) { return; } foreach (var child in current.EnumerateDirectoriesSafe()) { GenerateSidecarFilesImpl(child, finalIcon.IsEmpty ? parentIcon : finalIcon, previewMode); } }
private void bgwCommand_DoWork(FolderIconCommandArguments arguments) { var parent = arguments.Directory.Parent; switch (arguments.Command) { case FolderIconCommand.GenerateSidecarFiles: var parentIcon = DesktopIniParser.GetIcon(parent.FullName); GenerateSidecarFilesImpl(arguments.Directory, parentIcon, arguments.PreviewMode, arguments.Recursive); break; case FolderIconCommand.SetIconTree: SetIconTreeImpl(arguments); break; case FolderIconCommand.SetIconsAuto: SetIconAutoImpl(arguments); break; case FolderIconCommand.FixAttributes: case FolderIconCommand.ApplyFolderIcons: var parentInfo = FolderIconInfo.Get(parent.FullName); ApplyFolderIconsImpl(arguments.Directory, parentInfo, arguments.PreviewMode, arguments.Command == FolderIconCommand.FixAttributes, arguments.Recursive); break; } }
bool SetIconAutoSingleImpl(DirectoryInfo current, FolderIconCommandArguments options) { if (!current.Exists && options.CreateDirectories) { current.Create(); } var paths = GetRelativePaths(current).ToArray(); var label = paths.FirstOrDefault()?.SubPath; var altLabel = Roots.GetAlternateRootLabels(label).FirstOrDefault(); var labels = string.IsNullOrWhiteSpace(label) ? new string[0] : Paths.Split(label); var name = labels.FirstOrDefault(x => x.ToUpperInvariant() != x && x.Length > 4); var icon = GetCurrentIcon(current); if (icon.IsEmpty || !icon.Exists) { ReportError($"Could not find Folder Icon for <<LINK:{current.FullName}::800>>"); return(false); } var newIcon = icon.ChangeDirectory(current); var ini = new DesktopIniParser(current) { IconResource = newIcon }; var depth = current.GetDepth(options.Root); var verbosity = Math.Min(MAX_VERBOSITY - 1, depth + 1); if (options.LastIcon.FullName != icon.FullName) { verbosity = Math.Min(verbosity, 1); } if (label?.IndexOf(Paths.DirectorySeparatorChar) == -1) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 2); } if (altLabel != null) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 1); } if (name == null || newIcon.FullName.Contains(name)) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 1); } if (newIcon.FullName.Contains("..\\")) { verbosity = Math.Min(verbosity, 2); } ReportProgress(0, new ReportedStatus($"Setting Folder Icon To <<LINK:{newIcon.Resource}||{newIcon.FullName}::500>> for <<LINK:{current.FullName}::800>> [<<LINK:{icon.Info}>>]", options.Command, current.FullName, newIcon.Resource, verbosity: verbosity)); var success = ini.Save(); if (!success) { ReportError($"Unable to save INI for {current.FullName}"); } options.LastIcon = icon; return(true); }
void ApplyFolderIconsImpl(DirectoryInfo current, IconReference parentIcon, bool previewMode, bool fixAttributesOnly = false, bool recursive = true) { var iniFile = new DesktopIniParser(current.FullName); var icon = iniFile.Icon; var iconInfo = FolderIconInfo.Get(current.FullName); //IconReference finalIcon; //var isNew = GetCurrentIcon(current, icon, parentIcon, iconInfo, out finalIcon); if (parentIcon.IsEmpty) { var parentDirectory = current.IsRoot() ? current : current.Parent; parentIcon = new DesktopIniParser(parentDirectory.FullName).Icon; } var finalIcon = GetCurrentIconFromSidecar(current, iconInfo, parentIcon); if (!fixAttributesOnly && !finalIcon.IsEmpty && iniFile.Icon.Resource != finalIcon.Resource) { ReportProgress(0, $"<<LINK:{finalIcon.Resource}||{finalIcon.Icon.FullName}::500>>\t==>\t<<LINK:{current.FullName}::800>> [<<LINK:{icon.Info}>>]"); //ReportStatus($"Setting Main Icon To [LINK:{icon.Info}::500] for [LINK:{current.FullName}::800] [Parent: [LINK:{parentIcon.Info}]]"); if (!previewMode) { iniFile.IconResource = finalIcon; iniFile.Save(); } } else { var thisFile = iniFile.Ini; var verified = thisFile.VerifyHiddenSystem(!previewMode); verified &= thisFile.Directory.VerifySystem(!previewMode); verified &= FolderInfoFile.GetFile(thisFile.Directory.FullName, SidecarType.Main).VerifyHiddenSystem(!previewMode); if (!verified) { ReportProgress(0, $"*** Fixing desktop.ini attributes for <<LINK:{current.FullName}::1200>> [<<LINK:{icon.Info}>>]"); } } if (!recursive) { return; } foreach (var child in current.EnumerateDirectoriesSafe()) { ApplyFolderIconsImpl(child, finalIcon.IsEmpty ? parentIcon : finalIcon, previewMode, fixAttributesOnly); } }
DesktopIniParser GetCurrentIconFromRoots(DirectoryInfo current) { string label; while (true) { var paths = GetRelativePaths(current); foreach (var path in paths) { label = path.SubPath; if (string.IsNullOrWhiteSpace(label)) { continue; } var ini = new DesktopIniParser(Paths.Combine(path.FullName, "desktop.ini")); if (ini.Ini.Exists) { return(ini); } } if (current.IsRoot()) { break; } current = current.Parent; } //var roots = new[] { Roots.Icons, Roots.Content, Roots.Action, current.GetExistingPath() }; //var relative = current.ParseRelativePath(this.Roots.Action); //label = relative.SubPath; //while (true) //{ // if (string.IsNullOrWhiteSpace(label)) // break; // foreach (var root in roots) // { // if (root?.Exists != true) // continue; // var ini = new DesktopIniParser(Paths.Combine(root.FullName, label)); // if (ini.Ini.Exists) // return ini; // } // label = Paths.GetDirectoryName(label); //} return(null); }
DesktopIniParser GetCurrentIconFromDirectoryTree(DirectoryInfo current) { //var relative = current.CreateRelativePath(Roots.GetActionRoot(current).FullName); //var label = relative.SubPath; while (true) { var ini = new DesktopIniParser(current); if (ini.Ini.Exists) { return(ini); } if (current.IsRoot()) { return(null); } current = current.Parent; } }
// ReSharper disable once UnusedMethodReturnValue.Local bool SetIconAutoSingleImpl(DirectoryInfo current, FolderIconCommandArguments options) { if (!current.Exists && options.CreateDirectories) { current.Create(); } var paths = GetRelativePaths(current, Roots.Action, 100).ToArray(); var label = paths.FirstOrDefault()?.Directory.SubPath; var altLabel = Roots.GetAlternateRootLabels(label).FirstOrDefault(); var labels = string.IsNullOrWhiteSpace(label) ? new string[0] : Paths.Split(label); var name = labels.FirstOrDefault(x => x.ToUpperInvariant() != x && x.Length > 4); var result = GetCurrentIcon(current); var icon = result.Icon; if (icon.IsEmpty || !icon.Exists) { ReportError($"Could not find Folder Icon for <<LINK:{current.FullName}::800>>"); return(false); } var newIcon = icon.ChangeDirectory(current); var ini = new DesktopIniParser(current); var oldIcon = ini.Icon; ini.IconResource = newIcon; var depth = current.GetDepth(options.Root); var verbosity = Math.Min(MAX_VERBOSITY - 1, depth + NEW_ICON_VERBOSITY); if (options.LastIcon.FullName != icon.FullName) { verbosity = Math.Min(verbosity, NEW_ICON_VERBOSITY); } if (label?.IndexOf(Paths.DirectorySeparatorChar) == -1) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 2); } if (altLabel != null) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 1); } if (name == null || newIcon.FullName.Contains(name)) { verbosity = Math.Min(verbosity, MANUAL_VERBOSITY - 1); } if (newIcon.FullName.Contains("..\\")) { verbosity = Math.Min(verbosity, NEW_ICON_VERBOSITY + 1); } if (Settings.Toggles.ReportChanges) { var oldIconPath = ResolvedPath.Resolve(oldIcon.FullName); var newIconPath = ResolvedPath.Resolve(newIcon.FullName); if (!oldIconPath.Resolved.Equals(newIconPath.Resolved, StringComparison.OrdinalIgnoreCase)) { verbosity = Math.Min(verbosity, verbosity > NEW_ICON_VERBOSITY ? 2 : 1); } } var message = $"{ReportedStatusElement.GetLink(newIcon.FullName, newIcon.Resource, 700, 115)}\t==>\t{ReportedStatusElement.GetLink(current.FullName, width: 700)} [{ReportedStatusElement.GetLink(icon.Info)}]"; ReportProgress(0, new ReportedStatus(message, options.Command, current.FullName, newIcon.Resource, verbosity: verbosity)); var success = options.PreviewMode || ini.Save(); if (!success) { ReportError($"Unable to save INI for {current.FullName}"); } options.LastIcon = icon; return(true); }