File indexing completed on 2024-05-05 05:48:31

0001 /*
0002     CT Cron Header
0003     --------------------------------------------------------------------
0004     SPDX-FileCopyrightText: 1999 Gary Meyer <gary@meyer.net>
0005     --------------------------------------------------------------------
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #pragma once
0010 
0011 #include <QList>
0012 #include <QString>
0013 #include <QStringList>
0014 
0015 class CTTask;
0016 class CTVariable;
0017 class CTInitializationError;
0018 
0019 class QFile;
0020 class QTextStream;
0021 
0022 struct passwd;
0023 
0024 #include "ctSaveStatus.h"
0025 
0026 class CommandLineStatus
0027 {
0028 public:
0029     int exitCode;
0030 
0031     QString commandLine;
0032 
0033     QString standardOutput;
0034     QString standardError;
0035 };
0036 
0037 class CommandLine
0038 {
0039 public:
0040     QString commandLine;
0041 
0042     QStringList parameters;
0043 
0044     CommandLineStatus execute();
0045 };
0046 
0047 class CTCronPrivate
0048 {
0049 public:
0050     /**
0051      * Indicates whether or not the crontab belongs to the system.
0052      */
0053     bool systemCron;
0054 
0055     /**
0056      * Indicates if this cron could have tasks and variables from different users.
0057      */
0058     bool multiUserCron;
0059 
0060     /**
0061      * Indicates whether or not the crontab belongs to the current user.
0062      */
0063     bool currentUserCron;
0064 
0065     /**
0066      * User login.
0067      */
0068     QString userLogin;
0069 
0070     /**
0071      * User real name.
0072      */
0073     QString userRealName;
0074 
0075     /**
0076      * User's scheduled tasks.
0077      */
0078     QList<CTTask *> task;
0079 
0080     /**
0081      * User's environment variables.  Note:  These are only environment variables
0082      * found in the user's crontab file and does not include any set in a
0083      * login or shell script such as ".bash_profile".
0084      */
0085     QList<CTVariable *> variable;
0086 
0087     int initialTaskCount;
0088     int initialVariableCount;
0089 
0090     /**
0091      * Contains path to the crontab binary file.
0092      */
0093     QString crontabBinary;
0094 };
0095 
0096 /**
0097  * A user (encapsulation of a single crontab file).  Encapsulates
0098  * file i/o, parsing, tokenization, and natural language description.
0099  */
0100 class CTCron
0101 {
0102 public:
0103     /**
0104      * If you already have a struct passwd, use it instead.
0105      * This is never used for the system crontab.
0106      */
0107     explicit CTCron(const QString &cronBinary, const struct passwd *userInfos, bool currentUserCron, CTInitializationError &ctInitializationError);
0108 
0109     /**
0110      * Destructor.
0111      */
0112     virtual ~CTCron();
0113 
0114     /**
0115      * Copy one user's tasks and environment variables to another user.
0116      */
0117     CTCron &operator=(const CTCron &source);
0118 
0119     virtual QList<CTTask *> tasks() const;
0120 
0121     virtual QList<CTVariable *> variables() const;
0122 
0123     virtual void addTask(CTTask *task);
0124     virtual void addVariable(CTVariable *variable);
0125 
0126     virtual void modifyTask(CTTask *task);
0127     virtual void modifyVariable(CTVariable *variable);
0128 
0129     virtual void removeVariable(CTVariable *variable);
0130     virtual void removeTask(CTTask *task);
0131 
0132     /**
0133      * Tokenizes to crontab file format.
0134      */
0135     QString exportCron() const;
0136 
0137     /**
0138      * Apply changes.
0139      */
0140     CTSaveStatus save();
0141 
0142     /**
0143      * Cancel changes.
0144      */
0145     void cancel();
0146 
0147     /**
0148      * Indicates whether or not dirty.
0149      */
0150     bool isDirty() const;
0151 
0152     /**
0153      * Returns the PATH environment variable value.
0154      * A short cut to iterating the tasks vector.
0155      */
0156     QString path() const;
0157 
0158     /**
0159      * Returns true if this cron could have tasks and variables from a different user.
0160      */
0161     bool isMultiUserCron() const;
0162 
0163     /**
0164      * Returns true if this cron is the system cron.
0165      */
0166     bool isSystemCron() const;
0167 
0168     /**
0169      * Returns true if this cron is the cron of the user who launched this app.
0170      */
0171     bool isCurrentUserCron() const;
0172 
0173     QString userLogin() const;
0174 
0175     /**
0176      * TODO
0177      * Bugged method for the moment (need to parse x,x,x,x data from /etc/passwd).
0178      */
0179     QString userRealName() const;
0180 
0181 protected:
0182     /**
0183      * Help constructor for subclasses.
0184      */
0185     explicit CTCron();
0186 
0187     // Initialize member variables from the struct passwd.
0188     bool initializeFromUserInfos(const struct passwd *userInfos);
0189 
0190 private:
0191     /**
0192      * Can't copy a user!
0193      */
0194     CTCron(const CTCron &source);
0195 
0196 protected:
0197     /**
0198      * Parses crontab file format.
0199      */
0200     void parseFile(const QString &fileName);
0201     void parseTextStream(QTextStream *stream);
0202 
0203     CTSaveStatus prepareSaveStatusError(const CommandLineStatus &commandLineStatus);
0204     // d probably stands for data.
0205     CTCronPrivate *const d;
0206 };
0207