Some UI stuff and adjustments....Seriously...I am lost to the changes I did

This commit is contained in:
Serraniel 2017-04-17 23:02:57 +02:00
parent fe4a4769f8
commit 8484a5e04c
7 changed files with 273 additions and 47 deletions

View file

@ -36,6 +36,16 @@ namespace DML.Application.Classes
}
}
internal void Delete()
{
Debug("Deleting job from database...");
Trace("Getting jobs collection...");
var jobDb = Core.Database.GetCollection<Job>("jobs");
Trace("Deleting value...");
jobDb.Delete(Id);
}
private Server FindServerById(ulong id)
{
Trace($"Trying to find server by Id: {id}");
@ -103,9 +113,12 @@ namespace DML.Application.Classes
if (m.Attachments.Length > 0)
{
result.Add(m);
Core.Scheduler.TotalAttachments++;
Trace($"Added message {m.Id}");
}
Debug($"Finished message {m.Id}");
Core.Scheduler.MessagesScanned++;
}
finished = finished || messages.Length < limit;

View file

@ -15,11 +15,71 @@ namespace DML.Application.Classes
{
internal class JobScheduler
{
private ulong messageScanned = 0;
private ulong totalAttachments = 0;
private ulong attachmentsDownloaded = 0;
private bool Run { get; set; } = false;
internal List<Job> JobList { get; set; } = new List<Job>();
internal Dictionary<int, Queue<Message>> RunningJobs = new Dictionary<int, Queue<Message>>();
internal int RunningThreads { get; set; } = 0;
internal ulong MessagesScanned
{
get
{
lock (this)
{
return messageScanned;
}
}
set
{
lock (this)
{
messageScanned = value;
}
}
}
internal ulong TotalAttachments
{
get
{
lock (this)
{
return totalAttachments;
}
}
set
{
lock (this)
{
totalAttachments = value;
}
}
}
internal ulong AttachmentsDownloaded
{
get
{
lock (this)
{
return attachmentsDownloaded;
}
}
set
{
lock (this)
{
attachmentsDownloaded = value;
}
}
}
internal ulong AttachmentsToDownload => TotalAttachments - AttachmentsDownloaded;
internal void Stop()
{
Run = false;
@ -28,7 +88,7 @@ namespace DML.Application.Classes
internal void Start()
{
Run = true;
Task.Run(async () =>
{
Info("Started JobScheduler...");
@ -71,7 +131,7 @@ namespace DML.Application.Classes
}
Trace($"Added {queue.Count} messages to queue.");
if(messages.Count!= queue.Count)
if (messages.Count != queue.Count)
Warn("Not all messages have been added into the queue.");
var startedDownload = false;
@ -128,50 +188,70 @@ namespace DML.Application.Classes
Trace("Queue found.");
}
Trace("Unlocked scheduler.");
Debug($"Messages to process for job {jobId}: {queue.Count}");
while (queue.Count > 0)
{
Trace("Locking scheduler...");
lock (this)
{
Trace("Checking if still a job...");
if (!RunningJobs.ContainsKey(jobId))
{
Warn($"Queue for job {jobId} not found!");
return;
}
Trace("Continue working...");
}
Trace("Unlocked scheduler.");
Trace("Dequeueing message...");
var message = queue.Dequeue();
Debug($"Attachments for message {message.Id}: {message.Attachments.Length}");
foreach (var a in message.Attachments)
{
var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme);
Trace("Replacing filename placeholders...");
fileName =
fileName.Replace("%guild%", message.Server.Name)
.Replace("%channel%", message.Channel.Name)
.Replace("%timestamp%", SweetUtils.DateTimeToUnixTimeStamp(message.Timestamp).ToString())
.Replace("%name%", a.Filename);
Trace($"Detemined file name: {fileName}.");
if (File.Exists(fileName) && new FileInfo(fileName).Length == a.Size)
try
{
Debug($"{fileName} already existing with its estimated size. Skipping...");
continue;
}
Trace("Determining directory...");
var fileDirectory = Path.GetDirectoryName(fileName);
var fileName = Path.Combine(Core.Settings.OperatingFolder, Core.Settings.FileNameScheme);
if (!Directory.Exists(fileDirectory))
Trace("Replacing filename placeholders...");
fileName =
fileName.Replace("%guild%", message.Server.Name)
.Replace("%channel%", message.Channel.Name)
.Replace("%timestamp%", SweetUtils.DateTimeToUnixTimeStamp(message.Timestamp).ToString())
.Replace("%name%", a.Filename);
Trace($"Detemined file name: {fileName}.");
if (File.Exists(fileName) && new FileInfo(fileName).Length == a.Size)
{
Debug($"{fileName} already existing with its estimated size. Skipping...");
continue;
}
Trace("Determining directory...");
var fileDirectory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(fileDirectory))
{
Info($"Directory {fileDirectory} does not exist. Creating directory...");
Directory.CreateDirectory(fileDirectory);
Debug("Created directory.");
}
var wc = new WebClient();
Debug($"Starting downloading of attachment {a.Id}...");
wc.DownloadFile(new Uri(a.Url), fileName);
Debug($"Downloaded attachment {a.Id}.");
Trace("Updating known timestamp for job...");
job.KnownTimestamp = SweetUtils.DateTimeToUnixTimeStamp(message.Timestamp);
job.Store();
}
finally
{
Info($"Directory {fileDirectory} does not exist. Creating directory...");
Directory.CreateDirectory(fileDirectory);
Debug("Created directory.");
AttachmentsDownloaded++;
}
var wc = new WebClient();
Debug($"Starting downloading of attachment {a.Id}...");
wc.DownloadFile(new Uri(a.Url), fileName);
Debug($"Downloaded attachment {a.Id}.");
Trace("Updating known timestamp for job...");
job.KnownTimestamp = SweetUtils.DateTimeToUnixTimeStamp(message.Timestamp);
job.Store();
}
}
}

