forked from haidarn2/Spoti15
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
202 lines (167 loc) · 5.54 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
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
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Text;
using System.Globalization;
using System.Resources;
using System.ServiceModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace Spoti15
{
[ServiceContract]
public interface ISpoti15WCF
{
[OperationContract]
bool Shutdown();
[OperationContract]
bool ShowTray();
}
class Spoti15WcfImpl : ISpoti15WCF
{
public bool Shutdown()
{
Program.Shutdown();
return true;
}
public bool ShowTray()
{
Program.ShowTray();
return true;
}
}
class Program
{
private static PrivateFontCollection pFonts = new PrivateFontCollection();
public static FontFamily[] FontFamilies
{
get
{
if(pFonts.Families.Length == 0)
LoadFonts();
return pFonts.Families;
}
}
public static FontFamily GetFontFamily(string family)
{
family = family.ToLower();
foreach(FontFamily f in FontFamilies)
{
if(f.Name.ToLower() == family)
{
return f;
}
}
return null;
}
public static void LoadFonts()
{
ResourceSet res = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in res)
{
string resKey = (String)entry.Key;
if (resKey == null || !resKey.StartsWith("font_"))
continue;
var path = Path.Combine(Application.StartupPath, string.Format("lcdfonts\\{0}.ttf", resKey.Substring(5)));
pFonts.AddFontFile(path);
/*
byte[] resVal = (byte[])entry.Value;
unsafe
{
fixed(byte * pFontData = resVal)
{
pFonts.AddMemoryFont((System.IntPtr)pFontData, resVal.Length);
}
}
*/
}
}
private static NotifyIcon notico;
private static MenuItem autostartItem;
private static ServiceHost host;
static void Main(string[] args)
{
Application.SetCompatibleTextRenderingDefault(false);
using (ChannelFactory<ISpoti15WCF> spotFactory = new ChannelFactory<ISpoti15WCF>(new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/Spoti15WCF")))
{
try
{
ISpoti15WCF iface = spotFactory.CreateChannel();
iface.Shutdown();
spotFactory.Close();
System.Threading.Thread.Sleep(1000);
}
catch
{
spotFactory.Abort();
spotFactory.Close();
}
}
host = new ServiceHost(typeof(Spoti15WcfImpl), new Uri[] { new Uri("net.pipe://localhost") });
host.AddServiceEndpoint(typeof(ISpoti15WCF), new NetNamedPipeBinding(), "Spoti15WCF");
host.Open();
if (args.Length == 0 || args[0] != "-autostart")
{
Properties.Settings.Default.HideIcon = false;
Properties.Settings.Default.Save();
}
ContextMenu cm = new ContextMenu();
MenuItem menu;
menu = new MenuItem();
menu.Text = "&Hide tray icon";
menu.Click += HideClick;
cm.MenuItems.Add(menu);
menu = new MenuItem();
menu.Checked = Autostart.IsEnabled();
menu.Text = "&Autostart";
menu.Click += AutostartClick;
cm.MenuItems.Add(menu);
autostartItem = menu;
cm.MenuItems.Add("-");
menu = new MenuItem();
menu.Text = "E&xit";
menu.Click += ExitClick;
cm.MenuItems.Add(menu);
notico = new NotifyIcon();
notico.Text = "Spoti15";
notico.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
notico.ContextMenu = cm;
notico.Visible = !Properties.Settings.Default.HideIcon;
Spoti15 spoti15 = new Spoti15();
Application.Run();
GC.KeepAlive(spoti15);
host.Close();
}
private static void AutostartClick(Object sender, EventArgs e)
{
if (Autostart.IsEnabled())
Autostart.Disable();
else
Autostart.Enable();
autostartItem.Checked = !autostartItem.Checked;
}
private static void HideClick(Object sender, EventArgs e)
{
Properties.Settings.Default.HideIcon = true;
Properties.Settings.Default.Save();
notico.Visible = false;
}
public static void ShowTray()
{
Properties.Settings.Default.HideIcon = false;
Properties.Settings.Default.Save();
notico.Visible = true;
}
private static void ExitClick(Object sender, EventArgs e)
{
Shutdown();
}
public static void Shutdown()
{
if(notico != null)
notico.Dispose();
Application.Exit();
}
}
}