Source Code (Use browser search to find items of interest.)

Class Index

kcron'CTCron (./kdeadmin/kcron/ctcron.h:33)

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, string _login = "");

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

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

/**
  * Tokenizes to crontab file format.
  */
  friend ostream& operator << (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.
  */
  string path() const;

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

/**
  * User  login.
  */
  string login;

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

/**
  * User's scheduled tasks.
  */
  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".
  */
  vector<CTVariable *> variable;

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

private:

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

  unsigned int initialTaskCount;
  unsigned int initialVariableCount;
  string       writeCommand;
  string       tmpFileName;

};


kcron'CTCron::CTCron() (./kdeadmin/kcron/ctcron.cpp:28)

CTCron::CTCron(bool _syscron, string _login) :
  syscron(_syscron)
{
  int uid(getuid());

  char ofile[20] = "/tmp/crontab.XXXXXX";
  sprintf (ofile+13,"%d",getpid());

  tmpFileName = string(ofile);

  string readCommand;
  passwd *pwd;

  if (uid == 0)
  // root, so provide requested crontab
  {
    if (syscron)
    {
      readCommand  = "cat /etc/crontab > " + tmpFileName;
      writeCommand = "cat " + tmpFileName + " > /etc/crontab";
      login = i18n("(System Crontab)");
      name = "";
    }
    else
    {
      readCommand  = "crontab -u " + _login + " -l > " + tmpFileName;
      writeCommand = "crontab -u " + _login + " " + tmpFileName;
      pwd = getpwnam(_login.c_str());
      if (pwd == 0)
        throw CTExceptionIO();
      login = pwd->pw_name;
      name = pwd->pw_gecos;
    }
  }
  else
  // regular user, so provide user's own crontab
  {
    readCommand  = "crontab -l > " + tmpFileName;
    writeCommand = "crontab "      + tmpFileName;
    pwd  = getpwuid(uid);
      if (pwd == 0)
        throw CTExceptionIO();
    login = pwd->pw_name;
    name = pwd->pw_gecos;
  }

  if (name == "")
    name = login;

  // Don't throw exception, if can't be read, it means the user
  // doesn't have a crontab.
  if (system(readCommand.c_str()) == 0)
  {
    ifstream cronfile(tmpFileName.c_str());
    cronfile >> *this;
  }

  if (unlink(tmpFileName.c_str()) != 0)
    throw CTExceptionIO();

  initialTaskCount      = task.size();
  initialVariableCount  = variable.size();
}


kcron'CTCron::~CTCron() (./kdeadmin/kcron/ctcron.cpp:181)

CTCron::~CTCron()
{
  for (CTTaskIterator i = task.begin(); i != task.end(); i++)
    delete *i;
  for (CTVariableIterator i = variable.begin(); i != variable.end(); i++)
    delete *i;
}


kcron'CTCron::apply() (./kdeadmin/kcron/ctcron.cpp:189)

void CTCron::apply()
{
  // write to temp file
  ofstream cronfile(tmpFileName.c_str());
  cronfile << *this << flush;

  // install temp file into crontab
  if (system(writeCommand.c_str()) != 0)
    throw CTExceptionIO();

  // remove the temp file
  if(unlink(tmpFileName.c_str()) != 0)
    throw CTExceptionIO();

  // mark as applied
  for (CTTaskIterator i = task.begin(); i != task.end(); i++)
    (*i)->apply();

  for (CTVariableIterator i = variable.begin(); i != variable.end(); i++)
    (*i)->apply();
}


kcron'CTCron::cancel() (./kdeadmin/kcron/ctcron.cpp:211)

void CTCron::cancel()
{
  for (CTTaskIterator i = task.begin(); i != task.end(); i++)
    (*i)->cancel();

  for (CTVariableIterator i = variable.begin(); i != variable.end(); i++)
    (*i)->cancel();
}


kcron'CTCron::dirty() (./kdeadmin/kcron/ctcron.cpp:220)

bool CTCron::dirty()
{
  bool isDirty(false);

  if (initialTaskCount != task.size()) isDirty = true;

  if (initialVariableCount != variable.size()) isDirty = true;

  for (CTTaskIterator i = task.begin(); i != task.end(); i++)
    if ((*i)->dirty()) isDirty = true;

  for (CTVariableIterator i = variable.begin(); i != variable.end(); i++)
    if ((*i)->dirty()) isDirty = true;

  return isDirty;
}


kcron'CTCron::path() (./kdeadmin/kcron/ctcron.cpp:237)

string CTCron::path() const
{
  string path;

  for (CTVariableIterator var = (CTVariableIterator)variable.begin();
    var != variable.end(); var++)
  {
    if ((*var)->variable == "PATH")
    {
      path = (*var)->value;
    }
  }
  return path;
}