2 * This file is part of TimedSilencer.
4 * TimedSilencer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * TimedSilencer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with TimedSilencer. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef PROFILEEVENT_H
19 #define PROFILEEVENT_H
24 #include <QCryptographicHash>
25 #include <QStringList>
28 enum WeekDay {NEVER, MON, TUE, WED, THU, FRI, SAT, SUN, EVERY_DAY};
30 class ProfileEvent : public QObject {
40 QList<long> alarmd_cookies;
43 // Activate as a default
49 QCryptographicHash hasher(QCryptographicHash::Md5);
50 hasher.addData(from_time.toString().toLocal8Bit());
51 hasher.addData(to_time.toString().toLocal8Bit());
52 foreach(int i, days) hasher.addData(QByteArray::number(i));
55 qDebug("getID(): %s", id.constData());
59 QVariant save() const {
60 QHash<QString, QVariant> m;
61 m["from_time"] = from_time;
62 m["to_time"] = to_time;
63 m["activated"] = activated;
64 QVariantList var_days;
65 foreach(const int& day, days) var_days << day;
67 QVariantList var_cookies;
68 foreach(const long& c, alarmd_cookies) var_cookies << (qlonglong)c;
69 m["alarmd_cookies"] = var_cookies;
73 bool affectsCurrentTime() {
74 if(!activated) return false;
75 Q_ASSERT(!days.empty());
76 if(days.empty()) days << NEVER;
77 if(days.first() != EVERY_DAY && days.first() != NEVER) {
78 // Check if the current week day is affected by this event
79 if(!days.contains(QDate::currentDate().dayOfWeek())) return false;
81 // Ok, it is the right day, are we in the interval?
82 bool in_silent_mode = false;
83 QTime ctime = QTime::currentTime();
84 if(from_time < to_time) {
85 in_silent_mode = (ctime > from_time && ctime < to_time);
87 // to_time is the next day
88 in_silent_mode = (ctime > from_time || (ctime < from_time && ctime < to_time));
90 return in_silent_mode;
93 static ProfileEvent* load(QVariant v) {
94 QHash<QString, QVariant> m = v.toHash();
95 ProfileEvent *pe = new ProfileEvent();
96 pe->from_time = m.value("from_time").toTime();
97 pe->to_time = m.value("to_time").toTime();
98 pe->activated = m.value("activated").toBool();
99 QVariantList var_days = m.value("days").toList();
100 foreach(const QVariant& var_day, var_days) pe->days << var_day.toInt();
101 QVariantList var_cookies = m.value("alarmd_cookies").toList();
102 foreach(const QVariant& var_c, var_cookies) pe->alarmd_cookies << var_c.toLongLong();
106 static QString formatDays(QList<int> selection) {
107 qDebug("CurrentValueText() called");
108 if(selection.isEmpty() || selection.contains(NEVER)) {
111 if(selection.contains(EVERY_DAY)) {
112 return tr("Every day");
114 QStringList selectedDays;
115 foreach(const int &i, selection) {
116 selectedDays << QDate::shortDayName(i);
118 return selectedDays.join(", ");
121 static ProfileEvent* findByID(QByteArray myid) {
122 QSettings settings("TimedSilencer", "TimedSilencer");
123 QHash<QString, QVariant> events = settings.value("events").toHash();
124 if(events.contains(myid)) {
125 return load(events.value(myid));
130 static void clearCookies(QByteArray myid) {
131 QSettings settings("TimedSilencer", "TimedSilencer");
132 QHash<QString, QVariant> events = settings.value("events").toHash();
133 Q_ASSERT(events.contains(myid));
134 if(events.contains(myid)) {
135 qDebug("Clearing event cookies in QSettings");
136 ProfileEvent *pe = load(events.value(myid));
137 pe->alarmd_cookies.clear();
138 events[myid] = pe->save();
139 settings.setValue("events", events);
144 static void setCookies(QByteArray myid, QList<long> cookies) {
145 QSettings settings("TimedSilencer", "TimedSilencer");
146 QHash<QString, QVariant> events = settings.value("events").toHash();
147 Q_ASSERT(events.contains(myid));
148 if(events.contains(myid)) {
149 qDebug("Setting event cookies in QSettings");
150 ProfileEvent *pe = load(events.value(myid));
151 Q_ASSERT(pe->alarmd_cookies.empty());
152 pe->alarmd_cookies = cookies;
153 events[myid] = pe->save();
154 settings.setValue("events", events);
161 #endif // PROFILEEVENT_H