using System.Drawing;
using System.IO;
using TBO.UI.tboTK;
using XWolf.IO;
namespace TBO
{
class LibraryTBO
{
private string path;
public LibraryTBO(TBOFile tbo)
{
Title = tbo.Title;
Variant = tbo.Variant;
Volume = tbo.Volume;
Author = tbo.Author;
Publisher = tbo.Publisher;
Year = tbo.Year;
Comments = tbo.Comments;
Image = GenThumb(tbo);
path = tbo.FileName;
}
public LibraryTBO(FileStream fs)
{
path = fs.ReadString();
Title = fs.ReadString();
Variant = fs.ReadString();
Volume = fs.ReadString();
Author = fs.ReadString();
Publisher = fs.ReadString();
Year = fs.ReadInt32();
Comments = fs.ReadString();
Image = Image.FromStream(new MemoryStream(fs.ReadBlock()));
}
private Image GenThumb(TBOFile tbo)
{
Image image;
using (Image page = tbo.GetImage(0))
{
if (page == null)
return null;
image = new Bitmap(256, 256);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImageKeepRatioEx(page, 0, 0, image.Width, image.Height);
}
}
return image;
}
public TBOFile Open()
{
return new TBOFile(path);
}
#region I/O
public void CopyTo(string newPath)
{
File.Copy(path, newPath);
path = newPath;
}
public void WriteTo(FileStream fs)
{
string p = path;
if (p.StartsWith(Library.BasePath))
p = p.Substring(Library.BasePath.Length + 1);
fs.WriteString(p);
fs.WriteString(Title);
fs.WriteString(Variant);
fs.WriteString(Volume);
fs.WriteString(Author);
fs.WriteString(Publisher);
fs.WriteInt32(Year);
fs.WriteString(Comments);
fs.WriteBlock(ImageWorks.ImagePNG(Image));
}
#endregion
private string GetDisplayName()
{
if (Library.ValidValue(Title))
{
string dname = Title;
if (Library.ValidValue(Variant))
dname = dname + " (" + Variant.Trim() + ")";
if (Library.ValidValue(Volume))
dname = dname + " Vol." + Volume.Trim();
return dname;
}
else return FileName;
}
public string Title { get; }
public string Variant { get; }
public string Volume { get; }
public string Author { get; }
public string Publisher { get; }
public int Year { get; }
public string Comments { get; }
public Image Image { get; }
public string FileName => Path.GetFileNameWithoutExtension(path);
public string DisplayName => GetDisplayName();
public string FilePath => path;
}
}