From ec19d8520ea39761053cccb2744931f0195b7bba Mon Sep 17 00:00:00 2001 From: sr55 Date: Mon, 28 Aug 2017 15:16:40 +0100 Subject: WinGui: Putting in some back-end infrastructure into the queue screen to support future features + some misc tidy up. --- win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs | 6 +- .../Encode/EventArgs/EncodeCompletedEventArgs.cs | 34 ++++--- win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs | 40 +++++++-- .../Services/Queue/Model/QueueStats.cs | 100 +++++++++++++++++++++ .../HandBrakeWPF/Services/Queue/Model/QueueTask.cs | 5 ++ .../HandBrakeWPF/Services/Queue/QueueProcessor.cs | 33 +++---- 6 files changed, 179 insertions(+), 39 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Services/Queue/Model/QueueStats.cs (limited to 'win/CS/HandBrakeWPF/Services') diff --git a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs index 8f7dee945..b822b976d 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs @@ -131,7 +131,7 @@ namespace HandBrakeWPF.Services.Encode /// /// The configuration. /// - public void ProcessLogs(string destination, bool isPreview, HBConfiguration configuration) + public string ProcessLogs(string destination, bool isPreview, HBConfiguration configuration) { try { @@ -162,11 +162,15 @@ namespace HandBrakeWPF.Services.Encode { this.WriteFile(logContent, Path.Combine(configuration.SaveLogCopyDirectory, encodeLogFile)); } + + return encodeLogFile; } catch (Exception exc) { Debug.WriteLine(exc); // This exception doesn't warrent user interaction, but it should be logged } + + return null; } /// diff --git a/win/CS/HandBrakeWPF/Services/Encode/EventArgs/EncodeCompletedEventArgs.cs b/win/CS/HandBrakeWPF/Services/Encode/EventArgs/EncodeCompletedEventArgs.cs index ea334705d..1f2fe3bb0 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/EventArgs/EncodeCompletedEventArgs.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/EventArgs/EncodeCompletedEventArgs.cs @@ -10,12 +10,10 @@ namespace HandBrakeWPF.Services.Encode.EventArgs { using System; - using System.Runtime.Serialization; /// /// Encode Progress Event Args /// - [DataContract] public class EncodeCompletedEventArgs : EventArgs { /// @@ -33,36 +31,50 @@ namespace HandBrakeWPF.Services.Encode.EventArgs /// /// The filename. /// - public EncodeCompletedEventArgs(bool sucessful, Exception exception, string errorInformation, string filename) + /// + /// The path and filename of the log for this encode. + /// + /// + /// The final size of the file in bytes. + /// + public EncodeCompletedEventArgs(bool sucessful, Exception exception, string errorInformation, string filename, string logPath, long finalSizeInBytes) { this.Successful = sucessful; this.Exception = exception; this.ErrorInformation = errorInformation; this.FileName = filename; + this.ActivityLogPath = logPath; + this.FinalFilesizeInBytes = finalSizeInBytes; } /// /// Gets or sets the file name. /// - [DataMember] - public string FileName { get; set; } + public string FileName { get; private set; } /// /// Gets or sets a value indicating whether Successful. /// - [DataMember] - public bool Successful { get; set; } + public bool Successful { get; private set; } /// /// Gets or sets Exception. /// - [DataMember] - public Exception Exception { get; set; } + public Exception Exception { get; private set; } /// /// Gets or sets ErrorInformation. /// - [DataMember] - public string ErrorInformation { get; set; } + public string ErrorInformation { get; private set; } + + /// + /// + /// + public string ActivityLogPath { get; private set; } + + /// + /// Final filesize in bytes + /// + public long FinalFilesizeInBytes { get; private set; } } } diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs index 2ad5f0688..f8bd78fb7 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs @@ -11,6 +11,7 @@ namespace HandBrakeWPF.Services.Encode { using System; using System.Diagnostics; + using System.IO; using HandBrake.ApplicationServices.Interop; using HandBrake.ApplicationServices.Interop.EventArgs; @@ -21,15 +22,16 @@ namespace HandBrakeWPF.Services.Encode using HandBrake.ApplicationServices.Services.Logging.Model; using HandBrakeWPF.Exceptions; + using HandBrakeWPF.Properties; using HandBrakeWPF.Services.Encode.Factories; - using EncodeTask = HandBrakeWPF.Services.Encode.Model.EncodeTask; - using IEncode = HandBrakeWPF.Services.Encode.Interfaces.IEncode; + using EncodeTask = Model.EncodeTask; + using IEncode = Interfaces.IEncode; /// /// LibHB Implementation of IEncode /// - public class LibEncode : HandBrakeWPF.Services.Encode.EncodeBase, IEncode + public class LibEncode : EncodeBase, IEncode { #region Private Variables @@ -63,7 +65,7 @@ namespace HandBrakeWPF.Services.Encode // Sanity Checking and Setup if (this.IsEncoding) { - throw new GeneralApplicationException("HandBrake is already encoding a file.", "Please stop the current encode. If the problem persists, please restart HandBrake.", null); + throw new GeneralApplicationException(Resources.Queue_AlreadyEncoding, Resources.Queue_AlreadyEncodingSolution, null); } // Setup @@ -99,7 +101,7 @@ namespace HandBrakeWPF.Services.Encode this.IsEncoding = false; this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc); - this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source)); + this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source, null, 0)); } } @@ -203,14 +205,36 @@ namespace HandBrakeWPF.Services.Encode this.ServiceLogMessage("Encode Completed ..."); // Handling Log Data - this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration); + string hbLog = this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration); + long filesize = this.GetFilesize(this.currentTask.Destination); // Raise the Encode Completed EVent. this.InvokeEncodeCompleted( e.Error - ? new EventArgs.EncodeCompletedEventArgs(false, null, string.Empty, this.currentTask.Destination) - : new EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Destination)); + ? new EventArgs.EncodeCompletedEventArgs(false, null, string.Empty, this.currentTask.Destination, hbLog, filesize) + : new EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Destination, hbLog, filesize)); } + + private long GetFilesize(string destination) + { + try + { + if (!string.IsNullOrEmpty(destination) && File.Exists(destination)) + { + return new FileInfo(destination).Length; + } + + return 0; + } + catch (Exception e) + { + this.ServiceLogMessage("Unable to get final filesize ..." + Environment.NewLine + e); + Debug.WriteLine(e); + } + + return 0; + } + #endregion } } diff --git a/win/CS/HandBrakeWPF/Services/Queue/Model/QueueStats.cs b/win/CS/HandBrakeWPF/Services/Queue/Model/QueueStats.cs new file mode 100644 index 000000000..857feb924 --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/Queue/Model/QueueStats.cs @@ -0,0 +1,100 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// A file to record stats about a queue task. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services.Queue.Model +{ + using System; + + using Caliburn.Micro; + + public class QueueStats : PropertyChangedBase + { + private DateTime startTime; + + private DateTime endTime; + + private long? finalFileSize; + + public QueueStats() + { + } + + public DateTime StartTime + { + get + { + return this.startTime; + } + set + { + if (value.Equals(this.startTime)) return; + this.startTime = value; + this.NotifyOfPropertyChange(() => this.StartTime); + this.NotifyOfPropertyChange(() => this.Duration); + } + } + + public DateTime EndTime + { + get + { + return this.endTime; + } + set + { + if (value.Equals(this.endTime)) return; + this.endTime = value; + this.NotifyOfPropertyChange(() => this.EndTime); + this.NotifyOfPropertyChange(() => this.Duration); + } + } + + public TimeSpan Duration + { + get + { + // TODO, take into account Paused Duration. Requires some refactoring first. + return this.EndTime - this.StartTime; + } + } + + /// + /// Final filesize in Bytes + /// + public long? FinalFileSize + { + get + { + return this.finalFileSize; + } + set + { + if (value == this.finalFileSize) return; + this.finalFileSize = value; + this.NotifyOfPropertyChange(() => this.FinalFileSize); + this.NotifyOfPropertyChange(() => this.FinalFileSizeInMegaBytes); + } + } + + public long? FinalFileSizeInMegaBytes + { + get + { + if (this.finalFileSize.HasValue) + { + return this.finalFileSize / 1024 / 1024; + } + + return 0; + } + } + + public string CompletedActivityLogPath { get; set; } + } +} diff --git a/win/CS/HandBrakeWPF/Services/Queue/Model/QueueTask.cs b/win/CS/HandBrakeWPF/Services/Queue/Model/QueueTask.cs index be3a8a4b9..334842d31 100644 --- a/win/CS/HandBrakeWPF/Services/Queue/Model/QueueTask.cs +++ b/win/CS/HandBrakeWPF/Services/Queue/Model/QueueTask.cs @@ -35,6 +35,7 @@ namespace HandBrakeWPF.Services.Queue.Model this.Status = QueueItemStatus.Waiting; id = id + 1; this.Id = string.Format("{0}.{1}", GeneralUtilities.ProcessId, id); + this.Statistics = new QueueStats(); } /// @@ -58,6 +59,8 @@ namespace HandBrakeWPF.Services.Queue.Model id = id + 1; this.Id = string.Format("{0}.{1}", GeneralUtilities.ProcessId, id); + + this.Statistics = new QueueStats(); } public string Id { get; } @@ -94,6 +97,8 @@ namespace HandBrakeWPF.Services.Queue.Model /// public HBConfiguration Configuration { get; set; } + public QueueStats Statistics { get; set; } + #endregion protected bool Equals(QueueTask other) diff --git a/win/CS/HandBrakeWPF/Services/Queue/QueueProcessor.cs b/win/CS/HandBrakeWPF/Services/Queue/QueueProcessor.cs index 2292d18a2..c7aee8131 100644 --- a/win/CS/HandBrakeWPF/Services/Queue/QueueProcessor.cs +++ b/win/CS/HandBrakeWPF/Services/Queue/QueueProcessor.cs @@ -48,7 +48,6 @@ namespace HandBrakeWPF.Services.Queue /// private static readonly object QueueLock = new object(); private readonly IUserSettingService userSettingService; - private readonly IErrorService errorService; private readonly BindingList queue = new BindingList(); private readonly string queueFile; private bool clearCompleted; @@ -72,10 +71,9 @@ namespace HandBrakeWPF.Services.Queue /// /// Services are not setup /// - public QueueProcessor(IEncode encodeService, IUserSettingService userSettingService, IErrorService errorService) + public QueueProcessor(IEncode encodeService, IUserSettingService userSettingService) { this.userSettingService = userSettingService; - this.errorService = errorService; this.EncodeService = encodeService; // If this is the first instance, just use the main queue file, otherwise add the instance id to the filename. @@ -318,19 +316,9 @@ namespace HandBrakeWPF.Services.Queue { if (this.queue.Count > 0) { - QueueTask job = this.queue.FirstOrDefault(q => q.Status == QueueItemStatus.Waiting); - if (job != null) - { - job.Status = QueueItemStatus.InProgress; - this.LastProcessedJob = job; - this.InvokeQueueChanged(EventArgs.Empty); - } - - this.BackupQueue(string.Empty); - return job; + return this.queue.FirstOrDefault(q => q.Status == QueueItemStatus.Waiting); } - this.BackupQueue(string.Empty); return null; } @@ -399,7 +387,7 @@ namespace HandBrakeWPF.Services.Queue if (job.Status != QueueItemStatus.Error && job.Status != QueueItemStatus.Completed) { throw new GeneralApplicationException( - "Job Error", "Unable to reset job status as it is not in an Error or Completed state", null); + Resources.Error, Resources.Queue_UnableToResetJob, null); } job.Status = QueueItemStatus.Waiting; @@ -437,10 +425,7 @@ namespace HandBrakeWPF.Services.Queue } catch (Exception exc) { - throw new GeneralApplicationException( - "Unable to restore queue file.", - "The file may be corrupted or from an older incompatible version of HandBrake", - exc); + throw new GeneralApplicationException(Resources.Queue_UnableToRestoreFile, Resources.Queue_UnableToRestoreFileExtended, exc); } if (list != null) @@ -544,6 +529,9 @@ namespace HandBrakeWPF.Services.Queue private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e) { this.LastProcessedJob.Status = QueueItemStatus.Completed; + this.LastProcessedJob.Statistics.EndTime = DateTime.Now; + this.LastProcessedJob.Statistics.CompletedActivityLogPath = e.ActivityLogPath; + this.LastProcessedJob.Statistics.FinalFileSize = e.FinalFilesizeInBytes; // Clear the completed item of the queue if the setting is set. if (this.clearCompleted) @@ -638,10 +626,15 @@ namespace HandBrakeWPF.Services.Queue LogService.GetLogger().LogMessage(Resources.PauseOnLowDiskspace, LogMessageType.ScanOrEncode, LogLevel.Info); job.Status = QueueItemStatus.Waiting; this.Pause(); + this.BackupQueue(string.Empty); return; // Don't start the next job. } + job.Status = QueueItemStatus.InProgress; + job.Statistics.StartTime = DateTime.Now; + this.LastProcessedJob = job; this.IsProcessing = true; + this.InvokeQueueChanged(EventArgs.Empty); this.InvokeJobProcessingStarted(new QueueProgressEventArgs(job)); this.EncodeService.Start(job.Task, job.Configuration); } @@ -653,6 +646,8 @@ namespace HandBrakeWPF.Services.Queue // Fire the event to tell connected services. this.OnQueueCompleted(new QueueCompletedEventArgs(false)); } + + this.BackupQueue(string.Empty); } #endregion -- cgit v1.2.3