New config (Ignore download times: yes/no)
[enigma2-plugins.git] / rsdownloader / src / RSListBrowser.py
1 ##\r
2 ## RS Downloader\r
3 ## by AliAbdul\r
4 ##\r
5 from Components.ActionMap import ActionMap\r
6 from Components.FileList import FileList
7 from Components.Label import Label
8 from Components.MenuList import MenuList\r
9 from RSTranslation import TitleScreen
10 from Screens.MessageBox import MessageBox
11 from Tools.Directories import fileExists
12
13 if fileExists("/usr/lib/enigma2/python/Screens/LTKeyBoard.pyc"):
14         from Screens.LTKeyBoard import LTKeyBoard
15         LT = True
16 else:
17         from Components.Input import Input
18         from Screens.InputBox import InputBox
19         LT = False\r
20 \r
21 ##############################################################################\r
22 \r
23 class RSListBrowser(TitleScreen):\r
24         skin = """\r
25                 <screen position="80,100" size="560,400" title="RS Downloader">\r
26                         <widget name="list" position="0,0" size="560,395" scrollbarMode="showOnDemand" />\r
27                 </screen>"""\r
28 \r
29         def __init__(self, session, path):\r
30                 TitleScreen.__init__(self, session)\r
31                 self["list"] = FileList(path, False, True, "^.*\.(txt)")\r
32                 self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.okClicked, "cancel": self.close}, -1)\r
33 \r
34         def okClicked(self):\r
35                 sel = self["list"].getFilename()
36                 if sel:
37                         dir = self["list"].getCurrentDirectory()
38                         self.session.open(TextEditor, "%s%s" % (dir, sel))\r
39 ##############################################################################
40
41 class TextEditor(TitleScreen):
42         skin = """\r
43                 <screen position="80,120" size="560,360" title="RS Downloader">\r
44                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />\r
45                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />\r
46                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />\r
47                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />\r
48                         <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />\r
49                         <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />\r
50                         <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />\r
51                         <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />\r
52                         <widget name="list" position="10,45" size="540,300" scrollbarMode="showOnDemand" />\r
53                 </screen>"""
54
55         def __init__(self, session, file):\r
56                 TitleScreen.__init__(self, session)
57                 
58                 self.file = file
59                 self.content = []
60                 
61                 self["key_red"] = Label(_("Clear"))
62                 self["key_green"] = Label(_("Save"))
63                 self["key_yellow"] = Label(_("Reload"))
64                 self["key_blue"] = Label(_("Delete"))\r
65                 self["list"] = MenuList(self.content)
66                 \r
67                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
68                         {
69                                 "red": self.clear,
70                                 "green": self.save,
71                                 "yellow": self.reload,
72                                 "blue": self.delete,
73                                 "ok": self.edit,
74                                 "cancel": self.close
75                         }, -1)
76                 
77                 self.reload()
78
79         def clear(self):
80                 while len(self.content):
81                         del self.content[0]
82                 self["list"].setList(self.content)
83
84         def save(self):
85                 content = ""
86                 for x in self.content:
87                         content = "%s%s\n" % (content, x)
88                 content = content.replace("\n\n", "\n")
89                 if content == "\n":
90                         content = ""
91                 try:
92                         f = open(self.file, "w")
93                         f.write(content)
94                         f.close()
95                         self.session.open(MessageBox, _("%s saved.") % self.file, MessageBox.TYPE_INFO)
96                 except:
97                         self.session.open(MessageBox, _("Error while writing %s!") % self.file, MessageBox.TYPE_ERROR)
98
99         def reload(self):
100                 self.clear()
101                 try:
102                         f = open(self.file, "r")
103                         content = f.read()
104                         f.close()
105                 except:
106                         content = ""
107                 if content.__contains__("\r"):
108                         content = content.split("\r")
109                 else:
110                         content = content.split("\n")
111                 for x in content:
112                         if (x != "\r") and (x != "\n") and (x != ""):
113                                 self.content.append(x)
114                 self["list"].setList(self.content)
115
116         def edit(self):
117                 if len(self.content) > 0:
118                         self.idx = self["list"].getSelectionIndex()
119                         text = self.content[self.idx]
120                         if LT:
121                                 self.session.openWithCallback(self.editCallback, LTKeyBoard, title=_("Edit:"), text=text)
122                         else:
123                                 self.session.openWithCallback(self.editCallback, InputBox, title=_("Edit:"), text=text, maxSize=False, type=Input.TEXT)
124
125         def editCallback(self, callback):
126                 if callback:
127                         del self.content[self.idx]
128                         self.content.append(callback)
129                         self["list"].setList(self.content)
130
131         def delete(self):
132                 if len(self.content) > 0:
133                         idx = self["list"].getSelectionIndex()
134                         del self.content[idx]
135                         self["list"].setList(self.content)
136