Support turbo2.
[vuplus_dvbapp] / RecordTimer.py
index 1cb7eb3..4803ef6 100755 (executable)
@@ -4,6 +4,7 @@ from enigma import eEPGCache, getBestPlayableServiceReference, \
 from Components.config import config
 from Components.UsageConfig import defaultMoviePath
 from Components.TimerSanityCheck import TimerSanityCheck
+from Components.SystemInfo import SystemInfo
 
 from Screens.MessageBox import MessageBox
 import Screens.Standby
@@ -18,6 +19,8 @@ from ServiceReference import ServiceReference
 from time import localtime, strftime, ctime, time
 from bisect import insort
 
+import os
+
 # ok, for descriptions etc we have:
 # service reference  (to get the service name)
 # name               (title)
@@ -47,6 +50,25 @@ class AFTEREVENT:
        DEEPSTANDBY = 2
        AUTO = 3
 
+def findSafeRecordPath(dirname):
+       if not dirname:
+               return None
+
+       from Components import Harddisk
+       dirname = os.path.realpath(dirname)
+       mountpoint = Harddisk.findMountPoint(dirname)
+       if mountpoint in ('/', '/media'):
+               print '[RecordTimer] media is not mounted:', dirname
+               return None
+       if not os.path.isdir(dirname):
+               try:
+                       os.makedirs(dirname)
+               except Exception, ex:
+                       print '[RecordTimer] Failed to create dir "%s":' % dirname, ex
+                       return None
+
+       return dirname
+
 # please do not translate log messages
 class RecordTimerEntry(timer.TimerEntry, object):
 ######### the following static methods and members are only in use when the box is in (soft) standby
@@ -90,7 +112,7 @@ class RecordTimerEntry(timer.TimerEntry, object):
                        Notifications.AddNotification(Screens.Standby.TryQuitMainloop, 1, onSessionOpenCallback=RecordTimerEntry.stopTryQuitMainloop, default_yes = default_yes)
 #################################################################
 
-       def __init__(self, serviceref, begin, end, name, description, eit, disabled = False, justplay = False, afterEvent = AFTEREVENT.AUTO, checkOldTimers = False, dirname = None, tags = None):
+       def __init__(self, serviceref, begin, end, name, description, eit, disabled = False, justplay = False, afterEvent = AFTEREVENT.AUTO, checkOldTimers = False, dirname = None, tags = None, descramble = True, record_ecm = False, filename = None):
                timer.TimerEntry.__init__(self, int(begin), int(end))
 
                if checkOldTimers == True:
@@ -114,7 +136,10 @@ class RecordTimerEntry(timer.TimerEntry, object):
                self.timer = None
                self.__record_service = None
                self.start_prepare = 0
-               self.justplay = justplay
+               if SystemInfo["PVRSupport"]:
+                       self.justplay = justplay
+               else:
+                       self.justplay = True
                self.afterEvent = afterEvent
                self.dirname = dirname
                self.dirnameHadToFallback = False
@@ -122,14 +147,24 @@ class RecordTimerEntry(timer.TimerEntry, object):
                self.autoincreasetime = 3600 * 24 # 1 day
                self.tags = tags or []
 
+               self.descramble = descramble
+               self.record_ecm = record_ecm
+
                self.log_entries = []
                self.resetState()
-       
+
+               self.Filename = filename
+               self.pvrConvert = False
+
        def log(self, code, msg):
                self.log_entries.append((int(time()), code, msg))
                print "[TIMER]", msg
 
        def calculateFilename(self):
+               if self.pvrConvert and self.Filename:
+                       self.log(0, "Filename calculated as: '%s'" % self.Filename)
+                       return self.Filename
+
                service_name = self.service_ref.getServiceName()
                begin_date = strftime("%Y%m%d %H%M", localtime(self.begin))
                begin_shortdate = strftime("%Y%m%d", localtime(self.begin))
@@ -154,21 +189,31 @@ class RecordTimerEntry(timer.TimerEntry, object):
                if config.recording.ascii_filenames.value:
                        filename = ASCIItranslit.legacyEncode(filename)
 
-               if not self.dirname or not Directories.fileExists(self.dirname, 'w'):
-                       if self.dirname:
-                               self.dirnameHadToFallback = True
-                       dirname = defaultMoviePath()
+               if not self.dirname:
+                       dirname = findSafeRecordPath(defaultMoviePath())
                else:
-                       dirname = self.dirname
+                       dirname = findSafeRecordPath(self.dirname)
+                       if dirname is None:
+                               dirname = findSafeRecordPath(defaultMoviePath())
+                               self.dirnameHadToFallback = True
+
+               if not dirname:
+                       return None
+
                self.Filename = Directories.getRecordingFilename(filename, dirname)
                self.log(0, "Filename calculated as: '%s'" % self.Filename)
                #begin_date + " - " + service_name + description)
+               return self.Filename
 
        def tryPrepare(self):
                if self.justplay:
                        return True
                else:
-                       self.calculateFilename()
+                       if not self.calculateFilename():
+                               self.do_backoff()
+                               self.start_prepare = time() + self.backoff
+                               return False
+
                        rec_ref = self.service_ref and self.service_ref.ref
                        if rec_ref and rec_ref.flags & eServiceReference.isGroup:
                                rec_ref = getBestPlayableServiceReference(rec_ref, eServiceReference())
@@ -196,7 +241,7 @@ class RecordTimerEntry(timer.TimerEntry, object):
                                if event_id is None:
                                        event_id = -1
 
