Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bluez/Manager.py
blob: c04c1fac9476f423bcf639c5bdf6466eb68d3acf (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
# Manager.py
#
# Copyright (C) 2008 Vinicius Gomes <vcgomes [at] gmail [dot] com>
# Copyright (C) 2008 Li Dongyang <Jerry87905 [at] gmail [dot] 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import Adapter
import Agent
import errors
from utils import raise_dbus_error
from BaseInterface import BaseInterface

class Manager(BaseInterface):

    '''
    Pass the name of the mainloop as a string to __init__.
    The mainloop support could not be changed after the first init of Manager.
    Supported mainloops are gobject, pyqt4 and ecore, use your perferred one.
    And gobject is supported by dbus-python natively.
    If you wanna use pyqt4 or ecore, make sure you have python bindings for them.
    '''

    _mainloop_support = ''

    @raise_dbus_error
    def __init__(self, mainloop):
        if Manager._mainloop_support == '':
            self.__setup_event_loop(mainloop)
        elif Manager._mainloop_support != mainloop:
            raise errors.DBusMainLoopAlreadyExistsError("Already have " + Manager._mainloop_support)
        super(Manager, self).__init__('org.bluez.Manager', '/')
    # __init__

    def __setup_event_loop(self, mainloop):
        def raise_mainloop_not_found(mainloop):
            raise errors.DBusMainLoopModuleNotFoundError('Can not find mofule for ' + mainloop)
        def parse_gobject_mainloop(mainloop):
            try:
                import dbus.mainloop.glib
            except ImportError:
                raise_mainloop_not_found(mainloop)
            dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
            return True

        def parse_pyqt4_mainloop(mainloop):
            try:
                import dbus.mainloop.qt
            except ImportError:
                raise_mainloop_not_found(mainloop)
            dbus.mainloop.qt.DBusQtMainLoop(set_as_default=True)
            return True

        def parse_ecore_mainloop(mainloop):
            try:
                import e_dbus
            except ImportError:
                raise_mainloop_not_found(mainloop)
            e_dbus.DBusEcoreMainLoop(set_as_default=True)
            return True

        _supported_mainloops = {'gobject':parse_gobject_mainloop,
                                'pyqt4':parse_pyqt4_mainloop,
                                'ecore':parse_ecore_mainloop}
        if (not isinstance(mainloop, str)) or (mainloop not in _supported_mainloops):
            raise errors.DBusMainLoopNotSupportedError('Supported mainloops are gobject, pyqt4 and ecore')
        else:
            parse_mainloop = _supported_mainloops.get(mainloop)
            if parse_mainloop(mainloop):
                Manager._mainloop_support = mainloop
    # __setup_event_loop

    @raise_dbus_error
    def GetProperties(self):
        return self.GetInterface().GetProperties()
    # GetProperties

    @raise_dbus_error
    def DefaultAdapter(self):
        obj_path = self.GetInterface().DefaultAdapter()
        return Adapter.Adapter(obj_path)
    # DefaultAdapter

    @raise_dbus_error
    def FindAdapter(self, pattern):
        obj_path = self.GetInterface().FindAdapter(pattern)
        return Adapter.Adapter(obj_path)
    # FindAdapter

    @raise_dbus_error
    def ListAdapters(self):
        obj_paths = self.GetInterface().ListAdapters()
        adapters = []
        for obj_path in obj_paths:
            adapters.append(Adapter.Adapter(obj_path))
        return adapters
    # ListAdapters

    def CreateAgent(self, cls=Agent.Agent, obj_path='/org/bluez/Agent'):
        '''
        Paramater cls should be a custom sub-class of Agent.
        Paramater obj_path is the dbus object path for the agent, should start with '/'.
        Returns an instance of specified cls.
        '''
        if not issubclass(cls, Agent.Agent):
            raise TypeError('Expecting a subclass of Agent')
        return cls(obj_path)
    # CreateAgent
# Manager