23f8f24c098b61b89181df9ff641f3e924b0c6e2
[vlc.git] / extras / dbus-vlc.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 #
4 # Copyright (C) 2006 Rafaël Carré <funman at videolanorg>
5 #
6 # $Id$
7
8 #  This program is free software; you can redistribute it and/or modify
9 #  it under the terms of the GNU General Public License as published by
10 #  the Free Software Foundation; either version 2 of the License, or
11 #  (at your option) any later version.
12
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 #
22
23 import dbus
24 import dbus.glib
25 import gtk
26 import gtk.glade
27 import egg.trayicon
28 import os
29
30 global position
31
32 def itemchange_handler(item):
33     l_item.set_text(item)
34
35 bus = dbus.SessionBus()
36 remote_object = bus.get_object("org.videolan.vlc", "/org/videolan/vlc")
37 interface  = dbus.Interface(remote_object, "org.videolan.vlc")
38 try:
39     remote_object.connect_to_signal("ItemChange", itemchange_handler, dbus_interface="org.videolan.vlc")
40 except:
41     True
42
43 def AddMRL(widget):
44     mrl = e_mrl.get_text()
45     if mrl != None and mrl != "":
46         interface.AddMRL(mrl, True)
47     else:
48         mrl = bt_file.get_filename()
49         if mrl != None and mrl != "":
50             interface.AddMRL("directory://" + mrl, True)
51
52 def Next(widget):
53     interface.Next()
54     update(0)
55
56 def Prev(widget):
57     interface.Prev()
58     update(0)
59
60 def Stop(widget):
61     interface.Stop()
62     update(0)
63
64 def update(widget):
65     itemchange_handler(str(interface.GetPlayingItem()))
66     vol.set_value(interface.VolumeGet())
67     GetPlayStatus(0)
68
69 def GetPlayStatus(widget):
70     status = str(interface.GetPlayStatus())
71     if status == "playing":
72         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
73     else:
74         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
75
76 def Quit(widget):
77     interface.Quit()
78     l_item.set_text("")
79
80 def TogglePause(widget):
81     if interface.TogglePause() == True:
82         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
83     else:
84         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
85     update(0)
86
87 def volchange(widget):
88     interface.VolumeSet(vol.get_value_as_int())
89
90 def expander(widget):
91     if exp.get_expanded() == False:
92         exp.set_label("Less")
93     else:
94         exp.set_label("More")
95
96 def delete_event(self, widget):
97     widget.hide()
98     return True
99
100 def destroy(widget):
101     gtk.main_quit()
102
103 def key_release(widget, event):
104     global position
105     if event.keyval == gtk.keysyms.Escape:
106         position = window.get_position()
107         widget.hide()
108
109 def tray_button(widget,event):
110     global position
111     if event.button == 1:
112         if window.get_property('visible'):
113             position = window.get_position()
114             window.hide()
115         else:
116             window.move(position[0], position[1])
117             window.show()
118     if event.button == 3:
119         menu.popup(None,None,None,event.button,event.time)
120
121 xml = gtk.glade.XML('dbus-vlc.glade')
122
123 bt_close    = xml.get_widget('close')
124 bt_quit     = xml.get_widget('quit')
125 bt_file     = xml.get_widget('ChooseFile')
126 bt_mrl      = xml.get_widget('AddMRL')
127 bt_next     = xml.get_widget('next')
128 bt_prev     = xml.get_widget('prev')
129 bt_stop     = xml.get_widget('stop')
130 bt_toggle   = xml.get_widget('toggle')
131 l_item      = xml.get_widget('item')
132 e_mrl       = xml.get_widget('mrl')
133 window      = xml.get_widget('window1')
134 img_bt_toggle=xml.get_widget('image6')
135 exp         = xml.get_widget('expander2')
136 expvbox     = xml.get_widget('expandvbox')
137 menu        = xml.get_widget('menu1')
138 menuitem    = xml.get_widget('menuquit')
139 vlcicon     = xml.get_widget('eventicon')
140 vol         = xml.get_widget('vol')
141
142 window.connect('delete_event',  delete_event)
143 window.connect('destroy',       destroy)
144 window.connect('key_release_event', key_release)
145
146 tray = egg.trayicon.TrayIcon("VLC")
147 eventbox = gtk.EventBox()
148 tray.add(eventbox)
149 eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
150 eventbox.connect('button_press_event', tray_button)
151 image = gtk.Image()
152 eventbox.add(image)
153 image.set_from_icon_name("vlc", gtk.ICON_SIZE_MENU)
154 tray.show_all()
155
156 def fuck():
157     return
158
159 def icon_clicked(widget, event):
160     update(0)
161
162 menu.attach_to_widget(eventbox, fuck)
163
164 bt_close.connect('clicked',     destroy)
165 bt_quit.connect('clicked',      Quit)
166 bt_mrl.connect('clicked',       AddMRL)
167 bt_toggle.connect('clicked',    TogglePause)
168 bt_next.connect('clicked',      Next)
169 bt_prev.connect('clicked',      Prev)
170 bt_stop.connect('clicked',      Stop)
171 exp.connect('activate',         expander)
172 menuitem.connect('activate',    destroy)
173 vlcicon.set_events(gtk.gdk.BUTTON_PRESS_MASK)
174 vlcicon.connect('button_press_event', icon_clicked)
175 vol.connect('value-changed',    volchange)
176
177 library = "/media/mp3"
178
179 try:
180     os.chdir(library)
181     bt_file.set_current_folder(library)
182 except:
183     print "edit this file to point to your media library"
184
185 window.set_icon_name('vlc')
186 window.set_title("VLC - D-Bus ctrl")
187 window.show()
188
189 try:
190     update(0)
191 except:
192     True
193
194 gtk.main()