2019-02-15 17:49:46 +01:00
|
|
|
|
#region LICENSE
|
|
|
|
|
/**********************************************************************************************
|
|
|
|
|
* Copyright (C) 2017-2019 - All Rights Reserved
|
|
|
|
|
*
|
|
|
|
|
* This file is part of "DML.Application".
|
|
|
|
|
* The official repository is hosted at https://github.com/Serraniel/DiscordMediaLoader
|
|
|
|
|
*
|
|
|
|
|
* "DML.Application" is licensed under Apache 2.0.
|
|
|
|
|
* Full license is included in the project repository.
|
|
|
|
|
*
|
|
|
|
|
* Users who edited MainForm.cs under the condition of the used license:
|
|
|
|
|
* - Serraniel (https://github.com/Serraniel)
|
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-06-15 18:33:00 +02:00
|
|
|
|
using System.Collections.Generic;
|
2017-05-20 10:23:31 +02:00
|
|
|
|
using System.Diagnostics;
|
2017-04-30 11:29:49 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2018-03-25 23:44:33 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2017-04-17 14:39:29 +02:00
|
|
|
|
using System.Windows.Forms;
|
2018-03-25 23:44:33 +02:00
|
|
|
|
using Discord;
|
2017-09-25 19:50:59 +02:00
|
|
|
|
using Discord.WebSocket;
|
2017-10-04 13:14:44 +02:00
|
|
|
|
using DML.AppCore.Classes;
|
2017-04-17 21:56:53 +02:00
|
|
|
|
using DML.Application.Classes;
|
2018-06-15 18:33:00 +02:00
|
|
|
|
using DML.Application.Helper;
|
2018-04-01 20:17:31 +02:00
|
|
|
|
using DML.Application.Classes.RPC;
|
2017-10-04 15:01:54 +02:00
|
|
|
|
using DML.Client;
|
2018-06-15 18:33:00 +02:00
|
|
|
|
using static DML.Client.DMLClient;
|
2017-04-17 14:39:29 +02:00
|
|
|
|
using static SweetLib.Utils.Logger.Logger;
|
2017-04-16 17:57:27 +02:00
|
|
|
|
|
|
|
|
|
namespace DML.Application
|
|
|
|
|
{
|
2018-03-25 23:44:33 +02:00
|
|
|
|
enum OnlineState
|
|
|
|
|
{
|
|
|
|
|
Online,
|
|
|
|
|
Idle,
|
|
|
|
|
DoNotDisturb,
|
|
|
|
|
Invisible
|
|
|
|
|
}
|
2017-04-16 17:57:27 +02:00
|
|
|
|
public partial class MainForm : Form
|
|
|
|
|
{
|
2017-04-17 14:39:29 +02:00
|
|
|
|
private bool IsInitialized { get; set; } = false;
|
2018-04-01 20:17:31 +02:00
|
|
|
|
private DiscordRpc.RichPresence Presence { get; }
|
|
|
|
|
|
2017-04-16 17:57:27 +02:00
|
|
|
|
public MainForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2017-04-17 14:39:29 +02:00
|
|
|
|
|
|
|
|
|
private void MainForm_Shown(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Trace("MainForm shown executed.");
|
|
|
|
|
RefreshComponents();
|
|
|
|
|
|
|
|
|
|
IsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshComponents()
|
|
|
|
|
{
|
|
|
|
|
Debug("Refreshing components...");
|
|
|
|
|
|
2018-03-25 23:44:33 +02:00
|
|
|
|
lbVersion.Text = $"v{Assembly.GetExecutingAssembly().GetName().Version} Copyright © by Serraniel";
|
2017-04-30 11:29:49 +02:00
|
|
|
|
|
2017-04-17 14:39:29 +02:00
|
|
|
|
Trace("Refreshing operating folder component...");
|
|
|
|
|
edOperatingFolder.Text = Core.Settings.OperatingFolder;
|
|
|
|
|
|
|
|
|
|
Trace("Refreshing name scheme component...");
|
|
|
|
|
edNameScheme.Text = Core.Settings.FileNameScheme;
|
|
|
|
|
|
|
|
|
|
Trace("Refreshing skip existing files component...");
|
|
|
|
|
cbSkipExisting.Checked = Core.Settings.SkipExistingFiles;
|
|
|
|
|
|
|
|
|
|
Trace("Refreshing thread limit component...");
|
|
|
|
|
edThreadLimit.Value = Core.Settings.ThreadLimit;
|
|
|
|
|
|
2017-04-30 11:06:10 +02:00
|
|
|
|
if (cbGuild.Items.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
Trace("Adding guilds to component...");
|
2018-06-15 18:33:00 +02:00
|
|
|
|
|
|
|
|
|
cbGuild.Items.AddRange(DMLClient.Client.Guilds.Where(g => g.Name != null).OrderBy(g => g.Name).Select(g => new IdentifiedString<ulong>(g.Id, g.Name)).ToArray());
|
|
|
|
|
|
2017-04-30 11:06:10 +02:00
|
|
|
|
cbGuild.SelectedIndex = 0;
|
|
|
|
|
Trace("Guild component initialized.");
|
|
|
|
|
}
|
2017-04-17 23:02:57 +02:00
|
|
|
|
|
|
|
|
|
Trace("Refreshing job list component...");
|
|
|
|
|
var oldIndex = lbxJobs.SelectedIndex;
|
|
|
|
|
lbxJobs.Items.Clear();
|
|
|
|
|
foreach (var job in Core.Scheduler.JobList)
|
|
|
|
|
{
|
2018-06-15 18:46:44 +02:00
|
|
|
|
lbxJobs.Items.Add(new IdentifiedString<int>(job.Id, $"{FindServerById(job.GuildId)?.Name}:{FindChannelById(FindServerById(job.GuildId), job.ChannelId)?.Name}"));
|
2017-04-17 23:02:57 +02:00
|
|
|
|
}
|
|
|
|
|
lbxJobs.SelectedIndex = oldIndex;
|
2018-03-25 23:44:33 +02:00
|
|
|
|
|
|
|
|
|
lbStatus.Text = DMLClient.Client.CurrentUser.Status.ToString();
|
2017-04-17 14:39:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoSomethingChanged(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug($"DoSomethingChanged excuted by {sender}.");
|
|
|
|
|
if (!IsInitialized)
|
|
|
|
|
{
|
|
|
|
|
Trace("Form not initialized. Leaving DoSomethingChanged...");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Trace("Updating operating folder...");
|
|
|
|
|
Core.Settings.OperatingFolder = edOperatingFolder.Text;
|
|
|
|
|
|
|
|
|
|
Trace("Updating name scheme...");
|
|
|
|
|
Core.Settings.FileNameScheme = edNameScheme.Text;
|
|
|
|
|
|
|
|
|
|
Trace("Updating skip existing files...");
|
|
|
|
|
Core.Settings.SkipExistingFiles = cbSkipExisting.Checked;
|
|
|
|
|
|
|
|
|
|
Trace("Updating thread limit...");
|
|
|
|
|
Core.Settings.ThreadLimit = (int)edThreadLimit.Value;
|
|
|
|
|
|
|
|
|
|
Trace("Storing new settings...");
|
|
|
|
|
Core.Settings.Store();
|
|
|
|
|
|
|
|
|
|
Info("New settings have been saved.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnSearchFolders_Click(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Trace("Operating folder button pressed.");
|
|
|
|
|
using (var browserDialog = new FolderBrowserDialog())
|
|
|
|
|
{
|
|
|
|
|
Debug("Showing file browser dialog for operating folder...");
|
|
|
|
|
|
|
|
|
|
browserDialog.SelectedPath = edOperatingFolder.Text;
|
|
|
|
|
browserDialog.ShowNewFolderButton = true;
|
|
|
|
|
browserDialog.Description = "Select an operating folder...";
|
|
|
|
|
|
|
|
|
|
if (browserDialog.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
edOperatingFolder.Text = browserDialog.SelectedPath;
|
|
|
|
|
Debug("Updated operating folder.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 19:50:59 +02:00
|
|
|
|
private SocketGuild FindServerByName(string name)
|
2017-04-17 14:39:29 +02:00
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find server by name: {name}");
|
2017-10-04 15:01:54 +02:00
|
|
|
|
return (from s in DMLClient.Client.Guilds where s.Name == name select s).FirstOrDefault();
|
2017-04-17 14:39:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 19:50:59 +02:00
|
|
|
|
private SocketTextChannel FindChannelByName(SocketGuild server, string name)
|
2017-04-17 14:39:29 +02:00
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find channel in {server} by name: {name}");
|
|
|
|
|
return (from c in server.TextChannels where c.Name == name select c).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 19:50:59 +02:00
|
|
|
|
private SocketGuild FindServerById(ulong id)
|
2017-04-17 23:02:57 +02:00
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find server by Id: {id}");
|
2017-10-04 15:01:54 +02:00
|
|
|
|
return (from s in DMLClient.Client.Guilds where s.Id == id select s).FirstOrDefault();
|
2017-04-17 23:02:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 19:50:59 +02:00
|
|
|
|
private SocketTextChannel FindChannelById(SocketGuild server, ulong id)
|
2017-04-17 23:02:57 +02:00
|
|
|
|
{
|
|
|
|
|
Trace($"Trying to find channel in {server} by id: {id}");
|
|
|
|
|
return (from c in server.TextChannels where c.Id == id select c).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 14:39:29 +02:00
|
|
|
|
private void cbGuild_SelectedIndexChanged(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Trace("Guild index changed.");
|
|
|
|
|
Debug("Updating channel dropdown component...");
|
|
|
|
|
|
|
|
|
|
UseWaitCursor = true;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-06-15 18:33:00 +02:00
|
|
|
|
var guild = FindServerById(((IdentifiedString<ulong>)cbGuild.SelectedItem).Id);
|
2017-04-17 14:39:29 +02:00
|
|
|
|
|
|
|
|
|
if (guild != null)
|
|
|
|
|
{
|
|
|
|
|
Trace("Cleaning channel component from old values...");
|
|
|
|
|
cbChannel.Items.Clear();
|
|
|
|
|
|
|
|
|
|
Trace("Adding new channels...");
|
2018-06-15 18:33:00 +02:00
|
|
|
|
|
|
|
|
|
cbChannel.Items.AddRange(guild.TextChannels.OrderBy(c => c.Position).Select(c => new IdentifiedString<ulong>(c.Id, c.Name)).ToArray());
|
|
|
|
|
|
2017-04-17 14:39:29 +02:00
|
|
|
|
Trace($"Added {cbChannel.Items.Count} channels.");
|
|
|
|
|
|
|
|
|
|
cbChannel.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Warn($"Guild {cbGuild.Text} could not be found!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
UseWaitCursor = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug("Finished updating channel dropdown component.");
|
|
|
|
|
}
|
2017-04-17 21:56:53 +02:00
|
|
|
|
|
|
|
|
|
private void btnAddJob_Click(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var job = new Job
|
|
|
|
|
{
|
2018-06-15 18:33:00 +02:00
|
|
|
|
GuildId = ((IdentifiedString<ulong>)cbGuild.SelectedItem).Id,
|
|
|
|
|
ChannelId = ((IdentifiedString<ulong>)cbChannel.SelectedItem).Id
|
2017-04-17 21:56:53 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!(from j in Core.Scheduler.JobList
|
|
|
|
|
where j.GuildId == job.GuildId && j.ChannelId == job.ChannelId
|
|
|
|
|
select j).Any())
|
|
|
|
|
{
|
|
|
|
|
job.Store();
|
2017-04-30 11:06:10 +02:00
|
|
|
|
Core.Scheduler.JobList.Add(job);
|
2017-04-17 21:56:53 +02:00
|
|
|
|
}
|
2017-04-30 11:06:10 +02:00
|
|
|
|
|
|
|
|
|
RefreshComponents();
|
2017-04-17 21:56:53 +02:00
|
|
|
|
}
|
2017-04-17 23:02:57 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 18:46:44 +02:00
|
|
|
|
var jobId = ((IdentifiedString<int>)lbxJobs.SelectedItem).Id;
|
2017-04-17 23:02:57 +02:00
|
|
|
|
|
2018-06-15 18:46:44 +02:00
|
|
|
|
var job = Core.Scheduler.JobList.FirstOrDefault(j => j.Id == jobId);
|
|
|
|
|
if (job != null)
|
2017-04-17 23:02:57 +02:00
|
|
|
|
{
|
2018-06-15 18:46:44 +02:00
|
|
|
|
Core.Scheduler.JobList.Remove(job);
|
|
|
|
|
Core.Scheduler.RunningJobs.Remove(job.Id);
|
|
|
|
|
job.Stop();
|
|
|
|
|
job.Delete();
|
2017-04-17 23:02:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2017-05-20 10:23:31 +02:00
|
|
|
|
progress = Math.Min(Math.Max(0, progress), 100);
|
2017-04-17 23:02:57 +02:00
|
|
|
|
pgbProgress.Maximum = 100;
|
|
|
|
|
pgbProgress.Value = progress;
|
|
|
|
|
|
|
|
|
|
lbProgress.Text = $"Scanned: {scanned} Downloaded: {done} Open: {totalAttachments - done}";
|
2018-04-01 20:17:31 +02:00
|
|
|
|
|
2018-04-23 21:40:25 +02:00
|
|
|
|
if (Core.Settings.UseRPC)
|
|
|
|
|
{
|
|
|
|
|
Core.RpcPresence.details = "Downloading media files";
|
|
|
|
|
Core.RpcPresence.state = $"{done} / {totalAttachments} ({pgbProgress.Value}%)";
|
|
|
|
|
Core.RpcPresence.largeImageKey = "main";
|
|
|
|
|
Core.RpcPresence.largeImageText = "Visit discordmedialoader.net";
|
|
|
|
|
Core.RpcPresence.smallImageKey = "author";
|
|
|
|
|
Core.RpcPresence.smallImageText = "Made by Serraniel";
|
|
|
|
|
|
|
|
|
|
Core.RpcUpdatePresence();
|
|
|
|
|
}
|
2017-04-17 23:02:57 +02:00
|
|
|
|
}
|
2017-04-30 11:29:49 +02:00
|
|
|
|
|
|
|
|
|
private void aboutToolStripMenuItem_Click(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(Properties.Resources.AboutString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void visitGithubToolStripMenuItem_Click(object sender, System.EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Process.Start("https://github.com/Serraniel/DiscordMediaLoader/");
|
|
|
|
|
}
|
2018-03-25 23:44:33 +02:00
|
|
|
|
|
|
|
|
|
private async void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
OnlineState state = (OnlineState)Convert.ToInt32(e.ClickedItem.Tag);
|
|
|
|
|
|
|
|
|
|
lbStatus.Text = state.ToString();
|
|
|
|
|
tmrTriggerRefresh.Start();
|
|
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case OnlineState.Online:
|
|
|
|
|
await DMLClient.Client.SetStatusAsync(UserStatus.Online);
|
|
|
|
|
break;
|
|
|
|
|
case OnlineState.Idle:
|
|
|
|
|
await DMLClient.Client.SetStatusAsync(UserStatus.Idle);
|
|
|
|
|
break;
|
|
|
|
|
case OnlineState.DoNotDisturb:
|
|
|
|
|
await DMLClient.Client.SetStatusAsync(UserStatus.DoNotDisturb);
|
|
|
|
|
break;
|
|
|
|
|
case OnlineState.Invisible:
|
|
|
|
|
await DMLClient.Client.SetStatusAsync(UserStatus.Invisible);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void tmrTriggerRefresh_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lbStatus.Text = DMLClient.Client.CurrentUser.Status.ToString();
|
|
|
|
|
tmrTriggerRefresh.Stop();
|
|
|
|
|
}
|
2018-08-29 23:16:04 +02:00
|
|
|
|
|
|
|
|
|
private void showTokenToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText(Core.Settings.LoginToken);
|
|
|
|
|
MessageBox.Show(this, "Your login token has been copied to your clipboard.", "Discord Media Loader",
|
|
|
|
|
MessageBoxButtons.OK);
|
|
|
|
|
}
|
2017-04-16 17:57:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|