~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to platform/gtk-x11/plat/onetime.py

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2009 Participatory Culture Foundation
 
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
28
28
 
29
29
import dbus
30
30
import dbus.service
 
31
import dbus.glib
 
32
from dbus.service import BusName
31
33
 
32
34
from miro import messages
33
35
 
34
 
if getattr(dbus, 'version', (0, 0, 0)) >= (0, 41, 0):
35
 
    import dbus.glib
36
 
 
37
 
# We do this crazy stuff so that Miro can run on platforms that have the
38
 
# older dbus-python bindings.  Dapper uses (0, 51, 0).  I think once we
39
 
# stop supporting Dapper, we can rip this whole section out.
40
 
# This section was re-written so that it doesn't trigger the dbus-python
41
 
# deprecation warning and is localized so we can just delete it and
42
 
# move on with our lives when the time comes. - willkahn-greene10-29-2007
43
 
 
44
 
# (0, 80, 0) is the first version that has do_not_queue
45
 
if getattr(dbus, 'version', (0, 0, 0)) >= (0, 80, 0):
46
 
    BusName = dbus.service.BusName
47
 
    NameExistsException = dbus.NameExistsException
48
 
 
49
 
else:
50
 
    import dbus.dbus_bindings
51
 
 
52
 
    class NameExistsException(dbus.DBusException):
53
 
        pass
54
 
 
55
 
    # these are in the dbus spec, so they're not going to change.
56
 
    REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
57
 
    REQUEST_NAME_REPLY_IN_QUEUE = 2
58
 
    REQUEST_NAME_REPLY_EXISTS = 3
59
 
    REQUEST_NAME_REPLY_ALREADY_OWNER = 4
60
 
    NAME_FLAG_DO_NOT_QUEUE = 4
61
 
 
62
 
    class BusNameFlags(object):
63
 
        """A base class for exporting your own Named Services across the Bus
64
 
        """
65
 
        def __new__(cls, name, bus=None, flags=0, do_not_queue=False):
66
 
            if do_not_queue:
67
 
                flags = flags | NAME_FLAG_DO_NOT_QUEUE
68
 
 
69
 
            # get default bus
70
 
            if bus == None:
71
 
                bus = dbus.Bus()
72
 
 
73
 
            # otherwise register the name
74
 
            conn = bus.get_connection()
75
 
            retval = dbus.dbus_bindings.bus_request_name(conn, name, flags)
76
 
 
77
 
            # TODO: more intelligent tracking of bus name states?
78
 
            if retval == REQUEST_NAME_REPLY_PRIMARY_OWNER:
79
 
                pass
80
 
            elif retval == REQUEST_NAME_REPLY_IN_QUEUE:
81
 
                # queueing can happen by default, maybe we should
82
 
                # track this better or let the user know if they're
83
 
                # queued or not?
84
 
                pass
85
 
            elif retval == REQUEST_NAME_REPLY_EXISTS:
86
 
                raise NameExistsException(name)
87
 
            elif retval == REQUEST_NAME_REPLY_ALREADY_OWNER:
88
 
                # if this is a shared bus which is being used by someone
89
 
                # else in this process, this can happen legitimately
90
 
                pass
91
 
            else:
92
 
                raise RuntimeError('requesting bus name %s returned unexpected value %s' % (name, retval))
93
 
 
94
 
            # and create the object
95
 
            bus_name = object.__new__(cls)
96
 
            bus_name._bus = bus
97
 
            bus_name._name = name
98
 
            bus_name._conn = conn
99
 
 
100
 
            return bus_name
101
 
 
102
 
        # do nothing because this is called whether or not the bus name
103
 
        # object was retrieved from the cache or created new
104
 
        def __init__(self, *args, **keywords):
105
 
            pass
106
 
 
107
 
        # we can delete the low-level name here because these objects
108
 
        # are guaranteed to exist only once for each bus name
109
 
        def __del__(self):
110
 
            dbus.dbus_bindings.bus_release_name(self._bus.get_connection(), self._name)
111
 
 
112
 
        def get_bus(self):
113
 
            """Get the Bus this Service is on"""
114
 
            return self._bus
115
 
 
116
 
        def get_name(self):
117
 
            """Get the name of this service"""
118
 
            return self._name
119
 
 
120
 
        def get_connection(self):
121
 
            """Get the connection for this service"""
122
 
            return self._conn
123
 
 
124
 
        def __repr__(self):
125
 
            return '<dbus.service.BusName %s on %r at %#x>' % (self._name, self._bus, id(self))
126
 
        __str__ = __repr__
127
 
 
128
 
    BusName = BusNameFlags
129
 
 
130
36
class OneTime(dbus.service.Object):
131
 
    """This makes sure we've only got one instance of Miro running at any given
132
 
    time.
 
37
    """This makes sure we've only got one instance of Miro running at
 
38
    any given time.
133
39
    """
134
40
    def __init__(self):
135
41
        bus = dbus.SessionBus()
136
 
        bus_name = BusName('org.participatoryculture.dtv.onetime', bus=bus, do_not_queue=True)
137
 
        dbus.service.Object.__init__(self, bus_name=bus_name,
138
 
                object_path='/org/participatoryculture/dtv/OneTime')
 
42
        bus_name = BusName('org.participatoryculture.dtv.onetime',
 
43
                           bus=bus, do_not_queue=True)
 
44
        dbus.service.Object.__init__(
 
45
            self, bus_name=bus_name,
 
46
            object_path='/org/participatoryculture/dtv/OneTime')
139
47
 
140
 
    @dbus.service.method('org.participatoryculture.dtv.OneTimeIface')
 
48
    @dbus.service.method(
 
49
        dbus_interface='org.participatoryculture.dtv.OneTimeIFace',
 
50
        in_signature='as')
141
51
    def handle_args(self, args):
142
52
        from miro import singleclick
143
53
        from miro import eventloop
144
 
        for i in xrange(len(args)):
145
 
            args[i] = args[i].encode('latin1')
146
 
            if args[i].startswith('file://'):
147
 
                args[i] = args[i][len('file://'):]
 
54
        for i, arg in enumerate(args):
 
55
            args[i] = arg.encode('latin1')
 
56
            if arg.startswith('file://'):
 
57
                args[i] = arg[len('file://'):]
148
58
 
149
59
        messages.OpenIndividualFiles(args).send_to_backend()