-                       prep_res=self.record_service.prepare(self.Filename + ".ts", self.begin, self.end, event_id, self.name.replace("\n", ""), self.description.replace("\n", ""), ' '.join(self.tags))
+                       prep_res=self.record_service.prepare(self.Filename + ".ts", self.begin, self.end, event_id, self.name.replace("\n", ""), self.description.replace("\n", ""), ' '.join(self.tags), bool(self.descramble), bool(self.record_ecm))
                        if prep_res:
                                if prep_res == -255:
                                        self.log(4, "failed to write meta information")
@@ -297,13 +342,13 @@ class RecordTimerEntry(timer.TimerEntry, object):
                                self.record_service = None
                        if self.afterEvent == AFTEREVENT.STANDBY:
                                if not Screens.Standby.inStandby: # not already in standby
-                                       Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A finished record timer wants to set your\nDreambox to standby. Do that now?"), timeout = 20)
+                                       Notifications.AddNotificationWithCallback(self.sendStandbyNotification, MessageBox, _("A finished record timer wants to set your\nSTB to standby. Do that now?"), timeout = 20)
                        elif self.afterEvent == AFTEREVENT.DEEPSTANDBY:
                                if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
                                        if Screens.Standby.inStandby: # in standby
                                                RecordTimerEntry.TryQuitMainloop() # start shutdown handling without screen
                                        else:
-                                               Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A finished record timer wants to shut down\nyour Dreambox. Shutdown now?"), timeout = 20)
+                                               Notifications.AddNotificationWithCallback(self.sendTryQuitMainloopNotification, MessageBox, _("A finished record timer wants to shut down\nyour STB. Shutdown now?"), timeout = 20)
                        return True
 
        def setAutoincreaseEnd(self, entry = None):
@@ -319,16 +364,14 @@ class RecordTimerEntry(timer.TimerEntry, object):
                timersanitycheck = TimerSanityCheck(NavigationInstance.instance.RecordTimer.timer_list, dummyentry)
                if not timersanitycheck.check():
                        simulTimerList = timersanitycheck.getSimulTimerList()
-                       new_end = simulTimerList[1].begin
-                       del simulTimerList
-                       new_end -= 30                           # 30 Sekunden Prepare-Zeit lassen
-               del dummyentry
+                       if simulTimerList is not None and len(simulTimerList) > 1:
+                               new_end = simulTimerList[1].begin
+                               new_end -= 30                           # 30 Sekunden Prepare-Zeit lassen
                if new_end <= time():
                        return False
                self.end = new_end
                return True
-       
-       
+
        def sendStandbyNotification(self, answer):
                if answer:
                        Notifications.AddNotification(Screens.Standby.Standby)
@@ -378,6 +421,9 @@ class RecordTimerEntry(timer.TimerEntry, object):
                        # that in our state, with also keeping the possibility to re-try.
                        # TODO: this has to be done.
                elif event == iRecordableService.evStart:
+                       if self.pvrConvert:
+                               return
+
                        text = _("A record has been started:\n%s") % self.name
                        if self.dirnameHadToFallback:
                                text = '\n'.join((text, _("Please note that the previously selected media could not be accessed and therefore the default directory is being used instead.")))
@@ -399,6 +445,18 @@ class RecordTimerEntry(timer.TimerEntry, object):
 
        record_service = property(lambda self: self.__record_service, setRecordService)
 
+       def isUsbRecordingPath(self):
+               dirname = None
+
+               if self.dirname:
+                       dirname = findSafeRecordPath(self.dirname)
+
+               if dirname is None:
+                       dirname = findSafeRecordPath(defaultMoviePath())
+
+               from Components import Harddisk
+               return Harddisk.isUsbStorage(dirname)
+
 def createTimer(xml):
        begin = int(xml.get("begin"))
        end = int(xml.get("end"))
@@ -406,7 +464,10 @@ def createTimer(xml):
        description = xml.get("description").encode("utf-8")
        repeated = xml.get("repeated").encode("utf-8")
        disabled = long(xml.get("disabled") or "0")
-       justplay = long(xml.get("justplay") or "0")
+       if SystemInfo["PVRSupport"]:
+               justplay = long(xml.get("justplay") or "0")
+       else:
+               justplay = long("1")
        afterevent = str(xml.get("afterevent") or "nothing")
        afterevent = {
                "nothing": AFTEREVENT.NONE,
@@ -455,6 +516,18 @@ class RecordTimer(timer.Timer):
                        print "unable to load timers from file!"
 
        def doActivate(self, w):
+               if w.state == RecordTimerEntry.StateWaiting:
+                       from Components.SystemInfo import SystemInfo
+                       if SystemInfo.get("DisableUsbRecord", True) and w.isUsbRecordingPath():
+                               service_name = w.service_ref.getServiceName()
+                               self.timer_list.remove(w)
+                               if w.dontSave is False:
+                                       w.resetState()
+                                       w.disable()
+                                       self.addTimerEntry(w)
+                               Notifications.AddNotification(MessageBox, _("Can not recording on a USB storage.\nService name : %s"% service_name), MessageBox.TYPE_ERROR)
+                               return
+
                # when activating a timer which has already passed,
                # simply abort the timer. don't run trough all the stages.
                if w.shouldSkip():
@@ -592,7 +665,10 @@ class RecordTimer(timer.Timer):
                        if timer.tags is not None:
                                list.append(' tags="' + str(stringToXML(' '.join(timer.tags))) + '"')
                        list.append(' disabled="' + str(int(timer.disabled)) + '"')
-                       list.append(' justplay="' + str(int(timer.justplay)) + '"')
+                       if SystemInfo["PVRSupport"]:
+                               list.append(' justplay="' + str(int(timer.justplay)) + '"')
+                       else:
+                               list.append(' justplay="1"')
                        list.append('>\n')
                        
                        if config.recording.debug.value: