~fabrixxm/confy

ref: 3711232f069768031fd703392d6fc7635fa708da confy/src/settings.py -rw-r--r-- 2.1 KiB
3711232ffabrixxm Fix typo 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# settings.py
#
# Copyright 2020 Fabio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from gi.repository import Gio
from gi.repository import Handy

# in secs
CACHE_NOCACHE = 0
CACHE_ONE_HOUR = 60 * 60
CACHE_ONE_DAY = 24 * CACHE_ONE_HOUR
CACHE_ONE_WEEK = 7 * CACHE_ONE_DAY

class CacheDuration:
    NOCACHE = 0
    ONEHOUR = 1
    ONEDAY = 2
    ONEWEEK = 3

    @classmethod
    def as_model(cls):
        m = Gio.ListStore.new(Handy.ValueObject)
        m.insert(0, Handy.ValueObject.new(_("No cache")))
        m.insert(1, Handy.ValueObject.new(_("One hour")))
        m.insert(2, Handy.ValueObject.new(_("One day")))
        m.insert(3, Handy.ValueObject.new(_("One week")))
        return m

    @classmethod
    def get_duration(cls, enum_value):
        return [
            CACHE_NOCACHE,
            CACHE_ONE_HOUR,
            CACHE_ONE_DAY,
            CACHE_ONE_WEEK
        ][enum_value]


class Settings(Gio.Settings):
    _instance = None

    def __init__(self):
        """
            Init settings
        """
        Gio.Settings.__init__(self)

    @classmethod
    def instance(kls):
        return kls._instance

    @classmethod
    def init(kls, app_id):
        """
            Initialize and return a new Settings object
        """
        settings = Gio.Settings.new(app_id)
        settings.__class__ = Settings
        kls._instance = settings
        return settings

    def get_list_cache(self):
        return CacheDuration.get_duration(self.get_int('list-cache'))

    def get_event_cache(self):
        return CacheDuration.get_duration(self.get_int('event-cache'))