Added utility method for looking up flag values in Rtcom database. Whoever thought...
[qwerkisync] / EventTypes / RtcomEvent.cpp
1 /*
2  * Copyright (C) 2011, Jamie Thompson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This program 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 GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18
19 #include "RtcomEvent.h"
20
21 #include <QDebug>
22
23 #include <QHash>
24 #include <QString>
25
26 #include <rtcom-eventlogger/event.h>
27 #include <rtcom-eventlogger/eventlogger.h>
28
29 #include <stdexcept>
30
31 using namespace EventTypes;
32
33 RtcomEvent::RtcomEvent()
34 {
35 }
36
37 void RtcomEvent::freeRTComContents(RTComElEvent &event)
38 {
39         rtcom_el_event_free_contents(&event);
40 }
41
42 const unsigned int RtcomEvent::GetFlagValue(const QString &value) const
43 {
44         static QHash<QString, uint> valueLookups;
45
46         if(!valueLookups.contains(value))
47         {
48                 RTComEl *el(rtcom_el_new());
49                 if(NULL != el)
50                 {
51                         gint flagValue;
52                         if(-1 != (flagValue = rtcom_el_get_flag_value(el, value.toUtf8())))
53                                 valueLookups.insert(value, flagValue);
54
55                         g_object_unref(el);
56
57                         if(-1 == flagValue)
58                         {
59                                 throw std::runtime_error(
60                                         QString("Unable to find flag value '%1'' in rtcom database")
61                                                 .arg(value).toStdString());
62                         }
63                 }
64                 else
65                         qDebug() << "Failed to create event logger.";
66         }
67
68         return valueLookups.value(value);
69 }
70