~ubuntu-branches/ubuntu/maverick/luckybackup/maverick

« back to all changes in this revision

Viewing changes to src/scheduleDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-21 20:09:36 UTC
  • Revision ID: james.westby@ubuntu.com-20090521200936-jcbu6euvoxz7sef7
Tags: upstream-0.3
Import upstream version 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ---------------------------------- scheduleDialog.cpp ---------------------------------------------------------------------------
 
2
Display a dialog. Schedules profiles via cron
 
3
 
 
4
===============================================================================================================================
 
5
===============================================================================================================================
 
6
     This file is part of "luckyBackup" project
 
7
     Copyright 2008-2009, 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          : 23 April 2009
 
27
 
 
28
===============================================================================================================================
 
29
===============================================================================================================================
 
30
*/
 
31
 
 
32
 
 
33
#include "scheduleDialog.h"
 
34
#include "scheduleClass.h"
 
35
 
 
36
int const maxSchedule = 20;             //The maximum number or profiles schedule permitted
 
37
schedule *Schedule[maxSchedule];        //The array that holds name etc of "schedule list"
 
38
QFile scheduleFile (schedulefilename );
 
39
QProcess *cronProcess;          //crontab process
 
40
 
 
41
// class scheduleDialog Constructor=================================================================================================
 
42
// schedule existing profiles to execute via cron
 
43
scheduleDialog::scheduleDialog (QDialog *parent) : QDialog (parent)
 
44
{
 
45
        uiS.setupUi(this);
 
46
 
 
47
        //variables initialization
 
48
        TotalSchedule=0;                //The number of available profiles schedulesin "schedule list"- starts from 1
 
49
        currentSchedule=0;              //this holds the current row from the "schedule list"- starts from 0
 
50
        noSchedules = TRUE;             //becomes true when no schedules are declared
 
51
 
 
52
        //connections ----------------------------------------------------------------------------------------------------
 
53
        connect(uiS.pushButton_schedule, SIGNAL( pressed() ), this, SLOT(schedulePressed() ) );         //cronIT pushButton
 
54
        connect(uiS.pushButton_cancel, SIGNAL( pressed() ), this, SLOT(cancelPressed() ) );             //cancel pushButton
 
55
 
 
56
        connect(uiS.pushButton_add, SIGNAL( pressed() ), this, SLOT(addPressed() ) );                   //add command pushButton
 
57
        connect(uiS.pushButton_remove, SIGNAL( pressed() ), this, SLOT(removePressed() ) );             //remove command pushButton
 
58
        connect(uiS.pushButton_modify, SIGNAL( pressed() ), this, SLOT(modifyPressed() ) );             //modify command pushButton
 
59
        
 
60
        connect(uiS.pushButton_modifyOkay, SIGNAL( pressed() ), this, SLOT(okaySchedulePressed() ) );           //modify okay pushButton
 
61
        connect(uiS.pushButton_modifyCancel, SIGNAL( pressed() ), this, SLOT(modifyCancelPressed() ) );         //modify cancel pushButton
 
62
 
 
63
        connect(uiS.listWidget_schedule , SIGNAL( currentRowChanged(int) ), this, SLOT(fillDetails() ) );       //list item selection changed
 
64
 
 
65
        
 
66
        cronProcess = new QProcess(this);       //create a new qprocess (for rsync) & connect signals
 
67
        connect(cronProcess, SIGNAL(readyReadStandardError()), this, SLOT(appendCrontabOutput()));
 
68
        connect(cronProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(appendCrontabOutput()));
 
69
 
 
70
        //initialize view of widgets
 
71
        uiS.groupBox_addProfileSchedule -> setVisible(FALSE);
 
72
        uiS.listWidget_schedule -> setEnabled(TRUE);
 
73
        uiS.pushButton_add -> setEnabled(TRUE);
 
74
        uiS.pushButton_modify -> setEnabled(TRUE);
 
75
        uiS.pushButton_remove -> setEnabled(TRUE);
 
76
        uiS.pushButton_cancel -> setEnabled(TRUE);
 
77
        uiS.pushButton_schedule -> setEnabled(TRUE);
 
78
        //profile combobox initialization
 
79
        uiS.comboBox_profile -> clear();
 
80
        currentProfileIndex = -1;
 
81
        QDir profiledir(profileDir);
 
82
        profiledir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot);
 
