autotimer,pluginsort: allow mphelp localization
[enigma2-plugins.git] / dreamirc / src / dreamIRCSetup.py
1 ##
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.config import *
6 from Components.ConfigList import *
7 from Components.Sources.StaticText import StaticText
8 from Tools.HardwareInfo import *
9
10 import xml.dom.minidom
11 from xml.dom.minidom import Node
12 from xml.dom import EMPTY_NAMESPACE
13 from Tools import XMLTools
14 from Tools.XMLTools import elementsWithTag, mergeText
15
16 from socket import gethostbyname_ex
17
18 from dreamIRCTools import *
19
20 class dreamIRCSetupScreen(ConfigListScreen, Screen):
21         from enigma import getDesktop
22         desk = getDesktop(0)
23         x= int(desk.size().width())
24         y= int(desk.size().height())
25         print "[dreamIRC] setup: current desktop size: %dx%d" % (x,y)
26
27         if (y>=720):
28                 skin = """
29                         <screen position="390,205" size="500,300" title="dreamIRC - edit settings" >
30                         <widget name="config" position="10,10" size="480,260" scrollbarMode="showOnDemand" />
31                         <ePixmap pixmap="skin_default/div-h.png" position="10,245" size="480,2" transparent="1" alphatest="on" />
32                         <ePixmap pixmap="skin_default/buttons/red.png" position="60,250" size="160,40" alphatest="on" />
33                         <ePixmap pixmap="skin_default/buttons/green.png" position="280,250" size="160,40" alphatest="on" />
34                         <widget source="key_red" render="Label" position="60,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
35                         <widget source="key_green" render="Label" position="280,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
36                 </screen>"""
37         else:
38                 skin = """
39                         <screen position="110,145" size="500,300" title="dreamIRC - edit settings" >
40                         <widget name="config" position="10,10" size="480,260" scrollbarMode="showOnDemand" />
41                         <ePixmap pixmap="skin_default/div-h.png" position="10,245" size="480,2" transparent="1" alphatest="on" />
42                         <ePixmap pixmap="skin_default/buttons/red.png" position="60,250" size="160,40" alphatest="on" />
43                         <ePixmap pixmap="skin_default/buttons/green.png" position="280,250" size="160,40" alphatest="on" />
44                         <widget source="key_red" render="Label" position="60,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
45                         <widget source="key_green" render="Label" position="280,250" zPosition="1" size="150,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
46                 </screen>"""
47
48         def __init__(self, session, args = 0):
49                 Screen.__init__(self, session)
50                 self.hardware_info = HardwareInfo()
51                 self.device=self.hardware_info.get_device_name()
52                 self.mac=getMacAddress()
53                 self.mac_end=self.mac[6:]
54                 self.dreamIRCconf = ConfigSubsection()
55                 self.reloadFile()
56                 list = []
57                 list.append(getConfigListEntry(_('Nickname'), self.dreamIRCconf.nick))      
58                 if config.usage.setup_level.index > 1: # advanced
59                         list.append(getConfigListEntry(_('Passwd'), self.dreamIRCconf.passwd))      
60                 if config.usage.setup_level.index >= 1: # intermediate+
61                         list.append(getConfigListEntry(_('Server1'), self.dreamIRCconf.server1))      
62                 if config.usage.setup_level.index > 1: # advanced
63                         list.append(getConfigListEntry(_('Server2'), self.dreamIRCconf.server2))      
64                         list.append(getConfigListEntry(_('Server3'), self.dreamIRCconf.server3))      
65                 if config.usage.setup_level.index >= 1: # intermediate+
66                         list.append(getConfigListEntry(_('Port'), self.dreamIRCconf.port))          
67                 list.append(getConfigListEntry(_('Channel'), self.dreamIRCconf.channel))    
68                 if config.usage.setup_level.index > 1: # i
69                         list.append(getConfigListEntry(_('Debug'), self.dreamIRCconf.debug))        
70
71                 self["key_red"] = StaticText(_("Cancel"))
72                 self["key_green"] = StaticText(_("Save"))
73
74                 ConfigListScreen.__init__(self, list)                                     
75                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
76                                 {
77                                                 "green": self.saveAndExit,
78                                                 "red": self.dontSaveAndExit,
79                                                 "cancel": self.dontSaveAndExit
80                                 }, -1)
81
82         def load(self):
83                 self.reloadFile()
84                 self.accounts=[ircsupport.IRCAccount(self.type, string.atoi(self.nr), str(self.nick), str(self.passwd), str(self.server1), string.atoi(self.port), str(self.channel))]
85                 print self.accounts
86                 return self.accounts
87
88         def reloadFile(self):
89                 try:
90                         doc = xml.dom.minidom.parse(accounts_xml)
91                         root = doc.childNodes[0]        
92                         for node in elementsWithTag(root.childNodes, "account"):
93                                 self.nick = node.getAttribute("nick")
94                                 self.passwd = node.getAttribute("passwd")
95                                 self.server1 = node.getAttribute("server1")
96                                 self.server2 = node.getAttribute("server2")
97                                 self.server3 = node.getAttribute("server3")
98                                 self.port = node.getAttribute("port")
99                                 self.channel = node.getAttribute("channel")
100                                 self.debug = node.getAttribute("debug")
101                         if ((self.nick.lower() == "dreamircuser") or (self.nick == "") or (self.nick[0] == " ") or (self.nick.lower() == "dm8000-vip")) :
102                                 print "[dreamIRC] nickname error... restoring default..."
103                                 self.nick = self.device+"_"+self.mac_end
104                 except IOError:
105                         self.type = "IRC"
106                         self.login = "1"
107                         self.nick = self.device+"_"+self.mac_end
108                         self.passwd = ""
109                         self.server1 = "irc.belwue.de"
110                         self.server2 = "irc.freenet.de"
111                         self.server3 = "irc.tu-illmenau.de"
112                         self.port = "06667"
113                         self.channel = "#dreamirc"
114                         self.debug = "False"
115
116                 if self.debug != "True" or self.debug != "False":
117                         self.debug="False"      
118                 self.dreamIRCconf.nick = ConfigText(default = self.nick, fixed_size = False)
119                 self.dreamIRCconf.passwd = ConfigText(default = self.passwd, fixed_size = False)
120                 self.dreamIRCconf.server1 = ConfigText(default = self.server1, fixed_size = False)
121                 self.dreamIRCconf.server2 = ConfigText(default = self.server2, fixed_size = False)
122                 self.dreamIRCconf.server3 = ConfigText(default = self.server3, fixed_size = False)
123                 self.dreamIRCconf.port = ConfigInteger(default = string.atoi(self.port), limits = (0, 99999))
124                 self.dreamIRCconf.channel = ConfigText(default = self.channel, fixed_size = False)
125                 self.dreamIRCconf.debug = ConfigSelection(default=self.debug, choices = ["False","True"])
126
127         def keySave(self):
128                 self.accounts=[]
129                 self.type = "IRC"
130                 self.login = "1"
131                 self.nick = self.dreamIRCconf.nick.value
132                 self.passwd = self.dreamIRCconf.passwd.value
133                 self.server1 = self.dreamIRCconf.server1.value
134                 self.server2 = self.dreamIRCconf.server2.value
135                 self.server3 = self.dreamIRCconf.server3.value
136                 self.port = self.dreamIRCconf.port.value
137                 self.channel = self.dreamIRCconf.channel.value
138                 self.debug = self.dreamIRCconf.debug.value
139                 global accounts_xml
140                 fp = file(accounts_xml, 'w')
141                 fp.write("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n")
142                 fp.write("<accounts>\n")
143                 fp.write("    <account type=\"%s\" login=\"%s\" nick=\"%s\" passwd=\"%s\" server1=\"%s\" server2=\"%s\" server3=\"%s\" port=\"%s\" channel=\"%s\" debug=\"%s\" />\n" % (self.type, self.login, self.nick, self.passwd, self.server1, self.server2, self.server3, self.port, self.channel, self.debug))
144                 fp.write("</accounts>\n")
145                 fp.close()
146                 if self.server1:
147                         self.checkServer(self.server1)
148                 if self.server2:
149                         self.checkServer(self.server2)
150                 if self.server3:
151                         self.checkServer(self.server3)
152
153         def checkServer(self, server):
154                         try:
155                                 result=gethostbyname_ex(server)
156                         except:
157                                 self.session.open(MessageBox, _("irc server %s not responding!\nplease check your network settings and/or irc servername..." %server), MessageBox.TYPE_ERROR)   
158
159         def saveAndExit(self):
160                 for x in self["config"].list:
161                         x[1].save()
162                 self.keySave()
163                 self.close()
164
165         def dontSaveAndExit(self):
166                 for x in self["config"].list:
167                         x[1].cancel()
168                 self.close()
169                 
170 class dreamIRCConfig:
171         def load(self):
172                 self.pipe=MessagePipe()
173                 self.status1=False
174                 self.status2=False
175                 self.status3=False
176                 self.hardware_info = HardwareInfo()
177                 self.device=self.hardware_info.get_device_name()
178                 self.mac=getMacAddress()
179                 self.mac_end=self.mac[6:]
180                 try:
181                         doc = xml.dom.minidom.parse(accounts_xml)
182                         root = doc.childNodes[0]
183                         for node in elementsWithTag(root.childNodes, "account"):
184                                 self.type = node.getAttribute("type")
185                                 self.login = node.getAttribute("login")
186                                 self.nick = node.getAttribute("nick")
187                                 if ((self.nick.lower() == "dreamircuser") or (self.nick == "") or (self.nick[0] == " ") or (self.nick.lower() == "dm8000-vip")) :
188                                         print "[dreamIRC] nickname error... restoring default..."
189                                         self.nick = self.device+"_"+self.mac_end
190                                 self.passwd = node.getAttribute("passwd")
191                                 self.server1 = node.getAttribute("server1") # atm only ip.. cause of probs with theads and dns..
192                                 self.server2 = node.getAttribute("server2") 
193                                 self.server3 = node.getAttribute("server3") 
194                                 self.port = node.getAttribute("port")
195                                 self.channel = node.getAttribute("channel")
196                                 self.debug = node.getAttribute("debug") # not used yet.. later will enable/disable console debug out..
197                 except IOError:
198                         self.type = "IRC"
199                         self.login = "1"
200                         self.nick = self.device+"_"+self.mac_end
201                         self.passwd = ""
202                         self.server1 = "irc.freenet.de"
203                         self.server2 = "irc.freenet.de"
204                         self.server3 = "irc.tu-illmenau.de"
205                         self.port = "06667"
206                         self.channel = "#dreamirc"
207                         self.debug = ""
208                 self.server1 = self.server1.strip()
209                 self.server2 = self.server2.strip()
210                 self.server3 = self.server3.strip()
211                 if self.server3:
212                         try:
213                                 self.result3=gethostbyname_ex(self.server3)
214                                 if self.result3:
215                                         for ip_tmp3 in self.result3[2]:
216                                                 self.ip=ip_tmp3
217                                                 self.server=self.server3
218                                                 self.status3=True
219                         except:
220                                 print "unable to resolve hostname %s..." % self.server3
221                 if self.server2:
222                         try:
223                                 self.result2=gethostbyname_ex(self.server2)
224                                 if self.result2:
225                                         for ip_tmp2 in self.result2[2]:
226                                                 self.ip=ip_tmp2
227                                                 self.server=self.server2
228                                                 self.status2=True
229                         except:
230                                 print "unable to resolve hostname %s..." % self.server2
231                 if self.server1:
232                         try:
233                                 self.result1=gethostbyname_ex(self.server1)
234                                 if self.result1:
235                                         for ip_tmp1 in self.result1[2]:
236                                                 self.ip=ip_tmp1
237                                                 self.server=self.server1
238                                                 self.status1=True
239                         except:
240                                 print "unable to resolve hostname %s..." % self.server1
241                                 
242                 if self.status1==False and self.status2==False and self.status3==False:
243                         self.pipe.add("ERROR!!! no irc server was valid... please check settings...")
244                         return False
245                 else:
246                         print " account = type: %s login:%s nick:%s passwd:%s server:%s ip:%s port:%s channel:%s debug:%s " % (self.type, self.login, self.nick, self.passwd, self.server, self.ip, self.port, self.channel, self.debug)
247                         self.accounts=[ircsupport.IRCAccount(self.type, string.atoi(self.login), str(self.nick), str(self.passwd), str(self.ip), string.atoi(self.port), str(self.channel))]
248                         print self.accounts
249                         return self.accounts
250         
251         def channel(self):
252                         doc = xml.dom.minidom.parse(accounts_xml)
253                         root = doc.childNodes[0]
254                         for node in elementsWithTag(root.childNodes, "account"):
255                                 self.channel = node.getAttribute("channel")
256                         return self.channel
257
258
259