File: blockalarmsattribute.cpp

package info (click to toggle)
akonadi-calendar 4:18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,808 kB
  • sloc: cpp: 14,079; xml: 36; makefile: 8; sh: 4
file content (167 lines) | stat: -rw-r--r-- 4,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
    Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>

    Copyright (C) 2010 Klarälvdalens Datakonsult AB,
        a KDAB Group company, info@kdab.net,
        author Tobias Koenig <tokoe@kdab.com>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "blockalarmsattribute.h"
#include <attributefactory.h>
#include <QByteArray>
#include <QDataStream>

using namespace Akonadi;

class Q_DECL_HIDDEN BlockAlarmsAttribute::Private
{
public:
    int audio = 1;
    int display = 1;
    int email = 1;
    int procedure = 1;
};

BlockAlarmsAttribute::BlockAlarmsAttribute()
    : d(new Private)
{
}

BlockAlarmsAttribute::~BlockAlarmsAttribute()
{
    delete d;
}

void BlockAlarmsAttribute::blockAlarmType(KCalCore::Alarm::Type type, bool block)
{
    switch (type) {
    case KCalCore::Alarm::Audio:
        d->audio = block;
        return;
    case KCalCore::Alarm::Display:
        d->display = block;
        return;
    case KCalCore::Alarm::Email:
        d->email = block;
        return;
    case KCalCore::Alarm::Procedure:
        d->procedure = block;
        return;
    default:
        return;
    }
}

void BlockAlarmsAttribute::blockEverything(bool block)
{
    blockAlarmType(KCalCore::Alarm::Audio, block);
    blockAlarmType(KCalCore::Alarm::Display, block);
    blockAlarmType(KCalCore::Alarm::Email, block);
    blockAlarmType(KCalCore::Alarm::Procedure, block);
}

bool BlockAlarmsAttribute::isAlarmTypeBlocked(KCalCore::Alarm::Type type) const
{
    switch (type) {
    case KCalCore::Alarm::Audio:
        return d->audio;
    case KCalCore::Alarm::Display:
        return d->display;
    case KCalCore::Alarm::Email:
        return d->email;
    case KCalCore::Alarm::Procedure:
        return d->procedure;
    default:
        return false;
    }
}

bool BlockAlarmsAttribute::isEverythingBlocked() const
{
    return isAlarmTypeBlocked(KCalCore::Alarm::Audio) && isAlarmTypeBlocked(KCalCore::Alarm::Display)
           && isAlarmTypeBlocked(KCalCore::Alarm::Email) && isAlarmTypeBlocked(KCalCore::Alarm::Procedure);
}

QByteArray BlockAlarmsAttribute::type() const
{
    static const QByteArray sType("BlockAlarmsAttribute");
    return sType;
}

BlockAlarmsAttribute *BlockAlarmsAttribute::clone() const
{
    BlockAlarmsAttribute *copy = new BlockAlarmsAttribute();
    copy->d->audio = d->audio;
    copy->d->display = d->display;
    copy->d->email = d->email;
    copy->d->procedure = d->procedure;

    return copy;
}

QByteArray BlockAlarmsAttribute::serialized() const
{
    QByteArray ba;
    QDataStream stream(&ba, QIODevice::WriteOnly);
    stream << d->audio;
    stream << d->display;
    stream << d->email;
    stream << d->procedure;

    return ba;
}

void BlockAlarmsAttribute::deserialize(const QByteArray &data)
{
    // Pre-4.11, default behavior
    if (data.isEmpty()) {
        d->audio = 1;
        d->display = 1;
        d->email = 1;
        d->procedure = 1;
    } else {
        QByteArray ba = data;
        QDataStream stream(&ba, QIODevice::ReadOnly);
        int i;
        stream >> i;
        d->audio = i;
        stream >> i;
        d->display = i;
        stream >> i;
        d->email = i;
        stream >> i;
        d->procedure = i;
    }
}

namespace
{

// Anonymous namespace; function is invisible outside this file.
bool dummy()
{
    Akonadi::AttributeFactory::registerAttribute<Akonadi::BlockAlarmsAttribute>();

    return true;
}

// Called when this library is loaded.
const bool registered = dummy();

} // namespace