View file

@ -28,6 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.pnlSettings = new System.Windows.Forms.GroupBox();
this.lbThreadLimit = new System.Windows.Forms.Label();
@ -44,11 +45,18 @@
this.lbChannel = new System.Windows.Forms.Label();
this.cbGuild = new System.Windows.Forms.ComboBox();
this.lbGuild = new System.Windows.Forms.Label();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.statusStrip = new System.Windows.Forms.StatusStrip();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lbxJobs = new System.Windows.Forms.ListBox();
this.btnDelete = new System.Windows.Forms.Button();
this.lbProgress = new System.Windows.Forms.ToolStripStatusLabel();
this.pgbProgress = new System.Windows.Forms.ToolStripProgressBar();
this.tmrRefreshProgress = new System.Windows.Forms.Timer(this.components);
this.pnlSettings.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.edThreadLimit)).BeginInit();
this.groupBox1.SuspendLayout();
this.statusStrip.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// pnlSettings
@ -220,16 +228,21 @@
this.lbGuild.TabIndex = 0;
this.lbGuild.Text = "Guild:";
//
// statusStrip1
// statusStrip
//
this.statusStrip1.Location = new System.Drawing.Point(0, 311);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(553, 22);
this.statusStrip1.TabIndex = 2;
this.statusStrip1.Text = "statusStrip1";
this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.pgbProgress,
this.lbProgress});
this.statusStrip.Location = new System.Drawing.Point(0, 311);
this.statusStrip.Name = "statusStrip";
this.statusStrip.Size = new System.Drawing.Size(553, 22);
this.statusStrip.TabIndex = 2;
this.statusStrip.Text = "statusStrip1";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btnDelete);
this.groupBox2.Controls.Add(this.lbxJobs);
this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox2.Location = new System.Drawing.Point(0, 150);
this.groupBox2.Name = "groupBox2";
@ -238,13 +251,51 @@
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Jobs";
//
// lbxJobs
//
this.lbxJobs.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lbxJobs.FormattingEnabled = true;
this.lbxJobs.Location = new System.Drawing.Point(6, 19);
this.lbxJobs.Name = "lbxJobs";
this.lbxJobs.Size = new System.Drawing.Size(541, 108);
this.lbxJobs.TabIndex = 0;
//
// btnDelete
//
this.btnDelete.Dock = System.Windows.Forms.DockStyle.Bottom;
this.btnDelete.Location = new System.Drawing.Point(3, 135);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(547, 23);
this.btnDelete.TabIndex = 1;
this.btnDelete.Text = "Delete selected";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// lbProgress
//
this.lbProgress.Name = "lbProgress";
this.lbProgress.Size = new System.Drawing.Size(0, 17);
//
// pgbProgress
//
this.pgbProgress.Name = "pgbProgress";
this.pgbProgress.Size = new System.Drawing.Size(100, 16);
//
// tmrRefreshProgress
//
this.tmrRefreshProgress.Enabled = true;
this.tmrRefreshProgress.Interval = 500;
this.tmrRefreshProgress.Tick += new System.EventHandler(this.tmrRefreshProgress_Tick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(553, 333);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.statusStrip);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.pnlSettings);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
@ -258,6 +309,9 @@
((System.ComponentModel.ISupportInitialize)(this.edThreadLimit)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.statusStrip.ResumeLayout(false);
this.statusStrip.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
@ -267,7 +321,7 @@
private System.Windows.Forms.GroupBox pnlSettings;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.StatusStrip statusStrip;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label lbThreadLimit;
private System.Windows.Forms.NumericUpDown edThreadLimit;
@ -282,5 +336,10 @@
private System.Windows.Forms.Label lbChannel;
private System.Windows.Forms.ComboBox cbGuild;
private System.Windows.Forms.Label lbGuild;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.ListBox lbxJobs;
private System.Windows.Forms.ToolStripProgressBar pgbProgress;
private System.Windows.Forms.ToolStripStatusLabel lbProgress;
private System.Windows.Forms.Timer tmrRefreshProgress;
}
}