83
        profiledir.setSorting(QDir::Name | QDir::LocaleAware);
 
84
        QStringList profileNames = profiledir.entryList(QStringList("*.profile"));
 
85
        QString profilename="";
 
86
        for (count = 0; count < profileNames.size(); ++count)
 
87
        {
 
88
                profilename = profileNames[count];
 
89
                if (currentProfile == profileDir + profilename) // set the currentProfileIndex to the index of the current profile
 
90
                        currentProfileIndex = count;
 
91
                profilename.chop(8);
 
92
                uiS.comboBox_profile -> addItem (profilename);
 
93
        }
 
94
        if (currentProfileIndex == -1)  // if no default profile exists
 
95
                currentProfileIndex = 0;        
 
96
        
 
97
        //open the schedule.dat file and fill arrays & schedule list
 
98
        if (!scheduleFile.open(QIODevice::ReadOnly))    //if there is an error opening it, delete it to create a new one
 
99
                scheduleFile.remove();
 
100
        else
 
101
        {
 
102
                QDataStream in(&scheduleFile);
 
103
                in.setVersion(QDataStream::Qt_4_3);
 
104
 
 
105
                QVariant v;                                     //we will import everything as QVariant using this temp variable
 
106
                QString tempAppName = "asxeto";
 
107
                double tempAppVersion=0;
 
108
                in >> v;        tempAppName = v.toString();     //input the application name & version--------------------------
 
109
                in >> v;        tempAppVersion = v.toDouble();
 
110
                if ( (tempAppName != appName) || (tempAppVersion < validScheduleVersion) )//check if the file is a valid luckybackup schedule file
 
111
                {
 
112
                        message = tr("The existent schedule file is not a valid luckyBackup v.")
 
113
                                + countStr.setNum(appVersion) + " "+tr("file")+"<br><br>"+tr("Starting from scratch")+"...<br>" ;
 
114
                        QMessageBox::warning(this, appName, message, QMessageBox::Ok | QMessageBox::Default);
 
115
                        scheduleFile.remove();
 
116
                        return;
 
117
                }
 
118
 
 
119
                in >> v;        TotalSchedule = v.toInt();      //input the size of the schedule list first
 
120
 
 
121
                currentSchedule = 0;
 
122
                while (currentSchedule < TotalSchedule)
 
123
                {
 
124
                        schedule *tempSchedule = new schedule;
 
125
 
 
126
                        in >> v; tempSchedule   -> SetName(v.toString());
 
127
 
 
128
                        in >> v; tempSchedule   -> SetProfilePath(v.toString());
 
129
                        in >> v; tempSchedule   -> SetProfileName(v.toString());
 
130
 
 
131
                        in >> v; tempSchedule   -> SetMonth(v.toInt());
 
132
                        in >> v; tempSchedule   -> SetMonthDay(v.toInt());
 
133
                        in >> v; tempSchedule   -> SetWeekDay(v.toInt());
 
134
                        in >> v; tempSchedule   -> SetHour(v.toInt());
 
135
                        in >> v; tempSchedule   -> SetMinute(v.toInt());
 
136
 
 
137
                        in >> v; tempSchedule   -> SetSkipCritical(v.toBool());
 
138
 
 
139
                        Schedule[currentSchedule] = tempSchedule;
 
140
                        uiS.listWidget_schedule -> addItem(scheduleText(Schedule[currentSchedule] -> GetProfileName(),
 
141
                                                        Schedule[currentSchedule] -> GetMonth(),
 
142
                                                        Schedule[currentSchedule] -> GetMonthDay(),
 
143
                                                        Schedule[currentSchedule] -> GetWeekDay(),
 
144
                                                        Schedule[currentSchedule] -> GetHour(),
 
145
                                                        Schedule[currentSchedule] -> GetMinute()));
 
146
                        currentSchedule++;
 
147
                }
 
148
                TotalSchedule = uiS.listWidget_schedule -> count();     //Get the schedule list size
 
149
 
 
150
                if (TotalSchedule > 0)
 
151
                {
 
152
                        uiS.listWidget_schedule -> setCurrentRow(0);
 
153
                        noSchedules = FALSE;
 
154
                }
 
155
                else
 
156
                        uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
 
157
                scheduleFile.close();                                   //close the file
 
158
        }
 
