-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
206 lines (188 loc) · 6.91 KB
/
MainWindow.xaml.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
205
206
using Hardcodet.Wpf.TaskbarNotification;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Threading;
namespace SystrayProxyManager
{
enum ExitOrigin { User, Mutex, None};
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ExitOrigin exitOrigin = ExitOrigin.None;
private Proxies proxies = new Proxies();
private DispatcherTimer timer;
private static System.Threading.Mutex mutex = new System.Threading.Mutex(false, "{5D1B5C13-C70C-43CA-8039-7A353F026452}");
public MainWindow()
{
InitializeComponent();
if (!mutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show("only one instance at a time");
exitOrigin = ExitOrigin.Mutex;
this.Close();
return;
}
SetTimer();
taskBarIcon.Visibility = Visibility.Visible;
proxies.userDefinedProxyState = proxies.CurrentProxyState;
proxies.userDefinedProxyServer = proxies.CurrentProxyServer;
taskBarIcon.LeftClickCommand = new TaskBarIcon_LeftClick(proxies, taskBarIcon);
taskBarIcon.ToolTipText = proxies.CurrentProxyServer.ToString();
UpdateSetProxyMenu();
dgProxy.ItemsSource = proxies.proxies;
}
#region UI update
private void UpdateSetProxyMenu()
{
menu_proxy.Items.Clear();
foreach (Proxy proxy in proxies.proxies)
{
MenuItem item = new MenuItem() { Header = proxy.Name, Tag = proxy};
item.Click += SetProxy_Click;
menu_proxy.Items.Add(item);
}
}
#endregion UI update
#region Timer
private void SetTimer()
{
timer = new DispatcherTimer();
timer.Tick += new EventHandler(Timer_Elapsed);
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
/// <summary>
/// Check proxy state and update tray icon
/// </summary>
private void Timer_Elapsed(object sender, EventArgs e)
{
taskBarIcon.ToolTipText = proxies.CurrentProxyServer.ToString();
if (menu_Force.IsChecked)
{
if (taskBarIcon.ToolTipText != proxies.userDefinedProxyServer.ToString())
proxies.CurrentProxyServer = proxies.userDefinedProxyServer;
if (proxies.CurrentProxyState != proxies.userDefinedProxyState)
proxies.CurrentProxyState = proxies.userDefinedProxyState;
}
if (proxies.CurrentProxyState)
taskBarIcon.Icon = Resource.on;
else
taskBarIcon.Icon = Resource.off;
}
#endregion Timer
#region MainMenu click events
private void Exit_Click(Object sender, RoutedEventArgs e)
{
exitOrigin = ExitOrigin.User;
this.Close();
}
private void Copyright_Click(Object sender, RoutedEventArgs e) =>
MessageBox.Show(Resource.License, "SystrayProxyManager - Copyrights and license",
MessageBoxButton.OK,
MessageBoxImage.Information,
MessageBoxResult.OK,
MessageBoxOptions.DefaultDesktopOnly);
private void IESettings_Click(Object sender, RoutedEventArgs e) =>
IESettings.Run();
private void Clear_Click(Object sender, RoutedEventArgs e) =>
proxies.CurrentProxyServer = null;
private void Force_Click(Object sender, RoutedEventArgs e) =>
menu_Force.IsChecked = menu_Force.IsChecked ? true : false;
private void Edit_Click(Object sender, RoutedEventArgs e)
{
CenterWindowOnScreen();
this.Show();
this.ShowInTaskbar = true;
}
private void SetProxy_Click(Object sender, EventArgs e)
{
MenuItem item = sender as MenuItem;
if (item != null)
{
Proxy proxy = item.Tag as Proxy;
if (proxy != null)
{
proxies.CurrentProxyServer = proxy;
proxies.userDefinedProxyServer = proxy;
}
}
}
#endregion MainMenu click events
#region MainWindow events
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (exitOrigin == ExitOrigin.None)
{
UpdateSetProxyMenu();
this.Hide();
this.ShowInTaskbar = false;
e.Cancel = true;
}
}
private void Window_Closed(object sender, EventArgs e)
{
if (exitOrigin == ExitOrigin.User)
{
timer.Stop();
proxies.SaveProxies();
mutex.ReleaseMutex();
}
}
#endregion MainWindow events
private void CenterWindowOnScreen()
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth - windowWidth) / 2;
this.Top = (screenHeight - windowHeight) / 2;
}
}
public class TaskBarIcon_LeftClick : ICommand
{
Proxies proxies;
TaskbarIcon taskBarIcon;
public event EventHandler CanExecuteChanged;
public TaskBarIcon_LeftClick(Proxies proxies, TaskbarIcon taskBarIcon)
{
this.proxies = proxies;
this.taskBarIcon = taskBarIcon;
}
public void Execute(object o)
{
if (proxies.CurrentProxyState)
{
proxies.CurrentProxyState = false;
proxies.userDefinedProxyState = false;
taskBarIcon.Icon = Resource.on;
}
else
{
proxies.CurrentProxyState = true;
proxies.userDefinedProxyState = true;
taskBarIcon.Icon = Resource.off;
}
}
public bool CanExecute(object parameter) =>
true;
}
internal static class IESettings
{
private static System.Diagnostics.Process InternetSettings = new System.Diagnostics.Process()
{
// Microsoft Windows Internet Settings window
StartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "inetcpl.cpl ,4")
{
CreateNoWindow = true,
UseShellExecute = false,
}
};
internal static void Run() =>
InternetSettings.Start();
}
}