summaryrefslogtreecommitdiffstats
path: root/src/cttask.cpp
blob: bd579689278678e76ff08744f40b9295c7a56675 (plain)
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
358
359
360
361
362
363
/***************************************************************************
 *   CT Task Implementation                                                *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Gary Meyer <gary@meyer.net>                       *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

// Do not introduce any TQt or KDE dependencies into the "CT"-prefixed classes.
// I want to be able to reuse these classes with another GUI toolkit. -GM 11/99

#include "cttask.h"


CTTask::CTTask(string tokStr, string _comment, bool _syscron) :
  syscron(_syscron)
{
  if (tokStr.substr(0,2) == "#\\")
  {
    tokStr = tokStr.substr(2,tokStr.length() - 2);
    enabled = false;
  }
  else if (tokStr.substr(0,1) == "#")
  {
    tokStr = tokStr.substr(1,tokStr.length() - 1);
    enabled = false;
  }
  else
    enabled = true;

  if (tokStr.substr(0,1) == "-")
  {
    tokStr = tokStr.substr(1,tokStr.length() - 1);
    silent = true;
  }
  else
  {
    silent = false;
  }
  
  if (tokStr.substr(0,1) == "@")
  {
    if (tokStr.substr(1,6) == "reboot")
    {
      // Dunno what to do with this...
      tokStr = "0 0 1 1 *"+tokStr.substr(7,tokStr.length()-1);
    }
    else if (tokStr.substr(1,6) == "yearly")
    {
      tokStr = "0 0 1 1 *"+tokStr.substr(7,tokStr.length()-1);
    }
    else if (tokStr.substr(1,8) == "annually")
    {
      tokStr = "0 0 1 1 *"+tokStr.substr(9,tokStr.length()-1);
    }
    else if (tokStr.substr(1,7) == "monthly")
    {
      tokStr = "0 0 1 * *"+tokStr.substr(8,tokStr.length()-1);
    }
    else if (tokStr.substr(1,6) == "weekly")
    {
      tokStr = "0 0 * * 0"+tokStr.substr(7,tokStr.length()-1);
    }
    else if (tokStr.substr(1,5) == "daily")
    {
      tokStr = "0 0 * * *"+tokStr.substr(6,tokStr.length()-1);
    }
    else if (tokStr.substr(1,6) == "hourly")
    {
      tokStr = "0 * * * *"+tokStr.substr(7,tokStr.length()-1);
    }
  }

  int spacepos(tokStr.find_first_of(" \t"));
  minute.initialize(tokStr.substr(0,spacepos));

  while(isspace(tokStr[spacepos+1])) spacepos++;
  tokStr     = tokStr.substr(spacepos+1,tokStr.length()-1);
  spacepos   = tokStr.find_first_of(" \t");
  hour.initialize(tokStr.substr(0,spacepos));

  while(isspace(tokStr[spacepos+1])) spacepos++;
  tokStr     = tokStr.substr(spacepos+1,tokStr.length()-1);
  spacepos   = tokStr.find_first_of(" \t");
  dayOfMonth.initialize(tokStr.substr(0,spacepos));

  while(isspace(tokStr[spacepos+1])) spacepos++;
  tokStr     = tokStr.substr(spacepos+1,tokStr.length()-1);
  spacepos   = tokStr.find_first_of(" \t");
  month.initialize(tokStr.substr(0,spacepos));

  while(isspace(tokStr[spacepos+1])) spacepos++;
  tokStr     = tokStr.substr(spacepos+1,tokStr.length()-1);
  spacepos   = tokStr.find_first_of(" \t");
  dayOfWeek.initialize(tokStr.substr(0,spacepos));

  if (syscron)
  {
    while(isspace(tokStr[spacepos+1])) spacepos++;
    tokStr     = tokStr.substr(spacepos+1,tokStr.length()-1);
    spacepos   = tokStr.find_first_of(" \t");
    user       = tokStr.substr(0,spacepos);
  }
  else
    user       = string("");

  //kdDebug() << "command before: " << tokStr << endl;
  command    = tokStr.substr(spacepos+1,tokStr.length()-1);
  // remove leading whitespace
  //kdDebug() << "command before: " << command << endl;
  while (command.find_first_of(" \t") == 0)
     command = command.substr(1,command.length()-1);
  //kdDebug() << "command after: " << command << endl;
  comment    = _comment;

  initialUser    = user;
  initialCommand = command;
  initialComment = comment;
  initialEnabled = enabled;
  initialSilent = silent;
}

CTTask::CTTask(const CTTask &source) :
  month(source.month),
  dayOfMonth(source.dayOfMonth),
  dayOfWeek(source.dayOfWeek),
  hour(source.hour),
  minute(source.minute),
  user(source.user),
  command(source.command),
  comment(source.comment),
  enabled(source.enabled),
  silent(source.silent),
  initialUser(""),
  initialCommand(""),
  initialComment(""),
  initialEnabled(true),
  initialSilent(false)
{
}

void CTTask::operator = (const CTTask& source)
{
  month          = source.month;
  dayOfMonth     = source.dayOfMonth;
  dayOfWeek      = source.dayOfWeek;
  hour           = source.hour;
  minute         = source.minute;
  user           = source.user;
  command        = source.command;
  comment        = source.comment;
  enabled        = source.enabled;
  silent         = source.silent;
  initialUser    = "";
  initialCommand = "";
  initialComment = "";
  initialEnabled = true;
  initialSilent  = false;
  return;
}

ostream& operator << (ostream& outputStream, const CTTask& task)
{
//  if (task.comment != string(""))
    outputStream << "# " << task.comment << "\n";

  if (!task.enabled)
    outputStream << "#\\";

  if (task.silent)
    outputStream << "-";

  outputStream << task.minute << " ";
  outputStream << task.hour << " ";
  outputStream << task.dayOfMonth << " ";
  outputStream << task.month << " ";
  outputStream << task.dayOfWeek << "\t";

  if (task.user != string(""))
    outputStream << task.user << "\t";

  outputStream << task.command << "\n";

  return outputStream;
}

void CTTask::apply()
{
  month.apply();
  dayOfMonth.apply();
  dayOfWeek.apply();
  hour.apply();
  minute.apply();
  initialUser    = user;
  initialCommand = command;
  initialComment = comment;
  initialEnabled = enabled;
  initialSilent = silent;
}

void CTTask::cancel()
{
  month.cancel();
  dayOfMonth.cancel();
  dayOfWeek.cancel();
  hour.cancel();
  minute.cancel();
  user    = initialUser;
  command = initialCommand;
  comment = initialComment;
  enabled = initialEnabled;
  silent = initialSilent;
}

bool CTTask::dirty() const
{
  return (month.dirty() || dayOfMonth.dirty() || dayOfWeek.dirty() ||
    hour.dirty() || minute.dirty() || (user != initialUser) ||
    (command != initialCommand) || (comment != initialComment) ||
    (enabled != initialEnabled) || (silent != initialSilent));
}

string CTTask::describe() const
{

  // Of the whole program, this method is probably the trickiest.
  //
  // This method creates the natural language description, such as
  // "At 1:00am, every Sun".
  //
  // First, I declare some strings for holding what can be internationalized.
  // Note the tokens such as "MONTHS".  Translators should reuse these
  // tokens in their translations.  See README.translators for more
  // information.
  //
  // Second, I get the descriptions from the component parts such as
  // days of the month.
  //
  // Third, I get hour/minute time combinations.  Although a little bit
  // awkward, I use the tm struct and strftime from <time.h>.  This
  // way this code is portable across all Unixes.
  //
  // Fourth, I know that "every day of the week" and "every day of the
  // month" simply makes "every day".
  //
  // Fifth and finally I do tag substitution to create the natural language
  // description.

  string tmFormat((const char *)i18n("%H:%M").local8Bit());
  string DOMFormat((const char *)i18n("Please translator, read the README.translators file in kcron's source code","DAYS_OF_MONTH of MONTHS").local8Bit());
  string DOWFormat((const char *)i18n("Really, read that file","every DAYS_OF_WEEK").local8Bit());
  string dateFormat((const char *)i18n("DOM_FORMAT as well as DOW_FORMAT").local8Bit());
  string timeFormat((const char *)i18n("At TIME").local8Bit());
  string format((const char *)i18n("TIME_FORMAT, DATE_FORMAT").local8Bit());

  // Get natural language description of day of month,
  // month name, and day of week.

  string DOMDesc(dayOfMonth.describe());
  string monthDesc(month.describe());
  string DOWDesc(dayOfWeek.describe());

  // Create time description.

  int total(minute.count()*hour.count());

  string timeDesc("");
  int count(0);

  for (int h = 0; h <= 23; h++)
    if (hour.get(h))
     for (int m = 0; m <= 59; m++)
       if (minute.get(m))
       {
         tm time;
         time.tm_sec  = 0;
         time.tm_min  = m;
         time.tm_hour = h;
         time.tm_mday = 0;
         time.tm_mon  = 0;
         time.tm_year = 0;
         time.tm_wday = 0;
         time.tm_yday = 0;
         time.tm_isdst= 0;

         char tmp[12];
         strftime(tmp, 12, tmFormat.c_str(), &time);
         string tmpStr = tmp;

         // remove leading space
         if (tmpStr.substr(0,1) == " ")
           tmpStr = tmpStr.substr(1,tmpStr.length()-1);

         timeDesc += tmpStr;
         count++;
         switch (total - count)
         {
           case 0:  break;
           case 1:  if (total > 2)
	   	      timeDesc += (const char *)i18n(", and ").local8Bit();
		    else
                      timeDesc += (const char *)i18n(" and ").local8Bit();
                    break;
           default: timeDesc += (const char*)i18n(", ").local8Bit();
         }
      }

  // "* * *" means truly every day.
  // Note: Languages may use different phrases to indicate
  // every day of month versus every day of week.

  if ((dayOfMonth.count() == 31) &&
    (dayOfWeek.count() == 7))
    dateFormat = (const char *)i18n("every day ").local8Bit();
  else
  {
    // Day of month not specified, so use day of week.
    if (dayOfMonth.count() == 31)
      dateFormat = "DOW_FORMAT";
    // Day of week not specified, so use day of month.
    if (dayOfWeek.count() == 7)
      dateFormat = "DOM_FORMAT";
  }

  // Replace tags to build natural language description.

  int pos(0);

  if ((pos = DOMFormat.find("DAYS_OF_MONTH")) > -1)
    DOMFormat.replace(pos,13,DOMDesc);

  if ((pos = DOMFormat.find("MONTHS")) > -1)
    DOMFormat.replace(pos,6,monthDesc);

  if ((pos = DOWFormat.find("DAYS_OF_WEEK")) > -1)
    DOWFormat.replace(pos,12,DOWDesc);

  if ((pos = dateFormat.find("DOM_FORMAT")) > -1)
    dateFormat.replace(pos,10,DOMFormat);

  if ((pos = dateFormat.find("DOW_FORMAT")) > -1)
    dateFormat.replace(pos,10,DOWFormat);

  if ((pos = timeFormat.find("TIME")) > -1)
    timeFormat.replace(pos,4,timeDesc);

  if ((pos = format.find("DATE_FORMAT")) > -1)
    format.replace(pos,11,dateFormat);

  if ((pos = format.find("TIME_FORMAT")) > -1)
    format.replace(pos,11,timeFormat);


  return format;
}

bool CTTask::system() const
{
  return syscron;
}