Implement o.fd.DBus method wrappers in Python instead of C.
[dbus-python-phuang.git] / _dbus_bindings / bus.c
blob761b67ec7c1ea61bee05a0560d00154930da1c3f
1 /* Implementation of Bus, a subtype of Connection.
3 * Copyright (C) 2006 Collabora Ltd.
5 * Licensed under the Academic Free License version 2.1
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus_bindings-internal.h"
24 #include "conn-internal.h"
26 PyDoc_STRVAR(Bus_tp_doc,
27 "If the address is an int it must be one of the constants BUS_SESSION,\n"
28 "BUS_SYSTEM, BUS_STARTER; if a string, it must be a D-Bus address.\n"
29 "The default is BUS_SESSION.\n"
30 "\n"
31 "Constructor::\n"
32 "\n"
33 " BusImplementation([address: str or int])\n"
36 /* Bus definition =================================================== */
38 static PyTypeObject BusType;
40 #define Bus_Check(ob) PyObject_TypeCheck(ob, &BusType)
42 /* Bus methods ====================================================== */
44 static PyObject *
45 Bus_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
47 PyObject *first = NULL, *mainloop = NULL;
48 DBusConnection *conn;
49 DBusError error;
50 Connection *self;
51 dbus_bool_t ret;
52 long type;
53 static char *argnames[] = {"address_or_type", "mainloop", NULL};
55 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
56 &first, &mainloop)) {
57 return NULL;
60 dbus_error_init(&error);
62 if (first && PyString_Check(first)) {
63 /* It's a custom address. First connect to it, then register. */
65 self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs);
66 if (!self) return NULL;
67 TRACE(self);
69 Py_BEGIN_ALLOW_THREADS
70 ret = dbus_bus_register(self->conn, &error);
71 Py_END_ALLOW_THREADS
72 if (!ret) {
73 DBusPyException_ConsumeError(&error);
74 Py_DECREF(self);
75 return NULL;
78 return (PyObject *)self;
81 /* If the first argument isn't a string, it must be an integer
82 representing one of the well-known bus types. */
84 if (first && !PyInt_Check(first)) {
85 PyErr_SetString(PyExc_TypeError, "A string address or an integer "
86 "bus type is required");
87 return NULL;
89 if (first) {
90 type = PyInt_AsLong(first);
92 else {
93 type = DBUS_BUS_SESSION;
96 if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
97 && type != DBUS_BUS_STARTER) {
98 PyErr_Format(PyExc_ValueError, "Unknown bus type %d", (int)type);
99 return NULL;
102 Py_BEGIN_ALLOW_THREADS
103 conn = dbus_bus_get_private(type, &error);
104 Py_END_ALLOW_THREADS
106 if (!conn) {
107 DBusPyException_ConsumeError(&error);
108 return NULL;
110 return DBusPyConnection_NewConsumingDBusConnection(cls, conn, mainloop);
113 PyDoc_STRVAR(Bus_get_unique_name__doc__,
114 "get_unique_name() -> str\n\n"
115 "Return this application's unique name on this bus.\n");
116 static PyObject *
117 Bus_get_unique_name(Connection *self, PyObject *args UNUSED)
119 const char *name;
121 TRACE(self);
122 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
123 Py_BEGIN_ALLOW_THREADS
124 name = dbus_bus_get_unique_name(self->conn);
125 Py_END_ALLOW_THREADS
126 if (!name) {
127 /* shouldn't happen, but C subtypes could have done something stupid */
128 PyErr_SetString(DBusPyException, "Unable to retrieve unique name");
129 return NULL;
131 return PyString_FromString(name);
134 /* Bus type object ================================================== */
136 static struct PyMethodDef Bus_tp_methods[] = {
137 #define ENTRY(name, flags) {#name, (PyCFunction)Bus_##name, flags, Bus_##name##__doc__},
138 ENTRY(get_unique_name, METH_NOARGS)
139 #undef ENTRY
140 {NULL},
143 static PyTypeObject BusType = {
144 PyObject_HEAD_INIT(NULL)
145 0, /*ob_size*/
146 "_dbus_bindings.BusImplementation", /*tp_name*/
147 0, /*tp_basicsize*/
148 0, /*tp_itemsize*/
149 /* methods */
150 0, /*tp_dealloc*/
151 0, /*tp_print*/
152 0, /*tp_getattr*/
153 0, /*tp_setattr*/
154 0, /*tp_compare*/
155 0, /*tp_repr*/
156 0, /*tp_as_number*/
157 0, /*tp_as_sequence*/
158 0, /*tp_as_mapping*/
159 0, /*tp_hash*/
160 0, /*tp_call*/
161 0, /*tp_str*/
162 0, /*tp_getattro*/
163 0, /*tp_setattro*/
164 0, /*tp_as_buffer*/
165 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
166 Bus_tp_doc, /*tp_doc*/
167 0, /*tp_traverse*/
168 0, /*tp_clear*/
169 0, /*tp_richcompare*/
170 0, /*tp_weaklistoffset*/
171 0, /*tp_iter*/
172 0, /*tp_iternext*/
173 Bus_tp_methods, /*tp_methods*/
174 0, /*tp_members*/
175 0, /*tp_getset*/
176 DEFERRED_ADDRESS(&ConnectionType), /*tp_base*/
177 0, /*tp_dict*/
178 0, /*tp_descr_get*/
179 0, /*tp_descr_set*/
180 0, /*tp_dictoffset*/
181 0, /*tp_init*/
182 0, /*tp_alloc*/
183 Bus_tp_new, /*tp_new*/
184 0, /*tp_free*/
185 0, /*tp_is_gc*/
188 dbus_bool_t
189 dbus_py_init_bus_types(void)
191 BusType.tp_base = &DBusPyConnection_Type;
192 if (PyType_Ready(&BusType) < 0) return 0;
193 return 1;
196 dbus_bool_t
197 dbus_py_insert_bus_types(PyObject *this_module)
199 if (PyModule_AddObject(this_module, "BusImplementation",
200 (PyObject *)&BusType) < 0) return 0;
201 return 1;
204 /* vim:set ft=c cino< sw=4 sts=4 et: */