//
// DirectoryAlbumArtwork.cs
//
// Author:
// Jacob Johnston (jacobj@inchoatethoughts.com)
//
// Copyright (C) 2009 Jacob Johnston
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
using System;
using System.Drawing;
using System.IO;
using ZenLibrary.RuleBase;
namespace ZenLibrary.Rules
{
///
/// This rule will scan the directory and try to find a match among
/// a specified list of file names (or search strings) that indicate
/// the presence of album artwork. If it finds one or more of these files,
/// the files will be scanned to see if they match the specified minimum
/// image size requirements.
///
class DirectoryAlbumArtwork : ConfigurableRule
{
private int artMinHeight = 500;
private int artMinWidth = 500;
private string artFileCombinedString = "Folder.JPG;Folder.JPEG;Folder.PNG";
public override string Name
{
get { return "Directory Has Album Artwork"; }
}
public override void Configure()
{
char[] splitChars = {';'};
DirectoryAlbumArtworkConfig configWindow = new DirectoryAlbumArtworkConfig();
configWindow.ArtHeightText.Text = artMinHeight.ToString();
configWindow.ArtWidthText.Text = artMinWidth.ToString();
string[] artFiles = artFileCombinedString.Split(splitChars);
foreach(string file in artFiles)
configWindow.FileList.Items.Add(file);
if (configWindow.ShowDialog() == true)
{
artMinHeight = int.Parse(configWindow.ArtHeightText.Text);
artMinWidth = int.Parse(configWindow.ArtWidthText.Text);
artFileCombinedString = string.Empty;
foreach (string file in configWindow.FileList.Items)
{
artFileCombinedString += file + ";";
}
}
Config.Instance.SaveConfig();
}
public override TestType TestType
{
get { return TestType.DirectoryScan; }
}
protected override void WriteCustomConfigAttributes(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("ArtHeight", artMinHeight.ToString());
writer.WriteAttributeString("ArtWidth", artMinWidth.ToString());
writer.WriteAttributeString("ArtFiles", artFileCombinedString);
base.WriteCustomConfigAttributes(writer);
}
protected override void ReadCustomConfigAttributes(System.Xml.XmlReader reader)
{
artMinHeight = int.Parse(reader["ArtHeight"]);
artMinWidth = int.Parse(reader["ArtWidth"]);
artFileCombinedString = reader["ArtFiles"];
base.ReadCustomConfigAttributes(reader);
}
public override RuleTestResult RunTest(DirectoryInfo directoryInfo, FileInfo fileInfo)
{
bool foundImage = false;
bool imageMeetsSizeRequirements = false;
int artWidth = 0;
int artHeight = 0;
char[] splitChars = {';'};
string[] artFiles = artFileCombinedString.Split(splitChars);
foreach (string artFileSearchString in artFiles)
{
FileInfo[] files = directoryInfo.GetFiles(artFileSearchString);
if (files.Length > 0)
{
foundImage = true;
foreach (FileInfo file in files)
{
using (Image image = Image.FromFile(file.FullName))
{
artWidth = Math.Max(artWidth, image.Width);
artHeight = Math.Max(artHeight, image.Height);
if (image.Width >= artMinWidth && image.Height >= artMinHeight)
{
imageMeetsSizeRequirements = true;
break;
}
}
}
if (imageMeetsSizeRequirements)
break;
}
}
if (foundImage && imageMeetsSizeRequirements)
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = true;
return result;
}
else if (foundImage && !imageMeetsSizeRequirements)
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = false;
result.ResultPath = directoryInfo.FullName;
result.RuleTestFailedString = string.Format("Directory \"{0}\" contains album artwork, but is only {1} x {2} pixels in size.", result.ResultPath, artWidth, artHeight);
return result;
}
else
{
RuleTestResult result = new RuleTestResult();
result.TestPassed = false;
result.ResultPath = directoryInfo.FullName;
result.RuleTestFailedString = string.Format("Directory \"{0}\" does not contain album artwork.", result.ResultPath);
return result;
}
}
}
}