tizen beta release
[platform/framework/web/wrt-plugins-common.git] / src / modules / tizen / DBus / Message.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @author          Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
18  */
19
20 #include "Message.h"
21 #include <cassert>
22 #include <Commons/Exception.h>
23
24 namespace WrtDeviceApis {
25 namespace DBus {
26 Message::Message(DBusMessage* message) : m_message(message)
27 {
28     assert(m_message && "Message cannot be NULL.");
29     addRef();
30 }
31
32 Message::Message(int type)
33 {
34     m_message = dbus_message_new(type);
35     if (!m_message) {
36         ThrowMsg(Commons::PlatformException, "Out of memory.");
37     }
38     addRef();
39 }
40
41 Message::~Message()
42 {
43     unref();
44 }
45
46 int Message::getType() const
47 {
48     return dbus_message_get_type(m_message);
49 }
50
51 std::string Message::getInterface() const
52 {
53     const char* interface = dbus_message_get_interface(m_message);
54     return (interface ? interface : std::string());
55 }
56
57 std::string Message::getPath() const
58 {
59     const char* path = dbus_message_get_path(m_message);
60     return (path ? path : std::string());
61 }
62
63 bool Message::isSignal(const std::string& name) const
64 {
65     return (dbus_message_is_signal(m_message,
66                                    getInterface().c_str(),
67                                    name.c_str())
68             == TRUE);
69 }
70
71 Message::ReadIterator Message::getReadIterator() const
72 {
73     return ReadIterator(new ReadIterator_(m_message));
74 }
75
76 void Message::addRef()
77 {
78     m_message = dbus_message_ref(m_message);
79 }
80
81 void Message::unref()
82 {
83     dbus_message_unref(m_message);
84 }
85
86 Message::ReadIterator_::ReadIterator_(DBusMessage* message)
87 {
88     assert(message && "Message cannot be NULL.");
89     m_valid = dbus_message_iter_init(message, &m_iterator);
90 }
91
92 bool Message::ReadIterator_::next()
93 {
94     if (!m_valid) {
95         Throw(Commons::OutOfRangeException);
96     }
97     return (m_valid = (dbus_message_iter_next(&m_iterator) == TRUE));
98 }
99
100 bool Message::ReadIterator_::isValid() const
101 {
102     return m_valid;
103 }
104
105 int Message::ReadIterator_::getArgType() const
106 {
107     if (!m_valid) {
108         Throw(Commons::OutOfRangeException);
109     }
110     return dbus_message_iter_get_arg_type(&m_iterator);
111 }
112
113 int Message::ReadIterator_::getInt() const
114 {
115     return static_cast<int>(getArgBasic<dbus_int32_t>(DBUS_TYPE_INT32));
116 }
117
118 std::string Message::ReadIterator_::getString() const
119 {
120     return getArgBasic<const char*>(DBUS_TYPE_STRING);
121 }
122
123 template<typename T>
124 T Message::ReadIterator_::getArgBasic(int type) const
125 {
126     if (getArgType() != type) {
127         ThrowMsg(Commons::ConversionException, "Type mismatch.");
128     }
129
130     if (!m_valid) {
131         Throw(Commons::OutOfRangeException);
132     }
133
134     T result;
135     dbus_message_iter_get_basic(&m_iterator, &result);
136     return result;
137 }
138 } // DBus
139 } // WrtDeviceApis