Ignore:
Timestamp:
Aug 2, 2017 9:45:09 AM (7 years ago)
Author:
riza
Message:

Close #2034: Add support to Python3 using PJSUA2 API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/pygui/buddy.py

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