/// <summary>
        /// Checks what drives are present and signals listeners when a medium is inserted or removed.
        /// </summary>
        void DetectDriveMedium()
        {
            // Loop through all current drives
            string[] driveLetters = Environment.GetLogicalDrives();
            var foundDrives = new Dictionary<DriveInfo, string>();
            foreach (var l in driveLetters) {

                // Get drive info for this drive
                var driveInfo = new System.IO.DriveInfo(l);

                // Skip if drive is not ready
                if (!driveInfo.IsReady) continue;

                // Get drive type
                string mediumType = MediumType.Unknown;
                DriveType driveType = DriveType.Unknown;
                try { driveType = driveInfo.DriveType; }
                catch (IOException e) {
                    Log("Cannot get drivetype for drive " + l, e);
                    continue;
                }

                // Switch on type
                switch (driveType) {

                    // We have a disc
                    case DriveType.CDRom:

                        try {
                            if (Directory.GetFiles(l, "*.cda", SearchOption.TopDirectoryOnly).Length > 0) {
                                mediumType = MediumType.AudioCD;
                            }
                            else if (Directory.Exists(l + "VIDEO_TS") && Directory.GetFiles(l + "VIDEO_TS", "*.vob").Length > 0) {
                                mediumType = MediumType.DVDVideo;
                            }
                            else if (File.Exists(l + @"BDMV\index.bdmv")) {
                                mediumType = MediumType.BluRayVideo;
                            }
                            else {
                                mediumType = MediumType.FileStorage;
                            }
                        }
                        catch (Exception e) {
                            // Many things can go wrong
                            Log("Unable to get mediumtype for drive " + l, e);
                            continue;
                        }

                        break;

                    // We have a removable drive (usb stick / hd)
                    case DriveType.Removable:
                        mediumType = MediumType.FileStorage;
                        break;
                }

                // If we support the medium type, store it
                if (mediumType != MediumType.Unknown)
                    foundDrives[driveInfo] = mediumType;

            }

            // Check which drives are new and which are no longer present
            List<DriveMedium> currentDriveMedia = new List<DriveMedium>();
            foreach (var d in foundDrives) {

                // Find medium
                DriveMedium existingDriveMedium = _driveMedia.FirstOrDefault(m => m.Is(d.Key, d.Value));

                // New medium!
                if (existingDriveMedium == null) {
                    DriveMedium newDriveMedium;
                    try { newDriveMedium = new DriveMedium(d.Key, d.Value); }
                    catch (Exception e) {
                        Log("Unable to get drive information for drive " + d.Key.Name, e);
                        continue;
                    }
                    newDriveMedium.Save();
                    currentDriveMedia.Add(newDriveMedium);
                    Log("New drive medium: " + newDriveMedium.Name);

                    // TODO: remove this debugging stuff
                    if (newDriveMedium.Type == MediumType.FileStorage) {
                        new FilesystemMusicContainer("Music Everywhere", newDriveMedium).Save();
                        new FilesystemMusicContainer("More Music", newDriveMedium).Save();
                        new FilesystemVideosContainer("Videos", newDriveMedium).Save();
                        new FilesystemPicturesContainer("Pictures", newDriveMedium).Save();
                    }
                    else if (newDriveMedium.Type == MediumType.AudioCD) {
                        new DiscMusicContainer("Superstrings 2", newDriveMedium).Save();
                    }
                    // /TODO

                }

                // Medium already exists
                else {
                    _driveMedia.Remove(existingDriveMedium);
                    currentDriveMedia.Add(existingDriveMedium);
                }

            }

            // Media that are still present in _media, must have been removed
            foreach (var m in _driveMedia) {
                Log("Removed drive medium: " + m.Name);
                //foreach (var c in m.Containers)
                //    c.Dispose();
                m.Dispose();
            }

            // Set the new current media
            _driveMedia = currentDriveMedia;
        }
