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"
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 ====================================================== */
45 Bus_tp_new(PyTypeObject
*cls
, PyObject
*args
, PyObject
*kwargs
)
47 PyObject
*first
= NULL
, *mainloop
= NULL
;
53 static char *argnames
[] = {"address_or_type", "mainloop", NULL
};
55 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OO", argnames
,
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
;
69 Py_BEGIN_ALLOW_THREADS
70 ret
= dbus_bus_register(self
->conn
, &error
);
73 DBusPyException_ConsumeError(&error
);
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");
90 type
= PyInt_AsLong(first
);
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
);
102 Py_BEGIN_ALLOW_THREADS
103 conn
= dbus_bus_get_private(type
, &error
);
107 DBusPyException_ConsumeError(&error
);
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");
117 Bus_get_unique_name(Connection
*self
, PyObject
*args UNUSED
)
122 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self
->conn
);
123 Py_BEGIN_ALLOW_THREADS
124 name
= dbus_bus_get_unique_name(self
->conn
);
127 /* shouldn't happen, but C subtypes could have done something stupid */
128 PyErr_SetString(DBusPyException
, "Unable to retrieve unique name");
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
)
143 static PyTypeObject BusType
= {
144 PyObject_HEAD_INIT(NULL
)
146 "_dbus_bindings.BusImplementation", /*tp_name*/
157 0, /*tp_as_sequence*/
165 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
166 Bus_tp_doc
, /*tp_doc*/
169 0, /*tp_richcompare*/
170 0, /*tp_weaklistoffset*/
173 Bus_tp_methods
, /*tp_methods*/
176 DEFERRED_ADDRESS(&ConnectionType
), /*tp_base*/
183 Bus_tp_new
, /*tp_new*/
189 dbus_py_init_bus_types(void)
191 BusType
.tp_base
= &DBusPyConnection_Type
;
192 if (PyType_Ready(&BusType
) < 0) return 0;
197 dbus_py_insert_bus_types(PyObject
*this_module
)
199 if (PyModule_AddObject(this_module
, "BusImplementation",
200 (PyObject
*)&BusType
) < 0) return 0;
204 /* vim:set ft=c cino< sw=4 sts=4 et: */