fix selection and bsod
[enigma2-plugins.git] / networkbrowser / src / UserDialog.py
1 # -*- coding: utf-8 -*-
2 # for localized messages
3 from __init__ import _
4 from Screens.Screen import Screen
5 from Screens.MessageBox import MessageBox
6 from Screens.VirtualKeyBoard import VirtualKeyBoard
7 from Components.ActionMap import ActionMap
8 from Components.config import ConfigText, ConfigPassword, NoSave, getConfigListEntry
9 from Components.ConfigList import ConfigListScreen
10 from Components.Label import Label
11 from Components.Pixmap import Pixmap
12 from Components.ActionMap import ActionMap, NumberActionMap
13 from enigma import ePoint
14 from cPickle import dump, load
15 from os import path as os_path, unlink, stat, mkdir
16 from time import time
17 from stat import ST_MTIME
18
19 def write_cache(cache_file, cache_data):
20         #Does a cPickle dump
21         if not os_path.isdir( os_path.dirname(cache_file) ):
22                 try:
23                         mkdir( os_path.dirname(cache_file) )
24                 except OSError:
25                         print os_path.dirname(cache_file), 'is a file'
26         fd = open(cache_file, 'w')
27         dump(cache_data, fd, -1)
28         fd.close()
29
30 def valid_cache(cache_file, cache_ttl):
31         #See if the cache file exists and is still living
32         try:
33                 mtime = stat(cache_file)[ST_MTIME]
34         except:
35                 return 0
36         curr_time = time()
37         if (curr_time - mtime) > cache_ttl:
38                 return 0
39         else:
40                 return 1
41
42 def load_cache(cache_file):
43         #Does a cPickle load
44         fd = open(cache_file)
45         cache_data = load(fd)
46         fd.close()
47         return cache_data
48
49 class UserDialog(Screen, ConfigListScreen):
50         skin = """
51                 <screen name="UserDialog" position="90,110" size="560,380" title="UserDialog">
52                         <widget name="config" position="10,10" size="540,200" zPosition="1" scrollbarMode="showOnDemand" />
53                         <widget name="ButtonGreen" pixmap="skin_default/buttons/button_green.png" position="20,330" zPosition="10" size="15,16" transparent="1" alphatest="on" />
54                         <widget name="introduction2" position="90,300" size="300,20" zPosition="10" font="Regular;21" halign="center" transparent="1" />
55                         <widget name="ButtonRedtext" position="410,345" size="140,21" zPosition="10" font="Regular;21" transparent="1" />
56                         <widget name="ButtonRed" pixmap="skin_default/buttons/button_red.png" position="390,345" zPosition="10" size="15,16" transparent="1" alphatest="on" />
57                         <widget name="VKeyIcon" pixmap="skin_default/vkey_icon.png" position="35,310" zPosition="10" size="60,48" transparent="1" alphatest="on" />
58                         <widget name="HelpWindow" pixmap="skin_default/vkey_icon.png" position="175,300" zPosition="1" size="1,1" transparent="1" alphatest="on" />
59                         <ePixmap pixmap="skin_default/bottombar.png" position="10,290" size="540,120" zPosition="1" transparent="1" alphatest="on" />
60                 </screen>"""
61
62         def __init__(self, session, plugin_path, hostinfo = None ):
63                 self.skin_path = plugin_path
64                 self.session = session
65                 Screen.__init__(self, self.session)
66                 self.hostinfo = hostinfo
67                 self.cache_ttl = 86400 #600 is default, 0 disables, Seconds cache is considered valid
68                 self.cache_file = '/etc/enigma2/' + self.hostinfo + '.cache' #Path to cache directory
69                 self.createConfig()
70
71                 self["shortcuts"] = ActionMap(["ShortcutActions","WizardActions"],
72                 {
73                         "red": self.close,
74                         "back": self.close,
75                         "ok": self.ok,
76                 }, -2)
77
78                 self["VirtualKB"] = ActionMap(["ShortcutActions","WizardActions"],
79                 {
80                         "green": self.KeyGreen,
81                 }, -2)
82
83                 self.list = []
84                 ConfigListScreen.__init__(self, self.list,session = self.session)
85                 self.createSetup()
86                 self.onLayoutFinish.append(self.layoutFinished)
87                 # Initialize Buttons
88                 self["ButtonGreen"] = Pixmap()
89                 self["VKeyIcon"] = Pixmap()
90                 self["HelpWindow"] = Pixmap()
91                 self["introduction2"] = Label(_("Press OK to save settings."))
92                 self["ButtonRed"] = Pixmap()
93                 self["ButtonRedtext"] = Label(_("Close"))
94
95         def layoutFinished(self):
96                 print self["config"].getCurrent()
97                 self.setTitle(_("Enter user and password for host: ")+ self.hostinfo)
98                 self["ButtonGreen"].show()
99                 self["VKeyIcon"].show()
100                 self["VirtualKB"].setEnabled(True)
101                 self["HelpWindow"].show()
102
103         # helper function to convert ips from a sring to a list of ints
104         def convertIP(self, ip):
105                 strIP = ip.split('.')
106                 ip = []
107                 for x in strIP:
108                         ip.append(int(x))
109                 return ip
110
111         def createConfig(self):
112                 self.usernameEntry = None
113                 self.passwordEntry = None
114                 self.username = None
115                 self.password = None
116
117                 if os_path.exists(self.cache_file):
118                         print 'Loading user cache from ',self.cache_file
119                         try:
120                                 self.hostdata = load_cache(self.cache_file)
121                                 username = self.hostdata['username']
122                                 password = self.hostdata['password']
123                         except:
124                                 username = "username"
125                                 password = "password"
126                 else:
127                         username = "username"
128                         password = "password"
129
130                 self.username = NoSave(ConfigText(default = username, visible_width = 50, fixed_size = False))
131                 self.password = NoSave(ConfigPassword(default = password, visible_width = 50, fixed_size = False))
132
133         def createSetup(self):
134                 self.list = []
135                 self.usernameEntry = getConfigListEntry(_("Username"), self.username)
136                 self.list.append(self.usernameEntry)
137                 self.passwordEntry = getConfigListEntry(_("Password"), self.password)
138                 self.list.append(self.passwordEntry)
139
140                 self["config"].list = self.list
141                 self["config"].l.setList(self.list)
142                 self["config"].onSelectionChanged.append(self.selectionChanged)
143
144         def KeyGreen(self):
145                 if self["config"].getCurrent() == self.usernameEntry:
146                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'username'), VirtualKeyBoard, title = (_("Enter username:")), text = self.username.value)
147                 if self["config"].getCurrent() == self.passwordEntry:
148                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'password'), VirtualKeyBoard, title = (_("Enter password:")), text = self.password.value)
149
150         def VirtualKeyBoardCallback(self, callback = None, entry = None):
151                 if callback is not None and len(callback) and entry is not None and len(entry):
152                         if entry == 'username':
153                                 self.username = NoSave(ConfigText(default = callback, visible_width = 50, fixed_size = False))
154                                 self.createSetup()
155                         if entry == 'password':
156                                 self.password = NoSave(ConfigPassword(default = callback, visible_width = 50, fixed_size = False))
157                                 self.createSetup()
158
159         def newConfig(self):
160                 if self["config"].getCurrent() == self.InterfaceEntry:
161                         self.createSetup()
162
163         def keyLeft(self):
164                 ConfigListScreen.keyLeft(self)
165
166         def keyRight(self):
167                 ConfigListScreen.keyRight(self)
168
169         def selectionChanged(self):
170                 current = self["config"].getCurrent()
171                 helpwindowpos = self["HelpWindow"].getPosition()
172                 if current[1].help_window.instance is not None:
173                         current[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
174
175         def ok(self):
176                 current = self["config"].getCurrent()
177                 self.hostdata = { 'username': self.username.value, 'password': self.password.value }
178                 write_cache(self.cache_file, self.hostdata)
179                 self.close(True)
180