summaryrefslogtreecommitdiff
path: root/src/interface.cpp
blob: 15d859659cf3e0beed51c97949b2c2f3367ae44a (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
160
161
162
163
164
165
166
167
168
169
/*
 *
 *  D-Bus++ - C++ bindings for D-Bus
 *
 *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
 *
 *
 *  This library 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 2.1 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <dbus-c++/debug.h>
#include <dbus-c++/interface.h>

#include "internalerror.h"

using namespace DBus;

Interface::Interface(const std::string &name)
  : _name(name)
{}

Interface::~Interface()
{}

InterfaceAdaptor *AdaptorBase::find_interface(const std::string &name)
{
  InterfaceAdaptorTable::const_iterator ii = _interfaces.find(name);

  return ii != _interfaces.end() ? ii->second : NULL;
}

InterfaceAdaptor::InterfaceAdaptor(const std::string &name)
  : Interface(name)
{
  debug_log("adding interface %s", name.c_str());

  _interfaces[name] = this;
}

Message InterfaceAdaptor::dispatch_method(const CallMessage &msg)
{
  const char *name = msg.member();

  MethodTable::iterator mi = _methods.find(name);
  if (mi != _methods.end())
  {
    return mi->second.call(msg);
  }
  else
  {
    return ErrorMessage(msg, DBUS_ERROR_UNKNOWN_METHOD, name);
  }
}

void InterfaceAdaptor::emit_signal(const SignalMessage &sig)
{
  SignalMessage &sig2 = const_cast<SignalMessage &>(sig);

  if (sig2.interface() == NULL)
    sig2.interface(name().c_str());

  _emit_signal(sig2);
}

Variant *InterfaceAdaptor::get_property(const std::string &name)
{
  PropertyTable::iterator pti = _properties.find(name);

  if (pti != _properties.end())
  {
    if (!pti->second.read)
      throw ErrorAccessDenied("property is not readable");

    return &(pti->second.value);
  }
  return NULL;
}

void InterfaceAdaptor::set_property(const std::string &name, Variant &value)
{
  PropertyTable::iterator pti = _properties.find(name);

  if (pti != _properties.end())
  {
    if (!pti->second.write)
      throw ErrorAccessDenied("property is not writeable");

    Signature sig = value.signature();

    if (pti->second.sig != sig)
      throw ErrorInvalidSignature("property expects a different type");

    pti->second.value = value;
    return;
  }
  throw ErrorFailed("requested property not found");
}

InterfaceProxy *ProxyBase::find_interface(const std::string &name)
{
  InterfaceProxyTable::const_iterator ii = _interfaces.find(name);

  return ii != _interfaces.end() ? ii->second : NULL;
}

InterfaceProxy::InterfaceProxy(const std::string &name)
  : Interface(name)
{
  debug_log("adding interface %s", name.c_str());

  _interfaces[name] = this;
}

bool InterfaceProxy::dispatch_signal(const SignalMessage &msg)
{
  const char *name = msg.member();

  SignalTable::iterator si = _signals.find(name);
  if (si != _signals.end())
  {
    si->second.call(msg);
    // Here we always return false because there might be
    // another InterfaceProxy listening for the same signal.
    // This way we instruct libdbus-1 to go on dispatching
    // the signal.
    return false;
  }
  else
  {
    return false;
  }
}

Message InterfaceProxy::invoke_method(const CallMessage &call)
{
  CallMessage &call2 = const_cast<CallMessage &>(call);

  if (call.interface() == NULL)
    call2.interface(name().c_str());

  return _invoke_method(call2);
}

bool InterfaceProxy::invoke_method_noreply(const CallMessage &call)
{
  CallMessage &call2 = const_cast<CallMessage &>(call);

  if (call.interface() == NULL)
    call2.interface(name().c_str());

  return _invoke_method_noreply(call2);
}