# # filebrowser.py # # A very simple file browser script to demonstrate the power of Python # on Series 60. # # Copyright (c) 2005 Nokia Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # FTP upload support Add-on By KiaZ - kiazsruf at yahoo dot it . # import os import appuifw import e32 import dir_iter import sys from socket import * class Filebrowser: def __init__(self): self.script_lock = e32.Ao_lock() self.dir_stack = [] self.current_dir = dir_iter.Directory_iter(e32.drive_list()) def ftp_upload(self, fullfilepath, remotefilename): cnffile = open('E:\\ftpconf.txt','r') line = cnffile.readline() cnffile.close() tappo = line.split(':') serverHost = tappo[0] user = tappo[1] pwd = tappo[2] serverPort = 21 filetoput = fullfilepath remote = remotefilename sSock = socket(AF_INET, SOCK_STREAM) #Connect to server sSock.connect((serverHost, serverPort)) #reading banner data = sSock.recv(1024) print data sSock.send('USER '+user+'\n') data = sSock.recv(1024) print '>>>',data sSock.send('PASS '+pwd+'\n') data = sSock.recv(1024) print '>>>',data #sSock.send('HELP\n') #data = sSock.recv(2048) #print '#>>>',data sSock.send('PASV\n') passive = sSock.recv(1024) print '#>>>',passive print '\n\n----------------------\n\n' stringone = passive.split(',') print '\n' firstnum = int(stringone[4]) secondnumt = (stringone[5]).split(')') secondnum = int(secondnumt[0]) dataport = (firstnum*256) + secondnum print 'DAta port ',dataport print '\n' datasock = socket(AF_INET, SOCK_STREAM) datasock.connect((serverHost, dataport)) #sSock.send('LIST\n') #list = datasock.recv(2048) #print "---\n" #print list print "--\n" #sSock.send('STOR prova.txt\n') #sSock.send('STOR '+filetoput+'\n') sSock.send('STOR '+remote+'\n') resp = sSock.recv(1024) print '#>>>',resp print '\n' filetoput = open(filetoput,'r') datasock.send(filetoput.read()) datasock.close() sSock.close() print ">>> File uploadato !" appuifw.note(u"File uploadato ! ") def inssrv(self): srv = appuifw.query(u"Insert server here","text") cnfstring = srv+":" appuifw.note(unicode("Server is "+srv)) user = appuifw.query(u"Insert username here","text") cnfstring = cnfstring + user + ":" appuifw.note(unicode("Username is "+user)) pwd = appuifw.query(u"Insert password here","text") cnfstring = cnfstring + pwd; appuifw.note(unicode("CNF : "+cnfstring)) cnffile = open('E:\\ftpconf.txt','w') cnffile.write(cnfstring) cnfile.close() def viewcnf(self): cnffile = open('E:\\ftpconf.txt','r') line = cnffile.readline() cnffile.close() appuifw.note(unicode("Current ftp configuration "+line)) def run(self): from key_codes import EKeyLeftArrow entries = self.current_dir.list_repr() if not self.current_dir.at_root: entries.insert(0, (u"..", u"")) self.lb = appuifw.Listbox(entries, self.lbox_observe) self.lb.bind(EKeyLeftArrow, lambda: self.lbox_observe(0)) appuifw.app.menu = [(u"insert server", self.inssrv),(u"wiew current config", self.viewcnf)] old_title = appuifw.app.title self.refresh() self.script_lock.wait() appuifw.app.title = old_title appuifw.app.body = None self.lb = None def refresh(self): appuifw.app.title = u"File browser" appuifw.app.menu = [(u"insert server", self.inssrv),(u"wiew current config", self.viewcnf)] appuifw.app.exit_key_handler = self.exit_key_handler appuifw.app.body = self.lb def do_exit(self): self.exit_key_handler() def exit_key_handler(self): appuifw.app.exit_key_handler = None self.script_lock.signal() def lbox_observe(self, ind = None): if not ind == None: index = ind else: index = self.lb.current() focused_item = 0 if self.current_dir.at_root: self.dir_stack.append(index) self.current_dir.add(index) elif index == 0: # ".." selected focused_item = self.dir_stack.pop() self.current_dir.pop() elif os.path.isdir(self.current_dir.entry(index-1)): self.dir_stack.append(index) self.current_dir.add(index-1) else: item = self.current_dir.entry(index-1) if os.path.splitext(item)[1] == '.py': i = appuifw.popup_menu([u"execfile()", u"Delete", u"Upload via ftp"]) else: i = appuifw.popup_menu([u"Open", u"Delete", u"Upload via ftp"]) if i == 0: if os.path.splitext(item)[1].lower() == u'.py': execfile(item, globals()) self.refresh() #appuifw.Content_handler().open_standalone(item) else: try: appuifw.Content_handler().open(item) except: import sys type, value = sys.exc_info() [:2] appuifw.note(unicode(str(type)+'\n'+str(value)), "info") return elif i == 1: os.remove(item) focused_item = index - 1 elif i == 2: #appuifw.note(unicode("Should call upload function for "+item)) patharray = item.split('\\') x = len(patharray) lastitem = patharray[x - 1] appuifw.note(unicode("remote file : "+lastitem)) self.ftp_upload(item,lastitem) entries = self.current_dir.list_repr() if not self.current_dir.at_root: entries.insert(0, (u"..", u"")) self.lb.set_list(entries, focused_item) if __name__ == '__main__': Filebrowser().run()