Ready for mentors
[cgmail.git] / cGmail / lib / utils.py
blob740d5b65911954db4ed129300326c9aebb5e11cb
2 # Copyright (C) 2007 Marco Ferragina <marco.ferragina@gmail.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 import threading
19 import os
20 import subprocess
21 import gconf
22 import sys
25 GSTPLAY,
26 GNOMEPLAY,
27 NOENGINE
28 ) = range(3)
30 try:
31 import gnome
32 gnome.sound_init("localhost")
33 ENGINE = GNOMEPLAY
34 # Disable gnome play, see bug https://bugs.launchpad.net/ubuntu/+bug/153704
35 # When the bug will be fixed remember to remove the raise ImportError
36 raise ImportError
37 except ImportError:
38 try:
39 import gst
40 import gobject
41 ENGINE = GSTPLAY
42 except ImportError:
43 ENGINE = NOENGINE
45 class __GstPlayThread(threading.Thread):
46 def __init__(self, ply):
47 self.ply = ply
48 threading.Thread.__init__(self)
49 def run(self):
50 self.ply.set_state(gst.STATE_PLAYING)
51 def bus_event(bus, message):
52 t = message.type
53 if t == gst.MESSAGE_EOS:
54 self.ply.set_state(gst.STATE_NULL)
55 return True
56 self.ply.get_bus().add_watch(bus_event)
59 def __gstplay(filename):
60 try:
61 cwd = os.getcwd()
62 location = os.path.join(cwd, filename)
63 ply = gst.element_factory_make("playbin", "player")
64 ply.set_property("uri", "file://" + location)
65 pt = __GstPlayThread(ply)
66 pt.start()
67 except:
68 pass
70 if ENGINE == GNOMEPLAY:
71 def playsnd(filename):
72 gnome.sound_play(filename)
73 elif ENGINE == GSTPLAY:
74 playsnd = __gstplay
75 else:
76 def playsnd(filename):
77 pass
81 def invoke_subprocess(cmdline):
82 try:
83 setsid = getattr(os, 'setsid', None)
84 p = subprocess.Popen(cmdline, close_fds = True, preexec_fn = setsid)
85 except OSError:
86 print "Warning: cannot execute", cmdline
87 return False
88 return True
90 def get_default_mail_reader():
91 client = gconf.client_get_default()
92 cmd = client.get_string("/desktop/gnome/url-handlers/mailto/command")
93 return cmd.split()[0]
95 def open_mail_reader():
96 cmdline = get_default_mail_reader()
97 invoke_subprocess(cmdline)
99 def open_browser(url):
100 client = gconf.client_get_default()
101 cmd = client.get_string("/desktop/gnome/applications/browser/exec")
102 cmdline = [cmd.split()[0], url]
103 invoke_subprocess(cmdline)
106 if __name__ == "__main__":
107 import gtk
108 open_browser("http://google.it")