summaryrefslogtreecommitdiffstats
path: root/src/ctcron.h
blob: 4843dfc331ae99c7eaf088d23eb659f1ad81fa2e (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
/***************************************************************************
 *   CT Cron Header                                                        *
 *   --------------------------------------------------------------------  *
 *   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.                                   * 
 ***************************************************************************/

#ifndef CTCRON_H
#define CTCRON_H

// 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 <vector>
#include <string>
#include <iostream>

#include <tqstring.h> // Anarchy! -WABA 

class CTException;
class CTTask;
class CTVariable;

struct passwd;

/**
  * A user (encapsulation of a single crontab file).  Encapsulates
  * file i/o, parsing, tokenization, and natural language description.
  */
class CTCron
{
public:

/**
  * Constructs the scheduled tasks, environment variables from crontab
  * files and obtains some information about the user from the system.
  *
  * Default is to construct from the user's crontab.  Can also be called,
  * passing TRUE, to construct from the system crontab.  Throws an
  * exception if the crontab file can not be found, read, or parsed.
  */
  CTCron(bool _syscron = false, std::string _login = "");
/**
  * If you already have a struct passwd, use it instead. This
  * is never used for the system crontab.
  */
  CTCron(const struct passwd * _login = 0L);

/**
  * Copy one user's tasks and environement variables to another user.
  */
  void operator = (const CTCron& source);

/**
  * Parses crontab file format.
  */
  friend std::istream& operator >> (std::istream& inputStream, CTCron& cron);

/**
  * Tokenizes to crontab file format.
  */
  friend std::ostream& operator << (std::ostream& outputStream, const CTCron& cron);

/**
  * Apply changes.
  */
  void apply();

/**
  * Cancel changes.
  */
  void cancel();

/**
  * Indicates whether or not dirty.
  */
  bool dirty();

/**
  * Returns the PATH environment variable value.  A short cut to iterating
  * the tasks vector.
  */
  std::string path() const;

  /**
   * Returns whether an error has occured
   */
  bool isError() { return !error.isEmpty(); }
  
  /**
   * Return error description
   */
  TQString errorMessage() { TQString r = error; error = TQString::null; return r; }  
   

/**
  * Indicates whether or not the crontab belongs to the system.
  */
  const bool syscron;

/**
  * User  login.
  */
  std::string login;

/**
  * User real name.
  */
  std::string name;

/**
  * User's scheduled tasks.
  */
  std::vector<CTTask *> task;

/**
  * User's environment variables.  Note:  These are only environment variables
  * found in the user's crontab file and does not include any set in a 
  * login or shell script such as ".bash_profile".
  */
  std::vector<CTVariable *> variable;

/**
  * Destructor.
  */
  ~CTCron();

private:

/**
  * Can't copy a user!
  */
  CTCron(const CTCron& source);

  unsigned int initialTaskCount;
  unsigned int initialVariableCount;
  TQString           writeCommand;
  TQString           tmpFileName;

  TQString           error;

protected:
  // Initialize member variables from the struct passwd.
  bool initFromPasswd(const struct passwd *);
};

typedef std::vector<CTTask*>::iterator CTTaskIterator;
typedef std::vector<CTVariable*>::iterator CTVariableIterator;

#endif // CTCRON_H