-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowContent.xaml.cs
More file actions
646 lines (566 loc) · 24.4 KB
/
Copy pathWindowContent.xaml.cs
File metadata and controls
646 lines (566 loc) · 24.4 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
using Microsoft.VisualBasic.FileIO;
using Start9.UI.Wpf.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace RibbonFileManager
{
/// <summary>
/// Interaction logic for WindowContent.xaml
/// </summary>
public partial class WindowContent : UserControl
{
public Boolean IsRenamingFiles
{
get => (Boolean)GetValue(IsRenamingFilesProperty);
set => SetValue(IsRenamingFilesProperty, value);
}
public static readonly DependencyProperty IsRenamingFilesProperty = DependencyProperty.Register("IsRenamingFiles", typeof(Boolean), typeof(WindowContent), new PropertyMetadata(false));
public MainWindow OwnerWindow
{
get => Window.GetWindow(this) as MainWindow;
}
public static event EventHandler CurrentDirectorySelectionChanged;
CancellationTokenSource _source;
public String CurrentFolderTitle
{
get
{
try
{
return CurrentLocation.Name;
}
catch
{
return String.Empty;
}
}
}
public NavigationStack<Location> NavigationStack { get; set; } = new NavigationStack<Location>();
public RecentLocationsList<Location> RecentLocations { get; set; } = new RecentLocationsList<Location>();
public IEnumerable<Location> HistoryElements => RecentLocations.Reverse();
public Location CurrentLocation
{
get => NavigationStack.Current;
}
public String CurrentDisplayName
{
get => (String)GetValue(CurrentDisplayNameProperty);
set => SetValue(CurrentDisplayNameProperty, value);
}
public static readonly DependencyProperty CurrentDisplayNameProperty = DependencyProperty.Register(nameof(CurrentDisplayName), typeof(String), typeof(WindowContent), new PropertyMetadata(String.Empty));
public Double IconSize
{
get => (Double)GetValue(IconSizeProperty);
set => SetValue(IconSizeProperty, value);
}
public static readonly DependencyProperty IconSizeProperty = DependencyProperty.Register("IconSize", typeof(Double), typeof(WindowContent), new PropertyMetadata((Double)48.0));
public Boolean ShowDetailsPane
{
get => (Boolean)GetValue(ShowDetailsPaneProperty);
set => SetValue(ShowDetailsPaneProperty, value);
}
public static readonly DependencyProperty ShowDetailsPaneProperty = DependencyProperty.Register("ShowDetailsPane", typeof(Boolean), typeof(WindowContent), new PropertyMetadata(false));
public Boolean ShowPreviewPane
{
get => (Boolean)GetValue(ShowPreviewPaneProperty);
set => SetValue(ShowPreviewPaneProperty, value);
}
public static readonly DependencyProperty ShowPreviewPaneProperty = DependencyProperty.Register("ShowPreviewPane", typeof(Boolean), typeof(WindowContent), new PropertyMetadata(false));
public Boolean ShowNavigationPane
{
get => (Boolean)GetValue(ShowNavigationPaneProperty);
set => SetValue(ShowNavigationPaneProperty, value);
}
public static readonly DependencyProperty ShowNavigationPaneProperty = DependencyProperty.Register("ShowNavigationPane", typeof(Boolean), typeof(WindowContent), new PropertyMetadata(true));
public FileBrowserView CurrentView
{
get => (FileBrowserView)GetValue(CurrentViewProperty);
set => SetValue(CurrentViewProperty, value);
}
public static readonly DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(FileBrowserView), typeof(WindowContent), new PropertyMetadata(FileBrowserView.Icons));
public Boolean ShowItemCheckboxes
{
get => (Boolean)GetValue(ShowItemCheckboxesProperty);
set => SetValue(ShowItemCheckboxesProperty, value);
}
public static readonly DependencyProperty ShowItemCheckboxesProperty = DependencyProperty.Register("ShowItemCheckboxes", typeof(Boolean), typeof(WindowContent), new PropertyMetadata(false));
//String _initPath;
Location _initLocation;
public WindowContent(String path)
{
InitializeComponent();
_initLocation = GetLocation(path);
if (NavigationStack.Count == 0)
NavigationStack.Add(_initLocation);
Loaded += WindowContent_Loaded;
}
static string _shellLocationPrefix = "shell:::";
public Location GetLocation(string path)
{
if (Directory.Exists(path))
return new DirectoryQuery(path);
else if (path.ToLowerInvariant().StartsWith(_shellLocationPrefix))
{
string guidString = path.Replace(_shellLocationPrefix, string.Empty);
return new ShellLocation(Guid.Parse(guidString));
}
else
return null;
}
private async void WindowContent_Loaded(Object sender, RoutedEventArgs e)
{
Loaded -= WindowContent_Loaded;
await NavigateAsync(_initLocation);
}
static async IAsyncEnumerable<DiskItem> GetDirectoryContents(String path, String query, CancellationToken token, Boolean recursive)
{
var entries = Directory.EnumerateFileSystemEntries(path, query, new EnumerationOptions { RecurseSubdirectories = recursive, IgnoreInaccessible = true });
var enumer = entries.GetEnumerator();
while (await Task.Run(enumer.MoveNext))
{
token.ThrowIfCancellationRequested();
yield return await Task.Run(() => new DiskItem(enumer.Current));
token.ThrowIfCancellationRequested();
}
}
static async IAsyncEnumerable<DiskItem> GetShellLocationContents(Location location, String query, CancellationToken token, Boolean recursive)
{
if ((location as ShellLocation).LocationGuid == ShellLocation.ThisPcGuid)
{
var entries = new List<string>()
{
Environment.ExpandEnvironmentVariables(@"%userprofile%\Desktop"),
Environment.ExpandEnvironmentVariables(@"%userprofile%\Documents"),
Environment.ExpandEnvironmentVariables(@"%userprofile%\Downloads"),
Environment.ExpandEnvironmentVariables(@"%userprofile%\Music"),
Environment.ExpandEnvironmentVariables(@"%userprofile%\Pictures"),
Environment.ExpandEnvironmentVariables(@"%userprofile%\Videos")
};
foreach (DriveInfo d in System.IO.DriveInfo.GetDrives())
entries.Add(d.RootDirectory.FullName);
var enumer = entries.GetEnumerator();
while (await Task.Run(enumer.MoveNext))
{
token.ThrowIfCancellationRequested();
yield return await Task.Run(() => new DiskItem(enumer.Current));
token.ThrowIfCancellationRequested();
}
}
else //We don't know what this GUID is
{
throw new Exception("Unrecognized Shell Location GUID");
}
}
public async Task RefreshAsync(Location location, CancellationToken token = default)
{
_source?.Cancel();
_source = new CancellationTokenSource();
var source = CancellationTokenSource.CreateLinkedTokenSource(_source.Token, token);
switch (location)
{
case DirectoryQuery l: await Navigate(new SearchQuery(l.Item.ItemPath, "*", false), source); break;
case SearchQuery s: await Navigate(s, source, false); break;
}
}
public async Task NavigateAsync(Location location, CancellationToken token = default)
{
NavigationStack.Add(location);
NavigationStack.Forward();
RecentLocations.Navigate(location);
await RefreshAsync(location, token);
CurrentDisplayName = location.Name;
}
async Task Navigate(SearchQuery l, CancellationTokenSource source, Boolean clearTextBox = true)
{
var old = CurrentDirectoryListView.ItemsSource;
OwnerWindow.Navigate(l);
if (clearTextBox)
OwnerWindow.SearchTextBox.Clear();
try
{
var results = new ObservableCollection<DiskItem>();
CurrentDirectoryListView.ItemsSource = results;
int nextDirectoryIndex = 0;
/*if (l is ShellLocation)
{
await foreach (var path in GetShellLocationContents(l, l.Query, source.Token, l.Recursive))
{
if (path.ItemCategory == DiskItemCategory.Directory)
{
results.Insert(nextDirectoryIndex, path);
nextDirectoryIndex++;
}
else
results.Add(path);
source.Token.ThrowIfCancellationRequested();
}
}
else
{*/
await foreach (var path in GetDirectoryContents(l.LocationPath, l.Query, source.Token, l.Recursive))
{
if (path.ItemCategory == DiskItemCategory.Directory)
{
results.Insert(nextDirectoryIndex, path);
nextDirectoryIndex++;
}
else
results.Add(path);
source.Token.ThrowIfCancellationRequested();
}
}
catch (OperationCanceledException) when (!String.IsNullOrEmpty(l.Query)) // if the user canceled a search, then preserve what's been searched
{
}
catch // else, fall back to the previous results
{
CurrentDirectoryListView.ItemsSource = old;
}
}
private async void AddressBox_KeyDown(Object sender, KeyEventArgs e)
{
var bar = sender as TextBox;
if (e.Key == Key.Enter)
{
var expText = Environment.ExpandEnvironmentVariables(bar.Text);
if (Directory.Exists(expText))
{
await NavigateAsync(new DirectoryQuery(expText));
CurrentDirectoryListView.Focus();
//NavBar_PathTextBox_LostFocus(null, null);
}
else
{
var failText = expText;
MessageBox<MessageBoxEnums.OkButton>.Show("Ribbon File Browser can't find '" + failText + "'. Check the #speeling and try again.", "File Commander"); //(this, "Ribbon File Browser can't find '" + failText + "'. Check the speeling and try again.", "Ribbon File Browser");
}
}
else if (e.Key == Key.Escape)
{
CurrentDirectoryListView.Focus();
}
}
private void RunAsAdminMenuItem_Loaded(Object sender, RoutedEventArgs e)
{
var item = (sender as MenuItem).Tag as ListViewItem;
if (((List<DiskItem>)CurrentDirectoryListView.ItemsSource)[(item.Parent as ListView).Items.IndexOf(item)].ItemCategory == DiskItemCategory.Directory)
(sender as MenuItem).Visibility = Visibility.Collapsed;
}
public void ShowPropertiesForSelection()
{
foreach (DiskItem i in CurrentDirectoryListView.SelectedItems)
{
i.ShowProperties();
}
}
private async void NavigationPaneTreeView_SelectedItemChanged(Object sender, RoutedPropertyChangedEventArgs<Object> e)
{
if (e.NewValue is DiskItem val)
await NavigateAsync(new DirectoryQuery(Environment.ExpandEnvironmentVariables(val.ItemPath)));
}
private async void CurrentDirectoryListView_KeyDown(Object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (e.Key == Key.C)
CopySelection();
else if (e.Key == Key.X)
CutSelection();
else if (e.Key == Key.V)
await PasteCurrentAsync();
}
else if (e.Key == Key.Delete)
await DeleteSelectionAsync();
}
private void CurrentDirectoryListView_SelectionChanged(Object sender, SelectionChangedEventArgs e)
{
DiskItem item = null;
if (CurrentDirectoryListView.SelectedItem != null)
item = CurrentDirectoryListView.SelectedItem as DiskItem;
OwnerWindow.ValidateCommandStates(CurrentDirectoryListView.SelectedItems.Count, item);
}
public async Task OpenSelectionAsync(DiskItem.OpenVerbs verb = DiskItem.OpenVerbs.Normal)
{
foreach (DiskItem i in CurrentDirectoryListView.SelectedItems)
{
if (i.ItemCategory == DiskItemCategory.Directory)
{
if (CurrentDirectoryListView.SelectedItems.Count == 1) //
{
await NavigateAsync(new DirectoryQuery(i.ItemPath));
break;
}
}
else
{
var info = new ProcessStartInfo()
{
FileName = i.ItemPath,
UseShellExecute = true
};
Process.Start(info);
}
}
}
public void EditSelection()
{
}
public void CopySelection()
{
Config.Cut = false;
SetClipboard();
}
public void CutSelection()
{
Config.Cut = true;
SetClipboard();
}
public void CopyPathToSelection()
{
var paths = "";
for (var i = 0; i < CurrentDirectoryListView.SelectedItems.Count; i++)
{
var d = (DiskItem)CurrentDirectoryListView.SelectedItems[i];
paths = paths + "\"" + d.ItemPath + "\"";
if (i < (CurrentDirectoryListView.SelectedItems.Count - 1))
{
paths += "\n";
}
}
Clipboard.SetText(paths);
}
public async Task PasteCurrentAsync()
{
if (!(NavigationStack.Current is DirectoryQuery curr)) return;
System.Collections.Specialized.StringCollection clipboard = null;
if (Clipboard.ContainsFileDropList())
{
clipboard = Clipboard.GetFileDropList();
foreach (string s in clipboard)
{
string outPath = Path.Combine(CurrentLocation.LocationPath, Path.GetFileName(s));
if (File.Exists(s))
{
if (s.ToLowerInvariant() == outPath.ToLowerInvariant())
{
string fixBasePath = outPath + " - Copy";
string fixedPath = fixBasePath;
int cycle = 1;
while (File.Exists(fixedPath))
{
fixedPath = fixBasePath + " (" + cycle.ToString() + ")";
cycle++;
}
FileSystem.CopyFile(s, fixedPath, UIOption.AllDialogs);
}
else if (File.Exists(outPath))
{
string fixedPath = outPath;
int cycle = 1;
while (File.Exists(fixedPath))
{
fixedPath = outPath + " (" + cycle.ToString() + ")";
cycle++;
}
FileSystem.CopyFile(s, fixedPath, UIOption.AllDialogs);
}
else
FileSystem.CopyFile(s, outPath, UIOption.AllDialogs); //File.Copy(s, outPath);
}
else if (Directory.Exists(s))
{
if (s.ToLowerInvariant() == outPath.ToLowerInvariant())
{
string fixBasePath = outPath + " - Copy";
string fixedPath = fixBasePath;
int cycle = 1;
while (Directory.Exists(fixedPath))
{
fixedPath = fixBasePath + " (" + cycle.ToString() + ")";
cycle++;
}
FileSystem.CopyDirectory(s, fixedPath, UIOption.AllDialogs);
}
else if (Directory.Exists(outPath))
{
string fixedPath = outPath;
int cycle = 1;
while (Directory.Exists(fixedPath))
{
fixedPath = outPath + " (" + cycle.ToString() + ")";
cycle++;
}
FileSystem.CopyDirectory(s, fixedPath, UIOption.AllDialogs);
}
else
FileSystem.CopyDirectory(s, outPath, UIOption.AllDialogs);
}
}
}
else
{
var items = Config.PasteIn(curr.Item.ItemPath);
var source = (ObservableCollection<DiskItem>)CurrentDirectoryListView.ItemsSource;
foreach (var d in items)
{
if (source.Contains(d))
{
source.Remove(d);
}
else
{
source.Add(d);
}
}
}
await RefreshAsync();
}
public void SetClipboard()
{
System.Collections.Specialized.StringCollection paths = new System.Collections.Specialized.StringCollection();
foreach (DiskItem d in CurrentDirectoryListView.SelectedItems)
paths.Add(d.ItemPath);
Clipboard.SetFileDropList(paths);
OwnerWindow.UpdateClipboardButtons();
/*Config.ClipboardContents.Clear();
foreach (DiskItem d in CurrentDirectoryListView.SelectedItems)
{
Config.ClipboardContents.Add(d);
}*/
}
public async Task RefreshAsync()
{
await RefreshAsync(CurrentLocation);
}
public async Task PasteShortcutAsync()
{
await RefreshAsync();
}
public async Task DeleteSelectionAsync()
{
for (var i = 0; i < CurrentDirectoryListView.SelectedItems.Count; i++)
{
var d = CurrentDirectoryListView.SelectedItems[i] as DiskItem;
if (d.ItemCategory == DiskItemCategory.Directory)
{
FileSystem.DeleteDirectory(d.ItemPath, UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
(CurrentDirectoryListView.ItemsSource as ObservableCollection<DiskItem>).Remove(d);
}
else if ((d.ItemCategory == DiskItemCategory.File) || (d.ItemCategory == DiskItemCategory.Shortcut))
{
FileSystem.DeleteFile(d.ItemPath, UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
(CurrentDirectoryListView.ItemsSource as ObservableCollection<DiskItem>).Remove(d);
}
}
//await RefreshAsync();
}
public void RenameSelection()
{
IsRenamingFiles = true;
}
public async Task CreateNewFolderAsync()
{
var path = Path.Combine(CurrentLocation.LocationPath, "New Folder");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
else
{
var cycle = 1;
while (Directory.Exists(path))
{
path = Path.Combine(CurrentLocation.LocationPath, @"New Folder (" + cycle.ToString() + ")");
MessageBox.Show(path);
cycle++;
}
Directory.CreateDirectory(path);
}
await RefreshAsync();
}
public void SelectAll()
{
CurrentDirectoryListView.SelectAll();
}
public void SelectNone()
{
CurrentDirectoryListView.SelectedItem = null;
}
public void InvertSelection()
{
foreach (ListViewItem item in CurrentDirectoryListView.Items)
item.IsSelected = !item.IsSelected;
}
private void FileManagerBase_CurrentDirectorySelectionChanged(Object sender, SelectionChangedEventArgs e)
{
CurrentDirectorySelectionChanged?.Invoke(this, null);
}
public void SetPanes(DiskItem item)
{
var size = DetailsFileIconRectangle.ActualHeight;
if (size <= 0)
size = 48;
DetailsFileIconRectangle.Fill = (ImageBrush) new Start9.UI.Wpf.Converters.IconToImageBrushConverter().Convert(item.ItemJumboIcon, null, size.ToString(), null);
DetailsFileNameTextBlock.Text =
(item.ItemCategory == DiskItemCategory.Directory) && (item.ItemDisplayName == CurrentLocation.Name)
? item.SubItems.Count.ToString() + " items"
: item.ItemDisplayName;
if (item.ItemCategory != DiskItemCategory.Directory)
{
if (item.ItemPath.ToLowerInvariant() == CurrentLocation.Name.ToLowerInvariant())
{
SetPreviewPaneLayer(0);
}
else
{
var ext = Path.GetExtension(item.ItemPath).ToLowerInvariant();
if (ext == "bmp" || ext == "png" || ext == "jpg" || ext == "jpeg")
{
(PreviewPaneGrid.Children[2] as System.Windows.Shapes.Rectangle).Fill = new ImageBrush(new BitmapImage(new Uri(item.ItemPath, UriKind.RelativeOrAbsolute)));
SetPreviewPaneLayer(2);
}
else
{
var content = File.ReadAllText(item.ItemPath);
if (content.Contains("\0\0"))
SetPreviewPaneLayer(1);
else
{
((PreviewPaneGrid.Children[4] as ScrollViewer).Content as TextBlock).Text = content;
SetPreviewPaneLayer(4);
}
}
}
}
}
void SetPreviewPaneLayer(Int32 index)
{
for (var i = 0; i < PreviewPaneGrid.Children.Count; i++)
{
var control = PreviewPaneGrid.Children[i];
control.Visibility = i == index ? Visibility.Visible : Visibility.Collapsed;
}
}
}
public enum FileBrowserView
{
Icons,
List,
Details,
Tiles,
Content
}
}