| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import sys |
| 21 |
if sys.version_info[0] >= 3: |
| 22 |
import tkinter as tk |
| 23 |
from tkinter import ttk |
| 24 |
from tkinter import messagebox as msgbox |
| 25 |
else: |
| 26 |
import Tkinter as tk |
| 27 |
import tkMessageBox as msgbox |
| 28 |
import ttk |
| 29 |
|
| 30 |
import random |
| 31 |
import pjsua2 as pj |
| 32 |
import _pjsua2 |
| 33 |
import accountsetting |
| 34 |
import application |
| 35 |
import call |
| 36 |
import chat as ch |
| 37 |
|
| 38 |
write=sys.stdout.write |
| 39 |
|
| 40 |
|
| 41 |
class Account(pj.Account): |
| 42 |
""" |
| 43 |
High level Python Account object, derived from pjsua2's Account object. |
| 44 |
""" |
| 45 |
def __init__(self, app): |
| 46 |
pj.Account.__init__(self) |
| 47 |
self.app = app |
| 48 |
self.randId = random.randint(1, 9999) |
| 49 |
self.cfg = pj.AccountConfig() |
| 50 |
self.cfgChanged = False |
| 51 |
self.buddyList = [] |
| 52 |
self.chatList = [] |
| 53 |
self.deleting = False |
| 54 |
|
| 55 |
def findChat(self, uri_str): |
| 56 |
uri = ch.ParseSipUri(uri_str) |
| 57 |
if not uri: return None |
| 58 |
|
| 59 |
for chat in self.chatList: |
| 60 |
if chat.isUriParticipant(uri) and chat.isPrivate(): |
| 61 |
return chat |
| 62 |
return None |
| 63 |
|
| 64 |
def newChat(self, uri_str): |
| 65 |
uri = ch.ParseSipUri(uri_str) |
| 66 |
if not uri: return None |
| 67 |
|
| 68 |
chat = ch.Chat(self.app, self, uri) |
| 69 |
self.chatList.append(chat) |
| 70 |
self.app.updateWindowMenu() |
| 71 |
return chat |
| 72 |
|
| 73 |
def statusText(self): |
| 74 |
status = '?' |
| 75 |
if self.isValid(): |
| 76 |
ai = self.getInfo() |
| 77 |
if ai.regLastErr: |
| 78 |
status = self.app.ep.utilStrError(ai.regLastErr) |
| 79 |
elif ai.regIsActive: |
| 80 |
if ai.onlineStatus: |
| 81 |
if len(ai.onlineStatusText): |
| 82 |
status = ai.onlineStatusText |
| 83 |
else: |
| 84 |
status = "Online" |
| 85 |
else: |
| 86 |
status = "Registered" |
| 87 |
else: |
| 88 |
if ai.regIsConfigured: |
| 89 |
if ai.regStatus/100 == 2: |
| 90 |
status = "Unregistered" |
| 91 |
else: |
| 92 |
status = ai.regStatusText |
| 93 |
else: |
| 94 |
status = "Doesn't register" |
| 95 |
else: |
| 96 |
status = '- not created -' |
| 97 |
return status |
| 98 |
|
| 99 |
def onRegState(self, prm): |
| 100 |
self.app.updateAccount(self) |
| 101 |
|
| 102 |
def onIncomingCall(self, prm): |
| 103 |
c = call.Call(self, call_id=prm.callId) |
| 104 |
call_prm = pj.CallOpParam() |
| 105 |
call_prm.statusCode = 180 |
| 106 |
c.answer(call_prm) |
| 107 |
ci = c.getInfo() |
| 108 |
msg = "Incoming call for account '%s'" % self.cfg.idUri |
| 109 |
if msgbox.askquestion(msg, "Accept call from '%s'?" % (ci.remoteUri), default=msgbox.YES) == u'yes': |
| 110 |
call_prm.statusCode = 200 |
| 111 |
c.answer(call_prm) |
| 112 |
|
| 113 |
|
| 114 |
chat = self.findChat(ci.remoteUri) |
| 115 |
if not chat: chat = self.newChat(ci.remoteUri) |
| 116 |
|
| 117 |
chat.showWindow() |
| 118 |
chat.registerCall(ci.remoteUri, c) |
| 119 |
chat.updateCallState(c, ci) |
| 120 |
else: |
| 121 |
c.hangup(call_prm) |
| 122 |
|
| 123 |
def onInstantMessage(self, prm): |
| 124 |
chat = self.findChat(prm.fromUri) |
| 125 |
if not chat: chat = self.newChat(prm.fromUri) |
| 126 |
|
| 127 |
chat.showWindow() |
| 128 |
chat.addMessage(prm.fromUri, prm.msgBody) |
| 129 |
|
| 130 |
def onInstantMessageStatus(self, prm): |
| 131 |
if prm.code/100 == 2: return |
| 132 |
|
| 133 |
chat = self.findChat(prm.toUri) |
| 134 |
if not chat: |
| 135 |
write("=== IM status to " + prm.toUri + "cannot find chat\r\n") |
| 136 |
return |
| 137 |
|
| 138 |
chat.addMessage(None, "Failed sending message to '%s': %s" % (prm.toUri, prm.reason)) |
| 139 |
|
| 140 |
def onTypingIndication(self, prm): |
| 141 |
chat = self.findChat(prm.fromUri) |
| 142 |
if not chat: |
| 143 |
write("=== Incoming typing indication from " + prm.fromUri + "cannot find chat\r\n") |
| 144 |
return |
| 145 |
|
| 146 |
chat.setTypingIndication(prm.fromUri, prm.isTyping) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
class AccountListFrame(ttk.Frame): |
| 151 |
""" |
| 152 |
This implements a Frame which contains account list and buttons to operate |
| 153 |
on them (Add, Modify, Delete, etc.). |
| 154 |
""" |
| 155 |
def __init__(self, parent, app, acc_list = []): |
| 156 |
ttk.Frame.__init__(self, parent, name='acclist') |
| 157 |
self.app = app |
| 158 |
self.accList = acc_list |
| 159 |
self.accDeletedList = [] |
| 160 |
self.pack(expand='yes', fill='both') |
| 161 |
self._createWidgets() |
| 162 |
for acc in self.accList: |
| 163 |
self._showAcc(acc) |
| 164 |
|
| 165 |
def _createWidgets(self): |
| 166 |
self.tv = ttk.Treeview(self, columns=('ID', 'Registrar', 'Default'), selectmode='browse') |
| 167 |
self.tv.heading('#0', text='Priority') |
| 168 |
self.tv.heading(0, text='ID') |
| 169 |
self.tv.heading(1, text='Registrar') |
| 170 |
self.tv.heading(2, text='Default?') |
| 171 |
self.tv.column('#0', width=60) |
| 172 |
self.tv.column(0, width=300) |
| 173 |
self.tv.column(1, width=200) |
| 174 |
self.tv.column(2, width=60) |
| 175 |
self.tv.grid(column=0, row=0, rowspan=4, padx=5, pady=5) |
| 176 |
|
| 177 |
ttk.Button(self, text='Add..', command=self._onBtnAdd).grid(column=1, row=0, padx=5) |
| 178 |
ttk.Button(self, text='Settings..', command=self._onBtnSettings).grid(column=1, row=1) |
| 179 |
ttk.Button(self, text='Set Default', command=self._onBtnSetDefault).grid(column=1, row=2) |
| 180 |
ttk.Button(self, text='Delete..', command=self._onBtnDelete).grid(column=1, row=3) |
| 181 |
|
| 182 |
def _showAcc(self, acc): |
| 183 |
is_default = 'Yes' if acc.isValid() and acc.isDefault() else '' |
| 184 |
values = (acc.cfg.idUri, acc.cfg.regConfig.registrarUri, is_default) |
| 185 |
self.tv.insert('', 0, str(acc.randId), open=True, text=str(acc.cfg.priority), values=values) |
| 186 |
|
| 187 |
def updateAccount(self, acc): |
| 188 |
is_default = 'Yes' if acc.isValid() and acc.isDefault() else '' |
| 189 |
values = (acc.cfg.idUri, acc.cfg.regConfig.registrarUri, is_default) |
| 190 |
self.tv.item(str(acc.randId), text=str(acc.cfg.priority), values=values) |
| 191 |
|
| 192 |
def _getSelectedAcc(self): |
| 193 |
items = self.tv.selection() |
| 194 |
if not items: |
| 195 |
return None |
| 196 |
iid = int(items[0]) |
| 197 |
return [acc for acc in self.accList if acc.randId==iid][0] |
| 198 |
|
| 199 |
def _onBtnAdd(self): |
| 200 |
cfg = pj.AccountConfig() |
| 201 |
dlg = accountsetting.Dialog(self.master, cfg) |
| 202 |
if dlg.doModal(): |
| 203 |
acc = Account(self.app) |
| 204 |
acc.cfg = cfg |
| 205 |
self._showAcc(acc) |
| 206 |
self.accList.append(acc) |
| 207 |
self.cfgChanged = True |
| 208 |
|
| 209 |
def _onBtnSettings(self): |
| 210 |
acc = self._getSelectedAcc() |
| 211 |
if not acc: |
| 212 |
return |
| 213 |
dlg = accountsetting.Dialog(self.master, acc.cfg) |
| 214 |
if dlg.doModal(): |
| 215 |
self.updateAccount(acc) |
| 216 |
self.cfgChanged = True |
| 217 |
|
| 218 |
def _onBtnDelete(self): |
| 219 |
acc = self._getSelectedAcc() |
| 220 |
if not acc: |
| 221 |
return |
| 222 |
msg = "Do you really want to delete account '%s'" % acc.cfg.idUri |
| 223 |
if msgbox.askquestion('Delete account?', msg, default=msgbox.NO) != u'yes': |
| 224 |
return |
| 225 |
self.accList.remove(acc) |
| 226 |
self.accDeletedList.append(acc) |
| 227 |
self.tv.delete( (str(acc.randId),) ) |
| 228 |
|
| 229 |
def _onBtnSetDefault(self): |
| 230 |
acc = self._getSelectedAcc() |
| 231 |
if not acc: |
| 232 |
return |
| 233 |
if acc.isValid(): |
| 234 |
acc.setDefault() |
| 235 |
for acc in self.accList: |
| 236 |
self.updateAccount(acc) |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == '__main__': |
| 240 |
application.main() |