aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Common/Storage/DefaultStorageProvider.cs
blob: 5f097d303137eac057f01838a3c803cd3a6c7960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.DI;
using Tango.Explorer;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Threading;

namespace Tango.PPC.Common.Storage
{
    /// <summary>
    /// Represents the default <see cref="IStorageProvider"/>.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.PPC.Common.Storage.IStorageProvider" />
    public class DefaultStorageProvider : ExtendedObject, IStorageProvider
    {
        private Thread _scanThread;
        private Dictionary<String, Action<List<ExplorerFileItem>>> _fileHandlers;

        /// <summary>
        /// Occurs when a new storage drive has been inserted.
        /// </summary>
        public event EventHandler<DriveInfo> StorageConnected;

        /// <summary>
        /// Occurs when a storage drive has been removed.
        /// </summary>
        public event EventHandler<DriveInfo> StorageDisconnected;

        private bool _isConnected;
        /// <summary>
        /// Gets a value indicating whether a storage drive is currently inserted.
        /// </summary>
        public bool IsConnected
        {
            get { return _isConnected; }
            set { _isConnected = value; RaisePropertyChangedAuto(); }
        }

        private DriveInfo _drive;
        /// <summary>
        /// Gets the inserted storage drive information.
        /// </summary>
        public DriveInfo Drive
        {
            get { return _drive; }
            set { _drive = value; RaisePropertyChangedAuto(); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultStorageProvider"/> class.
        /// </summary>
        public DefaultStorageProvider(IPPCApplicationManager applicationManager)
        {
            _fileHandlers = new Dictionary<string, Action<List<ExplorerFileItem>>>();
            var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList();

            if (drives.Count > 0)
            {
                IsConnected = true;
                Drive = drives.FirstOrDefault();
            }

            applicationManager.ApplicationReady += ApplicationManager_ApplicationReady;
        }

        /// <summary>
        /// Handles the ApplicationReady event of the ApplicationManager.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ApplicationManager_ApplicationReady(object sender, EventArgs e)
        {
            Initialize();
        }

        /// <summary>
        /// Registers a method for handling a file selection.
        /// </summary>
        /// <param name="extension">The file extension.</param>
        /// <param name="handler">The handler.</param>
        /// <exception cref="System.InvalidOperationException">Cannot register multiple file handlers for the same extension.</exception>
        public void RegisterFileHandler(string extension, Action<List<ExplorerFileItem>> handler)
        {
            if (_fileHandlers.ContainsKey(extension))
            {
                throw new InvalidOperationException("Cannot register multiple file handlers for the same extension.");
            }
            _fileHandlers.Add(extension, handler);
        }

        /// <summary>
        /// Unregisters the file handler.
        /// </summary>
        /// <param name="handler">The handler.</param>
        public void UnregisterFileHandler(Action<List<ExplorerFileItem>> handler)
        {
            var h = _fileHandlers.SingleOrDefault(x => x.Value == handler);

            if (h.Key != null)
            {
                _fileHandlers.Remove(h.Key);
            }
        }

        /// <summary>
        /// Submits a file selection.
        /// </summary>
        /// <param name="fileItems">The file item.</param>
        public void SubmitFileSelection(List<ExplorerFileItem> fileItems)
        {
            if (fileItems != null && fileItems.Count > 0)
            {
                String extension = Path.GetExtension(fileItems.First().Path);

                if (_fileHandlers.ContainsKey(extension))
                {
                    _fileHandlers[extension].Invoke(fileItems);
                }
            }
        }

        private void Initialize()
        {
            _scanThread = new Thread(ScanThreadMethod);
            _scanThread.IsBackground = true;
            _scanThread.Start();
        }

        private void ScanThreadMethod()
        {
            while (true)
            {
                var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList();

                if (IsConnected && !drives.Exists(x => x.RootDirectory.FullName == Drive.RootDirectory.FullName))
                {
                    StorageDisconnected?.Invoke(this, Drive);
                    Drive = null;
                    IsConnected = false;
                }

                if (!IsConnected && drives.Count > 0)
                {
                    Drive = drives.FirstOrDefault();
                    IsConnected = true;
                    StorageConnected?.Invoke(this, Drive);
                }

                Thread.Sleep(5000);
            }
        }
    }
}