s/jaemendo/jamaendo/g
[jamaendo] / jamaui / settings.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 import cPickle, os
25 import logging
26
27 VERSION = 1
28 log = logging.getLogger(__name__)
29
30 class Settings(object):
31     defaults = {
32         'volume':0.5,
33         'user':None,
34         'favorites':set([]) # local favorites - until we can sync back
35         }
36
37     def __init__(self):
38         self.__savename = "/tmp/jamaendo_uisettings"
39         for k,v in self.defaults.iteritems():
40             setattr(self, k, v)
41
42     def set_filename(self, savename):
43         self.__savename = savename
44
45     def favorite(self, album):
46         self.favorites.add(('album', album.ID))
47         self.save()
48
49     def load(self):
50         if not os.path.isfile(self.__savename):
51             return
52         try:
53             f = open(self.__savename)
54             settings = cPickle.load(f)
55             f.close()
56
57             if settings['version'] > VERSION:
58                 log.warning("Settings version %s higher than current version (%s)",
59                             settings['version'], VERSION)
60
61             for k in self.defaults.keys():
62                 if k in settings:
63                     setattr(self, k, settings[k])
64         except Exception, e:
65             log.exception('failed to load settings')
66
67     def save(self):
68         try:
69             settings = {
70                 'version':VERSION,
71                 }
72             for k in self.defaults.keys():
73                 settings[k] = getattr(self, k)
74             f = open(self.__savename, 'w')
75             cPickle.dump(settings, f)
76             f.close()
77         except Exception, e:
78             log.exception('failed to save settings')
79
80 settings = Settings()