import cleanup
[enigma2-plugins.git] / antiscrollbar / src / plugin.py
1 # (c) 2006 3c5x9, dream@3c5x9.de
2 # This Software is Free, use it where you want, when you want for whatever you want and modify it if you want. but don't remove my copyright!
3
4 from Screens.Screen import Screen
5 from Screens.InputBox import InputBox
6 from Screens.ChoiceBox import ChoiceBox
7 from Components.ActionMap import ActionMap
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.Input import Input
11 from Plugins.Plugin import PluginDescriptor
12 #############
13 from enigma import ePoint, eSize
14 #############
15 from ConfigParser import ConfigParser, DEFAULTSECT, DuplicateSectionError
16
17 ###############################################################################        
18 myname = "AntiScrollbar"     
19 myversion = "0.1"
20 ###############################################################################        
21 class AntiScrollMain(Screen):
22     step = 5    
23     def __init__(self, session, args = 0):
24         config = AntiScrollConfig()
25         try:
26           profil = config.getLastProfile()
27           self.size = [profil["sizex"],profil["sizey"]]
28           self.position = [profil["posx"],profil["posy"]]             
29         except Exception:
30           config.setProfile("standard",[200,200],[200,200])
31           config = AntiScrollConfig()          
32           profil = config.getProfile("standard")
33           
34         self.size = [profil["sizex"],profil["sizey"]]
35         self.position = [profil["posx"],profil["posy"]]           
36         ss  ="<screen position=\"%i,%i\" size=\"%i,%i\" title=\"%s\"  flags=\"wfNoBorder\" >" %(profil["posx"],profil["posy"],profil["sizex"],profil["sizey"],myname)
37         ss +="<widget name=\"label\" position=\"0,0\" size=\"%i,%i\"  backgroundColor=\"black\"  />" %(profil["sizex"],profil["sizey"])
38         ss +="</screen>"
39         self.skin = ss
40         self.session = session
41         Screen.__init__(self, session)
42         self.menu = args
43         self["label"] = Label()
44         self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","NumberActions"], 
45             {
46              "ok":      self.go,
47              "back":    self.close,
48              "menu":    self.openmenu,
49              "down":    self.down,
50              "up":      self.up,
51              "left":    self.left,
52              "right":   self.right,
53              "2":       self.key2,
54              "8":       self.key8,
55              "4":       self.key4,
56              "6":       self.key6,
57              }, -1)
58
59
60     def go(self):
61       pass
62     def openmenu(self):
63       self.session.open(AntiScrollMenu,callback=self.menuCallback,size=self.size,position=self.position)
64     def menuCallback(self,size,position):
65       self.size = size
66       self.position = position
67       self.move(self.position[0],self.position[1])
68       self.resize(self.size[0],self.size[1])
69        
70     def key2(self):
71       self.size= [self.size[0],self.size[1]-self.step]
72       self.resize(self.size[0],self.size[1])
73     def key8(self):
74       self.size= [self.size[0],self.size[1]+self.step]
75       self.resize(self.size[0],self.size[1])
76     def key4(self):
77       self.size= [self.size[0]-self.step,self.size[1]]
78       self.resize(self.size[0],self.size[1])
79     def key6(self):
80       self.size= [self.size[0]+self.step,self.size[1]]
81       self.resize(self.size[0],self.size[1])
82     
83     def down(self):
84       self.position = [self.position[0],self.position[1]+self.step]
85       self.move(self.position[0],self.position[1])
86     def up(self):
87       self.position = [self.position[0],self.position[1]-self.step]
88       self.move(self.position[0],self.position[1])
89     def left(self):
90       self.position = [self.position[0]-self.step,self.position[1]]
91       self.move(self.position[0],self.position[1])
92     def right(self):
93       self.position = [self.position[0]+self.step,self.position[1]]
94       self.move(self.position[0],self.position[1])
95     
96     def move(self, x, y):
97       print "["+myname+"] moving to", str(x) + ":" + str(y)
98       self.instance.move(ePoint(x, y))
99           
100     def resize(self, w, h):
101       print "["+myname+"] resizing to", str(w) + "x" + str(h)
102       self.instance.resize(eSize(*(w, h)))
103       self["label"].instance.resize(eSize(*(w, h)))
104 #############################
105 class  AntiScrollMenu(Screen):
106   def __init__(self,session,callback=None,size=None,position=None,arg=0):
107     self.session = session
108     self.callBack = callback
109     self.size= size
110     self.position = position
111     ss  ="<screen position=\"200,200\" size=\"300,200\" title=\"%s Menu\" >" % myname
112     ss +="<widget name=\"menu\" position=\"0,0\" size=\"300,150\" scrollbarMode=\"showOnDemand\" />" 
113     ss +="<widget name=\"label\" position=\"0,150\" size=\"300,50\"  font=\"Regular;18\" valign=\"center\" halign=\"center\" />"
114     ss +="</screen>"
115     self.skin = ss
116     Screen.__init__(self,session)
117     list = []
118     list.append(("load Profile",self.load))
119     list.append(("save Profile",self.save))
120     list.append(("save new Profile",self.savenew))
121     self["menu"] = MenuList(list)
122     self["label"] = Label("written by 3c5x9, V"+myversion)
123     self["actions"] = ActionMap(["WizardActions", "DirectionActions","MenuActions","NumberActions"],
124             {
125              "ok": self.go,
126              "back": self.close,
127              }, -1)
128   def go(self):
129     selection = self["menu"].getCurrent()
130     selection[1]()
131   
132   def load(self):
133     config = AntiScrollConfig()
134     liste = []
135     for i in config.getProfiles():
136       liste.append((i,i))
137     self.session.openWithCallback(self.loadProfile,ChoiceBox,_("select Profile to load"),liste)
138     
139   def loadProfile(self,value):
140     if value is not None:
141       config = AntiScrollConfig()
142       profil = config.getProfile(value[1])
143       if profil is not False:
144         self.callBack([profil["sizex"],profil["sizey"]],[profil["posx"],profil["posy"]])
145   def savenew(self):
146     self.session.openWithCallback(self.profilnameEntered,InputBox, title=_("Please enter a name for the Profile"), text="Profil", maxSize=False, type=Input.TEXT)
147     
148   def profilnameEntered(self,value):
149     if value is not None:
150       config = AntiScrollConfig()
151       config.setProfile(value,self.size,self.position)   
152   def save(self):
153     config = AntiScrollConfig()
154     liste = []
155     for i in config.getProfiles():
156       liste.append((i,i))
157     self.session.openWithCallback(self.saveProfile,ChoiceBox,_("select Profile to save"),liste)
158   def saveProfile(self,value):
159     if value is not None:
160       config = AntiScrollConfig()
161       config.setProfile(value[1],self.size,self.position)
162                        
163                           
164 ##############################
165 class AntiScrollConfig:
166     configfile = "/etc/enigma2/AntiScrollbar.conf"
167     
168     def __init__(self):
169         self.configparser = ConfigParser()
170         self.configparser.read(self.configfile)
171     def setLastProfile(self,name):
172         self.configparser.set(DEFAULTSECT,"lastprofile",name)
173         self.writeConfig()
174     def getLastProfile(self):
175         last = self.configparser.get(DEFAULTSECT,"lastprofile")    
176         return self.getProfile(last)
177     def getProfiles(self):
178         profiles=[]
179         sections = self.configparser.sections()
180         for section in sections:
181           profiles.append(section)
182         return profiles
183
184     def getProfile(self,name):
185       if self.configparser.has_section(name) is True:
186         print "loading profile ",name
187         l={}
188         l["sizex"] = int(self.configparser.get(name, "size_x"))
189         l["sizey"] = int(self.configparser.get(name, "size_y"))
190         l["posx"] = int(self.configparser.get(name, "position_x"))
191         l["posy"] = int(self.configparser.get(name, "position_y"))
192         self.setLastProfile(name)
193         return l
194       else:
195         print "couldnt find profil", name
196         return False
197     def setProfile(self,name,size,position):
198         try:
199           self.configparser.add_section(name)
200           self.configparser.set(name, "size_x",size[0])
201           self.configparser.set(name, "size_y",size[1])
202           self.configparser.set(name, "position_x",position[0])
203           self.configparser.set(name, "position_y",position[1])
204           self.writeConfig()
205           return True
206         except DuplicateSectionError:
207           self.deleteProfile(name)
208           self.setProfile(name,size,position)
209                                                                                                     
210     def deleteProfile(self,name):
211         self.configparser.remove_section(name)
212         self.writeConfig()
213
214     def writeConfig(self):
215         fp = open(self.configfile,"w")
216         self.configparser.write(fp)
217         fp.close()
218                             
219     
220      
221 #############################
222 def main(session, **kwargs):
223   session.open(AntiScrollMain)
224 def Plugins(**kwargs):
225   return [PluginDescriptor(name=myname,description="overlay for scrolling bars",where = PluginDescriptor.WHERE_PLUGINMENU,fnc = main),
226           PluginDescriptor(name=myname,description="overlay for scrolling bars",where = PluginDescriptor.WHERE_EXTENSIONSMENU,fnc = main)]