File indexing completed on 2024-04-21 04:40:44

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KOPENINGHOURS_RULE_P_H
0008 #define KOPENINGHOURS_RULE_P_H
0009 
0010 #include "interval.h"
0011 #include "selectors_p.h"
0012 
0013 
0014 #include <memory>
0015 
0016 namespace KOpeningHours {
0017 
0018 enum class State { // must be in same order as Interval::State
0019     Invalid,
0020     Open,
0021     Closed,
0022     Unknown,
0023     Off // extension, not in Interval::State
0024 };
0025 
0026 /** Result of a rule evalution. */
0027 class RuleResult
0028 {
0029 public:
0030     Interval interval;
0031     enum Mode {
0032         Override,
0033         Merge,
0034     };
0035     Mode mode;
0036 };
0037 
0038 /** Opening hours expression rule. */
0039 class Rule
0040 {
0041 public:
0042     enum Type : short {
0043         NormalRule,
0044         AdditionalRule,
0045         FallbackRule,
0046         GuessRuleType,
0047     };
0048     enum StateFlags : char {
0049         NoFlags,
0050         Off // "off" was used rather than "closed"
0051     };
0052 
0053     Interval::State state() const;
0054     bool hasImplicitState() const;
0055     void setState(State state);
0056     void copyStateFrom(const Rule &otherRule);
0057     bool hasComment() const;
0058     void setComment(const char *str, int len);
0059     int requiredCapabilities() const;
0060     /** Empty rules contain no selectors, have no comment and only implicit state. */
0061     bool isEmpty() const;
0062     bool hasSmallRangeSelector() const;
0063     bool hasWideRangeSelector() const;
0064 
0065     RuleResult nextInterval(const QDateTime &dt, OpeningHoursPrivate *context) const;
0066     QByteArray toExpression() const;
0067 
0068     /** Amount of selectors for this rule. */
0069     int selectorCount() const;
0070 
0071     QString m_comment;
0072     QString m_wideRangeSelectorComment;
0073 
0074     std::unique_ptr<Timespan> m_timeSelector;
0075     std::unique_ptr<WeekdayRange> m_weekdaySelector;
0076     std::unique_ptr<Week> m_weekSelector;
0077     std::unique_ptr<MonthdayRange> m_monthdaySelector;
0078     std::unique_ptr<YearRange> m_yearSelector;
0079     bool m_seen_24_7 = false;
0080     bool m_colonAfterWideRangeSelector = false;
0081 
0082     StateFlags m_stateFlags = NoFlags;
0083     Type m_ruleType = NormalRule;
0084 
0085 private:
0086     Interval::State m_state = Interval::Invalid;
0087 
0088     enum { RecursionLimit = 64 };
0089     RuleResult nextInterval(const QDateTime &dt, OpeningHoursPrivate *context, int recursionBudget) const;
0090 };
0091 
0092 }
0093 
0094 #endif // KOPENINGHOURS_RULE_P_H