forked from atauenis/fcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow-Actions.cs
321 lines (276 loc) · 14.5 KB
/
MainWindow-Actions.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
/* The File Commander main window
* File managing operations
* (C) The File Commander Team - https://github.com/atauenis/fcmd
* (C) 2013-14, Alexander Tauenis (atauenis@yandex.ru)
* Contributors should place own signs here.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace fcmd
{
partial class MainWindow
{
/* ЗАМЕТКА РАЗРАБОТЧИКУ
*
* В данном файле размещаются подпрограммы для управления файлами, которые
* вызываются из MainWindow.cs. Также планируется использование этих подпрограмм
* после реализации текстовой коммандной строки FC (которая внизу окна).
* Все комманды работают с активной и пассивой панелью - Active/PassivePanel.
* FC всегда их определяет сам. Пассивая панель - всегда получатель файлов.
* Названия комманд - UNIX в верблюжьем регистре (Ls, Rm, MkDir, Touch и т.п.).
* Всем коммандам параметры передаются строкой, но допускаются исключения, напр.,
* если базовая функция "перегружена" функцией для нужд графического интерфейса.
* Sorry for my bad english.
*/
/// <summary>
/// Reads the file <paramref name="url"/> and shows in FC Viewer
/// </summary>
/// <param name="url"></param>
public void FCView(string url)
{
VEd fcv = new VEd();
pluginner.IFSPlugin fs = ActivePanel.FS;
if (!fs.FileExists(url))
{
//MessageBox.Show(string.Format(locale.GetString("FileNotFound"), ActivePanel.list.SelectedItems[0].Text), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Xwt.MessageDialog.ShowError(string.Format(Locale.GetString("FileNotFound"),ActivePanel.GetValue(ActivePanel.dfDisplayName)));
return;
}
pluginner.File SelectedFile = fs.GetFile(url, new int());
string FileContent = Encoding.ASCII.GetString(fs.GetFileContent(url));
fcv.LoadFile(url, ActivePanel.FS, false);
fcv.Show();
}
/// <summary>
/// Reads the file <paramref name="url"/> and returns it as string
/// (for File Commander's CLI panel)
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string Cat(string url)
{
pluginner.IFSPlugin fs = ActivePanel.FS;
if (!fs.FileExists(url)) return "Файл не найден\n";
return Encoding.ASCII.GetString(fs.GetFileContent(url));
}
/// <summary>
/// Makes a new directory at the specifed <paramref name="url"/>
/// </summary>
/// <param name="url"></param>
public void MkDir(string url)
{
FileProcessDialog fpd = new FileProcessDialog();
fpd.lblStatus.Text = string.Format(Locale.GetString("DoingMkdir"), "\n" + url, null);
fpd.Show();
Thread MkDirThread = new Thread(delegate() { ActivePanel.FS.CreateDirectory(url); });
MkDirThread.Start();
do { /*Application.DoEvents();*/ Xwt.Application.MainLoop.DispatchPendingEvents(); }
while (MkDirThread.ThreadState == ThreadState.Running);
fpd.pbrProgress.Fraction = 1;
ActivePanel.LoadDir(ActivePanel.FS.CurrentDirectory);
fpd.Hide();
}
/// <summary>Removes the current selected files</summary>
public void Rm()
{
if (ActivePanel.GetValue(ActivePanel.dfDisplayName) == "..") { return; }
pluginner.ListView2Item[] chdrws = ActivePanel.ListingView.ChoosedRows.ToArray();//because the List may change due the process, we getting the copy of the list (as array, but how else?)
foreach (pluginner.ListView2Item selitem in chdrws)
{
string URL = selitem.Data[ActivePanel.dfURL].ToString();
Rm(URL);
}
ActivePanel.LoadDir();
}
/// <summary>
/// Removes the specifed file
/// </summary>
public string Rm(string url)
{
if (ActivePanel.GetValue<string>(ActivePanel.dfDisplayName) == "..") { return "Cannot remove .."; }
if (!Xwt.MessageDialog.Confirm(
String.Format(Locale.GetString("FCDelAsk"), url, null),
Xwt.Command.Remove,
true))
{ return Locale.GetString("Canceled"); };
FileProcessDialog fpd = new FileProcessDialog();
fpd.lblStatus.Text = String.Format(Locale.GetString("DoingRemove"), "\n" + url, null);
fpd.cmdCancel.Sensitive = false;
fpd.Show();
string curItemDel = ActivePanel.GetValue<string>(ActivePanel.dfURL);
pluginner.IFSPlugin fsdel = ActivePanel.FS;
if (fsdel.FileExists(curItemDel))
{
fpd.pbrProgress.Fraction = 0.5;
Thread RmFileThread = new Thread(delegate() { DoRmFile(curItemDel, fsdel); });
RmFileThread.Start();
do { Xwt.Application.MainLoop.DispatchPendingEvents(); }
while (RmFileThread.ThreadState == ThreadState.Running);
fpd.pbrProgress.Fraction = 1;
fpd.Hide();
return "Файл удалён.\n";
}
if (fsdel.DirectoryExists(curItemDel))
{
fpd.lblStatus.Text = String.Format(Locale.GetString("DoingRemove"), "\n" + url, "\n[" + Locale.GetString("Directory").ToUpper() + "]");
fpd.pbrProgress.Fraction = 0.5;
Thread RmDirThread = new Thread(delegate() { DoRmDir(curItemDel, fsdel); });
RmDirThread.Start();
do { Xwt.Application.MainLoop.DispatchPendingEvents(); }
while (RmDirThread.ThreadState == ThreadState.Running);
fpd.pbrProgress.Fraction = 1;
fpd.Hide();
return "Каталог удалён.\n";
}
return "Файл не найден";
}
/// <summary>
/// Copy the highlighted file to the passive panel. To be used in FC UI.
/// Includes asking of the destination path.
/// </summary>
[STAThread]
public void Cp()
{
if (ActivePanel.GetValue<string>(ActivePanel.dfDisplayName) == "..") { return; }
foreach (pluginner.ListView2Item selitem in ActivePanel.ListingView.ChoosedRows)
{
int Progress = 0;
string SourceURL = selitem.Data[ActivePanel.dfURL].ToString();
pluginner.IFSPlugin SourceFS = ActivePanel.FS;
pluginner.File SourceFile = SourceFS.GetFile(SourceURL, Progress);
//check for file existing
if (SourceFS.FileExists(SourceURL))
{
InputBox ibx = new InputBox(String.Format(Locale.GetString("CopyTo"), SourceFile.Name), PassivePanel.FS.CurrentDirectory + PassivePanel.FS.DirSeparator + SourceFile.Name);
if (ibx.ShowDialog(this))
{
String DestinationFilePath = ibx.Result;
string StatusMask = Locale.GetString("DoingCopy");
ReplaceQuestionDialog.ClickedButton dummy = ReplaceQuestionDialog.ClickedButton.Cancel;
AsyncCopy AC = new AsyncCopy();
Thread CpThread = new Thread(delegate() { DoCp(ActivePanel.FS, PassivePanel.FS, SourceFile, DestinationFilePath, ref dummy, AC); });
CpThread.TrySetApartmentState(ApartmentState.STA);
FileProcessDialog fpd = new FileProcessDialog();
fpd.InitialLocation = Xwt.WindowLocation.CenterParent;
fpd.lblStatus.Text = String.Format(StatusMask, ActivePanel.GetValue<string>(ActivePanel.dfURL), ibx.Result, null);
fpd.cmdCancel.Clicked += (object s, EventArgs e) => { CpThread.Abort(); new MsgBox(Locale.GetString("Canceled"), ActivePanel.GetValue<string>(ActivePanel.dfURL), MsgBox.MsgBoxType.Warning); };
AC.ReportMessage = Locale.GetString("CopyStatus");
AC.OnProgress += (msg, proc) =>
{
Xwt.Application.Invoke(
delegate
{
try
{
fpd.pbrProgress.Fraction = (double)((double)proc / (double)100);
fpd.lblStatus.Text = String.Format(StatusMask, ActivePanel.GetValue<string>(ActivePanel.dfURL), ibx.Result, msg);
}
catch { }
}
);
};
fpd.Show();
CpThread.Start();
do
{
Xwt.Application.MainLoop.DispatchPendingEvents();
}
while (CpThread.ThreadState == ThreadState.Running);
//todo: замер и показ скорости, пауза, запрос отмены, вывод в фоновый поток (кнопка "в фоне").
fpd.Hide();
}
continue;
}
//not a file...maybe directory?
if (SourceFS.DirectoryExists(SourceURL))//а вдруг есть такой каталог?
{
InputBox ibxd = new InputBox(String.Format(Locale.GetString("CopyTo"), SourceFile.Name), PassivePanel.FS.CurrentDirectory + "/" + SourceFile.Name);
if (ibxd.ShowDialog())
{
String DestinationDirPath = ibxd.Result;
//копирование каталога
Thread CpDirThread = new Thread(delegate() { DoCpDir(SourceURL, DestinationDirPath, ActivePanel.FS, PassivePanel.FS); });
CpDirThread.TrySetApartmentState(ApartmentState.STA);
FileProcessDialog CpDirProgressDialog = new FileProcessDialog();
CpDirProgressDialog.InitialLocation = Xwt.WindowLocation.CenterParent;
CpDirProgressDialog.lblStatus.Text = String.Format(Locale.GetString("DoingCopy"), "\n" + ActivePanel.GetValue<string>(ActivePanel.dfURL) + " [" + Locale.GetString("Directory") + "]\n", ibxd.Result, null);
CpDirProgressDialog.cmdCancel.Clicked += (object s, EventArgs e) => { CpDirThread.Abort(); new MsgBox(Locale.GetString("Canceled"), ActivePanel.GetValue<string>(ActivePanel.dfURL), MsgBox.MsgBoxType.Warning); };
CpDirProgressDialog.Show();
CpDirThread.Start();
do { Xwt.Application.MainLoop.DispatchPendingEvents(); }
while (CpDirThread.ThreadState == ThreadState.Running);
//LoadDir(PassivePanel.FSProvider.CurrentDirectory, PassivePanel); //обновление пассивной панели
PassivePanel.LoadDir();
CpDirProgressDialog.Hide();
}
continue;
}
//and, if none of those IF blocks has been entered, say that this isn't a real file nor a directory
Xwt.MessageDialog.ShowWarning(
Locale.GetString("FileNotFound"),
ActivePanel.GetValue<string>(
ActivePanel.dfURL
)
);
continue;
}
}
/// <summary>
/// Move the selected file or directory
/// </summary>
void Mv()
{
if (ActivePanel.GetValue<string>(ActivePanel.dfDisplayName) == "..") { return; }
pluginner.IFSPlugin SourceFS = ActivePanel.FS;
pluginner.IFSPlugin DestinationFS = PassivePanel.FS;
foreach (pluginner.ListView2Item selitem in ActivePanel.ListingView.ChoosedRows)
{
//Getting useful URL parts
string SourceName = selitem.Data[ActivePanel.dfDisplayName].ToString(); //ActivePanel.GetValue<string>(ActivePanel.dfDisplayName);
string SourcePath = selitem.Data[ActivePanel.dfURL].ToString(); //ActivePanel.GetValue<string>(ActivePanel.dfURL);
string DestinationPath = DestinationFS.CurrentDirectory + DestinationFS.DirSeparator + SourceName;
InputBox ibx = new InputBox
(
string.Format(Locale.GetString("MoveTo"), SourceName),
DestinationPath
);
if (ibx.ShowDialog())
DestinationPath = ibx.Result;
else return;
// Comparing the filesystems; if they is diffrent, use copy&delete
// instead of direct move (if anyone knows, how a file can be moved
// from an FTP to an ext2fs on Windows or MacOS please tell me :-) )
if (SourceFS.GetType() != DestinationFS.GetType())
{
Xwt.MessageDialog.ShowError("Cannot move between diffrent filesystems!\nНе сделана поддержка перемещения между разными ФС.");
Cp();
return;
//todo
}
//Now, assuming that the src & dest fs is same and supports
//cross-disk file moving
if (SourcePath == DestinationPath)
{
string itself = Locale.GetString("CantCopySelf");
string toshow = string.Format(Locale.GetString("CantMove"), SourcePath, itself);
Xwt.Application.Invoke(new Action(delegate { Xwt.MessageDialog.ShowWarning(toshow); }));
//calling the msgbox in non-main threads causes some UI bugs, thus pushing this call into main thread
return;
}
if (SourceFS.DirectoryExists(SourcePath))
{//this is a directory
SourceFS.MoveDirectory(SourcePath, DestinationPath);
}
if (SourceFS.FileExists(SourcePath))
{//this is a file
SourceFS.MoveFile(SourcePath, DestinationPath);
}
ActivePanel.LoadDir();
PassivePanel.LoadDir();
}
}
}
}