public static void UpdateShortcut(ItemFile Shortcut) { Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object dynamic shell = Activator.CreateInstance(t); var TargetPath = Shortcut.FilePath.GetShortcutPath(); var IconPath = Shortcut.Match == null?Shortcut.FilePath.GetShortcutPath() : Shortcut.Match.FilePath.Substring(Data.Session.O_DriveDependant ? 0 : 2); if (IconPath == "") { return; } try { var lnk = shell.CreateShortcut(Shortcut.FilePath); try { lnk.TargetPath = TargetPath; lnk.IconLocation = IconPath; lnk.Save(); } finally { Marshal.FinalReleaseComObject(lnk); } } finally { Marshal.FinalReleaseComObject(shell); } }
public static Exception ReMatch(ItemFile file) { try { file.Match = GetIconBasedOnContent(file); if (file.Match != null) { return(null); } var parents = file.Parents.Trim(0, 3); foreach (var parent in parents) { file.Match = Data.Session.Folders.FirstThat(x => x.Match != null && parent == Path.GetFullPath(x.FilePath))?.Match.Folder; if (file.Match != null) { return(null); } file.Match = GetFolderIcon(parent)?.Folder; if (file.Match != null) { return(null); } } } catch (Exception ex) { return(ex); } return(null); }
private static bool ParentNameCheck(ItemFile file, string[] querry, int depth) { if (file.Type == IconType.Normal) { depth++; } var parents = file.Parents.Trim(0, depth).Convert(x => Path.GetFileName(x).ToLower()); return(parents.Any(x => querry.Any(y => y == x || IconMatcher.SpellCheck(x, y) < 3 || x.Contains(y)))); }
private static bool FileContainCheck(ItemFile file, IconFile icon) { foreach (var format in icon.Formats) { if (format.LowerCase.Length >= GENERAL_MIN_CHAR_LENGTH && Regex.IsMatch(format.LowerCase, "[a-z]") && file.Formats.Any(x => x.LowerCase.GetWords().Length <= GENERAL_MIN_CHAR_LENGTH && x.LowerCase.ContainsWord(format.LowerCase))) { return(true); } } return(false); }
public static Exception SetIcon(ItemFile itemFile) { try { if (itemFile.FilePath.EndsWith(".lnk")) { UpdateShortcut(itemFile); } else { UpdateFolder(itemFile); } } catch (Exception ex) { return(ex); } return(null); }
private static int SimilarCheck(ItemFile file, IconFile test) { var s1 = file.Formats[0].LowerCase; var s2 = test.Formats[0].LowerCase; if (s1.Length > s2.Length) { s1.Substring(0, s2.Length); } else { s2.Substring(0, s1.Length); } if (s1.Length <= GENERAL_MIN_CHAR_LENGTH) { return(0); } return(SPELLCHECK_MAX_ERRORS - SpellCheck(s1, s2)); }
public static void UpdateFolder(ItemFile Folder) { Folder.Match = Folder.Match ?? Data.Session.FolderIcon; var iniPath = Path.Combine(Folder.FilePath, "desktop.ini"); if (Folder.Match == null) { return; } if (File.Exists(iniPath)) { //remove hidden and system attributes to make ini file writable File.SetAttributes( iniPath, File.GetAttributes(iniPath) & ~(FileAttributes.Hidden | FileAttributes.System)); } //create new ini file with the required contents var iniContents = $"[.ShellClassInfo]\r\n" + $"IconResource = {Folder.Match.FilePath.Substring(Data.Session.O_DriveDependant ? 0 : 2)},0\r\n" + $"[ViewState]\r\n" + $"Mode =\r\n" + $"Vid =\r\n" + $"FolderType = Generic"; File.WriteAllText(iniPath, iniContents); //hide the ini file and set it as system File.SetAttributes( iniPath, File.GetAttributes(iniPath) | FileAttributes.Hidden | FileAttributes.System); //set the folder as system File.SetAttributes( Folder.FilePath, File.GetAttributes(Folder.FilePath) | FileAttributes.System); }
private static IconFile GetIconBasedOnContent(ItemFile folder, int?limit = null) { var Files = Directory.GetFiles(folder.FilePath.EndsWith(".lnk") ? folder.FilePath.GetShortcutPath() : folder.FilePath, "*.*", SearchOption.TopDirectoryOnly); var Limit = (int)(limit != null ? Math.Max((int)limit, Files.Count() * 85 / 100) : Math.Max(3, Files.Count() * .35)); var CatCount = new Dictionary <FileTypeCategory, int>(); foreach (var cat in FileTypeCategory.Categories) { CatCount.Add(cat, 0); } foreach (var file in Files) { var ext = Path.GetExtension(file).ToLower(); if (ext == ".lnk" && !file.IsFolder()) { ext = Path.GetExtension(file.GetShortcutPath()).ToLower(); } foreach (var cat in FileTypeCategory.Categories) { if (cat.Extensions.Any(x => x == ext)) { CatCount[cat]++; break; } } } var max = CatCount.FirstThat(y => y.Value == CatCount.Max(x => x.Value)); if (max.Value >= Limit) { return(Data.Session.Icons.FirstThat(x => x.Categories.Contains(IconCategory.Category) && x.Type == folder.Type && x.Name == max.Key.IconName)); } return(null); }
public static IconCategory[] GetCategories(ItemFile file) { var Out = new List <IconCategory>(); if (ParentNameCheck(file, new string[] { "photoshop" }, 1)) { Out.Add(IconCategory.Photoshop); } if (ParentNameCheck(file, new string[] { "movies", "films" }, 2)) { Out.Add(IconCategory.Movie); } if (ParentNameCheck(file, new string[] { "series", "tv series" }, 2)) { Out.Add(IconCategory.TV); } if (ParentNameCheck(file, new string[] { "anime" }, 2)) { Out.Add(IconCategory.Anime); } if (ParentNameCheck(file, new string[] { "games", "gaming" }, 2)) { Out.Add(IconCategory.Game); } if (ParentNameCheck(file, new string[] { "application", "apps" }, 1)) { Out.Add(IconCategory.Application); } return(Out.Count == 0 ? new IconCategory[] { IconCategory.Any } : Out.ToArray()); }
public static Exception Match(ItemFile file) { try { var workingIcons = Data.Session.Icons .Where(x => x.Type == file.Type && (file.Categories.Any(x.Categories) || (file.Categories.IsOnly(IconCategory.Any) && !x.Categories.IsOnly(IconCategory.Photoshop) && !x.Categories.Contains(IconCategory.Season)))).OrderByDescending(x => x.Categories.Length).ToArray(); // Exact Match file.Match = workingIcons.FirstThat(x => file.Formats.Compare(x.Formats, FormatComparer.LowerCase, FormatComparer.LowerCase)); if (file.Match != null) { goto End; } // Exact Match without spaces file.Match = workingIcons.FirstThat(x => file.Formats.Compare(x.Formats, FormatComparer.LowerCaseNoSpace, FormatComparer.LowerCaseNoSpace)); if (file.Match != null) { goto End; } // Spell Check file.Match = workingIcons.Convert(x => new Tuple <IconFile, int>(x, SpellCheck(x.Formats, file.Formats))) .Where(x => x.Item2 > 0).OrderByDescending(x => x.Item2).FirstOrDefault()?.Item1; if (file.Match != null) { goto End; } // Icon Contains File Check file.Match = workingIcons.FirstThat(x => FileContainCheck(file, x)); if (file.Match != null) { goto End; } // File Contains Icon Check file.Match = workingIcons.FirstThat(x => IconContainCheck(x, file)); if (file.Match != null) { goto End; } // Similar Words file.Match = workingIcons.Convert(x => new Tuple <IconFile, int>(x, WordCheck(x.Formats[0].LowerCase, file.Formats[0].LowerCase))) .Where(x => x.Item2 > GENERAL_MIN_CHAR_LENGTH).OrderByDescending(x => x.Item2).FirstOrDefault()?.Item1; if (file.Match != null) { goto End; } // Abbreviation Check file.Match = workingIcons .FirstThat(x => x.Formats.Any(f1 => f1.Abbreviation.Length >= GENERAL_MIN_CHAR_LENGTH) && x.Formats.Compare(file.Formats, FormatComparer.Abbreviation, FormatComparer.LowerCaseWords) || (file.Formats.Any(f2 => f2.Abbreviation.Length >= GENERAL_MIN_CHAR_LENGTH) && file.Formats.Compare(x.Formats, FormatComparer.Abbreviation, FormatComparer.LowerCaseWords))); if (file.Match != null) { goto End; } // Similar Starting file.Match = workingIcons.Convert(x => new Tuple <IconFile, int>(x, SimilarCheck(file, x))) .Where(x => x.Item2 > 1).OrderByDescending(x => x.Item2).FirstOrDefault()?.Item1; if (file.Match != null) { goto End; } } catch (Exception ex) { return(ex); } End : return(null); }