buddy.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #
  2. # pjsua Python GUI Demo
  3. #
  4. # Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. #
  20. import sys
  21. if sys.version_info[0] >= 3: # Python 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. import random
  30. import pjsua2 as pj
  31. import endpoint
  32. import application
  33. # Buddy class
  34. class Buddy(pj.Buddy):
  35. """
  36. High level Python Buddy object, derived from pjsua2's Buddy object.
  37. """
  38. def __init__(self, app):
  39. pj.Buddy.__init__(self)
  40. self.app = app
  41. self.randId = random.randint(1, 9999)
  42. self.cfg = None
  43. self.account = None
  44. def statusText(self):
  45. bi = self.getInfo()
  46. status = ''
  47. if bi.subState == pj.PJSIP_EVSUB_STATE_ACTIVE:
  48. if bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_ONLINE:
  49. status = bi.presStatus.statusText
  50. if not status:
  51. status = 'Online'
  52. elif bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_OFFLINE:
  53. status = 'Offline'
  54. else:
  55. status = 'Unknown'
  56. return status
  57. def onBuddyState(self):
  58. self.app.updateBuddy(self)
  59. class SettingDialog(tk.Toplevel):
  60. """
  61. This implements buddy settings dialog to manipulate buddy settings.
  62. """
  63. def __init__(self, parent, cfg):
  64. tk.Toplevel.__init__(self, parent)
  65. self.transient(parent)
  66. self.parent = parent
  67. self.geometry("+100+100")
  68. self.title('Buddy settings')
  69. self.frm = ttk.Frame(self)
  70. self.frm.pack(expand='yes', fill='both')
  71. self.isOk = False
  72. self.cfg = cfg
  73. self.createWidgets()
  74. def doModal(self):
  75. if self.parent:
  76. self.parent.wait_window(self)
  77. else:
  78. self.wait_window(self)
  79. return self.isOk
  80. def createWidgets(self):
  81. # The notebook
  82. self.frm.rowconfigure(0, weight=1)
  83. self.frm.rowconfigure(1, weight=0)
  84. self.frm.columnconfigure(0, weight=1)
  85. self.frm.columnconfigure(1, weight=1)
  86. self.wTab = ttk.Notebook(self.frm)
  87. self.wTab.grid(column=0, row=0, columnspan=2, padx=5, pady=5, sticky=tk.N+tk.S+tk.W+tk.E)
  88. # Main buttons
  89. btnOk = ttk.Button(self.frm, text='Ok', command=self.onOk)
  90. btnOk.grid(column=0, row=1, sticky=tk.E, padx=20, pady=10)
  91. btnCancel = ttk.Button(self.frm, text='Cancel', command=self.onCancel)
  92. btnCancel.grid(column=1, row=1, sticky=tk.W, padx=20, pady=10)
  93. # Tabs
  94. self.createBasicTab()
  95. def createBasicTab(self):
  96. # Prepare the variables to set/receive values from GUI
  97. self.cfgUri = tk.StringVar()
  98. self.cfgUri.set( self.cfg.uri )
  99. self.cfgSubscribe = tk.BooleanVar(value = self.cfg.subscribe)
  100. # Build the tab page
  101. frm = ttk.Frame(self.frm)
  102. frm.columnconfigure(0, weight=1)
  103. frm.columnconfigure(1, weight=2)
  104. row = 0
  105. ttk.Label(frm, text='URI:').grid(row=row, column=0, sticky=tk.E, pady=2)
  106. ttk.Entry(frm, textvariable=self.cfgUri, width=40).grid(row=row, column=1, sticky=tk.W+tk.E, padx=6)
  107. row += 1
  108. ttk.Checkbutton(frm, text='Subscribe presence', variable=self.cfgSubscribe).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
  109. self.wTab.add(frm, text='Basic Settings')
  110. def onOk(self):
  111. # Check basic settings
  112. errors = "";
  113. if self.cfgUri.get():
  114. if not endpoint.validateSipUri(self.cfgUri.get()):
  115. errors += "Invalid Buddy URI: '%s'\n" % (self.cfgUri.get())
  116. if errors:
  117. msgbox.showerror("Error detected:", errors)
  118. return
  119. # Basic settings
  120. self.cfg.uri = self.cfgUri.get()
  121. self.cfg.subscribe = self.cfgSubscribe.get()
  122. self.isOk = True
  123. self.destroy()
  124. def onCancel(self):
  125. self.destroy()
  126. if __name__ == '__main__':
  127. application.main()