-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMainWindow-Workers.cs
200 lines (180 loc) · 6.74 KB
/
MainWindow-Workers.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
/* The File Commander main window
* Background workers
* (C) The File Commander Team - https://github.com/atauenis/fcmd
* (C) 2013-14, Alexander Tauenis (atauenis@yandex.ru)
* (C) 2014, Evgeny Akhtimirov (wilbit@me.com)
* Contributors should place own signs here.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using pluginner;
using pluginner.Toolkit;
namespace fcmd
{
partial class MainWindow
{
/* ЗАМЕТКА РАЗРАБОТЧИКУ
*
* В данном файле размещаются функции для работы с файлами и каталогами.
* Данные функции запускаются в отдельных от UI потоках из кода
* файла MainWindow-Actions.cs . Всякая функция должна иметь
* префикс Do, означающий её чисто утилитарную принадлежность.
*
* Вызовы пользовательского интерфейса (XWT) должны производиться через
* вызывалку Xwt.Application.Invoke(new Action(delegate { КОД ПОТОКА ИНТЕРФЕЙСА }));
* в противном случае возможны глюки (вылеты WPF, зависания и лаги GTK).
*/
/// <summary>
/// Background file copier
/// </summary>
/// <param name='SourceFS'>Source FS.</param>
/// <param name='DestinationFS'>Destination FS.</param>
/// <param name='SourceURL'>Source file URL.</param>
/// <param name='DestinationURL'>Destination URL.</param>
/// <param name="Feedback">If at the destination URL a file exists, a ReplaceQuestionDialog will be shown. This argument is the place, where the user's last choose should be saved.</param>
/// <param name="AC">The instance of AsyncCopy class that should be used to copy the file.</param>
private void DoCp(IFSPlugin SourceFS, IFSPlugin DestinationFS, string SourceURL, string DestinationURL, ref ReplaceQuestionDialog.ClickedButton Feedback, AsyncCopy AC)
{
if (SourceURL == DestinationURL){
string itself = Localizator.GetString("CantCopySelf");
string toshow = string.Format(Localizator.GetString("CantCopy"), SourceFS.GetMetadata(SourceURL).Name, itself);
Xwt.Application.Invoke(delegate { Xwt.MessageDialog.ShowWarning(toshow); });
//calling the msgbox in non-main threads causes some UI bugs, so push this call into main thread
return;
}
if(DestinationFS.FileExists (DestinationURL)){
ReplaceQuestionDialog rpd = null;
bool ready = false;
Xwt.Application.Invoke(
delegate
{
rpd = new ReplaceQuestionDialog(DestinationFS.GetMetadata(DestinationURL).Name);
ready = true;
}
);
do { } while (!ready);
ready = false;
var ClickedButton = ReplaceQuestionDialog.ClickedButton.Skip;
Xwt.Application.Invoke(
delegate
{
ClickedButton = rpd.Run();
ready = true;
}
);
do {} while (!ready);
switch(ClickedButton){
case ReplaceQuestionDialog.ClickedButton.Replace:
//continue execution
Feedback = rpd.ChoosedButton;
break;
case ReplaceQuestionDialog.ClickedButton.ReplaceAll:
//continue execution
Feedback = rpd.ChoosedButton;
break;
case ReplaceQuestionDialog.ClickedButton.ReplaceOld:
Feedback = rpd.ChoosedButton;
if(SourceFS.GetMetadata(SourceURL).LastWriteTimeUTC < DestinationFS.GetMetadata(DestinationURL).LastWriteTimeUTC)
{/*continue execution*/}
else
{return;}
break;
case ReplaceQuestionDialog.ClickedButton.Skip:
Feedback = rpd.ChoosedButton;
return;
case ReplaceQuestionDialog.ClickedButton.SkipAll:
Feedback = rpd.ChoosedButton;
return;
}
}
try
{
pluginner.FSEntryMetadata md = SourceFS.GetMetadata(SourceURL);
md.FullURL = DestinationURL;
System.IO.Stream SrcStream = SourceFS.GetFileStream(SourceURL);
DestinationFS.Touch(md);
System.IO.Stream DestStream = DestinationFS.GetFileStream(DestinationURL,true);
if(AC == null) AC = new AsyncCopy();
bool CpComplete = false;
AC.OnComplete += result => CpComplete = true;
//warning: due to some GTK# bugs, buffer sizes lesser than 128KB may cause
//an StackOverflowException at UI update code
AC.CopyFile(SrcStream, DestStream, 131072); //buffer is 1/8 megabyte or 128KB
do{ } while(!CpComplete); //don't stop this thread until the copy is finished
}
catch (Exception ex)
{
if (ex.GetType() != typeof(System.Threading.ThreadAbortException)) {
Utilities.ShowMessage(string.Format(Localizator.GetString("CantCopy"),SourceURL,ex.Message));
Console.WriteLine("Cannot copy because of {0}({1}) at \n{2}.", ex.GetType(), ex.Message, ex.StackTrace);
}
}
}
/// <summary>
/// Copy the entrie directory
/// </summary>
private void DoCpDir(string source, string destination, pluginner.IFSPlugin fsa, pluginner.IFSPlugin fsb)
{
if (!fsb.DirectoryExists(destination)) { fsb.CreateDirectory(destination); }
fsb.CurrentDirectory = destination;
foreach (DirItem di in fsa.DirectoryContent)
{
if (di.TextToShow == "..")
{ /* don't touch the link to the parent directory */}
else if (!di.IsDirectory)
{
//it is file
string s1 = di.URL; //source url
FSEntryMetadata md1 = fsa.GetMetadata(s1);
string s2 = destination + fsb.DirSeparator + md1.Name; //destination url
ReplaceQuestionDialog.ClickedButton Placeholder = ReplaceQuestionDialog.ClickedButton.Cancel;
DoCp(fsa, fsb, s1, s2, ref Placeholder, new AsyncCopy());
}
else if (di.IsDirectory)
{
//it is subdirectory
DoCpDir(di.URL, destination + fsb.DirSeparator + di.TextToShow, fsa,fsb);
}
}
}
/// <summary>
/// Background file remove
/// </summary>
/// <param name="url">url of the file</param>
/// <param name="fs">filesystem of the file</param>
private void DoRmFile(string url, pluginner.IFSPlugin fs)
{
try
{
fs.DeleteFile(url);
}
catch (Exception err)
{
Utilities.ShowError(err.Message, null);
}
}
/// <summary>
/// Background directory remove
/// </summary>
/// <param name="url">url of the file</param>
/// <param name="fs">filesystem of the file</param>
private void DoRmDir(string url, pluginner.IFSPlugin fs)
{
try
{
fs.DeleteDirectory(url, true);
}
catch (pluginner.ThisDirCannotBeRemovedException)
{
Utilities.ShowWarning(string.Format(Localizator.GetString("DirCantBeRemoved")),url);
}
catch (Exception err)
{
Utilities.ShowError(err.Message);
}
}
}
}