Example #2
0
        /// <summary>
        /// Checks what drives are present and signals listeners when a medium is inserted or removed.
        /// </summary>
        void DetectDriveMedium()
        {
            // Loop through all current drives
            string[] driveLetters = Environment.GetLogicalDrives();
            var      foundDrives  = new Dictionary <DriveInfo, Type>();

            foreach (var l in driveLetters)
            {
                // Get drive info for this drive
                var driveInfo = new System.IO.DriveInfo(l);

                // Skip if drive is not ready
                if (!driveInfo.IsReady)
                {
                    continue;
                }

                // Get drive type
                Type      mediumType = typeof(Medium);
                DriveType driveType  = DriveType.Unknown;
                try { driveType = driveInfo.DriveType; }
                catch (IOException e) {
                    Log("Cannot get drivetype for drive " + l, e);
                    continue;
                }

                // Switch on type
                switch (driveType)
                {
                // We have a disc
                case DriveType.CDRom:

                    try {
                        if (Directory.GetFiles(l, "*.cda", SearchOption.TopDirectoryOnly).Length > 0)
                        {
                            mediumType = typeof(AudioDiscMedium);
                        }
                        else if (Directory.Exists(l + "VIDEO_TS") && Directory.GetFiles(l + "VIDEO_TS", "*.vob").Length > 0)
                        {
                            mediumType = typeof(DVDVideoMedium);
                        }
                        else if (File.Exists(l + @"BDMV\index.bdmv"))
                        {
                            mediumType = typeof(BlurayVideoMedium);
                        }
                        else
                        {
                            mediumType = typeof(DataDiscMedium);
                        }
                    }
                    catch (Exception e) {
                        // Many things can go wrong
                        Log("Unable to get mediumtype for drive " + l, e);
                        continue;
                    }

                    break;

                // We have a removable drive (usb stick / hd)
                case DriveType.Removable:
                    mediumType = typeof(RemoveableDriveMedium);
                    break;
                }

                // If we support the medium type, store it
                if (mediumType != typeof(Medium))
                {
                    foundDrives[driveInfo] = mediumType;
                }
            }


            // Check which drives are new and which are no longer present
            List <DriveMedium> currentDriveMedia = new List <DriveMedium>();

            foreach (var d in foundDrives)
            {
                // Find medium
                DriveMedium existingDriveMedium = _driveMedia.FirstOrDefault(m => m.Is(d.Key, d.Value));

                // New medium!
                if (existingDriveMedium == null)
                {
                    DriveMedium newDriveMedium;
                    try {
                        newDriveMedium = (DriveMedium)Activator.CreateInstance(d.Value, new object[] { d.Key });
                    }
                    catch (Exception e) {
                        Log("Unable to get drive information for drive " + d.Key.Name, e);
                        continue;
                    }
                    newDriveMedium.Save();
                    currentDriveMedia.Add(newDriveMedium);
                    Log("New drive medium: " + newDriveMedium.Name);



                    // TODO: remove this debugging stuff
                    //if (newDriveMedium.Type == MediumType.FileStorage) {
                    //    new FilesystemMusicContainer("Music Everywhere", newDriveMedium).Save();
                    //    new FilesystemMusicContainer("More Music", newDriveMedium).Save();
                    //    new FilesystemVideosContainer("Videos", newDriveMedium).Save();
                    //    new FilesystemPicturesContainer("Pictures", newDriveMedium).Save();
                    //}
                    //else if (newDriveMedium.Type == MediumType.AudioCD) {
                    //    new DiscMusicContainer("Superstrings 2", newDriveMedium).Save();
                    //}
                    // /TODO
                }

                // Medium already exists
                else
                {
                    _driveMedia.Remove(existingDriveMedium);
                    currentDriveMedia.Add(existingDriveMedium);
                }
            }

            // Media that are still present in _media, must have been removed
            foreach (var m in _driveMedia)
            {
                Log("Removed drive medium: " + m.Name);
                //foreach (var c in m.Containers)
                //    c.Dispose();
                m.Dispose();
            }

            // Set the new current media
            _driveMedia = currentDriveMedia;
        }