Korero – Rev 1
?pathlinks?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Korero.Chat;
using Korero.Communication;
using Korero.Economy;
using Korero.Properties;
using Korero.Serialization;
using Korero.Utilities;
using Serilog;
namespace Korero.Profile
{
public partial class GroupProfileForm : Form
{
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private readonly CancellationToken _cancellationToken;
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly string _groupName;
private readonly MainForm _mainForm;
private readonly MqttCommunication _mqttCommunication;
private AvatarProfileForm _avatarProfileForm;
private PaymentForm _paymentForm;
#endregion
#region Constructors, Destructors and Finalizers
public GroupProfileForm()
{
InitializeComponent();
Utilities.WindowState.FormTracker.Track(this);
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
}
public GroupProfileForm(MainForm mainForm, string groupName, MqttCommunication mqttCommunication) : this()
{
_mainForm = mainForm;
_groupName = groupName;
_mqttCommunication = mqttCommunication;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Event Handlers
private async void GroupProfileForm_Shown(object sender, EventArgs e)
{
try
{
var data = new Dictionary<string, string>
{
{"command", "getgroupdata"},
{"group", Settings.Default.Group},
{"password", Settings.Default.Password},
{"target", _groupName},
{
"data",
new CSV(new[]
{"Charter", "InsigniaID"})
}
};
var callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
if (callback == null || !callback.Success)
{
if (callback != null)
{
Log.Warning("Command {Command} has failed with {Error}.",
callback.Command, callback.Error);
}
return;
}
var insigniaId = callback["InsigniaID"].FirstOrDefault();
if (!string.IsNullOrEmpty(insigniaId) &&
Guid.TryParse(insigniaId, out var insigniaImageId) &&
insigniaImageId != Guid.Empty)
{
var insigniaImage =
await Communication.Utilities.DownloadImage(insigniaImageId, _mainForm.MqttCommunication,
_cancellationToken);
if (insigniaImage != null)
{
pictureBox1.InvokeIfRequired(pictureBox =>
{
if (pictureBox.Image != null)
{
pictureBox.Image.Dispose();
pictureBox.Image = null;
}
pictureBox.Image = insigniaImage;
});
}
}
else
{
pictureBox1.InvokeIfRequired(pictureBox =>
{
if (pictureBox.Image != null)
{
pictureBox.Image.Dispose();
pictureBox.Image = null;
}
});
}
var charterText = callback["Charter"].FirstOrDefault();
if (!string.IsNullOrEmpty(charterText))
{
richTextBox1.Text = charterText;
}
data = new Dictionary<string, string>
{
{"command", "getgroupmembersdata"},
{"group", Settings.Default.Group},
{"password", Settings.Default.Password},
{"target", _groupName},
{
"data",
new CSV(new[]
{"ID", "Title", "OnlineStatus"})
}
};
callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
if (callback == null || !callback.Success)
{
if (callback != null)
{
Log.Warning("Command {Command} has failed with {Error}.",
callback.Command, callback.Error);
}
return;
}
var groupMembersData = callback.Data;
data = new Dictionary<string, string>
{
{"command", "batchavatarkeytoname"},
{"group", Settings.Default.Group},
{"password", Settings.Default.Password},
{
"avatars",
new CSV(callback["ID"])
}
};
callback = await _mqttCommunication.SendCommand(new Command(data), _cancellationToken);
if (callback == null || !callback.Success)
{
if (callback != null)
{
Log.Warning("Command {Command} has failed with {Error}.",
callback.Command, callback.Error);
}
return;
}
var nameToKey = new Dictionary<Guid, string>();
foreach (var avatar in CsvStride(callback.Data, 2))
{
if (!Guid.TryParse(avatar[0], out var id))
{
continue;
}
nameToKey.Add(id, avatar[1]);
}
foreach (var member in CsvStride(groupMembersData, 6))
{
var index = Array.IndexOf(member, "ID");
if (!Guid.TryParse(member[index + 1], out var id))
{
continue;
}
if (!nameToKey.TryGetValue(id, out var name))
{
continue;
}
index = Array.IndexOf(member, "Title");
var title = member[index + 1];
index = Array.IndexOf(member, "OnlineStatus");
var status = member[index + 1];
dataGridView1.InvokeIfRequired(dataGridView =>
{
index = dataGridView.Rows.Add();
dataGridView.Rows[index].Cells[0].Value = name;
dataGridView.Rows[index].Cells[1].Value = title;
dataGridView.Rows[index].Cells[2].Value = status;
});
}
}
catch (Exception ex)
{
Log.Warning(ex, "Error while retrieving group data.");
}
}
private void GroupProfileForm_FormClosing(object sender, FormClosingEventArgs e)
{
_cancellationTokenSource.Cancel();
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
}
private void ProfileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_avatarProfileForm != null)
{
return;
}
var index = dataGridView1.CurrentCell.RowIndex;
if (index == -1)
{
return;
}
var name = (string) dataGridView1.Rows[index].Cells[0].Value;
_avatarProfileForm = new AvatarProfileForm(_mainForm, name, _mqttCommunication);
_avatarProfileForm.Closing += AvatarProfileForm_Closing;
_avatarProfileForm.Show();
}
private void AvatarProfileForm_Closing(object sender, CancelEventArgs e)
{
if (_avatarProfileForm == null)
{
return;
}
_avatarProfileForm.Closing -= AvatarProfileForm_Closing;
_avatarProfileForm.Dispose();
_avatarProfileForm = null;
}
private void RichTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
Process.Start(e.LinkText);
}
private void ChatToolStripMenuItem_Click(object sender, EventArgs e)
{
var index = dataGridView1.CurrentCell.RowIndex;
if (index == -1)
{
return;
}
var name = (string) dataGridView1.Rows[index].Cells[0].Value;
if (_mainForm.ChatForm != null)
{
_mainForm.ChatForm.CreateOrSelect(name);
return;
}
_mainForm.ChatForm = new ChatForm(_mainForm, _mqttCommunication);
_mainForm.ChatForm.Closing += ChatForm_Closing;
_mainForm.ChatForm.Show();
_mainForm.ChatForm.CreateOrSelect(name);
}
private void ChatForm_Closing(object sender, CancelEventArgs e)
{
if (_mainForm.ChatForm == null)
{
return;
}
_mainForm.ChatForm.Closing -= ChatForm_Closing;
_mainForm.ChatForm.Dispose();
_mainForm.ChatForm = null;
}
private void PayToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_paymentForm != null)
{
return;
}
_paymentForm = new PaymentForm(_groupName, _mainForm, _mqttCommunication);
_paymentForm.Closing += PaymentForm_Closing;
_paymentForm.Show();
}
private void PaymentForm_Closing(object sender, CancelEventArgs e)
{
if (_paymentForm == null)
{
return;
}
_paymentForm.Closing -= PaymentForm_Closing;
_paymentForm.Dispose();
_paymentForm = null;
}
#endregion
#region Private Methods
private static IEnumerable<string[]> CsvStride(string input, int stride)
{
var i = 0;
return new CSV(input).Select(s => new {s, num = i++})
.GroupBy(t => t.num / stride, t => t.s)
.Select(g => g.ToArray());
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.