-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlinkAppDelegate.py
419 lines (343 loc) · 17.8 KB
/
BlinkAppDelegate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from __future__ import with_statement
from Foundation import (NSAppleEventManager,
NSBundle,
NSDistributedNotificationCenter,
NSImage,
NSImageView,
NSMakeRect,
NSNotificationSuspensionBehaviorDeliverImmediately,
NSThread,
NSObject,
NSUserDefaults,
NSLocalizedString)
from AppKit import (NSAlertDefaultReturn,
NSApp,
NSInformationalRequest,
NSRunAlertPanel,
NSTerminateLater,
NSTerminateNow,
NSWorkspace,
NSWorkspaceWillSleepNotification,
NSWorkspaceDidWakeNotification)
import Foundation
import LaunchServices
import objc
import time
import urllib
import os
import platform
import shutil
import struct
from application.notification import NotificationCenter, IObserver
from application import log
from application.python import Null
from sipsimple.account import AccountManager, BonjourAccount
from sipsimple.application import SIPApplication
from sipsimple.configuration.backend.file import FileParserError
from sipsimple.configuration.settings import SIPSimpleSettings
from sipsimple.threading import call_in_thread
from zope.interface import implements
from SIPManager import SIPManager
from iCloudManager import iCloudManager
from BlinkLogger import BlinkLogger
from EnrollmentController import EnrollmentController
import PreferencesController
from ScreenSharingController import ScreenSharingController
from resources import ApplicationData
from util import allocate_autorelease_pool, call_in_gui_thread, external_url_pattern
def fourcharToInt(fourCharCode):
return struct.unpack('>l', fourCharCode)[0]
class BlinkAppDelegate(NSObject):
'''Responsable for starting and stoping the application
Register URL types handled by Blink
Updating the dock icon with missed calls
Migrating data from one version to another
Start enrollment if run first time
Calling Initial SIP URL if necessary
Handle wakeup from sleep
Show about panel'''
implements(IObserver)
contactsWindowController = objc.IBOutlet()
aboutPanel = objc.IBOutlet()
migrationPanel = objc.IBOutlet()
migrationText = objc.IBOutlet()
migrationProgressWheel = objc.IBOutlet()
aboutVersion = objc.IBOutlet()
aboutBundle = objc.IBOutlet()
aboutSlogan = objc.IBOutlet()
aboutCopyright = objc.IBOutlet()
aboutzRTPIcon = objc.IBOutlet()
ui_notification_center = None
application_will_end = False
wake_up_timestamp = None
debug = False
blinkMenu = objc.IBOutlet()
ready = False
missedCalls = 0
missedChats = 0
urisToOpen = []
wait_for_enrollment = False
updater = None
def init(self):
self = super(BlinkAppDelegate, self).init()
if self:
self.applicationName = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleExecutable"))
self.applicationNamePrint = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleName"))
build = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleVersion"))
date = str(NSBundle.mainBundle().infoDictionary().objectForKey_("BlinkVersionDate"))
BlinkLogger().log_info(u"Starting %s build %s from %s" % (self.applicationNamePrint, build, date))
self.registerURLHandler()
NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_(self, "computerDidWake:", NSWorkspaceDidWakeNotification, None)
NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_(self, "computerWillSleep:", NSWorkspaceWillSleepNotification, None)
NSDistributedNotificationCenter.defaultCenter().addObserver_selector_name_object_suspensionBehavior_(self, "callFromAddressBook:", "CallTelephoneNumberWithBlinkFromAddressBookNotification", "AddressBook", NSNotificationSuspensionBehaviorDeliverImmediately)
NSDistributedNotificationCenter.defaultCenter().addObserver_selector_name_object_suspensionBehavior_(self, "callFromAddressBook:", "CallSipAddressWithBlinkFromAddressBookNotification", "AddressBook", NSNotificationSuspensionBehaviorDeliverImmediately)
NotificationCenter().add_observer(self, name="CFGSettingsObjectDidChange")
NotificationCenter().add_observer(self, name="SIPApplicationDidStart")
NotificationCenter().add_observer(self, name="SIPApplicationWillEnd")
NotificationCenter().add_observer(self, name="SIPApplicationDidEnd")
NotificationCenter().add_observer(self, name="SystemIPAddressDidChange")
NotificationCenter().add_observer(self, name="NetworkConditionsDidChange")
NotificationCenter().add_observer(self, name="SIPEngineTransportDidDisconnect")
NotificationCenter().add_observer(self, name="SIPEngineTransportDidConnect")
NotificationCenter().add_observer(self, name="DNSNameserversDidChange")
# remove obsolete settings
userdef = NSUserDefaults.standardUserDefaults()
userdef.removeObjectForKey_('SIPTrace')
userdef.removeObjectForKey_('MSRPTrace')
userdef.removeObjectForKey_('XCAPTrace')
userdef.removeObjectForKey_('EnablePJSIPTrace')
userdef.removeObjectForKey_('EnableNotificationsTrace')
try:
from Updater import Updater
except ImportError:
pass
else:
self.updater = Updater()
call_in_thread('file-io', self.purge_temporary_files)
return self
def purge_temporary_files(self):
for dir in ('.tmp_screenshots', '.tmp_snapshots', '.tmp_file_transfers'):
folder = ApplicationData.get(dir)
if os.path.exists(folder):
try:
shutil.rmtree(folder)
except EnvironmentError:
pass
def gui_notify(self, title, body, subtitle=None):
if self.application_will_end:
return
major, minor = platform.mac_ver()[0].split('.')[0:2]
if (int(major) == 10 and int(minor) >= 8) or int(major) > 10:
if self.ui_notification_center is None:
self.ui_notification_center = Foundation.NSUserNotificationCenter.defaultUserNotificationCenter()
self.ui_notification_center.setDelegate_(self)
notification = Foundation.NSUserNotification.alloc().init()
notification.setTitle_(title)
if subtitle is not None:
notification.setSubtitle_(subtitle)
notification.setInformativeText_(body)
self.ui_notification_center.scheduleNotification_(notification)
def userNotificationCenter_didDeliverNotification_(self, center, notification):
pass
def userNotificationCenter_didActivateNotification_(self, center, notification):
pass
def userNotificationCenter_shouldPresentNotification_(self, center, notification):
return True
# Needed by run_in_gui_thread and call_in_gui_thread
def callObject_(self, callable):
try:
callable()
except:
log.err()
def enroll(self):
enroll = EnrollmentController.alloc().init()
enroll.setCreateAccount()
enroll.runModal()
def updateDockTile(self):
if self.missedCalls > 0 or self.missedChats > 0:
icon = NSImage.imageNamed_("Blink")
image = NSImageView.alloc().initWithFrame_(NSMakeRect(0, 0, 32, 32))
image.setImage_(icon)
if self.missedCalls > 0 and self.missedChats > 0:
NSApp.dockTile().setBadgeLabel_("%i / %i" % (self.missedCalls, self.missedChats))
else:
NSApp.dockTile().setBadgeLabel_("%i" % (self.missedCalls + self.missedChats))
NSApp.dockTile().setContentView_(image)
else:
NSApp.dockTile().setBadgeLabel_("")
NSApp.dockTile().setContentView_(None)
icon = None
NSApp.setApplicationIconImage_(icon)
NSApp.dockTile().display()
def noteNewMessage(self, window):
if not NSApp.isActive():
self.missedChats += 1
self.updateDockTile()
def noteMissedCall(self):
self.missedCalls += 1
self.updateDockTile()
NSApp.requestUserAttention_(NSInformationalRequest)
def applicationShouldHandleReopen_hasVisibleWindows_(self, sender, flag):
if not flag:
self.contactsWindowController.showWindow_(None)
self.missedCalls = 0
self.missedChats = 0
self.updateDockTile()
return False
def applicationDidBecomeActive_(self, notif):
self.missedCalls = 0
self.missedChats = 0
self.updateDockTile()
def applicationDidFinishLaunching_(self, sender):
self.blinkMenu.setTitle_(self.applicationNamePrint)
config_file = ApplicationData.get('config')
self.icloud_manager = iCloudManager()
self.backend = SIPManager()
self.contactsWindowController.setup(self.backend)
while True:
try:
first_run = not os.path.exists(config_file)
self.contactsWindowController.first_run = first_run
self.backend.init()
self.backend.fetch_account()
accounts = AccountManager().get_accounts()
if not accounts or (first_run and accounts == [BonjourAccount()]):
self.wait_for_enrollment = True
self.enroll()
break
except FileParserError, exc:
BlinkLogger().log_warning(u"Error parsing configuration file: %s" % exc)
if NSRunAlertPanel(NSLocalizedString("Error", "Window title"),
NSLocalizedString("The configuration file is corrupted. You will need to replace it and re-enter your account information. \n\nYour current configuration file will be backed up to %s.corrupted. ", "Label") % config_file,
NSLocalizedString("Replace", "Button title"), NSLocalizedString("Quit", "Button title"), None) != NSAlertDefaultReturn:
NSApp.terminate_(None)
return
os.rename(config_file, config_file+".corrupted")
BlinkLogger().log_info(u"Renamed configuration file to %s" % config_file+".corrupted")
except BaseException, exc:
import traceback
print traceback.print_exc()
NSRunAlertPanel(NSLocalizedString("Error", "Window title"), NSLocalizedString("There was an error during startup of core functionality:\n%s", "Label") % exc,
NSLocalizedString("Quit", "Button title"), None, None)
NSApp.terminate_(None)
return
# window should be shown only after enrollment check
if self.wait_for_enrollment:
BlinkLogger().log_info('Starting User Interface')
self.contactsWindowController.model.moveBonjourGroupFirst()
self.contactsWindowController.showWindow_(None)
self.wait_for_enrollment = False
self.contactsWindowController.setupFinished()
def killSelfAfterTimeout_(self, arg):
time.sleep(5)
BlinkLogger().log_info(u"Forcing termination of apparently hanged Blink process")
os._exit(1)
def applicationShouldTerminate_(self, sender):
BlinkLogger().log_info('Application will terminate')
NSThread.detachNewThreadSelector_toTarget_withObject_("killSelfAfterTimeout:", self, None)
NotificationCenter().post_notification("BlinkShouldTerminate", None)
NotificationCenter().add_observer(self, name="SIPApplicationDidEnd")
SIPApplication().stop()
return NSTerminateLater
@allocate_autorelease_pool
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification)
def _NH_SIPEngineTransportDidDisconnect(self, notification):
BlinkLogger().log_info(u"%s connection %s <-> %s disconnected: %s" % (notification.data.transport.upper(), notification.data.local_address, notification.data.remote_address, notification.data.reason))
def _NH_SIPEngineTransportDidConnect(self, notification):
BlinkLogger().log_info(u"%s connection %s <-> %s established" % (notification.data.transport.upper(), notification.data.local_address, notification.data.remote_address))
def _NH_DNSNameserversDidChange(self, notification):
BlinkLogger().log_info(u"DNS servers changed to %s" % ", ".join(notification.data.nameservers))
def _NH_NetworkConditionsDidChange(self, notification):
pass
#BlinkLogger().log_info(u"Network conditions changed")
def _NH_SystemIPAddressDidChange(self, notification):
BlinkLogger().log_info(u"IP address changed to %s" % notification.data.new_ip_address)
if notification.data.new_ip_address is not None:
settings = SIPSimpleSettings()
app = SIPApplication()
app._initialize_tls()
app.engine.set_tcp_port(None)
app.engine.set_tcp_port(settings.sip.tcp_port)
def _NH_SIPApplicationWillEnd(self, notification):
call_in_thread('file-io', self.purge_temporary_files)
def _NH_CFGSettingsObjectDidChange(self, notification):
if 'gui.extended_debug' in notification.data.modified:
settings = SIPSimpleSettings()
self.debug = settings.gui.extended_debug
def _NH_SIPApplicationDidStart(self, notification):
settings = SIPSimpleSettings()
self.debug = settings.gui.extended_debug
settings.audio.enable_aec = settings.audio.echo_canceller.enabled
settings.audio.sound_card_delay = settings.audio.echo_canceller.tail_length
call_in_thread('file-io', self.purge_temporary_files)
def _NH_SIPApplicationDidEnd(self, notification):
call_in_gui_thread(NSApp.replyToApplicationShouldTerminate_, NSTerminateNow)
def applicationWillTerminate_(self, notification):
NotificationCenter().post_notification("BlinkWillTerminate", None)
def computerDidWake_(self, notification):
self.wake_up_timestamp = int(time.time())
NotificationCenter().post_notification("SystemDidWakeUpFromSleep", None)
def computerWillSleep_(self, notification):
NotificationCenter().post_notification("SystemWillSleep", None)
def callFromAddressBook_(self, notification):
url = notification.userInfo()["URI"]
name = notification.userInfo()["DisplayName"]
url = self.normalizeExternalURL(url)
BlinkLogger().log_info(u"Will start outgoing session to %s %s from Address Book" % (name, url))
if not self.ready:
self.urisToOpen.append((unicode(url), ('audio'), list()))
else:
self.contactsWindowController.joinConference(unicode(url), ('audio'))
@objc.IBAction
def orderFrontAboutPanel_(self, sender):
if not self.aboutPanel:
NSBundle.loadNibNamed_owner_("About", self)
version = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleShortVersionString"))
build = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleVersion"))
vdate = str(NSBundle.mainBundle().infoDictionary().objectForKey_("BlinkVersionDate"))
if self.applicationName == 'Blink Pro':
self.aboutVersion.setStringValue_("Version Pro %s build %s\n%s" % (version, build, vdate))
elif self.applicationName == 'Blink Lite':
self.aboutVersion.setStringValue_("Version Lite %s build %s\n%s" % (version, build, vdate))
else:
self.aboutVersion.setStringValue_("Version %s\n%s" % (version, vdate))
if self.applicationName == 'SIP2SIP':
self.aboutSlogan.setStringValue_(NSLocalizedString("Special edition of Blink SIP Client for SIP2SIP", "About panel label"))
self.aboutPanel.makeKeyAndOrderFront_(None)
def normalizeExternalURL(self, url):
return external_url_pattern.sub("", url)
def getURL_withReplyEvent_(self, event, replyEvent):
participants = set()
media_type = set()
url = event.descriptorForKeyword_(fourcharToInt('----')).stringValue()
url = self.normalizeExternalURL(url)
BlinkLogger().log_info(u"Will start outgoing session from external link: %s" % url)
url = urllib.unquote(url).replace(" ", "")
_split = url.split(';')
_url = []
for item in _split[:]:
if item.startswith("participant="):
puri = item.split("=")[1]
participants.add(puri)
elif item.startswith("media_type="):
m = item.split("=")[1]
media_type.add(m)
else:
_url.append(item)
_split.remove(item)
url = ";".join(_url)
if not self.ready:
self.urisToOpen.append((unicode(url), list(media_type), list(participants)))
else:
self.contactsWindowController.joinConference(unicode(url), list(media_type), list(participants))
def registerURLHandler(self):
event_class = event_id = fourcharToInt("GURL")
event_manager = NSAppleEventManager.sharedAppleEventManager()
event_manager.setEventHandler_andSelector_forEventClass_andEventID_(self, "getURL:withReplyEvent:", event_class, event_id)
bundleID = NSBundle.mainBundle().bundleIdentifier()
LaunchServices.LSSetDefaultHandlerForURLScheme("sip", bundleID)
LaunchServices.LSSetDefaultHandlerForURLScheme("tel", bundleID)