/
AddContactController.py
151 lines (115 loc) · 5.43 KB
/
AddContactController.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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from AppKit import *
from Foundation import *
from operator import attrgetter
from sipsimple.account import AccountManager, BonjourAccount
ICON_SIZE=48
class MyImageThing(NSImageView):
def mouseDown_(self, event):
super(MyImageThing, self).mouseDown_(event)
self.target().performSelector_withObject_(self.action(), self)
class AddContactController(NSObject):
window = objc.IBOutlet()
addButton = objc.IBOutlet()
addressText = objc.IBOutlet()
nameText = objc.IBOutlet()
groupCombo = objc.IBOutlet()
photoImage = objc.IBOutlet()
preferredMedia = objc.IBOutlet()
storagePlacePopUp = objc.IBOutlet()
aliasText = objc.IBOutlet()
defaultPhotoImage = NSImage.imageNamed_("NSUser")
def __new__(cls, *args, **kwargs):
return cls.alloc().init()
def __init__(self, contact, group):
NSBundle.loadNibNamed_owner_("AddContact", self)
self.storagePlacePopUp.removeAllItems()
self.storagePlacePopUp.addItemWithTitle_("None")
item = self.storagePlacePopUp.lastItem()
item.setRepresentedObject_(None)
accounts = [acct for acct in AccountManager().get_accounts() if not isinstance(acct, BonjourAccount)]
for account in sorted(accounts, key=attrgetter('order')):
self.storagePlacePopUp.addItemWithTitle_(u'%s'%account.id)
item = self.storagePlacePopUp.lastItem()
item.setRepresentedObject_(account)
# display the contact data
self.addressText.setStringValue_(contact.uri or "")
self.nameText.setStringValue_(contact.name or "")
self.groupCombo.setStringValue_(group or "")
self.photoImage.setImage_(contact.icon or self.defaultPhotoImage)
self.aliasText.setStringValue_("; ".join(contact.aliases))
index = self.storagePlacePopUp.indexOfItemWithRepresentedObject_(contact.stored_in_account) if contact.stored_in_account else 0
self.storagePlacePopUp.selectItemAtIndex_(index if index >0 else 0)
self.preferredMedia.selectCellWithTag_(2 if contact.preferred_media == "chat" else 1)
self.contact = contact
def setGroupNames(self, groups):
current = self.groupCombo.stringValue()
self.groupCombo.removeAllItems()
if groups:
self.groupCombo.addItemsWithObjectValues_(NSArray.arrayWithObjects_(*groups))
self.groupCombo.selectItemAtIndex_(0)
if current:
self.groupCombo.setStringValue_(current)
def runModal(self):
self.controlTextDidChange_(None)
rc = NSApp.runModalForWindow_(self.window)
self.window.orderOut_(self)
if rc == NSOKButton:
self.contact.setURI(str(self.addressText.stringValue()))
self.contact.setDetail(str(self.addressText.stringValue()))
self.contact.setName(unicode(self.nameText.stringValue()))
group = unicode(self.groupCombo.stringValue())
text_items = (item.strip() for item in str(self.aliasText.stringValue()).split(";"))
self.contact.setAliases((alias for alias in text_items if alias))
if self.photoImage.image() == self.defaultPhotoImage:
self.contact.setIcon(None)
else:
self.contact.setIcon(self.photoImage.image())
self.contact.saveIcon()
media = "audio" if self.preferredMedia.selectedCell().tag() == 1 else "chat"
self.contact.setPreferredMedia(media)
if self.storagePlacePopUp.selectedItem():
self.contact.stored_in_account = self.storagePlacePopUp.selectedItem().representedObject()
return True, group
return False, None
def comboBoxWillDismiss_(self, notification):
self.controlTextDidChange_(notification)
def controlTextDidChange_(self, notification):
try:
addr = str(self.addressText.stringValue())
except:
addr = None
group = self.groupCombo.objectValueOfSelectedItem()
if not group:
group = self.groupCombo.stringValue()
if group:
group = unicode(group)
addButton = self.window.contentView().viewWithTag_(10)
if not addr or not group:
addButton.setEnabled_(False)
else:
addButton.setEnabled_(True)
def windowShouldClose_(self, sender):
NSApp.stopModalWithCode_(NSCancelButton)
return True
@objc.IBAction
def buttonClicked_(self, sender):
if sender.tag() == 20: # ch icon
panel = NSOpenPanel.openPanel()
panel.setTitle_("Select Contact Icon")
if panel.runModalForTypes_(NSArray.arrayWithObjects_("tiff", "png", "jpeg", "jpg")) == NSFileHandlingPanelOKButton:
path = panel.filename()
image = NSImage.alloc().initWithContentsOfFile_(path)
self.photoImage.setImage_(image)
elif sender.tag() == 21: # clear icon
self.photoImage.setImage_(self.defaultPhotoImage)
elif sender.tag() == 10:
NSApp.stopModalWithCode_(NSOKButton)
else:
NSApp.stopModalWithCode_(NSCancelButton)
class EditContactController(AddContactController):
def __init__(self, contact, group):
AddContactController.__init__(self, contact, group)
self.window.setTitle_("Edit Contact")
self.addButton.setTitle_("OK")