159
}
 
160
//===================================================================================================================================================
 
161
//------------------------------------------------------------------SLOTS----------------------------------------------------------------------------
 
162
//===================================================================================================================================================
 
163
 
 
164
 
 
165
// cancel button pressed=====================================================================================================
 
166
void scheduleDialog::cancelPressed()
 
167
{
 
168
        close();        //emmit a QcloseEvent
 
169
}
 
170
 
 
171
//window closebutton pressed or QcloseEvent emitted==========================================================================
 
172
void scheduleDialog::closeEvent(QCloseEvent *event)
 
173
{
 
174
        event->accept();
 
175
}
 
176
 
 
177
 
 
178
// schedule button pressed=======================================================================================================
 
179
// Read all fields of dialog, fill appropriate variables, saves crontab file and creates a cron job
 
180
void scheduleDialog::schedulePressed()
 
181
{
 
182
        if (noSchedules)                        // if no schedules are declared, remove the info line first
 
183
                uiS.listWidget_schedule -> takeItem (0);
 
184
 
 
185
        TotalSchedule = uiS.listWidget_schedule -> count();     //Get the schedule list size
 
186
 
 
187
        //update schedule file ------------------------------------------------------------------------------------------
 
188
        if (scheduleFile.exists())              //if a schedule file exists delete it
 
189
                scheduleFile.remove();
 
190
 
 
191
        if (noSchedules)                        // if no schedules are declared, remove the info line
 
192
                uiS.listWidget_schedule -> takeItem (0);
 
193
 
 
194
        if (!scheduleFile.open(QIODevice::WriteOnly))   //create a new schedule file
 
195
                QMessageBox::information(this, appName, tr("Unable to create schedule file")+" !!<br><br>"+ scheduleFile.errorString());
 
196
        else            //write some stuff to schedule file
 
197
        {
 
198
                QDataStream out(&scheduleFile);
 
199
                out.setVersion(QDataStream::Qt_4_3);
 
200
 
 
201
                QVariant v;                                     //we will export everything as QVariant using this temp variable
 
202
                v = appName;            out << v;               //output the application name 
 
203
                v = appVersion;         out << v;               //output the application version 
 
204
                v = TotalSchedule;      out << v;               //output the size of the schedule list 
 
205
                currentSchedule = 0;
 
206
                while (currentSchedule < TotalSchedule)
 
207
                {
 
208
                        v = Schedule[currentSchedule] -> GetName();             out << v;
 
209
 
 
210
                        v = Schedule[currentSchedule] -> GetProfilePath();              out << v;
 
211
                        v = Schedule[currentSchedule] -> GetProfileName();              out << v;
 
212
 
 
213
                        v = Schedule[currentSchedule] -> GetMonth();                    out << v;
 
214
                        v = Schedule[currentSchedule] -> GetMonthDay();                 out << v;
 
215
                        v = Schedule[currentSchedule] -> GetWeekDay();                  out << v;
 
216
                        v = Schedule[currentSchedule] -> GetHour();                     out << v;
 
217
                        v = Schedule[currentSchedule] -> GetMinute();                   out << v;
 
218
 
 
219
                        v = Schedule[currentSchedule] -> GetSkipCritical();                     out << v;
 
220
 
 
221
                        currentSchedule++;
 
222
                }
 
223
                scheduleFile.close();   //close the file
 
224
        }
 
225
        
 
226
        createCron();           //create a cronfile & update user's crontab
 
227
 
 
228
        close();                //emmit a QcloseEvent
 
229
}
 
230
 
 
231
// add button pressed=====================================================================================================
 
232
// shows part of the widget to add a new profile schedule
 
233
void scheduleDialog::addPressed()
 
