src/extras/fileserver/fsgui.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # fsgui.py
       
     2 #
       
     3 # A launcher script for the simple file transfer server
       
     4 # for Series 60 Python environment.
       
     5 #     
       
     6 # Copyright (c) 2005 Nokia Corporation
       
     7 #
       
     8 # Licensed under the Apache License, Version 2.0 (the "License");
       
     9 # you may not use this file except in compliance with the License.
       
    10 # You may obtain a copy of the License at
       
    11 #
       
    12 #     http://www.apache.org/licenses/LICENSE-2.0
       
    13 #
       
    14 # Unless required by applicable law or agreed to in writing, software
       
    15 # distributed under the License is distributed on an "AS IS" BASIS,
       
    16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    17 # See the License for the specific language governing permissions and
       
    18 # limitations under the License.
       
    19 
       
    20 
       
    21 import appuifw
       
    22 import socket
       
    23 import os
       
    24 import e32
       
    25 
       
    26 CONFIG_DIR='c:/system/apps/python'
       
    27 
       
    28 def discover_address(configfile):
       
    29     import appuifw
       
    30     CONFIG_FILE=os.path.join(CONFIG_DIR,configfile)
       
    31     try:        
       
    32         f=open(CONFIG_FILE,'r')
       
    33         config=eval(f.read())
       
    34         f.close()
       
    35     except:
       
    36         config={}
       
    37 
       
    38     address=config.get('target','')
       
    39 
       
    40     if address:
       
    41         choice=appuifw.popup_menu([u'Default host',
       
    42                                    u'Other...'],u'Connect to:')
       
    43         if choice==1:
       
    44             address=None
       
    45         if choice==None:
       
    46             return None # popup menu was cancelled.    
       
    47     if not address:
       
    48         print "Discovering..."
       
    49         addr,services=socket.bt_discover()
       
    50         print "Discovered: %s, %s"%(addr,services)
       
    51         if len(services)>1:
       
    52             choices=services.keys()
       
    53             choices.sort()
       
    54             choice=appuifw.popup_menu([unicode(services[x])+": "+x
       
    55                                        for x in choices],u'Choose port:')
       
    56             port=services[choices[choice]]
       
    57         else:
       
    58             port=services[services.keys()[0]]
       
    59         address=(addr,port)
       
    60         config['target']=address
       
    61         # make sure the configuration file exists.
       
    62         if not os.path.isdir(CONFIG_DIR):
       
    63             os.makedirs(CONFIG_DIR)
       
    64         f=open(CONFIG_FILE,'wt')
       
    65         f.write(repr(config))
       
    66         f.close()
       
    67     return address
       
    68 
       
    69 def startserver():
       
    70     if os.path.isfile(u'e:\\system\\libs\\fileserver.py'):
       
    71         server_script=u'e:\\system\\libs\\fileserver.py'
       
    72     elif os.path.isfile(u'c:\\system\\libs\\fileserver.py'):
       
    73         server_script=u'c:\\system\\libs\\fileserver.py'
       
    74     else:
       
    75         appuifw.note(u'fileserver.py not found','error')
       
    76         return
       
    77     addr=discover_address('fileserver_conf.txt')
       
    78     if addr:
       
    79         e32.start_server(server_script)
       
    80     appuifw.note(u'File server started','info')
       
    81 
       
    82 startserver()