~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to dogtail/trayicon.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# jhbuild - a build script for GNOME 1.x and 2.x
2
2
# Copyright (C) 2001-2004 James Henstridge
3
3
#
4
 
#       trayicon.py: simple wrapper for zenity based tray icons
 
4
#       trayicon.py: simple wrapper for zenity based tray icons
5
5
#
6
6
# This program is free software; you can redistribute it and/or modify
7
7
# it under the terms of the GNU General Public License as published by
10
10
#
11
11
# This program is distributed in the hope that it will be useful,
12
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
14
# GNU General Public License for more details.
15
15
#
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with this program; if not, write to the Free Software
18
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA      02111-1307      USA
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA      02111-1307      USA
19
19
 
20
20
import sys
21
21
import os
22
22
try:
23
 
        import subprocess
 
23
    import subprocess
24
24
except ImportError:
25
 
        # I know, let's add an ugly hack!
26
 
        # Don't totally bail when not using python2.4 or newer.
27
 
        # FIXME: since we aren't using any new functionality of subprocess, we
28
 
        #    could just use the deprecated methods.
29
 
        class subprocess:
30
 
                PIPE = None
31
 
                def Popen(cmd, close_fds, preexec_fn, stdin): return None
32
 
                Popen = staticmethod(Popen)
33
 
 
34
 
        
 
25
    # I know, let's add an ugly hack!
 
26
    # Don't totally bail when not using python2.4 or newer.
 
27
    # FIXME: since we aren't using any new functionality of subprocess, we
 
28
    #    could just use the deprecated methods.
 
29
    class subprocess:
 
30
        PIPE = None
 
31
        def Popen(cmd, close_fds, preexec_fn, stdin): return None
 
32
        Popen = staticmethod(Popen)
 
33
 
 
34
 
35
35
 
36
36
class TrayIcon:
37
 
        def __init__(self):
38
 
                self._run_zenity()
39
 
        def _run_zenity(self):
40
 
                # run zenity with stdout and stderr directed to /dev/null
41
 
                def preexec():
42
 
                        null = open('/dev/null', 'w')
43
 
                        try:
44
 
                                os.dup2(null.fileno(), sys.stdout.fileno())
45
 
                                os.dup2(null.fileno(), sys.stderr.fileno())
46
 
                        finally:
47
 
                                null.close()
48
 
                        os.setsid()
49
 
                try:
50
 
                        self.proc = subprocess.Popen(['zenity', '--notification',
51
 
                                                                                 '--listen'],
52
 
                                                                                 close_fds=True,
53
 
                                                                                 preexec_fn=preexec,
54
 
                                                                                 stdin=subprocess.PIPE)
55
 
                except (OSError, IOError):
56
 
                        self.proc = None
57
 
 
58
 
        def close(self):
59
 
                status = None
60
 
                if self.proc:
61
 
                        self.proc.stdin.close()
62
 
                        status = self.proc.wait()
63
 
                        self.proc = None
64
 
                return status
65
 
 
66
 
        def _send_cmd(self, cmd):
67
 
                if not self.proc: return
68
 
                if isinstance(cmd, unicode):
69
 
                        cmd = cmd.encode('utf-8')
70
 
                try:
71
 
                        self.proc.stdin.write(cmd)
72
 
                        self.proc.stdin.flush()
73
 
                except (IOError, OSError), err:
74
 
                        self.close()
75
 
        def set_icon(self, icon):
76
 
                self._send_cmd('icon: %s\n' % icon)
77
 
 
78
 
        def set_tooltip(self, tooltip):
79
 
                self._send_cmd('tooltip: %s\n' % tooltip)
80
 
        
81
 
        def set_visible(self, visible):
82
 
                if visible:
83
 
                        visible = 'true'
84
 
                else:
85
 
                        visible = 'false'
86
 
                self._send_cmd('visible: %s\n' % visible)
87
 
 
 
37
    def __init__(self):
 
38
        self._run_zenity()
 
39
    def _run_zenity(self):
 
40
        # run zenity with stdout and stderr directed to /dev/null
 
41
        def preexec():
 
42
            null = open('/dev/null', 'w')
 
43
            try:
 
44
                os.dup2(null.fileno(), sys.stdout.fileno())
 
45
                os.dup2(null.fileno(), sys.stderr.fileno())
 
46
            finally:
 
47
                null.close()
 
48
            os.setsid()
 
49
        try:
 
50
            self.proc = subprocess.Popen(['zenity', '--notification',
 
51
                                                                     '--listen'],
 
52
                                                                     close_fds=True,
 
53
                                                                     preexec_fn=preexec,
 
54
                                                                     stdin=subprocess.PIPE)
 
55
        except (OSError, IOError):
 
56
            self.proc = None
 
57
 
 
58
    def close(self):
 
59
        status = None
 
60
        if self.proc:
 
61
            self.proc.stdin.close()
 
62
            status = self.proc.wait()
 
63
            self.proc = None
 
64
        return status
 
65
 
 
66
    def _send_cmd(self, cmd):
 
67
        if not self.proc: return
 
68
        if isinstance(cmd, unicode):
 
69
            cmd = cmd.encode('utf-8')
 
70
        try:
 
71
            self.proc.stdin.write(cmd)
 
72
            self.proc.stdin.flush()
 
73
        except (IOError, OSError), err:
 
74
            self.close()
 
75
    def set_icon(self, icon):
 
76
        self._send_cmd('icon: %s\n' % icon)
 
77
 
 
78
    def set_tooltip(self, tooltip):
 
79
        self._send_cmd('tooltip: %s\n' % tooltip)
 
80
 
 
81
    def set_visible(self, visible):
 
82
        if visible:
 
83
            visible = 'true'
 
84
        else:
 
85
            visible = 'false'
 
86
        self._send_cmd('visible: %s\n' % visible)