Changeset 4665


Ignore:
Timestamp:
Nov 29, 2013 12:16:43 PM (10 years ago)
Author:
bennylp
Message:

Re #1519: implemented Settings dialog and reorganization in config variables in Pygui app

Location:
pjproject/branches/projects/pjsua2/pjsip-apps/src/pygui
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/pjsua2/pjsip-apps/src/pygui/application.py

    r4664 r4665  
    3535import buddy 
    3636import endpoint 
     37import settings 
    3738 
    3839import os 
    3940import traceback 
    4041 
    41 class SipTransportConfig: 
    42         def __init__(self): 
    43                 #pj.PersistentObject.__init__(self) 
    44                 self.type = pj.PJSIP_TRANSPORT_UNSPECIFIED; 
    45                 self.cfg = pj.TransportConfig() 
    46                  
    47         def readObject(self, node): 
    48                 child_node = node.readContainer("SipTransportConfig") 
    49                 self.type = child_node.readInt("type") 
    50                 self.cfg.readObject(child_node) 
    51          
    52         def writeObject(self, node): 
    53                 child_node = node.writeNewContainer("SipTransportConfig") 
    54                 child_node.writeInt("type", self.type) 
    55                 self.cfg.writeObject(child_node) 
    56           
    5742         
    5843class Application(ttk.Frame): 
     
    8873                 
    8974                # Default config 
    90                 self.epCfg = pj.EpConfig() 
    91                 self.epCfg.uaConfig.threadCnt = 0; 
    92                 self.epCfg.logConfig.writer = self.logger 
    93                 self.epCfg.logConfig.filename = "pygui.log" 
    94                 self.epCfg.logConfig.fileFlags = pj.PJ_O_APPEND 
    95                 self.epCfg.logConfig.level = 5 
    96                 self.epCfg.logConfig.consoleLevel = 5 
    97                  
    98                 self.transportCfgs = [] 
    99                 t = SipTransportConfig() 
    100                 t.type = pj.PJSIP_TRANSPORT_UDP 
    101                 t.cfg.port = 0 
    102                 self.transportCfgs.append(t) 
    103                 t = SipTransportConfig() 
    104                 t.type = pj.PJSIP_TRANSPORT_TCP 
    105                 t.cfg.port = 0 
    106                 self.transportCfgs.append(t) 
    107                  
    108          
     75                self.appConfig = settings.AppConfig() 
     76                self.appConfig.epConfig.uaConfig.threadCnt = 0; 
     77                self.appConfig.epConfig.logConfig.writer = self.logger 
     78                self.appConfig.epConfig.logConfig.filename = "pygui.log" 
     79                self.appConfig.epConfig.logConfig.fileFlags = pj.PJ_O_APPEND 
     80                self.appConfig.epConfig.logConfig.level = 5 
     81                self.appConfig.epConfig.logConfig.consoleLevel = 5 
     82                 
    10983        def saveConfig(self, filename='pygui.js'): 
    110                 json = pj.JsonDocument() 
    111                  
    112                 # Write endpoint config 
    113                 json.writeObject(self.epCfg) 
    114                  
    115                 # Write transport config 
    116                 node = json.writeNewArray("transports") 
    117                 for t in self.transportCfgs: 
    118                         t.writeObject(node); 
    119                  
    120                 # Write account configs 
    121                 node = json.writeNewArray("accounts") 
     84                # Save disabled accounts since they are not listed in self.accList 
     85                disabled_accs = [ac for ac in self.appConfig.accounts if not ac.enabled] 
     86                self.appConfig.accounts = [] 
     87                 
     88                # Get account configs from active accounts 
    12289                for acc in self.accList: 
    123                         acc_node = node.writeNewContainer("Account") 
    124                         acc_node.writeObject(acc.cfg); 
    125  
    126                         # Write buddy configs 
    127                         buddy_node = acc_node.writeNewArray("buddies") 
     90                        acfg = settings.AccConfig() 
     91                        acfg.enabled = True 
     92                        acfg.config = acc.cfg 
    12893                        for bud in acc.buddyList: 
    129                                 buddy_node.writeObject(bud.cfg) 
    130                                  
    131                 json.saveFile(filename) 
     94                                acfg.buddyConfigs.append(bud.cfg) 
     95                        self.appConfig.accounts.append(acfg) 
     96                 
     97                # Put back disabled accounts 
     98                self.appConfig.accounts.extend(disabled_accs) 
     99                # Save 
     100                self.appConfig.saveFile(filename) 
    132101         
    133102        def start(self, cfg_file='pygui.js'): 
    134103                # Load config 
    135                 acc_cfgs = [] 
    136104                if cfg_file and os.path.exists(cfg_file): 
    137                         json = pj.JsonDocument() 
    138                         json.loadFile(cfg_file) 
    139                          
    140                         # Load endpoint config 
    141                         json.readObject(self.epCfg) 
    142                          
    143                         # Load transport configs 
    144                         node = json.readArray("transports") 
    145                         if node.hasUnread(): 
    146                                 self.transportCfgs = [] 
    147                                 while node.hasUnread(): 
    148                                         t = SipTransportConfig() 
    149                                         t.readObject(node) 
    150                                         self.transportCfgs.append(t) 
    151                          
    152                         # Load account configs 
    153                         node = json.readArray("accounts") 
    154                         while node.hasUnread(): 
    155                                 acc_node = node.readContainer("Account") 
    156                                 acc_cfg = pj.AccountConfig() 
    157                                 acc_cfg.readObject(acc_node) 
    158                  
    159                                 # Load buddy configs 
    160                                 buddy_cfgs = [] 
    161                                 buddy_node = acc_node.readArray("buddies") 
    162                                 while buddy_node.hasUnread(): 
    163                                         buddy_cfg = pj.BuddyConfig() 
    164                                         buddy_cfg.readObject(buddy_node) 
    165                                         buddy_cfgs.append(buddy_cfg) 
    166  
    167                                 acc_cfgs.append((acc_cfg, buddy_cfgs)) 
    168                                  
     105                        self.appConfig.loadFile(cfg_file) 
     106 
     107                self.appConfig.epConfig.uaConfig.threadCnt = 0; 
     108                self.appConfig.epConfig.logConfig.writer = self.logger 
     109                self.appConfig.epConfig.logConfig.level = 5 
     110                self.appConfig.epConfig.logConfig.consoleLevel = 5 
    169111                                 
    170112                # Initialize library 
    171                 self.epCfg.uaConfig.userAgent = "pygui-" + self.ep.libVersion().full; 
    172                 self.ep.libInit(self.epCfg) 
     113                self.appConfig.epConfig.uaConfig.userAgent = "pygui-" + self.ep.libVersion().full; 
     114                self.ep.libInit(self.appConfig.epConfig) 
    173115                self.master.title('pjsua2 Demo version ' + self.ep.libVersion().full) 
    174116                 
    175117                # Create transports 
    176                 for t in self.transportCfgs: 
    177                         self.ep.transportCreate(t.type, t.cfg) 
     118                if self.appConfig.udp.enabled: 
     119                        self.ep.transportCreate(self.appConfig.udp.type, self.appConfig.udp.config) 
     120                if self.appConfig.tcp.enabled: 
     121                        self.ep.transportCreate(self.appConfig.tcp.type, self.appConfig.tcp.config) 
     122                if self.appConfig.tls.enabled: 
     123                        self.ep.transportCreate(self.appConfig.tls.type, self.appConfig.tls.config) 
    178124                         
    179125                # Add accounts 
    180                 for cfg, buddy_cfgs in acc_cfgs: 
    181                         self._createAcc(cfg) 
    182                         acc = self.accList[-1] 
    183                         for buddy_cfg in buddy_cfgs: 
    184                                 self._createBuddy(acc, buddy_cfg) 
     126                for cfg in self.appConfig.accounts: 
     127                        if cfg.enabled: 
     128                                self._createAcc(cfg.config) 
     129                                acc = self.accList[-1] 
     130                                for buddy_cfg in cfg.buddyConfigs: 
     131                                        self._createBuddy(acc, buddy_cfg) 
    185132                                 
    186133                # Start library 
     
    294241                self.buddyMenu = tk.Menu(top, tearoff=False) 
    295242                labels = ['Video call', 'Audio call', 'Send instant message', '-', 
     243                          'Subscribe', 'Unsubscribe', '-', 
    296244                          'Settings...', '-', 
    297245                          'Delete...'] 
     
    420368                elif label=='Send instant message': 
    421369                        pass 
     370                elif label=='Subscribe': 
     371                        bud.subscribePresence(True) 
     372                elif label=='Unsubscribe': 
     373                        bud.subscribePresence(False) 
    422374                elif label=='Settings...': 
    423375                        subs = bud.cfg.subscribe 
     
    477429         
    478430        def _onMenuSettings(self): 
    479                 msgbox.showinfo(self.master.title(), 'Settings') 
     431                dlg = settings.Dialog(self, self.appConfig) 
     432                if dlg.doModal(): 
     433                        msgbox.showinfo(self.master.title(), 'You need to restart for new settings to take effect') 
    480434         
    481435        def _onMenuSaveSettings(self): 
Note: See TracChangeset for help on using the changeset viewer.