Debian Codegraph @

src/scheduleDialog.cpp

raw symbols SCIP
1 /* ---------------------------------- scheduleDialog.cpp ---------------------------------------------------------------------------
2 Display a dialog. Schedules profiles via cron
3
4 ===============================================================================================================================
5 ===============================================================================================================================
6 This file is part of "luckyBackup" project
7 Copyright, Loukas Avgeriou
8 luckyBackup is distributed under the terms of the GNU General Public License
9 luckyBackup is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 luckyBackup is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with luckyBackup. If not, see <http://www.gnu.org/licenses/>.
21
22
23 project version : Please see "main.cpp" for project version
24
25 developer : luckyb
26 last modified : 22 May 2016
27
28 ===============================================================================================================================
29 ===============================================================================================================================
30 */
31
32
33 #include "scheduleDialog.h"
34
35 #include <QTextStream>
36 #include <QTextCodec>
37 #include <QMessageBox>
38 #include <QProcess>
39
40 #include "scheduleClass.h"
41 #include "global.h"
42 #include "textDialog.h"
43
44 int const maxSchedule = 20; //The maximum number or profiles schedule permitted
45 schedule *Schedule[maxSchedule]; //The array that holds name etc of "schedule list"
46 //QFile scheduleFile (schedulefilename );
47 QProcess *cronProcess; //crontab process
48
49 // class scheduleDialog Constructor=================================================================================================
50 // schedule existing profiles to execute via cron
51 scheduleDialog::scheduleDialog (QDialog *parent)
52 : QDialog (parent)
53 , scheduleFile(schedulefilename)
54 {
55 uiS.setupUi(this);
56
57 //variables initialization
58 TotalSchedule=0; //The number of available profiles schedulesin "schedule list"- starts from 1
59 currentSchedule=0; //this holds the current row from the "schedule list"- starts from 0
60 noSchedules = true; //becomes true when no schedules are declared
61 changesMade = false; //becomes true if changes are made but no cronIt is pressed
62
63 //connections ----------------------------------------------------------------------------------------------------
64 connect(uiS.pushButton_schedule, SIGNAL( pressed() ), this, SLOT(schedulePressed() ) ); //cronIT pushButton
65 connect(uiS.pushButton_cancel, SIGNAL( pressed() ), this, SLOT(cancelPressed() ) ); //cancel pushButton
66
67 connect(uiS.pushButton_add, SIGNAL( pressed() ), this, SLOT(addPressed() ) ); //add command pushButton
68 connect(uiS.pushButton_remove, SIGNAL( pressed() ), this, SLOT(removePressed() ) ); //remove command pushButton
69 connect(uiS.pushButton_modify, SIGNAL( pressed() ), this, SLOT(modifyPressed() ) ); //modify command pushButton
70
71 connect(uiS.pushButton_viewCrontab, SIGNAL( pressed() ), this, SLOT(viewCrontab() ) ); //view curreent crontab command pushButton
72
73 connect(uiS.pushButton_modifyOkay, SIGNAL( pressed() ), this, SLOT(okaySchedulePressed() ) ); //modify okay pushButton
74 connect(uiS.pushButton_modifyCancel, SIGNAL( pressed() ), this, SLOT(modifyCancelPressed() ) ); //modify cancel pushButton
75
76 connect(uiS.listWidget_schedule , SIGNAL( currentRowChanged(int) ), this, SLOT(fillDetails() ) ); //list item selection changed
77
78
79 cronProcess = new QProcess(this); //create a new qprocess (for crontab) & connect signals
80 connect(cronProcess, SIGNAL(readyReadStandardError()), this, SLOT(appendCrontabOutput()));
81 connect(cronProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(appendCrontabOutput()));
82
83 //initialize view of widgets
84 uiS.groupBox_addProfileSchedule -> setVisible(false);
85 uiS.listWidget_schedule -> setEnabled(true);
86 uiS.pushButton_add -> setEnabled(true);
87 uiS.pushButton_modify -> setEnabled(true);
88 uiS.pushButton_remove -> setEnabled(true);
89 uiS.pushButton_cancel -> setEnabled(true);
90 uiS.pushButton_schedule -> setEnabled(true);
91
92 //profile combobox initialization
93 uiS.comboBox_profile -> clear();
94 currentProfileIndex = -1;
95 QDir profiledir(profileDir);
96 profiledir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot);
97 profiledir.setSorting(QDir::Name | QDir::LocaleAware);
98 QStringList profileNames = profiledir.entryList(QStringList("*.profile"));
99 QString profilename="";
100 for (count = 0; count < profileNames.size(); ++count)
101 {
102 profilename = profileNames[count];
103 if (currentProfile == profileDir + profilename) // set the currentProfileIndex to the index of the current profile
104 currentProfileIndex = count;
105 profilename.chop(8);
106 uiS.comboBox_profile -> addItem (profilename);
107 }
108 if (currentProfileIndex == -1) // if no default profile exists
109 currentProfileIndex = 0;
110
111 //open the schedule.dat file and fill arrays & schedule list
112 if (!scheduleFile.open(QIODevice::ReadOnly)) //if there is an error opening it, delete it to create a new one
113 scheduleFile.remove();
114 else
115 {
116 QTextStream in(&scheduleFile);
117
118 QString ScheduleLine=""; //temp variable to import the profile line by line
119 ScheduleLine = in.readLine();
120
121 // First check if the profile is a text or data stream
122 // if data, use the qvariant style
123 if (ScheduleLine !="***************************** WARNING *****************************")
124 {
125 //in.reset();
126 scheduleFile.close(); scheduleFile.open(QIODevice::ReadOnly); //close & reopen the file
127
128 // Try to read the schedulefile in the old data/qvariant format (not a text)
129 // This will probably become obsolete after some time
130 // Last version qvariant is used: 0.4.6
131 QDataStream inV(&scheduleFile);
132 inV.setVersion(QDataStream::Qt_4_3);
133
134 QVariant v; //we will import everything as QVariant using this temp variable
135 QString vString; //temp variable to import "labels" of real data
136 QString tempAppName = "asxeto";
137 double tempAppVersion=0;
138
139 inV>>v; if (v.toString()=="appName")
140 {inV >> v; tempAppName = v.toString();} //input the application name & version--------------------------
141 inV>>v; if (v.toString()=="appVersion")
142 {inV >> v; tempAppVersion = v.toDouble();}
143
144 if ( (tempAppName != appName) || (tempAppVersion < validScheduleVersion) )//check if the file is a valid luckybackup schedule file
145 {
146 message = tr("The existent schedule file is not a valid luckyBackup v.","v is for version")
147 + countStr.setNum(appVersion) + tr(" file","BEWARE of the whitespace before file")+"<br><br>"+tr("Starting from scratch")+"...<br>" ;
148 textDialog textdialogW ("QtWarning", message, this);
149 textdialogW.exec();
150 scheduleFile.remove();
151 return;
152 }
153
154 inV>>v; if (v.toString()=="TotalSchedules")
155 {inV >> v; TotalSchedule = v.toInt();} //input the size of the schedule list first
156
157 currentSchedule = 0; vString="";
158 while (currentSchedule < TotalSchedule)
159 {
160 schedule *tempSchedule = new schedule;
161
162 inV>>v; vString = v.toString(); inV >> v; //input a label in vString and real data in v
163
164 while (vString != "current schedule end")
165 {
166 if (vString == "ScheduleName") tempSchedule -> SetName(v.toString());
167
168 if (vString == "ProfilePath") tempSchedule -> SetProfilePath(v.toString());
169 if (vString == "ProfileName") tempSchedule -> SetProfileName(v.toString());
170
171 if (vString == "ScheduleMonth") tempSchedule -> SetMonth(v.toInt());
172 if (vString == "ScheduleMonthDay") tempSchedule -> SetMonthDay(v.toInt());
173 if (vString == "ScheduleWeekDay") tempSchedule -> SetWeekDay(v.toInt());
174 if (vString == "ScheduleHour") tempSchedule -> SetHour(v.toInt());
175 if (vString == "ScheduleMinute") tempSchedule -> SetMinute(v.toInt());
176
177 if (vString == "ScheduleSkipCritical") tempSchedule -> SetSkipCritical(v.toBool());
178 if (vString == "ScheduleConsoleMode") tempSchedule -> SetConsoleMode(v.toBool());
179 if (vString == "ScheduleAtReboot") tempSchedule -> SetReboot(v.toBool());
180 if (vString == "ScheduleAtRebootDelay") tempSchedule -> SetDelayReboot(v.toInt());
181
182 inV>>v; vString = v.toString();
183 if (vString!="current schedule end")
184 inV >> v;
185 else
186 Schedule[currentSchedule] = tempSchedule;
187
188 }
189 uiS.listWidget_schedule -> addItem(scheduleText(Schedule[currentSchedule] -> GetProfileName(),
190 Schedule[currentSchedule] -> GetMonth(),
191 Schedule[currentSchedule] -> GetMonthDay(),
192 Schedule[currentSchedule] -> GetWeekDay(),
193 Schedule[currentSchedule] -> GetHour(),
194 Schedule[currentSchedule] -> GetMinute(),
195 Schedule[currentSchedule] -> GetReboot() ));
196 currentSchedule++;
197 }
198 saveScheduleFile(); //re-save the scheduleFile as text
199 }
200
201 // This is the normal text schedule file used from version 0.4.7 and after
202 else
203 {
204 QString tempAppName = "asxeto";
205 profileDescription = "";
206 double tempAppVersion=0;
207 bool IntOk;
208
209 // Read all lines until the first task line or end of file if invalid
210 while ( !(ScheduleLine.startsWith("[Schedule]")) && (!in.atEnd()) )
211 {
212 ScheduleLine = in.readLine();
213
214 //input the application name & version--------------------------
215 if (ScheduleLine.startsWith("appName=")) tempAppName = ScheduleLine.remove("appName=");
216 if (ScheduleLine.startsWith("appVersion=")) tempAppVersion = (ScheduleLine.remove("appVersion=")).toDouble(&IntOk);
217
218 //input the number of schedules
219 if (ScheduleLine.startsWith("TotalSchedules=")) TotalSchedule = (ScheduleLine.remove("TotalSchedules=")).toInt(&IntOk,10);
220 }
221
222 if ( (tempAppName != appName) || (tempAppVersion < validScheduleVersion) )//check if the file is a valid luckybackup schedule file
223 {
224 message = tr("The existent schedule file is not a valid luckyBackup v.","v is for version")
225 + countStr.setNum(appVersion) + tr(" file","BEWARE of the whitespace before file")+"<br><br>"+tr("Starting from scratch")+"...<br>" ;
226 textDialog textdialogW ("QtWarning", message, this);
227 textdialogW.exec();
228 scheduleFile.remove();
229 return;
230 }
231
232 currentSchedule = 0; ScheduleLine="";
233 while (currentSchedule < TotalSchedule)
234 {
235 schedule *tempSchedule = new schedule;
236 ScheduleLine = in.readLine(); // inport the next line
237 while ( (!ScheduleLine.startsWith("[Schedule_end]")) && (!in.atEnd()) )
238 {
239 if (ScheduleLine.startsWith("ScheduleName=")) tempSchedule -> SetName(ScheduleLine.remove("ScheduleName="));
240
241 if (ScheduleLine.startsWith("ProfilePath=")) tempSchedule -> SetProfilePath(ScheduleLine.remove("ProfilePath="));
242 if (ScheduleLine.startsWith("ProfileName=")) tempSchedule -> SetProfileName(ScheduleLine.remove("ProfileName="));
243
244 if (ScheduleLine.startsWith("ScheduleMonth=")) tempSchedule -> SetMonth(ScheduleLine.remove("ScheduleMonth=").toInt(&IntOk,10));
245 if (ScheduleLine.startsWith("ScheduleMonthDay=")) tempSchedule -> SetMonthDay(ScheduleLine.remove("ScheduleMonthDay=").toInt(&IntOk,10));
246 if (ScheduleLine.startsWith("ScheduleWeekDay=")) tempSchedule -> SetWeekDay(ScheduleLine.remove("ScheduleWeekDay=").toInt(&IntOk,10));
247 if (ScheduleLine.startsWith("ScheduleHour=")) tempSchedule -> SetHour(ScheduleLine.remove("ScheduleHour=").toInt(&IntOk,10));
248 if (ScheduleLine.startsWith("ScheduleMinute=")) tempSchedule -> SetMinute(ScheduleLine.remove("ScheduleMinute=").toInt(&IntOk,10));
249
250 if (ScheduleLine.startsWith("ScheduleSkipCritical=")) tempSchedule -> SetSkipCritical(ScheduleLine.remove("ScheduleSkipCritical=").toInt(&IntOk,10));
251 if (ScheduleLine.startsWith("ScheduleConsoleMode=")) tempSchedule -> SetConsoleMode(ScheduleLine.remove("ScheduleConsoleMode=").toInt(&IntOk,10));
252 if (ScheduleLine.startsWith("ScheduleAtReboot=")) tempSchedule -> SetReboot(ScheduleLine.remove("ScheduleAtReboot=").toInt(&IntOk,10));
253 if (ScheduleLine.startsWith("ScheduleAtRebootDelay=")) tempSchedule -> SetDelayReboot(ScheduleLine.remove("ScheduleAtRebootDelay=").toInt(&IntOk,10));
254
255 ScheduleLine = in.readLine(); // import the next line
256 if (ScheduleLine.startsWith("[Schedule_end]")) // all properties of the specific schedule have been loaded
257 Schedule[currentSchedule] = tempSchedule;
258 }
259 uiS.listWidget_schedule -> addItem(scheduleText(Schedule[currentSchedule] -> GetProfileName(),
260 Schedule[currentSchedule] -> GetMonth(),
261 Schedule[currentSchedule] -> GetMonthDay(),
262 Schedule[currentSchedule] -> GetWeekDay(),
263 Schedule[currentSchedule] -> GetHour(),
264 Schedule[currentSchedule] -> GetMinute(),
265 Schedule[currentSchedule] -> GetReboot() ));
266
267 currentSchedule++;
268 }
269 }
270 //TotalSchedule = uiS.listWidget_schedule -> count(); //Get the schedule list size
271
272 if (TotalSchedule > 0)
273 {
274 uiS.listWidget_schedule -> setCurrentRow(0);
275 noSchedules = false;
276 }
277 else
278 uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
279
280 scheduleFile.close(); //close the file
281 }
282
283 // use this cause when the dialog starts and ther's only 1 schedule the filldetails cannot be called !!
284 currentSchedule = uiS.listWidget_schedule -> currentRow(); //current operations list row number
285 fillDetails();
286 }
287 //===================================================================================================================================================
288 //------------------------------------------------------------------SLOTS----------------------------------------------------------------------------
289 //===================================================================================================================================================
290
291
292 // CLOSE button pressed=====================================================================================================
293 void scheduleDialog::cancelPressed()
294 {
295 close(); //emmit a QcloseEvent
296 }
297
298 //window closebutton pressed or QcloseEvent emitted==========================================================================
299 void scheduleDialog::closeEvent(QCloseEvent *event)
300 {
301 if (changesMade)
302 {
303 textDialog textdialogQ ("QtQuestion", tr("You have made changes without clicking the cronIT button") + ".<br>" +
304 tr("Are you sure you want to close the schedule dialog ?"), this);
305 textdialogQ.exec();
306
307 if (textdialogQ.getGoOn() > 0) //if user answers yes
308 event->accept();
309 else
310 event->ignore();
311 }
312 else
313 event->accept();
314 }
315
316
317 // schedule button pressed=======================================================================================================
318 // Read all fields of dialog, fill appropriate variables, saves crontab file and creates a cron job
319 void scheduleDialog::schedulePressed()
320 {
321 if (noSchedules) // if no schedules are declared, remove the info line first
322 uiS.listWidget_schedule -> takeItem (0);
323
324 TotalSchedule = uiS.listWidget_schedule -> count(); //Get the schedule list size
325
326 //update schedule file ------------------------------------------------------------------------------------------
327 if (!saveScheduleFile()) //create a new schedule file
328 {
329 textDialog textdialogI ("QtInformation", tr("Unable to create schedule file")+" !!<br><br>"+ scheduleFile.errorString(), this);
330 textdialogI.exec();
331 }
332
333 createCron(); //create a cronfile & update user's crontab
334 crontabUpdated = true;
335 changesMade = false;
336
337 textDialog textdialogI ("QtInformation", "<font color=blue><b>" + currentUser + "</b></font><br><br>" +
338 tr("Your crontab is updated successfully"), this);
339 textdialogI.exec();
340
341 //close(); //emmit a QcloseEvent
342 }
343
344 // add button pressed=====================================================================================================
345 // shows part of the widget to add a new profile schedule
346 void scheduleDialog::addPressed()
347 {
348 if (noSchedules) // if no schedules are declared, remove the info line first
349 {
350 uiS.listWidget_schedule -> takeItem (0);
351 noSchedules = false;
352 }
353
354 TotalSchedule = uiS.listWidget_schedule -> count(); //Get the schedule list size
355 currentSchedule = TotalSchedule; //We will add an item !!
356 if ( (TotalSchedule) >= (maxSchedule-1) ) //if the maxSchedulelimit-1 is reached
357 {
358 QMessageBox::warning(this, appName,
359 tr("You have reached the maximum limit of supported scheduled profiles")+ "<br><br>"+
360 tr("You have to remove an item if you wish to add another")+" !!",
361 QMessageBox::Ok | QMessageBox::Default);
362 return;
363 }
364
365 uiS.groupBox_addProfileSchedule -> setVisible(true);
366 uiS.listWidget_schedule -> setEnabled(false);
367 uiS.groupBox_addProfileSchedule -> setTitle(tr("Add profile schedule"));
368 uiS.pushButton_add -> setEnabled(false);
369 uiS.pushButton_modify -> setEnabled(false);
370 uiS.pushButton_remove -> setEnabled(false);
371 uiS.pushButton_cancel -> setEnabled(false);
372 uiS.pushButton_schedule -> setEnabled(false);
373 uiS.pushButton_viewCrontab -> setEnabled(false);
374 clearScheduleDetails();
375 }
376
377 // modify button pressed=====================================================================================================
378 // modifies a profile schedule
379 void scheduleDialog::modifyPressed()
380 {
381 currentSchedule = uiS.listWidget_schedule -> currentRow(); //current operations list row number
382 if ((currentSchedule < 0) || (noSchedules)) //if nothing is selected or no schedules declared, do nothing
383 return;
384
385 uiS.groupBox_addProfileSchedule -> setVisible(true);
386 uiS.groupBox_addProfileSchedule -> setTitle(tr("Modify profile schedule"));
387 uiS.listWidget_schedule -> setEnabled(false);
388 uiS.pushButton_add -> setEnabled(false);
389 uiS.pushButton_modify -> setEnabled(false);
390 uiS.pushButton_remove -> setEnabled(false);
391 uiS.pushButton_cancel -> setEnabled(false);
392 uiS.pushButton_schedule -> setEnabled(false);
393 uiS.pushButton_viewCrontab -> setEnabled(false);
394 }
395
396
397 // List item Remove button pressed==============================================================================================
398 // Removes an item from the schedule list
399 void scheduleDialog::removePressed()
400 {
401 int selected;
402
403 //schedule list remove button
404 selected = uiS.listWidget_schedule -> currentRow(); //current list row number
405 if (selected < 0) //if nothing is selected do nothing
406 return;
407 uiS.listWidget_schedule -> takeItem(selected);
408
409 //update the Schedules array by shifting the elements one position to the left
410 for (int i = selected; i < TotalSchedule+1; ++i)
411 Schedule[i] = Schedule[i + 1]; // copy next element left
412
413 TotalSchedule = uiS.listWidget_schedule -> count(); //Get the schedule list size
414 if (TotalSchedule == 0)
415 {
416 noSchedules = true;
417 uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
418 }
419 changesMade = true;
420 }
421
422 // current button pressed==============================================================================================
423 // Displays the current crontab
424 void scheduleDialog::viewCrontab()
425 {
426 // copy user's crontab to QString CronTab
427 CronTab = "";
428
429 if (WINrunning)
430 cronProcess -> start ("at "); // execute crontab -l command & capture output to QString CronTab
431 else
432 cronProcess -> start ("crontab",QStringList() << "-l"); // execute crontab -l command & capture output to QString CronTab
433
434 cronProcess -> waitForStarted();
435 cronProcess -> waitForFinished();
436
437 if (CronTab == "")
438 CronTab = currentUser + " - " + tr("no crontab entries exist");
439 else
440 CronTab.prepend("User: " + currentUser + " - " + tr("crontab content") + "\n\n");
441 textDialog textdialog ("ViewCurrentCrontab", CronTab, this);
442 textdialog.exec();
443 }
444
445 // modify cancel button pressed=================================================================================================
446 void scheduleDialog::modifyCancelPressed()
447 {
448 uiS.groupBox_addProfileSchedule -> setTitle(tr("Profile schedule details"));
449 uiS.groupBox_addProfileSchedule -> setVisible(false);
450 uiS.listWidget_schedule -> setEnabled(true);
451 uiS.pushButton_add -> setEnabled(true);
452 uiS.pushButton_modify -> setEnabled(true);
453 uiS.pushButton_remove -> setEnabled(true);
454 uiS.pushButton_cancel -> setEnabled(true);
455 uiS.pushButton_schedule -> setEnabled(true);
456 uiS.pushButton_viewCrontab -> setEnabled(true);
457
458 TotalSchedule = uiS.listWidget_schedule -> count(); //Get the schedule list size
459 if (TotalSchedule == 0)
460 {
461 noSchedules = true;
462 uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
463 }
464 }
465
466 // cancelSchedulePressed button pressed===============================================================================================
467 // sets the Profile Schedule details fields to default values
468 void scheduleDialog::clearScheduleDetails()
469 {
470 //initialize all fields
471 uiS.comboBox_profile -> setCurrentIndex(currentProfileIndex);
472 uiS.comboBox_month -> setCurrentIndex(0);
473 uiS.comboBox_dayOfMonth -> setCurrentIndex(0);
474 uiS.comboBox_DayOfWeek -> setCurrentIndex(0);
475 uiS.spinBox_hour -> setValue(0);
476 uiS.spinBox_minute -> setValue(0);
477 uiS.spinBox_delayReboot -> setValue(3);
478 uiS.checkBox_skipCritical -> setCheckState(Qt::Checked);
479 uiS.checkBox_consoleMode -> setCheckState(Qt::Unchecked);
480 uiS.checkBox_reboot -> setCheckState(Qt::Unchecked);
481 }
482
483 // fillDetails=====================================================================================================
484 //function to fill the fields of schedule details from arrays
485 void scheduleDialog::fillDetails()
486 {
487 currentSchedule = uiS.listWidget_schedule -> currentRow(); //current operations list row number
488 if ((currentSchedule < 0) || (noSchedules)) //if nothing is selected or no schedules declared, do nothing
489 return;
490
491 //read array currentSchedule element data & fill add fields
492 uiS.comboBox_profile -> setCurrentIndex( uiS.comboBox_profile -> findText(Schedule[currentSchedule] -> GetProfileName()) );
493 uiS.comboBox_month -> setCurrentIndex(Schedule[currentSchedule] -> GetMonth());
494 uiS.comboBox_dayOfMonth -> setCurrentIndex(Schedule[currentSchedule] -> GetMonthDay());
495 uiS.comboBox_DayOfWeek -> setCurrentIndex(Schedule[currentSchedule] -> GetWeekDay());
496 uiS.spinBox_hour -> setValue(Schedule[currentSchedule] -> GetHour());
497 uiS.spinBox_minute -> setValue(Schedule[currentSchedule] -> GetMinute());
498 uiS.spinBox_delayReboot -> setValue(Schedule[currentSchedule] -> GetDelayReboot());
499
500 if (Schedule[currentSchedule] -> GetSkipCritical())
501 uiS.checkBox_skipCritical -> setCheckState(Qt::Checked);
502 else
503 uiS.checkBox_skipCritical -> setCheckState(Qt::Unchecked);
504
505 if (Schedule[currentSchedule] -> GetConsoleMode())
506 uiS.checkBox_consoleMode -> setCheckState(Qt::Checked);
507 else
508 uiS.checkBox_consoleMode -> setCheckState(Qt::Unchecked);
509 if (Schedule[currentSchedule] -> GetReboot())
510 uiS.checkBox_reboot -> setCheckState(Qt::Checked);
511 else
512 uiS.checkBox_reboot -> setCheckState(Qt::Unchecked);
513 }
514
515 // okaySchedulePressed=====================================================================================================
516 // adss a new profile scedule to the list or modifies an existing one
517 void scheduleDialog::okaySchedulePressed()
518 {
519 noSchedules = false;
520 schedule *tempOp = new schedule;
521
522 //check valid & existent profile*****************************************************
523 QFile proFile (profileDir + uiS.comboBox_profile -> currentText() + ".profile");
524 if (!proFile.open(QIODevice::ReadOnly))
525 {
526 QMessageBox::information(this, appName, tr("Invalid profile")+"<br><br>" + proFile.errorString());
527 return;
528 }
529 /* I have disabled this check as TOO much. If you wish to re-enable it, use the new profile "text format" from loadcurrentprofile()
530 else //read the file to check if it's valid
531 {
532 QDataStream in(&proFile);
533 in.setVersion(QDataStream::Qt_4_3);
534
535 QVariant v; //we will import everything as QVariant using this temp variable
536 QString vString; //temp variable to import "labels" of real data
537 QString tempAppName = "asxeto";
538 double tempAppVersion=0;
539 in>>v; if (v.toString()=="appName")
540 in >> v; tempAppName = v.toString(); //input the application name & version--------------------------
541 in>>v; if (v.toString()=="appVersion")
542 in >> v; tempAppVersion = v.toDouble();
543 if ( (tempAppName != appName) || (tempAppVersion < validProfileVersion) )//check if the file is a valid luckybackup profile
544 {
545 message = tr("The profile you are trying to open is not a valid luckyBackup v.","v is for version")+" "
546 + countStr.setNum(appVersion) + tr(" profile","BEWARE of the whitespace in the beginning")+"<br><br>";
547 QMessageBox::warning(this, appName, message, QMessageBox::Ok | QMessageBox::Default);
548 return;
549 }
550 }
551 */ proFile.close();
552
553 //read fields & fill array
554 tempOp -> SetProfileName (uiS.comboBox_profile -> currentText());
555 tempOp -> SetProfilePath (profileDir + uiS.comboBox_profile -> currentText() + ".profile");
556
557 tempOp -> SetReboot (uiS.checkBox_reboot -> isChecked());
558 tempOp -> SetMonth (uiS.comboBox_month -> currentIndex());
559 tempOp -> SetMonthDay (uiS.comboBox_dayOfMonth -> currentIndex());
560 tempOp -> SetWeekDay (uiS.comboBox_DayOfWeek -> currentIndex());
561 tempOp -> SetHour (uiS.spinBox_hour -> value());
562 tempOp -> SetMinute (uiS.spinBox_minute -> value());
563 tempOp -> SetDelayReboot (uiS.spinBox_delayReboot -> value());
564 tempOp -> SetSkipCritical (uiS.checkBox_skipCritical -> isChecked());
565 tempOp -> SetConsoleMode (uiS.checkBox_consoleMode -> isChecked());
566 tempOp -> SetName (scheduleText(tempOp -> GetProfileName(),
567 tempOp -> GetMonth(),
568 tempOp -> GetMonthDay(),
569 tempOp -> GetWeekDay(),
570 tempOp -> GetHour(),
571 tempOp -> GetMinute(),
572 tempOp -> GetReboot()));
573 Schedule[currentSchedule] = tempOp;
574
575 if (currentSchedule == TotalSchedule)
576 {
577 uiS.listWidget_schedule -> addItem( Schedule[currentSchedule] -> GetName() ); //add item at schedule list
578 uiS.listWidget_schedule -> setCurrentRow(currentSchedule);
579 }
580 else //take out the selected item from the list & insert a new one at the same position with the new name
581 (uiS.listWidget_schedule -> currentItem()) -> setText(Schedule[currentSchedule] -> GetName());
582
583 TotalSchedule = uiS.listWidget_schedule -> count(); //set the TotalSchedule to what it is now
584
585 modifyCancelPressed();
586 changesMade = true;
587 }
588
589 // scheduleText=====================================================================================================
590 // returns the text displayed at the schedule list (eg execute profile default at .... every...)
591 QString scheduleDialog::scheduleText(QString name,int month,int monthday, int weekday, int hour, int minute, bool reboot)
592 {
593 QString tempProfileName = tr("Execute profile")+" '"+ name + "', ";
594 if (reboot)
595 {
596 tempProfileName.append(tr("once, at system startup","full phrase: Execute profile xxxxx, once at system startup"));
597 return tempProfileName;
598 }
599
600 if (hour == -1) // this means every hour
601 {
602 tempProfileName.append(tr("hourly at minute ","full phrase: Execute profile <PROFILENAME> hourly at minute <MM>") + " ");
603 }
604 else
605 {
606 tempProfileName.append(tr("at")+" ");
607 if (hour < 10)
608 tempProfileName.append("0"+countStr.setNum(hour) + ":");
609 else
610 tempProfileName.append(countStr.setNum(hour) + ":");
611 }
612 if (minute < 10)
613 tempProfileName.append("0"+countStr.setNum(minute));
614 else
615 tempProfileName.append(countStr.setNum(minute));
616 tempProfileName.append(", "+tr("every")+" ");
617
618 if ((weekday==0) && (monthday== 0))
619 tempProfileName.append(tr("day"));
620 if (weekday==1)
621 tempProfileName.append(tr("Sunday"));
622 if (weekday==2)
623 tempProfileName.append(tr("Monday"));
624 if (weekday==3)
625 tempProfileName.append(tr("Tuesday"));
626 if (weekday==4)
627 tempProfileName.append(tr("Wednesday"));
628 if (weekday==5)
629 tempProfileName.append(tr("Thursday"));
630 if (weekday==6)
631 tempProfileName.append(tr("Friday"));
632 if (weekday==7)
633 tempProfileName.append(tr("Saturday"));
634
635 if (monthday != 0)
636 {
637 if (weekday!=0)
638 tempProfileName.append(" "+tr("& on the")+" ");
639
640 tempProfileName.append(countStr.setNum(monthday));
641
642 if (monthday == 1) tempProfileName.append(tr("st","as in 1st day"));
643 if (monthday == 2) tempProfileName.append(tr("nd","as in 2nd day"));
644 if (monthday == 3) tempProfileName.append(tr("rd","as in 3rd day"));
645 if ((monthday > 3) && (monthday < 8)) tempProfileName.append(tr("th","as in 4th, 5th, 6th or 7th day"));
646 if (monthday == 8) tempProfileName.append(tr("th","as in 8th day"));
647 if (monthday == 9) tempProfileName.append(tr("th","as in 9th day"));
648 if (monthday == 10) tempProfileName.append(tr("th","as in 10th day"));
649 if (monthday == 11) tempProfileName.append(tr("th","as in 11th day"));
650 if ((monthday > 11) && (monthday < 20)) tempProfileName.append(tr("th","as in 12th....19th day"));
651 if (monthday == 20) tempProfileName.append(tr("th","as in 20th day"));
652 if (monthday == 21) tempProfileName.append(tr("st","as in 21st day"));
653 if (monthday == 22) tempProfileName.append(tr("nd","as in 22nd day"));
654 if (monthday == 23) tempProfileName.append(tr("rd","as in 23rd day"));
655 if ((monthday > 23) && (monthday < 31)) tempProfileName.append(tr("th","as in 24th...30th day"));
656 if (monthday == 31) tempProfileName.append(tr("st","as in 31st day"));
657 tempProfileName.append(" "+tr("day"));
658 }
659 if (month == 0)
660 {
661 tempProfileName.append(" "+tr("of any month"));
662 return tempProfileName;
663 }
664 if (month == 1)
665 tempProfileName.append(" "+tr("of")+" "+tr("January","full phrase: 'of January'"));
666 if (month == 2)
667 tempProfileName.append(" "+tr("of")+" "+tr("February","full phrase: 'of February'"));
668 if (month == 3)
669 tempProfileName.append(" "+tr("of")+" "+tr("March","full phrase: 'of March'"));
670 if (month == 4)
671 tempProfileName.append(" "+tr("of")+" "+tr("April","full phrase: 'of April'"));
672 if (month == 5)
673 tempProfileName.append(" "+tr("of")+" "+tr("May","full phrase: 'of May'"));
674 if (month == 6)
675 tempProfileName.append(" "+tr("of")+" "+tr("June","full phrase: 'of June'"));
676 if (month == 7)
677 tempProfileName.append(" "+tr("of")+" "+tr("July","full phrase: 'of July'"));
678 if (month == 8)
679 tempProfileName.append(" "+tr("of")+" "+tr("August","full phrase: 'of August'"));
680 if (month == 9)
681 tempProfileName.append(" "+tr("of")+" "+tr("September","full phrase: 'of September'"));
682 if (month == 10)
683 tempProfileName.append(" "+tr("of")+" "+tr("October","full phrase: 'of October'"));
684 if (month == 11)
685 tempProfileName.append(" "+tr("of")+" "+tr("November","full phrase: 'of November'"));
686 if (month == 12)
687 tempProfileName.append(" "+tr("of")+" "+tr("December","full phrase: 'of December'"));
688
689 return tempProfileName;
690 }
691
692 // createCron=====================================================================================================
693 //creates a cron file & updates the user's crontab
694 void scheduleDialog::createCron()
695 {
696 QString ScheduleLine = "";
697 QString ProfilePath="";
698 QString ProfileName="";
699 QString luckyEntryStart = "\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
700 QString luckyEntryEnd = "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
701
702 // windows used variables:
703 QString WinScheduleLine = "";
704 int WinScheduleType;
705 QString batFileName ="";
706 QString batLine ="";
707
708 // copy user's crontab to QString CronTab & delete luckybackup's entries
709 CronTab = "";
710 cronProcess -> start ("crontab",QStringList() << "-l"); // execute crontab -l command & capture output to QString CronTab
711 cronProcess -> waitForStarted();
712 cronProcess -> waitForFinished();
713 int x = CronTab.indexOf(luckyEntryStart);
714 int y = CronTab.indexOf(luckyEntryEnd) + luckyEntryEnd.size() - x;
715
716 //--------------------windows part - Juan patch ---------------------------------------------
717 //--------------------Windows 8: Use New-ScheduledTaskTrigger----------------------------
718 //Get-ScheduledTask------New-ScheduledTaskTrigger
719 if (WINrunning)
720 {
721 //windows execute at to delete command
722 cronProcess -> start ("at /delete /yes"); // execute crontab command to replace the user's crontab with luckybackup cron
723 cronProcess -> waitForStarted();
724 cronProcess -> waitForFinished();
725 cronProcess -> start ("del "+scheduleDir+" *.bat"); // execute crontab command to replace the user's crontab with luckybackup cron
726 cronProcess -> waitForStarted();
727 cronProcess -> waitForFinished();
728 currentSchedule = 0;
729 while (currentSchedule < TotalSchedule)
730 {
731 if (!Schedule[currentSchedule] -> GetReboot() ) //append @reboot if checkbox selected NOT SUPPORTED
732 {
733 WinScheduleLine = "";
734 WinScheduleType=0;
735 batFileName=scheduleDir;
736 //begin windows time procesing: using at, cannot have hourly or monthly schedules
737 WinScheduleLine.append(" "+countStr.setNum(Schedule[currentSchedule] -> GetHour())+":");
738 batFileName.append(countStr.setNum(Schedule[currentSchedule] -> GetHour()));
739 WinScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMinute()) + " /every:");
740 batFileName.append(countStr.setNum(Schedule[currentSchedule] -> GetMinute()));
741 if (Schedule[currentSchedule] -> GetMonthDay() != 0) //Day of month
742 {
743 WinScheduleType=1;
744 WinScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMonthDay()) + " ");
745 batFileName.append(countStr.setNum(Schedule[currentSchedule] -> GetMonthDay()) );
746 }
747 if (Schedule[currentSchedule] -> GetWeekDay() != 0) //Day of week
748 {
749 WinScheduleType=2;
750 QList<QString> daytypes;
751 daytypes << "L" << "M" << "X" << "J" << "V" << "S" << "D" ;
752 WinScheduleLine.append(daytypes[Schedule[currentSchedule] -> GetWeekDay() -1]+ " ");
753 batFileName.append(daytypes[Schedule[currentSchedule] -> GetWeekDay() -1]);
754 }
755 if (WinScheduleType==0)
756 WinScheduleLine.append("D,L,M,Mi,J,V,S ");
757 batFileName.append(".bat");
758 WinScheduleLine.append(" cmd /c \"");
759 WinScheduleLine.append(batFileName+"\"");
760 batLine.append("\""+luckyBackupDir+"luckybackup_console.exe\" -c --no-questions ");
761
762 if (Schedule[currentSchedule] -> GetSkipCritical()) //append --skip-critical
763 batLine.append("--skip-critical ");
764
765 ProfilePath = Schedule[currentSchedule] -> GetProfilePath();
766 //ProfilePath.replace(" ", "\\ ");
767 batLine.append("\""+ProfilePath+"\""); //append profile full path
768
769 // append cron logfile part
770 ProfileName = Schedule[currentSchedule] -> GetProfileName();
771 //ProfileName.replace(" ", "\\ ");
772 batLine.append(" > \"" + logDir + ProfileName + "-" + cronlogString +
773 "\" 2>&1"+ "\n"); //append logfile output with >> instead of single >
774
775 QFile batFile (batFileName);
776 if (!batFile.open(QIODevice::WriteOnly | QIODevice::Text)) //create a new batFile as textfile
777 QMessageBox::information(this, appName, tr("Unable to create cron file")+" !!<br><br>"+ batFile.errorString());
778 else{
779 //windows execute at to create command
780 QTextStream batOut(&batFile);
781 batOut << batLine;
782 batFile.close();
783 cronProcess -> start ("at "+WinScheduleLine); // execute crontab command to replace the user's crontab with luckybackup cron
784 cronProcess -> waitForStarted();
785 cronProcess -> waitForFinished();
786 }
787
788 currentSchedule++;
789 }
790 }
791 QString nada=cronProcess -> readAllStandardOutput();
792 return;
793 } //----------------windows part END---------------------------------
794
795
796 CronTab.remove (x,y); //remove luckyBackup entries from CronTab
797
798 QFile cronFile (cronfilename);
799 if (cronFile.exists()) //if a cronFile exists delete it
800 cronFile.remove();
801
802 if (!cronFile.open(QIODevice::WriteOnly | QIODevice::Text)) //create a new cronFile as textfile
803 QMessageBox::information(this, appName, tr("Unable to create cron file")+" !!<br><br>"+ cronFile.errorString());
804 else //write some stuff to schedule file
805 {
806 QTextStream out(&cronFile);
807 out << CronTab; //append the old user's crontab first (minus the luckybackup entries)
808 out << luckyEntryStart; //# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
809 currentSchedule = 0;
810 while (currentSchedule < TotalSchedule)
811 {
812 if (Schedule[currentSchedule] -> GetReboot()) //append @reboot if checkbox selected
813 {
814 ScheduleLine.append("@reboot ");
815 if (Schedule[currentSchedule] -> GetDelayReboot() > 0 )
816 ScheduleLine.append("sleep " + countStr.setNum(Schedule[currentSchedule] -> GetDelayReboot() * 60) +"; ");
817 }
818 else // else append time-day etc
819 {
820 ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMinute()) + " "); //minute
821 if (Schedule[currentSchedule] -> GetHour() == -1)
822 ScheduleLine.append("* "); //hourly
823 else
824 ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetHour()) + " "); //hour
825
826 if (Schedule[currentSchedule] -> GetMonthDay() == 0) //Day of month
827 ScheduleLine.append("* ");
828 else
829 ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMonthDay()) + " ");
830
831 if (Schedule[currentSchedule] -> GetMonth() == 0) //Month
832 ScheduleLine.append("* ");
833 else
834 ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMonth()) + " ");
835
836 if (Schedule[currentSchedule] -> GetWeekDay() == 0) //Day of week
837 ScheduleLine.append("* ");
838 else
839 ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetWeekDay() -1 ) + " ");
840 }
841
842 if (Schedule[currentSchedule] -> GetConsoleMode()) //append -c --no-questions for CL or --silent for gui
843 ScheduleLine.append("/usr/bin/luckybackup -c --no-questions ");
844 else
845 ScheduleLine.append("env DISPLAY=:0 /usr/bin/luckybackup --silent ");
846
847 if (Schedule[currentSchedule] -> GetSkipCritical()) //append --skip-critical
848 ScheduleLine.append("--skip-critical ");
849
850 ProfilePath = Schedule[currentSchedule] -> GetProfilePath();
851 ProfilePath.replace(" ", "\\ ");
852 ScheduleLine.append(ProfilePath); //append profile full path
853
854 // append cron logfile part
855 ProfileName = Schedule[currentSchedule] -> GetProfileName();
856 ProfileName.replace(" ", "\\ ");
857 ScheduleLine.append(" > " + logDir + ProfileName + "-" + cronlogString +
858 " 2>&1"+ "\n"); //append logfile output with >> instead of single >
859
860 out << ScheduleLine;
861
862 ScheduleLine = "";
863 currentSchedule++;
864 }
865 out << luckyEntryEnd; //# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
866 }
867
868 cronFile.close();
869
870 cronProcess -> start ("crontab",QStringList() << cronfilename); // execute crontab command to replace the user's crontab with luckybackup cron
871 cronProcess -> waitForStarted();
872 cronProcess -> waitForFinished();
873
874 //if the new user's crontab contains no actual schedules > remove user's crontab completely
875 CronTab = "";
876 cronProcess -> start ("crontab",QStringList() << "-l"); // execute crontab -l command & capture output to QString CronTab
877 cronProcess -> waitForStarted();
878 cronProcess -> waitForFinished();
879 if (CronTab == luckyEntryStart + luckyEntryEnd)
880 {
881 cronProcess -> start ("crontab",QStringList() << "-r"); // remove user's crontab
882 cronProcess -> waitForStarted();
883 cronProcess -> waitForFinished();
884 }
885 }
886
887 // appendCrontabOutput=====================================================================================================
888 //appends the user's crontab to a QString
889 void scheduleDialog::appendCrontabOutput()
890 {
891 QTextCodec *codec = QTextCodec::codecForName("UTF-8");
892 CronTab = codec->toUnicode(cronProcess -> readAllStandardOutput());
893 //CronTab = QString(cronProcess -> readAllStandardOutput());
894 }
895
896 // saveScheduleFile=====================================================================================================
897 // saves the schedule file in text format
898 bool scheduleDialog::saveScheduleFile()
899 {
900 if (scheduleFile.exists()) //if a schedule file exists delete it
901 scheduleFile.remove();
902
903 if (!scheduleFile.open(QIODevice::WriteOnly)) //create a new schedule file
904 return false;
905 else //write some stuff to schedule file
906 {
907 QTextStream out(&scheduleFile);
908
909 out << "***************************** WARNING *****************************\n";
910 out << "Do NOT edit this file directly, unless you REALLY know what you are doing !!\n\n\n";
911
912 out << "[app_global]\n";
913 out << "appName=" << appName << "\n"; //output the application name
914 out << "appVersion=" << appVersion << "\n"; //output the application version
915 out << "File_Type=" << "luckybackup_schedule_file" << "\n\n"; //output the application file type (?)
916
917 out << "\n[schedule_global]\n";
918 out << "TotalSchedules=" << TotalSchedule << "\n\n"; //output the size of the schedule list
919
920 currentSchedule = 0;
921 while (currentSchedule < TotalSchedule)
922 {
923 out << "[Schedule] - " << currentSchedule << "\n";
924
925 out << "ScheduleName=" << Schedule[currentSchedule] -> GetName() << "\n";
926
927 out << "ProfilePath=" << Schedule[currentSchedule] -> GetProfilePath() << "\n";
928 out << "ProfileName=" << Schedule[currentSchedule] -> GetProfileName() << "\n";
929
930 out << "ScheduleMonth=" << Schedule[currentSchedule] -> GetMonth() << "\n";
931 out << "ScheduleMonthDay=" << Schedule[currentSchedule] -> GetMonthDay() << "\n";
932 out << "ScheduleWeekDay=" << Schedule[currentSchedule] -> GetWeekDay() << "\n";
933 out << "ScheduleHour=" << Schedule[currentSchedule] -> GetHour() << "\n";
934 out << "ScheduleMinute=" << Schedule[currentSchedule] -> GetMinute() << "\n";
935
936 out << "ScheduleSkipCritical=" << Schedule[currentSchedule] -> GetSkipCritical() << "\n";
937 out << "ScheduleConsoleMode=" << Schedule[currentSchedule] -> GetConsoleMode() << "\n";
938 out << "ScheduleAtReboot=" << Schedule[currentSchedule] -> GetReboot() << "\n";
939 out << "ScheduleAtRebootDelay=" << Schedule[currentSchedule] -> GetDelayReboot() << "\n";
940
941 out << "[Schedule_end] - " << currentSchedule << "\n\n";
942 currentSchedule++;
943 }
944
945 out << "\n[all_schedules_end]" << "\n";
946 }
947
948 scheduleFile.close(); //close the file
949 return true;
950 }
951
952 // end of scheduleDialog.cpp ---------------------------------------------------------------------------
953