Debian Codegraph @

Xpjproject/pjsip-apps/src/pygui/buddy.py

raw symbols SCIP
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
30 import random
31 import pjsua2 as pj
32 import endpoint
33 import application
34
35 # Buddy class
36 class Buddy(pj.Buddy):
37 """
38 High level Python Buddy object, derived from pjsua2's Buddy object.
39 """
40 def __init__(self, app):
41 pj.Buddy.__init__(self)
42 self.app = app
43 self.randId = random.randint(1, 9999)
44 self.cfg = None
45 self.account = None
46
47 def statusText(self):
48 bi = self.getInfo()
49 status = ''
50 if bi.subState == pj.PJSIP_EVSUB_STATE_ACTIVE:
51 if bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_ONLINE:
52 status = bi.presStatus.statusText
53 if not status:
54 status = 'Online'
55 elif bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_OFFLINE:
56 status = 'Offline'
57 else:
58 status = 'Unknown'
59 return status
60
61 def onBuddyState(self):
62 self.app.updateBuddy(self)
63
64 class SettingDialog(tk.Toplevel):
65 """
66 This implements buddy settings dialog to manipulate buddy settings.
67 """
68 def __init__(self, parent, cfg):
69 tk.Toplevel.__init__(self, parent)
70 self.transient(parent)
71 self.parent = parent
72 self.geometry("+100+100")
73 self.title('Buddy settings')
74
75 self.frm = ttk.Frame(self)
76 self.frm.pack(expand='yes', fill='both')
77
78 self.isOk = False
79 self.cfg = cfg
80
81 self.createWidgets()
82
83 def doModal(self):
84 if self.parent:
85 self.parent.wait_window(self)
86 else:
87 self.wait_window(self)
88 return self.isOk
89
90 def createWidgets(self):
91 # The notebook
92 self.frm.rowconfigure(0, weight=1)
93 self.frm.rowconfigure(1, weight=0)
94 self.frm.columnconfigure(0, weight=1)
95 self.frm.columnconfigure(1, weight=1)
96 self.wTab = ttk.Notebook(self.frm)
97 self.wTab.grid(column=0, row=0, columnspan=2, padx=5, pady=5, sticky=tk.N+tk.S+tk.W+tk.E)
98
99 # Main buttons
100 btnOk = ttk.Button(self.frm, text='Ok', command=self.onOk)
101 btnOk.grid(column=0, row=1, sticky=tk.E, padx=20, pady=10)
102 btnCancel = ttk.Button(self.frm, text='Cancel', command=self.onCancel)
103 btnCancel.grid(column=1, row=1, sticky=tk.W, padx=20, pady=10)
104
105 # Tabs
106 self.createBasicTab()
107
108 def createBasicTab(self):
109 # Prepare the variables to set/receive values from GUI
110 self.cfgUri = tk.StringVar()
111 self.cfgUri.set( self.cfg.uri )
112 self.cfgSubscribe = tk.BooleanVar(value = self.cfg.subscribe)
113
114 # Build the tab page
115 frm = ttk.Frame(self.frm)
116 frm.columnconfigure(0, weight=1)
117 frm.columnconfigure(1, weight=2)
118 row = 0
119 ttk.Label(frm, text='URI:').grid(row=row, column=0, sticky=tk.E, pady=2)
120 ttk.Entry(frm, textvariable=self.cfgUri, width=40).grid(row=row, column=1, sticky=tk.W+tk.E, padx=6)
121 row += 1
122 ttk.Checkbutton(frm, text='Subscribe presence', variable=self.cfgSubscribe).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)
123
124 self.wTab.add(frm, text='Basic Settings')
125
126
127 def onOk(self):
128 # Check basic settings
129 errors = "";
130 if self.cfgUri.get():
131 if not endpoint.validateSipUri(self.cfgUri.get()):
132 errors += "Invalid Buddy URI: '%s'\n" % (self.cfgUri.get())
133
134 if errors:
135 msgbox.showerror("Error detected:", errors)
136 return
137
138 # Basic settings
139 self.cfg.uri = self.cfgUri.get()
140 self.cfg.subscribe = self.cfgSubscribe.get()
141
142 self.isOk = True
143 self.destroy()
144
145 def onCancel(self):
146 self.destroy()
147
148
149 if __name__ == '__main__':
150 application.main()