From 57713f695a67a3eae7fd72aae80e16f1e98332bc Mon Sep 17 00:00:00 2001 From: Daniel Brunner <0xFEEDC0DE64@gmail.com> Date: Sun, 9 Jul 2017 17:19:37 +0200 Subject: [PATCH] Implemented progress dialog whily copying --- MusicOrganizer/CopyDialog.Designer.cs | 112 +++++++++++++++++++ MusicOrganizer/CopyDialog.cs | 144 +++++++++++++++++++++++++ MusicOrganizer/CopyDialog.resx | 123 +++++++++++++++++++++ MusicOrganizer/IndexDialog.Designer.cs | 2 +- MusicOrganizer/IndexDialog.cs | 8 +- MusicOrganizer/MainForm.Designer.cs | 2 +- MusicOrganizer/MainForm.cs | 63 +---------- MusicOrganizer/MusicOrganizer.csproj | 9 ++ 8 files changed, 397 insertions(+), 66 deletions(-) create mode 100644 MusicOrganizer/CopyDialog.Designer.cs create mode 100644 MusicOrganizer/CopyDialog.cs create mode 100644 MusicOrganizer/CopyDialog.resx diff --git a/MusicOrganizer/CopyDialog.Designer.cs b/MusicOrganizer/CopyDialog.Designer.cs new file mode 100644 index 0000000..c00ddf1 --- /dev/null +++ b/MusicOrganizer/CopyDialog.Designer.cs @@ -0,0 +1,112 @@ +namespace MusicOrganizer +{ + partial class CopyDialog + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.statusLabel = new System.Windows.Forms.Label(); + this.progressBar = new System.Windows.Forms.ProgressBar(); + this.continueButton = new System.Windows.Forms.Button(); + this.cancelButton = new System.Windows.Forms.Button(); + this.backgroundWorker = new System.ComponentModel.BackgroundWorker(); + this.SuspendLayout(); + // + // statusLabel + // + this.statusLabel.AutoSize = true; + this.statusLabel.Location = new System.Drawing.Point(13, 13); + this.statusLabel.Name = "statusLabel"; + this.statusLabel.Size = new System.Drawing.Size(92, 20); + this.statusLabel.TabIndex = 0; + this.statusLabel.Text = "statusLabel"; + // + // progressBar + // + this.progressBar.Location = new System.Drawing.Point(17, 36); + this.progressBar.Name = "progressBar"; + this.progressBar.Size = new System.Drawing.Size(568, 35); + this.progressBar.TabIndex = 1; + // + // continueButton + // + this.continueButton.Enabled = false; + this.continueButton.Location = new System.Drawing.Point(469, 77); + this.continueButton.Name = "continueButton"; + this.continueButton.Size = new System.Drawing.Size(116, 41); + this.continueButton.TabIndex = 3; + this.continueButton.Text = "Continue"; + this.continueButton.UseVisualStyleBackColor = true; + this.continueButton.Click += new System.EventHandler(this.continueButton_Click); + // + // cancelButton + // + this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.cancelButton.Location = new System.Drawing.Point(347, 77); + this.cancelButton.Name = "cancelButton"; + this.cancelButton.Size = new System.Drawing.Size(116, 41); + this.cancelButton.TabIndex = 4; + this.cancelButton.Text = "Cancel"; + this.cancelButton.UseVisualStyleBackColor = true; + this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click); + // + // backgroundWorker + // + this.backgroundWorker.WorkerReportsProgress = true; + this.backgroundWorker.WorkerSupportsCancellation = true; + this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker_DoWork); + this.backgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker_ProgressChanged); + this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker_RunWorkerCompleted); + // + // CopyDialog + // + this.AcceptButton = this.continueButton; + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.cancelButton; + this.ClientSize = new System.Drawing.Size(597, 130); + this.Controls.Add(this.cancelButton); + this.Controls.Add(this.continueButton); + this.Controls.Add(this.progressBar); + this.Controls.Add(this.statusLabel); + this.Name = "CopyDialog"; + this.Text = "Copying progress"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CopyDialog_FormClosing); + this.Load += new System.EventHandler(this.CopyDialog_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label statusLabel; + private System.Windows.Forms.ProgressBar progressBar; + private System.Windows.Forms.Button continueButton; + private System.Windows.Forms.Button cancelButton; + private System.ComponentModel.BackgroundWorker backgroundWorker; + } +} \ No newline at end of file diff --git a/MusicOrganizer/CopyDialog.cs b/MusicOrganizer/CopyDialog.cs new file mode 100644 index 0000000..60f411c --- /dev/null +++ b/MusicOrganizer/CopyDialog.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Windows.Forms; + +namespace MusicOrganizer +{ + internal partial class CopyDialog : Form + { + private List items; + private string targetFolder; + + public CopyDialog() + { + InitializeComponent(); + } + + public DialogResult Execute(List items) + { + using (var dialog = new FolderBrowserDialog()) + { + var result = dialog.ShowDialog(); + + if (result != DialogResult.OK) + return DialogResult.Cancel; + + if (string.IsNullOrWhiteSpace(dialog.SelectedPath)) + return DialogResult.Cancel; + + targetFolder = dialog.SelectedPath; + } + + this.items = items; + + return ShowDialog(); + } + + private void CopyDialog_Load(object sender, EventArgs e) + { + backgroundWorker.RunWorkerAsync(); + } + + private void CopyDialog_FormClosing(object sender, FormClosingEventArgs e) + { + if (backgroundWorker.IsBusy) + { + e.Cancel = true; + if (!backgroundWorker.CancellationPending) + backgroundWorker.CancelAsync(); + } + } + + private void continueButton_Click(object sender, EventArgs e) + { + if (!backgroundWorker.IsBusy) + { + DialogResult = DialogResult.OK; + Close(); + } + } + + private void cancelButton_Click(object sender, EventArgs e) + { + if (backgroundWorker.IsBusy) + { + if (!backgroundWorker.CancellationPending) + backgroundWorker.CancelAsync(); + cancelButton.Enabled = false; + } + else + { + DialogResult = DialogResult.Cancel; + Close(); + } + } + + private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) + { + var worker = sender as BackgroundWorker; + + for (int i = 0; i < items.Count; i++) + { + var item = items[i]; + var statusStr = Path.GetFileName(item.filename) + ": "; + + if (worker.CancellationPending) + { + worker.ReportProgress((i + 1) * 100 / items.Count, statusStr + "Cancelled!"); + e.Cancel = true; + return; + } + + var bpmPath = Path.Combine(targetFolder, item.bpm); + + if (!Directory.Exists(bpmPath)) + try + { + worker.ReportProgress((i + 1) * 100 / items.Count, statusStr + "Creating bpm folder..."); + Directory.CreateDirectory(bpmPath); + } + catch (Exception ex) + { + worker.ReportProgress((i + 1) * 100 / items.Count, statusStr + "Exception:\r\n" + ex.Message); + continue; + } + + var target = Path.Combine(bpmPath, Path.GetFileName(item.filename)); + + worker.ReportProgress((i + 1) * 100 / items.Count, statusStr + "Copying..."); + + try + { + File.Copy(item.filename, target); + } + catch (Exception ex) + { + worker.ReportProgress((i + 1) * 100 / items.Count, statusStr + "Exception:\r\n" + ex.Message); + continue; + } + } + } + + private void backgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) + { + progressBar.Value = e.ProgressPercentage; + statusLabel.Text = e.UserState as string; + } + + private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) + { + if (e.Cancelled) + { + DialogResult = DialogResult.Cancel; + Close(); + } + else + { + cancelButton.Enabled = false; + continueButton.Enabled = true; + } + } + } +} \ No newline at end of file diff --git a/MusicOrganizer/CopyDialog.resx b/MusicOrganizer/CopyDialog.resx new file mode 100644 index 0000000..975fd0d --- /dev/null +++ b/MusicOrganizer/CopyDialog.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/MusicOrganizer/IndexDialog.Designer.cs b/MusicOrganizer/IndexDialog.Designer.cs index 30c2d91..a6ec5e0 100644 --- a/MusicOrganizer/IndexDialog.Designer.cs +++ b/MusicOrganizer/IndexDialog.Designer.cs @@ -93,7 +93,7 @@ this.Controls.Add(this.progressBar); this.Controls.Add(this.statusLabel); this.Name = "IndexDialog"; - this.Text = "IndexDialog"; + this.Text = "Indexing progress"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.IndexDialog_FormClosing); this.Load += new System.EventHandler(this.IndexDialog_Load); this.ResumeLayout(false); diff --git a/MusicOrganizer/IndexDialog.cs b/MusicOrganizer/IndexDialog.cs index edc9516..c354261 100644 --- a/MusicOrganizer/IndexDialog.cs +++ b/MusicOrganizer/IndexDialog.cs @@ -110,11 +110,13 @@ namespace MusicOrganizer if(worker.CancellationPending) { - worker.ReportProgress(i * 100 / paths.Length, statusStr + "Cancelled!"); + worker.ReportProgress((i + 1) * 100 / paths.Length, statusStr + "Cancelled!"); e.Cancel = true; return; } + worker.ReportProgress((i + 1) * 100 / paths.Length, statusStr + "Indexing..."); + TagLib.File file; try @@ -123,7 +125,7 @@ namespace MusicOrganizer } catch (Exception ex) { - worker.ReportProgress(i * 100 / paths.Length, statusStr + "Exception!\r\n" + ex.Message); + worker.ReportProgress((i + 1) * 100 / paths.Length, statusStr + "Exception!\r\n" + ex.Message); Thread.Sleep(5000); continue; } @@ -142,8 +144,6 @@ namespace MusicOrganizer title = file.Tag.Title, bpm = string.Format("{0}BPM", file.Tag.BeatsPerMinute) }); - - worker.ReportProgress((i + 1) * 100 / paths.Length, statusStr + "Ok!"); } } diff --git a/MusicOrganizer/MainForm.Designer.cs b/MusicOrganizer/MainForm.Designer.cs index 7d5a3d5..312e5b3 100644 --- a/MusicOrganizer/MainForm.Designer.cs +++ b/MusicOrganizer/MainForm.Designer.cs @@ -120,7 +120,7 @@ this.Controls.Add(this.objectListView1); this.Controls.Add(this.panel1); this.Name = "MainForm"; - this.Text = "MainForm"; + this.Text = "Music organizer"; this.panel1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.objectListView1)).EndInit(); this.ResumeLayout(false); diff --git a/MusicOrganizer/MainForm.cs b/MusicOrganizer/MainForm.cs index 94b6be1..b4a1903 100644 --- a/MusicOrganizer/MainForm.cs +++ b/MusicOrganizer/MainForm.cs @@ -1,11 +1,10 @@ using System; using System.Collections.Generic; -using System.IO; using System.Windows.Forms; namespace MusicOrganizer { - public partial class MainForm : Form + internal partial class MainForm : Form { private List items; @@ -34,64 +33,8 @@ namespace MusicOrganizer private void button2_Click(object sender, EventArgs e) { - string targetFolder; - - using (var dialog = new FolderBrowserDialog()) - { - var result = dialog.ShowDialog(); - - if (result != DialogResult.OK) - return; - - if (string.IsNullOrWhiteSpace(dialog.SelectedPath)) - return; - - targetFolder = dialog.SelectedPath; - } - - foreach(var item in items) - { - var bpmPath = Path.Combine(targetFolder, item.bpm); - - again0: - if(!Directory.Exists(bpmPath)) - try - { - Directory.CreateDirectory(bpmPath); - } - catch(Exception ex) - { - var result = MessageBox.Show(string.Format("Error occured when creating bpm folder for file\n\n{0}\n\n{1}", item.filename, ex.Message), "Error occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); - - switch (result) - { - case DialogResult.Abort: return; - case DialogResult.Retry: goto again0; - case DialogResult.Ignore: continue; - default: throw new Exception("Unknown option clicked."); - } - } - - var target = Path.Combine(bpmPath, Path.GetFileName(item.filename)); - - again1: - try - { - File.Copy(item.filename, target); - } - catch(Exception ex) - { - var result = MessageBox.Show(string.Format("Error occured when copying file\n\n{0}\n\nto bpmfolder\n\n{1}\n\n{2}", item.filename, target, ex.Message), "Error occured", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error); - - switch (result) - { - case DialogResult.Abort: return; - case DialogResult.Retry: goto again1; - case DialogResult.Ignore: continue; - default: throw new Exception("Unknown option clicked."); - } - } - } + using (var dialog = new CopyDialog()) + dialog.Execute(items); } } } \ No newline at end of file diff --git a/MusicOrganizer/MusicOrganizer.csproj b/MusicOrganizer/MusicOrganizer.csproj index 14be6bb..4d12d04 100644 --- a/MusicOrganizer/MusicOrganizer.csproj +++ b/MusicOrganizer/MusicOrganizer.csproj @@ -53,6 +53,12 @@ + + Form + + + CopyDialog.cs + Form @@ -88,6 +94,9 @@ + + CopyDialog.cs + IndexDialog.cs