234
{
 
235
        if (noSchedules)                        // if no schedules are declared, remove the info line first
 
236
        {
 
237
                uiS.listWidget_schedule -> takeItem (0);
 
238
                noSchedules = FALSE;
 
239
        }
 
240
 
 
241
        TotalSchedule = uiS.listWidget_schedule -> count();     //Get the schedule list size
 
242
        currentSchedule = TotalSchedule;                        //We will add an item !!
 
243
        if ( (TotalSchedule) >= (maxSchedule-1) )       //if the maxSchedulelimit-1 is reached 
 
244
        {
 
245
                QMessageBox::warning(this, appName,
 
246
                                tr("You have reached the maximum limit of supported scheduled profiles")+ "<br><br>"+
 
247
                                tr("You have to remove an item if you wish to add another")+" !!",
 
248
                                QMessageBox::Ok | QMessageBox::Default);
 
249
                return;
 
250
        }
 
251
        
 
252
        uiS.groupBox_addProfileSchedule -> setVisible(TRUE);
 
253
        uiS.listWidget_schedule -> setEnabled(FALSE);
 
254
        uiS.groupBox_addProfileSchedule -> setTitle(tr("Add profile schedule"));
 
255
        uiS.pushButton_add -> setEnabled(FALSE);
 
256
        uiS.pushButton_modify -> setEnabled(FALSE);
 
257
        uiS.pushButton_remove -> setEnabled(FALSE);
 
258
        uiS.pushButton_cancel -> setEnabled(FALSE);
 
259
        uiS.pushButton_schedule -> setEnabled(FALSE);
 
260
        clearScheduleDetails();
 
261
}
 
262
 
 
263
// modify button pressed=====================================================================================================
 
264
// modifies a profile schedule
 
265
void scheduleDialog::modifyPressed()
 
266
{
 
267
        currentSchedule = uiS.listWidget_schedule -> currentRow();              //current operations list row number
 
268
        if ((currentSchedule < 0) || (noSchedules))                             //if nothing is selected or no schedules declared, do nothing
 
269
                return;
 
270
 
 
271
        uiS.groupBox_addProfileSchedule -> setVisible(TRUE);
 
272
        uiS.groupBox_addProfileSchedule -> setTitle(tr("Modify profile schedule"));
 
273
        uiS.listWidget_schedule -> setEnabled(FALSE);
 
274
        uiS.pushButton_add -> setEnabled(FALSE);
 
275
        uiS.pushButton_modify -> setEnabled(FALSE);
 
276
        uiS.pushButton_remove -> setEnabled(FALSE);
 
277
        uiS.pushButton_cancel -> setEnabled(FALSE);
 
278
        uiS.pushButton_schedule -> setEnabled(FALSE);
 
279
}
 
280
 
 
281
 
 
282
// List item Remove button pressed==============================================================================================
 
283
// Removes an item from the schedule list
 
284
void scheduleDialog::removePressed()
 
285
{
 
286
        int selected;
 
287
 
 
288
        //schedule list remove button
 
289
        selected = uiS.listWidget_schedule -> currentRow();     //current list row number
 
290
                if (selected < 0)                               //if nothing is selected do nothing
 
291
                        return;
 
292
        uiS.listWidget_schedule -> takeItem(selected);
 
293
 
 
294
        TotalSchedule = uiS.listWidget_schedule -> count();     //Get the schedule list size
 
295
        if (TotalSchedule == 0)
 
296
        {
 
297
                noSchedules = TRUE;
 
298
                uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
 
299
        }
 
300
}
 
301
 
 
302
// modify cancel button pressed=================================================================================================
 
303
void scheduleDialog::modifyCancelPressed()
 
304
{
 
305
        uiS.groupBox_addProfileSchedule -> setTitle(tr("Profile schedule details"));
 
306
        uiS.groupBox_addProfileSchedule -> setVisible(FALSE);
 
307
        uiS.listWidget_schedule -> setEnabled(TRUE);
 
308
        uiS.pushButton_add -> setEnabled(TRUE);
 
309
        uiS.pushButton_modify -> setEnabled(TRUE);
 
310
        uiS.pushButton_remove -> setEnabled(TRUE);
 
311
        uiS.pushButton_cancel -> setEnabled(TRUE);
 
312
        uiS.pushButton_schedule -> setEnabled(TRUE);
 
313
 
 
314
        TotalSchedule = uiS.listWidget_schedule -> count();     //Get the schedule list size
 
315
        if (TotalSchedule == 0)
 
316
        {
 
317
                noSchedules = TRUE;
 
318
                uiS.listWidget_schedule -> addItem(tr("No schedules are declared !!"));
 
319
        }
 
320
}
 
321
 
 
322
// cancelSchedulePressed button pressed===============================================================================================
 
323
// sets the Profile Schedule details fields to default values
 
