diff --git a/MPfm/MPfm.MVP/Navigation/NavigationManager.cs b/MPfm/MPfm.MVP/Navigation/NavigationManager.cs index 48eb3baa..484954a8 100644 --- a/MPfm/MPfm.MVP/Navigation/NavigationManager.cs +++ b/MPfm/MPfm.MVP/Navigation/NavigationManager.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; +using MPfm.Library.UpdateLibrary; using MPfm.MVP.Bootstrap; using TinyIoC; using MPfm.MVP.Views; @@ -254,5 +255,29 @@ public virtual IPlaylistView CreatePlaylistView() }; return _playlistView; } + + public virtual IUpdateLibraryView CreateUpdateLibraryView(UpdateLibraryMode mode, List filePaths, string folderPath) + { + if (_updateLibraryView != null) + { + _updateLibraryView.ShowView(true); + return _updateLibraryView; + } + + Action onViewReady = (view) => + { + _updateLibraryPresenter = Bootstrapper.GetContainer().Resolve(); + _updateLibraryPresenter.BindView((IUpdateLibraryView)view); + _updateLibraryPresenter.UpdateLibrary(mode, filePaths, folderPath); + }; + + _updateLibraryView = Bootstrapper.GetContainer().Resolve(new NamedParameterOverloads() { { "onViewReady", onViewReady } }); + _updateLibraryView.OnViewDestroy = (view) => + { + _updateLibraryView = null; + _updateLibraryPresenter = null; + }; + return _updateLibraryView; + } } } diff --git a/MPfm/MPfm.MVP/Presenters/MainPresenter.cs b/MPfm/MPfm.MVP/Presenters/MainPresenter.cs index f9be7a37..57149038 100644 --- a/MPfm/MPfm.MVP/Presenters/MainPresenter.cs +++ b/MPfm/MPfm.MVP/Presenters/MainPresenter.cs @@ -15,10 +15,11 @@ // You should have received a copy of the GNU General Public License // along with MPfm. If not, see . +using System.Collections.Generic; +using MPfm.Library.UpdateLibrary; using MPfm.MVP.Navigation; using MPfm.MVP.Presenters.Interfaces; using MPfm.MVP.Views; -using System; namespace MPfm.MVP.Presenters { @@ -27,42 +28,60 @@ namespace MPfm.MVP.Presenters /// public class MainPresenter : BasePresenter, IMainPresenter { - readonly NavigationManager _navigationManager; - + readonly NavigationManager _navigationManager; + public MainPresenter(NavigationManager navigationManager) { - _navigationManager = navigationManager; + _navigationManager = navigationManager; } - - public override void BindView(IMainView view) - { - view.OnOpenPlaylistWindow = OpenPlaylistWindow; - view.OnOpenEffectsWindow = OpenEffectsWindow; - view.OnOpenPreferencesWindow = OpenPreferencesWindow; - view.OnOpenSyncWindow = OpenSyncWindow; + + public override void BindView(IMainView view) + { + view.OnOpenPlaylistWindow = OpenPlaylistWindow; + view.OnOpenEffectsWindow = OpenEffectsWindow; + view.OnOpenPreferencesWindow = OpenPreferencesWindow; + view.OnOpenSyncWindow = OpenSyncWindow; + view.OnAddFilesToLibrary = AddFilesToLibrary; + view.OnAddFolderToLibrary = AddFolderToLibrary; + view.OnUpdateLibrary = UpdateLibrary; - base.BindView(view); - } - - void OpenPlaylistWindow() - { - _navigationManager.CreatePlaylistView(); - } + base.BindView(view); + } - void OpenEffectsWindow() + void OpenPlaylistWindow() + { + _navigationManager.CreatePlaylistView(); + } + + void OpenEffectsWindow() + { + _navigationManager.CreateEffectsView(); + } + + void OpenPreferencesWindow() + { + _navigationManager.CreatePreferencesView(); + } + + void OpenSyncWindow() + { + _navigationManager.CreateSyncView(); + } + + void AddFolderToLibrary(string folderPath) { - _navigationManager.CreateEffectsView(); + _navigationManager.CreateUpdateLibraryView(UpdateLibraryMode.SpecificFolder, null, folderPath); } - void OpenPreferencesWindow() + void AddFilesToLibrary(List filePaths) { - _navigationManager.CreatePreferencesView(); + _navigationManager.CreateUpdateLibraryView(UpdateLibraryMode.SpecificFiles, filePaths, null); } - void OpenSyncWindow() + void UpdateLibrary() { - _navigationManager.CreateSyncView(); - } - } + _navigationManager.CreateUpdateLibraryView(UpdateLibraryMode.WholeLibrary, null, null); + } + } } diff --git a/MPfm/MPfm.MVP/Views/IMainView.cs b/MPfm/MPfm.MVP/Views/IMainView.cs index 692a1f2e..c4812581 100644 --- a/MPfm/MPfm.MVP/Views/IMainView.cs +++ b/MPfm/MPfm.MVP/Views/IMainView.cs @@ -16,6 +16,7 @@ // along with MPfm. If not, see . using System; +using System.Collections.Generic; namespace MPfm.MVP.Views { @@ -28,5 +29,9 @@ public interface IMainView : ILibraryBrowserView, ISongBrowserView, IPlayerView Action OnOpenEffectsWindow { get; set; } Action OnOpenPlaylistWindow { get; set; } Action OnOpenSyncWindow { get; set; } + + Action> OnAddFilesToLibrary { get; set; } + Action OnAddFolderToLibrary { get; set; } + Action OnUpdateLibrary { get; set; } } } diff --git a/MPfm/MPfm.Windows/Classes/Forms/frmMain.cs b/MPfm/MPfm.Windows/Classes/Forms/frmMain.cs index 6898c6e4..fd95c871 100644 --- a/MPfm/MPfm.Windows/Classes/Forms/frmMain.cs +++ b/MPfm/MPfm.Windows/Classes/Forms/frmMain.cs @@ -866,16 +866,10 @@ public void ExitApplication() /// Event arguments private void miFileAddFiles_Click(object sender, EventArgs e) { - // Display dialog if (dialogAddFiles.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) - { - // The user has cancelled the operation return; - } - //// Update the library using the specified folder - //formUpdateLibraryStatus = new frmUpdateLibraryStatus(this, dialogAddFiles.FileNames.ToList()); - //formUpdateLibraryStatus.ShowDialog(this); + OnAddFilesToLibrary(dialogAddFiles.FileNames.ToList()); } /// @@ -886,30 +880,10 @@ private void miFileAddFiles_Click(object sender, EventArgs e) /// Event arguments private void miFileAddFolder_Click(object sender, EventArgs e) { - // Display dialog if (dialogAddFolder.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) - { - // The user has cancelled the operation return; - } - //// Update the library using the specified folder - //formUpdateLibraryStatus = new frmUpdateLibraryStatus(this, dialogAddFolder.SelectedPath); - //formUpdateLibraryStatus.ShowDialog(this); - - //// Show panel - //ShowUpdateLibraryProgress(true); - - //// Start update timer to display progress - //timerUpdateLibrary.Start(); - - //// Start new thread - //new Thread(delegate() - //{ - // List filePaths = AudioTools.SearchAudioFilesRecursive(dialogAddFolder.SelectedPath, "MP3;FLAC;OGG;MPC;APE;WV"); - // updateLibrary = new Library.UpdateLibrary(1, library.Gateway.DatabaseFilePath); - // Task> audioFiles = updateLibrary.LoadFiles(filePaths); - //}).Start(); + OnAddFolderToLibrary(dialogAddFolder.SelectedPath); } /// @@ -1221,12 +1195,7 @@ private void miHelpAbout_Click(object sender, EventArgs e) /// Event arguments private void btnUpdateLibrary_Click(object sender, EventArgs e) { - // Display contextual menu - //menuToolbarLibrary.Show(btnUpdateLibrary, new Point(0, btnUpdateLibrary.Height)); - //toolStripMain.Height; - - // Update the whole library - UpdateLibrary(); + OnUpdateLibrary(); } /// @@ -3402,47 +3371,23 @@ public void EnableTooltips(bool enable) #region IMainView implementation - public Action OnViewDestroy { get; set; } - public Action OnAudioFileFormatFilterChanged { get; set; } - public Action OnTreeNodeSelected { get; set; } - public Action OnTreeNodeDoubleClicked { get; set; } - public Action OnTreeNodeExpanded { get; set; } - public Func> OnTreeNodeExpandable { get; set; } - public Action OnTableRowDoubleClicked { get; set; } - public Action OnOpenPreferencesWindow { get; set; } public Action OnOpenEffectsWindow { get; set; } public Action OnOpenPlaylistWindow { get; set; } public Action OnOpenSyncWindow { get; set; } + public Action> OnAddFilesToLibrary { get; set; } + public Action OnAddFolderToLibrary { get; set; } + public Action OnUpdateLibrary { get; set; } - public Action OnPlayerPlay { get; set; } - public Action> OnPlayerPlayFiles { get; set; } - public Action OnPlayerPause { get; set; } - public Action OnPlayerStop { get; set; } - public Action OnPlayerPrevious { get; set; } - public Action OnPlayerNext { get; set; } - public Action OnPlayerSetVolume { get; set; } - public Action OnPlayerSetPitchShifting { get; set; } - public Action OnPlayerSetTimeShifting { get; set; } - public Action OnPlayerSetPosition { get; set; } - public Func OnPlayerRequestPosition { get; set; } + #endregion - public void PlayerError(Exception ex) - { - MethodInvoker methodUIUpdate = delegate - { - MessageBox.Show(string.Format("An error occured in Player: {0}", ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - }; + #region ILibraryBrowserView implementation - if (InvokeRequired) - BeginInvoke(methodUIUpdate); - else - methodUIUpdate.Invoke(); - } - - public void ShowView(bool shown) - { - } + public Action OnAudioFileFormatFilterChanged { get; set; } + public Action OnTreeNodeSelected { get; set; } + public Action OnTreeNodeDoubleClicked { get; set; } + public Action OnTreeNodeExpanded { get; set; } + public Func> OnTreeNodeExpandable { get; set; } public void RefreshLibraryBrowser(IEnumerable entities) { @@ -3489,8 +3434,9 @@ public void RefreshLibraryBrowser(IEnumerable entities) public void RefreshLibraryBrowserNode(LibraryBrowserEntity entity, IEnumerable entities, object userData) { Console.WriteLine("frmMain - RefreshLibraryBrowserNode - entities.Count: {0}", entities.Count()); - MethodInvoker methodUIUpdate = delegate { - var node = (TreeNode) userData; + MethodInvoker methodUIUpdate = delegate + { + var node = (TreeNode)userData; treeLibraryBrowser.BeginUpdate(); foreach (var childEntity in entities) @@ -3525,10 +3471,45 @@ public void RefreshLibraryBrowserNode(LibraryBrowserEntity entity, IEnumerable OnTableRowDoubleClicked { get; set; } + public void RefreshSongBrowser(IEnumerable audioFiles) { } + #endregion + + #region IPlayerView implementation + + public Action OnPlayerPlay { get; set; } + public Action> OnPlayerPlayFiles { get; set; } + public Action OnPlayerPause { get; set; } + public Action OnPlayerStop { get; set; } + public Action OnPlayerPrevious { get; set; } + public Action OnPlayerNext { get; set; } + public Action OnPlayerSetVolume { get; set; } + public Action OnPlayerSetPitchShifting { get; set; } + public Action OnPlayerSetTimeShifting { get; set; } + public Action OnPlayerSetPosition { get; set; } + public Func OnPlayerRequestPosition { get; set; } + + public void PlayerError(Exception ex) + { + MethodInvoker methodUIUpdate = delegate + { + MessageBox.Show(string.Format("An error occured in Player: {0}", ex), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + }; + + if (InvokeRequired) + BeginInvoke(methodUIUpdate); + else + methodUIUpdate.Invoke(); + } + public void RefreshPlayerStatus(PlayerStatusType status) { } diff --git a/MPfm/MPfm.Windows/Classes/Forms/frmUpdateLibrary.cs b/MPfm/MPfm.Windows/Classes/Forms/frmUpdateLibrary.cs index eb6985e9..c9a9d161 100644 --- a/MPfm/MPfm.Windows/Classes/Forms/frmUpdateLibrary.cs +++ b/MPfm/MPfm.Windows/Classes/Forms/frmUpdateLibrary.cs @@ -38,68 +38,23 @@ public frmUpdateLibrary(Action onViewReady) { InitializeComponent(); ViewIsReady(); - - //lblTitle.Text = "Updating"; - //lblMessage.Text = "Calculating..."; } - /// - /// Fires when the form is shown. The form is shown when the user has started the update process. - /// - /// Sender - /// Arguments private void frmUpdateLibraryStatus_Shown(object sender, EventArgs e) { - // Stop any song playing - //Main.Player.Stop(); - - // Reset buttons btnCancel.Enabled = true; btnOK.Enabled = false; btnSaveLog.Enabled = false; - - //// Reset log - //sbLog = new StringBuilder(1000); - - //// Start timer - //timerEnabled = true; - //workerTimer.RunWorkerAsync(); - - //// Reset variables - //startTime = DateTime.Now; - //startTimeAddFiles = DateTime.MinValue; - //listAlbums = new List(); - - // Update UI - lblEstimatedTimeLeft.Text = "Estimated time left: Calculating..."; - - // Start the update process - //Main.Library.UpdateLibrary(mode, FilePaths, FolderPath); } private void btnOK_Click(object sender, EventArgs e) { - // Refresh controls on the main form - Cursor.Current = Cursors.WaitCursor; - - // Reset query and refresh all controls - //Main.ResetQuery(); - //Main.RefreshAll(); - - Cursor.Current = Cursors.Default; - - // Close this form - this.Close(); + Close(); } private void btnCancel_Click(object sender, EventArgs e) { - //// Set the cancel update library flag to true - //Main.Library.CancelUpdateLibrary = true; - - //// Reset buttons - //btnOK.Enabled = true; - //btnCancel.Enabled = false; + OnCancelUpdateLibrary(); } private void btnSaveLog_Click(object sender, EventArgs e) @@ -133,187 +88,6 @@ private void btnSaveLog_Click(object sender, EventArgs e) //} } - ///// - ///// Fires when the update library process sends a progress changed message. - ///// - ///// Update data - //private void Library_OnUpdateLibraryProgress(UpdateLibraryProgressData data) - //{ - // //// Invoke UI updates - // //MethodInvoker methodUIUpdate = delegate - // //{ - // // // Check if there is an error - // // if (data.Error != null) - // // { - // // //string errorMessage = "Error: Cannot add this file to the library - " + status.errorInformation + "\n\nFile Path: " + status.filePath + "\nReason: " + status.error.Message; - // // string errorMessage; - - // // if (!String.IsNullOrEmpty(data.FilePath)) - // // { - // // errorMessage = data.FilePath + "\n"; - // // } - // // else - // // { - // // errorMessage = "====================\n"; - // // } - - // // errorMessage += " Error: " + data.Error.Message; - - // // if (data.Error.InnerException != null) - // // { - // // errorMessage += "\n " + data.Error.InnerException.Message; - // // } - // // errorMessage += "\n"; - - // // // Log for file - // // sbLog.Insert(0, errorMessage); - - // // // Add to log screen - // // lbLog.Items.Insert(0, "Error reading " + Path.GetFileName(data.FilePath) + ": " + data.Error.Message); - - // // lblMessage.Text = data.Message; - // // lblProgress.Text = data.Percentage.ToString("0.000") + " %"; - // // progressBar.Value = Convert.ToInt32(data.Percentage); - // // } - // // else - // // { - // // // Update UI - // // if (data.CurrentFilePosition > 0 && data.TotalNumberOfFiles > 0) - // // { - // // // Set start time when the process has finished finding the files and is ready to add files into library - // // if (startTimeAddFiles == DateTime.MinValue) - // // { - // // startTimeAddFiles = DateTime.Now; - // // } - - // // // Calculate time elapsed - // // TimeSpan timeElapsed = DateTime.Now.Subtract(startTimeAddFiles); - - // // // Update title - // // lblTitle.Text = data.Title + " (file " + data.CurrentFilePosition.ToString() + " of " + data.TotalNumberOfFiles.ToString() + ")"; - - // // // Calculate time remaining - // // double msPerFile = timeElapsed.TotalMilliseconds / data.CurrentFilePosition; - // // double remainingTime = (data.TotalNumberOfFiles - data.CurrentFilePosition) * msPerFile; - // // TimeSpan timeRemaining = new TimeSpan(0, 0, 0, 0, (int)remainingTime); - - // // // Update estimated time left (from more precise to more vague) - // // if (timeRemaining.TotalSeconds == 0) - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : N/A"; - // // } - // // else if (timeRemaining.Minutes == 1) - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : 1 minute"; - // // } - // // else if (timeRemaining.TotalSeconds <= 10) - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : A few seconds"; - // // } - // // else if (timeRemaining.TotalSeconds <= 30) - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : Less than 30 seconds"; - // // } - // // else if (timeRemaining.TotalSeconds <= 60) - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : Less than a minute"; - // // } - // // else - // // { - // // lblEstimatedTimeLeft.Text = "Estimated time left : " + timeRemaining.Minutes.ToString() + " minutes"; - // // } - // // } - // // else - // // { - // // lblTitle.Text = data.Title; - // // } - // // lblMessage.Text = data.Message; - // // lblProgress.Text = data.Percentage.ToString("0.000") + " %"; - // // progressBar.Value = Convert.ToInt32(data.Percentage); - - // // if (!String.IsNullOrEmpty(data.LogEntry)) - // // { - // // sbLog.Insert(0, data.LogEntry + "\n"); - // // } - - // // // Update song if exists - // // if (data.Song != null) - // // { - // // //lblArtist.Text = data.Song.ArtistName; - // // //lblAlbum.Text = data.Song.AlbumTitle; - // // //lblSongTitle.Text = data.Song.SongTitle; - - // // // Check if the folder has already been treated (we want to get the ID3 - // // // image just once per album. This speeds up the update library process a lot.) - - // // ////string path = Path.GetDirectoryName(data.FilePath); - // // //string artistAlbum = data.Song.ArtistName + " - " + data.Song.AlbumTitle; - // // //if (!listAlbums.Contains(artistAlbum)) - // // //{ - // // // try - // // // { - // // // // Get album art from ID3 tag or folder.jpg - // // // Image image = MPfm.Library.Library.GetAlbumArtFromID3OrFolder(data.FilePath); - - // // // // If image is null... - // // // if (image == null) - // // // { - // // // // Display nothing - // // // picAlbum.Image = null; - // // // } - // // // else - // // // { - // // // // Update the album art - // // // picAlbum.Image = ImageManipulation.ResizeImage(image, picAlbum.Size.Width, picAlbum.Size.Height); - // // // } - - // // // // Add the folder in the list of folders - // // // listAlbums.Add(artistAlbum); - // // // } - // // // catch (Exception ex) - // // // { - // // // // Do nothing since this is only album art. - // // // } - // // //} - - // // //lbLog.Items.Insert(0, data.LogEntry); - // // //if (lbLog.Items.Count > 1000) - // // //{ - // // // lbLog.Items.RemoveAt(lbLog.Items.Count - 1); - // // //} - // // } - // // else - // // { - // // //lblArtist.Text = string.Empty; - // // //lblAlbum.Text = string.Empty; - // // //lblSongTitle.Text = string.Empty; - // // //picAlbum.Image = null; - // // } - // // } - // //}; - - // //// Check if invoking is necessary - // //if (InvokeRequired) - // //{ - // // BeginInvoke(methodUIUpdate); - // //} - // //else - // //{ - // // methodUIUpdate.Invoke(); - // //} - - // ////if (data.ProgressUndefined) - // ////{ - // //// progressBar.Style = ProgressBarStyle.Continuous; - // ////} - // ////else - // ////{ - // //// progressBar.Style = ProgressBarStyle.Marquee; - // //// lblProgress.Text = data.Percentage.ToString("0.00") + " %"; - // //// progressBar.Value = Convert.ToInt32(data.Percentage); - // ////} - //} - #region IUpdateLibraryView implementation public Action, string> OnStartUpdateLibrary { get; set; } @@ -321,14 +95,94 @@ private void btnSaveLog_Click(object sender, EventArgs e) public void RefreshStatus(UpdateLibraryEntity entity) { + MethodInvoker methodUIUpdate = delegate + { + lblTitle.Text = entity.Title; + lblMessage.Text = entity.Subtitle; + lblProgress.Text = string.Format("{0:0.0} %", entity.PercentageDone * 100); + progressBar.Value = (int)entity.PercentageDone; + + // TODO: Add time estimation like below + + // // // Set start time when the process has finished finding the files and is ready to add files into library + // // if (startTimeAddFiles == DateTime.MinValue) + // // { + // // startTimeAddFiles = DateTime.Now; + // // } + + // // // Calculate time elapsed + // // TimeSpan timeElapsed = DateTime.Now.Subtract(startTimeAddFiles); + + // // // Update title + // // lblTitle.Text = data.Title + " (file " + data.CurrentFilePosition.ToString() + " of " + data.TotalNumberOfFiles.ToString() + ")"; + + // // // Calculate time remaining + // // double msPerFile = timeElapsed.TotalMilliseconds / data.CurrentFilePosition; + // // double remainingTime = (data.TotalNumberOfFiles - data.CurrentFilePosition) * msPerFile; + // // TimeSpan timeRemaining = new TimeSpan(0, 0, 0, 0, (int)remainingTime); + + // // // Update estimated time left (from more precise to more vague) + // // if (timeRemaining.TotalSeconds == 0) + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : N/A"; + // // } + // // else if (timeRemaining.Minutes == 1) + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : 1 minute"; + // // } + // // else if (timeRemaining.TotalSeconds <= 10) + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : A few seconds"; + // // } + // // else if (timeRemaining.TotalSeconds <= 30) + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : Less than 30 seconds"; + // // } + // // else if (timeRemaining.TotalSeconds <= 60) + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : Less than a minute"; + // // } + // // else + // // { + // // lblEstimatedTimeLeft.Text = "Estimated time left : " + timeRemaining.Minutes.ToString() + " minutes"; + // // } + }; + + if (InvokeRequired) + BeginInvoke(methodUIUpdate); + else + methodUIUpdate.Invoke(); } public void AddToLog(string entry) { + MethodInvoker methodUIUpdate = delegate + { + lbLog.Items.Insert(0, entry); + if (lbLog.Items.Count > 1000) + lbLog.Items.RemoveAt(lbLog.Items.Count - 1); + }; + + if (InvokeRequired) + BeginInvoke(methodUIUpdate); + else + methodUIUpdate.Invoke(); } public void ProcessEnded(bool canceled) { + MethodInvoker methodUIUpdate = delegate { + lblTitle.Text = "Update library completed successfully"; + lblMessage.Text = string.Empty; + btnCancel.Enabled = false; + btnOK.Enabled = true; + btnSaveLog.Enabled = true; + }; + + if (InvokeRequired) + BeginInvoke(methodUIUpdate); + else + methodUIUpdate.Invoke(); } #endregion