dreamIRC initial check-in
[enigma2-plugins.git] / dreamirc / src / plugin.py
1 from enigma import *
2 from Screens.Screen import Screen
3 from Screens.VirtualKeyBoard import VirtualKeyBoard
4
5 from Components.Pixmap import Pixmap
6 from Components.ActionMap import HelpableActionMap, ActionMap, NumberActionMap
7 from Components.ScrollLabel import ScrollLabel
8 from Components.Input import Input
9 from Components.Label import Label
10 from Components.HTMLComponent import *
11 from Components.GUIComponent import *
12 from Plugins.Plugin import PluginDescriptor
13
14 from Tools.NumericalTextInput import *
15 from Tools.Directories import *
16
17 #from socket import socket
18
19 import e2reactor
20
21 from twisted.internet import reactor
22 from twisted.internet import protocol
23 from twisted.python import log
24 from twisted.internet.defer import *
25
26 #import e2chat, e2account, e2support, dreamIRCTools, dreamIRCSetup
27 from e2chat import *
28 from e2account import *
29 from e2support import *
30 from dreamIRCTools import *
31 from dreamIRCSetup import *
32 from protocols import irc
33 import ircsupport
34
35 #from enigma import eLabel, eWidget, eSlider, fontRenderClass, ePoint, eSize, getDesktop
36
37 import os 
38 import string
39 import time
40 import datetime
41 import sys
42 x=0
43 y=0
44
45 class dreamIRCMainMenu(Screen):
46
47         from enigma import getDesktop
48         desk = getDesktop(0)
49         global x,y
50         x= int(desk.size().width())
51         y= int(desk.size().height())
52         print "[dreamIRC] mainscreen: current desktop size: %dx%d" % (x,y)
53
54         if (y>=720):
55                 skin = """
56                         <screen position="80,80" size="1120,600"  title="dreamIRC" >
57                                 <widget name="buddy" position="940,35" size="170,450" font="Regular;14" />
58                                 <widget name="chat" position="10,35" size="920,460" font="Regular;14" />
59                                 <widget name="input" position="10,550" size="830,20" font="Regular;16" />
60                                 <widget name="chat.desc" position="10,10" size="460,20" font="Regular;16" />
61                                 <widget name="buddy.desc" position="940,10" size="120,20" font="Regular;16" />
62                                 <widget name="input.desc" position="10,520" size="360,18" font="Regular;16" />
63                                 <widget name="red.pic" position="910,511" size="15,15" pixmap="skin_default/buttons/button_red.png" transparent="1" alphatest="on"/>
64                                 <widget name="green.pic" position="910,531" size="15,15" pixmap="skin_default/buttons/button_green.png" transparent="1" alphatest="on"/>
65                                 <widget name="yellow.pic" position="910,551" size="15,15" pixmap="skin_default/buttons/button_yellow.png" transparent="1" alphatest="on"/>
66                                 <widget name="blue.pic" position="910,571" size="15,15" pixmap="skin_default/buttons/button_blue.png" transparent="1" alphatest="on"/>
67                                 <widget name="disconnect.desc" position="940,510" size="110,20" font="Regular;16" />
68                                 <widget name="connect.desc" position="940,530" size="110,20" font="Regular;16" />
69                                 <widget name="settings.desc" position="940,550" size="110,20" font="Regular;16" />
70                                 <widget name="blue.desc" position="940,570" size="180,20" font="Regular;16" />                                
71                         </screen>"""
72         else:   
73                 skin = """
74                         <screen position="60,80" size="600,450"  title="dreamIRC" >
75                                 <widget name="buddy" position="480,35" size="120,310" font="Regular;14" />
76                                 <widget name="chat" position="10,35" size="460,310" font="Regular;14" />
77                                 <widget name="input" position="10,400" size="360,20" font="Regular;16" />
78                                 <widget name="chat.desc" position="10,10" size="460,20" font="Regular;16" />
79                                 <widget name="buddy.desc" position="480,10" size="120,20" font="Regular;16" />
80                                 <widget name="input.desc" position="10,370" size="360,18" font="Regular;16" />
81                                 <widget name="red.pic" position="470,362" size="15,15" pixmap="skin_default/buttons/button_red.png" transparent="1" alphatest="on"/>
82                                 <widget name="green.pic" position="470,382" size="15,15" pixmap="skin_default/buttons/button_green.png" transparent="1" alphatest="on"/>
83                                 <widget name="yellow.pic" position="470,402" size="15,15" pixmap="skin_default/buttons/button_yellow.png" transparent="1" alphatest="on"/>
84                                 <widget name="blue.pic" position="470,422" size="15,15" pixmap="skin_default/buttons/button_blue.png" transparent="1" alphatest="on"/>
85                                 <widget name="disconnect.desc" position="490,360" size="110,20" font="Regular;16" />
86                                 <widget name="connect.desc" position="490,380" size="110,20" font="Regular;16" />
87                                 <widget name="settings.desc" position="490,400" size="110,20" font="Regular;16" />
88                                 <widget name="blue.desc" position="490,420" size="110,20" font="Regular;16" />
89                         </screen>"""
90                 
91         
92         def __init__(self, session, args = 0):
93                 global x,y
94                 self.skin = dreamIRCMainMenu.skin
95                 Screen.__init__(self, session)
96
97                 self.menu = args
98                 self.pipe=MessagePipe()
99                 self.account = AccountManager(self.session)
100
101                 self.list = []
102                 self.menuList = []
103                 
104                 self.connected = False
105
106                 self["buddy"] = BuddyWindow("")
107                 self["chat"] = ChatWindow("")
108                 self["input"] = Input("")
109
110                 self["buddy.desc"] = Label(_("User Online"))
111                 self["input.desc"] = Label(_("Type your text here and press OK to send:"))
112                 self["chat.desc"] = ChanName(_("ChatBox"))
113                 self["connect.desc"] = Label(_("Connect"))
114                 self["disconnect.desc"] = Label(_("Disconnect"))
115                 self["settings.desc"] = Label(_("Settings"))
116                 if y>=720:
117                                 self["blue.desc"] = Label(_("virtual Keyboard"))
118                 else:
119                                 self["blue.desc"] = Label(_("virtual Keyb."))                           
120                 self["green.pic"] = Pixmap()
121                 self["red.pic"] = Pixmap()
122                 self["yellow.pic"] = Pixmap()
123                 self["blue.pic"] = Pixmap()
124                 
125                 self.checkStatus()
126                 
127                 self["actions"] = NumberActionMap(["dreamIRCActions", "InputBoxActions", "InputAsciiActions", "KeyboardInputActions"],
128                 {
129                         "gotAsciiCode": self.gotAsciiCode,
130                         "red": self.redPressed,
131                         "green": self.greenPressed,
132                         "yellow": self.yellowPressed,
133                         "blue": self.bluePressed,
134                         "ok": self.go,
135                         "cancel": self.closePlugin,
136                         "back": self.closePlugin,
137                         "right": self.keyRight,
138                         "left": self.keyLeft,
139                         "up": self["chat"].pageUp,
140                         "down": self["chat"].pageDown,
141                         "buddyUp": self["buddy"].pageUp,
142                         "buddyDown": self["buddy"].pageDown,
143                         "home": self.keyHome,                
144                         "end": self.keyEnd,
145                         "delete": self.keyDelete,
146                         "deleteForward": self.keyDeleteForward,
147                         "deleteBackward": self.keyDeleteBackward,
148                         "tab": self.keyTab,
149                         "1": self.keyNumberGlobal,
150                         "2": self.keyNumberGlobal,
151                         "3": self.keyNumberGlobal,
152                         "4": self.keyNumberGlobal,
153                         "5": self.keyNumberGlobal,
154                         "6": self.keyNumberGlobal,
155                         "7": self.keyNumberGlobal,
156                         "8": self.keyNumberGlobal,
157                         "9": self.keyNumberGlobal,
158                         "0": self.keyNumberGlobal            
159                 }, -1)
160                 rcinput = eRCInput.getInstance()
161                 rcinput.setKeyboardMode(rcinput.kmAscii)
162
163         def gotAsciiCode(self):
164                 self["input"].handleAscii(getPrevAsciiCode())
165         
166         def keyUp(self):
167                 self["input"].up()
168         
169         def keyDown(self):
170                 self["input"].down()
171         
172         def keyLeft(self):
173                 self["input"].left()
174         
175         def keyRight(self):
176                 self["input"].right()
177         
178         def keyTab(self):
179                 self["input"].tab()
180         
181         def keyHome(self):
182                 self["input"].home()
183         
184         def keyEnd(self):
185                 self["input"].end()
186
187         def keyNumberGlobal(self, number):
188                 print "You pressed number " + str(number)
189                 self["input"].number(number)
190                 
191         def keyDelete(self):
192                 self["input"].delete()
193         
194         def keyDeleteForward(self):
195                 self["input"].delete()
196         
197         def keyDeleteBackward(self):
198                 self["input"].left()
199                 self["input"].delete()
200                 
201         def closePlugin(self):
202                 rcinput = eRCInput.getInstance()
203                 rcinput.setKeyboardMode(rcinput.kmNone)
204                 self.close(None)
205                 
206         def greenPressed(self):
207                 if self.checkStatus()==0:
208                         self.pipe.add("connecting... pls wait...")
209                         self.account = AccountManager(self.session)    #reload accounts :)
210                         self.account.startConnect()
211                         self["disconnect.desc"].show()
212                         self["red.pic"].show()
213
214         def redPressed(self):
215                 if self.checkStatus()==1:
216                         self.pipe.add("disconnecting... pls wait...")
217                         self.pipe.addOutText("/QUIT")
218                         try:
219                                 timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime(time.time()))
220                                 fp = file("/var/log/dreamIRC.log", 'r')
221                                 fp.close()
222                                 os.rename("/var/log/dreamIRC.log", "/var/log/dreamIRC_%s.log"%timestamp)
223                         except IOError:
224                                 print "--- nothing to remove---"
225                         self.pipe.clear()
226                         self.pipe.add(" -- not connected.. pls press green to connect!!\n")
227                         self.pipe.clearBuddyList()
228                         self.pipe.resetDesc()
229                         self["disconnect.desc"].hide()
230                         self["red.pic"].hide()
231
232         def checkStatus(self):
233                 status = self.account.getConnectionInfo()
234                 if status[0]==1:
235                         self["disconnect.desc"].show()
236                         self["red.pic"].show()
237                 elif status[0]==0:
238                         self["disconnect.desc"].hide()
239                         self["red.pic"].hide()
240                 return status[0]
241         
242         def bluePressed(self):
243                 self.session.openWithCallback(self.VirtualKeyBoardTextEntry, VirtualKeyBoard, title = (_("Enter your text here:")), text = "")
244
245                 
246         def yellowPressed(self):
247                 self.session.openWithCallback(self.resetKeyboard,dreamIRCSetupScreen)
248                 
249         def resetKeyboard(self):
250                 rcinput = eRCInput.getInstance()
251                 rcinput.setKeyboardMode(rcinput.kmAscii)
252                 
253
254         def go(self):
255                 print " TEXT = %s   - laenge = %d  !!!!" % (self["input"].getText(),len(self["input"].getText()))
256                 if (len(self["input"].getText()) >= 1):
257                         self.pipe.addOutText(self["input"].getText())
258                         self.clearInput()
259                         
260         def clearInput(self):
261                 self["input"].setText("")
262                         
263         def VirtualKeyBoardTextEntry(self, callback = None):
264                 if callback is not None and len(callback):
265                         print " TEXT = %s   - laenge = %d  !!!!" % (callback,len(callback))
266                         self.pipe.addOutText(callback)
267
268 def main(session, **kwargs):
269         session.open(dreamIRCMainMenu)
270
271 def Plugins(**kwargs):
272         return PluginDescriptor(
273                 name="dreamIRC",
274                 description="dreamIRC Client for Enigma2",
275                 icon="plugin.png",
276                 where=[ PluginDescriptor.WHERE_EXTENSIONSMENU, PluginDescriptor.WHERE_PLUGINMENU ],
277                 fnc=main)