324
void scheduleDialog::clearScheduleDetails()
 
325
{
 
326
        //initialize all fields
 
327
        uiS.comboBox_profile -> setCurrentIndex(currentProfileIndex);
 
328
        uiS.comboBox_month -> setCurrentIndex(0);
 
329
        uiS.comboBox_dayOfMonth -> setCurrentIndex(0);
 
330
        uiS.comboBox_DayOfWeek -> setCurrentIndex(0);
 
331
        uiS.spinBox_hour -> setValue(0);
 
332
        uiS.spinBox_minute -> setValue(0);
 
333
        uiS.checkBox_skipCritical -> setCheckState(Qt::Checked);
 
334
}
 
335
 
 
336
// fillDetails=====================================================================================================
 
337
//function to fill the fields of schedule details from arrays
 
338
void scheduleDialog::fillDetails()
 
339
{
 
340
        currentSchedule = uiS.listWidget_schedule -> currentRow();              //current operations list row number
 
341
        if ((currentSchedule < 0) || (noSchedules))                             //if nothing is selected or no schedules declared, do nothing
 
342
                return;
 
343
 
 
344
        //read array currentSchedule element data & fill add fields
 
345
        uiS.comboBox_profile -> setCurrentIndex( uiS.comboBox_profile -> findText(Schedule[currentSchedule] -> GetProfileName()) );
 
346
        uiS.comboBox_month -> setCurrentIndex(Schedule[currentSchedule] -> GetMonth());
 
347
        uiS.comboBox_dayOfMonth -> setCurrentIndex(Schedule[currentSchedule] -> GetMonthDay());
 
348
        uiS.comboBox_DayOfWeek -> setCurrentIndex(Schedule[currentSchedule] -> GetWeekDay());
 
349
        uiS.spinBox_hour -> setValue(Schedule[currentSchedule] -> GetHour());
 
350
        uiS.spinBox_minute -> setValue(Schedule[currentSchedule] -> GetMinute());
 
351
        if (Schedule[currentSchedule] -> GetSkipCritical())
 
352
                uiS.checkBox_skipCritical -> setCheckState(Qt::Checked);
 
353
        else
 
354
                uiS.checkBox_skipCritical -> setCheckState(Qt::Unchecked);
 
355
}
 
356
 
 
357
// okaySchedulePressed=====================================================================================================
 
358
// adss a new profile scedule to the list or modifies an existing one
 
359
void scheduleDialog::okaySchedulePressed()
 
