-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogBrowserForm.cs
More file actions
154 lines (134 loc) · 5.27 KB
/
LogBrowserForm.cs
File metadata and controls
154 lines (134 loc) · 5.27 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Windows.Forms;
using System.Linq;
namespace WorkTimer
{
public partial class LogBrowserForm : Form
{
private Dictionary<string, string> _logFilePaths = new Dictionary<string, string>();
private List<LogEntry>? _currentSessionLog;
private string _currentSessionDisplayName = "(Currently Running)";
public LogBrowserForm(List<LogEntry>? currentLog, string? currentTaskName)
{
InitializeComponent();
_currentSessionLog = currentLog;
if (!string.IsNullOrWhiteSpace(currentTaskName))
{
_currentSessionDisplayName = $"(Running) {currentTaskName}";
}
else if (currentLog != null)
{
_currentSessionDisplayName = "(Running) Untitled";
}
}
private void LogBrowserForm_Load(object? sender, EventArgs e)
{
LoadLogFiles();
}
private string GetAppDataPath()
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WorkTimer");
Directory.CreateDirectory(path);
return path;
}
private void LoadLogFiles()
{
var selectedItem = lstLogFiles.SelectedItem;
lstLogFiles.Items.Clear();
txtLogDetails.Clear();
_logFilePaths.Clear();
if (_currentSessionLog != null && _currentSessionLog.Any())
{
lstLogFiles.Items.Add(_currentSessionDisplayName);
}
string logDir = Path.Combine(GetAppDataPath(), "WorkTimerLogs");
if (!Directory.Exists(logDir))
{
return;
}
var logFiles = Directory.GetFiles(logDir, "*.log");
foreach (var file in logFiles)
{
string displayName = Path.GetFileNameWithoutExtension(file).Replace("_", " ");
_logFilePaths[displayName] = file;
lstLogFiles.Items.Add(displayName);
}
if (selectedItem != null && lstLogFiles.Items.Contains(selectedItem))
{
lstLogFiles.SelectedItem = selectedItem;
}
}
private void lstLogFiles_SelectedIndexChanged(object sender, EventArgs e)
{
txtLogDetails.Clear();
if (lstLogFiles.SelectedItem == null)
{
btnDelete.Enabled = false;
return;
}
string? selectedLog = lstLogFiles.SelectedItem.ToString();
if (selectedLog == _currentSessionDisplayName)
{
btnDelete.Enabled = false; // Cannot delete a running session
if (_currentSessionLog != null)
{
foreach (var entry in _currentSessionLog)
{
txtLogDetails.AppendText($"[{entry.Timestamp:HH:mm:ss}] [{entry.EventType}] {entry.Message}{Environment.NewLine}");
}
}
}
else
{
btnDelete.Enabled = true;
if (selectedLog != null && _logFilePaths.TryGetValue(selectedLog, out string? filePath))
{
try
{
string json = File.ReadAllText(filePath);
var logEntries = JsonSerializer.Deserialize<List<LogEntry>>(json);
if (logEntries != null)
{
foreach (var entry in logEntries)
{
txtLogDetails.AppendText($"[{entry.Timestamp:HH:mm:ss}] [{entry.EventType}] {entry.Message}{Environment.NewLine}");
}
}
}
catch (Exception ex)
{
txtLogDetails.Text = $"Error loading log file: {ex.Message}";
}
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (lstLogFiles.SelectedItem == null)
{
MessageBox.Show("Please select a log to delete.", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
var result = MessageBox.Show("Are you sure you want to permanently delete this log file?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
string? selectedLog = lstLogFiles.SelectedItem.ToString();
if (selectedLog != null && _logFilePaths.TryGetValue(selectedLog, out string? filePath))
{
try
{
File.Delete(filePath);
LoadLogFiles(); // Refresh the list
}
catch (Exception ex)
{
MessageBox.Show($"Could not delete log file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}