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