Korero – Rev 2

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Korero.Communication;
using Korero.Economy;
using Korero.Properties;
using Korero.Serialization;
using Korero.Utilities;
using Serilog;

namespace Korero.Profile
{
    public partial class AvatarProfileForm : Form
    {
        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private readonly CancellationToken _cancellationToken;

        private readonly CancellationTokenSource _cancellationTokenSource;

        private readonly string _firstName;

        private readonly string _lastName;

        private readonly MainForm _mainForm;

        private readonly MqttCommunication _mqttCommunication;

        private GroupProfileForm _groupProfileForm;

        private PaymentForm _paymentForm;

        #endregion

        #region Constructors, Destructors and Finalizers

        public AvatarProfileForm()
        {
            InitializeComponent();

            _cancellationTokenSource = new CancellationTokenSource();
            _cancellationToken = _cancellationTokenSource.Token;
        }

        public AvatarProfileForm(MainForm mainForm, string firstName, string lastName,
                                 MqttCommunication mqttCommunication) : this()
        {
            _mainForm = mainForm;
            _firstName = firstName;
            _lastName = lastName;

            _mqttCommunication = mqttCommunication;
        }

        public AvatarProfileForm(MainForm mainForm, string fullName, MqttCommunication mqttCommunication) : this()
        {
            _mainForm = mainForm;

            var split = fullName.Split(' ');
            _firstName = split[0];
            _lastName = split[1];

            _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 void AvatarProfileForm_Load(object sender, EventArgs e)
        {
            Utilities.WindowState.FormTracker.Track(this);
        }
        private async void AvatarProfileForm_Shown(object sender, EventArgs e)
        {
            secondLifeNameTextBox.Text = $"{_firstName} {_lastName}";

            var data = new Dictionary<string, string>
            {
                {"command", "getprofiledata"},
                {"group", Settings.Default.Group},
                {"password", Settings.Default.Password},
                {"entity", "avatar"},
                {"firstname", _firstName},
                {"lastname", _lastName},
                {
                    "data",
                    new CSV(new[]
                        {"BornOn", "AboutText", "ProfileImage", "GroupName", "FirstLifeText", "FirstLifeImage"})
                }
            };

            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 bornOnText = callback["BornOn"].FirstOrDefault();

            if (!string.IsNullOrEmpty(bornOnText) && DateTime.TryParse(bornOnText, out var bornOnDateTime))
            {
                secondLifeBornTextBox.Text = $"{bornOnText} ({(DateTime.Now - bornOnDateTime.Date).Days} days)";
            }

            foreach (var group in callback["GroupName"])
            {
                if (string.IsNullOrEmpty(group))
                {
                    continue;
                }

                if (!secondLifeGroupsListBox.Items.Contains(group))
                {
                    secondLifeGroupsListBox.Items.Add(group);
                }
            }

            var aboutText = callback["AboutText"].FirstOrDefault();

            if (!string.IsNullOrEmpty(aboutText))
            {
                secondLifeAboutRichTextBox.Text = aboutText;
            }

            var profileImageText = callback["ProfileImage"].FirstOrDefault();

            if (!string.IsNullOrEmpty(profileImageText) &&
                Guid.TryParse(profileImageText, out var profileImageId) &&
                profileImageId != Guid.Empty)
            {
                var profileImage =
                    await Communication.Utilities.DownloadImage(profileImageId, _mainForm.MqttCommunication,
                        _cancellationToken);
                if (profileImage != null)
                {
                    secondLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
                    {
                        if (pictureBox.Image != null)
                        {
                            pictureBox.Image.Dispose();
                            pictureBox.Image = null;
                        }

                        pictureBox.Image = profileImage;
                    });
                }
            }
            else
            {
                secondLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
                {
                    if (pictureBox.Image != null)
                    {
                        pictureBox.Image.Dispose();
                        pictureBox.Image = null;
                    }
                });
            }

            var firstLifeText = callback["FirstLifeText"].FirstOrDefault();
            if (!string.IsNullOrEmpty(firstLifeText))
            {
                firstLifeInfoRichTextBox.Text = firstLifeText;
            }

            var firstLifeImageText = callback["FirstLifeImage"].FirstOrDefault();
            if (!string.IsNullOrEmpty(firstLifeImageText) &&
                Guid.TryParse(firstLifeImageText, out var firstLifeImageId) &&
                firstLifeImageId != Guid.Empty)
            {
                var firstLifeImage =
                    await Communication.Utilities.DownloadImage(firstLifeImageId, _mainForm.MqttCommunication,
                        _cancellationToken);
                if (firstLifeImage != null)
                {
                    firstLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
                    {
                        if (pictureBox.Image != null)
                        {
                            pictureBox.Image.Dispose();
                            pictureBox.Image = null;
                        }

                        pictureBox.Image = firstLifeImage;
                    });
                }
            }
            else
            {
                firstLifeProfilePictureBox.InvokeIfRequired(pictureBox =>
                {
                    if (pictureBox.Image != null)
                    {
                        pictureBox.Image.Dispose();
                        pictureBox.Image = null;
                    }
                });
            }
        }

        private void SecondLifeAboutRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            Process.Start(e.LinkText);
        }

        private void FirstLifeInfoRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            Process.Start(e.LinkText);
        }

        private void AvatarProfileForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _cancellationTokenSource.Cancel();

            if (secondLifeProfilePictureBox.Image != null)
            {
                secondLifeProfilePictureBox.Image.Dispose();
                secondLifeProfilePictureBox.Image = null;
            }

            if (firstLifeProfilePictureBox.Image != null)
            {
                firstLifeProfilePictureBox.Image.Dispose();
                firstLifeProfilePictureBox.Image = null;
            }
        }

        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var index = secondLifeGroupsListBox.SelectedIndex;
            if (index == -1)
            {
                return;
            }

            var groupName = (string) secondLifeGroupsListBox.Items[index];

            if (_groupProfileForm != null)
            {
                return;
            }

            _groupProfileForm = new GroupProfileForm(_mainForm, groupName, _mqttCommunication);
            _groupProfileForm.Closing += GroupProfileForm_Closing;
            _groupProfileForm.Show();
        }

        private void GroupProfileForm_Closing(object sender, CancelEventArgs e)
        {
            if (_groupProfileForm == null)
            {
                return;
            }

            _groupProfileForm.Dispose();
            _groupProfileForm = null;
        }

        private void PayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_paymentForm != null)
            {
                return;
            }

            _paymentForm = new PaymentForm(_firstName, _lastName, _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


    }
}

Generated by GNU Enscript 1.6.5.90.