view frontends/wix/form.py @ 39:2e3411a6baad

Wix: external server management in gateways manager, SàT: bug fixes in gateway management - SàT: from twisted.words.protocols.jabber.error module imported as jab_error to avoir name colision with error log method - XEP_0100 plugin: added errback for disco info - XEP_0077 plugin: registration callback now look for "x" element (data form: see XEP-0004), and send an error message if it can't find it. - Wix: added fields for browsing external server gateways
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2009 17:29:02 +1100
parents a61beb21d16d
children a5b5fb5fc9fd
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
wix: a SAT frontend
Copyright (C) 2009  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 wx
import pdb
from xml.dom import minidom
from logging import debug, info, error
from tools.jid  import JID


class Form(wx.Frame):
    def __init__(self, host, target, xml_data='', title="Form", type="Form"):
        super(Form, self).__init__(None, title=title)

        self.host = host
        self.target = target
        self.ctl_list = []  # usefull to access ctrl

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(True)
        
        #events
        self.Bind(wx.EVT_CLOSE, self.onClose, self)
        
        self.MakeModal()

        self.constructForm(xml_data)

        self.Show()

    def constructForm(self, xml_data):
        panel=wx.Panel(self)
        panel.sizer = wx.BoxSizer(wx.VERTICAL)

        cat_dom = minidom.parseString(xml_data.encode('utf-8'))
        
        for elem in cat_dom.documentElement.getElementsByTagName("elem"):
            name = elem.getAttribute("name")
            label = elem.getAttribute("label") if elem.hasAttribute('label') else name
            type = elem.getAttribute("type")
            value = elem.firstChild.wholeText if elem.firstChild else u''  #TODO: must check if the first child is really the text value
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            if type=="text":
                ctrl = wx.StaticText(panel, -1, value)
            elif type=="string":
                label=wx.StaticText(panel, -1, label+": ")
                ctrl = wx.TextCtrl(panel, -1, value)
                self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
                sizer.Add(label)
            elif type=="password":
                label=wx.StaticText(panel, -1, label+": ")
                ctrl = wx.TextCtrl(panel, -1, value, style=wx.TE_PASSWORD)
                self.ctl_list.append({'name':name, 'type':type, 'control':ctrl})
                sizer.Add(label)
            else:
                error("FIXME FIXME FIXME: type [%s] is not implemented" % type)  #FIXME !
                raise NotImplementedError
            sizer.Add(ctrl, 1, flag=wx.EXPAND)
            #self.ctl_list[(name, category)] = ctrl
            panel.sizer.Add(sizer, flag=wx.EXPAND)

        submitButton = wx.Button(panel,wx.ID_OK, label="Submit")
        cancelButton = wx.Button(panel,wx.ID_CANCEL)
        dialogButtons = wx.StdDialogButtonSizer()
        dialogButtons.AddButton(submitButton)
        dialogButtons.AddButton(cancelButton)
        dialogButtons.Realize()
        panel.Bind(wx.EVT_BUTTON, self.onFormSubmitted, submitButton)
        panel.Bind(wx.EVT_BUTTON, self.onFormCancelled, cancelButton)
        panel.sizer.Add(dialogButtons, flag=wx.ALIGN_CENTER_HORIZONTAL)

        panel.SetSizer(panel.sizer)
        panel.SetAutoLayout(True)
        panel.sizer.Fit(self)
        self.sizer.Add(panel, 1, flag=wx.EXPAND)
        cat_dom.unlink()

    def onFormSubmitted(self, event):
        """Called when submit button is clicked"""
        debug("Submitting form")
        data = []
        for ctrl in self.ctl_list:
            data.append((ctrl["name"], ctrl["control"].GetValue()))
        print "submitting:",data
        id = self.host.bridge.gatewayRegister("SUBMIT",self.target, data)
        self.host.current_action_ids.add(id)
        print "action id:",id
        self.MakeModal(False)
        self.Destroy()
        
    def onFormCancelled(self, event):
        """Called when cancel button is clicked"""
        debug("Cancelling form")
        self.MakeModal(False)
        self.Close()
   
    def onClose(self, event):
        """Close event: we have to send the form."""
        debug("close")
        self.MakeModal(False)
        event.Skip()