-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
200 lines (179 loc) · 7.67 KB
/
Settings.cs
File metadata and controls
200 lines (179 loc) · 7.67 KB
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
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace HA_Volume
{
public partial class Settings : Form
{
private System.Windows.Forms.Screen[] Screens;
dynamic HAData;
public Settings()
{
InitializeComponent();
}
//Dropdown to change which monitor the OSD will show on, it will also change the location of the settings window to "preview" which monitor it will be.
private void cmbMonitor_SelectedIndexChanged(object sender, EventArgs e)
{
int x = Screen.AllScreens[cmbMonitor.SelectedIndex].Bounds.Location.X + Screen.AllScreens[cmbMonitor.SelectedIndex].WorkingArea.Width /2 - (this.Width /2);
int y = Screen.AllScreens[cmbMonitor.SelectedIndex].Bounds.Location.Y + Screen.AllScreens[cmbMonitor.SelectedIndex].WorkingArea.Height /2 - (this.Height /2 );
this.Location = new Point(x, y);
Properties.Settings.Default.Monitor = cmbMonitor.SelectedIndex;
}
//Populates the dropdown with all detected monitors and sets the application version label, disables the dropdowns until valid URL is detected.
private void Settings_Load(object sender, EventArgs e)
{
lblVersion.Text = Application.ProductVersion;
Screens = System.Windows.Forms.Screen.AllScreens;
foreach (Screen s in Screens)
{
cmbMonitor.Items.Add(s.DeviceName.Replace("\\\\.\\DISPLAY", ""));
}
cmbMonitor.SelectedIndex = Properties.Settings.Default.Monitor;
if(txtURL.Text == "" || txtToken.Text == "" || HAAPI.Validate_URL(txtURL.Text))
{
cmbEntity.Enabled = true;
cmbOnSource.Enabled = true;
}
else
{
MessageBox.Show("Invalid URL, please enter the URL in the following format [PROTO]://[IP or DOMAIN]:[PORT] for example http://192.168.1.10:8123", "HA Volume - Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
cmbEntity.Enabled = false;
cmbOnSource.Enabled = false;
}
}
//Restarts application for changes to take afect.
private void Settings_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
Application.Restart();
Environment.Exit(0);
}
//Loads my website.
private void lblCopyright_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("https://cyanlabs.net/applications/ha-volume/");
}
//Populates the entity dropdown with all HA media_player entities.
private void cmbEntity_Click(object sender, EventArgs e)
{
HAData = HAAPI.GET();
foreach (dynamic item in HAData) {
if (item.ContainsKey("entity_id")) {
string entityid = item["entity_id"];
if (entityid.Contains("media_player."))
{
if (!cmbEntity.Items.Contains(item["entity_id"])) cmbEntity.Items.Add(item["entity_id"]);
}
}
}
}
//Populates the source dropdown with all available HA sources.
private void cmbOnSource_Click(object sender, EventArgs e)
{
HAData = HAAPI.GET(Properties.Settings.Default.HAEntity);
if (HAData["attributes"].ContainsKey("source_list"))
{
foreach (string source in HAData["attributes"]["source_list"])
{
if (!cmbOnSource.Items.Contains(source)) cmbOnSource.Items.Add(source);
}
}
}
//Populates the source dropdown with all available HA sources.
private void cmbOffSource_Click(object sender, EventArgs e)
{
HAData = HAAPI.GET(Properties.Settings.Default.HAEntity);
if (HAData["attributes"].ContainsKey("source_list"))
{
foreach (string source in HAData["attributes"]["source_list"])
{
if (!cmbOffSource.Items.Contains(source)) cmbOffSource.Items.Add(source);
}
}
}
//Adds/removes the registry key which makes HA Volume launch on windows startup.
private void chkStartup_CheckedChanged(object sender, EventArgs e)
{
var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartup.Checked) {
key.SetValue("HAVolume",AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName);
} else {
key.DeleteValue("HAVolume");
}
}
//Validates URL when textbox focus is lost.
private void txtURL_Leave(object sender, EventArgs e)
{
if (HAAPI.Validate_URL(txtURL.Text))
{
cmbEntity.Enabled = true;
cmbOnSource.Enabled = true;
}
else
{
MessageBox.Show("Invalid URL, please enter the URL in the following format [PROTO]://[IP or DOMAIN]:[PORT] for example http://192.168.1.10:8123", "HA Volume - Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
cmbEntity.Enabled = false;
cmbOnSource.Enabled = false;
}
}
private void cmbApplicationStart_Click(object sender, EventArgs e)
{
HAData = HAAPI.GET();
foreach (dynamic item in HAData)
{
if (item.ContainsKey("entity_id"))
{
string entityid = item["entity_id"];
if (entityid.Contains("switch."))
{
if (!cmbApplicationStart.Items.Contains(item["entity_id"])) cmbApplicationStart.Items.Add(item["entity_id"]);
}
}
}
}
private void cmbApplicationStop_Click(object sender, EventArgs e)
{
HAData = HAAPI.GET();
foreach (dynamic item in HAData)
{
if (item.ContainsKey("entity_id"))
{
string entityid = item["entity_id"];
if (entityid.Contains("switch."))
{
if (!cmbApplicationStop.Items.Contains(item["entity_id"])) cmbApplicationStop.Items.Add(item["entity_id"]);
}
}
}
}
private void cmbApplicationStart_SelectedIndexChanged(object sender, EventArgs e)
{
cmbApplicationStart.Text = Properties.Settings.Default.StartSwitch;
}
private void cmbApplicationStop_SelectedIndexChanged(object sender, EventArgs e)
{
cmbApplicationStop.Text = Properties.Settings.Default.StopSwitch;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
Title = "Browse Exe Files",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "txt",
Filter = "executable files (*.bat,*.cmd,*.exe,*.lnk)|*.bat;*.cmd;*.exe;*.lnk",
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtExtraApplication.Text = openFileDialog1.FileName;
}
else
{
txtExtraApplication.Text = "";
txtExtraApplicationArgs.Text = "";
}
}
}
}