-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountSettings.py
264 lines (217 loc) · 11.4 KB
/
AccountSettings.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
# Copyright (C) 2009-2011 AG Projects. See LICENSE for details.
#
from AppKit import (NSFileHandlingPanelOKButton,
NSOpenPanel,
NSRunAlertPanel,
NSSavePanel,
NSWorkspace)
from Foundation import (NSBundle,
NSIndexSet,
NSNotFound,
NSObject,
NSLocalizedString,
NSURL,
NSURLCredential,
NSURLCredentialPersistenceForSession,
NSURLRequest,
NSURLRequestReloadIgnoringLocalAndRemoteCacheData)
from WebKit import WebActionOriginalURLKey
import objc
from BlinkLogger import BlinkLogger
import urlparse
accountWindowList = []
class AccountSettings(NSObject):
window = objc.IBOutlet()
webView = objc.IBOutlet()
spinWheel = objc.IBOutlet()
spinWheel2 = objc.IBOutlet()
errorText = objc.IBOutlet()
loadingText = objc.IBOutlet()
@classmethod
def isSupportedAccount_(cls, account):
return account.server.settings_url is not None
@classmethod
def createWithOwner_(cls, owner):
w = AccountSettings.alloc().initWithOwner_(owner)
accountWindowList.append(w)
return w
def initWithOwner_(self, owner):
self = super(AccountSettings, self).init()
if self:
self.owner = owner
NSBundle.loadNibNamed_owner_("AccountSettings", self)
return self
def close_(self, sender):
self.window.close()
def windowWillClose_(self, notification):
if self.window in accountWindowList:
accountWindowList.remove(self.window)
def showSettingsForAccount_(self, account):
if account.server.settings_url is None:
return
query_string = "realm=%s&tab=settings&user_agent=blink" % account.id.domain
if account.server.settings_url.query:
query_string = "%s&%s" % (account.server.settings_url.query, query_string)
url = urlparse.urlunparse(account.server.settings_url[:4] + (query_string,) + account.server.settings_url[5:])
url = NSURL.URLWithString_(url)
request = NSURLRequest.requestWithURL_cachePolicy_timeoutInterval_(url, NSURLRequestReloadIgnoringLocalAndRemoteCacheData, 15)
self.showAccountRequest(account, request)
def showAccountRequest(self, account, request):
self._account = account
self._authRequestCount = 0
self.webView.setHidden_(True)
self.loadingText.setHidden_(False)
self.spinWheel.setHidden_(False)
self.spinWheel.startAnimation_(None)
self.errorText.setHidden_(True)
self.window.setTitle_(NSLocalizedString("Server Tools For %s", "Window title") % self._account.id)
self.webView.mainFrame().loadRequest_(request)
self.window.makeKeyAndOrderFront_(self)
def showPSTNAccessforAccount_(self, account):
if account.server.settings_url is None:
return
self._account = account
self.webView.setHidden_(True)
self.loadingText.setHidden_(False)
self.spinWheel.setHidden_(False)
self.spinWheel.startAnimation_(None)
self.errorText.setHidden_(True)
self.window.setTitle_(NSLocalizedString("Server Tools For %s", "Window title") % self._account.id)
query_string = "realm=%s&tab=payments&user_agent=blink" % self._account.id
if account.server.settings_url.query:
query_string = "%s&%s" % (account.server.settings_url.query, query_string)
url = urlparse.urlunparse(account.server.settings_url[:4] + (query_string,) + account.server.settings_url[5:])
url = NSURL.URLWithString_(url)
request = NSURLRequest.requestWithURL_cachePolicy_timeoutInterval_(url, NSURLRequestReloadIgnoringLocalAndRemoteCacheData, 15)
self.webView.mainFrame().loadRequest_(request)
self.window.makeKeyAndOrderFront_(self)
def showIncomingCall(self, session, url):
self._account = session.account
self.webView.setHidden_(True)
self.loadingText.setHidden_(False)
self.spinWheel.setHidden_(False)
self.spinWheel.startAnimation_(None)
self.errorText.setHidden_(True)
_t = "%s <%s@%s>" % (session.remote_identity.display_name, session.remote_identity.uri.user, session.remote_identity.uri.host)
self.window.setTitle_(NSLocalizedString("Incoming Call From %s", "Window title") % _t)
url = NSURL.URLWithString_(url)
request = NSURLRequest.requestWithURL_cachePolicy_timeoutInterval_(url, NSURLRequestReloadIgnoringLocalAndRemoteCacheData, 15)
self.webView.mainFrame().loadRequest_(request)
self.window.makeKeyAndOrderFront_(self)
def webView_runOpenPanelForFileButtonWithResultListener_(self, sender, resultListener):
panel = NSOpenPanel.openPanel()
if panel.runModal() == NSFileHandlingPanelOKButton:
resultListener.chooseFilename_(panel.filename())
else:
resultListener.cancel()
def webView_didStartProvisionalLoadForFrame_(self, sender, frame):
self._authRequestCount = 0
self.errorText.setHidden_(True)
if self.spinWheel.isHidden():
self.spinWheel2.startAnimation_(None)
def webView_didFinishLoadForFrame_(self, sender, frame):
self.spinWheel.stopAnimation_(None)
self.loadingText.setHidden_(True)
self.spinWheel.setHidden_(True)
self.webView.setHidden_(False)
self.spinWheel2.stopAnimation_(None)
def webView_didFailProvisionalLoadWithError_forFrame_(self, sender, error, frame):
self.spinWheel.stopAnimation_(None)
self.spinWheel2.stopAnimation_(None)
self.loadingText.setHidden_(True)
self.spinWheel.setHidden_(True)
e = error.localizedDescription()
self.errorText.setStringValue_(NSLocalizedString("Could not load page: %s", "Label") % e)
self.errorText.setHidden_(False)
BlinkLogger().log_error(u"Could not load Server Tools page: %s" % error)
def webView_didFailLoadWithError_forFrame_(self, sender, error, frame):
self.spinWheel.stopAnimation_(None)
self.spinWheel2.stopAnimation_(None)
self.loadingText.setHidden_(True)
self.spinWheel.setHidden_(True)
self.errorText.setHidden_(False)
e = error.localizedDescription()
self.errorText.setStringValue_(NSLocalizedString("Could not load page: %s", "Label") % e)
BlinkLogger().log_error(u"Could not load Server Tools page: %s" % error)
def webView_createWebViewWithRequest_(self, sender, request):
window = AccountSettings.createWithOwner_(self.owner)
window.showAccountRequest(self._account, request)
return window.webView
def webView_resource_didReceiveAuthenticationChallenge_fromDataSource_(self, sender, identifier, challenge, dataSource):
self._authRequestCount += 1
if self._authRequestCount > 2:
BlinkLogger().log_debug(u"Could not load Server Tools page: authentication failure")
self.errorText.setHidden_(False)
e = NSLocalizedString("Authentication failure", "Label")
self.errorText.setStringValue_(NSLocalizedString("Could not load page: %s", "Label") % e)
self.spinWheel.stopAnimation_(None)
self.spinWheel2.stopAnimation_(None)
self.loadingText.setHidden_(True)
else:
credential = NSURLCredential.credentialWithUser_password_persistence_(self._account.id.username, self._account.server.web_password or self._account.auth.password, NSURLCredentialPersistenceForSession)
challenge.sender().useCredential_forAuthenticationChallenge_(credential, challenge)
def webView_decidePolicyForNewWindowAction_request_newFrameName_decisionListener_(self, webView, info, request, frame, listener):
try:
theURL = info[WebActionOriginalURLKey]
if self._account.server.settings_url is not None and theURL.host() == self._account.server.settings_url.hostname:
listener.use()
elif self._account.web_alert.alert_url is not None and theURL.host() in self._account.web_alert.alert_url:
listener.use()
else:
# use system wide web browser
NSWorkspace.sharedWorkspace().openURL_(theURL)
listener.ignore()
except KeyError:
pass
def webView_decidePolicyForNavigationAction_request_frame_decisionListener_(self, webView, info, request, frame, listener):
# intercept when user clicks on links so that we process them in different ways
try:
theURL = info[WebActionOriginalURLKey]
if self._account.server.settings_url is not None and theURL.host() == self._account.server.settings_url.hostname:
listener.use()
elif self._account.web_alert.alert_url is not None and theURL.host() in self._account.web_alert.alert_url:
listener.use()
else:
# use system wide web browser
NSWorkspace.sharedWorkspace().openURL_(theURL)
listener.ignore()
except KeyError:
pass
def webView_resource_didCancelAuthenticationChallenge_fromDataSource_(self, sender, identifier, challenge, dataSource):
BlinkLogger().log_info(u"Cancelled authentication request")
# download delegate
def download_decideDestinationWithSuggestedFilename_(self, download, filename):
panel = NSSavePanel.savePanel()
panel.setTitle_(NSLocalizedString("Download File", "Window title"))
if panel.runModalForDirectory_file_("", filename) == NSFileHandlingPanelOKButton:
download.setDestination_allowOverwrite_(panel.filename(), True)
BlinkLogger().log_info(u"Downloading file to %s" % panel.filename())
else:
download.cancel()
BlinkLogger().log_info(u"Download cancelled")
def downloadDidBegin_(self, download):
BlinkLogger().log_info(u"Download started...")
def downloadDidFinish_(self, download):
BlinkLogger().log_info(u"Download finished")
def download_didReceiveDataOfLength_(self, download, length):
pass
def download_didFailWithError_(self, download, error):
download.cancel()
BlinkLogger().log_info(u"Download error: %s" % error.localizedDescription())
e = error.localizedDescription()
NSRunAlertPanel(NSLocalizedString("Error", "Window title"), NSLocalizedString("Error downloading file: %s", "Label") % e, NSLocalizedString("OK", "Button title"), "", "")
# API exported to webpage. Be careful with what you export.
def addContact_withDisplayName_(self, uri, display_name):
BlinkLogger().log_info(u"Adding contact %s <%s>" % (display_name, uri))
contact = self.owner.model.addContact(uri=[(uri, 'sip')], name=display_name)
self.owner.contactOutline.reloadData()
row = self.owner.contactOutline.rowForItem_(contact)
if row != NSNotFound:
self.owner.contactOutline.selectRowIndexes_byExtendingSelection_(NSIndexSet.indexSetWithIndex_(row), False)
self.owner.contactOutline.scrollRowToVisible_(row)
def isSelectorExcludedFromWebScript_(self, sel):
if sel == "addContact:withDisplayName:":
return False
return True
def webView_didClearWindowObject_forFrame_(self, sender, windowObject, frame):
windowObject.setValue_forKey_(self, "blink")