1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """ a filebrowser implementation for pychan """
25
26 import pychan
27 import pychan.widgets as widgets
28 import sys
29
31 return string.encode(sys.getfilesystemencoding())
32
34 """ The B{FileBrowser} displays directory and file listings from the vfs.
35
36 B{The fileSelected} parameter is a callback invoked when a file selection has been made; its
37 signature must be fileSelected(path,filename).
38
39 B{If selectdir} is set, fileSelected's filename parameter should be optional.
40
41 B{The savefile} option provides a box for supplying a new filename that doesn't exist yet.
42
43 B{The selectdir} option allows directories to be selected as well as files.
44 """
45 - def __init__(self, engine, fileSelected, savefile=False, selectdir=False, extensions=('xml',), guixmlpath="gui/filebrowser.xml"):
46 """
47 @type engine: object
48 @param engine: initiated instance of FIFE
49 @type fileSelected: function
50 @param fileSelected: callback invoked on file selection
51 @type savefile: bool
52 @param savefile: flag to provide a gui for an usergiven filename
53 @type selectdir: bool
54 @param selectdir: flag to fire fileSelected without filename
55 @type extensions: tuple
56 @param extensions: list of extensions the filebrowser should show (defaults to xml)
57 @type guixmlpath: string
58 @param guixmlpath: path to the xml guifile defaults to (gui/filebrowser.xml)
59 """
60 self.engine = engine
61 self.fileSelected = fileSelected
62
63 self._widget = None
64 self.savefile = savefile
65 self.selectdir = selectdir
66
67 self.guixmlpath = guixmlpath
68
69 self.extensions = extensions
70 self.path = './..'
71 self.dir_list = []
72 self.file_list = []
73
93
95 """ sets the current directory according to path """
96 path_copy = self.path
97 self.path = path
98 if not self._widget: return
99
100 def decodeList(list):
101 fs_encoding = sys.getfilesystemencoding()
102 if fs_encoding is None: fs_encoding = "ascii"
103
104 newList = []
105 for i in list:
106 try: newList.append(unicode(i, fs_encoding))
107 except:
108 newList.append(unicode(i, fs_encoding, 'replace'))
109 print "WARNING: Could not decode item:", i
110 return newList
111
112 dir_list_copy = list(self.dir_list)
113 file_list_copy = list(self.file_list)
114
115 self.dir_list = []
116 self.file_list = []
117
118 try:
119 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path))
120 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path))
121 self.dir_list = decodeList(dir_list)
122 self.file_list = decodeList(file_list)
123 except:
124 self.path = path_copy
125 self.dir_list = list(dir_list_copy)
126 self.file_list = list(file_list_copy)
127 print "WARNING: Tried to browse to directory that is not accessible!"
128
129 self._widget.distributeInitialData({
130 'dirList' : self.dir_list,
131 'fileList' : self.file_list
132 })
133
134 self._widget.adaptLayout()
135
137 """ callback for directory ListBox """
138 selection = self._widget.collectData('dirList')
139 if selection >= 0 and selection < len(self.dir_list):
140 new_dir = u2s(self.dir_list[selection])
141 lst = self.path.split('/')
142 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.':
143 lst.pop()
144 else:
145 lst.append(new_dir)
146
147 path = '/'.join(lst)
148 self.setDirectory(path)
149
151 """ callback for selectButton, hides filebrowser """
152 self._widget.hide()
153 selection = self._widget.collectData('fileList')
154
155 if self.savefile:
156 if self._widget.collectData('saveField'):
157 self.fileSelected(self.path, u2s(self._widget.collectData('saveField')))
158 return
159
160 if selection >= 0 and selection < len(self.file_list):
161 self.fileSelected(self.path, u2s(self.file_list[selection]))
162 return
163
164 if self.selectdir:
165 self.fileSelected(self.path)
166 return
167
168 print 'FileBrowser: error, no selection.'
169