using Manina.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
namespace HeliosPlus.UIForms
{
#region Shortcut Adaptor
///
/// A custom item adaptor for ImageListView that reads from a Profile.
///
class ShortcutAdaptor : ImageListView.ImageListViewItemAdaptor
{
private bool disposed;
///
/// Initializes a new instance of the class.
///
public ShortcutAdaptor()
{
disposed = false;
}
///
/// Returns the thumbnail image for the given item.
///
/// Item key.
/// Requested image size.
/// Embedded thumbnail usage.
/// true to automatically rotate images based on Exif orientation; otherwise false.
/// The thumbnail image from the given item or null if an error occurs.
public override Image GetThumbnail(object key, Size size, UseEmbeddedThumbnails useEmbeddedThumbnails, bool useExifOrientation)
{
if (disposed)
return null;
try
{
ShortcutItem shortcut = (ShortcutItem) key;
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(() => { return false; });
return shortcut.ShortcutBitmap.GetThumbnailImage(256, 256, myCallback, IntPtr.Zero);
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutAdapter/GetThumbnail exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// If we have a problem with converting the submitted key to a profile
// Then we return null
return null;
}
return null;
}
///
/// Returns a unique identifier for this thumbnail to be used in persistent
/// caching.
///
/// Item key.
/// Requested image size.
/// Embedded thumbnail usage.
/// true to automatically rotate images based on Exif orientation; otherwise false.
/// A unique identifier string for the thumnail.
public override string GetUniqueIdentifier(object key, Size size, UseEmbeddedThumbnails useEmbeddedThumbnails, bool useExifOrientation)
{
if (disposed)
return null;
try
{
ShortcutItem shortcut = (ShortcutItem) key;
//return shortcut.Name;
return shortcut.Name;
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutAdapter/GetUniqueIdentifier exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// If we have a problem with converting the submitted key to a Shortcut
// Then we return null
return null;
}
}
///
/// Returns the path to the source image for use in drag operations.
///
/// Item key.
/// The path to the source image.
public override string GetSourceImage(object key)
{
if (disposed)
return null;
try
{
string shortcutName = (string)key;
return shortcutName;
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutAdaptor/GetSourceImage exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// If we have a problem with converting the submitted key to a profile
// Then we return null
return null;
}
}
///
/// Returns the details for the given item.
///
/// Item key.
/// An array of tuples containing item details or null if an error occurs.
public override Utility.Tuple[] GetDetails(object key)
{
if (disposed)
return null;
List> details = new List>();
try
{
ShortcutItem shortcut = (ShortcutItem) key;
// Get file info
if (shortcut.ShortcutBitmap is Bitmap)
{
// Have to do some gymnastics to get rid of the
// System.Drawing.Image exception created while accessing the Size
bool gotSize = false;
Size mySize = new Size(256,256);
while (!gotSize)
{
try
{
mySize = shortcut.ShortcutBitmap.Size;
gotSize = true;
}
catch (Exception ex)
{
// catch the System.Drawing.Image exception created while accessing the Size
mySize = new Size(256, 256);
}
}
// Have to do some gymnastics to get rid of the
// System.Drawing.Image exception created while accessing the SizeF
bool gotSizeF = false;
SizeF mySizeF = new SizeF(256, 256);
while (!gotSizeF)
{
try
{
mySizeF = shortcut.ShortcutBitmap.PhysicalDimension;
gotSizeF = true;
}
catch (Exception ex)
{
// catch the System.Drawing.Image exception created while accessing the Size
mySizeF = new SizeF(256, 256);
}
}
string name = shortcut.Name;
string filepath = Path.GetDirectoryName(shortcut.SavedShortcutIconCacheFilename);
string filename = Path.GetFileName(shortcut.SavedShortcutIconCacheFilename);
DateTime now = DateTime.Now;
details.Add(new Utility.Tuple(ColumnType.DateCreated, string.Empty, now));
details.Add(new Utility.Tuple(ColumnType.DateAccessed, string.Empty, now));
details.Add(new Utility.Tuple(ColumnType.DateModified, string.Empty, now));
details.Add(new Utility.Tuple(ColumnType.FileSize, string.Empty, (long)0));
details.Add(new Utility.Tuple(ColumnType.FilePath, string.Empty, filepath ?? ""));
details.Add(new Utility.Tuple(ColumnType.FolderName, string.Empty, filepath ?? ""));
details.Add(new Utility.Tuple(ColumnType.Dimensions, string.Empty, mySize));
details.Add(new Utility.Tuple(ColumnType.Resolution, string.Empty, mySizeF));
details.Add(new Utility.Tuple(ColumnType.ImageDescription, string.Empty, name ?? ""));
details.Add(new Utility.Tuple(ColumnType.EquipmentModel, string.Empty, ""));
details.Add(new Utility.Tuple(ColumnType.DateTaken, string.Empty, now));
details.Add(new Utility.Tuple(ColumnType.Artist, string.Empty, ""));
details.Add(new Utility.Tuple(ColumnType.Copyright, string.Empty, ""));
details.Add(new Utility.Tuple(ColumnType.ExposureTime, string.Empty, (float)0));
details.Add(new Utility.Tuple(ColumnType.FNumber, string.Empty, (float)0));
details.Add(new Utility.Tuple(ColumnType.ISOSpeed, string.Empty, (ushort)0));
details.Add(new Utility.Tuple(ColumnType.UserComment, string.Empty, ""));
details.Add(new Utility.Tuple(ColumnType.Rating, string.Empty, (ushort)0));
details.Add(new Utility.Tuple(ColumnType.Software, string.Empty, ""));
details.Add(new Utility.Tuple(ColumnType.FocalLength, string.Empty, (float)0));
}
return details.ToArray();
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutAdapter/Utility.Tuple exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// If we have a problem with converting the submitted key to a profile
// Then we return null
return null;
}
}
public override void Dispose()
{
disposed = true;
}
}
#endregion
}