/
BlinkAppDelegate.py
379 lines (306 loc) · 15.3 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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from __future__ import with_statement
from Foundation import *
from AppKit import *
import LaunchServices
import objc
from random import randint
import os
import platform
import re
import shutil
import struct
import unicodedata
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 sipsimple.util import TimestampedNotificationData
from zope.interface import implements
from SIPManager import SIPManager
from iCloudManager import iCloudManager
from BlinkLogger import BlinkLogger
from SmileyManager import SmileyManager
from EnrollmentController import EnrollmentController
import PreferencesController
from DesktopSharingController import DesktopSharingController
from resources import ApplicationData, Resources
from util import allocate_autorelease_pool, call_in_gui_thread, run_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()
debug = False
blinkMenu = objc.IBOutlet()
ready = False
missedCalls = 0
missedChats = 0
urisToOpen = []
def init(self):
self = super(BlinkAppDelegate, self).init()
if self:
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)
ns_nc = NSNotificationCenter.defaultCenter()
ns_nc.addObserver_selector_name_object_(self, "userDefaultsDidChange:", "NSUserDefaultsDidChangeNotification", NSUserDefaults.standardUserDefaults())
nc = NotificationCenter()
nc.add_observer(self, name="SIPApplicationDidEnd")
self.applicationName = str(NSBundle.mainBundle().infoDictionary().objectForKey_("CFBundleExecutable"))
def purge_screenshots():
screenshots_folder = ApplicationData.get('.tmp_screenshots')
if os.path.exists(screenshots_folder):
try:
shutil.rmtree(screenshots_folder)
except EnvironmentError:
pass
userdef = NSUserDefaults.standardUserDefaults()
self.debug = userdef.boolForKey_("debug")
call_in_thread('file-io', purge_screenshots)
DesktopSharingController.vncServerPort = 5900
# Migrate configuration from Blink Lite to Blink Pro
# TODO: this will not work in the sandbox environment -adi
app_dir_name = (name for name in Resources.directory.split('/') if name.endswith('.app')).next()
path = unicodedata.normalize('NFC', NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, True)[0])
lite_path = os.path.join(path, 'Blink Lite')
pro_path = os.path.join(path, 'Blink Pro')
classic_path = os.path.join(path, 'Blink')
if os.path.isdir(classic_path):
migration_path = classic_path
migration_source = 'Blink'
elif os.path.isdir(lite_path):
migration_path = lite_path
migration_source = 'Blink Lite'
else:
migration_path = None
if self.applicationName == 'Blink Pro' and not os.path.exists(pro_path) and migration_path:
self.showMigrationPanel()
try:
shutil.copytree(migration_path, pro_path)
except shutil.Error, e:
BlinkLogger().log_info(u"Could not migrate configuration from %s: %s" % (migration_source, e))
else:
with open(os.path.join(pro_path, 'config'), 'r+') as f:
data = ''.join(f.readlines())
f.seek(0, 0)
f.truncate()
data = re.sub('Library/Application Support/%s' % migration_source, 'Library/Application Support/Blink Pro', data)
m = re.search('\/(?P<name>Blink[\w ]*)\.app', data)
if m:
name = m.groupdict()['name']
data = re.sub('%s.app/Contents/Resources' % name, '%s/Contents/Resources' % str(app_dir_name), data)
f.write(data)
self.hideMigrationPanel()
return self
def userDefaultsDidChange_(self, notification):
self.debug = NSUserDefaults.standardUserDefaults().boolForKey_("debug")
@run_in_gui_thread
def showMigrationPanel(self, text=None):
NSBundle.loadNibNamed_owner_("MigrationPanel", self)
self.migrationPanel.makeKeyAndOrderFront_(None)
self.migrationProgressWheel.startAnimation_(None)
if text is not None:
self.migrationText.setStringValue_(text)
else:
self.migrationText.setStringValue_('Migrating configuration and data from the previous version. Please wait...')
@run_in_gui_thread
def hideMigrationPanel(self):
self.migrationPanel.close()
# 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
NSApp.requestUserAttention_(NSCriticalRequest)
self.updateDockTile()
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 applicationWillFinishLaunching_(self, sender):
return
receiptPath = NSBundle.mainBundle().appStoreReceiptURL().path()
# TODO: receipt validation
if not NSFileManager.defaultManager().fileExistsAtPath_(receiptPath):
exit(173)
#https://github.com/roddi/ValidateStoreReceipt
def applicationDidFinishLaunching_(self, sender):
self.blinkMenu.setTitle_(self.applicationName)
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.enroll()
break
except FileParserError, exc:
BlinkLogger().log_warning(u"Error parsing configuration file: %s" % exc)
if NSRunAlertPanel("Error Reading Configurations",
"""The configuration file could not be read. The file could be corrupted or written by an older version of Blink. You might need to Replace it and re-enter your account information. Your old file will be backed up.""",
"Replace", "Quit", None) != NSAlertDefaultReturn:
NSApp.terminate_(None)
return
os.rename(config_file, config_file+".oldfmt")
BlinkLogger().log_info(u"Renamed configuration file to %s" % config_file+".oldfmt")
except BaseException, exc:
import traceback
print traceback.print_exc()
NSRunAlertPanel("Error", "There was an error during startup of core Blink functionality:\n%s" % exc,
"Quit", None, None)
NSApp.terminate_(None)
return
# window should be shown only after enrollment check
self.contactsWindowController.showWindow_(None)
self.contactsWindowController.setupFinished()
smileys = SmileyManager()
smileys.load_theme(str(NSBundle.mainBundle().resourcePath())+"/smileys" , "default")
def killSelfAfterTimeout_(self, arg):
# wait 4 seconds then kill self
import time
time.sleep(4)
import os
import signal
BlinkLogger().log_info(u"Forcing termination of apparently hanged Blink process")
os.kill(os.getpid(), signal.SIGTERM)
def applicationShouldTerminate_(self, sender):
self.contactsWindowController.closeAllSessions()
self.contactsWindowController.journal_replicator.save_journal_on_disk()
NSThread.detachNewThreadSelector_toTarget_withObject_("killSelfAfterTimeout:", self, 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_SIPApplicationDidEnd(self, notification):
call_in_gui_thread(NSApp.replyToApplicationShouldTerminate_, NSTerminateNow)
def applicationWillTerminate_(self, notification):
pass
def computerDidWake_(self, notification):
NotificationCenter().post_notification("SystemDidWakeUpFromSleep", None, TimestampedNotificationData())
def computerWillSleep_(self, notification):
NotificationCenter().post_notification("SystemWillSleep", None, TimestampedNotificationData())
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"))
self.aboutPanel.setTitle_('About %s' % self.applicationName)
if self.applicationName == 'Blink Pro':
target = "Pro"
elif self.applicationName == 'Blink Lite':
target = "Lite"
else:
target = ''
self.aboutVersion.setStringValue_("Version %s %s build %s\n%s" % (target, version, build, vdate))
self.aboutPanel.makeKeyAndOrderFront_(None)
def normalizeExternalURL(self, url):
return external_url_pattern.sub("", url)
def getURL_withReplyEvent_(self, event, replyEvent):
participants = set()
media = set()
url = event.descriptorForKeyword_(fourcharToInt('----')).stringValue()
url = self.normalizeExternalURL(url)
BlinkLogger().log_info(u"Will start outgoing session to %s from external link" % url)
_split = url.split(';')
_url = []
for item in _split[:]:
if item.startswith("participant="):
puri = item.split("=")[1]
participants.add(puri)
elif item.startswith("media="):
m = item.split("=")[1]
media.add(m)
else:
_url.append(item)
_split.remove(item)
url = ";".join(_url)
if not self.ready:
self.urisToOpen.append((unicode(url), list(media), list(participants)))
else:
self.contactsWindowController.joinConference(unicode(url), list(media), 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)