Added spanish translation
[timedsilencer] / profileevent.h
1 /*
2  * This file is part of TimedSilencer.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #ifndef PROFILEEVENT_H
19 #define PROFILEEVENT_H
20
21 #include <QTime>
22 #include <QVariant>
23 #include <QHash>
24 #include <QCryptographicHash>
25 #include <QStringList>
26 #include <QSettings>
27
28 enum WeekDay {NEVER, MON, TUE, WED, THU, FRI, SAT, SUN, EVERY_DAY};
29
30 class ProfileEvent : public QObject {
31   Q_OBJECT
32 private:
33   QByteArray id;
34
35 public:
36   QTime from_time;
37   QTime to_time;
38   bool activated;
39   QList<int> days;
40   QList<long> alarmd_cookies;
41
42   ProfileEvent() {
43     // Activate as a default
44     activated = true;
45   }
46
47   QByteArray getID() {
48     if(id.isEmpty()) {
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));
53       id = hasher.result();
54     }
55     //qDebug("getID(): %s", id.constData());
56     return id;
57   }
58
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;
66     m["days"] = var_days;
67     QVariantList var_cookies;
68     foreach(const long& c, alarmd_cookies) var_cookies << (qlonglong)c;
69     m["alarmd_cookies"] = var_cookies;
70     return m;
71   }
72
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;
80     }
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);
86     } else {
87       // to_time is the next day
88       in_silent_mode = (ctime >= from_time || (ctime < from_time && ctime < to_time));
89     }
90     return in_silent_mode;
91   }
92
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();
103     return pe;
104   }
105
106   static QString formatDays(QList<int> selection) {
107     qDebug("CurrentValueText() called");
108     if(selection.isEmpty() || selection.contains(NEVER)) {
109       return tr("Never");
110     }
111     if(selection.contains(EVERY_DAY)) {
112       return tr("Every day");
113     }
114     QStringList selectedDays;
115     foreach(const int &i, selection) {
116       selectedDays << QDate::shortDayName(i);
117     }
118     return selectedDays.join(", ");
119   }
120
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));
126     }
127     return 0;
128   }
129
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);
140       delete pe;
141     }
142   }
143
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);
155       delete pe;
156     }
157   }
158
159   static void setStatus(QByteArray myid, bool status) {
160     QSettings settings("TimedSilencer", "TimedSilencer");
161     QHash<QString, QVariant> events = settings.value("events").toHash();
162     Q_ASSERT(events.contains(myid));
163     if(events.contains(myid)) {
164       qDebug("Setting event status in QSettings");
165       ProfileEvent *pe = load(events.value(myid));
166       pe->activated = status;
167       events[myid] = pe->save();
168       settings.setValue("events", events);
169       delete pe;
170     }
171   }
172
173 };
174
175 #endif // PROFILEEVENT_H