-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtoucan.i
357 lines (331 loc) · 10.9 KB
/
toucan.i
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
%module toucan
%include "typemaps.i"
%{
#include <wx/datetime.h>
#include <wx/event.h>
#include "toucan.h"
#include "rules.h"
#include "path.h"
#include "fileops.h"
#include "filecounter.h"
#include "basicfunctions.h"
#include "data/syncdata.h"
#include "data/backupdata.h"
#include "data/securedata.h"
#include "sync/syncjob.h"
#include "backup/backupjob.h"
#include "secure/securejob.h"
void Sync(SyncData *data){
if(data->GetSource().GetFullPath() == wxEmptyString || !data->GetSource().IsDir()){
throw std::invalid_argument("The source path is invalid");
}
if(data->GetDest().GetFullPath() == wxEmptyString || !data->GetDest().IsDir()){
throw std::invalid_argument("The destination path is invalid");
}
if(data->GetFunction() == wxEmptyString){
throw std::invalid_argument("A valid function must be selected");
}
if(!wxDirExists(Path::Normalise(data->GetSource().GetFullPath()))){
throw std::invalid_argument("The source must exist");
}
FileCounter counter;
if(data->GetFunction() == _("Mirror")){
counter.AddPath(data->GetDest().GetFullPath());
}
else if(data->GetFunction() == _("Equalise")){
counter.AddPath(data->GetSource().GetFullPath());
counter.AddPath(data->GetDest().GetFullPath());
}
else{
counter.AddPath(data->GetSource().GetFullPath());
}
counter.Count();
int count = counter.GetCount();
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_PROGRESSSETUP);
event->SetInt(count);
wxGetApp().QueueEvent(event);
SyncJob *job = new SyncJob(data);
job->Create();
job->Run();
job->Wait();
}
void Sync(const wxString &source, const wxString &dest, const wxString &function,
SyncChecks checks = SyncChecks(), SyncOptions options = SyncOptions(),
const wxString &rules = wxEmptyString)
{
SyncData *data = new SyncData(wxT("LastSyncJob"));
data->SetSource(wxFileName::DirName(source));
data->SetDest(wxFileName::DirName(dest));
data->SetFunction(function);
data->SetCheckSize(checks.Size);
data->SetCheckTime(checks.Time);
data->SetCheckShort(checks.Short);
data->SetCheckFull(checks.Full);
data->SetIgnoreRO(options.IgnoreRO);
data->SetAttributes(options.Attributes);
data->SetTimeStamps(options.TimeStamps);
data->SetRecycle(options.Recycle);
data->SetPreviewChanges(options.PreviewChanges);
data->SetNoSkipped(options.NoSkipped);
RuleSet *ruleset = new RuleSet(rules);
ruleset->TransferFromFile();
data->SetRules(ruleset);
try{
Sync(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
void Sync(const wxString &jobname){
SyncData *data = new SyncData(jobname);
try{
data->TransferFromFile();
Sync(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
void Backup(BackupData *data){
if(data->GetLocations().Count() == 0){
throw std::invalid_argument("You must select some paths to backup");
}
if(data->GetFunction() == wxEmptyString){
throw std::invalid_argument("A valid function must be selected");
}
if(data->GetFunction() != _("Differential") && data->GetFunction() != _("Restore") &&
(data->GetFileLocation() == wxEmptyString || wxDirExists(data->GetFileLocation()))){
throw std::invalid_argument("The backup archive file must be specified");
}
else if((data->GetFunction() == _("Differential") || data->GetFunction() == _("Restore")) &&
(data->GetFileLocation() == wxEmptyString || !wxDirExists(data->GetFileLocation()))){
throw std::invalid_argument("The backup folder must be specified and exist");
}
if(data->GetFormat() == wxEmptyString){
throw std::invalid_argument("A valid format must be selected");
}
FileCounter counter;
counter.AddPaths(data->GetLocations());
counter.Count();
int count = counter.GetCount();
if(data->GetTest()){
count = count * 2;
}
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_PROGRESSSETUP);
event->SetInt(count);
wxGetApp().QueueEvent(event);
if(data->GetUsesPassword()){
if(wxGetApp().m_Password == wxEmptyString){
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_GETPASSWORD);
int id = wxDateTime::Now().GetTicks();
event->SetInt(id);
wxGetApp().QueueEvent(event);
while(wxGetApp().m_StatusMap[id] != true){
wxMilliSleep(100);
}
}
data->SetPassword(wxGetApp().m_Password);
}
BackupJob *job = new BackupJob(data);
job->Create();
job->Run();
job->Wait();
}
void Backup(const wxString &jobname){
BackupData *data = new BackupData(jobname);
try{
data->TransferFromFile();
Backup(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
void Backup(const wxArrayString &paths, const wxString &backuplocation, const wxString &function,
const wxString &format, int compressionlevel = 3,
BackupOptions options = BackupOptions(), const wxString &rules = wxEmptyString){
BackupData *data = new BackupData(wxT("LastBackupJob"));
data->SetLocations(paths);
data->SetFileLocation(backuplocation);
data->SetFunction(function);
data->SetFormat(format);
data->SetRatio(compressionlevel);
data->SetUsesPassword(options.Password);
data->SetTest(options.Test);
data->SetSolid(options.Solid);
RuleSet *ruleset = new RuleSet(rules);
ruleset->TransferFromFile();
data->SetRules(ruleset);
try{
Backup(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
void Secure(SecureData *data){
if(data->GetLocations().Count() == 0){
throw std::invalid_argument("You must select some paths to secure");
}
if(data->GetFunction() == wxEmptyString){
throw std::invalid_argument("A valid function must be selected");
}
FileCounter counter;
counter.AddPaths(data->GetLocations());
counter.Count();
int count = counter.GetCount();
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_PROGRESSSETUP);
event->SetInt(count);
wxGetApp().QueueEvent(event);
if(wxGetApp().m_Password == wxEmptyString){
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_GETPASSWORD);
int id = wxDateTime::Now().GetTicks();
event->SetInt(id);
wxGetApp().QueueEvent(event);
while(wxGetApp().m_StatusMap[id] != true){
wxMilliSleep(100);
}
data->SetPassword(wxGetApp().m_Password);
}
SecureJob *job = new SecureJob(data);
job->Create();
job->Run();
job->Wait();
}
void Secure(const wxString &jobname){
SecureData *data = new SecureData(jobname);
try{
data->TransferFromFile();
Secure(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
void Secure(const wxArrayString &paths, const wxString &function, const wxString &rules = wxEmptyString){
SecureData *data = new SecureData(wxT("LastSecureJob"));
data->SetLocations(paths);
data->SetFunction(function);
RuleSet *ruleset = new RuleSet(rules);
ruleset->TransferFromFile();
data->SetRules(ruleset);
try{
Secure(data);
}
catch(std::exception &arg){
OutputProgress(arg.what(), Error);
}
}
//Helper functions
wxString ExpandVariable(const wxString &variable){
return Path::Normalise(variable);
}
bool Delete(const wxString &path){
wxString normpath = Path::Normalise(path);
if(File::Delete(normpath, false, false)){
OutputProgress(_("Deleted ") + normpath, Message);
}
else{
OutputProgress(_("Failed to delete ") + normpath, Error);
return false;
}
return true;
}
bool Copy(const wxString &source, const wxString &dest){
wxString normsource = Path::Normalise(source);
wxString normdest = Path::Normalise(dest);
if(File::Copy(normsource, normdest)){
OutputProgress(_("Copied ") + normsource, Message);
}
else{
OutputProgress(_("Failed to copy ") + normsource, Error);
return false;
}
return true;
}
bool Move(const wxString &source, const wxString &dest){
wxString normsource = Path::Normalise(source);
wxString normdest = Path::Normalise(dest);
if(File::Rename(normsource, normdest, true)){
OutputProgress(_("Moved") + normsource, Message);
}
else{
OutputProgress(_("Failed to move ") + normsource, Error);
return false;
}
return true;
}
bool Rename(const wxString &source, const wxString &dest){
wxString normsource = Path::Normalise(source);
wxString normdest = Path::Normalise(dest);
if(File::Rename(normsource, normdest, true)){
OutputProgress(_("Moved") + normsource, Message);
}
else{
OutputProgress(_("Failed to move ") + normsource, Error);
return false;
}
return true;
}
int Execute(const wxString &path, bool async = false){
wxString normpath = Path::Normalise(path);
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_PROCESS);
int id = wxDateTime::Now().GetTicks();
event->SetInt(id);
event->SetString(normpath);
wxGetApp().QueueEvent(event);
if(!async){
while(wxGetApp().m_StatusMap[id] != true){
wxMilliSleep(100);
}
}
return wxGetApp().m_ProcessStatusMap[id];
}
wxString GetScriptPath(const wxString &name){
wxString path = wxGetApp().GetSettingsPath() + "scripts" + wxFILE_SEP_PATH + name + ".lua";
if(wxFileExists(path)){
return path;
}
else{
return wxEmptyString;
}
}
void InputPassword(){
wxCommandEvent *event = new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED, ID_GETPASSWORD);
int id = wxDateTime::Now().GetTicks();
event->SetInt(id);
wxGetApp().QueueEvent(event);
while(wxGetApp().m_StatusMap[id] != true){
wxMilliSleep(100);
}
}
bool Shutdown(){
return wxShutdown();
}
%}
void Sync(const wxString &jobname);
void Sync(const wxString &source, const wxString &dest, const wxString &function,
SyncChecks checks = SyncChecks(), SyncOptions options = SyncOptions(),
const wxString &rules = wxEmptyString);
void Backup(const wxString &jobname);
void Backup(const wxArrayString &paths, const wxString &backuplocation, const wxString &function,
const wxString &format, int compressionlevel = 3,
BackupOptions options = BackupOptions(), const wxString &rules = wxEmptyString);
void Secure(const wxString &jobname);
void Secure(const wxArrayString &paths, const wxString &function, const wxString &rules = wxEmptyString);
wxString ExpandVariable(const wxString &variable);
wxString GetScriptPath(const wxString &name);
bool Delete(const wxString &path);
bool Copy(const wxString &source, const wxString &dest);
bool Move(const wxString &source, const wxString &dest);
bool Rename(const wxString &source, const wxString &dest);
int Execute(const wxString &path, bool async = false);
bool Shutdown();
void InputPassword();
// We want to get all enums and only OutputProgess function from basicfunctions.h.
// This is to avoid repeating declarations here, but not pull unnecessary stuff at the same time.
%rename("$ignore", %$not %$isenum, %$not %$isenumitem, notmatch$name="OutputProgress") "";
%include "basicfunctions.h";
// Poor man's "rename disable". Know a better way? Patches are welcome!
%rename("%s") "";