view backend/couchdb/Preferences.py @ 258:4ca1fac32dde

Pull the constant of days to keep feed entries into the abstract class
author Dirk Olmes <dirk@xanthippe.ping.de>
date Tue, 12 Mar 2019 02:39:18 +0100
parents f79be01821c4
children
line wrap: on
line source

# -*- coding: utf-8 -*-
from ..AbstractPreferences import AbstractPreferences
import CouchDb

PROXY_PORT = "proxyPort"

class Preferences(AbstractPreferences):
    def __init__(self, database):
        self.database = database
        self._initDocument()
        self.documentIsDirty = False

    def _initDocument(self):
        viewResults = self.database.view(CouchDb.preference())
        try:
            row = iter(viewResults).next()
            self.document = self.database[row.id]
        except StopIteration:
            empty = {"doctype": "preferences"}
            doc_id, doc_rev = self.database.save(empty)
            self.document = self.database[doc_id]

    def _documentValue(self, key, defaultValue=None):
        if key in self.document.keys():
            return self.document[key]
        else:
            return defaultValue

    def _setDocumentValue(self, key, value):
        self.document[key] = value
        self.documentIsDirty = True

    def proxyHost(self):
        return self._documentValue(self.PROXY_HOST)

    def useProxy(self):
        return self._documentValue(self.USE_PROXY, True)

    def setUseProxy(self, value):
        self._setDocumentValue(USE_PROXY, value)

    def setProxyHost(self, hostname):
        if hostname is None:
            if PROXY_HOST in self.document.keys():
                del self.document[PROXY_HOST]
                self.documentIsDirty = True
        else:
            self._setDocumentValue(PROXY_HOST, hostname)

    def proxyPort(self):
        return self._documentValue(PROXY_PORT)

    def setProxyPort(self, port):
        if port is None:
            if PROXY_PORT in self.document.keys():
                del self.document[PROXY_PORT]
                self.documenIsDirty = True
        else:
            self._setDocumentValue(PROXY_PORT, port)

    def showOnlyUnreadFeeds(self):
        return self._documentValue(self.SHOW_ONLY_UNREAD_FEEDS, False)

    def setShowOnlyUnreadFeeds(self, flag):
        self._setDocumentValue(self.SHOW_ONLY_UNREAD_FEEDS, flag)

    def startMaximized(self):
        return self._documentValue(self.START_MAXIMIZED, False)

    def setStartMaximized(self, flag):
        self._setDocumentValue(START_MAXIMIZED, flag)

    def hideReadFeedEntries(self):
        return self._documentValue(self.HIDE_READ_FEED_ENTRIES, False)

    def setHideReadFeedEntries(self, flag):
        self._setDocumentValue(self.HIDE_READ_FEED_ENTRIES, flag)

    def daysToKeepFeedEntries(self):
        value = self._documentValue(self.DAYS_TO_KEEP_FEED_ENTRIES, 90)
        return int(value)

    def setDaysToKeepFeedEntries(self, days):
        self._setDocumentValue(DAYS_TO_KEEP_FEED_ENTRIES, days)

    def commit(self):
        if self.documentIsDirty:
            self.database.save(self.document)

    def rollback(self):
        if self.documentIsDirty:
            self._initDocument()