360
{
 
361
        noSchedules = FALSE;
 
362
        schedule *tempOp = new schedule;
 
363
 
 
364
        //check valid & existent profile*****************************************************
 
365
        QFile proFile (profileDir + uiS.comboBox_profile -> currentText() + ".profile");
 
366
        if (!proFile.open(QIODevice::ReadOnly))
 
367
        {
 
368
                QMessageBox::information(this, appName, tr("Invalid profile")+"<br><br>" + proFile.errorString());
 
369
                return;
 
370
        }
 
371
        else    //read the file to check if it's valid
 
372
        {
 
373
                QDataStream in(&proFile);
 
374
                in.setVersion(QDataStream::Qt_4_3);
 
375
 
 
376
                QVariant v;                                     //we will import everything as QVariant using this temp variable
 
377
                QString vString;                                //temp variable to import "labels" of real data
 
378
                QString tempAppName = "asxeto";
 
379
                double tempAppVersion=0;
 
380
                in>>v;  if (v.toString()=="appName")
 
381
                        in >> v;        tempAppName = v.toString();     //input the application name & version--------------------------
 
382
                in>>v;  if (v.toString()=="appVersion")
 
383
                        in >> v;        tempAppVersion = v.toDouble();
 
384
                if ( (tempAppName != appName) || (tempAppVersion < validProfileVersion) )//check if the file is a valid luckybackup profile
 
385
                {
 
386
                        message = tr("The profile you are trying to open is not a valid luckyBackup v.")+" "
 
387
                                + countStr.setNum(appVersion) + " "+tr("profile")+"<br><br>";
 
388
                        QMessageBox::warning(this, appName, message, QMessageBox::Ok | QMessageBox::Default);
 
389
                        return;
 
390
                }
 
391
        }
 
392
        proFile.close();
 
393
 
 
394
        //read fields & fill array
 
395
        tempOp -> SetProfileName (uiS.comboBox_profile -> currentText());
 
396
        tempOp -> SetProfilePath (profileDir + uiS.comboBox_profile -> currentText() + ".profile");
 
397
 
 
398
        tempOp -> SetMonth (uiS.comboBox_month -> currentIndex());
 
399
        tempOp -> SetMonthDay (uiS.comboBox_dayOfMonth -> currentIndex());
 
400
        tempOp -> SetWeekDay (uiS.comboBox_DayOfWeek -> currentIndex());
 
401
        tempOp -> SetHour (uiS.spinBox_hour -> value());
 
402
        tempOp -> SetMinute (uiS.spinBox_minute -> value());
 
403
        tempOp -> SetSkipCritical (uiS.checkBox_skipCritical -> isChecked());
 
404
        tempOp -> SetName (scheduleText(tempOp -> GetProfileName(),
 
405
                        tempOp -> GetMonth(),
 
406
                        tempOp -> GetMonthDay(),
 
407
                        tempOp -> GetWeekDay(),
 
408
                        tempOp -> GetHour(),
 
409
                        tempOp -> GetMinute()));
 
410
        Schedule[currentSchedule] = tempOp;
 
411
 
 
412
        if (currentSchedule == TotalSchedule)
 
413
        {
 
414
                uiS.listWidget_schedule -> addItem( Schedule[currentSchedule] -> GetName() );   //add item at schedule listt
 
415
                uiS.listWidget_schedule -> setCurrentRow(currentSchedule);
 
416
        }
 
417
        else    //take out the selected item from the list & insert a new one at the same position with the new name
 
418
                (uiS.listWidget_schedule -> currentItem()) -> setText(Schedule[currentSchedule] -> GetName());
 
419
 
 
420
        TotalSchedule = uiS.listWidget_schedule -> count();             //set the TotalSchedule to what it is now
 
421
 
 
422
        modifyCancelPressed();
 
423
}
 
424
 
 
425
// scheduleText=====================================================================================================
 
426
// returns the text displayed at the schedule list (eg execute profile default at .... every...)
 
427
QString scheduleDialog::scheduleText(QString name,int month,int monthday, int weekday, int hour, int minute)
 
