PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/PlugIn/AddressBook/AddressBookURL-plugin.py

https://github.com/wilane/blink-cocoa
Python | 124 lines | 123 code | 0 blank | 1 comment | 0 complexity | 1882300ad07f34852993c87a138ecfa0 MD5 | raw file
  1. """
  2. This plugin adds an 'Dial With Blink' label action to "url" properties in
  3. the AddressBook application.
  4. To install this plugin you have to build it (using 'python setup.py py2app')
  5. and then copy it to '~/Library/Address\ Book\ Plug-Ins' (this folder may
  6. not exist yet.
  7. """
  8. import AddressBook
  9. from AddressBook import *
  10. from AppKit import *
  11. import re
  12. import sys
  13. class NSLogger(object):
  14. closed = False
  15. encoding = 'UTF-8'
  16. mode = 'w'
  17. name = '<NSLogger>'
  18. newlines = None
  19. softspace = 0
  20. def close(self): pass
  21. def flush(self): pass
  22. def fileno(self): return -1
  23. def isatty(self): return False
  24. def next(self): raise IOError("cannot read from NSLogger")
  25. def read(self): raise IOError("cannot read from NSLogger")
  26. def readline(self): raise IOError("cannot read from NSLogger")
  27. def readlines(self): raise IOError("cannot read from NSLogger")
  28. def readinto(self, buf): raise IOError("cannot read from NSLogger")
  29. def seek(self, offset, whence=0): raise IOError("cannot seek in NSLogger")
  30. def tell(self): raise IOError("NSLogger does not have position")
  31. def truncate(self, size=0): raise IOError("cannot truncate NSLogger")
  32. def write(self, text):
  33. pool= Foundation.NSAutoreleasePool.alloc().init()
  34. if isinstance(text, basestring):
  35. text = text.rstrip()
  36. elif not isinstance(text, buffer):
  37. raise TypeError("write() argument must be a string or read-only buffer")
  38. Foundation.NSLog("%@", text)
  39. def writelines(self, lines):
  40. pool= Foundation.NSAutoreleasePool.alloc().init()
  41. for line in lines:
  42. if isinstance(line, basestring):
  43. line = line.rstrip()
  44. elif not isinstance(line, buffer):
  45. raise TypeError("writelines() argument must be a sequence of strings")
  46. Foundation.NSLog("%@", line)
  47. sys.stdout = NSLogger()
  48. sys.stderr = NSLogger()
  49. class BlinkProURLAddressDialerDelegate (NSObject):
  50. blink_bundle_id = 'com.agprojects.Blink'
  51. valid_pattern = re.compile("((sip:|sips:)?[\w\-\.+]+@(\w[\w\-]+\.)+[\w\-]+)")
  52. def init(self):
  53. self = super(BlinkProURLAddressDialerDelegate, self).init()
  54. if self:
  55. NSWorkspace.sharedWorkspace().notificationCenter().addObserver_selector_name_object_(self, "workspaceDidLaunchApplication:", NSWorkspaceDidLaunchApplicationNotification, None)
  56. self.selected_address = None
  57. self.selected_name = None
  58. return self
  59. def actionProperty(self):
  60. return kABURLsProperty
  61. def workspaceDidLaunchApplication_(self, notification):
  62. bundle = notification.userInfo()["NSApplicationBundleIdentifier"]
  63. if bundle == self.blink_bundle_id and self.selected_address and self.selected_name:
  64. print 'Calling %s at %s from AddressBook using Blink' % (self.selected_name, self.selected_address)
  65. userInfo = {'URI': self.selected_address,
  66. 'DisplayName': self.selected_name}
  67. NSDistributedNotificationCenter.defaultCenter().postNotificationName_object_userInfo_("CallSipAddressWithBlinkFromAddressBookNotification", "AddressBook", userInfo)
  68. def titleForPerson_identifier_(self, person, identifier):
  69. return u"Call with Blink Pro"
  70. def shouldEnableActionForPerson_identifier_(self, person, identifier):
  71. urls = person.valueForProperty_(AddressBook.kABURLsProperty)
  72. address = urls.valueForIdentifier_(identifier)
  73. return self.valid_pattern.match(address) is not None
  74. def performActionForPerson_identifier_(self, person, identifier):
  75. applications = NSWorkspace.sharedWorkspace().launchedApplications()
  76. try:
  77. app = (app for app in applications if app['NSApplicationBundleIdentifier'] == self.blink_bundle_id).next()
  78. except StopIteration:
  79. isBlinkRunning = False
  80. else:
  81. isBlinkRunning = True
  82. first = person.valueForProperty_(AddressBook.kABFirstNameProperty)
  83. last = person.valueForProperty_(AddressBook.kABLastNameProperty)
  84. middle = person.valueForProperty_(AddressBook.kABMiddleNameProperty)
  85. name = u""
  86. if first and last and middle:
  87. name += unicode(first) + " " + unicode(middle) + " " + unicode(last)
  88. elif first and last:
  89. name += unicode(first) + " " + unicode(last)
  90. elif last:
  91. name += unicode(last)
  92. elif first:
  93. name += unicode(first)
  94. company = person.valueForProperty_(AddressBook.kABOrganizationProperty)
  95. if company:
  96. name += " ("+unicode(company)+")" if name else unicode(company)
  97. urls = person.valueForProperty_(AddressBook.kABURLsProperty)
  98. address = urls.valueForIdentifier_(urls)
  99. if isBlinkRunning:
  100. print 'Calling %s at %s from AddressBook using Blink Pro' % (name, address)
  101. userInfo = {'URI': address,
  102. 'DisplayName': name
  103. }
  104. NSDistributedNotificationCenter.defaultCenter().postNotificationName_object_userInfo_("CallSipAddressWithBlinkFromAddressBookNotification", "AddressBook", userInfo)
  105. else:
  106. print 'Starting Blink Pro...'
  107. self.selected_address = address
  108. self.selected_name = name
  109. NSWorkspace.sharedWorkspace().launchApplication_("Blink Pro")