114 lines
4.2 KiB
C#
114 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
using dezentrale.model;
|
|
|
|
namespace dezentrale.view
|
|
{
|
|
public class LvAttachments : CustomListView<Attachment>
|
|
{
|
|
protected override List<ConfigLVDataHandler> DefaultColumns
|
|
{
|
|
get
|
|
{
|
|
return new List<ConfigLVDataHandler>()
|
|
{
|
|
new ConfigLVDataHandler()
|
|
{
|
|
Name = "fileName",
|
|
Display = "FileName",
|
|
Width = 80,
|
|
CustomToString = x => ((Attachment)x).FileName,
|
|
},
|
|
new ConfigLVDataHandler()
|
|
{
|
|
Name = "added",
|
|
Display = "Added",
|
|
Width = 88,
|
|
CustomToString = ( x => (((Attachment)x).Added.Year > 1 ? ((Attachment)x).Added.ToString() : "-") ),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
private IAttachmentOwner owner;
|
|
public LvAttachments(IAttachmentOwner owner) : base(Program.config.AttachmentsColumns, LvAttachments_ColumnsChanged)
|
|
{
|
|
this.owner = owner;
|
|
LoadFromList(owner.Attachments);
|
|
|
|
AddMenuItem("Add file...", (sender, e) =>
|
|
{
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
|
DialogResult dr = ofd.ShowDialog();
|
|
if(dr == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
byte[] data = System.IO.File.ReadAllBytes(ofd.FileName);
|
|
|
|
Attachment a = new Attachment(System.IO.Path.GetFileName(ofd.FileName), data);
|
|
owner.Attachments.Add(a);
|
|
owner.AttachmentChanged(a, eAttachmentAction.Added);
|
|
AddEntry(a);
|
|
} catch(Exception ex)
|
|
{
|
|
MessageBox.Show($"Error:\r\n{ex.Message}");
|
|
}
|
|
}
|
|
});
|
|
AddMenuItem("Save file as...", (sender, e) =>
|
|
{
|
|
List<Attachment> save = GetSelectedItems();
|
|
if(save.Count < 1)
|
|
{
|
|
MessageBox.Show("No file selected.");
|
|
return;
|
|
}
|
|
SaveFileDialog sfd = new SaveFileDialog();
|
|
sfd.FileName = save[0].FileName;
|
|
DialogResult dr = sfd.ShowDialog();
|
|
if(dr == DialogResult.OK)
|
|
{
|
|
try
|
|
{
|
|
byte[] data = save[0].GetContents();
|
|
System.IO.File.WriteAllBytes(sfd.FileName, data);
|
|
} catch(Exception ex)
|
|
{
|
|
MessageBox.Show($"Error:\r\n{ex.Message}");
|
|
}
|
|
}
|
|
});
|
|
AddMenuItem("Delete file(s)", (sender, e) =>
|
|
{
|
|
|
|
List<Attachment> rem = GetSelectedItems();
|
|
if (rem.Count < 1)
|
|
{
|
|
MessageBox.Show("No file selected.");
|
|
return;
|
|
}
|
|
if(owner.Attachments.Remove(rem[0]))
|
|
{
|
|
this.RemoveEntry(rem[0]);
|
|
owner.AttachmentChanged(rem[0], eAttachmentAction.Deleted);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show($"Couldn't remove entry {rem[0].FileName}");
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void LvAttachments_ColumnsChanged(object sender, ColumnsChangedArgs e)
|
|
{
|
|
Console.WriteLine("LvAttachments_ColumnsChanged");
|
|
Program.config.AttachmentsColumns.Clear();
|
|
foreach (ConfigLVDataHandler c in e.Columns) Program.config.AttachmentsColumns.Add(new ConfigLVColumn(c));
|
|
XmlData.SaveToFile(Program.ConfigFile, Program.config);
|
|
}
|
|
}
|
|
} |