Merge branch 'refs/heads/master' of ssh://sreichholf@scm.schwerkraft.elitedvb.net...
[vuplus_dvbapp-plugin] / automaticvolumeadjustment / src / AutomaticVolumeAdjustmentConfig.py
1 # -*- coding: utf-8 -*-
2 #
3 #  AutomaticVolumeAdjustment E2
4 #
5 #  $Id$
6 #
7 #  Coded by Dr.Best (c) 2010
8 #  Support: www.dreambox-tools.info
9 #
10 #  This plugin is licensed under the Creative Commons 
11 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
12 #  License. To view a copy of this license, visit
13 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
14 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
15 #
16 #  Alternatively, this plugin may be distributed and executed on hardware which
17 #  is licensed by Dream Multimedia GmbH.
18
19 #  This plugin is NOT free software. It is open source, you are allowed to
20 #  modify it (if you keep the license), but it may not be commercially 
21 #  distributed other than under the conditions noted above.
22 #
23 from Components.config import ConfigSubsection, ConfigText, \
24         config, ConfigInteger, Config, ConfigSubList, ConfigDirectory, NoSave, ConfigYesNo, ConfigSelectionNumber, ConfigSelection
25 from os import path as os_path, open as os_open, close as os_close, O_RDWR as os_O_RDWR, O_CREAT  as os_O_CREAT 
26 from pickle import load as pickle_load, dump as pickle_dump
27
28 CONFIG_FILE_VOLUME = '/usr/lib/enigma2/python/Plugins/SystemPlugins/AutomaticVolumeAdjustment/config_volume'
29
30 def getVolumeDict():
31         if os_path.exists(CONFIG_FILE_VOLUME):
32                 pkl_file = open(CONFIG_FILE_VOLUME, 'rb')
33                 if pkl_file:
34                         volumedict = pickle_load(pkl_file)
35                         pkl_file.close()
36                         return volumedict
37         return {}
38
39 def saveVolumeDict(dict):
40         pkl_file = open(CONFIG_FILE_VOLUME, 'wb')
41         if pkl_file:
42                 pickle_dump(dict, pkl_file)
43                 pkl_file.close()
44
45 class AutomaticVolumeAdjustmentConfig():
46         def __init__(self):
47                 self.CONFIG_FILE = '/usr/lib/enigma2/python/Plugins/SystemPlugins/AutomaticVolumeAdjustment/config'
48                 # load config file
49                 self.loadConfigFile()
50
51         # load config file and initialize 
52         def loadConfigFile(self):
53                 print "[AutomaticVolumeAdjustmentConfig] Loading config file..."
54                 self.config = Config()
55                 if not os_path.exists(self.CONFIG_FILE):
56                         fd = os_open( self.CONFIG_FILE, os_O_RDWR|os_O_CREAT)
57                         os_close( fd )
58                 self.config.loadFromFile(self.CONFIG_FILE)
59                 self.config.entriescount =  ConfigInteger(0)
60                 self.config.Entries = ConfigSubList()
61                 self.config.enable = ConfigYesNo(default = False)
62                 self.config.modus = ConfigSelection(choices = [("0", _("Automatic volume adjust")), ("1", _("Remember service volume value"))], default = "0")
63                 self.config.adustvalue = ConfigSelectionNumber(-50, 50, 5, default = 25)
64                 self.config.mpeg_max_volume = ConfigSelectionNumber(10, 100, 5, default = 100)
65                 self.config.show_volumebar = ConfigYesNo(default = False)
66                 self.initConfig()
67
68         def initConfig(self):
69                 count = self.config.entriescount.value
70                 if count != 0:
71                         i = 0
72                         while i < count:
73                                 self.initEntryConfig()
74                                 i += 1
75                 print "[AutomaticVolumeAdjustmentConfig] Loaded %s entries from config file..." % count
76
77         def initEntryConfig(self):
78                 self.config.Entries.append(ConfigSubsection())
79                 i = len(self.config.Entries) - 1
80                 self.config.Entries[i].servicereference = ConfigText(default = "")
81                 self.config.Entries[i].name = NoSave(ConfigDirectory(default = _("Press OK to select a service")))
82                 self.config.Entries[i].adjustvalue = ConfigSelectionNumber(-50, 50, 5, default = 25)
83                 return self.config.Entries[i]
84         
85         def remove(self, configItem):
86                 self.config.entriescount.value = self.config.entriescount.value - 1
87                 self.config.entriescount.save()
88                 self.config.Entries.remove(configItem)
89                 self.config.Entries.save()
90                 self.save()
91         
92         def save(self):
93                 print "[AutomaticVolumeAdjustmentConfig] saving config file..."
94                 self.config.saveToFile(self.CONFIG_FILE)