/// <summary> /// Travserse the specified folder and looks for media files in it's sub folders /// </summary> /// <param name="folder">Folder to search</param> private void TraverseFolder(DirectoryInfo folder) { try { SelectedFolderPath = folder.FullName; foreach (FileType fileType in FileTypes) { foreach (IntelligentString extension in fileType.Extensions) { foreach (FileInfo fi in folder.GetFiles("*" + extension.Value)) { if (!MediaItem.MediaItemPartLocationExists(fi.FullName)) { MediaItem mediaItem; switch (fileType.MediaItemType) { case MediaItemTypeEnum.Song: mediaItem = new Song(); break; case MediaItemTypeEnum.Video: mediaItem = new Video(); break; default: throw new UnknownEnumValueException(fileType.MediaItemType); } mediaItem.ParseFilename(fi.FullName); mediaItem.Parts.Add(fi.FullName, fi.Length, MediaItem.GetDuration(fi.FullName)); OnFoundMediaItem(mediaItem); } } } } foreach (DirectoryInfo subFolder in folder.GetDirectories()) { TraverseFolder(subFolder); } } catch (UnauthorizedAccessException) { } }
/// <summary> /// Browses for a media file to add to the media item parts collection /// </summary> public void BrowseForMediaFiles() { try { if (IsOrganising) { GeneralMethods.MessageBoxApplicationError("Please wait for the library to finish organising before editing any media items"); return; } FileType[] fileTypes = App.GetFileTypesForMediaItem(MediaItem); if (fileTypes != null) { String[] filenames = BrowseForMediaFilenames(fileTypes, true); if (filenames.Length > 0) { Dictionary <MediaItemPart, TimeSpan> parts = new Dictionary <MediaItemPart, TimeSpan>(); foreach (String filename in filenames) { if (MediaItem.MediaItemPartLocationExists(filename)) { GeneralMethods.MessageBoxApplicationError("There is already a " + MediaItem.Type.ToString().ToLower() + " in the system with the filename: \"" + filename + "\""); return; } FileInfo fi = new FileInfo(filename); if (!fileTypes.Any(p => p.Extensions.Any(e => e.Value.ToLower() == fi.Extension.ToLower()))) { try { FileType fileType = GetFileTypeForExtension(fileTypes, fi.Extension, MediaItem.Type); if (fileType != null) { fileType.Save(); if (!fileTypes.Contains(fileType)) { OnFileTypeAdded(fileType); } } } catch (System.Exception e) { GeneralMethods.MessageBoxException(e, "Could not add extension to file type: "); } } MediaItemPart part = new MediaItemPart(fi.FullName) { Size = fi.Length, Index = Convert.ToInt16(MediaItem.Parts.Count) }; //store the duration of the part parts.Add(part, MediaItem.GetDuration(fi.FullName)); } foreach (KeyValuePair <MediaItemPart, TimeSpan> part in parts) { MediaItemPart newPart = MediaItem.Parts.Add(part.Key.Location, part.Key.Size, part.Key.Duration); MediaItem.SetPartDuration(newPart.Index, part.Value); } OnPartAdded(parts.Keys.ToArray()); } } } catch (System.Exception e) { GeneralMethods.MessageBoxException(e, "Could not add part: "); } }
private void btnBrowsePart_Click(object sender, RoutedEventArgs e) { try { if (IsOrganising) { GeneralMethods.MessageBoxApplicationError("Please wait for the library to finish organising before editing any media items"); return; } Button btnBrowsePart = sender as Button; MediaItemExists mediaItemExists = btnBrowsePart.DataContext as MediaItemExists; String[] filenames = MediaItemPartsView.BrowseForMediaFilenames(FileTypes.ToArray(), false); if (filenames.Length == 1) { String filename = filenames[0]; if (MediaItem.MediaItemPartLocationExists(filename)) { GeneralMethods.MessageBoxApplicationError("There is already a " + mediaItemExists.MediaItem.Type.ToString().ToLower() + " in the system with the filename: \"" + filename + "\""); return; } FileInfo fi = new FileInfo(filename); if (!FileTypes.Any(p => p.Extensions.Any(ext => ext.Value.ToLower() == fi.Extension.ToLower()))) { try { FileType fileType = MediaItemPartsView.GetFileTypeForExtension(FileTypes.ToArray(), fi.Extension, mediaItemExists.MediaItem.Type); if (fileType != null) { fileType.Save(); if (!FileTypes.Contains(fileType)) { FileTypes.Add(fileType); OnFileTypeAdded(fileType); } } } catch (System.Exception ex) { GeneralMethods.MessageBoxException(ex, "Could not add extension to file type: "); } } mediaItemExists.Part.Location = filename; mediaItemExists.Part.Size = fi.Length; mediaItemExists.MediaItem.SetPartDuration(mediaItemExists.Part.Index, MediaItem.GetDuration(fi.FullName)); mediaItemExists.MediaItem.Save(); ItemsSource.Remove(mediaItemExists); if (ItemsSource.Count == 0) { DialogResult = true; } } } catch (System.Exception ex) { GeneralMethods.MessageBoxException(ex, "Could not locate file: "); } }