changeset 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 8537df794f74
children 8f56238309d9
files frontends/primitivus/chat.py frontends/primitivus/custom_widgets.py frontends/primitivus/files_management.py frontends/primitivus/primitivus
diffstat 4 files changed, 112 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/primitivus/chat.py	Thu Aug 12 19:35:56 2010 +0800
+++ b/frontends/primitivus/chat.py	Thu Aug 12 23:09:31 2010 +0800
@@ -26,6 +26,7 @@
 import time
 from tools.jid  import JID
 from card_game import CardGame
+from files_management import FileDialog
 
 
 class ChatText(urwid.FlowWidget):
@@ -131,6 +132,8 @@
         if self.type == 'group':
             game = _("Game")
             menu.addMenu(game, "Tarot", self.onTarotRequest)
+        elif self.type == 'one2one':
+            menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest)
         return menu
 
     def setType(self, type):
@@ -250,3 +253,8 @@
             self.host.showPopUp(custom_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp)) 
         else:
             self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)
+    
+    def onSendFileRequest(self, menu):
+        dialog = FileDialog()
+        self.host.showPopUp(dialog, 80, 80)
+
--- a/frontends/primitivus/custom_widgets.py	Thu Aug 12 19:35:56 2010 +0800
+++ b/frontends/primitivus/custom_widgets.py	Thu Aug 12 23:09:31 2010 +0800
@@ -899,7 +899,7 @@
         top_columns.widget_list[1] = label_widget
 
 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap):
-    def __init__(self, original_widget, left_char = utf8decode("│"), right_char = ''):
+    def __init__(self, original_widget, left_char = u"│", right_char = ''):
         """Draw a separator on left and/or right of original_widget."""
         
         widgets = [original_widget]
--- /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])
--- a/frontends/primitivus/primitivus	Thu Aug 12 19:35:56 2010 +0800
+++ b/frontends/primitivus/primitivus	Thu Aug 12 23:09:31 2010 +0800
@@ -230,10 +230,10 @@
             #we still have popup to show, we display it
             self.showPopUp(next_popup)
 
-    def showPopUp(self, pop_up_widget):
+    def showPopUp(self, pop_up_widget, perc_width=40, perc_height=40):
         "Show a pop-up window if possible, else put it in queue"
         if not isinstance(self.loop.widget,urwid.Overlay):
-            display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', 40), 'middle', ('relative', 40))
+            display_widget = urwid.Overlay(pop_up_widget, self.main_widget, 'center', ('relative', perc_width), 'middle', ('relative', perc_height))
             self.loop.widget = display_widget
         else:
             self.notBar.addPopUp(pop_up_widget)