1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# This file is in the public domain
6
"""Provides a shared preferences dictionary"""
8
from desktopcouch.records.server import CouchDatabase
9
from desktopcouch.records.record import Record
13
class User_dict(dict):
14
''' a dictionary with extra methods:
16
persistence: load, save and db_connect
17
gobject signals: connect and emit.
19
Don't use this directly. Please use the preferences instance.'''
21
def __init__(self, *args, **kwds):
22
dict.__init__(self, *args, **kwds)
24
self._db_name = "mythremote"
29
"http://wiki.ubuntu.com/Quickly/RecordTypes/Mythremote/"
32
class Publisher(gtk.Invisible): # pylint: disable=R0904
33
'''set up signals in a separate class
35
gtk.Invisible has 230 public methods'''
36
__gsignals__ = {'changed' : (gobject.SIGNAL_RUN_LAST,
37
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
38
'loaded' : (gobject.SIGNAL_RUN_LAST,
39
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))}
41
publisher = Publisher()
42
self.emit = publisher.emit
43
self.connect = publisher.connect
48
create if necessary'''
49
# logging.basicConfig will be called now
50
self._database = CouchDatabase(self._db_name, create=True)
54
self._database.update_fields(self._key, self)
59
self.update({"record_type": self._record_type})
61
results = self._database.get_records(
62
record_type=self._record_type, create_view=True)
64
if len(results.rows) == 0:
65
# No preferences have ever been saved
66
# save them before returning.
67
self._key = self._database.put_record(Record(self))
69
self.update(results.rows[0].value)
71
self._key = results.rows[0].value["_id"]
72
self.emit('loaded', None)
74
def update(self, *args, **kwds):
75
''' interface for dictionary
77
send changed signal when appropriate '''
81
new_data.update(*args, **kwds)
84
for key in new_data.keys():
85
if new_data.get(key) != dict.get(self, key):
86
changed_keys.append(key)
87
dict.update(self, new_data)
89
self.emit('changed', tuple(changed_keys))
91
def __setitem__(self, key, value):
92
''' interface for dictionary
94
send changed signal when appropriate '''
95
if value != dict.get(self, key):
96
dict.__setitem__(self, key, value)
97
self.emit('changed', (key,))
99
preferences = User_dict()