-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportForm.cs
More file actions
149 lines (130 loc) · 5.37 KB
/
Copy pathExportForm.cs
File metadata and controls
149 lines (130 loc) · 5.37 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
using System;
using System.IO;
using System.Threading;
using Eto.Drawing;
using Eto.Forms;
namespace devtime
{
public class ExportForm : Dialog<DialogResult>
{
private readonly string _outputFile;
private readonly string _context;
private readonly string _from;
private readonly string _to;
private readonly Exporter.ExportFormat _exportFormat;
private readonly bool _exportDate;
private readonly bool _exportTime;
private readonly bool _exportLog;
private readonly Exporter.TimeFormat _timeFormat;
private readonly Exporter.TimeApproximation _timeApproximation;
private readonly ProgressBar _progressBar;
private Thread? _exporterThread;
private volatile bool _exporterShouldRun = true;
private DialogResult _result = DialogResult.Cancel;
public ExportForm(string outputFile, string context, string from, string to, Exporter.ExportFormat exportFormat, bool exportDate, bool exportTime, bool exportLog, Exporter.TimeFormat timeFormat, Exporter.TimeApproximation timeApproximation)
{
Title = "Exporting...";
Icon = IconLoader.LoadIcon("export.png");
Resizable = false;
_outputFile = outputFile;
_context = context;
_from = from;
_to = to;
_exportFormat = exportFormat;
_exportDate = exportDate;
_exportTime = exportTime;
_exportLog = exportLog;
_timeFormat = timeFormat;
_timeApproximation = timeApproximation;
_progressBar = new ProgressBar { MinValue = 0, MaxValue = 100, Value = 0 };
Content = new StackLayout
{
Padding = new Padding(15, 10),
Spacing = 5,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
Items =
{
new Label { Text = "Exporting data..." },
_progressBar
}
};
Shown += (s, e) =>
{
_exporterThread = new Thread(ExportWork);
_exporterThread.Start();
};
Closing += (s, e) =>
{
if (_exporterThread != null && _exporterThread.IsAlive)
{
_exporterShouldRun = false;
_exporterThread.Join();
}
};
}
private void ExportWork()
{
try
{
using (StreamWriter output = new StreamWriter(_outputFile))
{
Exporter? exporter = null;
switch (_exportFormat)
{
case Exporter.ExportFormat.CSV:
exporter = new CsvExporter(output, _exportDate, _exportTime, _exportLog, _timeFormat, _timeApproximation);
break;
case Exporter.ExportFormat.JSON:
exporter = new JsonExporter(output, _exportDate, _exportTime, _exportLog, _timeFormat, _timeApproximation);
break;
}
if (exporter == null) return;
long totalEntries = DB.SelectTotalEntryCount(_context, _from, _to);
long bufferSize = Config.DatabaseExportBufferSize;
if (bufferSize <= 0) bufferSize = 30; // fallback
long iterations = totalEntries / bufferSize + (totalEntries % bufferSize > 0 ? 1 : 0);
Application.Instance.Invoke(() =>
{
_progressBar.MaxValue = (int)Math.Max(1, iterations);
});
exporter.ExportStart();
bool first = true;
for (long i = 0; i < iterations && _exporterShouldRun; ++i)
{
var entries = DB.SelectEntries(_context, _from, _to, (int)bufferSize, (int)(i * bufferSize));
for (int j = 0; j < entries.Count && _exporterShouldRun; ++j)
{
if (!first)
{
exporter.ExportMid();
}
first = false;
var entry = entries[j];
exporter.ExportEntry(entry.Date, entry.LoggedTime, entry.Log ?? "");
}
Application.Instance.Invoke(() =>
{
_progressBar.Value = (int)i + 1;
});
}
exporter.ExportEnd();
if (_exporterShouldRun)
{
_result = totalEntries > 0 ? DialogResult.Ok : DialogResult.Retry;
}
}
}
catch (Exception ex)
{
Application.Instance.Invoke(() =>
{
MessageBox.Show(this, $"Failed to export data.\n\n{ex.Message}", "devtime error", MessageBoxButtons.OK, MessageBoxType.Error);
});
}
Application.Instance.AsyncInvoke(() =>
{
Close(_result);
});
}
}
}