[MerlinMusicPlayer] fixed google cover display
[enigma2-plugins.git] / epgbackup / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # Config
5 from Components.config import config, ConfigYesNo, ConfigNumber, ConfigSelection, \
6         ConfigSubsection, ConfigSelectionNumber, ConfigDirectory, NoSave
7 from Screens.MessageBox import MessageBox
8 from Screens.Standby import TryQuitMainloop
9 from Tools.BoundFunction import boundFunction
10
11 # Error-print
12 from EPGBackupTools import debugOut, PLUGIN_VERSION
13 from traceback import format_exc
14
15 extPrefix = _("EXTENSIONMENU_PREFIX")
16
17 config.plugins.epgbackup = ConfigSubsection()
18 # Do not change order of choices
19 config.plugins.epgbackup.show_setup_in = ConfigSelection(choices=[
20                 ("extension", _("extensions")),
21                 ("plugin", _("pluginmenue")),
22                 ("both", _("extensions") + "/" + _("pluginmenue")),
23                 ("system", _("systemmenue")),
24         ], default = "both")
25 config.plugins.epgbackup.show_make_backup_in_extmenu = ConfigYesNo(default = False)
26 config.plugins.epgbackup.show_backuprestore_in_extmenu = ConfigYesNo(default = False)
27 config.plugins.epgbackup.backup_enabled = ConfigYesNo(default = True)
28 config.plugins.epgbackup.make_backup_after_unsuccess_restore = ConfigYesNo(default = True)
29 config.plugins.epgbackup.callAfterEPGRefresh = ConfigYesNo(default = True)
30 config.plugins.epgbackup.backupSaveInterval = ConfigSelection(choices = [
31         ("-1",_("backup timer disabled")),
32         ("30",_("30 minutes")),
33         ("60",_("1 hour")),
34         ("300",_("6 hours")),
35         ("1200",_("1 day")),
36     ], default = "-1")
37 config.plugins.epgbackup.show_messages_background = ConfigYesNo(default = True)
38 config.plugins.epgbackup.filesize_valid = ConfigSelectionNumber(min = 1, 
39         max = 20, stepwidth = 1, default = 3, wraparound = True)
40 config.plugins.epgbackup.timespan_valid = ConfigNumber(default=7)
41 config.plugins.epgbackup.showadvancedoptions = NoSave(ConfigYesNo(default = False))
42 config.plugins.epgbackup.epgwrite_wait = ConfigNumber(default=3)
43 config.plugins.epgbackup.showin_usr_scripts = ConfigYesNo(default = True)
44 config.plugins.epgbackup.backup_strategy = ConfigSelection(choices = [
45                 ("youngest_before_biggest", _("Youngest before Biggest"), _("The youngest file from the saved backup-files will be restored.\nIf it is older than the current existing EPG-file and the EPG-file isn't valid then the biggest backup-file will be restored.")),
46                 ("biggest_before_youngest", _("Biggest before Youngest"), _("The biggest file from the saved backup-files will be restored.\nIf it is smaller than the current existing EPG-file and the EPG-file isn't valid then the youngest backup-file will be restored.")),
47                 ("youngest", _("Only younger"), _("The backup-file will only be restored if it is younger than the current existing EPG-file.")),
48                 ("biggest", _("Only bigger"), _("The backup-file will only be restored if it is greater than the current existing EPG-file.")),
49         ], default = "youngest_before_biggest"
50 )
51 config.plugins.epgbackup.enable_debug = ConfigYesNo(default = False)
52 config.plugins.epgbackup.plugin_debug_in_file = ConfigYesNo(default = False)
53 config.plugins.epgbackup.backup_log_dir = ConfigDirectory(default = "/tmp")
54 config.plugins.epgbackup.max_boot_count = ConfigNumber(default=3)
55
56 try:
57         from Components.Language import language
58         from Plugins.SystemPlugins.MPHelp import registerHelp, XMLHelpReader
59         from Tools.Directories import resolveFilename, SCOPE_PLUGINS, fileExists
60         lang = language.getLanguage()[:2]
61         
62         HELPPATH = resolveFilename(SCOPE_PLUGINS, "Extensions/EPGBackup")
63         if fileExists(HELPPATH + "/locale/" + str(lang) + "/mphelp.xml"):
64                 helpfile = HELPPATH + "/locale/" + str(lang) + "/mphelp.xml"
65         else:
66                 helpfile = HELPPATH + "/mphelp.xml"
67         reader = XMLHelpReader(helpfile)
68         epgBackuphHelp = registerHelp(*reader)
69 except:
70         debugOut("Help-Error:\n" + str(format_exc()), forced=True)
71         epgBackuphHelp = None
72         
73 # Plugin
74 epgbackup = None
75 from Components.PluginComponent import plugins
76 from Plugins.Plugin import PluginDescriptor
77
78 gUserScriptExists = False
79 # Autostart
80 def autostart(reason, **kwargs):
81         global epgbackup
82         global gUserScriptExists
83         
84         if reason == 0 and "session" in kwargs:
85                 session = kwargs["session"]
86                 
87                 from EPGBackupSupport import EPGBackupSupport
88                 try:
89                         epgbackup = EPGBackupSupport(session)
90                 except:
91                         debugOut("Error while initializing EPGBackupSupport:\n" + str(format_exc()), forced=True)
92         
93                 try:
94                         from Plugins.Extensions.UserScripts.plugin import UserScriptsConfiguration
95                         gUserScriptExists = True
96                         del UserScriptsConfiguration
97                 except:
98                         pass
99
100 def openconfig(session, **kwargs):
101         try:
102                 from EPGBackupConfig import EPGBackupConfig
103                 session.openWithCallback(doneConfiguring, EPGBackupConfig)
104         except:
105                 debugOut("Config-Import-Error:\n" + str(format_exc()), forced=True)
106
107 def showinSetup(menuid):
108         if menuid == "system":
109                 return [(extPrefix + " " + _("EXTENSIONNAME_SETUP"), openconfig, "EPGBackupConfig", None)]
110         return []
111
112 def makeBackup(session, **kwargs):
113         epgbackup.makeBackup(interactive = True)
114
115 def restoreBackup(session, **kwargs):
116         epgbackup.forceDefaultRestore()
117
118 def doneConfiguring(session, needsRestart):
119         if needsRestart:
120                 session.openWithCallback(boundFunction(restartGUICB, session), MessageBox, \
121                         _("To apply your Changes the GUI has to be restarted.\nDo you want to restart the GUI now?"), \
122                         MessageBox.TYPE_YESNO, title = _("EPGBackup Config V %s") % (PLUGIN_VERSION), timeout =  30)
123
124 def restartGUICB(session, answer):
125         if answer is True:
126                 session.open(TryQuitMainloop, 3)
127
128 SetupPlugDescExt = PluginDescriptor(name = extPrefix + " " + _("EXTENSIONNAME_SETUP"), \
129         description = _("Backup and restore EPG Data, including integration of EPGRefresh-plugin"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, \
130         fnc = openconfig,
131         needsRestart = False)
132 SetupPlugDescPlug = PluginDescriptor(name = extPrefix + " " + _("EXTENSIONNAME_SETUP"), \
133         description = _("Backup and restore EPG Data, including integration of EPGRefresh-plugin"), where = PluginDescriptor.WHERE_PLUGINMENU, \
134         fnc = openconfig,
135         needsRestart = False)
136 MakePlugDescExt = PluginDescriptor(name = extPrefix + " " + _("Make Backup"), \
137         description = _("Start making a Backup"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, \
138         fnc = makeBackup,
139         needsRestart = False)
140 RestorePlugDescExt = PluginDescriptor(name = extPrefix + " " + _("Restore Backup"), \
141         description = _("Start a Restore of a Backup"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, \
142         fnc = restoreBackup,
143         needsRestart = False)
144
145 def AdjustPlugin(enable, PlugDescriptor):
146         try:
147                 if enable:
148                         plugins.addPlugin(PlugDescriptor)
149                 else:
150                         plugins.removePlugin(PlugDescriptor)
151         except ValueError:
152                 pass
153         except:
154                 debugOut("AdjustPlugin-Error:\n" + str(format_exc()), forced=True)
155
156 def PluginHousekeeping(configentry):
157         PlugDescInstall = []
158         PlugDescDeinstall = []
159         # value == extension: prior config-entry is both, so extension has not to be added
160         # value == both: prior config-entry is plugin, so only extension must be added
161         if configentry == config.plugins.epgbackup.show_setup_in:
162                 # systemmenu don't have to be adjusted, because restart is required
163                 if config.plugins.epgbackup.show_setup_in.value == "extension":
164                         PlugDescDeinstall.append(SetupPlugDescPlug)
165                 elif config.plugins.epgbackup.show_setup_in.value == "plugin":
166                         PlugDescInstall.append(SetupPlugDescPlug)
167                         PlugDescDeinstall.append(SetupPlugDescExt)
168                 elif config.plugins.epgbackup.show_setup_in.value == "both":
169                         PlugDescInstall.append(SetupPlugDescExt)
170         elif configentry == config.plugins.epgbackup.show_make_backup_in_extmenu:
171                 if configentry.value:
172                         PlugDescInstall.append(MakePlugDescExt)
173                 else:
174                         PlugDescDeinstall.append(MakePlugDescExt)
175         elif configentry == config.plugins.epgbackup.show_backuprestore_in_extmenu:
176                 if configentry.value:
177                         PlugDescInstall.append(RestorePlugDescExt)
178                 else:
179                         PlugDescDeinstall.append(RestorePlugDescExt)
180         
181         for PlugDescriptor in PlugDescDeinstall:
182                 AdjustPlugin(False, PlugDescriptor)
183         for PlugDescriptor in PlugDescInstall:
184                 AdjustPlugin(True, PlugDescriptor)
185
186 config.plugins.epgbackup.show_setup_in.addNotifier(PluginHousekeeping, initial_call = False, immediate_feedback = True)
187 config.plugins.epgbackup.show_make_backup_in_extmenu.addNotifier(PluginHousekeeping, initial_call = False, immediate_feedback = True)
188 config.plugins.epgbackup.show_backuprestore_in_extmenu.addNotifier(PluginHousekeeping, initial_call = False, immediate_feedback = True)
189
190 def Plugins(**kwargs):
191         pluginList = [
192                 PluginDescriptor(
193                         where = [PluginDescriptor.WHERE_SESSIONSTART,PluginDescriptor.WHERE_AUTOSTART],
194                         fnc = autostart)
195         ]
196         
197         if config.plugins.epgbackup.show_setup_in.value == "system":
198                 pluginList.append (PluginDescriptor(
199                         name = extPrefix + " " + _("EXTENSIONNAME_SETUP"),
200                         description = _("Keep EPG-Data over Crashes"),
201                         where = PluginDescriptor.WHERE_MENU,
202                         fnc = showinSetup,
203                         needsRestart = False)
204                 )
205         else:
206                 if config.plugins.epgbackup.show_setup_in.value in ("plugin", "both"):
207                         pluginList.append(SetupPlugDescPlug)
208                 if config.plugins.epgbackup.show_setup_in.value in ("extension", "both"):
209                         pluginList.append(SetupPlugDescExt)
210         
211         if config.plugins.epgbackup.show_make_backup_in_extmenu.value:
212                 pluginList.append(MakePlugDescExt)
213         if config.plugins.epgbackup.show_backuprestore_in_extmenu.value:
214                 pluginList.append(RestorePlugDescExt)
215         
216         return pluginList