View file

@ -42,6 +42,16 @@ namespace DML.Application
cbGuild.Items.AddRange(Core.Client.Servers.OrderBy(g => g.Name).Select(g => g.Name).ToArray());
cbGuild.SelectedIndex = 0;
Trace("Guild component initialized.");
Trace("Refreshing job list component...");
var oldIndex = lbxJobs.SelectedIndex;
lbxJobs.Items.Clear();
foreach (var job in Core.Scheduler.JobList)
{
lbxJobs.Items.Add(
$"{FindServerById(job.GuildId).Name}:{FindChannelById(FindServerById(job.GuildId), job.ChannelId).Name}");
}
lbxJobs.SelectedIndex = oldIndex;
}
private void DoSomethingChanged(object sender, System.EventArgs e)
@ -102,6 +112,18 @@ namespace DML.Application
return (from c in server.TextChannels where c.Name == name select c).FirstOrDefault();
}
private Server FindServerById(ulong id)
{
Trace($"Trying to find server by Id: {id}");
return (from s in Core.Client.Servers where s.Id == id select s).FirstOrDefault();
}
private Channel FindChannelById(Server server, ulong id)
{
Trace($"Trying to find channel in {server} by id: {id}");
return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault();
}
private void cbGuild_SelectedIndexChanged(object sender, System.EventArgs e)
{
Trace("Guild index changed.");
@ -152,5 +174,54 @@ namespace DML.Application
job.Store();
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
Trace("Deleting job pressed.");
if (lbxJobs.SelectedIndex < 0)
{
Warn("No job selected.");
MessageBox.Show("No job has been seleted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
var jobNameData = lbxJobs.SelectedItem.ToString().Split(':');
var guildName = "";
for (var i = 0; i < jobNameData.Length - 1; i++)
guildName += jobNameData[i] + ":";
guildName = guildName.Substring(0, guildName.Length - 1);
var channelName = jobNameData[jobNameData.Length - 1];
var guild = FindServerByName(guildName);
var channel = FindChannelByName(guild, channelName);
foreach (var job in Core.Scheduler.JobList)
{
if (job.GuildId == guild.Id && job.ChannelId == channel.Id)
{
Core.Scheduler.JobList.Remove(job);
Core.Scheduler.RunningJobs.Remove(job.Id);
job.Delete();
}
}
lbxJobs.SelectedIndex = -1;
RefreshComponents();
}
private void tmrRefreshProgress_Tick(object sender, System.EventArgs e)
{
var scanned = Core.Scheduler.MessagesScanned;
var totalAttachments = Core.Scheduler.TotalAttachments;
var done = Core.Scheduler.AttachmentsDownloaded;
var progress = totalAttachments > 0 ? (int)(100 * done / totalAttachments) : 0;
pgbProgress.Maximum = 100;
pgbProgress.Value = progress;
lbProgress.Text = $"Scanned: {scanned} Downloaded: {done} Open: {totalAttachments - done}";
}
}
}

View file

@ -117,9 +117,12 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<metadata name="statusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="tmrRefreshProgress.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>126, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.2.142.0")]
[assembly: AssemblyFileVersion("0.2.142.0")]
[assembly: AssemblyVersion("0.2.148.0")]
[assembly: AssemblyFileVersion("0.2.148.0")]

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.99.70.0")]
[assembly: AssemblyFileVersion("0.99.70.0")]
[assembly: AssemblyVersion("0.99.75.0")]
[assembly: AssemblyFileVersion("0.99.75.0")]