-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMainForm.cs
571 lines (463 loc) · 16.7 KB
/
MainForm.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
using ApplicationLogger.Properties;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
namespace ApplicationLogger {
public partial class MainForm : Form {
// Constants
private const string SETTINGS_FIELD_RUN_AT_STARTUP = "RunAtStartup";
private const string REGISTRY_KEY_ID = "ApplicationLogger"; // Registry app key for when it's running at startup
private const string CONFIG_FILE = "ApplicationLogger.cfg";
private const string LINE_DIVIDER = "\t";
private const string LINE_END = "\r\n";
private const string DATE_TIME_FORMAT = "o"; // 2008-06-15T21:15:07.0000000
// Properties
private Timer timerCheck;
private ContextMenu contextMenu;
private MenuItem menuItemOpen;
private MenuItem menuItemOpenLog;
private MenuItem menuItemStartStop;
private MenuItem menuItemRunAtStartup;
private MenuItem menuItemExit;
private bool allowClose;
private bool allowShow;
private bool isRunning;
private bool isUserIdle;
private bool hasInitialized;
private string lastUserProcessId;
private string lastFileNameSaved;
private int lastDayLineLogged;
private DateTime lastTimeQueueWritten;
private List<string> queuedLogMessages;
private string configPath;
private float? configIdleTime; // In seconds
private float? configTimeCheckInterval; // In seconds
private float? configMaxQueueTime;
private int? configMaxQueueEntries;
private string newUserProcessId; // Temp
private StringBuilder lineToLog; // Temp, used to create the line
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
public MainForm() {
InitializeComponent();
initializeForm();
}
// ================================================================================================================
// EVENT INTERFACE ------------------------------------------------------------------------------------------------
private void onFormLoad(object sender, EventArgs e) {
// First time the form is shown
}
protected override void SetVisibleCore(bool isVisible) {
if (!allowShow) {
// Initialization form show, when it's ran: doesn't allow showing form
isVisible = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(isVisible);
}
private void onFormClosing(object sender, FormClosingEventArgs e) {
// Form is attempting to close
if (!allowClose) {
// User initiated, just minimize instead
e.Cancel = true;
Hide();
}
}
private void onFormClosed(object sender, FormClosedEventArgs e) {
// Stops everything
stop();
// If debugging, un-hook itself from startup
if (System.Diagnostics.Debugger.IsAttached && windowsRunAtStartup) windowsRunAtStartup = false;
}
private void onTimer(object sender, EventArgs e) {
// Timer tick: check for the current application
// Check the user is idle
if (SystemHelper.GetIdleTime() >= configIdleTime * 1000f) {
if (!isUserIdle) {
// User is now idle
isUserIdle = true;
lastUserProcessId = null;
logUserIdle();
}
} else {
if (isUserIdle) {
// User is not idle anymore
isUserIdle = false;
}
}
// Check the user process
if (!isUserIdle) {
var process = getCurrentUserProcess();
if (process != null) {
// Valid process, create a unique id
newUserProcessId = process.ProcessName + "_" + process.MainWindowTitle;
if (lastUserProcessId != newUserProcessId) {
// New process
logUserProcess(process);
lastUserProcessId = newUserProcessId;
}
}
}
// Write to log if enough time passed
if (queuedLogMessages.Count > 0 && (DateTime.Now - lastTimeQueueWritten).TotalSeconds > configMaxQueueTime ) {
commitLines();
}
}
private void onResize(object sender, EventArgs e) {
// Resized window
//notifyIcon.BalloonTipTitle = "Minimize to Tray App";
//notifyIcon.BalloonTipText = "You have successfully minimized your form.";
if (WindowState == FormWindowState.Minimized) {
//notifyIcon.ShowBalloonTip(500);
this.Hide();
}
}
private void onMenuItemOpenClicked(object Sender, EventArgs e) {
showForm();
}
private void onMenuItemStartStopClicked(object Sender, EventArgs e) {
if (isRunning) {
stop();
} else {
start();
}
}
private void onMenuItemOpenLogClicked(object Sender, EventArgs e) {
commitLines();
Process.Start(getLogFileName());
}
private void onMenuItemRunAtStartupClicked(object Sender, EventArgs e) {
menuItemRunAtStartup.Checked = !menuItemRunAtStartup.Checked;
settingsRunAtStartup = menuItemRunAtStartup.Checked;
applySettingsRunAtStartup();
}
private void onMenuItemExitClicked(object Sender, EventArgs e) {
exit();
}
private void onDoubleClickNotificationIcon(object sender, MouseEventArgs e) {
showForm();
}
// ================================================================================================================
// INTERNAL INTERFACE ---------------------------------------------------------------------------------------------
private void initializeForm() {
// Initialize
if (!hasInitialized) {
allowClose = false;
isRunning = false;
queuedLogMessages = new List<string>();
lineToLog = new StringBuilder();
lastFileNameSaved = "";
allowShow = false;
// Force working folder
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
// Read configuration
readConfiguration();
// Create context menu for the tray icon and update it
createContextMenu();
// Update tray
updateTrayIcon();
// Check if it needs to run at startup
applySettingsRunAtStartup();
// Finally, start
start();
hasInitialized = true;
}
}
private void createContextMenu() {
// Initialize context menu
contextMenu = new ContextMenu();
// Initialize menu items
menuItemOpen = new MenuItem();
menuItemOpen.Index = 0;
menuItemOpen.Text = "&Open";
menuItemOpen.Click += new EventHandler(onMenuItemOpenClicked);
contextMenu.MenuItems.Add(menuItemOpen);
menuItemStartStop = new MenuItem();
menuItemStartStop.Index = 0;
menuItemStartStop.Text = ""; // Set later
menuItemStartStop.Click += new EventHandler(onMenuItemStartStopClicked);
contextMenu.MenuItems.Add(menuItemStartStop);
contextMenu.MenuItems.Add("-");
menuItemOpenLog = new MenuItem();
menuItemOpenLog.Index = 0;
menuItemOpenLog.Text = ""; // Set later
menuItemOpenLog.Click += new EventHandler(onMenuItemOpenLogClicked);
contextMenu.MenuItems.Add(menuItemOpenLog);
contextMenu.MenuItems.Add("-");
menuItemRunAtStartup = new MenuItem();
menuItemRunAtStartup.Index = 0;
menuItemRunAtStartup.Text = "Run at Windows startup";
menuItemRunAtStartup.Click += new EventHandler(onMenuItemRunAtStartupClicked);
menuItemRunAtStartup.Checked = settingsRunAtStartup;
contextMenu.MenuItems.Add(menuItemRunAtStartup);
contextMenu.MenuItems.Add("-");
menuItemExit = new MenuItem();
menuItemExit.Index = 1;
menuItemExit.Text = "E&xit";
menuItemExit.Click += new EventHandler(onMenuItemExitClicked);
contextMenu.MenuItems.Add(menuItemExit);
notifyIcon.ContextMenu = contextMenu;
updateContextMenu();
}
private void updateContextMenu() {
// Update start/stop command
if (menuItemStartStop != null) {
if (isRunning) {
menuItemStartStop.Text = "&Stop";
} else {
menuItemStartStop.Text = "&Start";
}
}
// Update filename
if (menuItemOpenLog != null) {
var filename = getLogFileName();
if (!System.IO.File.Exists(filename)) {
// Doesn't exist
menuItemOpenLog.Text = "Open &log file";
menuItemOpenLog.Enabled = false;
} else {
// Exists
menuItemOpenLog.Text = "Open &log file (" + filename + ")";
menuItemOpenLog.Enabled = true;
}
}
}
private void updateTrayIcon() {
if (isRunning) {
notifyIcon.Icon = ApplicationLogger.Properties.Resources.iconNormal;
notifyIcon.Text = "Application Logger (started)";
} else {
notifyIcon.Icon = ApplicationLogger.Properties.Resources.iconStopped;
notifyIcon.Text = "Application Logger (stopped)";
}
}
private void readConfiguration() {
// Read the current configuration file
// Read default file
ConfigParser configDefault = new ConfigParser(ApplicationLogger.Properties.Resources.default_config);
ConfigParser configUser;
if (!System.IO.File.Exists(CONFIG_FILE)) {
// Config file not found, create it first
Console.Write("Config file does not exist, creating");
// Write file so it can be edited by the user
System.IO.File.WriteAllText(CONFIG_FILE, ApplicationLogger.Properties.Resources.default_config);
// User config is the same as the default
configUser = configDefault;
} else {
// Read the existing user config
configUser = new ConfigParser(System.IO.File.ReadAllText(CONFIG_FILE));
}
// Interprets config data
configPath = configUser.getString("path") ?? configDefault.getString("path");
configIdleTime = configUser.getFloat("idleTime") ?? configDefault.getFloat("idleTime");
configTimeCheckInterval = configUser.getFloat("checkInterval") ?? configDefault.getFloat("checkInterval");
configMaxQueueEntries = configUser.getInt("maxQueueEntries") ?? configDefault.getInt("maxQueueEntries");
configMaxQueueTime = configUser.getFloat("maxQueueTime") ?? configDefault.getFloat("maxQueueTime");
}
private void start() {
if (!isRunning) {
// Initialize timer
timerCheck = new Timer();
timerCheck.Tick += new EventHandler(onTimer);
timerCheck.Interval = (int)(configTimeCheckInterval * 1000f);
timerCheck.Start();
lastUserProcessId = null;
lastTimeQueueWritten = DateTime.Now;
isRunning = true;
updateContextMenu();
updateTrayIcon();
}
}
private void stop() {
if (isRunning) {
logStop();
timerCheck.Stop();
timerCheck.Dispose();
timerCheck = null;
isRunning = false;
updateContextMenu();
updateTrayIcon();
}
}
private void logUserIdle() {
// Log that the user is idle
logLine("status::idle", true, false, configIdleTime ?? 0);
updateText("User idle");
newUserProcessId = null;
}
private void logStop() {
// Log stopping the application
logLine("status::stop", true);
updateText("Stopped");
newUserProcessId = null;
}
private void logEndOfDay() {
// Log an app focus change after the end of the day, and at the end of the specific log file
logLine("status::end-of-day", true, true);
newUserProcessId = null;
}
private void logUserProcess(Process process) {
// Log the current user process
int dayOfLog = DateTime.Now.Day;
if (dayOfLog != lastDayLineLogged) {
// The last line was logged on a different day, so check if it should be a new file
string newFileName = getLogFileName();
if (newFileName != lastFileNameSaved && lastFileNameSaved != "") {
// It's a new file: commit current with an end-of-day event
logEndOfDay();
}
}
try {
logLine("app::focus", process.ProcessName, process.MainModule.FileName, process.MainWindowTitle);
updateText("Name: " + process.ProcessName + ", " + process.MainWindowTitle);
} catch (Exception exception) {
logLine("app::focus", process.ProcessName, "?", "?");
updateText("Name: " + process.ProcessName + ", ?");
}
}
private void logLine(string type, bool forceCommit = false, bool usePreviousDayFileName = false, float idleTimeOffsetSeconds = 0) {
logLine(type, "", "", "", forceCommit, usePreviousDayFileName, idleTimeOffsetSeconds);
}
private void logLine(string type, string title, string location, string subject, bool forceCommit = false, bool usePreviousDayFileName = false, float idleTimeOffsetSeconds = 0) {
// Log a single line
DateTime now = DateTime.Now;
now.AddSeconds(idleTimeOffsetSeconds);
lineToLog.Clear();
lineToLog.Append(now.ToString(DATE_TIME_FORMAT));
lineToLog.Append(LINE_DIVIDER);
lineToLog.Append(type);
lineToLog.Append(LINE_DIVIDER);
lineToLog.Append(Environment.MachineName);
lineToLog.Append(LINE_DIVIDER);
lineToLog.Append(title);
lineToLog.Append(LINE_DIVIDER);
lineToLog.Append(location);
lineToLog.Append(LINE_DIVIDER);
lineToLog.Append(subject);
lineToLog.Append(LINE_END);
//Console.Write("LOG ==> " + lineToLog.ToString());
queuedLogMessages.Add(lineToLog.ToString());
lastDayLineLogged = DateTime.Now.Day;
if (queuedLogMessages.Count > configMaxQueueEntries || forceCommit) {
if (usePreviousDayFileName) {
commitLines(lastFileNameSaved);
} else {
commitLines();
}
}
}
private void commitLines(string fileName = null) {
// Commit all currently queued lines to the file
// If no commit needed, just return
if (queuedLogMessages.Count == 0) return;
lineToLog.Clear();
foreach (var line in queuedLogMessages) {
lineToLog.Append(line);
}
string commitFileName = fileName ?? getLogFileName();
bool saved = false;
// Check if the path exists, creating it otherwise
string filePath = System.IO.Path.GetDirectoryName(commitFileName);
if (filePath.Length > 0 && !System.IO.Directory.Exists(filePath)) {
System.IO.Directory.CreateDirectory(filePath);
}
try {
System.IO.File.AppendAllText(commitFileName, lineToLog.ToString());
saved = true;
} catch (Exception exception) {
}
if (saved) {
// Saved successfully, now clear the queue
queuedLogMessages.Clear();
lastTimeQueueWritten = DateTime.Now;
updateContextMenu();
}
}
private void updateText(string text) {
labelApplication.Text = text;
}
private void applySettingsRunAtStartup() {
// Check whether it's properly set to run at startup or not
if (settingsRunAtStartup) {
// Should run at startup
if (!windowsRunAtStartup) windowsRunAtStartup = true;
} else {
// Should not run at startup
if (windowsRunAtStartup) windowsRunAtStartup = false;
}
}
private void showForm() {
allowShow = true;
Show();
WindowState = FormWindowState.Normal;
}
private void exit() {
allowClose = true;
Close();
}
// ================================================================================================================
// ACCESSOR INTERFACE ---------------------------------------------------------------------------------------------
private bool settingsRunAtStartup {
// Whether the settings say the app should run at startup or not
get {
return (bool)Settings.Default[SETTINGS_FIELD_RUN_AT_STARTUP];
}
set {
Settings.Default[SETTINGS_FIELD_RUN_AT_STARTUP] = value;
Settings.Default.Save();
}
}
private bool windowsRunAtStartup {
// Whether it's actually set to run at startup or not
get {
return getStartupRegistryKey().GetValue(REGISTRY_KEY_ID) != null;
}
set {
if (value) {
// Add
getStartupRegistryKey(true).SetValue(REGISTRY_KEY_ID, Application.ExecutablePath.ToString());
//Console.WriteLine("RUN AT STARTUP SET AS => TRUE");
} else {
// Remove
getStartupRegistryKey(true).DeleteValue(REGISTRY_KEY_ID, false);
//Console.WriteLine("RUN AT STARTUP SET AS => FALSE");
}
}
}
private RegistryKey getStartupRegistryKey(bool writable = false) {
return Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", writable);
}
private Process getCurrentUserProcess() {
// Find the process that's currently on top
var processes = Process.GetProcesses();
var foregroundWindowHandle = SystemHelper.GetForegroundWindow();
foreach (var process in processes) {
if (process.Id <= 4) { continue; } // system processes
if (process.MainWindowHandle == foregroundWindowHandle) return process;
}
// Nothing found!
return null;
}
private string getLogFileName() {
// Get the log filename for something to be logged now
var now = DateTime.Now;
var filename = configPath;
// Replaces variables
filename = filename.Replace("[[month]]", now.ToString("MM"));
filename = filename.Replace("[[day]]", now.ToString("dd"));
filename = filename.Replace("[[year]]", now.ToString("yyyy"));
filename = filename.Replace("[[machine]]", Environment.MachineName);
var pathOnly = System.IO.Path.GetDirectoryName(filename);
var fileOnly = System.IO.Path.GetFileName(filename);
// Make it safe
foreach (char c in System.IO.Path.GetInvalidFileNameChars()) {
fileOnly = fileOnly.Replace(c, '_');
}
return (pathOnly.Length > 0 ? pathOnly + "\\" : "") + fileOnly;
}
}
}