-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPersistence.cs
98 lines (87 loc) · 3.74 KB
/
Persistence.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
using System;
using System.IO;
namespace Herring
{
static class Persistence
{
private static TextWriter writer;
public static string GetLocalDataDir()
{
string dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
dir = System.IO.Path.Combine(dir, "Herring Activity Tracker");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
return dir;
}
public static string GetScreenDataDir()
{
string dir = GetLocalDataDir();
dir = System.IO.Path.Combine(dir, "Screen");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
return dir;
}
public static string GetApplicationDir()
{
string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
dir = System.IO.Path.Combine(dir, "Herring Activity Tracker");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
return dir;
}
private static string ConstructFileName(DateTime date, bool full)
{
string name = String.Format("herring{0:D4}{1:D2}{2:D2}_{3:D2}{4:D2}{5:D2}.csv", date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
string path = Path.Combine(GetLocalDataDir(), name);
return path;
}
public static void Store(ActivitySummary data)
{
if (writer == null)
{
string path = ConstructFileName(DateTime.Now, true);
writer = new StreamWriter(path);
writer.WriteLine("time;span;process;title;subtitle;document;share;keyboard-intensity;mouse-intensity;");
}
writer.Write(data.TimePoint.ToString() + ";");
writer.Write(data.Span.ToString() + ";");
writer.Write(";");
writer.Write(";");
writer.Write(";");
writer.Write(";");
writer.Write(data.TotalShare.ToString("F2") + ";");
writer.Write(data.TotalKeyboardIntensity.ToString("F2") + ";");
writer.Write(data.TotalMouseIntensity.ToString("F2") + ";");
writer.WriteLine();
foreach (ActivityEntry entry in data.Entries)
{
writer.Write(";");
writer.Write(";");
writer.Write(entry.App.Path + ";");
writer.Write(entry.ApplicationTitle.Replace(';', ',').Replace('\r',' ').Replace("\n", " ") + ";");
writer.Write(entry.WindowTitle.Replace(';', ',').Replace('\r', ' ').Replace("\n", " ") + ";");
writer.Write(entry.DocumentUrl.Replace(';', ',').Replace('\r', ' ').Replace("\n", " ") + ";");
writer.Write(entry.Share.ToString("F2") + ";");
writer.Write(entry.KeyboardIntensity.ToString("F2") + ";");
writer.Write(entry.MouseIntensity.ToString("F2") + ";");
writer.WriteLine();
}
writer.Flush();
}
public static void StoreBitmap(System.Drawing.Bitmap bmp)
{
DateTime date = DateTime.Now;
string name = String.Format("screen{0:D4}{1:D2}{2:D2}_{3:D2}{4:D2}{5:D2}.png", date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second);
string path = Path.Combine(GetScreenDataDir(), name);
bmp.Save(path);
}
public static void Close()
{
if (writer != null)
{
writer.Close();
writer = null;
}
}
}
}