1 '''
2 Low level Skype for Linux interface implemented
3 using python-dbus package.
4
5 This module handles the options that you can pass to L{ISkype.__init__<skype.ISkype.__init__>}
6 for Linux machines when the transport is set to DBus. See below.
7
8 @newfield option: Option, Options
9
10 @option: C{Bus} DBus bus object as returned by python-dbus package.
11 If not specified, private session bus is created and used. See also C{MainLoop}.
12 @option: C{MainLoop} DBus mainloop object. Use only without specifying the C{Bus}
13 (if you use C{Bus}, pass the mainloop to the bus constructor). If neither C{Bus} or
14 C{MainLoop} is specified, glib mainloop is used. In such case, the mainloop is
15 also run on a separate thread upon attaching to Skype. If you want to use glib
16 mainloop but you want to run the loop yourself (for example because your GUI toolkit
17 does it for you), pass C{dbus.mainloop.glib.DBusGMainLoop()} object as C{MainLoop}
18 parameter.
19
20 @requires: Skype for Linux 2.0 (beta) or newer.
21 '''
22
23 import threading
24 import time
25 import weakref
26 from Skype4Py.API import ICommand, _ISkypeAPIBase
27 from Skype4Py.enums import *
28 from Skype4Py.errors import ISkypeAPIError
29 from Skype4Py.utils import cndexp
30
31
32 try:
33 import dbus, dbus.service
34 except ImportError:
35 import sys
36 if sys.argv == ['(imported)']:
37
38
39 from faked_dbus import dbus
40 else:
41 raise
42
43
45 '''DBus object which exports a Notify method. This will be called by Skype for all
46 notifications with the notification string as a parameter. The Notify method of this
47 class calls in turn the callable passed to the constructor.
48 '''
49
53
54 @dbus.service.method(dbus_interface='com.Skype.API.Client')
56 self.notify(unicode(com))
57
58
61 _ISkypeAPIBase.__init__(self, opts)
62 self.RegisterHandler(handler)
63 self.skype_in = self.skype_out = self.dbus_name_owner_watch = None
64 self.bus = opts.pop('Bus', None)
65 try:
66 mainloop = opts.pop('MainLoop')
67 if self.bus != None:
68 raise TypeError('Bus and MainLoop cannot be used at the same time!')
69 except KeyError:
70 if self.bus == None:
71 import dbus.mainloop.glib
72 import gobject
73 gobject.threads_init()
74 dbus.mainloop.glib.threads_init()
75 mainloop = dbus.mainloop.glib.DBusGMainLoop()
76 self.mainloop = gobject.MainLoop()
77 if self.bus == None:
78 from dbus import SessionBus
79 self.bus = SessionBus(private=True, mainloop=mainloop)
80 if opts:
81 raise TypeError('Unexpected parameter(s): %s' % ', '.join(opts.keys()))
82
84 self.DebugPrint('thread started')
85 if hasattr(self, 'mainloop'):
86 self.mainloop.run()
87 self.DebugPrint('thread finished')
88
90 if hasattr(self, 'mainloop'):
91 self.mainloop.quit()
92 self.skype_in = self.skype_out = None
93 if self.dbus_name_owner_watch != None:
94 self.bus.remove_signal_receiver(self.dbus_name_owner_watch)
95 self.dbus_name_owner_watch = None
96 self.DebugPrint('closed')
97
102
103 - def Attach(self, Timeout=30000, Wait=True):
104 if self.skype_out:
105 return
106 try:
107 self.start()
108 except AssertionError:
109 pass
110 try:
111 self.wait = True
112 def ftimeout():
113 self.wait = False
114 t = threading.Timer(Timeout / 1000.0, ftimeout)
115 if Wait:
116 t.start()
117 while self.wait:
118 try:
119 self.skype_out = self.bus.get_object('com.Skype.API', '/com/Skype')
120 self.skype_in = _SkypeNotifyCallback(self.bus, self.notify)
121 except dbus.DBusException:
122 time.sleep(1.0)
123 else:
124 break
125 else:
126 raise ISkypeAPIError('Skype attach timeout')
127 finally:
128 t.cancel()
129 self.dbus_name_owner_watch = self.bus.add_signal_receiver(self.dbus_name_owner_changed,
130 'NameOwnerChanged',
131 'org.freedesktop.DBus',
132 'org.freedesktop.DBus',
133 '/org/freedesktop/DBus',
134 arg0='com.Skype.API')
135 c = ICommand(-1, 'NAME %s' % self.FriendlyName, '', True, Timeout)
136 self.SendCommand(c)
137 if c.Reply != 'OK':
138 self.skype_in = self.skype_out = None
139 self.SetAttachmentStatus(apiAttachRefused)
140 return
141 self.SendCommand(ICommand(-1, 'PROTOCOL %s' % self.Protocol))
142 self.SetAttachmentStatus(apiAttachSuccess)
143
145 try:
146 self.bus.get_object('com.Skype.API', '/com/Skype')
147 return True
148 except dbus.DBusException:
149 return False
150
151 - def Start(self, Minimized=False, Nosplash=False):
152
153 if not self.IsRunning():
154 import os
155 if os.fork() == 0:
156 os.setsid()
157 os.execlp('skype')
158
160 import os
161 from signal import SIGINT
162 fh = os.popen('ps -o %p --no-heading -C skype')
163 pid = fh.readline().strip()
164 fh.close()
165 if pid:
166 os.kill(int(pid), SIGINT)
167 self.skype_in = self.skype_out = None
168
192
194 self.DebugPrint('<-', repr(com))
195 if com.startswith(u'#'):
196 p = com.find(u' ')
197 Command = self.CommandsStackPop(int(com[1:p]))
198 if Command:
199 Command.Reply = com[p + 1:]
200 if Command.Blocking:
201 Command._event.set()
202 del Command._event
203 else:
204 Command._timer.cancel()
205 del Command._timer
206 self.CallHandler('rece', Command)
207 else:
208 self.CallHandler('rece_api', com[p + 1:])
209 else:
210 self.CallHandler('rece_api', com)
211
217