view backend/sqlalchemy/Preferences.py @ 249:5695197a3ca5

Add an about Qt help menu item
author Dirk Olmes <dirk@xanthippe.ping.de>
date Thu, 09 Jun 2016 05:19:03 +0200
parents e34c53a3e407
children
line wrap: on
line source

# -*- coding: utf-8 -*-
from Preference import Preference

DAYS_TO_KEEP_FEED_ENTRIES = "DAYS_TO_KEEP_FEED_ENTRIES"
HIDE_READ_ENTRIES = "HIDE_READ_FEED_ENTRIES"
PROXY_HOST = "PROXY_HOST"
PROXY_PORT = "PROXY_PORT"
SHOW_ONLY_UNREAD_FEEDS = "SHOW_ONLY_UNREAD_FEEDS"
START_MAXIMIZED = "START_MAXIMIZED"

def str2bool(string):
    return string.lower() in ["yes", "true", "t", "1"]

def bool2str(bool):
    if bool:
        return "True"
    else:
        return "False"

class Preferences(object):
    def __init__(self, session):
        self.session = session
        self.cache = {}

    def _cachedPreference(self, key, defaultValue=None, addIfMissing=True):
        if key in self.cache:
            return self.cache[key]
        else:
            pref = Preference.forKey(key, self.session)
            if pref is not None:
                self.cache[key] = pref
            elif pref is None and addIfMissing:
                pref = Preference(key, str(defaultValue))
                self.session.add(pref)
                self.cache[key] = pref
            return pref

    def commit(self):
        self.session.commit()

    def rollback(self):
        self.session.rollback()

    def startMaximized(self):
        pref = self._cachedPreference(START_MAXIMIZED, False)
        return str2bool(pref.value)

    def setStartMaximized(self, flag):
        pref = self._cachedPreference(START_MAXIMIZED)
        pref.value = bool2str(flag)

    def hideReadFeedEntries(self):
        pref = self._cachedPreference(HIDE_READ_ENTRIES, False)
        return str2bool(pref.value)

    def setHideReadFeedEntries(self, flag):
        pref = self._cachedPreference(HIDE_READ_ENTRIES)
        pref.value = bool2str(flag)

    def showOnlyUnreadFeeds(self):
        pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS, False)
        return str2bool(pref.value)

    def setShowOnlyUnreadFeeds(self, flag):
        pref = self._cachedPreference(SHOW_ONLY_UNREAD_FEEDS)
        pref.value = bool2str(flag)

    def proxyHost(self):
        pref = self._cachedPreference(PROXY_HOST)
        return pref.value

    def setProxyHost(self, hostname):
        if hostname is None:
            pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
            if pref is not None:
                self.session.delete(pref)
                del(self.cache[PROXY_HOST])
        else:
            pref = self._cachedPreference(PROXY_HOST)
            pref.value = str(hostname)

    def proxyPort(self):
        pref = self._cachedPreference(PROXY_PORT, 3128)
        return int(pref.value)

    def setProxyPort(self, port):
        if port is None:
            pref = self._cachedPreference(PROXY_PORT, addIfMissing=False)
            if pref is not None:
                self.session.delete(pref)
                del(self.cache[PROXY_PORT])
        else:
            pref = self._cachedPreference(PROXY_PORT)
            pref.value = str(port)

    def isProxyConfigured(self):
        pref = self._cachedPreference(PROXY_HOST, addIfMissing=False)
        return pref is not None

    def daysToKeepFeedEntries(self):
        pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES, 90, addIfMissing=True)
        return int(pref.value)

    def setDaysToKeepFeedEntries(self, dayString):
        pref = self._cachedPreference(DAYS_TO_KEEP_FEED_ENTRIES)
        pref.value = dayString