-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotiWindow.xaml.cs
300 lines (247 loc) · 10 KB
/
NotiWindow.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using MySql.Data.MySqlClient;
using System.Net;
using System.Media;
using System.Linq;
namespace TimeforBreak
{
/// <summary>
/// Interaction logic for NotiWindow.xaml
/// </summary>
public partial class NotiWindow : Window
{
public object ActiveWindow { get; private set; }
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
DispatcherTimer _timer;
TimeSpan _time;
string unit = "minutes.";
private void WindowIgnored()
{
user.t1 = DateTime.Now;
user.time_ = user.t1.ToString("yyyy-MM-dd HH:mm:ss");
user.secondsDiff01 = (int)((user.t1 - user.t0).TotalSeconds);
Trace.WriteLine("diiff01" + user.secondsDiff01);
//user.time_ = DateTime.Now.ToString();
user.title_ = GetActiveWindowTitle();
user.response = -1;
user.reason = "0";
MySqlConnection con = new MySqlConnection();
try
{
//con.ConnectionString = "Server = dsquare.ist.psu.edu; database = timebreak; UID =timebreak; password =mFPS9mMEBCgD;";
con.ConnectionString = "server = 107.180.40.19; Port = 3306; database = timebreak; UID=yuhan; Password=lab323;";
//con.ConnectionString = "data source = YPL5142-LOANER\\MSSQLSERVER01; database = timebreak; integrated security = SSPI";
con.Open();
Trace.WriteLine("Noti_Connected");
string cmdStr = "Insert into tblUsage(username, work_time, time0, active_window0, time1, secondsDiff01, active_window1, response) VALUES(@username, @work_time, @time0, @active_window0, @time1, @secondsDiff01, @active_window1, @response)";
using (MySqlCommand cmd = new MySqlCommand(cmdStr, con))
{
cmd.Parameters.AddWithValue("@username", user.username);
cmd.Parameters.AddWithValue("@work_time", user.work_time / 60);
cmd.Parameters.AddWithValue("@time0", user.time);
cmd.Parameters.AddWithValue("@active_window0", user.title);
cmd.Parameters.AddWithValue("@time1", user.time_);
cmd.Parameters.AddWithValue("@secondsDiff01", user.secondsDiff01);
cmd.Parameters.AddWithValue("@active_window1", user.title_);
cmd.Parameters.AddWithValue("@response", user.response);
cmd.ExecuteNonQuery();
Trace.WriteLine("inserted_Noti" + user.username + user.time_ + user.title_ + user.response);
}
}
catch (Exception e1)
{
Trace.WriteLine(e1.ToString());
}
con.Close();
// user._timer.Start();
}
//protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
//{
// bool wasCodeClosed = new StackTrace().GetFrames().FirstOrDefault(x => x.GetMethod() == typeof(Window).GetMethod("Close")) != null;
// if (wasCodeClosed)
// {
// //closed by this.close
// }
// else
// {
// user._timer.Start();
// }
//}
public NotiWindow()
{
InitializeComponent();
//WPlaySound.PlaySoundEvent("isNotification.Default");
// user._timer.Stop();
this.Topmost = true;
this.Activate();
label5.Content = "";
// Clear previous cookies
//Trace.WriteLine("start: " + user.response);
//user.response = -1;
//user.reason = "";
//user.comment = "";
//Trace.WriteLine("after cleaning: " + user.response);
if (user.work_time / 60 == 1)
{
unit = "minute.";
}
label2_copy.Content = (user.work_time / 60).ToString() + " " + unit;
//var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
//this.Left = desktopWorkingArea.Right - this.Width;
//this.Top = desktopWorkingArea.Bottom - this.Height;
//this.Activate();
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = workingArea.Right - this.Width;
this.Top = workingArea.Bottom - this.Height;
this.Activate();
}));
_time = TimeSpan.FromSeconds(user.disppear);
_timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
{
if (_time == TimeSpan.FromSeconds(12))
{
ChangeLayout0();
}
if (_time == TimeSpan.FromSeconds(11))
{
ChangeLayout1();
}
if (_time == TimeSpan.FromSeconds(10))
{
ChangeLayout2();
}
if (_time == TimeSpan.FromSeconds(9))
{
ChangeLayout3();
}
if (_time == TimeSpan.Zero)
{
_timer.Stop();
WindowIgnored();
Trace.WriteLine("countdown");
//MainWindow main = new MainWindow();
//main.Show();
user.count_mins = 0;
user.count_seconds = 0;
user._timer.Start();
this.Close();
}
_time = _time.Add(TimeSpan.FromSeconds(-1));
}, Application.Current.Dispatcher);
}
private bool CheckConnection(String URL)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 5000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) return true;
else return false;
}
catch
{
return false;
}
}
private void ChangeLayout0()
{
brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffb3b3"));
Trace.WriteLine("12 seconds left");
//this.Background = System.Windows.Media.Brushes.MistyRose;
this.Activate();
}
private void ChangeLayout1()
{
brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffcccc"));
Trace.WriteLine("11 seconds left");
//this.Background = System.Windows.Media.Brushes.MistyRose;
this.Activate();
}
private void ChangeLayout2()
{
brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffe6e6"));
Trace.WriteLine("10 seconds left");
//this.Background = System.Windows.Media.Brushes.MistyRose;
this.Activate();
}
private void ChangeLayout3()
{
brd.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#f9f9ff"));
Trace.WriteLine("9 seconds left");
//this.Background = System.Windows.Media.Brushes.MistyRose;
this.Activate();
}
private static string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
if (Buff.ToString().Contains(" - "))
{
string temp = Buff.ToString();
int index = temp.LastIndexOf(" - ");
int index1 = (temp.Length) - index;
Trace.WriteLine("index: " + index + ", Length: " + temp.Length + ", last-index: " + index1);
string temp1 = temp.Substring(index + 3, temp.Length - 3 - index);
return temp1;
}
else
{
return Buff.ToString();
}
}
return null;
}
private void radioButton_Checked(object sender, RoutedEventArgs e)
{
user.response = 1;
user.reason = "";
user.comment = "";
label5.Content = "";
Notiwin1 noti1 = new Notiwin1();
noti1.Show();
this.Close();
}
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
user.response = 0;
label5.Content = "";
Notiwin2 noti2 = new Notiwin2();
noti2.Show();
this.Close();
}
private void button_close_Click(object sender, RoutedEventArgs e)
{
WindowIgnored();
//MainWindow main = new MainWindow();
//main.Show();
user.count_mins = 0;
user.count_seconds = 0;
user._timer.Start();
this.Close();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if (_timer != null)
_timer.Stop();
}
}
}