Changeset 5638 for pjproject/trunk/pjsip-apps/src/pygui/application.py
- Timestamp:
- Aug 2, 2017 9:45:09 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/pygui/application.py
r4798 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 pjsua2 as pj … … 45 45 # http://lists.pjsip.org/pipermail/pjsip_lists.pjsip.org/2014-March/017223.html 46 46 USE_THREADS = False 47 47 48 write=sys.stdout.write 49 48 50 class Application(ttk.Frame): 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 self.quitting = False 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 51 """ 52 The Application main frame. 53 """ 54 def __init__(self): 55 global USE_THREADS 56 ttk.Frame.__init__(self, name='application', width=300, height=500) 57 self.pack(expand='yes', fill='both') 58 self.master.title('pjsua2 Demo') 59 self.master.geometry('500x500+100+100') 60 61 # Logger 62 self.logger = log.Logger() 63 64 # Accounts 65 self.accList = [] 66 67 # GUI variables 68 self.showLogWindow = tk.IntVar(value=0) 69 self.quitting = False 70 71 # Construct GUI 72 self._createWidgets() 73 74 # Log window 75 self.logWindow = log.LogWindow(self) 76 self._onMenuShowHideLogWindow() 77 78 # Instantiate endpoint 79 self.ep = endpoint.Endpoint() 80 self.ep.libCreate() 81 82 # Default config 83 self.appConfig = settings.AppConfig() 84 if USE_THREADS: 85 self.appConfig.epConfig.uaConfig.threadCnt = 1 86 self.appConfig.epConfig.uaConfig.mainThreadOnly = False 87 else: 88 self.appConfig.epConfig.uaConfig.threadCnt = 0 89 self.appConfig.epConfig.uaConfig.mainThreadOnly = True 90 self.appConfig.epConfig.logConfig.writer = self.logger 91 self.appConfig.epConfig.logConfig.filename = "pygui.log" 92 self.appConfig.epConfig.logConfig.fileFlags = pj.PJ_O_APPEND 93 self.appConfig.epConfig.logConfig.level = 5 94 self.appConfig.epConfig.logConfig.consoleLevel = 5 95 96 def saveConfig(self, filename='pygui.js'): 97 # Save disabled accounts since they are not listed in self.accList 98 disabled_accs = [ac for ac in self.appConfig.accounts if not ac.enabled] 99 self.appConfig.accounts = [] 100 101 # Get account configs from active accounts 102 for acc in self.accList: 103 acfg = settings.AccConfig() 104 acfg.enabled = True 105 acfg.config = acc.cfg 106 for bud in acc.buddyList: 107 acfg.buddyConfigs.append(bud.cfg) 108 self.appConfig.accounts.append(acfg) 109 110 # Put back disabled accounts 111 self.appConfig.accounts.extend(disabled_accs) 112 # Save 113 self.appConfig.saveFile(filename) 114 115 def start(self, cfg_file='pygui.js'): 116 global USE_THREADS 117 # Load config 118 if cfg_file and os.path.exists(cfg_file): 119 self.appConfig.loadFile(cfg_file) 120 121 if USE_THREADS: 122 self.appConfig.epConfig.uaConfig.threadCnt = 1 123 self.appConfig.epConfig.uaConfig.mainThreadOnly = False 124 else: 125 self.appConfig.epConfig.uaConfig.threadCnt = 0 126 self.appConfig.epConfig.uaConfig.mainThreadOnly = True 127 self.appConfig.epConfig.uaConfig.threadCnt = 0 128 self.appConfig.epConfig.uaConfig.mainThreadOnly = True 129 self.appConfig.epConfig.logConfig.writer = self.logger 130 self.appConfig.epConfig.logConfig.level = 5 131 self.appConfig.epConfig.logConfig.consoleLevel = 5 132 133 # Initialize library 134 self.appConfig.epConfig.uaConfig.userAgent = "pygui-" + self.ep.libVersion().full; 135 self.ep.libInit(self.appConfig.epConfig) 136 self.master.title('pjsua2 Demo version ' + self.ep.libVersion().full) 137 138 # Create transports 139 if self.appConfig.udp.enabled: 140 self.ep.transportCreate(self.appConfig.udp.type, self.appConfig.udp.config) 141 if self.appConfig.tcp.enabled: 142 self.ep.transportCreate(self.appConfig.tcp.type, self.appConfig.tcp.config) 143 if self.appConfig.tls.enabled: 144 self.ep.transportCreate(self.appConfig.tls.type, self.appConfig.tls.config) 145 146 # Add accounts 147 for cfg in self.appConfig.accounts: 148 if cfg.enabled: 149 self._createAcc(cfg.config) 150 acc = self.accList[-1] 151 for buddy_cfg in cfg.buddyConfigs: 152 self._createBuddy(acc, buddy_cfg) 153 154 # Start library 155 self.ep.libStart() 156 157 # Start polling 158 if not USE_THREADS: 159 self._onTimer() 160 161 def updateAccount(self, acc): 162 if acc.deleting: 163 return # ignore 164 iid = str(acc.randId) 165 text = acc.cfg.idUri 166 status = acc.statusText() 167 168 values = (status,) 169 if self.tv.exists(iid): 170 self.tv.item(iid, text=text, values=values) 171 else: 172 self.tv.insert('', 'end', iid, open=True, text=text, values=values) 173 174 def updateBuddy(self, bud): 175 iid = 'buddy' + str(bud.randId) 176 text = bud.cfg.uri 177 status = bud.statusText() 178 179 values = (status,) 180 if self.tv.exists(iid): 181 self.tv.item(iid, text=text, values=values) 182 else: 183 self.tv.insert(str(bud.account.randId), 'end', iid, open=True, text=text, values=values) 184 185 def _createAcc(self, acc_cfg): 186 acc = account.Account(self) 187 acc.cfg = acc_cfg 188 self.accList.append(acc) 189 self.updateAccount(acc) 190 acc.create(acc.cfg) 191 acc.cfgChanged = False 192 self.updateAccount(acc) 193 194 def _createBuddy(self, acc, buddy_cfg): 195 bud = buddy.Buddy(self) 196 bud.cfg = buddy_cfg 197 bud.account = acc 198 bud.create(acc, bud.cfg) 199 self.updateBuddy(bud) 200 acc.buddyList.append(bud) 201 202 def _createWidgets(self): 203 self._createAppMenu() 204 205 # Main pane, a Treeview 206 self.tv = ttk.Treeview(self, columns=('Status'), show='tree') 207 self.tv.pack(side='top', fill='both', expand='yes', padx=5, pady=5) 208 209 self._createContextMenu() 210 211 # Handle close event 212 self.master.protocol("WM_DELETE_WINDOW", self._onClose) 213 214 def _createAppMenu(self): 215 # Main menu bar 216 top = self.winfo_toplevel() 217 self.menubar = tk.Menu() 218 top.configure(menu=self.menubar) 219 220 # File menu 221 file_menu = tk.Menu(self.menubar, tearoff=False) 222 self.menubar.add_cascade(label="File", menu=file_menu) 223 file_menu.add_command(label="Add account..", command=self._onMenuAddAccount) 224 file_menu.add_checkbutton(label="Show/hide log window", command=self._onMenuShowHideLogWindow, variable=self.showLogWindow) 225 file_menu.add_separator() 226 file_menu.add_command(label="Settings...", command=self._onMenuSettings) 227 file_menu.add_command(label="Save Settings", command=self._onMenuSaveSettings) 228 file_menu.add_separator() 229 file_menu.add_command(label="Quit", command=self._onMenuQuit) 230 231 # Window menu 232 self.window_menu = tk.Menu(self.menubar, tearoff=False) 233 self.menubar.add_cascade(label="Window", menu=self.window_menu) 234 235 # Help menu 236 help_menu = tk.Menu(self.menubar, tearoff=False) 237 self.menubar.add_cascade(label="Help", menu=help_menu) 238 help_menu.add_command(label="About", underline=2, command=self._onMenuAbout) 239 240 def _showChatWindow(self, chat_inst): 241 chat_inst.showWindow() 242 243 def updateWindowMenu(self): 244 # Chat windows 245 self.window_menu.delete(0, tk.END) 246 for acc in self.accList: 247 for c in acc.chatList: 248 cmd = lambda arg=c: self._showChatWindow(arg) 249 self.window_menu.add_command(label=c.title, command=cmd) 250 251 def _createContextMenu(self): 252 top = self.winfo_toplevel() 253 254 # Create Account context menu 255 self.accMenu = tk.Menu(top, tearoff=False) 256 # Labels, must match with _onAccContextMenu() 257 labels = ['Unregister', 'Reregister', 'Add buddy...', '-', 258 'Online', 'Invisible', 'Away', 'Busy', '-', 259 'Settings...', '-', 260 'Delete...'] 261 for label in labels: 262 if label=='-': 263 self.accMenu.add_separator() 264 else: 265 cmd = lambda arg=label: self._onAccContextMenu(arg) 266 self.accMenu.add_command(label=label, command=cmd) 267 268 # Create Buddy context menu 269 # Labels, must match with _onBuddyContextMenu() 270 self.buddyMenu = tk.Menu(top, tearoff=False) 271 labels = ['Audio call', 'Send instant message', '-', 272 'Subscribe', 'Unsubscribe', '-', 273 'Settings...', '-', 274 'Delete...'] 275 276 for label in labels: 277 if label=='-': 278 self.buddyMenu.add_separator() 279 else: 280 cmd = lambda arg=label: self._onBuddyContextMenu(arg) 281 self.buddyMenu.add_command(label=label, command=cmd) 282 283 if (top.tk.call('tk', 'windowingsystem')=='aqua'): 284 self.tv.bind('<2>', self._onTvRightClick) 285 self.tv.bind('<Control-1>', self._onTvRightClick) 286 else: 287 self.tv.bind('<3>', self._onTvRightClick) 288 self.tv.bind('<Double-Button-1>', self._onTvDoubleClick) 289 290 def _getSelectedAccount(self): 291 items = self.tv.selection() 292 if not items: 293 return None 294 try: 295 iid = int(items[0]) 296 except: 297 return None 298 accs = [acc for acc in self.accList if acc.randId==iid] 299 if not accs: 300 return None 301 return accs[0] 302 303 def _getSelectedBuddy(self): 304 items = self.tv.selection() 305 if not items: 306 return None 307 try: 308 iid = int(items[0][5:]) 309 iid_parent = int(self.tv.parent(items[0])) 310 except: 311 return None 312 313 accs = [acc for acc in self.accList if acc.randId==iid_parent] 314 if not accs: 315 return None 316 317 buds = [b for b in accs[0].buddyList if b.randId==iid] 318 if not buds: 319 return None 320 321 return buds[0] 322 323 def _onTvRightClick(self, event): 324 iid = self.tv.identify_row(event.y) 325 #iid = self.tv.identify('item', event.x, event.y) 326 if iid: 327 self.tv.selection_set( (iid,) ) 328 acc = self._getSelectedAccount() 329 if acc: 330 self.accMenu.post(event.x_root, event.y_root) 331 else: 332 # A buddy is selected 333 self.buddyMenu.post(event.x_root, event.y_root) 334 335 def _onTvDoubleClick(self, event): 336 iid = self.tv.identify_row(event.y) 337 if iid: 338 self.tv.selection_set( (iid,) ) 339 acc = self._getSelectedAccount() 340 if acc: 341 self.cfgChanged = False 342 dlg = accountsetting.Dialog(self.master, acc.cfg) 343 if dlg.doModal(): 344 self.updateAccount(acc) 345 acc.modify(acc.cfg) 346 else: 347 bud = self._getSelectedBuddy() 348 acc = bud.account 349 chat = acc.findChat(bud.cfg.uri) 350 if not chat: 351 chat = acc.newChat(bud.cfg.uri) 352 chat.showWindow() 353 354 def _onAccContextMenu(self, label): 355 acc = self._getSelectedAccount() 356 if not acc: 357 return 358 359 if label=='Unregister': 360 acc.setRegistration(False) 361 elif label=='Reregister': 362 acc.setRegistration(True) 363 elif label=='Online': 364 ps = pj.PresenceStatus() 365 ps.status = pj.PJSUA_BUDDY_STATUS_ONLINE 366 acc.setOnlineStatus(ps) 367 elif label=='Invisible': 368 ps = pj.PresenceStatus() 369 ps.status = pj.PJSUA_BUDDY_STATUS_OFFLINE 370 acc.setOnlineStatus(ps) 371 elif label=='Away': 372 ps = pj.PresenceStatus() 373 ps.status = pj.PJSUA_BUDDY_STATUS_ONLINE 374 ps.activity = pj.PJRPID_ACTIVITY_AWAY 375 ps.note = "Away" 376 acc.setOnlineStatus(ps) 377 elif label=='Busy': 378 ps = pj.PresenceStatus() 379 ps.status = pj.PJSUA_BUDDY_STATUS_ONLINE 380 ps.activity = pj.PJRPID_ACTIVITY_BUSY 381 ps.note = "Busy" 382 acc.setOnlineStatus(ps) 383 elif label=='Settings...': 384 self.cfgChanged = False 385 dlg = accountsetting.Dialog(self.master, acc.cfg) 386 if dlg.doModal(): 387 self.updateAccount(acc) 388 acc.modify(acc.cfg) 389 elif label=='Delete...': 390 msg = "Do you really want to delete account '%s'?" % acc.cfg.idUri 391 if msgbox.askquestion('Delete account?', msg, default=msgbox.NO) != u'yes': 392 return 393 iid = str(acc.randId) 394 self.accList.remove(acc) 395 acc.setRegistration(False) 396 acc.deleting = True 397 del acc 398 self.tv.delete( (iid,) ) 399 elif label=='Add buddy...': 400 cfg = pj.BuddyConfig() 401 dlg = buddy.SettingDialog(self.master, cfg) 402 if dlg.doModal(): 403 self._createBuddy(acc, cfg) 404 else: 405 assert not ("Unknown menu " + label) 406 407 def _onBuddyContextMenu(self, label): 408 bud = self._getSelectedBuddy() 409 if not bud: 410 return 411 acc = bud.account 412 413 if label=='Audio call': 414 chat = acc.findChat(bud.cfg.uri) 415 if not chat: chat = acc.newChat(bud.cfg.uri) 416 chat.showWindow() 417 chat.startCall() 418 elif label=='Send instant message': 419 chat = acc.findChat(bud.cfg.uri) 420 if not chat: chat = acc.newChat(bud.cfg.uri) 421 chat.showWindow(True) 422 elif label=='Subscribe': 423 bud.subscribePresence(True) 424 elif label=='Unsubscribe': 425 bud.subscribePresence(False) 426 elif label=='Settings...': 427 subs = bud.cfg.subscribe 428 uri = bud.cfg.uri 429 dlg = buddy.SettingDialog(self.master, bud.cfg) 430 if dlg.doModal(): 431 self.updateBuddy(bud) 432 # URI updated? 433 if uri != bud.cfg.uri: 434 cfg = bud.cfg 435 # del old 436 iid = 'buddy' + str(bud.randId) 437 acc.buddyList.remove(bud) 438 del bud 439 self.tv.delete( (iid,) ) 440 # add new 441 self._createBuddy(acc, cfg) 442 # presence subscribe setting updated 443 elif subs != bud.cfg.subscribe: 444 bud.subscribePresence(bud.cfg.subscribe) 445 elif label=='Delete...': 446 msg = "Do you really want to delete buddy '%s'?" % bud.cfg.uri 447 if msgbox.askquestion('Delete buddy?', msg, default=msgbox.NO) != u'yes': 448 return 449 iid = 'buddy' + str(bud.randId) 450 acc.buddyList.remove(bud) 451 del bud 452 self.tv.delete( (iid,) ) 453 else: 454 assert not ("Unknown menu " + label) 455 456 def _onTimer(self): 457 if not self.quitting: 458 self.ep.libHandleEvents(10) 459 if not self.quitting: 460 self.master.after(50, self._onTimer) 461 462 def _onClose(self): 463 self.saveConfig() 464 self.quitting = True 465 self.ep.libDestroy() 466 self.ep = None 467 self.update() 468 self.quit() 469 470 def _onMenuAddAccount(self): 471 cfg = pj.AccountConfig() 472 dlg = accountsetting.Dialog(self.master, cfg) 473 if dlg.doModal(): 474 self._createAcc(cfg) 475 476 def _onMenuShowHideLogWindow(self): 477 if self.showLogWindow.get(): 478 self.logWindow.deiconify() 479 else: 480 self.logWindow.withdraw() 481 482 def _onMenuSettings(self): 483 dlg = settings.Dialog(self, self.appConfig) 484 if dlg.doModal(): 485 msgbox.showinfo(self.master.title(), 'You need to restart for new settings to take effect') 486 487 def _onMenuSaveSettings(self): 488 self.saveConfig() 489 490 def _onMenuQuit(self): 491 self._onClose() 492 493 def _onMenuAbout(self): 494 msgbox.showinfo(self.master.title(), 'About') 495 494 496 495 497 class ExceptionCatcher: 496 """Custom Tk exception catcher, mainly to display more information 497 498 """ 499 500 self.func = func 501 502 503 504 505 506 507 508 except pj.Error,error:509 print 'Exception:' 510 print ' ', error.info()511 print 'Traceback:' 512 print traceback.print_stack()513 514 except Exception,error:515 print 'Exception:' 516 print ' ', str(error)517 print 'Traceback:' 518 print traceback.print_stack()519 498 """Custom Tk exception catcher, mainly to display more information 499 from pj.Error exception 500 """ 501 def __init__(self, func, subst, widget): 502 self.func = func 503 self.subst = subst 504 self.widget = widget 505 def __call__(self, *args): 506 try: 507 if self.subst: 508 args = apply(self.subst, args) 509 return apply(self.func, args) 510 except pj.Error as error: 511 write("Exception:\r\n") 512 write(" ," + error.info() + "\r\n") 513 write("Traceback:\r\n") 514 write(traceback.print_stack()) 515 log.writeLog2(1, 'Exception: ' + error.info() + '\n') 516 except Exception as error: 517 write("Exception:\r\n") 518 write(" ," + str(error) + "\r\n") 519 write("Traceback:\r\n") 520 write(traceback.print_stack()) 521 log.writeLog2(1, 'Exception: ' + str(error) + '\n') 520 522 521 523 def main(): 522 523 524 525 526 524 #tk.CallWrapper = ExceptionCatcher 525 app = Application() 526 app.start() 527 app.mainloop() 528 527 529 if __name__ == '__main__': 528 530 main()
Note: See TracChangeset
for help on using the changeset viewer.