annotate frontends/primitivus/files_management.py @ 181:a566f654929e

README update: - Sortilège shorcuts have been replaced by Primitivus shortcuts - Credits updated
author Goffi <goffi@goffi.org>
date Mon, 16 Aug 2010 21:59:52 +0800
parents d6c0c5dca9b9
children 879beacb8e16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import urwid
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import custom_widgets
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from tools.jid import JID
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import os, os.path
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from xml.dom import minidom
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from logging import debug, info, error
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
28 from time import time
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class PathEdit(custom_widgets.AdvancedEdit):
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
31 """AdvancedEdit with manage file paths"""
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def keypress(self, size, key):
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
34 if key == '~' and self.edit_pos==0:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
35 expanded = os.path.expanduser('~')
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
36 self.set_edit_text(os.path.normpath(expanded+'/'+self.edit_text))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
37 self.set_edit_pos(len(expanded)+1)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
38 elif key == 'ctrl w':
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if self.edit_pos<2:
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 before = self.edit_text[:self.edit_pos]
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:])
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.set_edit_pos(pos)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 return
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 else:
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 return super(PathEdit, self).keypress(size, key)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
49 class FilesViewer(urwid.WidgetWrap):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
50 """List specialised for files"""
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
51
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
52 def __init__(self, onPreviousDir, onDirClick, onFileClick):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
53 self.path=''
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
54 self.key_cache = ''
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
55 self.key_time = time()
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
56 self.onPreviousDir = onPreviousDir
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
57 self.onDirClick = onDirClick
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
58 self.onFileClick = onFileClick
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
59 self.files_list = urwid.SimpleListWalker([])
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
60 self.show_hidden = True
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
61 listbox = urwid.ListBox(self.files_list)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
62 urwid.WidgetWrap.__init__(self, listbox)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
63
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
64 def keypress(self, size, key):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
65 if key=='meta h':
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
66 #(un)hide hidden files
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
67 self.show_hidden = not self.show_hidden
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
68 self.showDirectory(self.path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
69 if key=='meta d':
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
70 #jump to directories
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
71 if self.files_list:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
72 self._w.set_focus(0)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
73 elif key=='meta f':
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
74 for idx in range(len(self.files_list)):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
75 if isinstance(self.files_list[idx].base_widget,urwid.Divider):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
76 if idx<len(self.files_list)-1:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
77 self._w.set_focus(idx+1)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
78 break
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
79 elif len(key) == 1:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
80 if time() - self.key_time > 2:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
81 self.key_cache=key
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
82 else:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
83 self.key_cache+=key
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
84 self.key_time = time()
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
85 for idx in range(len(self.files_list)):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
86 if isinstance(self.files_list[idx],custom_widgets.ClickableText) and self.files_list[idx].get_text().lower().startswith(self.key_cache.lower()):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
87 self._w.set_focus(idx)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
88 break
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
89 else:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
90 return self._w.keypress(size, key)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
91
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
92 def showDirectory(self, path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
93 self.path = path
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
94 del self.files_list[:]
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
95 directories = []
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
96 files = []
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
97 try:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
98 for filename in os.listdir(path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
99 fullpath = os.path.join(path,filename)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
100 if os.path.isdir(fullpath):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
101 directories.append(filename)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
102 else:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
103 files.append(filename)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
104 except OSError:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
105 self.files_list.append(urwid.Text(("warning",_("Impossible to list directory")),'center'))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
106 directories.sort()
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
107 files.sort()
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
108 if os.path.abspath(path)!='/' and os.path.abspath(path) != '//':
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
109 previous_wid = custom_widgets.ClickableText('..',default_attr='directory')
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
110 urwid.connect_signal(previous_wid,'click',self.onPreviousDir)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
111 self.files_list.append(previous_wid)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
112 for directory in directories:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
113 if directory.startswith('.') and not self.show_hidden:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
114 continue
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
115 dir_wid = custom_widgets.ClickableText(directory,default_attr='directory')
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
116 urwid.connect_signal(dir_wid,'click',self.onDirClick)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
117 self.files_list.append(dir_wid)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
118 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator'))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
119 for filename in files:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
120 if filename.startswith('.') and not self.show_hidden:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
121 continue
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
122 file_wid = custom_widgets.ClickableText(filename)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
123 urwid.connect_signal(file_wid,'click',self.onFileClick)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
124 self.files_list.append(file_wid)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
125
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
126
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
127
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 class FileDialog(urwid.WidgetWrap):
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
130 def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
131 """Create file dialog
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
132 @param title: title of the window/popup
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
133 @param style: NOT USED YET #FIXME
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
134 """
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
135 self.ok_cb = ok_cb
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.__home_path = os.path.expanduser('~')
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
137 self.path_wid = PathEdit(_('Path: '))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
138 self.path_wid.setCompletionMethod(self._directory_completion)
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139 urwid.connect_signal(self.path_wid, 'change', self.onPathChange)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
140 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')])
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 bookm_list = urwid.SimpleListWalker([])
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.bookmarks = list(self.getBookmarks())
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 self.bookmarks.sort()
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 for bookmark in self.bookmarks:
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if bookmark.startswith(self.__home_path):
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 bookmark="~"+bookmark[len(self.__home_path):]
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 book_wid = custom_widgets.ClickableText(bookmark)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 bookm_list.append(book_wid)
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title'))
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
151 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick)
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
152 center_row = urwid.Columns([('weight',2,bookm_wid),
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
153 ('weight',8,custom_widgets.VerticalSeparator(self.files_wid))])
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
154
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
155 buttons = []
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
156 buttons.append(custom_widgets.CustomButton(_('Cancel'),cancel_cb))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
157 max_len = max([button.getSize() for button in buttons])
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
158 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
159 main_frame = custom_widgets.FocusFrame(center_row, header, buttons_wid)
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 decorated = custom_widgets.LabelLine(main_frame, custom_widgets.SurroundedText(title))
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
161 urwid.WidgetWrap.__init__(self, decorated)
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
162 self.path_wid.set_edit_text(os.getcwdu())
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
163
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
164 def _directory_completion(self, path, completion_data):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
165 path=os.path.abspath(path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
166 if not os.path.isdir(path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
167 head,dir_start = os.path.split(path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
168 else:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
169 head=path
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
170 dir_start=''
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
171 try:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
172 filenames = os.listdir(head)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
173 filenames.sort()
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
174 try:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
175 start_idx=filenames.index(completion_data['last_dir'])+1
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
176 if start_idx == len(filenames):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
177 start_idx = 0
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
178 except (KeyError,ValueError):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
179 start_idx = 0
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
180 for idx in range(start_idx,len(filenames)) + range(0,start_idx):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
181 full_path = os.path.join(head,filenames[idx])
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
182 if filenames[idx].lower().startswith(dir_start.lower()) and os.path.isdir(full_path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
183 completion_data['last_dir'] = filenames[idx]
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
184 return full_path
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
185 except OSError:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
186 pass
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
187 return path
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def getBookmarks(self):
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
190 gtk_bookm = os.path.expanduser("~/.gtk-bookmarks")
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
191 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xml")
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192 bookmarks = set()
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 try:
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
194 with open(gtk_bookm) as gtk_fd:
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
195 for bm in gtk_fd.readlines():
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 if bm.startswith("file:///"):
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 bookmarks.add(bm[7:].replace('\n',''))
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 except IOError:
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
199 info(_('No GTK bookmarks file found'))
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 pass
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 try:
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 dom = minidom.parse(kde_bookm)
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
204 for elem in dom.getElementsByTagName('bookmark'):
176
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 bm = elem.getAttribute("href")
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 if bm.startswith("file:///"):
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
207 bookmarks.add(bm[7:])
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
208 except IOError:
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 info(_('No KDE bookmarks file found'))
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 pass
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 return bookmarks
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 def onBookmarkSelected(self, button):
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 self.path_wid.set_edit_text(os.path.expanduser(button.get_text()))
a50953ac6191 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216
179
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
217 def onPathChange(self, edit, path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
218 if os.path.isdir(path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
219 self.files_wid.showDirectory(path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
220
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
221 def onPreviousDir(self, wid):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
222 path = os.path.abspath(self.path_wid.get_edit_text())
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
223 if not os.path.isdir(path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
224 path = dirname(path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
225 self.path_wid.set_edit_text(os.path.split(path)[0])
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
226
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
227 def onDirClick(self, wid):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
228 path = os.path.abspath(self.path_wid.get_edit_text())
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
229 if not os.path.isdir(path):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
230 path = dirname(path)
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
231 self.path_wid.set_edit_text(os.path.join(path,wid.get_text()))
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
232
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
233 def onFileClick(self, wid):
d6c0c5dca9b9 Primitivus: new FileDialog
Goffi <goffi@goffi.org>
parents: 176
diff changeset
234 self.ok_cb(os.path.abspath(os.path.join(self.files_wid.path,wid.get_text())))