Added the stament import for PlayList service
[zukebox] / zukebox_server / src / server / zukebox_server.py
1
2 from ziface import ZIface
3 import dbus.service
4 import dbus.mainloop.glib
5
6 from brisa.core.reactors import GLib2Reactor
7 reactor = GLib2Reactor()
8
9 from brisa.core import log
10 from brisa.core import config
11 from brisa.upnp.device import Device, Service
12 from brisa.upnp.services.cds import ContentDirectory
13 from brisa.upnp.services.connmgr import ConnectionManagerServer
14
15 from zukebox_server.services.playlist.zukebox_playlist import PlayList
16
17 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
18
19 class ZukeBoxServer(ZIface, dbus.service.Object):
20     """
21     Introduction
22     ============
23     ZukeBox is an abstraction for a JukeBox.
24     How works?
25         In a JukeBox somebody pay and choose a song for play, this way the
26     JukeBox should have default set of songs. ZukeBox has the same idea,
27     except for payment ;) , you can choose a song availble in the server 
28     called ZukeBox Server or send a request for server to play your music, 
29     
30     Thus the clientes should be scan the network and get services availble 
31     in ZukeBox Server.
32     Services availble are:
33         1. Show the songs availble in the ZukeBox Server.
34         2. Play a song availble.
35         3. Play a song sent by a control point.
36     Some services will be availble too throught dbus.service a method for 
37     rescan the default paths and save in the database.
38     """
39
40     DBUS_SERVICE_NAME = "br.org.zagaia"
41     DBUS_OBJ_PATH = "/br/org/zagaia/ZukeBox"
42     DBUS_IFACE = "br.org.zagaia.ZukeBox"
43
44     plugins_folder = config.get_parameter("zukebox_server", "plugins")
45     plugins_module_path = "zukebox_server.plugins"
46
47     def __init__(self, _server_name, _listen_url):
48         """ ZukeBox Server Construct
49         @param _server_name: Name of the ZukeBox Server
50         @param _listen_url: url to listen for requests
51
52         @type _server_name: string
53         @type _listen_url: string
54         """
55         ZIface.__init__(self)
56         bus = dbus.SessionBus()
57         busname = dbus.service.BusName(self.DBUS_SERVICE_NAME, bus=bus)
58
59         dbus.service.Object.__init__(self, busname, self.DBUS_OBJ_PATH)
60         self.server_name = _server_name
61         self.listen_url = _listen_url
62         self.device = None
63         self.cds = None
64
65     def _create_device(self):
66         model_name = "ZukeBox Server version 0.1"
67         project_page = "http://portal.fucapi.edu.br/nepcomp/zagaia"
68         serial_no = '0000010'
69         model_description = 'A UPnP Audio Server for ZukeBox'
70
71         self.device = Device('urn:schemas-upnp-org:device:ZukeBoxServer:1',
72                 self.server_name, force_listen_url=self.listen_url,
73                 manufacturer="Zagaia Laboratory and INdT Brazil",
74                 manufacturer_url=project_page,
75                 model_description=model_description,
76                 model_name=model_name, model_number=serial_no,
77                 model_url=project_page, serial_number=serial_no)
78
79     def _create_services(self):
80         self.cds = ContentDirectory(self.plugins_folder,
81                 self.plugins_module_path)
82         cm = ConnectionManagerServer()
83         self.device.add_service(self.cds)
84         self.device.add_service(cm)
85
86     def start(self):
87         """Start the ZukeBox Server"""
88         self._create_device()
89         self._create_services()
90         self.device.start()
91         reactor.add_after_stop_func(self.device.stop)
92         reactor.main()
93
94     # DBUS
95     @dbus.service.method(DBUS_IFACE)
96     def halt(self):
97         reactor.main_quit()
98
99     @dbus.service.method(DBUS_IFACE)
100     def rescan_audio_folder(self):
101         if not self.cds:
102             return
103         pm = self.cds.control_controller.plugin_manager
104         if not "audio_library" in pm.plugins_instances:
105             return
106         pm.plugins_instances["audio_library"].process_audio_folder()
107
108     @dbus.service.method(DBUS_IFACE)
109     def reload_config(self):
110         config.manager.update()
111
112
113