d39d7152fa92d52275845134febd5839e7d6e882
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / CrashReport / plugin.py
1 # twisted-mail twisted-names python-compression python-mime python-email
2 import os
3
4 from Plugins.Plugin import PluginDescriptor
5
6 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigText, ConfigSelection, ConfigYesNo,ConfigText
7 from Components.ConfigList import ConfigListScreen
8 from Components.ActionMap import ActionMap
9 from Components.Sources.StaticText import StaticText
10 from Components.Pixmap import Pixmap
11 from Components.Label import Label
12
13 from Screens.Screen import Screen
14 from Screens.VirtualKeyBoard import VirtualKeyBoard
15 from Screens.ChoiceBox import ChoiceBox
16 from Screens.MessageBox import MessageBox
17
18 from enigma import ePoint, eConsoleAppContainer
19
20 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
21
22 g_configfile=resolveFilename(SCOPE_PLUGINS, "SystemPlugins/CrashReport/settings")
23 g_senderfile=resolveFilename(SCOPE_PLUGINS, "SystemPlugins/CrashReport/sender.py")
24
25 g_default_activation   = ""
26 g_default_aftersummit  = ""
27 g_default_optionalinfo = False
28 g_default_username     = ""
29 g_default_useremail    = ""
30 g_default_machineinfo  = True
31
32 def getValue(str):
33         idx = str.find("=")
34         #print "---->>[%s][%d][%d]" % (str, len(str), idx)
35         if idx == len(str):
36                 return ""
37         elif idx == -1:
38                 return str
39         return str[idx+1:]
40
41 def saveConfig(activation, aftersummit, machineinfo, optionalinfo, username="", useremail=""):
42         global g_configfile
43         configs = []
44         configs.append("activation=%s\n"   % (activation))
45         configs.append("aftersummit=%s\n"  % (aftersummit))
46         configs.append("optionalinfo=%s\n" % (str(optionalinfo)))
47         configs.append("username=%s\n"  % (username))
48         configs.append("useremail=%s\n" % (useremail))
49         configs.append("machineinfo=%s\n" % (str(machineinfo)))
50
51         f = open(g_configfile, 'w')
52         f.writelines(configs)
53         f.close()
54         loadConfig()
55
56 def loadConfig():
57         global g_configfile
58         if os.path.exists(g_configfile) == False:
59                 return  
60         global g_default_activation
61         global g_default_aftersummit
62         global g_default_optionalinfo
63         global g_default_username
64         global g_default_useremail
65         global g_default_machineinfo
66         f = open(g_configfile)
67         conf_list = f.readlines()
68         f.close()
69         print "load config : ", conf_list
70         if len(conf_list) < 6:
71                 return
72         g_default_activation  = getValue(conf_list[0].strip())
73         g_default_aftersummit = getValue(conf_list[1].strip())
74         if getValue(conf_list[2].strip()) == "True":
75                 g_default_optionalinfo = True
76         g_default_username    = getValue(conf_list[3].strip())
77         g_default_useremail   = getValue(conf_list[4].strip())
78         if getValue(conf_list[5].strip()) == "False":
79                 g_default_machineinfo = False
80
81 class CrashlogReportConfiguration(Screen, ConfigListScreen):
82         skin =  """
83                 <screen name="CrashlogReportSetting" position="209,48" size="865,623" title="CrashlogReport Setting" flags="wfNoBorder" backgroundColor="transparent">  
84                         <ePixmap pixmap="Vu_HD/Bg_EPG_list.png" zPosition="-1" position="0,0" size="865,623" alphatest="on" />
85                         <ePixmap pixmap="Vu_HD/menu/ico_title_Setup.png" position="32,41" size="40,40" alphatest="blend"  transparent="1" />
86                         <eLabel text="CrashlogReport Setting" position="90,50" size="600,32" font="Semiboldit;32" foregroundColor="#5d5d5d" backgroundColor="#27b5b9bd" transparent="1" />
87                         <ePixmap pixmap="Vu_HD/icons/clock.png" position="750,55" zPosition="1" size="20,20" alphatest="blend" />
88                         <widget source="global.CurrentTime" render="Label" position="770,57" zPosition="1" size="50,20" font="Regular;20" foregroundColor="#1c1c1c" halign="right" backgroundColor="#27d9dee2" transparent="1">
89                                 <convert type="ClockToText">Format:%H:%M</convert>
90                         </widget>
91                         <ePixmap pixmap="Vu_HD/buttons/red.png" position="45,98" size="25,25" alphatest="blend" />
92                         <ePixmap pixmap="Vu_HD/buttons/green.png" position="240,98" size="25,25" alphatest="blend" />
93                         <ePixmap pixmap="Vu_HD/buttons/button_off.png" position="435,98" size="25,25" alphatest="blend" />
94                         <ePixmap pixmap="Vu_HD/buttons/button_off.png" position="630,98" size="25,25" alphatest="blend" />
95                         <widget source="key_red" render="Label" position="66,97" zPosition="1" size="150,25" font="Regular;20" halign="center" valign="center" backgroundColor="darkgrey" foregroundColor="#1c1c1c" transparent="1" />
96                         <widget source="key_green" render="Label" position="268,97" zPosition="1" size="150,25" font="Regular;20" halign="center" valign="center" backgroundColor="darkgrey" foregroundColor="#1c1c1c" transparent="1" />
97                         <widget name="config" zPosition="2" position="50,130" itemHeight="36" size="750,324" scrollbarMode="showOnDemand" transparent="1" />
98                         <widget source="status" render="Label" position="160,525" size="540,60" zPosition="10" foregroundColor="#3c3c3c" backgroundColor="#27aeaeae" font="Regular;20" halign="center" valign="center" transparent="1"/>
99                         <widget name="VKeyIcon" pixmap="Vu_HD/buttons/key_text.png" position="500,350" zPosition="10" size="35,25" transparent="1" alphatest="on" />
100                         <widget name="HelpWindow" pixmap="Vu_HD/vkey_icon.png" position="310,400" zPosition="1" size="1,1" transparent="1" alphatest="on" />
101                 </screen>
102                 """
103         def __init__(self, session):
104                 Screen.__init__(self, session)
105                 self.session = session
106
107                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
108                 {
109                         "ok":     self.keyOK,
110                         "cancel": self.keyCancel,
111                         "red":    self.keyCancel,
112                         "green":  self.keyOK,
113                 }, -2)
114                 self["VirtualKB"] = ActionMap(["VirtualKeyboardActions" ],
115                 {
116                         "showVirtualKeyboard": self.cbSetTitle,
117                 }, -1)
118
119                 self.list = []
120                 ConfigListScreen.__init__(self, self.list, session=self.session)
121                 self["key_red"] = StaticText(_("Close"))
122                 self["key_green"] = StaticText(_("Save"))
123                 self["status"] = StaticText(" ")
124                 self["VKeyIcon"] = Pixmap()
125                 self["HelpWindow"] = Pixmap()
126
127                 self["VKeyIcon"].hide()
128                 self["VirtualKB"].setEnabled(False)
129
130                 self.initGlobal()
131                 self.setupUI()
132
133         def initGlobal(self):
134                 global g_default_activation
135                 global g_default_aftersummit
136                 global g_default_optionalinfo
137                 global g_default_username
138                 global g_default_useremail
139                 global g_default_machineinfo
140                 if g_default_activation == "":
141                         g_default_activation = "send_summit"
142                 if g_default_aftersummit == "":
143                         g_default_aftersummit = "rename"
144                 g_default_optionalinfo
145                 if g_default_username == "":
146                         g_default_username = "Vuplus User"
147                 if g_default_useremail == "":
148                         g_default_useremail = "yourmail@here.is"
149                 self.g_activation   = ConfigSelection(default=g_default_activation,  choices=[("send_summit", _("Enable")), ("send_disable", _("Disable"))])
150                 self.g_aftersummit  = ConfigSelection(default=g_default_aftersummit, choices=[("rename", _("Rename")), ("delete", _("Delete"))])
151                 self.g_optionalinfo = ConfigYesNo(default = g_default_optionalinfo)
152                 self.g_username     = ConfigText(default = g_default_username,  fixed_size = False)
153                 self.g_useremail    = ConfigText(default = g_default_useremail, fixed_size = False)
154                 self.g_machineinfo  = ConfigYesNo(default = g_default_machineinfo)
155
156                 self._activation     = getConfigListEntry(_("Activation mode"), self.g_activation)
157                 self._after_action   = getConfigListEntry(_("Action after summit"),      self.g_aftersummit)
158                 self._add_user_info  = getConfigListEntry(_("Include optional infomation"), self.g_optionalinfo)
159                 self._user_info_name = getConfigListEntry(_("User name"),  self.g_username)
160                 self._user_info_mail = getConfigListEntry(_("User Email"), self.g_useremail)
161                 self._machineinfo    = getConfigListEntry(_("Include settop information"), self.g_machineinfo)
162
163                 if not self.cbSelectionChanged in self["config"].onSelectionChanged:
164                         self["config"].onSelectionChanged.append(self.cbSelectionChanged)
165
166         def setupUI(self):
167                 self.list = []
168                 self.list.append(self._activation)
169                 if self.g_activation.value == "send_summit":
170                         self.list.append(self._after_action)
171                         self.list.append(self._add_user_info)
172                         if self.g_optionalinfo.value:
173                                 self.list.append(self._user_info_name)
174                                 self.list.append(self._user_info_mail)
175                 self.list.append(self._machineinfo)
176                 self["config"].list = self.list
177                 self["config"].l.setList(self.list)
178
179         def resetUI(self):
180                 if self["config"].getCurrent() == self._activation or self["config"].getCurrent() == self._add_user_info:
181                         self.setupUI()
182
183         def cbSelectionChanged(self):
184                 current = self["config"].getCurrent()
185                 if current == self._activation:
186                         self.enableVKIcon(False)
187                 elif current == self._after_action:
188                         self.enableVKIcon(False)
189                 elif current == self._add_user_info:
190                         self.enableVKIcon(False)
191                 elif current == self._user_info_name:
192                         self.enableVKIcon(True)
193                 elif current == self._user_info_mail:
194                         self.enableVKIcon(True)
195                 elif current == self._machineinfo:
196                         self.enableVKIcon(False)
197
198         def cbSetTitle(self):
199                 t = " "
200                 if self["config"].getCurrent() == self._user_info_mail:
201                         t = "Please enter user email:"
202                 if self["config"].getCurrent() == self._user_info_name:
203                         t = "Please enter user name:"
204                 self.session.openWithCallback(self.NameCallback, VirtualKeyBoard, title = (t), text = self._user_info_name.value)
205
206         def enableVKIcon(self, mode):
207                 if mode:
208                         self["VKeyIcon"].show()
209                 else:
210                         self["VKeyIcon"].hide()
211                 self["VirtualKB"].setEnabled(True)
212
213         def saveConfig(self):
214                 if self.g_optionalinfo.value:
215                         saveConfig(self.g_activation.value, self.g_aftersummit.value, self.g_machineinfo.value, self.g_optionalinfo.value, self.g_username.value, self.g_useremail.value)
216                 else:
217                         saveConfig(self.g_activation.value, self.g_aftersummit.value, self.g_machineinfo.value, self.g_optionalinfo.value)
218
219         def keyLeft(self):
220                 ConfigListScreen.keyLeft(self)
221                 self.resetUI()
222
223         def keyRight(self):
224                 ConfigListScreen.keyRight(self)
225                 self.resetUI()
226
227         def keyOK(self):
228                 self.saveConfig()
229                 self.close()
230
231         def keyCancel(self):
232                 global g_configfile
233                 if os.path.exists(g_configfile) == False:
234                         self.saveConfig()
235                 self.close()
236
237 def main(session, **kwargs):
238         loadConfig()
239         session.open(CrashlogReportConfiguration)
240
241 def opensetting(menuid, **kwargs):
242         if menuid != "system":
243                 return []
244         return [(_("Crashlog Reporting"), main, "crashlog_configure", 70)]
245
246 def generateInformation():
247         from os import popen
248         from Tools.DreamboxHardware import getFPVersion
249         from Components.Harddisk import harddiskmanager
250         from Components.NimManager import nimmanager
251         from Components.About import about
252         def command(cmd):
253                 try:
254                         result = popen(cmd, "r").read().strip()
255                         return str(result)
256                 except:
257                         pass
258         information = [
259                  "kernel         : %s\n"%(command("uname -a"))
260                 ,"version        : %s (%s)\n"%(str(about.getImageVersionString()), str(about.getEnigmaVersionString()))
261                 ,"frontprocessor : %s\n"%(str(getFPVersion()))
262                 ,"frontend       : %s\n"%(str(nimmanager.nimList()))
263                 ,"hdd info       : %s\n"%(str(harddiskmanager.HDDList()))
264                 ,"network information : \n%s\n"%(command("ifconfig -a"))
265                 ]
266         f = open("/tmp/machine.info", 'w')
267         f.writelines(information)
268         f.close()
269
270 sender = None
271 def autosubmit(reason, **kwargs):
272         global g_default_activation
273         global g_default_aftersummit
274         global g_default_username
275         global g_default_useremail
276
277         print "[CrashReport] auto submit"
278         loadConfig()
279         import os
280         def isExistCrashlog(d='/media/hdd'):
281                 try:
282                         for f in os.listdir(d):
283                                 if f.startswith("enigma2_crash_") and f.endswith(".log"):
284                                         return True
285                 except:
286                         pass
287                 return False
288
289         def cbDataAvail(ret_data):
290                 print ret_data
291         def cbAppClosed(retval):
292                 if os.path.exists("/tmp/machine.info"):
293                         os.system("rm -f /tmp/machine.info")
294
295         if "session" in kwargs:
296                 if isExistCrashlog() == False:
297                         print "[CrashReport] no crash-log"
298                         return
299                 session = kwargs["session"]
300                 if g_default_activation == "send_summit":
301                         global sender
302                         global g_senderfile
303                         sender = eConsoleAppContainer()
304                         sender.dataAvail.append(cbDataAvail)
305                         sender.appClosed.append(cbAppClosed)
306
307                         if g_default_username == "":
308                                 un = "Vuplus User"
309                         else:
310                                 un = g_default_username
311                         if g_default_useremail == "":
312                                 um = "yourmail@here.is"
313                         else:
314                                 um = g_default_useremail
315                         if g_default_machineinfo:
316                                 generateInformation()
317
318                         sender.execute(_("python %s %s %s %s" % (g_senderfile, um, un.replace(" ", "_"), g_default_aftersummit)))
319
320 def Plugins(**kwargs):
321         return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], needsRestart = False, fnc = autosubmit),
322                 PluginDescriptor(name=_("CrashlogReportSetting"), description=_("CrashlogReport setting"),where=PluginDescriptor.WHERE_MENU, needsRestart = False, fnc=opensetting)]
323