428
{
 
429
                        QString tempProfileName = tr("Execute profile")+" '"+ name + "', "+tr("at")+" ";
 
430
                        if (hour < 10)
 
431
                                tempProfileName.append("0"+countStr.setNum(hour) + ":");
 
432
                        else
 
433
                                tempProfileName.append(countStr.setNum(hour) + ":");
 
434
                        if (minute < 10) 
 
435
                                tempProfileName.append("0"+countStr.setNum(minute));
 
436
                        else
 
437
                                tempProfileName.append(countStr.setNum(minute));
 
438
                        tempProfileName.append(", "+tr("every")+" ");
 
439
 
 
440
                        if ((weekday==0) && (monthday== 0))
 
441
                                tempProfileName.append(tr("day"));
 
442
                        if (weekday==1)
 
443
                                tempProfileName.append(tr("Sunday"));
 
444
                        if (weekday==2)
 
445
                                tempProfileName.append(tr("Monday"));
 
446
                        if (weekday==3)
 
447
                                tempProfileName.append(tr("Tuesday"));
 
448
                        if (weekday==4)
 
449
                                tempProfileName.append(tr("Wednesday"));
 
450
                        if (weekday==5)
 
451
                                tempProfileName.append(tr("Thursday"));
 
452
                        if (weekday==6)
 
453
                                tempProfileName.append(tr("Friday"));
 
454
                        if (weekday==7)
 
455
                                tempProfileName.append(tr("Saturday"));
 
456
 
 
457
                        if (monthday != 0) 
 
458
                        {
 
459
                                if (weekday!=0)
 
460
                                        tempProfileName.append(" "+tr("& on the")+" ");
 
461
 
 
462
                                tempProfileName.append(countStr.setNum(monthday));
 
463
                                
 
464
                                if (monthday == 1) tempProfileName.append(tr("st","as in 1st day"));
 
465
                                if (monthday == 2) tempProfileName.append(tr("nd","as in 2nd day"));
 
466
                                if (monthday == 3) tempProfileName.append(tr("rd","as in 3rd day"));
 
467
                                if ((monthday > 3) && (monthday < 8)) tempProfileName.append(tr("th","as in 4th, 5th, 6th or 7th day"));
 
468
                                if (monthday == 8) tempProfileName.append(tr("th","as in 8th day"));
 
469
                                if (monthday == 9) tempProfileName.append(tr("th","as in 9th day"));
 
470
                                if (monthday == 10) tempProfileName.append(tr("th","as in 10th day"));
 
471
                                if (monthday == 11) tempProfileName.append(tr("th","as in 11th day"));
 
472
                                if ((monthday > 11) && (monthday < 20)) tempProfileName.append(tr("th","as in 12th....19th day"));
 
473
                                if (monthday == 20) tempProfileName.append(tr("th","as in 20th day"));
 
474
                                if (monthday == 21) tempProfileName.append(tr("st","as in 21st day"));
 
475
                                if (monthday == 22) tempProfileName.append(tr("nd","as in 22nd day"));
 
476
                                if (monthday == 23) tempProfileName.append(tr("rd","as in 23rd day"));
 
477
                                if ((monthday > 23) && (monthday < 31)) tempProfileName.append(tr("th","as in 24th...30th day"));
 
478
                                if (monthday == 31) tempProfileName.append(tr("st","as in 31st day"));
 
479
                                tempProfileName.append(" "+tr("day"));
 
480
                        }
 
481
                        if (month == 0)
 
482
                        {
 
483
                                tempProfileName.append(" "+tr("of any month"));
 
484
                                return tempProfileName;
 
485
                        }
 
486
                        if (month == 1)
 
487
                                tempProfileName.append(" "+tr("of")+" "+tr("January","full phrase: 'of January'"));
 
488
                        if (month == 2)
 
489
                                tempProfileName.append(" "+tr("of")+" "+tr("February","full phrase: 'of February'"));
 
490
                        if (month == 3)
 
491
                                tempProfileName.append(" "+tr("of")+" "+tr("March","full phrase: 'of March'"));
 
492
                        if (month == 4)
 
493
                                tempProfileName.append(" "+tr("of")+" "+tr("April","full phrase: 'of April'"));
 
494
                        if (month == 5)
 
495
                                tempProfileName.append(" "+tr("of")+" "+tr("May","full phrase: 'of May'"));
 
496
                        if (month == 6)
 
497
                                tempProfileName.append(" "+tr("of")+" "+tr("June","full phrase: 'of June'"));
 
498
                        if (month == 7)
 
499
                                tempProfileName.append(" "+tr("of")+" "+tr("July","full phrase: 'of July'"));
 
500
                        if (month == 8)
 
501
                                tempProfileName.append(" "+tr("of")+" "+tr("August","full phrase: 'of August'"));
 
502
                        if (month == 9)
 
503
                                tempProfileName.append(" "+tr("of")+" "+tr("September","full phrase: 'of September'"));
 
504
                        if (month == 10)
 
505
                                tempProfileName.append(" "+tr("of")+" "+tr("October","full phrase: 'of October'"));
 
506
                        if (month == 11)
 
507
                                tempProfileName.append(" "+tr("of")+" "+tr("November","full phrase: 'of November'"));
 
508
                        if (month == 12)
 
509
                                tempProfileName.append(" "+tr("of")+" "+tr("December","full phrase: 'of December'"));
 
510
 
 
511
                        return tempProfileName;
 
512
}
 
513
 
 
514
// createCron=====================================================================================================
 
515
//creates a cron file & updates the user's crontab
 
516
void scheduleDialog::createCron()
 
517
{
 
518
        QString luckyCommand = "/usr/bin/luckybackup -c --no-questions ";
 
519
        QString ScheduleLine = "";
 
520
        QString ProfilePath="";
 
521
        QString luckyEntryStart = "\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 
522
        QString luckyEntryEnd = "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
 
523
 
 
524
        // copy user's crontab to QString CronTab & delete luckybackup's entries
 
525
        CronTab = "";
 
526
        cronProcess -> start ("crontab",QStringList() << "-l"); // execute crontab -l command & capture output to QString CronTab
 
527
        cronProcess -> waitForStarted();
 
528
        cronProcess -> waitForFinished();
 
529
        int x = CronTab.indexOf(luckyEntryStart);
 
530
        int y = CronTab.indexOf(luckyEntryEnd) + luckyEntryEnd.size() - x;
 
531
        CronTab.remove  (x,y);                                  //remove luckyBackup entries from CronTab
 
532
        //CronTab.append("\n");
 
533
 
 
534
        QFile cronFile (cronfilename);
 
535
        if (cronFile.exists())          //if a cronFile exists delete it
 
536
                cronFile.remove();
 
537
 
 
538
        if (!cronFile.open(QIODevice::WriteOnly | QIODevice::Text))     //create a new cronFile as textfile
 
539
                QMessageBox::information(this, appName, tr("Unable to create cron file")+" !!<br><br>"+ cronFile.errorString());
 
540
        else            //write some stuff to schedule file
 
541
        {
 
542
                QTextStream out(&cronFile);
 
543
                out << CronTab;         //append the old user's crontab first (minus the luckybackup entries)
 
544
                out << luckyEntryStart; //# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
 
545
                currentSchedule = 0;
 
546
                while (currentSchedule < TotalSchedule)
 
547
                {
 
548
                        ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMinute()) + " ");   //minute
 
549
                        ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetHour()) + " ");     //hour
 
550
                        if (Schedule[currentSchedule] -> GetMonthDay() == 0)                                    //Day of month
 
551
                                ScheduleLine.append("* ");
 
552
                        else
 
553
                                ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMonthDay()) + " ");
 
554
                        if (Schedule[currentSchedule] -> GetMonth() == 0)                                       //Month
 
555
                                ScheduleLine.append("* ");
 
556
                        else
 
557
                                ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetMonth()) + " ");
 
558
                        if (Schedule[currentSchedule] -> GetWeekDay() == 0)                                     //Day of week
 
559
                                ScheduleLine.append("*  ");
 
560
                        else
 
561
                                ScheduleLine.append(countStr.setNum(Schedule[currentSchedule] -> GetWeekDay() -1 ) + "  ");
 
562
 
 
563
                        ScheduleLine.append(luckyCommand);                              //append command luckybackup 
 
564
 
 
565
                        if (Schedule[currentSchedule] -> GetSkipCritical())                     //append --skip-critical
 
566
                                ScheduleLine.append("--skip-critical ");
 
567
                        ProfilePath = Schedule[currentSchedule] -> GetProfilePath();
 
568
                        ProfilePath.replace(" ", "\\ ");
 
569
                        ScheduleLine.append(ProfilePath);       //append profile full path
 
570
                        ScheduleLine.append(" >> " + cronlogfilename + " 2>&1"+ "\n");          //append logfile output
 
571
 
 
572
                        out << ScheduleLine;
 
573
 
 
574
                        ScheduleLine = "";
 
575
                        currentSchedule++;
 
576
                }
 
577
                out << luckyEntryEnd;   //# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ end of luckybackup entries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
 
578
        }
 
579
 
 
580
        cronFile.close();
 
581
 
 
582
        cronProcess -> start ("crontab",QStringList() << cronfilename); // execute crontab command to replace the user's crontab with luckybackup cron
 
583
        cronProcess -> waitForStarted();
 
584
        cronProcess -> waitForFinished();
 
585
 
 
586
        //if the new user's crontab contains no actual schedules > remove user's crontab completely
 
587
        CronTab = "";
 
588
        cronProcess -> start ("crontab",QStringList() << "-l"); // execute crontab -l command & capture output to QString CronTab
 
589
        cronProcess -> waitForStarted();
 
590
        cronProcess -> waitForFinished();
 
591
        if (CronTab == luckyEntryStart + luckyEntryEnd)
 
592
        {
 
593
                cronProcess -> start ("crontab",QStringList() << "-r"); // remove user's crontab
 
594
                cronProcess -> waitForStarted();
 
595
                cronProcess -> waitForFinished();
 
596
        }
 
597
}
 
598
 
 
599
// appendCrontabOutput=====================================================================================================
 
600
//appends the user's crontab to a QString
 
601
void scheduleDialog::appendCrontabOutput()
 
602
{
 
603
        CronTab = QString(cronProcess -> readAllStandardOutput());
 
604
}
 
605
 
 
606
// end of scheduleDialog.cpp ---------------------------------------------------------------------------
 
607