~zsombi/ubuntu-ui-toolkit/fix-1217650

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/alarmmanager_p.h

  • Committer: Tarmac
  • Author(s): Zsombor Egri
  • Date: 2013-08-19 10:53:19 UTC
  • mfrom: (679.1.10 alarms-api)
  • Revision ID: tarmac-20130819105319-4nwhirmnh8ow7bcr
Alarm management QML API.

Approved by Christian Dywan, PS Jenkins bot, Zsombor Egri.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Author: Zsombor Egri <zsombor.egri@canonical.com>
 
17
 */
 
18
 
 
19
#ifndef ALARMMANAGER_H
 
20
#define ALARMMANAGER_H
 
21
 
 
22
#include <QtCore/QObject>
 
23
#include <QtQml/QQmlListProperty>
 
24
#include <QtCore/QSet>
 
25
 
 
26
#include "ucalarm.h"
 
27
 
 
28
class AlarmData {
 
29
public:
 
30
    enum Change {
 
31
        NoChange    = 0,
 
32
        Enabled     = 0x0001,
 
33
        Date        = 0x0002,
 
34
        Message     = 0x0004,
 
35
        Sound       = 0x0008,
 
36
        Type        = 0x0010,
 
37
        Days        = 0x0020
 
38
    };
 
39
 
 
40
    AlarmData()
 
41
        : changes(0)
 
42
        , type(UCAlarm::OneTime)
 
43
        , days(UCAlarm::AutoDetect)
 
44
        , enabled(true)
 
45
    {
 
46
    }
 
47
    AlarmData(const AlarmData &other)
 
48
        : changes(0)
 
49
        , cookie(other.cookie)
 
50
        , date(other.date)
 
51
        , message(other.message)
 
52
        , type(other.type)
 
53
        , days(other.days)
 
54
        , enabled(other.enabled)
 
55
    {
 
56
    }
 
57
 
 
58
    bool compare(const AlarmData &other)
 
59
    {
 
60
        // cookie, sound, and enabled do not count on alarm equality
 
61
        return date == other.date
 
62
                && message == other.message
 
63
                && type == other.type
 
64
                && days == other.days;
 
65
    }
 
66
 
 
67
    bool operator==(const AlarmData &other)
 
68
    {
 
69
        return compare(other);
 
70
    }
 
71
 
 
72
    QSet<Qt::DayOfWeek> daysToSet() const
 
73
    {
 
74
        QSet<Qt::DayOfWeek> result;
 
75
        for (Qt::DayOfWeek day = Qt::Monday; day <= Qt::Sunday; day = static_cast<Qt::DayOfWeek>(static_cast<int>(day) + 1)) {
 
76
            if (days & (1 << (static_cast<int>(day) - 1)))
 
77
                result << day;
 
78
        }
 
79
        return result;
 
80
    }
 
81
 
 
82
    void daysFromSet(QSet<Qt::DayOfWeek> set)
 
83
    {
 
84
        days = 0;
 
85
        QSetIterator<Qt::DayOfWeek> i(set);
 
86
        while (i.hasNext()) {
 
87
            int day = static_cast<int>(i.next());
 
88
            days |= static_cast<UCAlarm::DayOfWeek>(1 << (day - 1));
 
89
        }
 
90
    }
 
91
 
 
92
    static QHash<int, QByteArray> roles() {
 
93
        QHash<int, QByteArray> hash;
 
94
        int i = 0;
 
95
        hash.insert(i++, QByteArray("message"));
 
96
        hash.insert(i++, QByteArray("date"));
 
97
        hash.insert(i++, QByteArray("type"));
 
98
        hash.insert(i++, QByteArray("daysOfWeek"));
 
99
        hash.insert(i++, QByteArray("sound"));
 
100
        hash.insert(i++, QByteArray("enabled"));
 
101
        return hash;
 
102
    }
 
103
 
 
104
    QVariant roleData(int role) const {
 
105
        switch (role) {
 
106
        case 0: return message;
 
107
        case 1: return date;
 
108
        case 2: return type;
 
109
        case 3: return static_cast<int>(days);
 
110
        case 4: return sound;
 
111
        case 5: return enabled;
 
112
        default: return QVariant();
 
113
        }
 
114
    }
 
115
 
 
116
    unsigned int changes;
 
117
    QVariant cookie;
 
118
 
 
119
    // data members
 
120
    QDateTime date;
 
121
    QString message;
 
122
    QUrl sound;
 
123
    UCAlarm::AlarmType type;
 
124
    UCAlarm::DaysOfWeek days;
 
125
    bool enabled;
 
126
};
 
127
 
 
128
class AlarmManagerPrivate;
 
129
class AlarmManager : public QObject
 
130
{
 
131
    Q_OBJECT
 
132
public:
 
133
    explicit AlarmManager(QObject *parent = 0);
 
134
    ~AlarmManager();
 
135
 
 
136
    static AlarmManager &instance() {
 
137
        static AlarmManager instance;
 
138
        return instance;
 
139
    }
 
140
 
 
141
    QList<AlarmData> alarms() const;
 
142
 
 
143
    // invokables to be also accessible from QML
 
144
    int set(AlarmData &alarm, int &changes);
 
145
    int cancel(AlarmData &alarm);
 
146
 
 
147
Q_SIGNALS:
 
148
    void alarmsChanged();
 
149
    
 
150
private:
 
151
    Q_DISABLE_COPY(AlarmManager)
 
152
    Q_DECLARE_PRIVATE(AlarmManager)
 
153
    QScopedPointer<AlarmManagerPrivate> d_ptr;
 
154
};
 
155
 
 
156
#endif // ALARMMANAGER_H