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 { /// /// Represents the default . /// /// /// public class DefaultStorageProvider : ExtendedObject, IStorageProvider { private Thread _scanThread; private Dictionary>> _fileHandlers; /// /// Occurs when a new storage drive has been inserted. /// public event EventHandler StorageConnected; /// /// Occurs when a storage drive has been removed. /// public event EventHandler StorageDisconnected; private bool _isConnected; /// /// Gets a value indicating whether a storage drive is currently inserted. /// public bool IsConnected { get { return _isConnected; } set { _isConnected = value; RaisePropertyChangedAuto(); } } private DriveInfo _drive; /// /// Gets the inserted storage drive information. /// public DriveInfo Drive { get { return _drive; } set { _drive = value; RaisePropertyChangedAuto(); } } /// /// Initializes a new instance of the class. /// public DefaultStorageProvider(IPPCApplicationManager applicationManager) { _fileHandlers = new Dictionary>>(); var drives = DriveInfo.GetDrives().Where(x => x.DriveType == DriveType.Removable).ToList(); if (drives.Count > 0) { IsConnected = true; Drive = drives.FirstOrDefault(); } applicationManager.ApplicationReady += ApplicationManager_ApplicationReady; } /// /// Handles the ApplicationReady event of the ApplicationManager. /// /// The source of the event. /// The instance containing the event data. private void ApplicationManager_ApplicationReady(object sender, EventArgs e) { Initialize(); } /// /// Registers a method for handling a file selection. /// /// The file extension. /// The handler. /// Cannot register multiple file handlers for the same extension. public void RegisterFileHandler(string extension, Action> handler) { if (_fileHandlers.ContainsKey(extension)) { throw new InvalidOperationException("Cannot register multiple file handlers for the same extension."); } _fileHandlers.Add(extension, handler); } /// /// Unregisters the file handler. /// /// The handler. public void UnregisterFileHandler(Action> handler) { var h = _fileHandlers.SingleOrDefault(x => x.Value == handler); if (h.Key != null) { _fileHandlers.Remove(h.Key); } } /// /// Submits a file selection. /// /// The file item. public void SubmitFileSelection(List 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); } } } }