~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

« back to all changes in this revision

Viewing changes to applets/unmaintained/stacks/stacks_launcher.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-08-29 14:29:52 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100829142952-rhvuetyms9bv5uu7
Tags: upstream-0.4.0+bzr1372
ImportĀ upstreamĀ versionĀ 0.4.0+bzr1372

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
 
3
 
# Copyright (c) 2007 Timon ter Braak
 
2
# Copyright (C) 2010  onox <denkpadje@gmail.com>
4
3
#
5
4
# This library is free software; you can redistribute it and/or
6
5
# modify it under the terms of the GNU Lesser General Public
13
12
# Lesser General Public License for more details.
14
13
#
15
14
# You should have received a copy of the GNU Lesser General Public
16
 
# License along with this library; if not, write to the
17
 
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
# Boston, MA 02111-1307, USA.
 
15
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19
16
 
20
17
import os
 
18
 
21
19
import gtk
22
 
import gnomedesktop
23
 
 
24
 
#Borrowed LaunchManager from "gimmie"
 
20
from desktopagnostic import fdo, vfs
 
21
 
 
22
 
25
23
class LaunchManager:
26
 
    '''
27
 
    A program lauching utility which handles opening a URI or executing a
28
 
    program or .desktop launcher, handling variable expansion in the Exec
29
 
    string.
30
 
 
31
 
    Adds the launched URI or launcher to the ~/.recently-used log.  Sets a
32
 
    DESKTOP_STARTUP_ID environment variable containing useful information such
33
 
    as the URI which caused the program execution and a timestamp.
 
24
 
 
25
    """A program lauching utility which handles opening a URI or
 
26
    executing a program or .desktop launcher.
 
27
 
 
28
    Sets a DESKTOP_STARTUP_ID environment variable containing useful
 
29
    information such as the URI which caused the program execution and
 
30
    a timestamp.
34
31
 
35
32
    See the startup notification spec for more information on
36
33
    DESKTOP_STARTUP_IDs.
37
 
    '''
38
 
    def __init__(self):
39
 
        return
40
 
 
41
 
    def launch_uri(self, uri, mimetype = None):
42
 
        assert uri, "Must specify URI to launch"
43
 
 
44
 
        child = os.fork()
45
 
        if not child:
46
 
            # Inside forked child
47
 
            os.setsid()
48
 
            os.environ['STACKS_LAUNCHER'] = uri
49
 
            os.environ['DESKTOP_STARTUP_ID'] = self.make_startup_id(uri)
50
 
            os.spawnlp(os.P_NOWAIT, "xdg-open", "xdg-open", uri)
51
 
            os._exit(0)
52
 
        else:
53
 
            os.wait()
54
 
        return child
55
 
 
56
 
    def get_local_path(self, uri):
57
 
        scheme, path = urllib.splittype(uri)
58
 
        if scheme == None:
59
 
            return uri
60
 
        elif scheme == "file":
61
 
            path = urllib.url2pathname(path)
62
 
            if path[:3] == "///":
63
 
                path = path[2:]
64
 
            return path
65
 
        return None
66
 
 
67
 
    def launch_command_with_uris(self, command, uri_list, launcher_uri = None):
68
 
        if command.rfind("%U") > -1:
69
 
            uri_str = ""
70
 
            for uri in uri_list:
71
 
                uri_str = uri_str + " " + uri
72
 
            return self.launch_command(command.replace("%U", uri_str), launcher_uri)
73
 
        elif command.rfind("%F") > -1:
74
 
            file_str = ""
75
 
            for uri in uri_list:
76
 
                uri = self.get_local_path(self, uri)
77
 
                if uri:
78
 
                    file_str = file_str + " " + uri
79
 
                else:
80
 
                    print " !!! Command does not support non-file URLs: ", command
81
 
            return self.launch_command(command.replace("%F", file_str), launcher_uri)
82
 
        elif command.rfind("%u") > -1:
83
 
            startup_ids = []
84
 
            for uri in uri_list:
85
 
                startup_ids.append(self.launch_command(command.replace("%u", uri), launcher_uri))
86
 
            else:
87
 
                return self.launch_command(command.replace("%u", ""), launcher_uri)
