-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMSWindowManager.py
478 lines (403 loc) · 20.2 KB
/
SMSWindowManager.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from AppKit import NSApp, NSPortraitOrientation, NSFitPagination, NSOffState, NSOnState
from Foundation import (NSBundle,
NSImage,
NSLocalizedString,
NSNotFound,
NSObject,
NSPrintInfo,
NSTabViewItem,
NSWindowController)
import objc
from application.notification import IObserver, NotificationCenter, NotificationData
from application.python import Null
from zope.interface import implements
from sipsimple.account import AccountManager
from sipsimple.core import SIPURI
from sipsimple.configuration.settings import SIPSimpleSettings
from sipsimple.payloads.iscomposing import IsComposingMessage
from sipsimple.streams.applications.chat import CPIMMessage, CPIMParserError
from sipsimple.util import ISOTimestamp
from BlinkLogger import BlinkLogger
from SMSViewController import SMSViewController
from util import allocate_autorelease_pool, format_identity_to_string, html2txt, run_in_gui_thread
class SMSWindowController(NSWindowController):
implements(IObserver)
tabView = objc.IBOutlet()
tabSwitcher = objc.IBOutlet()
toolbar = objc.IBOutlet()
encryptionMenu = objc.IBOutlet()
encryptionIconMenuItem = objc.IBOutlet()
def initWithOwner_(self, owner):
self= super(SMSWindowController, self).init()
if self:
self._owner = owner
NSBundle.loadNibNamed_owner_("SMSSession", self)
self.notification_center = NotificationCenter()
self.notification_center.add_observer(self, name="BlinkShouldTerminate")
self.unreadMessageCounts = {}
return self
def selectedSessionController(self):
activeTab = self.tabView.selectedTabViewItem()
if activeTab:
return activeTab.identifier()
return None
def getTitle(self):
session = self.selectedSessionController()
if session:
display_name = session.display_name
sip_address = '%s@%s' % (session.target_uri.user, session.target_uri.host)
if display_name and display_name != sip_address:
title = NSLocalizedString("Instant Messages with %s", "Window Title") % display_name + " <%s>" % format_identity_to_string(session.target_uri)
else:
title = NSLocalizedString("Instant Messages with %s", "Window Title") % format_identity_to_string(session.target_uri)
else:
title = NSLocalizedString("Instant Messages", "Window Title")
return title
@allocate_autorelease_pool
@run_in_gui_thread
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification.sender, notification.data)
def _NH_BlinkShouldTerminate(self, sender, data):
if self.window():
self.window().orderOut_(self)
def menuWillOpen_(self, menu):
if menu == self.encryptionMenu:
settings = SIPSimpleSettings()
item = menu.itemWithTag_(1)
item.setHidden_(not settings.chat.enable_encryption)
item = menu.itemWithTag_(3)
item.setEnabled_(False)
item.setState_(NSOffState)
item.setHidden_(True)
item = menu.itemWithTag_(4)
item.setState_(NSOffState)
item.setEnabled_(False)
item.setState_(NSOffState)
item = menu.itemWithTag_(5)
item.setHidden_(True)
item = menu.itemWithTag_(6)
item.setHidden_(True)
item = menu.itemWithTag_(7)
item.setHidden_(True)
item = menu.itemWithTag_(9)
item.setHidden_(True)
selectedSession = self.selectedSessionController()
if selectedSession:
display_name = '%s@%s' % (selectedSession.target_uri.user, selectedSession.target_uri.host)
item.setHidden_(False)
item = menu.itemWithTag_(1)
my_fingerprint = selectedSession.otr_account.getPrivkey()
item.setTitle_(NSLocalizedString("My fingerprint is %s", "Menu item") % str(my_fingerprint))
item = menu.itemWithTag_(3)
item.setTitle_(NSLocalizedString("Always require OTR encryption with %s", "Menu item") % display_name)
if selectedSession.contact is not None:
item.setEnabled_(True)
item.setState_(NSOnState if selectedSession.require_encryption else NSOffState)
item.setHidden_(not settings.chat.enable_encryption)
else:
item.setEnabled_(False)
item.setHidden_(True)
item.setState_(NSOffState)
item = menu.itemWithTag_(4)
if settings.chat.enable_encryption:
item.setHidden_(False)
if selectedSession.require_encryption and selectedSession.is_encrypted:
item.setEnabled_(False)
else:
item.setEnabled_(True)
if selectedSession.otr_negotiation_in_progress:
item.setTitle_(NSLocalizedString("OTR negotiation in progress...", "Menu item"))
item.setEnabled_(False)
else:
item.setTitle_(NSLocalizedString("Activate OTR encryption for this session", "Menu item") if not selectedSession.is_encrypted else NSLocalizedString("Deactivate OTR encryption for this session", "Menu item"))
else:
item.setEnabled_(False)
item.setTitle_(NSLocalizedString("OTR encryption is disabled in Chat preferences", "Menu item"))
if settings.chat.enable_encryption:
ctx = selectedSession.otr_account.getContext(selectedSession.session_id)
fingerprint = ctx.getCurrentKey()
if fingerprint:
item = menu.itemWithTag_(6)
item.setHidden_(False)
item = menu.itemWithTag_(7)
item.setHidden_(False)
fingerprint_verified = selectedSession.otr_account.getTrust(selectedSession.remote_uri, str(fingerprint))
item.setEnabled_(False)
_t = NSLocalizedString("%s's fingerprint is ", "Menu item") % display_name
item.setTitle_( "%s %s" % (_t, fingerprint) if fingerprint is not None else NSLocalizedString("No Fingerprint Discovered", "Menu item"))
item = menu.itemWithTag_(5)
item.setEnabled_(True if fingerprint else False)
item.setHidden_(False)
item.setTitle_(NSLocalizedString("I have verified %s's fingerprint", "Menu item") % display_name)
item.setState_(NSOnState if fingerprint_verified else NSOffState)
item = menu.itemWithTag_(9)
item.setHidden_(False)
else:
item = menu.itemWithTag_(9)
item.setHidden_(True)
def noteNewMessageForSession_(self, session):
index = self.tabView.indexOfTabViewItemWithIdentifier_(session)
if index == NSNotFound:
return
tabItem = self.tabView.tabViewItemAtIndex_(index)
item = self.tabSwitcher.itemForTabViewItem_(tabItem)
if item:
if self.tabView.selectedTabViewItem() == tabItem:
item.setBadgeLabel_("")
else:
count = self.unreadMessageCounts[session] = self.unreadMessageCounts.get(session, 0) + 1
item.setBadgeLabel_(str(count))
def noteView_isComposing_(self, smsview, flag):
index = self.tabView.indexOfTabViewItemWithIdentifier_(smsview)
if index == NSNotFound:
return
tabItem = self.tabView.tabViewItemAtIndex_(index)
item = self.tabSwitcher.itemForTabViewItem_(tabItem)
if item:
item.setComposing_(flag)
def addViewer_(self, viewer):
tabItem = NSTabViewItem.alloc().initWithIdentifier_(viewer)
tabItem.setView_(viewer.getContentView())
sip_address = '%s@%s' % (viewer.target_uri.user, viewer.target_uri.host)
if viewer.display_name and viewer.display_name != sip_address:
tabItem.setLabel_("%s" % viewer.display_name)
else:
tabItem.setLabel_(format_identity_to_string(viewer.target_uri))
self.tabSwitcher.addTabViewItem_(tabItem)
self.tabSwitcher.selectLastTabViewItem_(None)
self.window().makeFirstResponder_(viewer.chatViewController.inputText)
def removeViewer_(self, viewer):
i = self.tabView.indexOfTabViewItemWithIdentifier_(viewer)
if i != NSNotFound:
item = self.tabView.tabViewItemAtIndex_(i)
self.tabSwitcher.removeTabViewItem_(item)
@property
def viewers(self):
return (item.identifier() for item in self.tabView.tabViewItems())
def close_(self, sender):
selected = self.selectedSessionController()
if self.unreadMessageCounts.has_key(selected):
del self.unreadMessageCounts[selected]
self.tabSwitcher.removeTabViewItem_(self.tabView.selectedTabViewItem())
if self.tabView.numberOfTabViewItems() == 0:
self.window().performClose_(None)
def tabView_shouldCloseTabViewItem_(self, sender, item):
if self.unreadMessageCounts.has_key(item.identifier()):
del self.unreadMessageCounts[item.identifier()]
return True
def tabView_didSelectTabViewItem_(self, sender, item):
self.window().setTitle_(self.getTitle())
if self.unreadMessageCounts.has_key(item.identifier()):
del self.unreadMessageCounts[item.identifier()]
self.noteNewMessageForSession_(item.identifier())
selectedSession = self.selectedSessionController()
if selectedSession:
selectedSession.updateEncryptionWidgets()
def tabViewDidChangeNumberOfTabViewItems_(self, tabView):
if tabView.numberOfTabViewItems() == 0:
self.window().performClose_(None)
def tabView_didDettachTabViewItem_atPosition_(self, tabView, item, pos):
if tabView.numberOfTabViewItems() > 1:
session = item.identifier()
window = SMSWindowManager().dettachSMSViewer(session)
if window:
window.window().setFrameOrigin_(pos)
def windowShouldClose_(self, sender):
for item in self.tabView.tabViewItems().copy():
self.tabSwitcher.removeTabViewItem_(item)
if self in SMSWindowManager().windows:
SMSWindowManager().windows.remove(self)
self.notification_center.remove_observer(self, name="BlinkShouldTerminate")
return True
@objc.IBAction
def userClickedEncryptionMenu_(self, sender):
# dispatch the click to the active session
selectedSession = self.selectedSessionController()
if selectedSession:
selectedSession.userClickedEncryptionMenu_(sender)
@objc.IBAction
def toolbarButtonClicked_(self, sender):
session = self.selectedSessionController()
contactWindow = self._owner._owner
if sender.itemIdentifier() == 'audio':
contactWindow.startSessionWithTarget(format_identity_to_string(session.target_uri))
elif sender.itemIdentifier() == 'video':
contactWindow.startSessionWithTarget(format_identity_to_string(session.target_uri), media_type="video")
elif sender.itemIdentifier() == 'smileys':
chatViewController = self.selectedSessionController().chatViewController
chatViewController.expandSmileys = not chatViewController.expandSmileys
sender.setImage_(NSImage.imageNamed_("smiley_on" if chatViewController.expandSmileys else "smiley_off"))
chatViewController.toggleSmileys(chatViewController.expandSmileys)
elif sender.itemIdentifier() == 'history' and NSApp.delegate().applicationName != 'Blink Lite':
contactWindow.showHistoryViewer_(None)
contactWindow.historyViewer.filterByURIs((format_identity_to_string(session.target_uri),))
@objc.IBAction
def printDocument_(self, sender):
if NSApp.delegate().applicationName == 'Blink Lite':
return
printInfo = NSPrintInfo.sharedPrintInfo()
printInfo.setTopMargin_(30)
printInfo.setBottomMargin_(30)
printInfo.setLeftMargin_(10)
printInfo.setRightMargin_(10)
printInfo.setOrientation_(NSPortraitOrientation)
printInfo.setHorizontallyCentered_(True)
printInfo.setVerticallyCentered_(False)
printInfo.setHorizontalPagination_(NSFitPagination)
printInfo.setVerticalPagination_(NSFitPagination)
NSPrintInfo.setSharedPrintInfo_(printInfo)
# print the content of the web view
print_view = self.selectedSessionController().chatViewController.outputView
print_view.mainFrame().frameView().documentView().print_(self)
SMSWindowManagerInstance = None
def SMSWindowManager():
global SMSWindowManagerInstance
if SMSWindowManagerInstance is None:
SMSWindowManagerInstance = SMSWindowManagerClass.alloc().init()
return SMSWindowManagerInstance
class SMSWindowManagerClass(NSObject):
implements(IObserver)
#__metaclass__ = Singleton
windows = []
received_call_ids = set()
def init(self):
self = super(SMSWindowManagerClass, self).init()
if self:
self.notification_center = NotificationCenter()
self.notification_center.add_observer(self, name="SIPEngineGotMessage")
return self
def setOwner_(self, owner):
self._owner = owner
def raiseLastWindowFront(self):
try:
window = self.windows[0]
except IndexError:
return False
window.window().makeKeyAndOrderFront_(None)
return True
def openMessageWindow(self, target, target_name, account, create_if_needed=True, note_new_message=True):
for window in self.windows:
for viewer in window.viewers:
if viewer.matchesTargetAccount(target, account):
break
else:
continue
break
else:
window, viewer = None, None
if not viewer and create_if_needed:
viewer = SMSViewController.alloc().initWithAccount_target_name_(account, target, target_name)
if not self.windows:
window = SMSWindowController.alloc().initWithOwner_(self)
self.windows.append(window)
else:
window = self.windows[0]
viewer.windowController = window
window.addViewer_(viewer)
elif viewer:
window = self.windowForViewer(viewer)
if window:
if note_new_message:
window.window().makeKeyAndOrderFront_(None)
NSApp.delegate().noteNewMessage(window)
return viewer
def dettachSMSViewer(self, viewer):
oldWindow = self.windowForViewer(viewer)
oldWindow.removeViewer_(viewer)
window = SMSWindowController.alloc().initWithOwner_(self)
self.windows.append(window)
window.addViewer_(viewer)
window.window().makeKeyAndOrderFront_(None)
return window
def windowForViewer(self, viewer):
for window in self.windows:
if viewer in window.viewers:
return window
else:
return None
@allocate_autorelease_pool
@run_in_gui_thread
def handle_notification(self, notification):
handler = getattr(self, '_NH_%s' % notification.name, Null)
handler(notification.sender, notification.data)
def _NH_SIPEngineGotMessage(self, sender, data):
account = AccountManager().find_account(data.request_uri)
if not account:
BlinkLogger().log_warning(u"Could not find local account for incoming SMS to %s, using default" % data.request_uri)
account = AccountManager().default_account
call_id = data.headers.get('Call-ID', Null).body
try:
self.received_call_ids.remove(call_id)
except KeyError:
self.received_call_ids.add(call_id)
else:
# drop duplicate message received
return
is_cpim = False
cpim_message = None
is_replication_message = False
if data.content_type == 'message/cpim':
try:
cpim_message = CPIMMessage.parse(data.body)
except CPIMParserError:
BlinkLogger().log_warning(u"Incoming SMS from %s to %s has invalid CPIM content" % format_identity_to_string(data.from_header), account.id)
return
else:
is_cpim = True
body = cpim_message.body
content_type = cpim_message.content_type
sender_identity = cpim_message.sender or data.from_header
if cpim_message.sender and data.from_header.uri == data.to_header.uri and data.from_header.uri == cpim_message.sender.uri:
is_replication_message = True
window_tab_identity = cpim_message.recipients[0] if cpim_message.recipients else data.to_header
else:
window_tab_identity = data.from_header
else:
body = data.body.decode('utf-8')
content_type = data.content_type
sender_identity = data.from_header
window_tab_identity = sender_identity
is_html = content_type == 'text/html'
if content_type in ('text/plain', 'text/html'):
pass
#BlinkLogger().log_info(u"Incoming SMS %s from %s to %s received" % (call_id, format_identity_to_string(sender_identity), account.id))
elif content_type == 'application/im-iscomposing+xml':
# body must not be utf-8 decoded
body = cpim_message.body if is_cpim else data.body
msg = IsComposingMessage.parse(body)
state = msg.state.value
refresh = msg.refresh.value if msg.refresh is not None else None
content_type = msg.content_type.value if msg.content_type is not None else None
last_active = msg.last_active.value if msg.last_active is not None else None
viewer = self.openMessageWindow(SIPURI.new(window_tab_identity.uri), window_tab_identity.display_name, account, create_if_needed=False, note_new_message=False)
if viewer:
viewer.gotIsComposing(self.windowForViewer(viewer), state, refresh, last_active)
return
else:
BlinkLogger().log_warning(u"Incoming SMS %s from %s to %s has unknown content-type %s" % (call_id, format_identity_to_string(data.from_header), account.id, data.content_type))
return
# display the message
note_new_message = False if is_replication_message else True
viewer = self.openMessageWindow(SIPURI.new(window_tab_identity.uri), window_tab_identity.display_name, account, note_new_message=note_new_message)
self.windowForViewer(viewer).noteNewMessageForSession_(viewer)
replication_state = None
replication_timestamp = None
if is_replication_message:
replicated_response_code = data.headers.get('X-Replication-Code', Null).body
if replicated_response_code == '202':
replication_state = 'deferred'
elif replicated_response_code == '200':
replication_state = 'delivered'
else:
replication_state = 'failed'
replicated_timestamp = data.headers.get('X-Replication-Timestamp', Null).body
try:
replication_timestamp = ISOTimestamp(replicated_timestamp)
except Exception:
replication_timestamp = ISOTimestamp.now()
window = self.windowForViewer(viewer).window()
viewer.gotMessage(sender_identity, call_id, body, is_html, is_replication_message, replication_timestamp, window=window)
self.windowForViewer(viewer).noteView_isComposing_(viewer, False)