-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigurationForm.cs
193 lines (172 loc) · 7.6 KB
/
ConfigurationForm.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
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
using System;
using System.IO;
using System.Windows.Forms;
using yt_dl_protocol.Properties;
namespace yt_dl_protocol
{
public partial class ConfigurationForm : Form
{
public ConfigurationForm()
{
InitializeComponent();
}
private void ConfigurationForm_Load(object sender, EventArgs e)
{
YtDlDownloadPathTextBox.Text = Settings.Default.ytdl_path;
DownloadPathTextBox.Text = Settings.Default.download_path;
AdditionalArgsTextBox.Text = Settings.Default.additional_args;
AutoCloseCommandWindowCheckBox.Checked = Settings.Default.autoclose_on_finish;
UpdateCanRegisterState();
}
private void UpdateCanRegisterState()
{
bool isRegistered = Utils.IsProtocolRegistered(Settings.Default.protocol_ytdl);
bool isValidYtDlPath = File.Exists(Settings.Default.ytdl_path) || File.Exists(YtDlDownloadPathTextBox.Text);
bool isValidDlPath = Directory.Exists(Settings.Default.download_path) || Directory.Exists(DownloadPathTextBox.Text);
bool canRegister = isValidYtDlPath && isValidDlPath;
UpdateButton.Enabled = isValidYtDlPath;
if (canRegister)
{
SaveButton.Enabled = canRegister;
RegisterButton.Enabled = !isRegistered;
UnregisterButton.Enabled = isRegistered;
UpdateButton.Visible = true;
ProtocolStatusPictureBox.Image = isRegistered ? Resources.success : Resources.warning;
ProtocolStatusPictureBox.Cursor = Cursors.Default;
ProtocolToolTip.SetToolTip(ProtocolStatusPictureBox, isRegistered ? "The protocol has currently been registered." : "The protocol has currently not been registered.");
ProtocolStatusLabel.Cursor = Cursors.Default;
}
else
{
UpdateButton.Visible = false;
ProtocolStatusPictureBox.Image = Resources.error;
ProtocolStatusPictureBox.Cursor = Cursors.Help;
ProtocolToolTip.SetToolTip(ProtocolStatusPictureBox, "You need to set up the required paths below before you can register the protocol." );
ProtocolStatusLabel.Cursor = Cursors.Help;
}
}
private void BrowseButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Executable Files|*.exe",
Title = "Select youtube-dl or yt-dlp executable",
Multiselect = false,
CheckFileExists = true,
CheckPathExists = true
};
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK && File.Exists(openFileDialog.FileName) && Utils.ValidateExecutableIsYtDl(openFileDialog.FileName))
{
YtDlDownloadPathTextBox.Text = openFileDialog.FileName;
Settings.Default.ytdl_path = YtDlDownloadPathTextBox.Text;
UpdateButton.Enabled = true;
}
else
{
MessageBox.Show("The provided file is invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
UpdateButton.Enabled = false;
}
UpdateCanRegisterState();
}
private void RegisterButton_Click(object sender, EventArgs e)
{
HandleProtocolRegistering(true);
}
private void UnregisterButton_Click(object sender, EventArgs e)
{
HandleProtocolRegistering(false);
}
private void HandleProtocolRegistering(bool register)
{
if (register)
{
Settings.Default.protocol_registered = Utils.RegisterURLProtocol("ytdl", Application.ExecutablePath, "yt-dl-protocol-handler");
UnregisterButton.Enabled = true;
RegisterButton.Enabled = false;
}
else
{
Settings.Default.protocol_registered = Utils.UnregisterURLProtocol("ytdl");
UnregisterButton.Enabled = false;
RegisterButton.Enabled = true;
}
bool isRegistered = Utils.IsProtocolRegistered(Settings.Default.protocol_ytdl);
ProtocolStatusPictureBox.Image = isRegistered ? Resources.success : Resources.warning;
SaveSettings();
}
private void SaveSettings()
{
Settings.Default.autoclose_on_finish = AutoCloseCommandWindowCheckBox.Checked;
Settings.Default.additional_args = AdditionalArgsTextBox.Text;
Settings.Default.Save();
Settings.Default.Reload();
UpdateCanRegisterState();
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (!File.Exists(YtDlDownloadPathTextBox.Text) || !Directory.Exists(DownloadPathTextBox.Text))
MessageBox.Show("The settings could not be saved due to one or more invalid paths.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
SaveSettings();
MessageBox.Show("The settings have been saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
bool isRegistered = Utils.IsProtocolRegistered(Settings.Default.protocol_ytdl);
RegisterButton.Enabled = !isRegistered;
UnregisterButton.Enabled = isRegistered;
}
private void CheckForUpdatesButton_Click(object sender, EventArgs e)
{
Enabled = false;
var form = new CommandForm("-U");
form.ShowDialog();
Enabled = true;
}
private void BrowseDownloadPathButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog selectFolderDialog = new FolderBrowserDialog
{
RootFolder = Environment.SpecialFolder.Desktop,
Description = "Select a destination",
ShowNewFolderButton = true,
};
DialogResult result = selectFolderDialog.ShowDialog();
if (result == DialogResult.OK && Directory.Exists(selectFolderDialog.SelectedPath))
{
DownloadPathTextBox.Text = selectFolderDialog.SelectedPath;
Settings.Default.download_path = DownloadPathTextBox.Text;
}
else
{
MessageBox.Show("The provided directory is invalid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
UpdateCanRegisterState();
}
private void Guide_Button_Click(object sender, EventArgs e)
{
new BookmarkForm().ShowDialog();
}
private void ProtocolStatusPictureBox_BackgroundImageChanged(object sender, EventArgs e)
{
if (ProtocolStatusPictureBox.BackgroundImage == Properties.Resources.error)
{
if (File.Exists(Settings.Default.ytdl_path))
{
ProtocolStatusPictureBox.Cursor = Cursors.Default;
ProtocolStatusLabel.Cursor = Cursors.Default;
}
else
{
ProtocolStatusPictureBox.Cursor = Cursors.Help;
ProtocolStatusLabel.Cursor = Cursors.Help;
}
}
else
{
ProtocolStatusPictureBox.Cursor = Cursors.Default;
ProtocolStatusLabel.Cursor = Cursors.Default;
}
}
}
}