d9f8cb46a5bbc6e31d9e1e01ab637875d7d2c3da
[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     GetPlayStatus(0)
67
68 def GetPlayStatus(widget):
69     status = str(interface.GetPlayStatus())
70     if status == "playing":
71         img_bt_toggle.set_from_stock("gtk-media-pause", gtk.ICON_SIZE_SMALL_TOOLBAR)
72     else:
73         img_bt_toggle.set_from_stock("gtk-media-play", gtk.ICON_SIZE_SMALL_TOOLBAR)
74
75 def Quit(widget):
76     interface.Quit()
77     l_item.set_text("")
78
79 def TogglePause(widget):
80     if interface.TogglePause() == True:
81         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PAUSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
82     else:
83         img_bt_toggle.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_SMALL_TOOLBAR)
84     update(0)
85
86 def expander(widget):
87     if exp.get_label() == "More":
88         exp.set_label("Less")
89     else:
90         exp.set_label("More")
91
92 def delete_event(self, widget):
93     widget.hide()
94     return True
95
96 def destroy(widget):
97     gtk.main_quit()
98
99 def key_release(widget, event):
100     global position
101     if event.keyval == gtk.keysyms.Escape:
102         position = window.get_position()
103         widget.hide()
104
105 def tray_button(widget,event):
106     global position
107     if event.button == 1:
108         if window.get_property('visible'):
109             position = window.get_position()
110             window.hide()
111         else:
112             window.move(position[0], position[1])
113             window.show()
114     if event.button == 3:
115         menu.popup(None,None,None,event.button,event.time)
116
117 xml = gtk.glade.XML('dbus-vlc.glade')
118
119 bt_close    = xml.get_widget('close')
120 bt_quit     = xml.get_widget('quit')
121 bt_file     = xml.get_widget('ChooseFile')
122 bt_mrl      = xml.get_widget('AddMRL')
123 bt_next     = xml.get_widget('next')
124 bt_prev     = xml.get_widget('prev')
125 bt_stop     = xml.get_widget('stop')
126 bt_toggle   = xml.get_widget('toggle')
127 l_item      = xml.get_widget('item')
128 e_mrl       = xml.get_widget('mrl')
129 window      = xml.get_widget('window1')
130 img_bt_toggle=xml.get_widget('image6')
131 exp         = xml.get_widget('expander2')
132 expvbox     = xml.get_widget('expandvbox')
133 menu        = xml.get_widget('menu1')
134 menuitem    = xml.get_widget('menuquit')
135 vlcicon     = xml.get_widget('eventicon')
136
137
138 window.connect('delete_event',  delete_event)
139 window.connect('destroy',       destroy)
140 window.connect('key_release_event', key_release)
141
142 tray = egg.trayicon.TrayIcon("VLC")
143 eventbox = gtk.EventBox()
144 tray.add(eventbox)
145 eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
146 eventbox.connect('button_press_event', tray_button)
147 image = gtk.Image()
148 eventbox.add(image)
149 image.set_from_icon_name("vlc", gtk.ICON_SIZE_MENU)
150 tray.show_all()
151
152 def fuck():
153     return
154
155 def icon_clicked(widget, event):
156     update(0)
157
158 menu.attach_to_widget(eventbox, fuck)
159
160 bt_close.connect('clicked',     destroy)
161 bt_quit.connect('clicked',      Quit)
162 bt_mrl.connect('clicked',       AddMRL)
163 bt_toggle.connect('clicked',    TogglePause)
164 bt_next.connect('clicked',      Next)
165 bt_prev.connect('clicked',      Prev)
166 bt_stop.connect('clicked',      Stop)
167 exp.connect('activate',         expander)
168 menuitem.connect('activate',    destroy)
169 vlcicon.set_events(gtk.gdk.BUTTON_PRESS_MASK)
170 vlcicon.connect('button_press_event', icon_clicked)
171
172 library = "/media/mp3"
173
174 try:
175     os.chdir(library)
176     bt_file.set_current_folder(library)
177 except:
178     print "edit this file to point to your media library"
179
180 window.set_icon_name('vlc')
181 window.set_title("VLC - D-Bus ctrl")
182 window.show()
183
184 try:
185     update(0)
186 except:
187     True
188
189 gtk.main()