88
 
            return startup_ids
89
 
        elif command.rfind("%f") > -1:
90
 
            startup_ids = []
91
 
            for uri in uri_list:
92
 
                uri = self.get_local_path(self, uri)
93
 
                if uri:
94
 
                    startup_ids.append(self.launch_command(command.replace("%f", uri),
95
 
                                                          launcher_uri))
96
 
                else:
97
 
                    print " !!! Command does not support non-file URLs: ", command
98
 
            else:
99
 
                return self.launch_command(command.replace("%f", ""), launcher_uri)
100
 
            return startup_ids
101
 
        else:
102
 
            return self.launch_command(command, launcher_uri)
103
 
 
104
 
    def make_startup_id(self, key, ev_time = None):
105
 
        if not ev_time:
106
 
            ev_time = gtk.get_current_event_time()
107
 
        if not key:
108
 
            return "STACKS_TIME%d" % ev_time
109
 
        else:
110
 
            return "STACKS:%s_TIME%d" % (key, ev_time)
111
 
 
112
 
    def parse_startup_id(self, id):
113
 
        if id and id.startswith("STACKS:"):
 
34
 
 
35
    """
 
36
 
 
37
    def launch_uri(self, uri, mimetype=None):
 
38
        file = vfs.File.for_uri(uri)
 
39
 
 
40
        if file is not None and file.exists():
114
41
            try:
115
 
                uri = id[len("STACKS:"):id.rfind("_TIME")]
116
 
                timestamp = id[id.rfind("_TIME") + len("_TIME"):]
117
 
                return (uri, timestamp)
118
 
            except IndexError:
119
 
                pass
120
 
        return (None, None)
121
 
 
122
 
    def launch_command(self, command, launcher_uri = None):
123
 
        startup_id = self.make_startup_id(launcher_uri)
124
 
        child = os.fork()
125
 
        if not child:
126
 
            # Inside forked child
127
 
            os.setsid()
128
 
            os.environ['DESKTOP_STARTUP_ID'] = startup_id
129
 
            if launcher_uri:
130
 
                os.environ['STACKS_LAUNCHER'] = launcher_uri
131
 
            os.popen2(command)
132
 
            os._exit(0)
133
 
        else:
134
 
            os.wait()
135
 
            return (child, startup_id)
136
 
 
137
 
    def launch_dot_desktop(self, uri):
138
 
        item = gnomedesktop.item_new_from_uri(
139
 
                    uri, gnomedesktop.LOAD_ONLY_IF_EXISTS)
140
 
        if not item: return
141
 
        
142
 
        type = item.get_string(gnomedesktop.KEY_TYPE)
143
 
        if type == "Application":
144
 
            command = item.get_string(gnomedesktop.KEY_EXEC)
145
 
            self.launch_command(command, uri)
146
 
        elif type == "Link":
147
 
            command = "xdg-open " + item.get_string(gnomedesktop.KEY_URL)
148
 
            self.launch_command(command, uri)
 
42
                file.launch()
 
43
            except glib.GError, e:
 
44
                print "Error when opening: %s" % e
 
45
        else:
 
46
            print "File at URI not found (%s)" % uri
 
47
 
 
48
    def launch_dot_desktop(self, desktop_uri):
 
49
        file = vfs.File.for_uri(desktop_uri)
 
50
 
 
51
        if file is not None and file.exists():
 
52
            entry = fdo.DesktopEntry.for_file(file)
 
53
 
 
54
            if entry.key_exists("Exec"):
 
55
                if entry.key_exists("StartupNotify"):
 
56
                    ev_time = gtk.get_current_event_time()
 
57
                    os.environ['DESKTOP_STARTUP_ID'] = "STACKS:%s_TIME%d" % (desktop_uri, ev_time)
 
58
 
 
59
                try:
 
60
                    entry.launch(0, None)
 
61
                except glib.GError, e:
 
62
                    print "Error when launching: %s" % e
 
63
 
 
64
                if entry.key_exists("StartupNotify"):
 
65
                    del os.environ['DESKTOP_STARTUP_ID']
 
66
        else:
 
67
            print "File not found (%s)" % desktop_uri