Logo Search packages:      
Sourcecode: calibre version File versions  Download package


#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement

__license__   = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

import os, copy

from PyQt4.Qt import QAbstractItemModel, QVariant, Qt, QColor, QFont, QIcon, \
        QModelIndex, SIGNAL

from calibre.utils.search_query_parser import SearchQueryParser
from calibre.gui2 import NONE
from calibre.utils.localization import get_language
from calibre.web.feeds.recipes.collection import \
        get_builtin_recipe_collection, get_custom_recipe_collection, \
        SchedulerConfig, download_builtin_recipe
from calibre.utils.pyparsing import ParseException

class NewsTreeItem(object):

    def __init__(self, builtin, custom, scheduler_config, parent=None):
        self.builtin, self.custom = builtin, custom
        self.scheduler_config = scheduler_config
        self.parent = parent
        if self.parent is not None:
        self.children = []

    def row(self):
        if self.parent is not None:
            return self.parent.children.index(self)
        return 0

    def append(self, child):
        child.parent = self

    def data(self, role):
        return NONE

    def flags(self):
        return Qt.ItemIsEnabled|Qt.ItemIsSelectable

    def sort(self):
        for child in self.children:

    def prune(self):
        for child in list(self.children):
            if len(child.children) == 0:
                child.parent = None

class NewsCategory(NewsTreeItem):

    def __init__(self, category, builtin, custom, scheduler_config, parent):
        NewsTreeItem.__init__(self, builtin, custom, scheduler_config, parent)
        self.category = category
        self.cdata = get_language(self.category)
        self.bold_font = QFont()
        self.bold_font = QVariant(self.bold_font)

    def data(self, role):
        if role == Qt.DisplayRole:
            return QVariant(self.cdata + ' [%d]'%len(self.children))
        elif role == Qt.FontRole:
            return self.bold_font
        elif role == Qt.ForegroundRole and self.category == _('Scheduled'):
            return QVariant(QColor(0, 255, 0))
        return NONE

    def flags(self):
        return Qt.ItemIsEnabled

    def __cmp__(self, other):
        def decorate(x):
            if x == _('Scheduled'):
                x = '0' + x
            elif x == _('Custom'):
                x = '1' + x
                x = '2' + x
            return x

        return cmp(decorate(self.cdata), decorate(getattr(other, 'cdata', '')))

class NewsItem(NewsTreeItem):

    def __init__(self, urn, title, default_icon, custom_icon,
            builtin, custom, scheduler_config, parent):
        NewsTreeItem.__init__(self, builtin, custom, scheduler_config, parent)
        self.urn, self.title = urn, title
        if 'custom:' in self.urn:
            self.icon = custom_icon
            icon = I('news/%s.png'%self.urn[8:])
            if os.path.exists(icon):
                self.icon = QVariant(QIcon(icon))
                self.icon = default_icon

    def data(self, role):
        if role == Qt.DisplayRole:
            return QVariant(self.title)
        if role == Qt.DecorationRole:
            return self.icon
        return NONE

    def __cmp__(self, other):
        return cmp(self.title, getattr(other, 'title', ''))

