[go: up one dir, main page]

Menu

[14870f]: / script.cpp  Maximize  Restore  History

Download this file

301 lines (280 with data), 8.5 kB

  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
/////////////////////////////////////////////////////////////////////////////////
// Author: Steven Lamerton
// Copyright: Copyright (C) 2007-2009 Steven Lamerton
// License: GNU GPL 2 (See readme for more info)
/////////////////////////////////////////////////////////////////////////////////
#include <wx/arrstr.h>
#include <wx/tokenzr.h>
#include <wx/fileconf.h>
#include <wx/listctrl.h>
#include <wx/msgdlg.h>
#include "job.h"
#include "toucan.h"
#include "script.h"
#include "variables.h"
#include "basicfunctions.h"
#include "filecounter.h"
#include "data/jobdata.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"
#include "controls/loglistctrl.h"
#include "forms/frmmain.h"
#include "forms/frmprogress.h"
bool ScriptManager::Execute(){
StartUp();
if(!Validate()){
CleanUp();
return false;
}
if(wxGetApp().GetUsesGUI()){
ProgressBarSetup();
}
if(GetCount() != 0){
SetCommand(1);
ParseCommand(0);
}
return true;
}
bool ScriptManager::StartUp(){
//Set up all of the form related stuff
// m_ProgressWindow = wxGetApp().ProgressWindow;
// m_ProgressWindow->m_List->DeleteAllItems();
wxGetApp().MainWindow->m_Notebook->Disable();
//Send all errors to the list control
// LogListCtrl* logList = new LogListCtrl(m_ProgressWindow->m_List);
// delete wxLog::SetActiveTarget(logList);
//Set up the buttons on the progress box
// m_ProgressWindow->m_OK->Enable(false);
// m_ProgressWindow->m_Save->Enable(false);
// m_ProgressWindow->m_Cancel->Enable(true);
//Send a blank item to get the item count up
// OutputBlank();
m_Time = wxDateTime::Now();
OutputProgress(_("Starting"), true);
OutputProgress(wxEmptyString);
SetGaugeValue(0);
//Show the window
if(wxGetApp().GetUsesGUI()){
m_ProgressWindow->Refresh();
m_ProgressWindow->Update();
m_ProgressWindow->Show();
}
m_ProgressWindow->m_List->SetColumnWidth(1, m_ProgressWindow->m_List->GetClientSize().GetWidth() - m_ProgressWindow->m_List->GetColumnWidth(0));
return true;
}
bool ScriptManager::Validate(){
//Check the script to see if it is valid, check number of parameters and job name
bool valid = true;
for(unsigned int i = 0; i < m_Script.Count(); i++){
//Split the script line up into tokens, quote mark limited
wxStringTokenizer tkz(m_Script.Item(i), wxT("\""), wxTOKEN_STRTOK);
wxString token = tkz.GetNextToken();
token.Trim();
if(token == wxT("Sync") || token == wxT("Secure") || token == _("Delete") || token == _("Execute") || token == wxT("Backup")){
if(tkz.CountTokens() != 1){
OutputProgress(wxString::Format(_("Line %d has an incorrect number of parameters"), i+1));
valid = false;
}
//We have the correct number of parameters, check the job names
if(token == wxT("Sync")){
wxString job = tkz.GetNextToken();
SyncData data(job);
if(data.TransferFromFile()){
//We dont yet need to do anything special for Sync
;
}
else{
OutputProgress(wxString::Format(job + _(" not recognised on line %d"), i+1));
valid = false;
}
}
else if(token == wxT("Backup")){
wxString job = tkz.GetNextToken();
BackupData data(job);
if(data.TransferFromFile()){
if(data.GetUsesPassword()){
wxString pass = InputPassword();
if(pass == wxEmptyString){
valid = false;
}
else{
m_Password = pass;
}
}
}
else{
OutputProgress(wxString::Format(job + _(" not recognised on line %d"), i+1));
valid = false;
}
}
else if(token == wxT("Secure")){
wxString job = tkz.GetNextToken();
SecureData data(job);
if(data.TransferFromFile()){
wxString pass = InputPassword();
if(pass == wxEmptyString){
valid = false;
}
else{
m_Password = pass;
}
}
else{
OutputProgress(wxString::Format(job + _(" not recognised on line %d"), i+1));
valid = false;
}
}
}
else if(token == wxT("Move") || token == wxT("Copy") || token == wxT("Rename")){
if(tkz.CountTokens() != 3){
OutputProgress(wxString::Format(_("Line %d has an incorrect number of parameters"), i+1));
valid = false;
}
}
else{
OutputProgress(wxString::Format(token + _(" not recognised on line %d"), i+1));
valid = false;
}
}
return valid;
}
bool ScriptManager::ProgressBarSetup(){
OutputProgress(_("Setting up progress bar"));
long count = 0;
FileCounter counter;
for(unsigned int i = 0; i < GetScript().GetCount(); i++){
wxString strLine = m_Script.Item(i);
wxStringTokenizer tkz(strLine, wxT("\""), wxTOKEN_STRTOK);
wxString strToken = tkz.GetNextToken();
strToken.Trim();
if(strToken == wxT("Sync")){
strToken = tkz.GetNextToken();
SyncData data(strToken);
data.TransferFromFile();
if(data.GetFunction() != wxT("Clean")){
counter.AddPath(data.GetSource());
}
//Add both paths for mirror and equalise as they look at both sides
if(data.GetFunction() == _("Equalise") || data.GetFunction() == _("Mirror") || data.GetFunction() == _("Clean")){
counter.AddPath(data.GetDest());
}
}
else if(strToken == wxT("Backup")){
strToken = tkz.GetNextToken();
BackupData data(strToken);
data.TransferFromFile();
if(data.GetFunction() != _("Restore")){
counter.AddPaths(data.GetLocations());
}
//Add an extra three for the message 7zip sends
count += (3 * data.GetLocations().Count());
}
else if(strToken == wxT("Secure")){
strToken = tkz.GetNextToken();
SecureData data(strToken);
data.TransferFromFile();
counter.AddPaths(data.GetLocations());
}
else if(strToken == wxT("Delete")){
count++;
}
else if(strToken == wxT("Move")){
count++;
}
else if(strToken == wxT("Copy")){
count++;
}
else if(strToken == wxT("Rename")){
count++;
}
else if(strToken == wxT("Execute")){
count++;
}
}
counter.Count();
count += counter.GetCount();
SetGaugeValue(0);
SetGaugeRange(count);
return true;
}
bool ScriptManager::ParseCommand(int i){
/*if(wxGetApp().GetAbort()){
return false;
}
wxDateTime now = wxDateTime::Now();
wxString strLine = m_Script.Item(i);
wxStringTokenizer tkz(strLine, wxT("\""), wxTOKEN_STRTOK);
wxString strToken = tkz.GetNextToken();
strToken.Trim();
JobData* data;
Job* job;
if(strToken == wxT("Sync")){
data = new SyncData(tkz.GetNextToken());
SyncData* syncdata = static_cast <SyncData*> (data);
job = new SyncJob(syncdata);
}
else if(strToken == wxT("Backup")){
data = new BackupData(tkz.GetNextToken());
BackupData* backupdata = static_cast <BackupData*> (data);
job = new BackupJob(backupdata);
if(backupdata->GetUsesPassword()){
backupdata->SetPassword(GetPassword());
}
}
else if(strToken == wxT("Secure")){
data = new SecureData(tkz.GetNextToken());
SecureData* securedata = static_cast <SecureData*> (data);
job = new SecureJob(securedata);
securedata->SetPassword(GetPassword());
}
else{
return false;
}
if(!data->TransferFromFile()){
CleanUp();
}
if(!data->IsReady()){
wxMessageBox(_("Not all of the required fields are filled"), _("Error"), wxICON_ERROR);
CleanUp();
}
if(!job->Execute()){
CleanUp();
}*/
return true;
}
bool ScriptManager::CleanUp(){
/*m_ProgressWindow->m_OK->Enable(true);
m_ProgressWindow->m_Save->Enable(true);
m_ProgressWindow->m_Cancel->Enable(false);
wxDateTime now = wxDateTime::Now();
if(wxGetApp().ProgressWindow->m_Gauge->IsEnabled()){
SetGaugeValue(wxGetApp().ProgressWindow->m_Gauge->GetRange());
}
OutputBlank();
OutputProgress(_("Elapsed"), now.Subtract(m_Time).Format());
OutputProgress(_("Finished"), now.FormatTime());*/
//Yield here to make sure all output is shown
wxGetApp().Yield();
//Resize the second column to show all of the text
//m_ProgressWindow->m_List->SetColumnWidth(1, -1);
//Remove finished jobs
if (wxGetApp().m_Jobs_Config->HasGroup(wxT("LastSyncJob"))){
wxGetApp().m_Jobs_Config->DeleteGroup(wxT("LastSyncJob"));
}
if (wxGetApp().m_Jobs_Config->HasGroup(wxT("LastBackupJob"))){
wxGetApp().m_Jobs_Config->DeleteGroup(wxT("LastBackupJob"));
}
if (wxGetApp().m_Jobs_Config->HasGroup(wxT("LastSecureJob"))){
wxGetApp().m_Jobs_Config->DeleteGroup(wxT("LastSecureJob"));
}
if (wxGetApp().m_Jobs_Config->HasGroup(wxT("LastRestoreJob"))){
wxGetApp().m_Jobs_Config->DeleteGroup(wxT("LastRestoreJob"));
}
wxGetApp().m_Jobs_Config->Flush();
wxGetApp().MainWindow->m_Notebook->Enable();
return true;
}