21
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 Primitivus: a SAT frontend |
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 import urwid |
|
23 import custom_widgets |
|
24 from tools.jid import JID |
|
25 import os, os.path |
|
26 from xml.dom import minidom |
|
27 from logging import debug, info, error |
|
28 |
|
29 class PathEdit(custom_widgets.AdvancedEdit): |
|
30 |
|
31 def keypress(self, size, key): |
|
32 if key == 'ctrl w': |
|
33 if self.edit_pos<2: |
|
34 return |
|
35 before = self.edit_text[:self.edit_pos] |
|
36 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1 |
|
37 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:]) |
|
38 self.set_edit_pos(pos) |
|
39 return |
|
40 else: |
|
41 return super(PathEdit, self).keypress(size, key) |
|
42 |
|
43 class FileDialog(urwid.WidgetWrap): |
|
44 |
|
45 def __init__(self,title=_("Please select a file")): |
|
46 self.__home_path = os.path.expanduser('~') |
|
47 self.path_wid = PathEdit(_('Path: '), os.getcwdu()) |
|
48 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) |
|
49 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')]) |
|
50 bookm_list = urwid.SimpleListWalker([]) |
|
51 self.bookmarks = list(self.getBookmarks()) |
|
52 self.bookmarks.sort() |
|
53 for bookmark in self.bookmarks: |
|
54 if bookmark.startswith(self.__home_path): |
|
55 bookmark="~"+bookmark[len(self.__home_path):] |
|
56 book_wid = custom_widgets.ClickableText(bookmark) |
|
57 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) |
|
58 bookm_list.append(book_wid) |
|
59 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title')) |
|
60 self.files_list = urwid.SimpleListWalker([urwid.Text('toto.mkv')]) |
|
61 files_wid = urwid.ListBox(self.files_list) |
|
62 center_row = urwid.Columns([('weight',2,bookm_wid), |
|
63 ('weight',8,custom_widgets.VerticalSeparator(files_wid))]) |
|
64 main_frame = custom_widgets.FocusFrame(center_row, header) |
|
65 decorated = custom_widgets.LabelLine(main_frame, custom_widgets.SurroundedText(title)) |
|
66 urwid.WidgetWrap.__init__(self, decorated) |
|
67 |
|
68 def getBookmarks(self): |
|
69 gnome_bookm = os.path.expanduser("~/.gtk-bookmarks") |
|
70 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xm") |
|
71 bookmarks = set() |
|
72 try: |
|
73 with open(gnome_bookm) as gnome_fd: |
|
74 for bm in gnome_fd.readlines(): |
|
75 if bm.startswith("file:///"): |
|
76 bookmarks.add(bm[7:].replace('\n','')) |
|
77 except IOError: |
|
78 info(_('No Gnome bookmarks file found')) |
|
79 pass |
|
80 |
|
81 try: |
|
82 dom = minidom.parse(kde_bookm) |
|
83 for elem in getElementsByTagName('bookmark'): |
|
84 bm = elem.getAttribute("href") |
|
85 if bm.startswith("file:///"): |
|
86 bookmarks.add(bm[7:]) |
|
87 except IOError: |
|
88 info(_('No KDE bookmarks file found')) |
|
89 pass |
|
90 |
|
91 return bookmarks |
|
92 |
|
93 def onBookmarkSelected(self, button): |
|
94 self.path_wid.set_edit_text(os.path.expanduser(button.get_text())) |
|
95 |
|
96 def onPathChange(self, edit, text): |
|
97 if os.path.isdir(text): |
|
98 del self.files_list[:] |
|
99 files = os.listdir(text) |
|
100 files.sort() |
|
101 self.files_list.extend([custom_widgets.ClickableText(filename) for filename in files]) |