class RecipeModel(QAbstractItemModel, SearchQueryParser):

    LOCATIONS = ['all']

    def __init__(self, db, *args):
        QAbstractItemModel.__init__(self, *args)
        self.db = db
        self.default_icon = QVariant(QIcon(I('news.svg')))
        self.custom_icon = QVariant(QIcon(I('user_profile.svg')))
        self.builtin_recipe_collection = get_builtin_recipe_collection()
        self.scheduler_config = SchedulerConfig()

    def get_builtin_recipe(self, urn, download=True):
        if download:
                return download_builtin_recipe(urn)
                import traceback
        return P('recipes/%s.recipe'%urn, data=True)

    def get_recipe(self, urn, download=True):
        coll = self.custom_recipe_collection if urn.startswith('custom:') \
                else self.builtin_recipe_collection
        for recipe in coll:
            if recipe.get('id', False) == urn:
                if coll is self.builtin_recipe_collection:
                    return self.get_builtin_recipe(urn[8:], download=download)
                return self.db.get_feed(int(urn[len('custom:'):]))

    def update_custom_recipe(self, urn, title, script):
        self.db.update_feed(int(urn[len('custom:'):]), script, title)
        self.custom_recipe_collection = get_custom_recipe_collection(self.db)

    def add_custom_recipe(self, title, script):
        self.db.add_feed(title, script)
        self.custom_recipe_collection = get_custom_recipe_collection(self.db)

    def remove_custom_recipes(self, urns):
        ids = [int(x[len('custom:'):]) for x in urns]
        self.custom_recipe_collection = get_custom_recipe_collection(self.db)

    def do_refresh(self, restrict_to_urns=set([])):
        self.custom_recipe_collection = get_custom_recipe_collection(self.db)

        def factory(cls, parent, *args):
            args = list(args)
            if cls is NewsItem:
                args.extend([self.default_icon, self.custom_icon])
            args += [self.builtin_recipe_collection,
                     self.custom_recipe_collection, self.scheduler_config,
            return cls(*args)

        def ok(urn):
            if restrict_to_urns is None:
                return False
            return not restrict_to_urns or urn in restrict_to_urns

        new_root = factory(NewsTreeItem, None)
        scheduled = factory(NewsCategory, new_root, _('Scheduled'))
        custom = factory(NewsCategory, new_root, _('Custom'))
        lang_map = {}
        self.all_urns = set([])
        self.showing_count = 0
        for x in self.scheduler_config.iter_recipes():
            urn = x.get('id')
            if ok(urn):
                factory(NewsItem, scheduled, urn, x.get('title'))
        for x in self.custom_recipe_collection:
            urn = x.get('id')
            if ok(urn):
                factory(NewsItem, custom, urn, x.get('title'))
                self.showing_count += 1
        for x in self.builtin_recipe_collection:
            urn = x.get('id')
            if ok(urn):
                lang = x.get('language', 'und')
                if lang not in lang_map:
                    lang_map[lang] = factory(NewsCategory, new_root, lang)
                factory(NewsItem, lang_map[lang], urn, x.get('title'))
                self.showing_count += 1
        self.root = new_root

    def recipe_from_urn(self, urn):
        coll = self.custom_recipe_collection if 'custom:' in urn else \
        for x in coll:
            if x.get('id', None) == urn:
                return copy.deepcopy(x)

    def schedule_info_from_urn(self, urn):
        return self.scheduler_config.get_schedule_info(urn)

    def account_info_from_urn(self, urn):
        return self.scheduler_config.get_account_info(urn)

    def universal_set(self):
        return self.all_urns

    def get_customize_info(self, urn):
        return self.scheduler_config.get_customize_info(urn)

    def get_matches(self, location, query):
        query = query.strip().lower()
        if not query:
            return self.universal_set()
        results = set([])
        for urn in self.universal_set():
            recipe = self.recipe_from_urn(urn)
            if query in recipe.get('title', '').lower() or \
                    query in recipe.get('description', '').lower():
        return results

    def search(self, query, refinement):
            results = self.parse(unicode(query))
            if not results:
                results = None
        except ParseException:
            results = []
        self.emit(SIGNAL('searched(PyQt_PyObject)'), True)

    def columnCount(self, parent):
        return 1

    def data(self, index, role):
        if not index.isValid():
            return NONE
        item = index.internalPointer()
        return item.data(role)

    def headerData(self, *args):
        return NONE

    def flags(self, index):
        if not index.isValid():
            return Qt.ItemIsEnabled|Qt.ItemIsSelectable
        item = index.internalPointer()
        return item.flags()

    def resort(self):

    def index(self, row, column, parent):
        if not self.hasIndex(row, column, parent):
            return QModelIndex()

        if not parent.isValid():
            parent_item = self.root
            parent_item = parent.internalPointer()

            child_item = parent_item.children[row]
        except IndexError:
            return QModelIndex()

        ans = self.createIndex(row, column, child_item)
        return ans

    def parent(self, index):
        if not index.isValid():
            return QModelIndex()

        child_item = index.internalPointer()
        parent_item = child_item.parent

        if parent_item is self.root or parent_item is None:
            return QModelIndex()

        ans = self.createIndex(parent_item.row(), 0, parent_item)
        return ans

    def rowCount(self, parent):
        if parent.column() > 0:
            return 0

        if not parent.isValid():
            parent_item = self.root
            parent_item = parent.internalPointer()

        return len(parent_item.children)

    def update_recipe_schedule(self, urn, schedule_type, schedule,
            add_title_tag=True, custom_tags=[]):
        recipe = self.recipe_from_urn(urn)
        self.scheduler_config.schedule_recipe(recipe, schedule_type, schedule,
                add_title_tag=add_title_tag, custom_tags=custom_tags)

    def update_last_downloaded(self, urn):

    def set_account_info(self, urn, un, pw):
        self.scheduler_config.set_account_info(urn, un, pw)

    def get_account_info(self, urn):
        return self.scheduler_config.get_account_info(urn)

    def get_schedule_info(self, urn):
        return self.scheduler_config.get_schedule_info(urn)

    def un_schedule_recipe(self, urn):

    def schedule_recipe(self, urn, sched_type, schedule):
                sched_type, schedule)

    def customize_recipe(self, urn, add_title_tag, custom_tags):
        self.scheduler_config.customize_recipe(urn, add_title_tag,

    def get_to_be_downloaded_recipes(self):
        ans = self.scheduler_config.get_to_be_downloaded_recipes()
        ans2 = [x for x in ans if self.get_recipe(x, download=False) is not None]
        for x in set(ans) - set(ans2):
        return ans2

    def scheduled_urns(self):
        ans = []
        with self.scheduler_config.lock:
            for recipe in self.scheduler_config.iter_recipes():
        return ans

Generated by  Doxygen 1.6.0   Back to index