src/extras/filebrowser.py
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 # Copyright (c) 2005-2009 Nokia Corporation
       
     2 #
       
     3 # Licensed under the Apache License, Version 2.0 (the "License");
       
     4 # you may not use this file except in compliance with the License.
       
     5 # You may obtain a copy of the License at
       
     6 #
       
     7 #     http://www.apache.org/licenses/LICENSE-2.0
       
     8 #
       
     9 # Unless required by applicable law or agreed to in writing, software
       
    10 # distributed under the License is distributed on an "AS IS" BASIS,
       
    11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    12 # See the License for the specific language governing permissions and
       
    13 # limitations under the License.
       
    14 
       
    15 #
       
    16 # filebrowser.py
       
    17 #
       
    18 # A very simple file browser script to demonstrate the power of Python
       
    19 # on Series 60.
       
    20 #
       
    21 
       
    22 import os
       
    23 import appuifw
       
    24 import e32
       
    25 import dir_iter
       
    26 
       
    27 
       
    28 class Filebrowser:
       
    29 
       
    30     def __init__(self):
       
    31         self.script_lock = e32.Ao_lock()
       
    32         self.dir_stack = []
       
    33         self.current_dir = dir_iter.Directory_iter(e32.drive_list())
       
    34 
       
    35     def run(self):
       
    36         from key_codes import EKeyLeftArrow
       
    37         entries = self.current_dir.list_repr()
       
    38         if not self.current_dir.at_root:
       
    39             entries.insert(0, (u"..", u""))
       
    40         self.lb = appuifw.Listbox(entries, self.lbox_observe)
       
    41         self.lb.bind(EKeyLeftArrow, lambda: self.lbox_observe(0))
       
    42         old_title = appuifw.app.title
       
    43         self.refresh()
       
    44         self.script_lock.wait()
       
    45         appuifw.app.title = old_title
       
    46         appuifw.app.body = None
       
    47         self.lb = None
       
    48 
       
    49     def refresh(self):
       
    50         appuifw.app.title = u"File browser"
       
    51         appuifw.app.menu = []
       
    52         appuifw.app.screen = 'normal'
       
    53         appuifw.app.orientation = 'automatic'
       
    54         appuifw.app.exit_key_handler = self.exit_key_handler
       
    55         appuifw.app.body = self.lb
       
    56 
       
    57     def exit_key_handler(self):
       
    58         appuifw.app.exit_key_handler = None
       
    59         self.script_lock.signal()
       
    60 
       
    61     def lbox_observe(self, ind = None):
       
    62         if not ind == None:
       
    63             index = ind
       
    64         else:
       
    65             index = self.lb.current()
       
    66         focused_item = 0
       
    67 
       
    68         if self.current_dir.at_root:
       
    69             self.dir_stack.append(index)
       
    70             self.current_dir.add(index)
       
    71         elif index == 0:                              # ".." selected
       
    72             focused_item = self.dir_stack.pop()
       
    73             self.current_dir.pop()
       
    74         elif os.path.isdir(self.current_dir.entry(index-1)):
       
    75             self.dir_stack.append(index)
       
    76             self.current_dir.add(index-1)
       
    77         else:
       
    78             item = self.current_dir.entry(index-1)
       
    79             if os.path.splitext(item)[1].lower() == u'.py':
       
    80                 i = appuifw.popup_menu([u"execfile()", u"Delete"])
       
    81             else:
       
    82                 i = appuifw.popup_menu([u"Open", u"Delete"])
       
    83             if i == 0:
       
    84                 if os.path.splitext(item)[1].lower() == u'.py':
       
    85                     execfile(item, globals())
       
    86                     self.refresh()
       
    87                     #appuifw.Content_handler().open_standalone(item)
       
    88                 else:
       
    89                     try:
       
    90                         appuifw.Content_handler().open(item)
       
    91                     except:
       
    92                         import sys
       
    93                         type, value = sys.exc_info() [:2]
       
    94                         appuifw.note(unicode(str(type) + '\n' + str(value)), \
       
    95                                      "info")
       
    96                 return
       
    97             elif i == 1:
       
    98                 os.remove(item)
       
    99                 focused_item = index - 1
       
   100 
       
   101         entries = self.current_dir.list_repr()
       
   102         if not self.current_dir.at_root:
       
   103             entries.insert(0, (u"..", u""))
       
   104         self.lb.set_list(entries, focused_item)
       
   105 
       
   106 if __name__ == '__main__':
       
   107     Filebrowser().run()