Skip to content

Commit 3aa1aaf

Browse files
Initial commit
0 parents  commit 3aa1aaf

File tree

253 files changed

+9663
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

253 files changed

+9663
-0
lines changed

CommonSet.cs

Lines changed: 397 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,397 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
// import the library of windows network
8+
using System.Net.NetworkInformation;
9+
using System.Diagnostics;
10+
11+
// import the library for auto start
12+
using Microsoft.Win32;
13+
using System.Diagnostics;
14+
15+
class CommonSet
16+
{
17+
// veriable set
18+
public NetworkInterface[] interfaces;
19+
20+
public double previousUploadValue = 0;
21+
public double previousDownloadValue = 0;
22+
23+
public double uploadTrafficValue;
24+
public double downloadTrafficValue;
25+
26+
public string configFileName = "configuration";
27+
public string floatingPositionFileName = "position";
28+
public string configurationExecutableName = "SNTMConfiguration.exe";
29+
public string floatingWindowExecutableName = "SNTMFloatingWindow.exe";
30+
public string baseProgramExecutableName = "SNTMStartProcess.exe";
31+
public string booterProgramExecutableName = "SNTMBooter.exe";
32+
33+
public string configurationProcessName = "SNTMConfiguration";
34+
public string floatingWindowProcessName = "SNTMFloatingWindow";
35+
public string baseProgramProcessName = "SNTMStartProcess";
36+
public string booterProgramProcessName = "SNTMBooter";
37+
38+
public int loadedLanguageIndex = 0;
39+
public int loadedInterfaceIndex = 0;
40+
public double loadedUpdateFrequency = 1;
41+
public int loadedSpeedUnitIndex = 0;
42+
public string loadedSpeedUnit = "";
43+
public int loadedDisplayMethodIndex = 0;
44+
public int loadedDisplaySizeIndex = 0;
45+
public int loadedDarkThemeIndex = 0;
46+
public bool loadedAutoStartCheck = true;
47+
public bool loadedShowBubbleCheck = false;
48+
49+
public double maxFrequency = 10;
50+
public double minFrequency = 0.1;
51+
52+
public int numberOfSpeedUnit = 6;
53+
public int numberOfWindowSize = 3;
54+
public int numberOfLanguage = 2;
55+
56+
public long previousSystemTime = 0;
57+
public long currentSystemTime = 0;
58+
59+
public bool CheckIfRunning(String programName)
60+
{
61+
Process[] processes = Process.GetProcessesByName(programName);
62+
// check if there any program running with same name
63+
if (processes.Length > 0)
64+
{
65+
return true;
66+
}
67+
return false;
68+
}
69+
70+
public bool CheckIfRunningSameProcess(String programName)
71+
{
72+
Process[] processes = Process.GetProcessesByName(programName);
73+
// check if there any program running with same name
74+
if (processes.Length > 1)
75+
{
76+
return true;
77+
}
78+
return false;
79+
}
80+
81+
public bool CheckIfNameChanged(String programName)
82+
{
83+
Process[] processes = Process.GetProcessesByName(programName);
84+
// check if there any program running with same name
85+
if (processes.Length < 1)
86+
{
87+
return true;
88+
}
89+
return false;
90+
}
91+
92+
public void ApplyAutoStartSetting(bool autoStartChecked)
93+
{
94+
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
95+
96+
// enable or disable if the auto start check box is checked or not
97+
if (autoStartChecked)
98+
{
99+
rkApp.SetValue("SNTMauto", System.AppDomain.CurrentDomain.BaseDirectory + booterProgramExecutableName);
100+
}
101+
else
102+
{
103+
rkApp.DeleteValue("SNTMauto", false);
104+
}
105+
106+
}
107+
108+
public bool SaveFloatingWindowPosition(double xPos, double yPos)
109+
{
110+
try
111+
{
112+
using (System.IO.StreamWriter file = new System.IO.StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + floatingPositionFileName, false))
113+
{
114+
// update the position
115+
file.WriteLine(String.Format("{0:F2}", xPos));
116+
file.WriteLine(String.Format("{0:F2}", yPos));
117+
}
118+
}
119+
catch
120+
{
121+
return false;
122+
}
123+
return true;
124+
}
125+
126+
public void InitCommonSet()
127+
{
128+
GetInterfaceInformation();
129+
// get the time when the software is started
130+
GetCurrentTimeToPreviousTime();
131+
}
132+
133+
private void GetCurrentTimeToPreviousTime()
134+
{
135+
this.previousSystemTime = System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
136+
}
137+
138+
private void GetInterfaceInformation()
139+
{
140+
// get network interface information
141+
142+
// check if the network is available
143+
if (!NetworkInterface.GetIsNetworkAvailable())
144+
{
145+
return;
146+
}
147+
148+
// get the list of network interface
149+
interfaces = NetworkInterface.GetAllNetworkInterfaces();
150+
}
151+
152+
// check if this computer have any network interface, need to run init common set (get interface information) first
153+
public bool CheckIfHaveInterface()
154+
{
155+
if (interfaces.Length > 0)
156+
{
157+
return true;
158+
}
159+
return false;
160+
}
161+
162+
// start dpecified process
163+
public bool StartProcess(string targetExecutable)
164+
{
165+
try
166+
{
167+
168+
System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + targetExecutable);
169+
}
170+
catch
171+
{
172+
return false;
173+
}
174+
return true;
175+
}
176+
177+
// end specified process
178+
public void EndProcess(string targetedProcess)
179+
{
180+
// check if targeted process is opened, if yes close all the floating window
181+
Process[] processes = Process.GetProcessesByName(targetedProcess);
182+
foreach (var process in processes)
183+
{
184+
process.Kill();
185+
}
186+
187+
}
188+
189+
// end all process
190+
public void EndAllProcess()
191+
{
192+
// close all the related application
193+
// check if floating window is opened, if yes close all the floating window
194+
Process[] processes = Process.GetProcessesByName(floatingWindowProcessName);
195+
foreach (var process in processes)
196+
{
197+
process.Kill();
198+
}
199+
200+
// check if configuration window is opened, if yes close all the floating window
201+
processes = Process.GetProcessesByName(configurationProcessName);
202+
foreach (var process in processes)
203+
{
204+
process.Kill();
205+
}
206+
207+
// close booter
208+
processes = Process.GetProcessesByName(booterProgramProcessName);
209+
foreach (var process in processes)
210+
{
211+
process.Kill();
212+
}
213+
214+
// close this application
215+
processes = Process.GetProcessesByName(baseProgramProcessName);
216+
foreach (var process in processes)
217+
{
218+
process.Kill();
219+
}
220+
221+
222+
}
223+
224+
public void GetTrafficInformation(int SpeedUnitSelectedIndex, int selectedInterfaceIndex)
225+
{
226+
/*Task.Factory.StartNew(() =>
227+
{*/
228+
double divisionParameter = 1.0;
229+
long uploadTrafficValueTemp = interfaces[selectedInterfaceIndex].GetIPv4Statistics().BytesSent;
230+
long downloadTrafficValueTemp = interfaces[selectedInterfaceIndex].GetIPv4Statistics().BytesReceived;
231+
232+
// get the value corrseponding to the selected speed
233+
uploadTrafficValue = uploadTrafficValueTemp - previousUploadValue;
234+
downloadTrafficValue = downloadTrafficValueTemp - previousDownloadValue;
235+
236+
// calculate the division parameter
237+
for (int i = 0; i <= SpeedUnitSelectedIndex; i++)
238+
{
239+
if (i % 2 == 0)
240+
{
241+
divisionParameter *= 1024;
242+
}
243+
}
244+
245+
// if the unit is for small "b", then * 8
246+
if (SpeedUnitSelectedIndex % 2 == 0)
247+
{
248+
divisionParameter /= 8;
249+
}
250+
251+
// Get current system time
252+
currentSystemTime = System.DateTimeOffset.Now.ToUnixTimeMilliseconds();
253+
254+
// divide the value accroading to the update frequency
255+
divisionParameter *= (Convert.ToDouble(currentSystemTime) - Convert.ToDouble(previousSystemTime)) / 1000.0;
256+
257+
// move current time as previous time for next traffic calculation
258+
previousSystemTime = currentSystemTime;
259+
260+
// compute the traffic
261+
uploadTrafficValue /= divisionParameter;
262+
downloadTrafficValue /= divisionParameter;
263+
264+
// update the current value as previous value
265+
previousUploadValue = uploadTrafficValueTemp;
266+
previousDownloadValue = downloadTrafficValueTemp;
267+
//});
268+
269+
}
270+
271+
272+
public bool LoadSetting()
273+
{
274+
try
275+
{
276+
using (System.IO.StreamReader file = new System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + configFileName))
277+
{
278+
bool formatError = false;
279+
// read language index
280+
if (!Int32.TryParse(file.ReadLine(), out loadedLanguageIndex))
281+
{
282+
formatError = true;
283+
}
284+
else
285+
{
286+
// check if the language index is in range
287+
if (loadedLanguageIndex < 0 || loadedLanguageIndex > numberOfLanguage)
288+
{
289+
formatError = true;
290+
}
291+
}
292+
293+
// read interface index
294+
if (!Int32.TryParse(file.ReadLine(), out loadedInterfaceIndex))
295+
{
296+
formatError = true;
297+
}
298+
else
299+
{
300+
// check if the index is in range
301+
if (loadedInterfaceIndex < 0 || loadedInterfaceIndex >= interfaces.Length)
302+
{
303+
formatError = true;
304+
}
305+
}
306+
307+
// read update frequency
308+
if (!Double.TryParse(file.ReadLine(), out loadedUpdateFrequency))
309+
{
310+
formatError = true;
311+
}
312+
// check if the update frequency is in range
313+
else if (loadedUpdateFrequency < minFrequency || loadedUpdateFrequency > maxFrequency)
314+
{
315+
formatError = true;
316+
}
317+
318+
// read speed unit index
319+
if (!Int32.TryParse(file.ReadLine(), out loadedSpeedUnitIndex))
320+
{
321+
formatError = true;
322+
}
323+
else if (loadedSpeedUnitIndex < 0 || loadedSpeedUnitIndex > numberOfSpeedUnit)
324+
{
325+
formatError = true;
326+
}
327+
328+
// read speed unit
329+
loadedSpeedUnit = file.ReadLine();
330+
331+
// read show method index
332+
if (!Int32.TryParse(file.ReadLine(), out loadedDisplayMethodIndex))
333+
{
334+
formatError = true;
335+
}
336+
else if (loadedDisplayMethodIndex < 0 || loadedDisplayMethodIndex > 3)
337+
{
338+
formatError = true;
339+
}
340+
341+
// read floating window size index
342+
if (!Int32.TryParse(file.ReadLine(), out loadedDisplaySizeIndex))
343+
{
344+
formatError = true;
345+
}
346+
else if (loadedDisplaySizeIndex < 0 || loadedDisplaySizeIndex >= numberOfWindowSize)
347+
{
348+
formatError = true;
349+
}
350+
351+
// read dark mode index
352+
if (!Int32.TryParse(file.ReadLine(), out loadedDarkThemeIndex))
353+
{
354+
formatError = true;
355+
}
356+
else
357+
{
358+
// check if the dark mode index is in range
359+
if (loadedDarkThemeIndex < 0 || loadedDarkThemeIndex > 1)
360+
{
361+
formatError = true;
362+
}
363+
}
364+
365+
// read auto start option
366+
if (!Boolean.TryParse(file.ReadLine(), out loadedAutoStartCheck))
367+
{
368+
formatError = true;
369+
}
370+
371+
// read show bubble option
372+
if (!Boolean.TryParse(file.ReadLine(), out loadedShowBubbleCheck))
373+
{
374+
formatError = true;
375+
}
376+
377+
// check if the format of file correct
378+
if (formatError)
379+
{
380+
// format of the file is wrong, return false
381+
return false;
382+
}
383+
else
384+
{
385+
// return true to tell the file readed successfully
386+
return true;
387+
}
388+
389+
}
390+
}
391+
catch
392+
{
393+
// error occur, return false
394+
return false;
395+
}
396+
}
397+
}

0 commit comments

Comments
 (0)