30
30
import dbus.service
32
from dbus.service import BusName
32
34
from miro import messages
34
if getattr(dbus, 'version', (0, 0, 0)) >= (0, 41, 0):
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
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
50
import dbus.dbus_bindings
52
class NameExistsException(dbus.DBusException):
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
62
class BusNameFlags(object):
63
"""A base class for exporting your own Named Services across the Bus
65
def __new__(cls, name, bus=None, flags=0, do_not_queue=False):
67
flags = flags | NAME_FLAG_DO_NOT_QUEUE
73
# otherwise register the name
74
conn = bus.get_connection()
75
retval = dbus.dbus_bindings.bus_request_name(conn, name, flags)
77
# TODO: more intelligent tracking of bus name states?
78
if retval == REQUEST_NAME_REPLY_PRIMARY_OWNER:
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
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
92
raise RuntimeError('requesting bus name %s returned unexpected value %s' % (name, retval))
94
# and create the object
95
bus_name = object.__new__(cls)
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):
107
# we can delete the low-level name here because these objects
108
# are guaranteed to exist only once for each bus name
110
dbus.dbus_bindings.bus_release_name(self._bus.get_connection(), self._name)
113
"""Get the Bus this Service is on"""
117
"""Get the name of this service"""
120
def get_connection(self):
121
"""Get the connection for this service"""
125
return '<dbus.service.BusName %s on %r at %#x>' % (self._name, self._bus, id(self))
128
BusName = BusNameFlags
130
36
class OneTime(dbus.service.Object):
131
"""This makes sure we've only got one instance of Miro running at any given
37
"""This makes sure we've only got one instance of Miro running at
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')
140
@dbus.service.method('org.participatoryculture.dtv.OneTimeIface')
49
dbus_interface='org.participatoryculture.dtv.OneTimeIFace',
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://'):]
149
59
messages.OpenIndividualFiles(args).send_to_backend()