~jhonnyc/cgmail/jonathanc-branch

« back to all changes in this revision

Viewing changes to src/utils.py

  • Committer: Marco Ferragina
  • Date: 2007-05-06 15:51:12 UTC
  • Revision ID: marco.ferragina@gmail.com-20070506155112-874uk2m8blrknyuf
Restructured package source dir. Now is much more organized. Implemented right click menu on account window treeview

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import threading
2
 
import os
3
 
import subprocess
4
 
import gconf
5
 
 
6
 
(
7
 
        GSTPLAY,
8
 
        GNOMEPLAY,
9
 
        NOENGINE
10
 
) = range(3)
11
 
 
12
 
try:
13
 
        import gnome
14
 
        gnome.sound_init("localhost")
15
 
        ENGINE = GNOMEPLAY
16
 
except ImportError:
17
 
        try:
18
 
                import gst
19
 
                import gobject
20
 
                ENGINE = GSTPLAY
21
 
        except ImportError:
22
 
                ENGINE = NOENGINE
23
 
 
24
 
class __GstPlayThread(threading.Thread):
25
 
        def __init__(self, ply):
26
 
                self.ply = ply
27
 
                threading.Thread.__init__(self)
28
 
        def run(self):
29
 
                self.ply.set_state(gst.STATE_PLAYING)
30
 
                def bus_event(bus, message):
31
 
                        t = message.type
32
 
                        if t == gst.MESSAGE_EOS:
33
 
                                self.ply.set_state(gst.STATE_NULL)
34
 
                        return True
35
 
                self.ply.get_bus().add_watch(bus_event)
36
 
                        
37
 
 
38
 
def __gstplay(filename):
39
 
        try:
40
 
                cwd = os.getcwd()
41
 
                location = os.path.join(cwd, filename)
42
 
                ply = gst.element_factory_make("playbin", "player")
43
 
                ply.set_property("uri", "file://" + location)
44
 
                pt = __GstPlayThread(ply)
45
 
                pt.start()
46
 
        except:
47
 
                pass
48
 
 
49
 
if ENGINE == GNOMEPLAY:
50
 
        def playsnd(filename):
51
 
                gnome.sound_play(filename)
52
 
elif ENGINE == GSTPLAY:
53
 
        playsnd = __gstplay
54
 
else:
55
 
        def playsnd(filename):
56
 
                pass
57
 
 
58
 
 
59
 
 
60
 
def invoke_subprocess(cmdline):
61
 
        try:
62
 
                setsid = getattr(os, 'setsid', None)
63
 
                p = subprocess.Popen(cmdline, close_fds = True, preexec_fn = setsid)
64
 
        except OSError:
65
 
                print "Warning: cannot execute", cmdline
66
 
                return False
67
 
        return True
68
 
 
69
 
def get_default_mail_reader():
70
 
        client = gconf.client_get_default()
71
 
        cmd  = client.get_string("/desktop/gnome/url-handlers/mailto/command")
72
 
        return cmd.split()[0]
73
 
 
74
 
def open_mail_reader():
75
 
        cmdline = get_default_mail_reader()
76
 
        invoke_subprocess(cmdline)
77
 
 
78
 
def open_browser(url):
79
 
        client = gconf.client_get_default()
80
 
        cmd  = client.get_string("/desktop/gnome/applications/browser/exec")
81
 
        cmdline = [cmd.split()[0], url]
82
 
        invoke_subprocess(cmdline)
83
 
 
84
 
if __name__ == "__main__":
85
 
        import gtk
86
 
        open_browser("http://google.it")