-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
406 lines (377 loc) · 15 KB
/
MainWindow.xaml.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Shapes;
namespace FileManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
using FileManager.Entities;
public partial class MainWindow : Window
{
private List<Panel> panels = new List<Panel>();
static int numOfPanels = 0;
private ListView ActiveListView;
public MainWindow()
{
InitializeComponent();
AddNewPanel();
AddNewPanel();
ActiveListView = panels[0].PanelsListView;
}
private GridViewColumn AddGridViewColumn(string header, string binding)
{
GridViewColumn column = new GridViewColumn();
column.Header = header;
column.DisplayMemberBinding = new Binding(binding);
column.Width = 100;
return column;
}
public Panel AddNewPanel()
{
ListView newLV = new ListView();
ComboBox newCB = new ComboBox();
panels.Add(new Panel(newCB, newLV));
newLV.Style = Resources["PanelListView"] as Style;
newLV.ItemContainerStyle = Resources["PanelListViewItem"] as Style; ;
GridView columns = new GridView();
columns.Columns.Add(AddGridViewColumn( "Name", "Name"));
columns.Columns.Add(AddGridViewColumn( "Type", "Extension"));
columns.Columns.Add(AddGridViewColumn( "Size", "Length"));
columns.Columns.Add(AddGridViewColumn( "Date of creation", "CreationTime"));
newLV.View = columns;
newLV.Loaded += PanelInitialized;
newCB.Style = Resources["DrivesComboBox"] as Style;
ColumnDefinition newColumn = new ColumnDefinition();
newColumn.Width = new GridLength(1, GridUnitType.Star);
PanelsGrid.ColumnDefinitions.Add(newColumn);
newLV.SetValue(Grid.RowProperty, 1);
newLV.SetValue(Grid.ColumnProperty, numOfPanels);
newCB.SetValue(Grid.RowProperty, 0);
newCB.SetValue(Grid.ColumnProperty, numOfPanels);
PanelsGrid.Children.Add(newLV);
PanelsGrid.Children.Add(newCB);
AddDrivesInComboBox(newCB);
newCB.SelectionChanged += DiskChanged;
return panels[numOfPanels++];
}
public void DeletePanel()
{
DeletePanelAt(numOfPanels);
}
public void DeletePanelAt(int index)
{
if (numOfPanels > 2)
{
PanelsGrid.ColumnDefinitions.RemoveAt(index - 1);
PanelsGrid.Children.Remove(panels[index - 1].PanelsComboBox);
PanelsGrid.Children.Remove(panels[index - 1].PanelsListView);
panels.RemoveAt(index - 1);
--numOfPanels;
}
else MessageBox.Show("Must be at least 2 panels!");
}
public void NotifyObserves(string message)
{
if (message == "Copy" || message == "Move" || message == "New" || message == "Rename" || message == "Delete")
{
DirectoryInfo changedDirectory = GetActivePanel().GetCurrentDirectory();
foreach (var x in panels)
if (x.IsChanged(changedDirectory)) x.Update();
}
if (message == "Copy" || message == "Move" || message == "New" || message == "Init" || message == "Change" || message == "Split" || message == "Restore")
GetActivePanel().Update();
}
public Panel GetActivePanel()
{
foreach (var x in panels)
if (x.PanelsListView.Equals(ActiveListView))
return x;
return null;
}
public void SetActivePanel(Panel panel)
{
ActiveListView = panel.PanelsListView;
}
public Panel GetConnectedPanel(ListView item)
{
foreach (var x in panels)
if (x.PanelsListView.Equals(item))
return x;
return null;
}
public Panel GetConnectedPanel(ComboBox item)
{
foreach (var x in panels)
if (x.PanelsComboBox.Equals(item))
return x;
return null;
}
private void AddDrivesInComboBox(ComboBox sender)
{
int i = 0;
ComboBox comboBox = sender as ComboBox;
DriveInfo[] allDrives = FileSystem.GetAllDrives();
foreach (var x in allDrives)
{
comboBox.Items.Add(new ComboBoxItem());
comboBox.Items[i++] = x.ToString();
}
comboBox.SelectionChanged -= DiskChanged;
comboBox.SelectedIndex = GetConnectedPanel(comboBox).GetCurrentDriveID();
comboBox.SelectionChanged += DiskChanged;
}
private void PanelInitialized(object sender, RoutedEventArgs e)
{
SetActivePanel(GetConnectedPanel(sender as ListView));
NotifyObserves("Init");
}
private void ItemHandled(object sender, MouseButtonEventArgs a)
{
ListViewItem child = sender as ListViewItem;
Panel x = GetActivePanel();
Item item = (Item)child.Content;
if (item.IsReadable)
{
if ((item.Directory != null))
{
x.ChangeDirectory(item.Directory);
NotifyObserves("Change");
}
else if (item.File != null)
FileOpen(item.File);
}
else MessageBox.Show("You haven't enough access rights to do so!");
}
private static void FileOpen(FileInfo file)
{
var editor = new TextViewer(file);
editor.ShowDialog();
}
private void PanelChanged(object sender, RoutedEventArgs e)
{
ActiveListView = sender as ListView;
}
public void DiskChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox child = sender as ComboBox;
string driveName = child.SelectedValue.ToString();
Panel panel = GetConnectedPanel(child);
SetActivePanel(panel);
panel.ChangeDrive(FileSystem.GetDrive(driveName));
NotifyObserves("Change");
}
private List<Item> GetSelectedExceptActive()
{
List<Item> result = new List<Item>();
foreach (var x in panels)
if (!x.Equals(GetActivePanel()))
{
result.AddRange(x.GetSelectedItems());
}
return result;
}
private List<Item> GetSelected()
{
List<Item> result = new List<Item>();
foreach (var x in panels)
result.AddRange(x.GetSelectedItems());
return result;
}
private Item GetOneSelected()
{
List<Item> selected = GetSelected();
return selected[0];
}
public void SplitFile(object sender, RoutedEventArgs e)
{
Item file = GetOneSelected();
if (file.File == null) return;
SplitFile dialog = new SplitFile();
dialog.ShowDialog();
if (dialog.Size > 0)
{
FileSystem.Split(File.ReadAllText(file.File.FullName, Encoding.UTF8), dialog.Size, file);
NotifyObserves("Split");
}
}
private bool Agreement(string operation, List<Item> list, string to)
{
string message;
message = "Do you really want to " + operation;
foreach (Item x in list)
message = message + " " + x.Name +
" (creation time: " + x.CreationTime.ToString() + ", size: " + x.Length.ToString() + "b) ";
if (operation != "delete")
message = message + " to " + GetActivePanel().GetCurrentDirectory().FullName + "?";
else message = message + "?";
MessageBoxButton button = MessageBoxButton.OKCancel;
MessageBoxImage icon = MessageBoxImage.Question;
MessageBoxResult result = MessageBox.Show(message, operation, button, icon);
if (result == MessageBoxResult.OK) return true;
else return false;
}
private void OnClickCopy(object sender, RoutedEventArgs e)
{
if (Agreement("copy", GetSelectedExceptActive(), GetActivePanel().GetCurrentDirectory().FullName))
{
FileSystem.Copy(GetSelectedExceptActive(), GetActivePanel().GetCurrentDirectory());
NotifyObserves("Copy");
}
}
private void OnClickMove(object sender, RoutedEventArgs e)
{
if (Agreement("move", GetSelectedExceptActive(), GetActivePanel().GetCurrentDirectory().FullName))
{
FileSystem.Move(GetSelectedExceptActive(), GetActivePanel().GetCurrentDirectory());
NotifyObserves("Move");
}
}
private void OnClickDelete(object sender, RoutedEventArgs e)
{
if (Agreement("delete", GetSelected(), ""))
{
FileSystem.Delete(GetSelected());
NotifyObserves("Delete");
}
}
private void OnClickNew(object sender, RoutedEventArgs e)
{
RenameDialog dialog = new RenameDialog();
var panel = GetActivePanel();
dialog.ShowDialog();
if (dialog.Name.Length + dialog.Extension.Length + panel.GetCurrentDirectory().FullName.Length >= FileSystem.MaxPathLength)
{
MessageBox.Show("A full path cannot be longer than " + FileSystem.MaxPathLength + " symbols");
return;
}
else if (dialog.Name != "")
{
FileSystem.New(panel.GetCurrentDirectory(), dialog.Type, dialog.Name, dialog.Extension);
NotifyObserves("New");
}
}
private void OnClickRename(object sender, RoutedEventArgs e)
{
ItemType type;
List<Item> items = GetSelected();
foreach (var x in items)
{
if (x.Directory != null) type = ItemType.Directory;
else type = ItemType.File;
RenameDialog dialog = new RenameDialog(type, x.Name, x.Extension);
dialog.ShowDialog();
if ((x.Directory != null && dialog.Name.Length + x.Directory.Parent.FullName.Length >= FileSystem.MaxPathLength)
|| (x.File != null && dialog.Name.Length + dialog.Extension.Length + x.File.Directory.FullName.Length >= FileSystem.MaxPathLength))
{
MessageBox.Show("A full path cannot be longer than " + FileSystem.MaxPathLength + " symbols");
return;
}
if ((dialog.Name != x.Name)
|| (type == ItemType.File && dialog.Extension != x.Extension))
{
if (type == ItemType.Directory)
{
string correctName = dialog.Name;
int dotOrSpace = correctName.Length - 1;
while (correctName[dotOrSpace] == '.' || correctName[dotOrSpace] == ' ')
--dotOrSpace;
if (dotOrSpace != correctName.Length - 1) correctName = correctName.Remove(dotOrSpace + 1);
if (correctName != x.Name) FileSystem.Rename(x.Directory, correctName);
}
else FileSystem.Rename(x.File, dialog.Name, dialog.Extension);
NotifyObserves("Rename");
}
}
}
private List<DirectoryInfo> CreateListOfCurrentDirecrtories()
{
List<DirectoryInfo> result = new List<DirectoryInfo>();
foreach (Panel x in panels)
result.Add(x.GetCurrentDirectory());
return result;
}
private void OnClickSave(object sender, RoutedEventArgs e)
{
CreateSaveConfigFile.Create(numOfPanels, CreateListOfCurrentDirecrtories());
}
private void OnClickLoadByDom(object sender, RoutedEventArgs e)
{
List<DirectoryInfo> tempList = ReadXMLConfigFileByDom.ReadXMLSaveConfigFile();
LoadByList(tempList);
}
private void OnClickLoadBySax(object sender, RoutedEventArgs e)
{
List<DirectoryInfo> tempList = ReadXMLConfigFileBySax.ReadXMLSaveConfigFile();
LoadByList(tempList);
}
private void OnClickLoadByLinq(object sender, RoutedEventArgs e)
{
List<DirectoryInfo> tempList = ReadXMLConfigFileByLinq.ReadXMLSaveConfigFile();
LoadByList(tempList);
}
private void LoadByList(List<DirectoryInfo> list)
{
List<DirectoryInfo> tempList = list;
while (numOfPanels < tempList.Count)
AddNewPanel();
int panelsNum = 0;
foreach (DirectoryInfo dir in tempList)
{
DriveInfo tempDrive = new DriveInfo(dir.Root.FullName);
if (panels[panelsNum].ChangeSource(dir))
{
SetActivePanel(panels[panelsNum]);
++panelsNum;
NotifyObserves("Restore");
}
else DeletePanelAt(panelsNum + 1);
}
}
private void OnClickSearch(object sender, KeyEventArgs e)
{
string required = "";
if (e.Key == Key.Enter)
{
TextBox textBox = sender as TextBox;
required = textBox.Text;
List<Item> forSearch = GetSelected();
List<Item> result = new List<Item>();
foreach(Item x in forSearch)
{
if (x.Directory != null)
result.AddRange(FileSystem.Search(x.Directory, required));
}
Panel resultPanel = AddNewPanel();
ListView resultList = resultPanel.PanelsListView;
resultList.Loaded -= PanelInitialized;
resultList.Items.Clear();
foreach (Item x in result)
{
x.Name = x.FullName;
resultList.Items.Add(x);
}
}
}
private void PanelAdded(object sender, RoutedEventArgs e)
{
AddNewPanel();
}
private void PanelDeleted(object sender, RoutedEventArgs e)
{
DeletePanel();
}
}
}