-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
93 lines (79 loc) · 3.17 KB
/
Program.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
using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.IO;
namespace TrayIconForDiscordWeb {
static class Program {
static Dictionary<string, Icon> icons = new Dictionary<string, Icon>();
static Icon defaultIcon;
static NotifyIcon trayIcon;
static HttpListener server;
static Thread httpThread;
static void httpThreadFunc() {
var currIcon = defaultIcon;
server = new HttpListener();
server.Prefixes.Add("http://127.0.0.1:15341/");
server.Start();
for (;;) {
var context = server.GetContext();
var response = context.Response;
response.Headers.Add("Access-Control-Allow-Origin", "*");
var path = context.Request.Url.LocalPath;
string reply = "";
if (path != null && path.StartsWith("/set-status/")) {
var name = path.Substring("/set-status/".Length);
Icon newIcon;
if (icons.TryGetValue(name, out newIcon)) {
// OK!
} else if (name.EndsWith("+") && icons.TryGetValue(name.Substring(0, name.Length - 1), out newIcon)) {
// OK!
} else newIcon = defaultIcon;
if (newIcon != currIcon) {
currIcon = newIcon;
trayIcon.Icon = newIcon;
}
reply = "OK!";
} else {
response.StatusCode = 404;
}
var buffer = Encoding.UTF8.GetBytes(reply);
response.ContentLength64 = buffer.Length;
var st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
}
static void onExit(object sender, EventArgs e) {
server.Stop();
httpThread.Abort();
Application.Exit();
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try {
foreach (var file in (new DirectoryInfo("Icons")).EnumerateFiles("*.ico")) {
var name = Path.GetFileNameWithoutExtension(file.Name);
icons[name] = Icon.ExtractAssociatedIcon(file.FullName);
}
defaultIcon = icons["default"];
} catch (Exception e) {
MessageBox.Show("Failed to enumerate icons:\n" + e.ToString(), "Uh oh", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
trayIcon = new NotifyIcon();
trayIcon.Text = "Tray Icon for Discord Web\nDouble-click to exit.";
trayIcon.Icon = defaultIcon;
trayIcon.Visible = true;
trayIcon.DoubleClick += new EventHandler(onExit);
httpThread = new Thread(new ThreadStart(httpThreadFunc));
httpThread.Start();
Application.Run();
}
}
}