1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EncodeBase.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// A Base Class for the Encode Services.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services.Encode
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using HandBrake.ApplicationServices.Interop.EventArgs;
using HandBrake.ApplicationServices.Model;
using HandBrakeWPF.Services.Encode.Interfaces;
using HandBrakeWPF.Utilities;
using EncodeCompletedEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs;
using EncodeCompletedStatus = HandBrakeWPF.Services.Encode.Interfaces.EncodeCompletedStatus;
using EncodeProgessStatus = HandBrakeWPF.Services.Encode.Interfaces.EncodeProgessStatus;
using EncodeProgressEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeProgressEventArgs;
using EncodeTask = HandBrakeWPF.Services.Encode.Model.EncodeTask;
using GeneralApplicationException = HandBrakeWPF.Exceptions.GeneralApplicationException;
using ILog = HandBrakeWPF.Services.Logging.Interfaces.ILog;
using LogService = HandBrakeWPF.Services.Logging.LogService;
/// <summary>
/// A Base Class for the Encode Services.
/// </summary>
public class EncodeBase
{
/// <summary>
/// Initializes a new instance of the <see cref="EncodeBase"/> class.
/// </summary>
public EncodeBase()
{
}
#region Events
/// <summary>
/// Fires when a new QueueTask starts
/// </summary>
public event EventHandler EncodeStarted;
/// <summary>
/// Fires when a QueueTask finishes.
/// </summary>
public event EncodeCompletedStatus EncodeCompleted;
/// <summary>
/// Encode process has progressed
/// </summary>
public event EncodeProgessStatus EncodeStatusChanged;
#endregion
#region Properties
/// <summary>
/// Gets or sets a value indicating whether IsEncoding.
/// </summary>
public bool IsEncoding { get; protected set; }
#endregion
#region Invoke Events
/// <summary>
/// Invoke the Encode Status Changed Event.
/// </summary>
/// <param name="e">
/// The EncodeProgressEventArgs.
/// </param>
public void InvokeEncodeStatusChanged(EncodeProgressEventArgs e)
{
EncodeProgessStatus handler = this.EncodeStatusChanged;
handler?.Invoke(this, e);
}
/// <summary>
/// Invoke the Encode Completed Event
/// </summary>
/// <param name="e">
/// The EncodeCompletedEventArgs.
/// </param>
public void InvokeEncodeCompleted(EncodeCompletedEventArgs e)
{
EncodeCompletedStatus handler = this.EncodeCompleted;
handler?.Invoke(this, e);
}
/// <summary>
/// Invoke the Encode Started Event
/// </summary>
/// <param name="e">
/// The EventArgs.
/// </param>
public void InvokeEncodeStarted(System.EventArgs e)
{
EventHandler handler = this.EncodeStarted;
handler?.Invoke(this, e);
}
#endregion
#region Methods
/// <summary>
/// Save a copy of the log to the users desired location or a default location
/// if this feature is enabled in options.
/// </summary>
/// <param name="destination">
/// The Destination File Path
/// </param>
/// <param name="isPreview">
/// The is Preview.
/// </param>
/// <param name="configuration">
/// The configuration.
/// </param>
public string ProcessLogs(string destination, bool isPreview, HBConfiguration configuration)
{
try
{
string logDir = DirectoryUtilities.GetLogDirectory();
string encodeDestinationPath = Path.GetDirectoryName(destination);
string destinationFile = Path.GetFileName(destination);
string encodeLogFile = destinationFile + " " + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt";
ILog log = LogService.GetLogger();
string logContent = log.ActivityLog;
// Make sure the log directory exists.
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
// Copy the Log to HandBrakes log folder in the users applciation data folder.
this.WriteFile(logContent, Path.Combine(logDir, encodeLogFile));
// Save a copy of the log file in the same location as the enocde.
if (configuration.SaveLogWithVideo)
{
this.WriteFile(logContent, Path.Combine(encodeDestinationPath, encodeLogFile));
}
// Save a copy of the log file to a user specified location
if (Directory.Exists(configuration.SaveLogCopyDirectory) && configuration.SaveLogToCopyDirectory)
{
this.WriteFile(logContent, Path.Combine(configuration.SaveLogCopyDirectory, encodeLogFile));
}
return Path.Combine(logDir, encodeLogFile);
}
catch (Exception exc)
{
Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged
}
return null;
}
/// <summary>
/// Verify the Encode Destination path exists and if not, create it.
/// </summary>
/// <param name="task">
/// The task.
/// </param>
/// <exception cref="Exception">
/// If the creation fails, an exception is thrown.
/// </exception>
protected void VerifyEncodeDestinationPath(EncodeTask task)
{
// Make sure the path exists, attempt to create it if it doesn't
try
{
string path = Directory.GetParent(task.Destination).ToString();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (Exception exc)
{
throw new GeneralApplicationException(
"Unable to create directory for the encoded output.", "Please verify that you have a valid path.", exc);
}
}
/// <summary>
/// The write file.
/// </summary>
/// <param name="content">
/// The content.
/// </param>
/// <param name="fileName">
/// The file name.
/// </param>
private void WriteFile(string content, string fileName)
{
try
{
using (StreamWriter fileWriter = new StreamWriter(fileName) { AutoFlush = true })
{
fileWriter.Write(content);
}
}
catch (Exception exc)
{
Debug.WriteLine(exc);
}
}
#endregion
}
}
|