/
MainForm.cs
143 lines (111 loc) · 4.59 KB
/
MainForm.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HandbrakeBatchEncoder.Factories;
using HandbrakeBatchEncoder.Properties;
using HandbrakeBatchEncoder.Services;
namespace HandbrakeBatchEncoder
{
public partial class MainForm : Form
{
private readonly IEncoderFactory encoderFactory;
private readonly IFileWatcher fileWatcher;
private delegate void UpdateEncodeQueueListDelegate();
private readonly List<string> encodeQueueFiles = new List<string>();
//private readonly BackgroundWorker _encoderWorker = new BackgroundWorker { WorkerReportsProgress = true };
//private readonly HandbrakeEncoder _encoder = new HandbrakeEncoder();
private IEncoder encoder;
public MainForm(IEncoderFactory encoderFactory,
IFileWatcher fileWatcher)
{
InitializeComponent();
this.encoderFactory = encoderFactory;
this.fileWatcher = fileWatcher;
}
private void MainForm_Load(object sender, EventArgs e)
{
// Setup watch on folder for file creations
fileWatcher.FileFound += NewFileFound;
fileWatcher.Watch();
// Update source path label
uxWatchFolderNameLabel.Text = fileWatcher.WatchedFolderName;
//// Hook up Background Worker Events
//// _encoderWorker.ProgressChanged += _encoderWorker_ProgressChanged;
//_encoderWorker.RunWorkerCompleted += _encoderWorker_RunWorkerCompleted;
//_encoderWorker.DoWork += OldHandbrakeEncoder.EncodeFile;
}
/// <summary>
/// Checks if the HandbrakeEncoder is available to immediately start
/// encoding the next file. If it is not, nothing is done until the
/// encoder is no longer busy.
/// </summary>
private void CheckAndQueueEncoderTask()
{
UpdateEncodeQueueList();
if (_encoderWorker.IsBusy || encodeQueueFiles.Count == 0)
{
return;
}
// Start encoding the next file in the queue
var sourceFile = encodeQueueFiles[0];
var destinationFile = Path.Combine(Settings.Default.OutputFolder,
Path.GetFileNameWithoutExtension(sourceFile));
destinationFile = Path.ChangeExtension(destinationFile, Settings.Default.OutputFileExtension);
var settings = new EncoderSettings
{
SourceFilename = sourceFile,
DestinationFilename = destinationFile
};
//_encoder.SourceFilename = sourceFile;
//_encoder.DestinationFilename = destinationFile;
_encoderWorker.RunWorkerAsync(settings);
//UpdateEncodeQueueList();
//_encodeQueueFiles.RemoveAt(0);
}
/// <summary>
/// Updates the pending list of files to be encoded in the UI.
/// </summary>
private void UpdateEncodeQueueList()
{
if (InvokeRequired)
{
Invoke(new UpdateEncodeQueueListDelegate(UpdateEncodeQueueList));
return;
}
uxEncodeQueueListView.Items.Clear();
foreach (var file in encodeQueueFiles)
{
uxEncodeQueueListView.Items.Add(Path.GetFileName(file));
}
uxNowEncodingFile.Text = encodeQueueFiles.Count > 0 ? Path.GetFileName(encodeQueueFiles[0]) : "";
}
/// <summary>
/// Adds a new file to the Encoder queue.
/// </summary>
/// <param name="filename"></param>
private void AddFileToQueue(string filename)
{
if (encodeQueueFiles.Contains(filename)) return;
encodeQueueFiles.Add(filename);
}
void NewFileFound(object sender, FileFoundArgs args)
{
AddFileToQueue(args.Filename);
CheckAndQueueEncoderTask();
}
void _encoderWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Pop the completed job off the queue
if (encodeQueueFiles.Count > 0)
{
encodeQueueFiles.RemoveAt(0);
}
// See if there's another item in the queue to encode
CheckAndQueueEncoderTask();
}
}
}