summaryrefslogtreecommitdiffstats
path: root/src/objectvalidation.cpp
blob: df8d3bca5c4a100a8b53cd9a827de0e1dcc167fb (plain)
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
/*
 * Copyright (C) 2013  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "objectvalidation.h"

#include "kolabevent.h"
#include "kolabtodo.h"
#include "kolabjournal.h"
#include "kolabcontact.h"
#include "kolabconfiguration.h"
#include "kolabfile.h"
#include "utils.h"
#include "tztable.h"
#include <boost/unordered_set.hpp>

namespace Kolab {

static boost::unordered_set<std::string> initializeTzSet() {
    boost::unordered_set<std::string> set;
    for (unsigned int i = 0; i < numOlsonTimezones; i++) {
        set.insert(olsonTimezones[i]);
    }
    return set;
}

const boost::unordered_set<std::string> tzSet = initializeTzSet();

bool isValid(const cDateTime &datetime)
{
    if (!datetime.isValid()) {
        return true;
    }
    const std::string tz = datetime.timezone();
    if (!tz.empty()) {
        if (datetime.isUTC() && !tz.empty()) {
            Utils::logMessage("A UTC datetime may not have a timezone", "", 0, Error);
            return false;
        }
        if (tzSet.find(tz) == tzSet.end()) {
            Utils::logMessage("Not a valid olson timezone: " + tz, "", 0, Error);
            return false;
        }
    }
    return true;
}

#define ASSERT(arg) \
    do {\
        if ( !(arg) ) { \
            Utils::logMessage(#arg " is false", __FILE__, __LINE__, Error); \
        } \
    } while(0)

#define ASSERTEQUAL(arg1, arg2) \
    do {\
        if ( (arg1) != (arg2) ) { \
            Utils::logMessage(#arg1 " != " #arg2, __FILE__, __LINE__, Error); \
        } \
    } while(0)

#define ASSERTEXISTING(arg) \
    do {\
        if ( !(arg).isValid() ) { \
            Utils::logMessage(#arg " is not set", __FILE__, __LINE__, Error); \
        } \
    } while(0)

#define ASSERTVALID(arg) \
    do {\
        if ( (arg).isValid() && !isValid((arg)) ) { \
            Utils::logMessage(#arg " is not valid", __FILE__, __LINE__, Error); \
        } \
    } while(0)

void validate(const Event &event)
{
    ASSERTVALID(event.created());
    if (event.created().isValid()) {
        ASSERT(event.created().isUTC());
        ASSERT(!event.created().isDateOnly());
    }
    ASSERTEXISTING(event.start());
    ASSERTVALID(event.start());
    ASSERTVALID(event.end());
    if (event.start().isValid() && event.end().isValid()) {
        ASSERTEQUAL(event.start().isDateOnly(), event.end().isDateOnly());
    }
}

void validate(const Todo& todo)
{
    ASSERTVALID(todo.created());
    if (todo.created().isValid()) {
        ASSERT(todo.created().isUTC());
        ASSERT(!todo.created().isDateOnly());
    }
    ASSERTVALID(todo.start());
    ASSERTVALID(todo.due());
    if (todo.start().isValid() && todo.due().isValid()) {
        ASSERTEQUAL(todo.start().isDateOnly(), todo.due().isDateOnly());
    }
}

void validate(const Journal& journal)
{
    ASSERTVALID(journal.created());
    if (journal.created().isValid()) {
        ASSERT(journal.created().isUTC());
        ASSERT(!journal.created().isDateOnly());
    }
    ASSERTVALID(journal.start());
}

void validate(const Contact& /* contact */)
{

}

void validate(const DistList& /* distlist */)
{

}

void validate(const Freebusy& /* freebusy */)
{

}

void validate(const Note& /* note */)
{

}

void validate(const Configuration& /* configuration */)
{

}

void validate(const File& /* file */)
{

}

}