public static int CleanUpOriginalWallpaper() { var count = 0; var originalFileList = OriginalWallpaperDirectory.GetFiles("*", SearchOption.AllDirectories); foreach (var currentOriginalFile in originalFileList) { if (WallpaperFileHelper.IsImage(currentOriginalFile)) { bool fileFound = false; foreach (var currentWallpaper in Wallpapers) { if (WallpaperFileHelper.ImageFilesMatch(currentWallpaper.File, currentOriginalFile)) { fileFound = true; break; } } if (!fileFound) { currentOriginalFile.MoveTo(Path.Combine(TrashDirectory.FullName, currentOriginalFile.Name)); count++; } } } return(count); }
internal static List <FileInfo> SortOutOrignals() { var movedFiles = new List <FileInfo>(); //Go through all original files and see if they are not listed in the other folder. (O(n)) var allOriginalFiles = OriginalWallpaperDirectory.GetFiles("*", SearchOption.AllDirectories); foreach (var currentFile in allOriginalFiles) { if (WallpaperFileHelper.IsImage(currentFile)) { if (currentFile.Name.Contains("#o")) { //Check if there is a corresponding wallpaper file var correspondingFileName = Path.GetFileNameWithoutExtension(currentFile.FullName).Replace("#o", "#"); var files = new[] { $"{correspondingFileName}.png", //Test for png $"{correspondingFileName}.jpg", //Test for jpg }; var foundFile = files.FirstOrDefault(file => System.IO.File.Exists(Path.Combine(WallpaperDirectory.FullName, currentFile.Directory.Name, file))); //Find the first path that exits if (foundFile is null) { //Move file, it does not belong here currentFile.MoveTo(Path.Combine(TrashDirectory.FullName, currentFile.Name)); movedFiles.Add(currentFile); } } } } return(movedFiles); }
public void SetIndexAndCorrectFiles(int newIndex) { Index = newIndex; WallpaperFileHelper.RenameFile(File, $"{Franchise.Name}#{Index}"); if (!(OriginalFile is null)) { WallpaperFileHelper.RenameFile(OriginalFile, $"{Franchise.Name}#o{Index}"); } }
public static void FullScan() { var allFiles = WallpaperDirectory.GetFiles("*", SearchOption.AllDirectories); foreach (var currentFile in allFiles) { if (WallpaperFileHelper.IsImage(currentFile)) { if (!currentFile.Name.Contains("#o")) { _ = new Wallpaper(currentFile, WallpaperCreationMode.Scan); } } } Wallpapers.Sort(); }
public static Tuple <int, int> Merge() { int countDeleted = 0; int countCopied = 0; if (false) //Environment.OSVersion.Platform == PlatformID.Win32NT { throw new NotImplementedException(); //Implement faster method of coping files here } else { //Delete all old files foreach (var currentFile in MergedWallpaperDirectory.GetFiles()) { if (WallpaperFileHelper.IsImage(currentFile)) { currentFile.Delete(); countDeleted++; } } //Get excluded types WallpaperType excludedType; if (WallpaperType.TryGet("X-MAS", out WallpaperType? type)) { excludedType = type; } else { throw new ArgumentNullException("Franchise not found.", nameof(excludedType)); } //Copy all wallpapers that are not excluded foreach (var currentWallpaper in Wallpapers) { if (currentWallpaper.Franchise.Type != excludedType) { currentWallpaper.File.CopyTo(Path.Combine(MergedWallpaperDirectory.FullName, currentWallpaper.File.Name)); countCopied++; } } } return(new Tuple <int, int>(countDeleted, countCopied)); }
public static List <Wallpaper> SortInNamedWorkingWallpapers() { var newWallpapers = new List <Wallpaper>(); var typeDirectories = WorkingDirectoryNamed.GetDirectories(); foreach (var currentTypeDirectory in typeDirectories) { var currentFiles = currentTypeDirectory.GetFiles("*", SearchOption.TopDirectoryOnly); foreach (var currentFile in currentFiles) //Go through the current type images { if (WallpaperFileHelper.IsImage(currentFile)) //Make sure they are an image { WallpaperFileHelper.FixImageExtension(currentFile); //not an original file if (!currentFile.Name.Contains("#o")) { Wallpaper newWallpaper; try { newWallpaper = new Wallpaper(currentFile, WallpaperCreationMode.New); } catch (WallpaperBuildExeption e) { if (e.UserAbortion) { continue; } else { throw e; } } //Move it to the right location newWallpaper.File.MoveTo(Path.Combine(WallpaperDirectory.FullName, newWallpaper.Franchise.Type.Name, $"{newWallpaper.Franchise.Name}#{newWallpaper.Index}{newWallpaper.File.Extension}")); if (!(newWallpaper.OriginalFile is null)) { newWallpaper.OriginalFile !.MoveTo(Path.Combine(OriginalWallpaperDirectory.FullName, newWallpaper.Franchise.Type.Name, $"{newWallpaper.Franchise.Name}#o{newWallpaper.Index}{newWallpaper.File.Extension}")); } newWallpapers.Add(newWallpaper); } } } //Now look for grouped images in franchise folders var currentTypeSubDirectories = currentTypeDirectory.GetDirectories(); foreach (var currentTypeSubDiretory in currentTypeSubDirectories) { var files = currentTypeSubDiretory.GetFiles("*", SearchOption.TopDirectoryOnly); foreach (var currentFile in files) { if (WallpaperFileHelper.IsImage(currentFile)) { WallpaperFileHelper.FixImageExtension(currentFile); //Check if this file is not an original. if (Regex.IsMatch(currentFile.Name, WallpaperManager.EditedFilesRegexString)) { Wallpaper newWallpaper; try { newWallpaper = new Wallpaper(currentFile, WallpaperCreationMode.NewInSubFolder); } catch (WallpaperBuildExeption e) { if (e.UserAbortion) { continue; } else { throw e; } } //Move it to the right location newWallpaper.File.MoveTo(Path.Combine(WallpaperDirectory.FullName, newWallpaper.Franchise.Type.Name, $"{newWallpaper.Franchise.Name}#{newWallpaper.Index}{newWallpaper.File.Extension}")); if (!(newWallpaper.OriginalFile is null)) { newWallpaper.OriginalFile !.MoveTo(Path.Combine(OriginalWallpaperDirectory.FullName, newWallpaper.Franchise.Type.Name, $"{newWallpaper.Franchise.Name}#o{newWallpaper.Index}{newWallpaper.File.Extension}")); } newWallpapers.Add(newWallpaper); } } } } } return(newWallpapers); }
//New Powerfull C'tor public Wallpaper(FileInfo file, WallpaperCreationMode mode) { WallpaperDataContainer container; if (!file.Exists) { throw new FileNotFoundException("File was not found."); } if (!WallpaperFileHelper.IsImage(file)) { throw new ArgumentException("Is not an image file.", nameof(file)); } //All parameters that have to be set. string typeName; string franchiseName; bool ignoreMissing; int? index; DirectoryInfo originalLookUpDirectory; string[] possibleOriginalFileNames; //Helpers //Setting the parameters. switch (mode) { case WallpaperCreationMode.Scan: // -------------------------------------------------------------------------------------------- if (file.Name.Contains("#o")) { throw new ArgumentException("Is an original file.", nameof(file)); } if (!Regex.IsMatch(file.Name, "\\S+#\\d+")) { throw new FileLoadException("Image file was not in the correct normalized format.", file.Name); } typeName = file.Directory.Name; var shortFileName1 = Path.GetFileNameWithoutExtension(file.Name); var splitShortFileName1 = shortFileName1.Split('#'); //The '#' separates franchise and index. franchiseName = splitShortFileName1[0]; ignoreMissing = true; index = Convert.ToInt32(splitShortFileName1[1]); originalLookUpDirectory = new DirectoryInfo(Path.Combine(WallpaperManager.OriginalWallpaperDirectory.FullName, typeName)); possibleOriginalFileNames = new[] { $"{franchiseName}#o{index}.png", //Test for png $"{franchiseName}#o{index}.jpg", //Test for jpg }; break; case WallpaperCreationMode.New: // -------------------------------------------------------------------------------------------- typeName = file.Directory.Name; var shortFileName2 = Path.GetFileNameWithoutExtension(file.Name); var splitShortFileName2 = shortFileName2.Split('#'); //The '#' separates franchise and index. if (splitShortFileName2.Length > 2) //Check if something has gone wrong while splitting { throw new ArgumentException("Is not in the correct format.", nameof(file)); } franchiseName = splitShortFileName2[0]; ignoreMissing = false; index = null; //Set a new index, the current one is just temporary originalLookUpDirectory = file.Directory; //Determine if there is a temp index given. In a not normalized format, an temp index is not mandatory. string fileNameIndex; if (splitShortFileName2.Length == 2) { if (Int32.TryParse(splitShortFileName2[1], out var _)) { fileNameIndex = splitShortFileName2[1]; } else { throw new ArgumentException("Could not parse index number.", nameof(file)); } } else //splitShortFileName2.Length == 1 { fileNameIndex = String.Empty; } possibleOriginalFileNames = new[] { $"{franchiseName}#o{fileNameIndex}.png", //Test for png $"{franchiseName}#o{fileNameIndex}.jpg", //Test for jpg }; break; case WallpaperCreationMode.NewInSubFolder: // -------------------------------------------------------------------------------------------- typeName = file.Directory.Parent.Name; franchiseName = file.Directory.Name; ignoreMissing = false; index = null; originalLookUpDirectory = file.Directory; var matchLength = Regex.Match(file.Name, WallpaperManager.EditedFilesRegexString).Value.Length; var originalFileNameWithoutExtension = file.Name.Substring(0, file.Name.Length - matchLength); possibleOriginalFileNames = new[] { $"{originalFileNameWithoutExtension}.png", //Test for png $"{originalFileNameWithoutExtension}.jpg", //Test for jpg }; break; default: throw new WallpaperBuildExeption($"Unknown {nameof(WallpaperCreationMode)}."); } try { container = ContructorHelper(file, typeName, franchiseName, ignoreMissing, index, originalLookUpDirectory, possibleOriginalFileNames); } catch (WallpaperBuildExeption e) { if (e.UserAbortion && !(e.ObjectsToDelte is null)) //Handle objects that need to be removed, when the process was aborted. { foreach (var currentObject in e.ObjectsToDelte) { if (currentObject is WallpaperType) { WallpaperManager.Types.Remove((WallpaperType)currentObject); } else { throw new ArgumentException($"Object {nameof(currentObject)} is an unexpected type."); } } } throw e; } File = file; OriginalFile = container.OriginalFile; Franchise = container.Franchise; Index = container.Index; WallpaperManager.Wallpapers.Add(this); //Add this instance to the list of all wallpapers. }