-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundJobsModule.cs
144 lines (125 loc) · 5.33 KB
/
BackgroundJobsModule.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BackgroundJobsLib;
using BackgroundJobsLib.Interop;
using FarNet;
namespace BackgroundJobs
{
/// <summary>
/// Provides Far manager interface for <see cref="IFileOperation"/>.
/// </summary>
[Guid("4C8259BE-A3F0-4EC0-90B8-F4699652FB42")]
[ModuleTool(Name = "Background Jobs", Options = ModuleToolOptions.Panels)]
public class BackgroundJobsModule : ModuleTool
{
private static List<FileOperationParams> _queue = new List<FileOperationParams>();
/// <summary>
/// Handles background jobs menu item call in panels menu.
/// </summary>
public override void Invoke(object sender, ModuleToolEventArgs e)
{
if (null == Far.Api.Panel)
{
Far.Api.Message(Strings.ErrorNoPanel,
Strings.ErrorHeader,
MessageOptions.Error | MessageOptions.Ok);
return;
}
if (null == Far.Api.Panel.SelectedFiles)
{
Far.Api.Message(Strings.ErrorNoFilesSelected,
Strings.ErrorHeader,
MessageOptions.Error | MessageOptions.Ok);
return;
}
ShowOperationsMenu();
}
private static void ShowOperationsMenu()
{
var operationsMenu = Far.Api.CreateMenu();
operationsMenu.Add(Strings.MenuCopy, CopyHandle);
operationsMenu.Add(Strings.MenuMove, MoveHandle);
operationsMenu.Add(Strings.MenuDelete, DeleteHandle);
operationsMenu.Add(string.Empty).IsSeparator = true;
operationsMenu.Add(Strings.MenuExecute, ExecuteHandle);
operationsMenu.Show();
}
private static void CopyHandle(object sender, MenuEventArgs args)
{
IPanel activePanel = Far.Api.Panel;
string sourceDir = activePanel.CurrentDirectory;
string destDir = Far.Api.Panel2.CurrentDirectory;
foreach (var file in activePanel.SelectedFiles)
{
string sourceFullName = Path.Combine(sourceDir, file.Name);
_queue.Add(new FileOperationParams(sourceFullName,
destDir,
file.Name,
FileOperationType.Copy));
}
Task.Factory.StartNew(WorkerThreadProc);
}
private static void MoveHandle(object sender, EventArgs args)
{
IPanel activePanel = Far.Api.Panel;
string sourceDir = activePanel.CurrentDirectory;
string destDir = Far.Api.Panel2.CurrentDirectory;
foreach (var file in activePanel.SelectedFiles)
{
string sourceFullName = Path.Combine(sourceDir, file.Name);
_queue.Add(new FileOperationParams(sourceFullName,
destDir,
file.Name,
FileOperationType.Move));
}
Task.Factory.StartNew(WorkerThreadProc);
}
private static void DeleteHandle(object sender, EventArgs args)
{
IPanel activePanel = Far.Api.Panel;
string sourceDir = activePanel.CurrentDirectory;
foreach (var file in activePanel.SelectedFiles)
{
string sourceFullName = Path.Combine(sourceDir, file.Name);
_queue.Add(new FileOperationParams(sourceFullName,
null,
null,
FileOperationType.Delete));
}
Task.Factory.StartNew(WorkerThreadProc);
}
private static void ExecuteHandle(object sender, EventArgs args)
{
Task.Factory.StartNew(WorkerThreadProc);
}
private static void WorkerThreadProc()
{
FileOperation fileOperation = new FileOperation();
foreach (var operationSettings in _queue)
{
switch (operationSettings.OperationType)
{
case FileOperationType.Copy:
fileOperation.CopyItem(operationSettings.Source,
operationSettings.Destination,
operationSettings.NewName);
break;
case FileOperationType.Move:
fileOperation.MoveItem(operationSettings.Source,
operationSettings.Destination,
operationSettings.NewName);
break;
case FileOperationType.Delete:
fileOperation.DeleteItem(operationSettings.Source);
break;
}
}
_queue.Clear();
fileOperation.PerformOperations();
fileOperation.Dispose();
}
}
}