QuickImage – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Configuration;

namespace QuickImage
{
    public partial class SettingsForm : Form
    {
        public bool SaveOnClose { get; private set; } 

        private readonly CancellationToken _cancellationToken;
        private readonly global::Configuration.Configuration _configuration;

        private SettingsForm()
        {
            InitializeComponent();

            SaveOnClose = false;
        }

        public SettingsForm(global::Configuration.Configuration configuration, CancellationToken cancellationToken) : this()
        {
            _configuration = configuration;
            _cancellationToken = cancellationToken;
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            JotFormTracker.Tracker.Track(this);

            checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration.OutboundDragDrop, nameof(_configuration.OutboundDragDrop.RenameOnDragDrop),
                true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
            checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration.OutboundDragDrop, nameof(_configuration.OutboundDragDrop.StripTagsOnDragDrop),
                true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
            checkBox3.DataBindings.Add(nameof(checkBox3.Checked), _configuration.OutboundDragDrop, nameof(_configuration.OutboundDragDrop.ConvertOnDragDrop),
                true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
            checkBox4.DataBindings.Add(nameof(checkBox4.Checked), _configuration.InboundDragDrop, nameof(_configuration.InboundDragDrop.ConvertOnDragDrop),
                true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
            checkBox5.DataBindings.Add(nameof(checkBox5.Checked), _configuration.InboundDragDrop, nameof(_configuration.InboundDragDrop.CopyOnDragDrop),
                true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);

            comboBox1.DataSource = Enum.GetValues(typeof(DragDropRenameMethod));
            comboBox1.DataBindings.Add(new Binding(nameof(comboBox1.SelectedItem), _configuration.OutboundDragDrop, nameof(_configuration.OutboundDragDrop.DragDropRenameMethod)));
            comboBox2.DataSource = new[] { "image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp" };
            comboBox2.DataBindings.Add(new Binding(nameof(comboBox2.SelectedItem), _configuration.OutboundDragDrop, nameof(_configuration.OutboundDragDrop.DragDropConvertType)));

            comboBox3.DataSource = new[] { "image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp" };
            comboBox3.DataBindings.Add(new Binding(nameof(comboBox3.SelectedItem), _configuration.OutboundDragDrop, nameof(_configuration.InboundDragDrop.DragDropConvertType)));

            if (_configuration is { OutboundDragDrop: { DragDropConvertExclude: {  Images: { Image: { } } } } })
            {
                foreach (var item in _configuration.OutboundDragDrop.DragDropConvertExclude.Images.Image)
                {
                    var idx = checkedListBox3.Items.IndexOf(item);
                    if (idx == -1)
                    {
                        continue;
                    }

                    checkedListBox3.SetItemChecked(idx, true);
                }
            }

            if (_configuration is { InboundDragDrop: { DragDropConvertExclude: { Images: { Image: { } } } } })
            {
                foreach (var item in _configuration.InboundDragDrop.DragDropConvertExclude.Images.Image)
                {
                    var idx = checkedListBox5.Items.IndexOf(item);
                    if (idx == -1)
                    {
                        continue;
                    }

                    checkedListBox5.SetItemChecked(idx, true);
                }
            }

            if (_configuration is { SupportedFormats: { Images: { Image: { } } } })
            {
                foreach (var item in _configuration.SupportedFormats.Images.Image)
                {
                    var idx = checkedListBox1.Items.IndexOf(item);
                    if (idx == -1)
                    {
                        continue;
                    }

                    checkedListBox1.SetItemChecked(idx, true);
                }
            }

            if (_configuration is { SupportedFormats: { Videos: { Video: { } } } })
            {
                foreach (var item in _configuration.SupportedFormats.Videos.Video)
                {
                    var idx = checkedListBox2.Items.IndexOf(item);
                    if (idx == -1)
                    {
                        continue;
                    }

                    checkedListBox2.SetItemChecked(idx, true);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveOnClose = true;

            var images = checkedListBox1.CheckedItems.OfType<string>().ToList();
            _configuration.SupportedFormats.Images = new Images(images);
;            var videos = checkedListBox2.CheckedItems.OfType<string>().ToArray();
            _configuration.SupportedFormats.Videos = new Videos(videos);

            var exclude = checkedListBox3.CheckedItems.OfType<string>().ToList();
            _configuration.OutboundDragDrop.DragDropConvertExclude.Images.Image = exclude;
            exclude = checkedListBox5.CheckedItems.OfType<string>().ToList();
            _configuration.InboundDragDrop.DragDropConvertExclude.Images.Image = exclude;

            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveOnClose = false;
            Close();
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            /*
            foreach (var item in checkedListBox1.Items.)
            {
                var idx = checkedListBox1.Items.IndexOf(item);
                if (idx == -1)
                {
                    continue;
                }

                checkedListBox1.SetItemChecked(idx, true);
            }
            */
        }

        private void button5_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < checkedListBox1.Items.Count; ++i)
            {
                switch (checkedListBox1.GetItemCheckState(i))
                {
                    case CheckState.Unchecked:
                        checkedListBox1.SetItemCheckState(i, CheckState.Checked);
                        break;
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < checkedListBox1.Items.Count; ++i)
            {
                switch (checkedListBox1.GetItemCheckState(i))
                {
                    case CheckState.Checked:
                        checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
                        break;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < checkedListBox2.Items.Count; ++i)
            {
                switch (checkedListBox2.GetItemCheckState(i))
                {
                    case CheckState.Unchecked:
                        checkedListBox2.SetItemCheckState(i, CheckState.Checked);
                        break;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < checkedListBox2.Items.Count; ++i)
            {
                switch (checkedListBox2.GetItemCheckState(i))
                {
                    case CheckState.Checked:
                        checkedListBox2.SetItemCheckState(i, CheckState.Unchecked);
                        break;
                }
            }
        }
    }
}