QuickImage – Rev 9

Subversion Repositories:
Rev:
using System;
using System.Threading;
using System.Windows.Forms;
using LibVLCSharp.Shared;
using QuickImage.Utilities;
using Serilog;
using Media = LibVLCSharp.Shared.Media;

namespace QuickImage
{
    public partial class PreviewForm : Form
    {
        private readonly string _fileName;
        private LibVLC _libVLC;
        private Media _media;
        private readonly Configuration.Configuration _configuration;
        private readonly CancellationToken _cancellationToken;
        private readonly MagicMime _magicMime;

        private PreviewForm()
        {
            InitializeComponent();
        }

        public PreviewForm(string fileName, global::Configuration.Configuration configuration, MagicMime magicMime, CancellationToken cancellationToken) : this()
        {
            _magicMime = magicMime;
            _fileName = fileName;
            _configuration = configuration;
            _cancellationToken = cancellationToken;
        }

        /// <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))
            {
                _media?.Dispose();
                _media = null;
                _libVLC?.Dispose();

                components.Dispose();
            }

            try
            {
                base.Dispose(disposing);
            }
            catch(InvalidOperationException)
            {
                // can happen if the form closed too soon, ignore it
            }
        }

        private void MediaPlayer_EndReached(object sender, EventArgs e)
        {
            //rewind and play
            if (_media == null)
            {
                _media.Dispose();
            }

            _media = new Media(_libVLC, _fileName);
            videoView1.InvokeIfRequired(view =>
            {
                view.MediaPlayer.Play(_media);
            });
        }

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

            try
            {
                Core.Initialize();
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Could not initialize libVLC.");

                Close();
                return;
            }

            _libVLC = new LibVLC();
            videoView1.MediaPlayer = new MediaPlayer(_libVLC);
            videoView1.MediaPlayer.EndReached += MediaPlayer_EndReached;
            videoView1.MediaPlayer.EnableHardwareDecoding = true;

            try
            {
                var mime = await _magicMime.GetMimeType(_fileName, _cancellationToken);

                if (_configuration.SupportedFormats.IsSupportedImage(mime))
                {
                    pictureBox1.ImageLocation = _fileName;
                    pictureBox1.BringToFront();
                    return;
                }

                if (_configuration.SupportedFormats.IsSupportedVideo(mime))
                {
                    _media = new Media(_libVLC, _fileName);
                    videoView1.MediaPlayer.Play(_media);
                    videoView1.BringToFront();
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception, "Could not identify file");

                Close();
            }
        }

        private void PreviewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            videoView1.MediaPlayer?.Stop();
        }
    }
}