honor horizontal alignment also when a mark is present
[enigma2.git] / lib / python / Plugins / SystemPlugins / ConfigurationBackup / plugin.py
1 from enigma import *
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Screens.Console import Console
5 from Components.ActionMap import ActionMap, NumberActionMap
6 from Components.Pixmap import *
7 from Components.Pixmap import Pixmap
8 from Components.Label import Label
9 from Components.MenuList import MenuList
10 from Components.config import config, configSelection, configSelection, getConfigListEntry, configElement, ConfigSubsection, currentConfigSelectionElement
11 from Components.ConfigList import ConfigList
12 from Plugins.Plugin import PluginDescriptor
13
14 from Tools.NumericalTextInput import *
15 from Tools.Directories import *
16 import os
17 import string
18 import time
19 import datetime
20
21 plugin_path = ""
22
23 BackupPath = {
24                 "hdd" : "/media/hdd/backup",
25                 "usb" : "/media/usb/backup",
26                 "cf" : "/media/cf/backup"
27         }
28
29 MountPoints = {
30                 "hdd" : "/media/hdd",
31                 "usb" : "/media/usb",
32                 "cf" : "/media/cf"
33         }
34
35 class BackupSetup(Screen):
36         skin = """
37                 <screen position="135,144" size="450,300" title="Backup and Restore" >
38                         <widget name="config" position="10,10" size="430,240" />
39                         <widget name="ok" position="10,255" size="100,40" pixmap="~/green.png" transparent="1" alphatest="on" />
40                         <widget name="oktext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
41                         <widget name="cancel" position="120,255" size="100,40" pixmap="~/red.png" transparent="1" alphatest="on" />
42                         <widget name="canceltext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="black" />
43                         <widget name="restore" position="230,255" size="100,40" pixmap="~/yellow.png" transparent="1" alphatest="on" />
44                         <widget name="restoretext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
45                         <widget name="backup" position="340,255" size="100,40" pixmap="~/blue.png" transparent="1" alphatest="on" />
46                         <widget name="backuptext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
47                 </screen>"""
48                 
49         def keyLeft(self):
50                 self["config"].handleKey(config.key["prevElement"])
51
52         def keyRight(self):
53                 self["config"].handleKey(config.key["nextElement"])
54
55         def keyNumberGlobal(self, number):
56                 print "You pressed number " + str(number)
57                 if (self["config"].getCurrent()[1].parent.enabled == True):
58                         self["config"].handleKey(config.key[str(number)])
59
60         def keyCancel(self):
61                 for x in self["config"].list:
62                         x[1].cancel()
63                 self.close()
64                 
65         def keySave(self):
66                 for x in self["config"].list:
67                         x[1].save()
68                 self.close()
69         
70         def __init__(self, session, args = None):
71                 Screen.__init__(self, session)
72                 self.skin_path = plugin_path
73                 
74                 self["oktext"] = Label(_("OK"))
75                 self["canceltext"] = Label(_("Cancel"))
76                 self["backuptext"] = Label(_("Backup"))
77                 self["restoretext"] = Label(_("Restore"))
78                 self["restore"] = Pixmap()
79                 self["backup"] = Pixmap()
80                 self["ok"] = Pixmap()
81                 self["cancel"] = Pixmap()
82
83                 self.path = ""
84                 self.list = []
85                 self["config"] = ConfigList(self.list)
86                 self.createSetup()
87
88                 self["actions"] = NumberActionMap(["SetupActions"],
89                 {
90                         "ok": self.keySave,
91                         "cancel": self.keyCancel,
92                         "left": self.keyLeft,
93                         "right": self.keyRight
94                 }, -1)
95                 
96                 self["shortcuts"] = ActionMap(["ShortcutActions"],
97                 {
98                         "red": self.keyCancel,
99                         "green": self.keySave,
100                         "blue": self.Backup,
101                         "yellow": self.Restore,
102                 })
103                 
104
105         def createSetup(self):
106                 print "Creating BackupSetup"
107                 self.list = [ ]
108                 self["config"] = ConfigList(self.list)
109                 config.backup = ConfigSubsection()
110                 config.backup.type = configElement("config.backup.type", configSelection, 0, (("full", _("full /etc directory")), ("settings", _("only /etc/enigma2 directory")), ("var", _("/var directory")), ("skin", _("/usr/share/enigma2 directory"))))
111                 config.backup.location = configElement("config.backup.location", configSelection, 0, (("usb", _("USB Stick")), ("cf", _("CF Drive")), ("hdd", _("Harddisk"))))
112                 self.list.append(getConfigListEntry(_("Backup Mode"), config.backup.type))
113                 self.list.append(getConfigListEntry(_("Backup Location"), config.backup.location))
114
115         def createBackupfolders(self):
116                 self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
117                 print "Creating Backup Folder if not already there..."
118                 if (os.path.exists(str(self.path)) == False):
119                         os.makedirs(str(self.path))
120
121         def Backup(self):
122                 print "this will start the backup now!"
123                 self.session.openWithCallback(self.runBackup, MessageBox, _("Do you want to backup now?\nAfter pressing OK, please wait!"))     
124
125         def Restore(self):
126                 print "this will start the restore now!"
127                 self.session.open(RestoreMenu)
128
129         def runBackup(self, result):
130                 if result:
131                         if os.path.ismount(MountPoints[str(currentConfigSelectionElement(config.backup.location))]):
132                                 self.createBackupfolders()
133                                 d = time.localtime()
134                                 dt = datetime.date(d.tm_year, d.tm_mon, d.tm_mday)
135                                 self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
136                                 if currentConfigSelectionElement(config.backup.type) == "full":
137                                         print "Backup Mode: Full"
138                                         self.session.open(Console, title = "Backup running", cmdlist = ["tar -czvf " + self.path + "/" + str(dt) + "_full_backup.tar.gz /etc/"])
139                                 if currentConfigSelectionElement(config.backup.type) == "settings":
140                                         print "Backup Mode: Settings"
141                                         self.session.open(Console, title = "Backup running", cmdlist = ["tar -czvf " + self.path + "/" + str(dt) + "_settings_backup.tar.gz /etc/enigma2/"])
142                                 if currentConfigSelectionElement(config.backup.type) == "var":
143                                         print "Backup Mode: var"
144                                         self.session.open(Console, title = "Backup running", cmdlist = [ "tar -czvf " + self.path + "/" + str(dt) + "_var_backup.tar.gz /var/"])
145                                 if currentConfigSelectionElement(config.backup.type) == "skin":
146                                         print "Backup Mode: skin"
147                                         self.session.open(Console, title ="Backup running", cmdlist = [ "tar -czvf " + self.path + "/" + str(dt) + "_skin_backup.tar.gz /usr/share/enigma2/"])
148                         else:
149                                 self.session.open(MessageBox, _("Sorry your Backup destination does not exist\n\nPlease choose an other one."), MessageBox.TYPE_INFO)
150
151 class RestoreMenu(Screen):
152         skin = """
153                 <screen position="135,144" size="450,300" title="Restore Backups" >
154                 <widget name="filelist" position="10,10" size="430,240" scrollbarMode="showOnDemand" />
155                 <widget name="cancel" position="120,255" size="100,40" pixmap="~/red.png" transparent="1" alphatest="on" />             
156                 <widget name="canceltext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="black" />
157                 <widget name="restore" position="230,255" size="100,40" pixmap="~/yellow.png" transparent="1" alphatest="on" />
158                 <widget name="restoretext" position="0,0" size="0,0" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1"  foregroundColor="black" />
159                 </screen>"""
160
161         def __init__(self, session, args = None):
162                 Screen.__init__(self, session)
163                 self.skin_path = plugin_path
164                 
165                 self["canceltext"] = Label(_("Cancel"))
166                 self["restoretext"] = Label(_("Restore"))
167                 self["restore"] = Pixmap()
168                 self["cancel"] = Pixmap()
169                 
170                 self.sel = []
171                 self.val = []
172                 self.entry = False
173                 self.exe = False
174                 
175                 self.path = ""
176
177                 self["actions"] = NumberActionMap(["SetupActions"],
178                 {
179                         "ok": self.KeyOk,
180                         "cancel": self.keyCancel
181                 }, -1)
182                 
183                 self["shortcuts"] = ActionMap(["ShortcutActions"],
184                 {
185                         "red": self.keyCancel,
186                         "yellow": self.KeyOk,
187                 })
188                 self.flist = []
189                 self["filelist"] = MenuList(self.flist)
190                 self.fill_list()
191
192
193         def fill_list(self):
194                 self.flist = []
195                 self.path = BackupPath[str(currentConfigSelectionElement(config.backup.location))]
196                 if (os.path.exists(str(self.path)) == False):
197                         os.makedirs(str(self.path))
198                 for file in os.listdir(str(self.path)):
199                         if (file.endswith(".tar.gz")):
200                                 self.flist.append((file))
201                                 self.entry = True
202                                 self["filelist"].l.setList(self.flist)
203
204         def KeyOk(self):
205             if (self.exe == False) and (self.entry == True):
206                 self.sel = self["filelist"].getCurrent()
207                 self.val = self.path + self.sel
208                 self.session.openWithCallback(self.startRestore, MessageBox, _("are you sure you want to restore\nfollowing backup:\n" + self.sel + "\nEnigma2 will restart after the restore"))
209
210         def keyCancel(self):
211                 self.close()
212
213         def startRestore(self, ret = False):
214                 if (ret == True):
215                         self.exe = True
216                         self.session.open(Console, title = "Restore running", cmdlist = ["tar -xzvf " + self.path + "/" + self.sel + " -C /", "killall enigma2"])
217                         
218         def Exit(self):
219                 self.close()
220
221 def BackupMain(session, **kwargs):
222         session.open(BackupSetup)
223
224 def Plugins(path, **kwargs):
225         global plugin_path
226         plugin_path = path
227         return PluginDescriptor(name="Backup/Restore", description="Backup and Restore your Settings", icon="backup.png", where = PluginDescriptor.WHERE_PLUGINMENU, fnc=BackupMain)