better error handling, does not crash any more on loss of connection to mail server
[enigma2-plugins.git] / emailclient / src / EmailConfig.py
1 from Screens.Screen import Screen
2 from Components.config import config, getConfigListEntry
3 from Components.ConfigList import ConfigListScreen
4 from Components.Label import Label
5 from Components.ActionMap import ActionMap
6
7 from . import _, initLog
8 import plugin
9
10 from enigma import getDesktop
11 DESKTOP_WIDTH = getDesktop(0).size().width()
12 DESKTOP_HEIGHT = getDesktop(0).size().height()
13 def scaleH(y2, y1):
14         if y2 == -1:
15                 y2 = y1*1280/720
16         elif y1 == -1:
17                 y1 = y2*720/1280
18         return scale(y2, y1, 1280, 720, DESKTOP_WIDTH)
19 def scaleV(y2, y1):
20         if y2 == -1:
21                 y2 = y1*720/576
22         elif y1 == -1:
23                 y1 = y2*576/720
24         return scale(y2, y1, 720, 576, DESKTOP_HEIGHT)
25 def scale(y2, y1, x2, x1, x):
26         return (y2 - y1) * (x - x1) / (x2 - x1) + y1
27
28 class EmailConfigScreen(ConfigListScreen,Screen):
29         width = max(2*140+100, 550)
30         height = 5*30+50
31         buttonsGap = (width-2*140)/3
32         skin = """
33                 <screen position="%d,%d" size="%d,%d" title="Email Setup" >
34                 <widget name="config" position="0,0" size="%d,%d" scrollbarMode="showOnDemand" />
35                 <ePixmap position="%d,%d" zPosition="4" size="140,40" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
36                 <ePixmap position="%d,%d" zPosition="4" size="140,40" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
37                 <widget name="buttonred" position="%d,%d" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;%d" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
38                 <widget name="buttongreen" position="%d,%d" zPosition="5" size="140,40" valign="center" halign="center" font="Regular;%d" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
39                 <widget name="info" position="%d,%d" size="100,40" halign="right" zPosition="2"  foregroundColor="white" font="Regular;%d"/> 
40                 </screen>""" %(
41                                         (DESKTOP_WIDTH-width)/2, (DESKTOP_HEIGHT-height)/2, width, height,
42                                         width, height-50,  # config
43                                         buttonsGap, height-45,
44                                         2*buttonsGap+140, height-45,
45                                         buttonsGap, height-45, scaleV(22,18),
46                                         2*buttonsGap+140, height-45, scaleV(22,18),
47                                         3*buttonsGap+2*140, height-45, scaleV(22,18)
48                                         )
49
50         def __init__(self, session, args = 0):
51                 Screen.__init__(self, session)
52                 self.list = []
53                 ConfigListScreen.__init__(self, self.list)
54                 self["buttonred"] = Label(_("cancel"))
55                 self["buttongreen"] = Label(_("ok"))
56                 self["info"] = Label('by 3c5x9')
57                 self["setupActions"] = ActionMap(["SetupActions"],
58                 {
59                         "green": self.save,
60                         "red": self.cancel,
61                         "save": self.save,
62                         "cancel": self.cancel,
63                         "ok": self.save,
64                 }, -2)
65                 self.createSetup()
66
67         def createSetup(self):
68                 self.list = [
69                         getConfigListEntry(_("Username"), config.plugins.emailimap.username),
70                         getConfigListEntry(_("Password"), config.plugins.emailimap.password),
71                         getConfigListEntry(_("IMAP Server"), config.plugins.emailimap.server),
72                         getConfigListEntry(_("IMAP Port"), config.plugins.emailimap.port),
73                         getConfigListEntry(_("max of Headers to load"), config.plugins.emailimap.maxheadertoload),
74                         getConfigListEntry(_("show deleted entries"), config.plugins.emailimap.showDeleted),
75                         getConfigListEntry(_("notify about new mails"), config.plugins.emailimap.checkForNewMails)
76                 ]
77                 if config.plugins.emailimap.checkForNewMails.value:
78                         self.list.append(getConfigListEntry(_("check interval (minutes)"), config.plugins.emailimap.checkPeriod))
79                         self.list.append(getConfigListEntry(_("display timeout (seconds)"), config.plugins.emailimap.timeout))
80                 self.list.append(getConfigListEntry(_("Debug"), config.plugins.emailimap.debug))
81                 self["config"].list = self.list
82                 self["config"].l.setList(self.list)
83
84         def keyLeft(self):
85                 ConfigListScreen.keyLeft(self)
86                 self.createSetup()
87
88         def keyRight(self):
89                 ConfigListScreen.keyRight(self)
90                 self.createSetup()
91
92         def save(self):
93                 print "saving"
94                 for x in self["config"].list:
95                         x[1].save()
96                 if plugin.mailChecker:
97                         plugin.mailChecker.exit()
98                 if config.plugins.emailimap.checkForNewMails.value:
99                         plugin.mailChecker = plugin.CheckMail()
100                 initLog()
101                         
102                 self.close(True)
103
104         def cancel(self):
105                 print "cancel"
106                 for x in self["config"].list:
107                         x[1].cancel()
108                 self.close(False)