annotate frontends/primitivus/files_management.py @ 176:a50953ac6191

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