reverted varianttype
[profile/ivi/automotive-message-broker.git] / plugins / common / abstractdbusinterface.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef ABSTRACTDBUSINTERFACE_H
20 #define ABSTRACTDBUSINTERFACE_H
21
22 #include <string>
23 #include <functional>
24 #include <unordered_map>
25 #include <list>
26 #include <gio/gio.h>
27 #include <boost/any.hpp>
28 #include <nullptr.h>
29 #include "abstractpropertytype.h"
30 #include <abstractroutingengine.h>
31 #include "varianttype.h"
32
33 const uint getPid(const char *owner);
34
35 typedef std::vector<VariantType*> PropertyDBusMap;
36
37 class AbstractDBusInterface
38 {
39
40 public:
41         AbstractDBusInterface(std::string interfaceName, std::string objectName, GDBusConnection* connection);
42
43         virtual ~AbstractDBusInterface();
44
45         void setDBusConnection(GDBusConnection* connection)
46         {
47                 mConnection = connection;
48         }
49
50         void registerObject();
51         void unregisterObject();
52
53         void addProperty(VariantType* property);
54         virtual void updateValue(VariantType* property);
55
56         static PropertyList implementedProperties() { return mimplementedProperties; }
57
58         static std::list<AbstractDBusInterface *> getObjectsForProperty(std::string property);
59
60         static list<AbstractDBusInterface*> interfaces();
61
62         static std::vector<string> supportedInterfaces();
63
64         std::string interfaceName() { return mInterfaceName; }
65
66         bool implementsProperty(std::string property);
67
68         /*!
69          * \brief hasPropertyDBus
70          * \param attributeName, name of DBus property
71          * \return true if attributeName is supported by this interface
72          */
73         bool hasPropertyDBus(std::string attributeName)
74         {
75
76                 for(auto i : propertyDBusMap)
77                 {
78                         if(i->name() == attributeName)
79                         {
80                                 return true;
81                         }
82                 }
83
84                 return false;
85         }
86
87         std::string objectPath() { return mObjectPath; }
88
89         bool isSupported() { return supported; }
90
91         double time() { return mTime; }
92
93         VariantType* property(std::string propertyName)
94         {
95                 if(properties.find(propertyName) != properties.end())
96                         return properties[propertyName];
97                 return nullptr;
98         }
99
100         AbstractRoutingEngine* re;
101
102         void setObjectPath(std::string op)
103         {
104                 if(objectMap.find(op) != objectMap.end())
105                         objectMap.erase(op);
106
107                 mObjectPath = op;
108                 objectMap[mObjectPath] = this;
109         }
110
111         std::string objectName() { return mPropertyName; }
112
113         Zone::Type zone() { return zoneFilter; }
114
115         std::string source() { return mSourceFilter; }
116
117         std::unordered_map<std::string, VariantType*> getProperties() { return properties; }
118
119         bool isRegistered() { return regId > 0; }
120
121 protected:
122
123         void startRegistration();
124
125         static GVariant *getProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
126                                                                  const gchar *interfaceName, const gchar * propertyName, GError** error,
127                                                                  gpointer userData);
128         static gboolean setProperty(GDBusConnection * connection, const gchar * sender, const gchar *objectPath,
129                                                                 const gchar *interfaceName, const gchar * propertyName, GVariant *value,
130                                                                 GError** error, gpointer userData);
131
132         virtual void setProperty(std::string propertyName, GVariant * value);
133         virtual GVariant * getProperty(std::string propertyName);
134
135         void setTimeout(int timeout);
136
137         std::unordered_map<std::string, VariantType*> properties;
138
139         Zone::Type zoneFilter;
140
141         PropertyDBusMap propertyDBusMap;
142
143         bool supported;
144         double mTime;
145         std::string mSourceFilter;
146
147 private:
148         std::string mInterfaceName;
149         std::string mObjectPath;
150         std::string mPropertyName;
151         std::string introspectionXml;
152         GDBusConnection * mConnection;
153         static std::unordered_map<std::string, AbstractDBusInterface*> objectMap;
154         static PropertyList mimplementedProperties;
155         guint regId;
156         int mTimeout;
157
158 };
159
160 #endif // ABSTRACTDBUSINTERFACE_H