- add meta informations for upcoming extension manager (not yet activated)9
[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.Sources.StaticText import StaticText
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="center,center" size="560,300" title="UserDialog">
52                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
53                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
54                         <widget name="config" position="5,50" size="550,200" zPosition="1" scrollbarMode="showOnDemand" />
55                         <ePixmap pixmap="skin_default/div-h.png" position="0,270" zPosition="1" size="560,2" />
56                         <widget source="introduction" render="Label" position="10,280" size="540,21" zPosition="10" font="Regular;21" halign="center" valign="center" backgroundColor="#25062748" transparent="1"/>
57                         <widget name="VKeyIcon" pixmap="skin_default/buttons/key_text.png" position="10,280" zPosition="10" size="35,25" transparent="1" alphatest="on" />
58                         <widget name="HelpWindow" pixmap="skin_default/vkey_icon.png" position="160,250" zPosition="1" size="1,1" transparent="1" alphatest="on" />     
59                 </screen>"""
60
61         def __init__(self, session, plugin_path, hostinfo = None ):
62                 self.skin_path = plugin_path
63                 self.session = session
64                 Screen.__init__(self, self.session)
65                 self.hostinfo = hostinfo
66                 self.cache_ttl = 86400 #600 is default, 0 disables, Seconds cache is considered valid
67                 self.cache_file = '/etc/enigma2/' + self.hostinfo + '.cache' #Path to cache directory
68                 self.createConfig()
69
70                 self["actions"] = NumberActionMap(["SetupActions"],
71                 {
72                         "ok": self.ok,
73                         "back": self.close,
74                         "cancel": self.close,
75                         "red": self.close,
76                 }, -2)
77
78                 self["VirtualKB"] = ActionMap(["VirtualKeyboardActions"],
79                 {
80                         "showVirtualKeyboard": self.KeyText,
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["VKeyIcon"] = Pixmap()
89                 self["HelpWindow"] = Pixmap()
90                 self["introduction"] = StaticText(_("Press OK to save settings."))
91                 self["key_red"] = StaticText(_("Close"))
92
93         def layoutFinished(self):
94                 self.setTitle(_("Enter user and password for host: ")+ self.hostinfo)
95
96         # helper function to convert ips from a sring to a list of ints
97         def convertIP(self, ip):
98                 strIP = ip.split('.')
99                 ip = []
100                 for x in strIP:
101                         ip.append(int(x))
102                 return ip
103
104         def createConfig(self):
105                 self.usernameEntry = None
106                 self.passwordEntry = None
107                 self.username = None
108                 self.password = None
109
110                 if os_path.exists(self.cache_file):
111                         print 'Loading user cache from ',self.cache_file
112                         try:
113                                 self.hostdata = load_cache(self.cache_file)
114                                 username = self.hostdata['username']
115                                 password = self.hostdata['password']
116                         except:
117                                 username = "username"
118                                 password = "password"
119                 else:
120                         username = "username"
121                         password = "password"
122
123                 self.username = NoSave(ConfigText(default = username, visible_width = 50, fixed_size = False))
124                 self.password = NoSave(ConfigPassword(default = password, visible_width = 50, fixed_size = False))
125
126         def createSetup(self):
127                 self.list = []
128                 self.usernameEntry = getConfigListEntry(_("Username"), self.username)
129                 self.list.append(self.usernameEntry)
130                 self.passwordEntry = getConfigListEntry(_("Password"), self.password)
131                 self.list.append(self.passwordEntry)
132
133                 self["config"].list = self.list
134                 self["config"].l.setList(self.list)
135                 self["config"].onSelectionChanged.append(self.selectionChanged)
136
137         def KeyText(self):
138                 if self["config"].getCurrent() == self.usernameEntry:
139                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'username'), VirtualKeyBoard, title = (_("Enter username:")), text = self.username.value)
140                 if self["config"].getCurrent() == self.passwordEntry:
141                         self.session.openWithCallback(lambda x : self.VirtualKeyBoardCallback(x, 'password'), VirtualKeyBoard, title = (_("Enter password:")), text = self.password.value)
142
143         def VirtualKeyBoardCallback(self, callback = None, entry = None):
144                 if callback is not None and len(callback) and entry is not None and len(entry):
145                         if entry == 'username':
146                                 self.username.setValue(callback)
147                                 self["config"].invalidate(self.usernameEntry)
148                         if entry == 'password':
149                                 self.password.setValue(callback)
150                                 self["config"].invalidate(self.passwordEntry)
151
152         def newConfig(self):
153                 if self["config"].getCurrent() == self.InterfaceEntry:
154                         self.createSetup()
155
156         def keyLeft(self):
157                 ConfigListScreen.keyLeft(self)
158
159         def keyRight(self):
160                 ConfigListScreen.keyRight(self)
161
162         def selectionChanged(self):
163                 current = self["config"].getCurrent()
164                 helpwindowpos = self["HelpWindow"].getPosition()
165                 if current[1].help_window.instance is not None:
166                         current[1].help_window.instance.move(ePoint(helpwindowpos[0],helpwindowpos[1]))
167
168         def ok(self):
169                 current = self["config"].getCurrent()
170                 self.hostdata = { 'username': self.username.value, 'password': self.password.value }
171                 write_cache(self.cache_file, self.hostdata)
172                 self.close(True)
173