TIVI-320: Add dbus-python to repos to support connman-test
[profile/ivi/dbus-python.git] / _dbus_bindings / bus.c
1 /* Implementation of Bus, a subtype of Connection.
2  *
3  * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25
26 #include "dbus_bindings-internal.h"
27 #include "conn-internal.h"
28
29 PyObject *
30 DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
31 {
32     PyObject *first = NULL, *mainloop = NULL;
33     DBusConnection *conn;
34     DBusError error;
35     Connection *self;
36     static char *argnames[] = {"address_or_type", "mainloop", NULL};
37
38     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
39                                      &first, &mainloop)) {
40         return NULL;
41     }
42
43     dbus_error_init(&error);
44
45     if (first && PyString_Check(first)) {
46         dbus_bool_t ret;
47
48         /* It's a custom address. First connect to it, then register. */
49
50         self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs);
51         if (!self) return NULL;
52         TRACE(self);
53
54         Py_BEGIN_ALLOW_THREADS
55         ret = dbus_bus_register(self->conn, &error);
56         Py_END_ALLOW_THREADS
57         if (!ret) {
58             DBusPyException_ConsumeError(&error);
59             Py_DECREF(self);
60             return NULL;
61         }
62
63         return (PyObject *)self;
64     }
65     else if (!first || PyInt_Check(first)) {
66         long type;
67         PyObject *libdbusconn;
68         PyObject *new_args;
69         PyObject *new_kwargs;
70
71         /* If the first argument isn't a string, it must be an integer
72         representing one of the well-known bus types. The default is
73         DBUS_BUS_SESSION. */
74
75         if (first) {
76             type = PyInt_AsLong(first);
77
78             if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
79                 && type != DBUS_BUS_STARTER) {
80                 PyErr_Format(PyExc_ValueError, "Unknown bus type %ld", type);
81                 return NULL;
82             }
83         }
84         else {
85             type = DBUS_BUS_SESSION;
86         }
87
88         Py_BEGIN_ALLOW_THREADS
89         conn = dbus_bus_get_private(type, &error);
90         Py_END_ALLOW_THREADS
91
92         if (!conn) {
93             DBusPyException_ConsumeError(&error);
94             return NULL;
95         }
96
97         libdbusconn = DBusPyLibDBusConnection_New (conn);
98         dbus_connection_unref (conn);
99
100         if (!libdbusconn)
101             return NULL;
102
103         new_args = PyTuple_Pack(2, libdbusconn, mainloop ? mainloop : Py_None);
104         Py_DECREF(libdbusconn);
105
106         if (!new_args) {
107             return NULL;
108         }
109
110         new_kwargs = PyDict_New();
111
112         if (!new_kwargs) {
113             Py_DECREF(new_args);
114             return NULL;
115         }
116
117         self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, new_args,
118                 new_kwargs);
119         Py_DECREF(new_args);
120         Py_DECREF(new_kwargs);
121
122         return (PyObject *)self;    /* whether NULL or not */
123     }
124     else {
125         PyErr_SetString(PyExc_TypeError, "A string address or an integer "
126                                          "bus type is required");
127         return NULL;
128     }
129 }
130
131 PyObject *
132 DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED)
133 {
134     const char *name;
135
136     TRACE(self);
137     DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
138     Py_BEGIN_ALLOW_THREADS
139     name = dbus_bus_get_unique_name(self->conn);
140     Py_END_ALLOW_THREADS
141     if (!name) {
142         return DBusPyException_SetString("This connection has no unique name "
143                                          "yet");
144     }
145     return PyString_FromString(name);
146 }
147
148 PyObject *
149 DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
150 {
151     const char *old_name, *new_name;
152
153     if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
154         return NULL;
155     }
156
157     TRACE(self);
158     DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
159
160     /* libdbus will assert if we try to set a unique name when there's
161      * already one, so we need to make sure that can't happen.
162      * (Thanks, libdbus.)
163      *
164      * The things that can set the unique name are:
165      * - this function - but we don't release the GIL, so only one instance of
166      *   this function can run
167      * - dbus_bus_get - but this is only called in a __new__ or __new__-like
168      *   function, so the new connection isn't available to other code yet
169      *   and this function can't be called on it
170      * - dbus_bus_register - same as dbus_bus_get
171      *
172      * Code outside dbus-python shouldn't be setting the unique name, because
173      * we're using a private connection; we have to trust the authors
174      * of mainloop bindings not to do silly things like that.
175      */
176     old_name = dbus_bus_get_unique_name(self->conn);
177     if (old_name != NULL) {
178         PyErr_Format(PyExc_ValueError, "This connection already has a "
179                      "unique name: '%s'", old_name);
180         return NULL;
181     }
182     dbus_bus_set_unique_name(self->conn, new_name);
183
184     Py_RETURN_NONE;
185 }
186
187 /* vim:set ft=c cino< sw=4 sts=4 et: */