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