diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/frontends/primitivus/files_management.py	Thu Aug 12 23:09:31 2010 +0800
@@ -0,0 +1,101 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+"""
+Primitivus: a SAT frontend
+Copyright (C) 2009, 2010  Jérôme Poisson (goffi@goffi.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import urwid
+import custom_widgets
+from tools.jid import JID
+import os, os.path
+from xml.dom import minidom
+from logging import debug, info, error
+
+class PathEdit(custom_widgets.AdvancedEdit):
+    
+    def keypress(self, size, key):
+        if key == 'ctrl w':
+            if self.edit_pos<2:
+                return
+            before = self.edit_text[:self.edit_pos]
+            pos = (before[:-1] if before.endswith('/') else before).rfind("/")+1
+            self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:])
+            self.set_edit_pos(pos)
+            return
+        else:
+            return super(PathEdit, self).keypress(size, key) 
+
+class FileDialog(urwid.WidgetWrap):
+
+    def __init__(self,title=_("Please select a file")):
+        self.__home_path = os.path.expanduser('~')
+        self.path_wid = PathEdit(_('Path: '), os.getcwdu())
+        urwid.connect_signal(self.path_wid, 'change', self.onPathChange)
+        header = urwid.Pile([self.path_wid, urwid.Divider(u'─')])
+        bookm_list = urwid.SimpleListWalker([])
+        self.bookmarks = list(self.getBookmarks())
+        self.bookmarks.sort()
+        for bookmark in self.bookmarks:
+            if bookmark.startswith(self.__home_path):
+                bookmark="~"+bookmark[len(self.__home_path):]
+            book_wid = custom_widgets.ClickableText(bookmark)
+            urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected)
+            bookm_list.append(book_wid)
+        bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title'))
+        self.files_list = urwid.SimpleListWalker([urwid.Text('toto.mkv')])
+        files_wid = urwid.ListBox(self.files_list)
+        center_row = urwid.Columns([('weight',2,bookm_wid),
+                     ('weight',8,custom_widgets.VerticalSeparator(files_wid))])
+        main_frame = custom_widgets.FocusFrame(center_row, header)
+        decorated = custom_widgets.LabelLine(main_frame, custom_widgets.SurroundedText(title))
+        urwid.WidgetWrap.__init__(self, decorated)
+
+    def getBookmarks(self):
+        gnome_bookm = os.path.expanduser("~/.gtk-bookmarks")
+        kde_bookm = os.path.expanduser("~/.kde/share/apps/kfileplaces/bookmarks.xm")
+        bookmarks = set()
+        try:
+            with open(gnome_bookm) as gnome_fd:
+                for bm in gnome_fd.readlines():
+                    if bm.startswith("file:///"):
+                        bookmarks.add(bm[7:].replace('\n',''))
+        except IOError:
+            info(_('No Gnome bookmarks file found'))
+            pass
+        
+        try:
+            dom = minidom.parse(kde_bookm)
+            for elem in getElementsByTagName('bookmark'):
+                bm = elem.getAttribute("href")
+                if bm.startswith("file:///"):
+                    bookmarks.add(bm[7:])
+        except IOError:
+            info(_('No KDE bookmarks file found'))
+            pass
+            
+        return bookmarks
+
+    def onBookmarkSelected(self, button):
+        self.path_wid.set_edit_text(os.path.expanduser(button.get_text()))
+
+    def onPathChange(self, edit, text):
+        if os.path.isdir(text):
+            del self.files_list[:]
+            files = os.listdir(text)
+            files.sort()
+            self.files_list.extend([custom_widgets.ClickableText(filename) for filename in files])