Added 'helldon' to transparently port the thing to the desktop :P
[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 import jamaendo
27 import datetime
28
29 from postoffice import postoffice
30
31 VERSION = 1
32 log = logging.getLogger(__name__)
33
34 class Settings(object):
35     defaults = {
36         'volume':0.1,
37         'user':None,
38         'favorites':set([]), # local favorites - until we can sync back
39         'playlists':{},
40         }
41
42     def __init__(self):
43         self.__savename = "/tmp/jamaendo_uisettings"
44         for k,v in self.defaults.iteritems():
45             setattr(self, k, v)
46
47     def __setattr__(self, key, value):
48         object.__setattr__(self, key, value)
49         if key in self.defaults.keys():
50             postoffice.notify('settings-changed', key, value)
51
52     def set_filename(self, savename):
53         self.__savename = savename
54
55     def favorite(self, album):
56         self.favorites.add(('album', album.ID))
57         self.save()
58         postoffice.notify('settings-changed', 'favorites', self.favorites)
59
60     def get_playlist(self, playlist, get_track_objects=True):
61         entry = self.playlists.get(playlist)
62         if entry:
63             if get_track_objects:
64                 return [jamaendo.Track(item['id'], item['data']) for item in entry]
65             return entry
66         return None
67
68     def add_to_playlist(self, playlist, track):
69         if isinstance(track, jamaendo.Track):
70             track = {'id':track.ID, 'data':track.get_data()}
71         assert(isinstance(track, dict))
72         lst = self.playlists.get(playlist)
73         if not lst:
74             lst = []
75             self.playlists[playlist] = lst
76         lst.append(track)
77         postoffice.notify('settings-changed', 'playlists', self.playlists)
78         log.debug("playlists is now %s", self.playlists)
79
80     def load(self):
81         if not os.path.isfile(self.__savename):
82             return
83         try:
84             f = open(self.__savename)
85             settings = cPickle.load(f)
86             f.close()
87
88             if settings['version'] > VERSION:
89                 log.warning("Settings version %s higher than current version (%s)",
90                             settings['version'], VERSION)
91
92             for k in self.defaults.keys():
93                 if k in settings:
94                     if k == 'playlists' and not isinstance(k, dict):
95                         continue
96                     setattr(self, k, settings[k])
97             print settings
98         except Exception, e:
99             log.exception('failed to load settings')
100
101     def save(self):
102         try:
103             settings = {
104                 'version':VERSION,
105                 }
106             for k in self.defaults.keys():
107                 settings[k] = getattr(self, k)
108             f = open(self.__savename, 'w')
109             cPickle.dump(settings, f)
110             f.close()
111             print settings
112         except Exception, e:
113             log.exception('failed to save settings')
114
115 settings = Settings()