Mercurial > fife-parpg
comparison engine/extensions/filebrowser.py @ 260:a0068e00f076
* Really fixed proper support for non-ascii filenames for FileBrowser
* Adjusted selection cursor hotspot
author | cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 08 Jun 2009 21:04:43 +0000 |
parents | 0b1992a3dfe7 |
children | 0adf9423062e |
comparison
equal
deleted
inserted
replaced
259:d5e251efb310 | 260:a0068e00f076 |
---|---|
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 import pychan | 3 import pychan |
4 import pychan.widgets as widgets | 4 import pychan.widgets as widgets |
5 import sys | 5 import sys |
6 | |
7 def u2s(string): | |
8 return string.encode(sys.getfilesystemencoding()) | |
6 | 9 |
7 class FileBrowser(object): | 10 class FileBrowser(object): |
8 """ | 11 """ |
9 FileBrowser displays directory and file listings from the vfs. | 12 FileBrowser displays directory and file listings from the vfs. |
10 The fileSelected parameter is a callback invoked when a file selection has been made; its | 13 The fileSelected parameter is a callback invoked when a file selection has been made; its |
45 self._widget.show() | 48 self._widget.show() |
46 | 49 |
47 def _setDirectory(self): | 50 def _setDirectory(self): |
48 selection = self._widget.collectData('dirList') | 51 selection = self._widget.collectData('dirList') |
49 if not (selection < 0): | 52 if not (selection < 0): |
50 new_dir = self.dir_list[selection] | 53 new_dir = u2s(self.dir_list[selection]) |
51 lst = self.path.split('/') | 54 lst = self.path.split('/') |
52 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': | 55 if new_dir == '..' and lst[-1] != '..' and lst[-1] != '.': |
53 lst.pop() | 56 lst.pop() |
54 else: | 57 else: |
55 lst.append(new_dir) | 58 lst.append(new_dir) |
56 self.path = '/'.join(lst) | 59 self.path = '/'.join(lst) |
57 | 60 |
58 | |
59 def decodeList(list): | 61 def decodeList(list): |
60 fs_encoding = sys.getfilesystemencoding() | 62 fs_encoding = sys.getfilesystemencoding() |
61 if fs_encoding is None: fs_encoding = "ascii" | 63 if fs_encoding is None: fs_encoding = "ascii" |
62 | 64 |
63 newList = [] | 65 newList = [] |
64 for i in list: | 66 for i in list: |
65 try: | 67 try: newList.append(unicode(i, fs_encoding)) |
66 newList.append(unicode(i, fs_encoding)) | |
67 except: | 68 except: |
68 newList.append("WARNING: This entry could not be decoded!") | 69 newList.append(unicode(i, fs_encoding, 'replace')) |
69 print "WARNING: Coult not decode an item!" | 70 print "WARNING: Could not decode item:", i |
70 return newList | 71 return newList |
72 | |
73 | |
71 | 74 |
72 self.dir_list = [] | 75 self.dir_list = [] |
73 self.file_list = [] | 76 self.file_list = [] |
74 | 77 |
75 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(str(self.path))) | 78 dir_list = ('..',) + filter(lambda d: not d.startswith('.'), self.engine.getVFS().listDirectories(self.path)) |
76 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(str(self.path))) | 79 file_list = filter(lambda f: f.split('.')[-1] in self.extensions, self.engine.getVFS().listFiles(self.path)) |
77 | 80 |
78 self.dir_list = decodeList(dir_list) | 81 self.dir_list = decodeList(dir_list) |
79 self.file_list = decodeList(file_list) | 82 self.file_list = decodeList(file_list) |
80 self._widget.distributeInitialData({ | 83 self._widget.distributeInitialData({ |
81 'dirList' : self.dir_list, | 84 'dirList' : self.dir_list, |
86 self._widget.hide() | 89 self._widget.hide() |
87 selection = self._widget.collectData('fileList') | 90 selection = self._widget.collectData('fileList') |
88 | 91 |
89 if self.savefile: | 92 if self.savefile: |
90 if self._widget.collectData('saveField'): | 93 if self._widget.collectData('saveField'): |
91 self.fileSelected(self.path,self._widget.collectData('saveField')) | 94 self.fileSelected(self.path, u2s(self._widget.collectData('saveField'))) |
92 return | 95 return |
93 | 96 |
94 if selection >= 0: | 97 if selection >= 0: |
95 self.fileSelected(self.path,self.file_list[selection]) | 98 self.fileSelected(self.path, u2s(self.file_list[selection])) |
96 return | 99 return |
97 | 100 |
98 if self.selectdir: | 101 if self.selectdir: |
99 self.fileSelected(self.path) | 102 self.fileSelected(self.path) |
100 return | 103 return |