-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsForm.cs
204 lines (185 loc) · 8.36 KB
/
SettingsForm.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
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace Torrent
{
public partial class SettingsForm : Form
{
string filePath = $@"C:\Users\" + Environment.UserName + @"\AppData\Local\Bitloader\settings.cfg"; //USE Environment.UserName for releases
public static DefaultSettings defaultSettings { get; private set; }
public SettingsForm()
{
if (defaultSettings == null)
defaultSettings = new DefaultSettings();
InitializeComponent();
if (!File.Exists(filePath))//If it's the first time you start the program.
{
this.Text = "Please set default file paths";
this.ShowDialog();
}
else
ReadSettingsFile();
}
public void ReadSettingsFile()
{
List<string[]> settings = new List<string[]>();
string[] lines = File.ReadAllLines(filePath);
for (int i = 0; i < lines.Length; ++i)
{
settings.Add(lines[i].Split('>'));
}
for (int i = 0; i < settings.Count; ++i)
{
switch (settings[i][0])//Add new settings here (3 places total)
{
case "TorrentPath":
defaultSettings.defaultTorrentPath = settings[i][1];
break;
case "SaveFolder":
defaultSettings.defaultSavePath = settings[i][1];
break;
case "MaxDownSpeed":
defaultSettings.defaultMaxDownloadSpeed = Convert.ToInt32(settings[i][1]);
break;
case "MaxUpSpeed":
defaultSettings.defaultMaxUploadSpeed = Convert.ToInt32(settings[i][1]);
break;
case "UnlimitedDownSpeed":
defaultSettings.unlimitedDownloadSpeed = Convert.ToBoolean(settings[i][1]);
break;
case "UnlimitedUpSpeed":
defaultSettings.unlimitedUploadSpeed = Convert.ToBoolean(settings[i][1]);
break;
default:
Console.WriteLine("Error loading settings, something is wrong...");
break;
}
}
}
public void SetDefaultValues()
{
//Add new settings here (3 places total)
// > Is used as a splitter.
string settings = string.Empty;
StringBuilder sb = new StringBuilder(settings);
if (!File.Exists(filePath))
{
sb.Append($@"TorrentPath>" + defaultTorrentPathTxt.Text + Environment.NewLine +
$@"SaveFolder>" + defaultSavePathTxt.Text + Environment.NewLine +
@"MaxDownSpeed>" + downloadSpeedSelector.Value.ToString() + Environment.NewLine +
@"MaxUpSpeed>" + uploadSpeedSelector.Value.ToString() + Environment.NewLine +
@"UnlimitedDownSpeed>" + unlimitedDownSpeedChk.Checked.ToString() + Environment.NewLine +
@"UnlimitedUpSpeed>" + unlimitedUpSpeedChk.Checked.ToString() + Environment.NewLine);
File.WriteAllText(filePath, sb.ToString());
ReadSettingsFile();
}
else
{
//Change this to use string builder.
sb.Append($@"TorrentPath>" + defaultSettings.defaultTorrentPath + Environment.NewLine +
@"SaveFolder>" + defaultSettings.defaultSavePath + Environment.NewLine +
@"MaxDownSpeed>" + defaultSettings.defaultMaxDownloadSpeed.ToString() + Environment.NewLine +
@"MaxUpSpeed>" + defaultSettings.defaultMaxUploadSpeed.ToString() + Environment.NewLine +
@"UnlimitedDownSpeed>" + unlimitedDownSpeedChk.Checked.ToString() + Environment.NewLine +
@"UnlimitedUpSpeed>" + unlimitedUpSpeedChk.Checked.ToString() + Environment.NewLine);
ReadSettingsFile();
File.WriteAllText(filePath, sb.ToString());
}
}
private string OpenFolderDialog()
{
//Opens file browser
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult dr = fbd.ShowDialog();
string folderPath = string.Empty;
if (dr == DialogResult.OK)
folderPath = fbd.SelectedPath;
return folderPath;
}
private void WriteFile(string filePath, string text)
{
}
#region Events
private void pSettings_Load(object sender, EventArgs e)
{
//Fill all boxes with the settings from the read file method
if (this.Text == "Please set default file paths")
{
downloadSpeedSelector.Value = 500;
uploadSpeedSelector.Value = 500;
}
else
{
downloadSpeedSelector.Value = defaultSettings.defaultMaxDownloadSpeed;
uploadSpeedSelector.Value = defaultSettings.defaultMaxUploadSpeed;
}
defaultTorrentPathTxt.Text = defaultSettings.defaultTorrentPath;
defaultSavePathTxt.Text = defaultSettings.defaultSavePath;
unlimitedDownSpeedChk.Checked = defaultSettings.unlimitedDownloadSpeed;
unlimitedUpSpeedChk.Checked = defaultSettings.unlimitedUploadSpeed;
}
private void addTorFolderBtn_Click(object sender, EventArgs e)
{
string newAddTorrentFolder = OpenFolderDialog();
defaultSettings.defaultTorrentPath = newAddTorrentFolder;
defaultTorrentPathTxt.Text = newAddTorrentFolder;
}
private void addSaveFolderPathBtn_Click(object sender, EventArgs e)
{
string newSaveFolder = OpenFolderDialog();
defaultSettings.defaultSavePath = newSaveFolder;
defaultSavePathTxt.Text = newSaveFolder;
}
private void downloadSpeedSelector_ValueChanged(object sender, EventArgs e)
{
int newDownloadSpeed = Convert.ToInt32(downloadSpeedSelector.Value);
defaultSettings.defaultMaxDownloadSpeed = newDownloadSpeed;
}
private void uploadSpeedSelector_ValueChanged(object sender, EventArgs e)
{
int newUploadSpeed = Convert.ToInt32(uploadSpeedSelector.Value);
defaultSettings.defaultMaxUploadSpeed = newUploadSpeed;
}
private void unlimitedDownSpeedChk_CheckedChanged(object sender, EventArgs e)
{
if (unlimitedDownSpeedChk.Checked == true)
downloadSpeedSelector.Enabled = false;
else
downloadSpeedSelector.Enabled = true;
}
private void unlimitedUpSpeedChk_CheckedChanged(object sender, EventArgs e)
{
if (unlimitedUpSpeedChk.Checked == true)
uploadSpeedSelector.Enabled = false;
else
uploadSpeedSelector.Enabled = true;
}
private void applyBtn_Click(object sender, EventArgs e)
{
if(this.Text == "Please set default file paths") //Only happens first time you start the program
{
if(!string.IsNullOrWhiteSpace(defaultTorrentPathTxt.Text) || !string.IsNullOrWhiteSpace(defaultSavePathTxt.Text))
{
SetDefaultValues();
this.Close();
return;
}
else
MessageBox.Show("Please select valid file paths for both the save folder and torrent folder.", "Warning");
}
if (unlimitedDownSpeedChk.Checked == false)
defaultSettings.defaultMaxDownloadSpeed = Convert.ToInt32(downloadSpeedSelector.Value);
if (unlimitedUpSpeedChk.Checked == false)
defaultSettings.defaultMaxUploadSpeed = Convert.ToInt32(uploadSpeedSelector.Value);
SetDefaultValues();
this.Close();
}
private void cancelBtn_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
}
}