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

0001 /*
0002     CT Unit of Time Interval 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 
0014 /**
0015  * A cron table unit parser and tokenizer.
0016  * Parses/tokenizes unit such as "0-3,5,6,10-30/5"
0017  * Also provides default natural language description.
0018  */
0019 class CTUnit
0020 {
0021 protected:
0022     CTUnit(int min, int max, const QString &tokStr = QLatin1String(""));
0023 
0024     /**
0025      * Get default natural language description.
0026      */
0027     virtual QString genericDescribe(const QList<QString> &label) const;
0028 
0029 public:
0030     /**
0031      * Base initial image as empty and copy enabled intervals.
0032      */
0033     CTUnit(const CTUnit &source);
0034 
0035     /**
0036      * Destructor.
0037      */
0038     virtual ~CTUnit();
0039 
0040     /**
0041      * Copy enabled intervals.
0042      */
0043     CTUnit &operator=(const CTUnit &unit);
0044 
0045     /**
0046      * Tokenizes unit into string such as
0047      * "0,1,2,3,5,6,10,15,20,25,30".
0048      */
0049     virtual QString exportUnit() const;
0050 
0051     /**
0052      * Parses unit such as "0-3,5,6,10-30/5".
0053      * And initialize array of enabled intervals.
0054      */
0055     void initialize(const QString &tokStr = QLatin1String(""));
0056 
0057     /**
0058      * Lower bound.
0059      */
0060     int minimum() const;
0061 
0062     /**
0063      * Upper bound.
0064      */
0065     int maximum() const;
0066 
0067     /**
0068      * Accessor.
0069      */
0070     bool isEnabled(int pos) const;
0071 
0072     bool isAllEnabled() const;
0073 
0074     void setEnabled(int pos, bool value);
0075 
0076     /**
0077      * Indicates whether enabled intervals have been modified.
0078      */
0079     bool isDirty() const;
0080 
0081     /**
0082      * Total count of enabled intervals.
0083      */
0084     int enabledCount() const;
0085 
0086     /**
0087      * Mark changes as applied.
0088      */
0089     void apply();
0090 
0091     /**
0092      * Mark cancel changes and revert to initial or last applied
0093      * image.
0094      */
0095     void cancel();
0096 
0097     /**
0098      * Find a period in enabled values
0099      * If no period has been found, return 0
0100      */
0101     int findPeriod(const QList<int> &periods) const;
0102 
0103 protected:
0104     /**
0105      * Parses unit such as "0-3,5,6,10-30/5".
0106      * Does not initialize array of enabled intervals.
0107      */
0108     void parse(const QString &tokenString = QLatin1String(""));
0109 
0110 private:
0111     int mMin;
0112     int mMax;
0113 
0114     int fieldToValue(const QString &entry) const;
0115     bool mDirty;
0116 
0117     QList<bool> mEnabled;
0118     QList<bool> mInitialEnabled;
0119 
0120     QString mInitialTokStr;
0121 
0122 public:
0123     /**
0124      * Constant indicating short format.
0125      */
0126     static const bool shortFormat = false;
0127 
0128     /**
0129      * Constant indicating long format.
0130      */
0131     static const bool longFormat = true;
0132 };
0133