annotate frontends/primitivus/files_management.py @ 21:96a2c5904e35

Primitivus: send_file first draft - a new dialog for choosing a file is work in progress
author Goffi <goffi@goffi.org>
date Thu, 12 Aug 2010 23:09:31 +0800
parents
children de305d93a503
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
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import custom_widgets
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from tools.jid import JID
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import os, os.path
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from xml.dom import minidom
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from logging import debug, info, error
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 class PathEdit(custom_widgets.AdvancedEdit):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def keypress(self, size, key):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 if key == 'ctrl w':
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 if self.edit_pos<2:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 before = self.edit_text[:self.edit_pos]
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 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
38 self.set_edit_pos(pos)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 return
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 else:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 return super(PathEdit, self).keypress(size, key)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 class FileDialog(urwid.WidgetWrap):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 def __init__(self,title=_("Please select a file")):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.__home_path = os.path.expanduser('~')
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.path_wid = PathEdit(_('Path: '), os.getcwdu())
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 urwid.connect_signal(self.path_wid, 'change', self.onPathChange)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')])
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 bookm_list = urwid.SimpleListWalker([])
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.bookmarks = list(self.getBookmarks())
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.bookmarks.sort()
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for bookmark in self.bookmarks:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if bookmark.startswith(self.__home_path):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 bookmark="~"+bookmark[len(self.__home_path):]
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 book_wid = custom_widgets.ClickableText(bookmark)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 bookm_list.append(book_wid)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title'))
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.files_list = urwid.SimpleListWalker([urwid.Text('toto.mkv')])
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 files_wid = urwid.ListBox(self.files_list)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 center_row = urwid.Columns([('weight',2,bookm_wid),
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 ('weight',8,custom_widgets.VerticalSeparator(files_wid))])
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 main_frame = custom_widgets.FocusFrame(center_row, header)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 decorated = custom_widgets.LabelLine(main_frame, custom_widgets.SurroundedText(title))
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 urwid.WidgetWrap.__init__(self, decorated)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def getBookmarks(self):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 gnome_bookm = os.path.expanduser("~/.gtk-bookmarks")
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xm")
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 bookmarks = set()
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 try:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 with open(gnome_bookm) as gnome_fd:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 for bm in gnome_fd.readlines():
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if bm.startswith("file:///"):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 bookmarks.add(bm[7:].replace('\n',''))
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 except IOError:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 info(_('No Gnome bookmarks file found'))
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 pass
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 try:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 dom = minidom.parse(kde_bookm)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 for elem in getElementsByTagName('bookmark'):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 bm = elem.getAttribute("href")
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if bm.startswith("file:///"):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 bookmarks.add(bm[7:])
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 except IOError:
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 info(_('No KDE bookmarks file found'))
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 pass
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return bookmarks
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 def onBookmarkSelected(self, button):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 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
95
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def onPathChange(self, edit, text):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if os.path.isdir(text):
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 del self.files_list[:]
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 files = os.listdir(text)
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 files.sort()
96a2c5904e35 Primitivus: send_file first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.files_list.extend([custom_widgets.ClickableText(filename) for filename in files])