This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
/
Controller.cs
544 lines (484 loc) · 17.7 KB
/
Controller.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using ANDREICSLIB;
using ANDREICSLIB.NewControls;
namespace FolderView
{
public class Controller
{
private const string dummyname = "zzzzzzzzdef";
public static String Clean(String s)
{
String outputpath = s;
outputpath = outputpath.Replace('\\', '-');
outputpath = outputpath.Replace('/', '-');
outputpath = outputpath.Replace(':', '-');
outputpath = outputpath.Replace('*', '-');
outputpath = outputpath.Replace('?', '-');
outputpath = outputpath.Replace('\"', '-');
outputpath = outputpath.Replace('<', '-');
outputpath = outputpath.Replace('>', '-');
outputpath = outputpath.Replace('|', '-');
return outputpath;
}
public static void RemoveDoubles(ref String input, char charact)
{
int len = -1;
do
{
len = input.Length;
input = input.Replace("--", "-");
} while (len != input.Length);
}
public static string GetFilePath(string outputdir)
{
var OFD = new OpenFileDialog();
OFD.Title = "Select a folder structure file to view";
OFD.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + outputdir;
DialogResult DR = OFD.ShowDialog();
if (DR == DialogResult.Cancel)
return null;
return OFD.FileName;
}
public static void GetRatings(TreeListView outputtree, TreeListView mainTV)
{
if (outputtree.SelectedItems.Count == 0)
return;
//first clear all sizes for selected items
foreach (TreeListViewItem TLVI in outputtree.SelectedItems)
{
var TLVI2 = treelistviewfuncs.getItemByName(mainTV, TLVI.Name);
if (TLVI2 == null)
continue;
TLVI2.rating = treelistviewfuncs.getRating(TLVI);
//if (TLVI2.rating>-1)
//TLVI2.SubItems[3].Text = TLVI2.rating.ToString();
}
}
public static void ClearSelections(List<TreeNode> selectedNodes)
{
try
{
retry:
foreach (TreeNode tn in selectedNodes)
{
if (tn.Checked)
{
tn.Checked = false;
goto retry;
}
}
}
finally
{
selectedNodes.Clear();
}
}
public static void Refreshtree(System.Windows.Forms.TreeView multiselecttree, TreeNode TX = null)
{
if (multiselecttree.Nodes.Count == 0)
{
multiselecttree.Nodes.Clear();
foreach (String s in Directory.GetLogicalDrives())
{
TreeNode TN = multiselecttree.Nodes.Add(s);
//dummy item
try
{
if (Directory.GetDirectories(TN.Text).Count() > 0)
TN.Nodes.Add(dummyname).Name = dummyname;
}
catch
{
continue;
}
}
}
else
{
foreach (TreeNode TN in multiselecttree.Nodes)
{
Expandnodes(TN, TX);
}
}
}
public static void Expandnodes(TreeNode TN, TreeNode TX = null)
{
if (TN.IsExpanded || TN == TX)
{
//Remove dummu
if (TN.Nodes[dummyname] != null)
{
TreeNode TN2 = TN.Nodes[dummyname];
TN.Nodes.Remove(TN2);
}
if (TN.Nodes.Count == 0)
{
Expandnode(TX);
}
foreach (TreeNode TN2 in TN.Nodes)
Expandnodes(TN2, TX);
}
}
public static void Expandnode(TreeNode TN)
{
try
{
foreach (String subdir in Directory.GetDirectories(TN.Text))
{
TreeNode TX2 = TN.Nodes.Add(subdir);
TX2.Name = subdir;
if (Directory.GetDirectories(subdir).Count() > 0)
TX2.Nodes.Add(dummyname).Name = dummyname;
}
}
catch
{
}
}
public static bool Errorcontinue(String error)
{
var options = new List<string> { "Yes", "No" };
var MMB = new ANDREICSLIB.NewControls.MultipleMessageBox("Continue?",
"Error:\n" + error + "\nDo you wish to continue?", options);
MMB.ShowDialog();
return MMB.Result.Equals("Yes");
}
public static bool Rem(ref TreeListView T, String FullPath)
{
for (int a = 0; a < T.Items.Count; a++)
{
if (T.Items[a].fullPath.Equals(FullPath))
{
T.Items.RemoveAt(a);
return true;
}
if (T.Items.Count > 0)
{
var newT = new TreeListViewItem();
treelistviewfuncs.CopyNode(T.Items[a], ref newT);
if (Rem(ref newT, FullPath))
{
T.Items.RemoveAt(a);
T.Items.Add(newT);
}
}
}
return false;
}
public static bool Rem(ref TreeListViewItem T, String FullPath)
{
for (int a = 0; a < T.Items.Count; a++)
{
if (T.Items[a].fullPath.Equals(FullPath))
{
T.Items.RemoveAt(a);
return true;
}
if (T.Items.Count > 0)
{
var newT = new TreeListViewItem();
treelistviewfuncs.CopyNode(T.Items[a], ref newT);
if (Rem(ref newT, FullPath))
{
T.Items.RemoveAt(a);
T.Items.Add(newT);
return true;
}
}
}
return false;
}
public static void SetImages(TreeListView TLV, bool showicon, ref ImageList imageList1)
{
foreach (TreeListViewItem TLVI in TLV.Items)
{
SetImage(TLVI, showicon, ref imageList1);
SetImages(TLVI.Items, showicon, ref imageList1);
}
}
public static void SetImages(TreeListViewItemCollection TLVIC, bool showicon, ref ImageList imageList1)
{
foreach (TreeListViewItem TLVI2 in TLVIC)
{
SetImage(TLVI2, showicon, ref imageList1);
SetImages(TLVI2.Items, showicon, ref imageList1);
}
}
public static void CopySelectedToClipboard(TreeListView outputtree)
{
String SC = "";
foreach (TreeListViewItem TLVI in outputtree.SelectedItems)
{
SC += TLVI.fullPath + Environment.NewLine;
}
if (string.IsNullOrEmpty(SC) == false)
Clipboard.SetText(SC);
else
MessageBox.Show("No items were copied - are they accessible?");
}
public static void ClearAndRecalculate(bool recursive, TreeListView outputtree, TreeListView mainTV)
{
if (outputtree.SelectedItems.Count > 0)
{
//first clear all sizes for selected items
foreach (TreeListViewItem TLVI in outputtree.SelectedItems)
{
treelistviewfuncs.clearSizes(treelistviewfuncs.getItemByName(mainTV, TLVI.Name), false);
}
//then repopulate
foreach (TreeListViewItem TLVI in outputtree.SelectedItems)
{
treelistviewfuncs.recalculateSizes(treelistviewfuncs.getItemByName(mainTV, TLVI.Name));
}
}
else
{
treelistviewfuncs.clearSizes(ref mainTV);
treelistviewfuncs.recalculateSizes(mainTV);
}
}
public static void SetImage(TreeListViewItem TLVI, bool showicon, ref ImageList imageList1)
{
if (TLVI.Visible == false)
return;
if (TLVI.isfolder)
{
if (TLVI.Items.Count >= 1 && TLVI.Items[0].Visible)
TLVI.ImageIndex = 0;
else
TLVI.ImageIndex = 1;
return;
}
if (showicon)
{
try
{
int x = ShellIcon.getSetImage(TLVI.Text, ref imageList1, false);
if (x != -1)
TLVI.ImageIndex = x;
}
catch
{
TLVI.ImageIndex = 2;
}
}
else
TLVI.ImageIndex = 2;
}
public static List<TreeListViewItem> GetDups(Form1.IncrementStatusDel incrementStatus, TreeListViewItem TLVI, bool fuzzy)
{
//get a list of all the files at this level
var ret = new List<TreeListViewItem>();
var retout = new List<TreeListViewItem>();
foreach (TreeListViewItem TLVI2 in TLVI.Items)
{
if (TLVI2.isfolder == false)
ret.Add(TLVI2);
if (TLVI2.Items.Count > 0)
{
retout.AddRange(GetDups(incrementStatus, TLVI2, fuzzy));
}
}
//go through and get the dups
foreach (TreeListViewItem R1 in ret)
{
incrementStatus(true);
foreach (TreeListViewItem R2 in ret)
{
if (R1 == R2)
continue;
String r11 = R1.Text;
GetString(ref r11);
String r22 = R2.Text;
GetString(ref r22);
if ((r11.Equals(r22) && fuzzy) || R1.Text.Equals(R2.Text) && fuzzy == false)
{
if (retout.Contains(R1) == false)
{
retout.Add(R1);
incrementStatus();
}
if (retout.Contains(R2) == false)
retout.Add(R2);
}
}
}
return retout;
}
public static void GetString(ref String i)
{
i = Clean(i);
i = i.Replace('.', '-');
i = i.ToLower();
String o = "";
foreach (char c in i)
{
if ((c < 'a' || c > 'z') && c != '-')
break;
else if (c != '-')
o = o + c;
}
i = o;
}
public static List<TreeListViewItem> GetMatches(TreeListViewItem TLVI, string livesearchtext)
{
//get a list of all the files at this level
var ret = new List<TreeListViewItem>();
var retout = new List<TreeListViewItem>();
foreach (TreeListViewItem TLVI2 in TLVI.Items)
{
if (TLVI2.isfolder == false)
ret.Add(TLVI2);
if (TLVI2.Items.Count > 0)
{
ret.AddRange(GetMatches(TLVI2, livesearchtext));
}
}
//go through and get the dups
foreach (TreeListViewItem node in ret)
{
String regx = "";
if (node.Text.StartsWith("^") == false)
regx = ".*";
String x = livesearchtext;
x = x.Replace(@"\", @"\\");
regx += x;
if (node.Text.EndsWith("$"))
regx += ".*";
var r = new Regex(regx, RegexOptions.IgnoreCase);
bool match = r.IsMatch(node.Text);
if (match)
{
retout.Add(node);
}
}
return retout;
}
public static void AddNode(TreeListViewItem TLVI, ref TreeListView sendto)
{
var parenttree = new List<TreeListViewItem>();
TreeListViewItem curr = TLVI;
while (curr != null)
{
parenttree.Add(curr);
curr = curr.Parent;
}
//initial folder
TreeListViewItem pop = parenttree[parenttree.Count - 1];
TreeListViewItem createin = treelistviewfuncs.getItemByText(sendto, pop.Text);
if (createin == null)
{
var create = new TreeListViewItem();
treelistviewfuncs.BaseCopy(pop, ref create);
sendto.Items.Add(create);
createin = treelistviewfuncs.getItemByText(sendto, pop.Text);
}
parenttree.RemoveAt(parenttree.Count - 1);
while (parenttree.Count > 0)
{
pop = parenttree[parenttree.Count - 1];
TreeListViewItem pop2 = treelistviewfuncs.getItemByText(createin, pop.Text);
if (pop2 == null)
{
var create = new TreeListViewItem();
treelistviewfuncs.BaseCopy(pop, ref create);
createin.Items.Add(create);
createin = treelistviewfuncs.getItemByText(createin, pop.Text);
}
else
createin = pop2;
parenttree.RemoveAt(parenttree.Count - 1);
}
}
public static String TimeFileName(String basestr, string fileext)
{
String t = DateTime.Now.ToString();
return basestr + t + "." + fileext;
}
public static void CollapseTLVI(TreeListView TLV)
{
TLV.CollapseAll();
}
public static void ExpandTLVI(TreeListView TLV)
{
TLV.ExpandAll();
}
public static void DisplayTreeListViewFile(Form1.IncrementStatusDel incrementStatus, TreeListView TV, String path)
{
TV.Items.Clear();
try
{
serialisation.DeserializeTreeListView(incrementStatus, TV, path);
}
catch (Exception ex)
{
MessageBox.Show("There was an error opening the file:" + ex);
}
}
public static void UpdateFileList(System.Windows.Forms.CheckedListBox filelist, string outputdir, string fileext)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));
filelist.Items.Clear();
if (Directory.Exists(outputdir) == false && File.Exists(outputdir) == false)
Directory.CreateDirectory(outputdir);
var di = new DirectoryInfo(outputdir);
FileInfo[] rgFiles = di.GetFiles("*." + fileext);
foreach (FileInfo fi in rgFiles)
{
filelist.Items.Add(fi.Name);
}
}
public static void SavepathsF(System.Windows.Forms.CheckedListBox filelist, List<string> input, int maxDepth, ref string outputdir, string fileext, bool savefilesize)
{
foreach (String fp in input)
{
String outputpathFinal = "";
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));
if (Directory.Exists(outputdir) == false)
{
try
{
Directory.CreateDirectory(outputdir);
}
catch (Exception ex)
{
MessageBox.Show("Could not create directory:" + outputdir + "\nError:" + ex);
return;
}
}
String outputpath = Clean(fp);
RemoveDoubles(ref outputpath, '-');
String t = DateTime.Now.ToString();
t = Clean(t);
outputpath = outputpath + " " + t;
outputpath = outputpath + "." + fileext;
outputpathFinal = outputdir + "/" + outputpath;
}
catch (Exception ex)
{
MessageBox.Show("There was an error opening the file:" + ex);
return;
}
TreeListView tv = treelistviewfuncs.getDirectoryTree(fp, null, 0, maxDepth, savefilesize);
try
{
serialisation.SerializeTreeListView(tv, outputpathFinal);
}
catch (Exception ex)
{
MessageBox.Show("There was an error saving the tree to the file:" + ex);
return;
}
UpdateFileList(filelist, outputdir, fileext);
}
}
}
}