enigma2 (20120309 rel32 -> 20120321 rel32)
[enigma2.git] / usr / lib / enigma2 / python / Screens / RecordPaths.py
1 from Screens.Screen import Screen
2 from Screens.LocationBox import MovieLocationBox, TimeshiftLocationBox
3 from Screens.MessageBox import MessageBox
4 from Components.Label import Label
5 from Components.config import config, ConfigSelection, getConfigListEntry, configfile
6 from Components.ConfigList import ConfigListScreen
7 from Components.ActionMap import ActionMap
8 from Tools.Directories import fileExists
9 from Components.UsageConfig import preferredPath, defaultStorageDevice
10 from Components.Harddisk import harddiskmanager
11
12 class RecordPathsSettings(Screen,ConfigListScreen):
13
14         def __init__(self, session):
15                 from Components.Sources.StaticText import StaticText
16                 Screen.__init__(self, session)
17                 self.skinName = "RecordPathsSettings"
18                 self["key_red"] = StaticText(_("Cancel"))
19                 self["key_green"] = StaticText(_("Save"))
20                 self["key_yellow"] = StaticText()
21                 self["key_blue"] = StaticText()
22                 self["introduction"] = StaticText()
23
24                 ConfigListScreen.__init__(self, [])
25                 self.initConfigList()
26
27                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
28                 {
29                     "green": self.save,
30                     "red": self.cancel,
31                     "cancel": self.cancel,
32                     "ok": self.ok,
33                 }, -2)
34
35         def checkReadWriteDir(self, configele):
36                 print "checkReadWrite: ", configele.value
37                 if configele.value in [x[0] for x in self.styles] or configele.value in [x[0] for x in self.storage_styles] or fileExists(configele.value, "w"):
38                         configele.last_value = configele.value
39                         return True
40                 else:
41                         dir = configele.value
42                         configele.value = configele.last_value
43                         self.session.open(
44                                 MessageBox,
45                                 _("The directory %s is not writable.\nMake sure you select a writable directory instead.")%dir,
46                                 type = MessageBox.TYPE_ERROR
47                                 )
48                         return False
49
50         def initConfigList(self):
51                 self.styles = [ ("<default>", _("<Default movie location>")), ("<current>", _("<Current movielist location>")), ("<timer>", _("<Last timer location>")) ]
52                 styles_keys = [x[0] for x in self.styles]
53
54                 mountpoints = [ (p.mountpoint, p.description) for p in harddiskmanager.getConfiguredStorageDevices() if p.isInitialized]
55                 mountpoints.sort(reverse = True)
56                 self.storage_styles = [ ("<undefined>", _("<No default storage device selected.>"))]
57                 self.storage_styles += mountpoints
58                 storage_styles_keys = [x[0] for x in self.storage_styles]
59                 default = defaultStorageDevice()
60                 if default not in storage_styles_keys:
61                         p = harddiskmanager.getDefaultStorageDevicebyUUID(default)
62                         if p is not None:
63                                 default = p.mountpoint
64                                 if p.mountpoint not in storage_styles_keys:
65                                         self.storage_styles.append((p.mountpoint, p.description))
66                         else:
67                                 cfg_uuid = config.storage.get(default, None)
68                                 if cfg_uuid is not None:
69                                         if cfg_uuid["enabled"].value:
70                                                 default = cfg_uuid["mountpoint"].value
71                                                 if default not in storage_styles_keys:
72                                                         description = cfg_uuid["device_description"].value.split()[0] or ""
73                                                         description += " ( " + _("Offline") + " )"
74                                                         self.storage_styles.append((default, description ))
75                                         else:
76                                                 default = "<undefined>"
77                                 else:
78                                         default = "<undefined>"
79                 print "DefaultDevice: ", default, self.storage_styles
80                 self.default_device = ConfigSelection(default = default, choices = self.storage_styles)
81                 tmp = config.movielist.videodirs.value
82                 default = config.usage.default_path.value
83                 if default not in tmp:
84                         tmp = tmp[:]
85                         tmp.append(default)
86                 print "DefaultPath: ", default, tmp
87                 self.default_dirname = ConfigSelection(default = default, choices = tmp)
88                 tmp = config.movielist.videodirs.value
89                 default = config.usage.timer_path.value
90                 if default not in tmp and default not in styles_keys:
91                         tmp = tmp[:]
92                         tmp.append(default)
93                 print "TimerPath: ", default, tmp
94                 self.timer_dirname = ConfigSelection(default = default, choices = self.styles+tmp)
95                 tmp = config.movielist.videodirs.value
96                 default = config.usage.instantrec_path.value
97                 if default not in tmp and default not in styles_keys:
98                         tmp = tmp[:]
99                         tmp.append(default)
100                 print "InstantrecPath: ", default, tmp
101                 self.instantrec_dirname = ConfigSelection(default = default, choices = self.styles+tmp)
102                 default = config.usage.timeshift_path.value
103                 tmp = config.usage.allowed_timeshift_paths.value
104                 if default not in tmp:
105                         tmp = tmp[:]
106                         tmp.append(default)
107                 print "TimeshiftPath: ", default, tmp
108                 self.timeshift_dirname = ConfigSelection(default = default, choices = tmp)
109                 self.timeshift_dirname.last_value = self.timeshift_dirname.value
110                 self.default_dirname.addNotifier(self.checkReadWriteDir, initial_call=False, immediate_feedback=False)
111                 self.default_dirname.last_value = self.default_dirname.value
112                 self.timer_dirname.addNotifier(self.checkReadWriteDir, initial_call=False, immediate_feedback=False)
113                 self.timer_dirname.last_value = self.timer_dirname.value
114                 self.instantrec_dirname.addNotifier(self.checkReadWriteDir, initial_call=False, immediate_feedback=False)
115                 self.instantrec_dirname.last_value = self.instantrec_dirname.value
116                 self.timeshift_dirname.addNotifier(self.checkReadWriteDir, initial_call=False, immediate_feedback=False)
117                 self.timeshift_dirname.last_value = self.timeshift_dirname.value
118
119                 self.list = []
120                 self.device_entry = getConfigListEntry(_("Default storage device"), self.default_device)
121                 self.list.append(self.device_entry)
122                 if config.usage.setup_level.index >= 2:
123                         self.default_entry = getConfigListEntry(_("Default movie location"), self.default_dirname)
124                         self.list.append(self.default_entry)
125                         self.timer_entry = getConfigListEntry(_("Timer record location"), self.timer_dirname)
126                         self.list.append(self.timer_entry)
127                         self.instantrec_entry = getConfigListEntry(_("Instant record location"), self.instantrec_dirname)
128                         self.list.append(self.instantrec_entry)
129                 else:
130                         self.default_entry = getConfigListEntry(_("Movie location"), self.default_dirname)
131                         self.list.append(self.default_entry)
132                 self.timeshift_entry = getConfigListEntry(_("Timeshift location"), self.timeshift_dirname)
133                 self.list.append(self.timeshift_entry)
134                 self["config"].setList(self.list)
135                 if not self.selectionChanged in self["config"].onSelectionChanged:
136                         self["config"].onSelectionChanged.append(self.selectionChanged)
137
138         def selectionChanged(self):
139                 currentry = self["config"].getCurrent()
140                 if currentry == self.device_entry:
141                         self["introduction"].setText(_("Please select the default storage device you want to use for recordings. This device gets mounted to /media/hdd."))
142                 elif currentry == self.default_entry:
143                         self["introduction"].setText(_("Please select the default movielist location which is used for recordings."))
144                 elif currentry == self.timeshift_entry:
145                         self["introduction"].setText(_("Please select the timeshift location which is used for storing timeshift recordings."))
146                 if config.usage.setup_level.index >= 2:
147                         if currentry == self.timer_entry:
148                                 self["introduction"].setText(_("Please select the default timer record location which is used for timer based recordings."))
149                         elif currentry == self.instantrec_entry:
150                                 self["introduction"].setText(_("Please select the default instant record location which is used for instant recordings."))
151
152         def ok(self):
153                 currentry = self["config"].getCurrent()
154                 self.lastvideodirs = config.movielist.videodirs.value
155                 self.lasttimeshiftdirs = config.usage.allowed_timeshift_paths.value
156                 if config.usage.setup_level.index >= 2:
157                         txt = _("Default movie location")
158                 else:
159                         txt = _("Movie location")
160                 if currentry == self.default_entry:
161                         self.entrydirname = self.default_dirname
162                         self.session.openWithCallback(self.dirnameSelected, MovieLocationBox, txt, preferredPath(self.default_dirname.value))
163                 elif currentry == self.timeshift_entry:
164                         self.entrydirname = self.timeshift_dirname
165                         config.usage.timeshift_path.value = self.timeshift_dirname.value
166                         self.session.openWithCallback(self.dirnameSelected,     TimeshiftLocationBox)
167                 if config.usage.setup_level.index >= 2:
168                         if currentry == self.timer_entry:
169                                 self.entrydirname = self.timer_dirname
170                                 self.session.openWithCallback(self.dirnameSelected,     MovieLocationBox,_("Initial location in new timers"), preferredPath(self.timer_dirname.value))
171                         elif currentry == self.instantrec_entry:
172                                 self.entrydirname = self.instantrec_dirname
173                                 self.session.openWithCallback(self.dirnameSelected,     MovieLocationBox, _("Location for instant recordings"), preferredPath(self.instantrec_dirname.value))
174
175         def dirnameSelected(self, res):
176                 if res is not None:
177                         self.entrydirname.value = res
178                         if config.movielist.videodirs.value != self.lastvideodirs:
179                                 styles_keys = [x[0] for x in self.styles]
180                                 tmp = config.movielist.videodirs.value
181                                 default = self.default_dirname.value
182                                 if default not in tmp:
183                                         tmp = tmp[:]
184                                         tmp.append(default)
185                                 self.default_dirname.setChoices(tmp, default=default)
186                                 tmp = config.movielist.videodirs.value
187                                 default = self.timer_dirname.value
188                                 if default not in tmp and default not in styles_keys:
189                                         tmp = tmp[:]
190                                         tmp.append(default)
191                                 self.timer_dirname.setChoices(self.styles+tmp, default=default)
192                                 tmp = config.movielist.videodirs.value
193                                 default = self.instantrec_dirname.value
194                                 if default not in tmp and default not in styles_keys:
195                                         tmp = tmp[:]
196                                         tmp.append(default)
197                                 self.instantrec_dirname.setChoices(self.styles+tmp, default=default)
198                                 self.entrydirname.value = res
199                         if config.usage.allowed_timeshift_paths.value != self.lasttimeshiftdirs:
200                                 tmp = config.usage.allowed_timeshift_paths.value
201                                 default = self.instantrec_dirname.value
202                                 if default not in tmp:
203                                         tmp = tmp[:]
204                                         tmp.append(default)
205                                 self.timeshift_dirname.setChoices(tmp, default=default)
206                                 self.entrydirname.value = res
207                         if self.entrydirname.last_value != res:
208                                 self.checkReadWriteDir(self.entrydirname)
209
210         def save(self):
211                 currentry = self["config"].getCurrent()
212                 defaultChanged = None
213                 if self.checkReadWriteDir(currentry[1]):
214                         config.usage.default_path.value = self.default_dirname.value
215                         config.usage.timer_path.value = self.timer_dirname.value
216                         config.usage.instantrec_path.value = self.instantrec_dirname.value
217                         config.usage.timeshift_path.value = self.timeshift_dirname.value
218                         config.usage.default_path.save()
219                         config.usage.timer_path.save()
220                         config.usage.instantrec_path.save()
221                         config.usage.timeshift_path.save()
222                         if self.default_device.value != defaultStorageDevice():
223                                 if self.default_device.value != "<undefined>": #changing default ?
224                                         tmp = harddiskmanager.getPartitionbyMountpoint(self.default_device.value)
225                                         if tmp is not None:
226                                                 defaultChanged = harddiskmanager.changeStorageDevice(tmp.uuid, "mount_default", None)
227                                 else: #disabling default ?
228                                         p = harddiskmanager.getDefaultStorageDevicebyUUID(defaultStorageDevice())
229                                         if p is not None:
230                                                 defaultChanged = harddiskmanager.changeStorageDevice(defaultStorageDevice(), "unmount", None)
231                         if defaultChanged is None:
232                                 self.close()
233                         elif defaultChanged is False:
234                                 self.session.open(MessageBox, _("There was en error while configuring your storage device."), MessageBox.TYPE_ERROR)
235                         else:
236                                 self.close()
237
238         def cancel(self):
239                 self.close()