~ubuntu-branches/ubuntu/vivid/hamster-applet/vivid

« back to all changes in this revision

Viewing changes to hamster/configuration.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2009-10-22 22:01:54 UTC
  • mfrom: (1.2.4 upstream) (5.2.2 sid)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20091022220154-do4zoetlf35l36pe
Tags: 2.28.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright (C) 2008 Toms Bauģis <toms.baugis at gmail.com>
 
4
 
 
5
# This file is part of Project Hamster.
 
6
 
 
7
# Project Hamster is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
 
 
12
# Project Hamster is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
 
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with Project Hamster.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
import gconf
 
21
import gettext
 
22
import os
 
23
import defs
 
24
from db import Storage
 
25
from dispatcher import Dispatcher
 
26
 
 
27
class Singleton(object):
 
28
     def __new__(cls, *args, **kwargs):
 
29
         if '__instance' not in vars(cls):
 
30
             cls.__instance = object.__new__(cls, *args, **kwargs)
 
31
         return cls.__instance
 
32
 
 
33
class RuntimeStore(Singleton):
 
34
    """
 
35
    Handles one-shot configuration that is not stored between sessions
 
36
    """
 
37
    database_file = ""
 
38
    data_dir = ""
 
39
    dispatcher = None
 
40
    storage = None
 
41
    trace_sql = False
 
42
 
 
43
    def __init__(self):
 
44
        gettext.install("hamster-applet", unicode = True)
 
45
 
 
46
        # Typically shared data dir is /usr/share/hamster-applet
 
47
        if os.path.realpath(__file__).startswith(defs.PYTHONDIR):
 
48
            data_dir = os.path.join(defs.DATA_DIR, "hamster-applet")
 
49
        else:
 
50
            data_dir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data'))
 
51
        self.data_dir = data_dir
 
52
        self.dispatcher = Dispatcher()
 
53
        self.storage = Storage(self.dispatcher)
 
54
 
 
55
    def get_art_dir(self):
 
56
        return os.path.join(self.data_dir, "art")
 
57
 
 
58
    art_dir = property(get_art_dir, None)
 
59
 
 
60
runtime = RuntimeStore()
 
61
runtime.database_file = os.path.expanduser("~/.gnome2/hamster-applet/hamster.db")
 
62
 
 
63
class GconfStore(Singleton):
 
64
    """
 
65
    Handles storing to and retrieving values from GConf 
 
66
    """
 
67
 
 
68
    # GConf directory for deskbar in window mode and shared settings
 
69
    GCONF_DIR = "/apps/hamster-applet/general"
 
70
    
 
71
    # GConf key for global keybinding
 
72
    GCONF_KEYBINDING = GCONF_DIR + "/keybinding"
 
73
    GCONF_ENABLE_TIMEOUT = GCONF_DIR + "/enable_timeout"
 
74
    GCONF_STOP_ON_SHUTDOWN = GCONF_DIR + "/stop_on_shutdown"  
 
75
    GCONF_NOTIFY_INTERVAL = GCONF_DIR + "/notify_interval" 
 
76
    GCONF_NOTIFY_ON_IDLE = GCONF_DIR + "/notify_on_idle"
 
77
 
 
78
    __instance = None
 
79
        
 
80
    def __init__(self):
 
81
        super(GconfStore, self).__init__()
 
82
        self._client = gconf.client_get_default()
 
83
        self.__connect_notifications()
 
84
        
 
85
    def __connect_notifications(self):
 
86
        self._client.add_dir(self.GCONF_DIR, gconf.CLIENT_PRELOAD_RECURSIVE)
 
87
        self._client.notify_add(self.GCONF_KEYBINDING, lambda x, y, z, a: runtime.dispatcher.dispatch("gconf_keybinding_changed", z.value.get_string()))
 
88
        self._client.notify_add(self.GCONF_ENABLE_TIMEOUT, lambda x, y, z, a: runtime.dispatcher.dispatch("gconf_timeout_enabled_changed", z.value.get_bool()))
 
89
        self._client.notify_add(self.GCONF_STOP_ON_SHUTDOWN, lambda x, y, z, a: runtime.dispatcher.dispatch("gconf_stop_on_shutdown_changed", z.value.get_bool()))
 
90
        self._client.notify_add(self.GCONF_NOTIFY_INTERVAL, lambda x, y, z, a: runtime.dispatcher.dispatch("gconf_notify_interval_changed", z.value.get_int()))
 
91
        self._client.notify_add(self.GCONF_NOTIFY_ON_IDLE, lambda x, y, z, a: runtime.dispatcher.dispatch("gconf_notify_on_idle_changed", z.value.get_bool()))
 
92
 
 
93
    
 
94
    def get_keybinding(self):
 
95
        return self._client.get_string(self.GCONF_KEYBINDING) or ""
 
96
    
 
97
    def get_timeout_enabled(self):
 
98
        return self._client.get_bool(self.GCONF_ENABLE_TIMEOUT) or False
 
99
 
 
100
    def get_stop_on_shutdown(self):
 
101
        return self._client.get_bool(self.GCONF_STOP_ON_SHUTDOWN) or False
 
102
        
 
103
    def get_notify_interval(self):
 
104
        return self._client.get_int(self.GCONF_NOTIFY_INTERVAL) or 27
 
105
 
 
106
    def get_notify_on_idle(self):
 
107
        return self._client.get_bool(self.GCONF_NOTIFY_ON_IDLE) or False
 
108
 
 
109
    #------------------------    
 
110
    def set_keybinding(self, binding):
 
111
        self._client.set_string(self.GCONF_KEYBINDING, binding)
 
112
    
 
113
    def set_timeout_enabled(self, enabled):
 
114
        self._client.set_bool(self.GCONF_ENABLE_TIMEOUT, enabled)
 
115
        
 
116
    def set_stop_on_shutdown(self, enabled):
 
117
        self._client.set_bool(self.GCONF_STOP_ON_SHUTDOWN, enabled)
 
118
    
 
119
    def set_notify_interval(self, interval):
 
120
        return self._client.set_int(self.GCONF_NOTIFY_INTERVAL, interval)
 
121
 
 
122
    def set_notify_on_idle(self, enabled):
 
123
        self._client.set_bool(self.GCONF_NOTIFY_ON_IDLE, enabled)
 
124