annotate urwid_satext/files_management.py @ 58:7155b81ffdd5

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