-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportEntryController.cs
More file actions
195 lines (159 loc) · 6.15 KB
/
Copy pathExportEntryController.cs
File metadata and controls
195 lines (159 loc) · 6.15 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
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
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace c2flux
{
public sealed class ExportEntryController
{
private readonly CsvExportService _csvExportService;
private readonly AppSettings _settings;
private readonly IWin32Window _owner;
private readonly Action<string> _setStatusText;
public ExportEntryController(
CsvExportService csvExportService,
AppSettings settings,
IWin32Window owner,
Action<string> setStatusText)
{
_csvExportService = csvExportService ?? throw new ArgumentNullException(nameof(csvExportService));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_owner = owner;
_setStatusText = setStatusText ?? throw new ArgumentNullException(nameof(setStatusText));
}
public void CopyEntryNameToClipboard(FileSystemEntry entry)
{
if (entry == null)
return;
string name = string.IsNullOrWhiteSpace(entry.Name)
? entry.FullPath
: entry.Name;
if (string.IsNullOrWhiteSpace(name))
return;
Clipboard.SetText(name, TextDataFormat.UnicodeText);
_setStatusText(LocalizationService.GetText("Status.ExportCopied") + name);
}
public void CopyEntryTreeTextToClipboard(FileSystemEntry rootEntry)
{
if (rootEntry == null)
return;
StringBuilder builder = new StringBuilder();
AppendTreeTextEntry(builder, rootEntry, string.Empty, true, 0, true);
string treeText = builder.ToString().TrimEnd();
if (string.IsNullOrWhiteSpace(treeText))
return;
Clipboard.SetText(treeText, TextDataFormat.UnicodeText);
_setStatusText(LocalizationService.GetText("Status.ExportCopied") + rootEntry.FullPath);
}
public void CopyEntryExportToClipboard(FileSystemEntry rootEntry)
{
if (rootEntry == null)
return;
string csvText = _csvExportService.ExportToString(new[] { rootEntry }, _settings);
if (string.IsNullOrEmpty(csvText))
return;
Clipboard.SetText(csvText, TextDataFormat.UnicodeText);
_setStatusText(LocalizationService.GetText("Status.ExportCopied") + rootEntry.FullPath);
}
public void ExportEntry(FileSystemEntry rootEntry)
{
if (rootEntry == null)
return;
DialogResult dialogResult =
AppDialogs.ShowSaveFileDialog(
_owner,
_settings,
LocalizationService.GetText("Toolbar.Export"),
_csvExportService.FileFilter,
string.Empty,
CreateExportFileName(rootEntry),
LocalizationService.GetText("Toolbar.Export"),
out string fileName);
if (dialogResult != DialogResult.OK)
return;
_csvExportService.Export(fileName, new[] { rootEntry }, _settings);
_setStatusText(LocalizationService.GetText("Status.ExportSaved") + fileName);
}
private void AppendTreeTextEntry(
StringBuilder builder,
FileSystemEntry entry,
string prefix,
bool isLast,
int level,
bool isRoot)
{
if (_settings.ExportMaxDepth.HasValue &&
level > _settings.ExportMaxDepth.Value)
{
return;
}
if (!isRoot)
{
builder.Append(prefix);
builder.Append(isLast ? "└─ " : "├─ ");
}
string name = string.IsNullOrWhiteSpace(entry.Name)
? entry.FullPath
: entry.Name;
builder.Append(name);
builder.Append(" [");
builder.Append(FormatSize(entry.SizeBytes));
builder.AppendLine("]");
string childPrefix = isRoot
? string.Empty
: prefix + (isLast ? " " : "│ ");
for (int index = 0; index < entry.Children.Count; index++)
{
AppendTreeTextEntry(
builder,
entry.Children[index],
childPrefix,
index == entry.Children.Count - 1,
level + 1,
false);
}
}
private static string FormatSize(long sizeBytes)
{
const double kilobyte = 1024D;
const double megabyte = kilobyte * 1024D;
const double gigabyte = megabyte * 1024D;
const double terabyte = gigabyte * 1024D;
if (sizeBytes >= terabyte)
{
return (sizeBytes / terabyte).ToString(
"0.##",
CultureInfo.InvariantCulture) + " TB";
}
if (sizeBytes >= gigabyte)
{
return (sizeBytes / gigabyte).ToString(
"0.##",
CultureInfo.InvariantCulture) + " GB";
}
if (sizeBytes >= megabyte)
{
return (sizeBytes / megabyte).ToString(
"0.##",
CultureInfo.InvariantCulture) + " MB";
}
if (sizeBytes >= kilobyte)
{
return (sizeBytes / kilobyte).ToString(
"0.##",
CultureInfo.InvariantCulture) + " KB";
}
return sizeBytes.ToString(CultureInfo.InvariantCulture) + " B";
}
private string CreateExportFileName(FileSystemEntry entry)
{
string name = string.IsNullOrWhiteSpace(entry.Name) ? "wtf-scan" : entry.Name;
foreach (char invalidFileNameChar in Path.GetInvalidFileNameChars())
{
name = name.Replace(invalidFileNameChar, '_');
}
return name + ".csv";
}
}
}