summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pygui/buddy.py
blob: 33537fc030c085b5ef22adf592269786d8ec58c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# $Id$
#
# pjsua Python GUI Demo
#
# Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
#
import sys
if sys.version_info[0] >= 3: # Python 3
	import tkinter as tk
	from tkinter import ttk
	from tkinter import messagebox as msgbox
else:
	import Tkinter as tk
	import tkMessageBox as msgbox
	import ttk

import random
import pjsua2 as pj
import endpoint
import application

# Buddy class
class Buddy(pj.Buddy):
	"""
	High level Python Buddy object, derived from pjsua2's Buddy object.
	"""
	def __init__(self, app):
		pj.Buddy.__init__(self)
		self.app = app
		self.randId = random.randint(1, 9999)
		self.cfg = None
		self.account = None

	def statusText(self):
		bi = self.getInfo()
		status = ''
		if bi.subState == pj.PJSIP_EVSUB_STATE_ACTIVE:
			if bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_ONLINE:
				status = bi.presStatus.statusText
				if not status:
					status = 'Online'
			elif bi.presStatus.status == pj.PJSUA_BUDDY_STATUS_OFFLINE:
				status = 'Offline'
			else:
				status = 'Unknown'
		return status
	
	def onBuddyState(self):
		self.app.updateBuddy(self)

class SettingDialog(tk.Toplevel):
	"""
	This implements buddy settings dialog to manipulate buddy settings.
	"""
	def __init__(self, parent, cfg):
		tk.Toplevel.__init__(self, parent)
		self.transient(parent)
		self.parent = parent
		self.geometry("+100+100")
		self.title('Buddy settings')
		
		self.frm = ttk.Frame(self)
		self.frm.pack(expand='yes', fill='both')
		
		self.isOk = False
		self.cfg = cfg
		
		self.createWidgets()
	
	def doModal(self):
		if self.parent:
			self.parent.wait_window(self)
		else:
			self.wait_window(self)
		return self.isOk
		
	def createWidgets(self):
		# The notebook
		self.frm.rowconfigure(0, weight=1)
		self.frm.rowconfigure(1, weight=0)
		self.frm.columnconfigure(0, weight=1)
		self.frm.columnconfigure(1, weight=1)
		self.wTab = ttk.Notebook(self.frm)
		self.wTab.grid(column=0, row=0, columnspan=2, padx=5, pady=5, sticky=tk.N+tk.S+tk.W+tk.E)
		
		# Main buttons
		btnOk = ttk.Button(self.frm, text='Ok', command=self.onOk)
		btnOk.grid(column=0, row=1, sticky=tk.E, padx=20, pady=10)
		btnCancel = ttk.Button(self.frm, text='Cancel', command=self.onCancel)
		btnCancel.grid(column=1, row=1, sticky=tk.W, padx=20, pady=10)
		
		# Tabs
		self.createBasicTab()
		
	def createBasicTab(self):
		# Prepare the variables to set/receive values from GUI
		self.cfgUri = tk.StringVar()
		self.cfgUri.set( self.cfg.uri )
		self.cfgSubscribe = tk.IntVar()
		self.cfgSubscribe.set(self.cfg.subscribe)
		
		# Build the tab page
		frm = ttk.Frame(self.frm)
		frm.columnconfigure(0, weight=1)
		frm.columnconfigure(1, weight=2)
		row = 0
		ttk.Label(frm, text='URI:').grid(row=row, column=0, sticky=tk.E, pady=2)
		ttk.Entry(frm, textvariable=self.cfgUri, width=40).grid(row=row, column=1, sticky=tk.W+tk.E, padx=6)
		row += 1
		ttk.Checkbutton(frm, text='Subscribe presence', variable=self.cfgSubscribe).grid(row=row, column=1, sticky=tk.W, padx=6, pady=2)

		self.wTab.add(frm, text='Basic Settings')
		
	
	def onOk(self):
		# Check basic settings
		errors = "";
		if self.cfgUri.get():
			if not endpoint.validateSipUri(self.cfgUri.get()):
				errors += "Invalid Buddy URI: '%s'\n" % (self.cfgUri.get())
				
		if errors:
			msgbox.showerror("Error detected:", errors)
			return
		
		# Basic settings
		self.cfg.uri = self.cfgUri.get()
		self.cfg.subscribe = self.cfgSubscribe.get()
		
		self.isOk = True
		self.destroy()
		
	def onCancel(self):
		self.destroy()


if __name__ == '__main__':
	application.main()