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

0001 /*
0002     CT Task 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 <QIcon>
0012 #include <QPair>
0013 #include <QString>
0014 #include <QStringList>
0015 
0016 #include "ctdom.h"
0017 #include "ctdow.h"
0018 #include "cthour.h"
0019 #include "ctminute.h"
0020 #include "ctmonth.h"
0021 
0022 /**
0023  * A scheduled task (encapsulation of crontab entry).  Encapsulates
0024  * parsing, tokenization, and natural language description.
0025  */
0026 class CTTask
0027 {
0028 public:
0029     /**
0030      * Constructs scheduled task from crontab format string.
0031      */
0032     explicit CTTask(const QString &tokenString, const QString &_comment, const QString &_userLogin, bool syscron = false);
0033 
0034     /**
0035      * Copy constructor.
0036      */
0037     CTTask(const CTTask &source);
0038 
0039     /**
0040      * Assignment operator.
0041      */
0042     CTTask &operator=(const CTTask &source);
0043 
0044     /**
0045      * Tokenizes scheduled task to crontab format.
0046      */
0047     QString exportTask();
0048 
0049     /**
0050      * Scheduling using the cron format.
0051      */
0052     QString schedulingCronFormat() const;
0053 
0054     /**
0055      * Mark changes as applied.
0056      */
0057     void apply();
0058 
0059     /**
0060      * Cancel changes.
0061      */
0062     void cancel();
0063 
0064     /**
0065      * Indicates whether or not the task has been modified.
0066      */
0067     bool dirty() const;
0068 
0069     /**
0070      * Returns natural language description of the task's schedule.
0071      */
0072     QString describe() const;
0073 
0074     /**
0075      * Indicates whether or not the task belongs to the system crontab.
0076      */
0077     bool isSystemCrontab() const;
0078 
0079     void setSystemCrontab(bool systemCrontab);
0080 
0081     QIcon commandIcon() const;
0082 
0083     /**
0084      * Internal methods
0085      */
0086     QPair<QString, bool> unQuoteCommand() const;
0087     QStringList separatePathCommand(const QString &command, bool quoted) const;
0088     QString decryptBinaryCommand(const QString &command) const;
0089 
0090     QString completeCommandPath() const;
0091 
0092     CTMonth month;
0093     CTDayOfMonth dayOfMonth;
0094     CTDayOfWeek dayOfWeek;
0095     CTHour hour;
0096     CTMinute minute;
0097 
0098     QString userLogin;
0099     QString command;
0100     QString comment;
0101 
0102     bool enabled;
0103     bool reboot;
0104 
0105 private:
0106     inline bool isSpaceAt(const QString &token, int pos)
0107     {
0108         if (pos >= token.length()) {
0109             return false;
0110         }
0111 
0112         if (token.at(pos) == QLatin1Char(' ')) {
0113             return true;
0114         }
0115 
0116         return false;
0117     }
0118 
0119     QString describeDayOfMonth() const;
0120     QString describeDayOfWeek() const;
0121     QString describeDateAndHours() const;
0122 
0123     QString createTimeFormat() const;
0124     QString createDateFormat() const;
0125 
0126     bool mSystemCrontab;
0127 
0128     QString mInitialUserLogin;
0129     QString mInitialCommand;
0130     QString mInitialComment;
0131     bool mInitialEnabled;
0132     bool mInitialReboot;
0133 };
0134