1#
2# Gramps - a GTK+/GNOME based genealogy program
3#
4# Copyright (C) 2002-2006  Donald N. Allingham
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20
21#-------------------------------------------------------------------------
22#
23# Standard Python modules
24#
25#-------------------------------------------------------------------------
26import logging
27LOG = logging.getLogger(".filter")
28
29#-------------------------------------------------------------------------
30#
31# Gramps modules
32#
33#-------------------------------------------------------------------------
34# we need global variableCustomFilters, so we need to query gramps.gen.filters
35# when we need this variable, not import it at the start!
36import gramps.gen.filters
37from . import Rule
38from ...const import GRAMPS_LOCALE as glocale
39_ = glocale.translation.gettext
40
41#-------------------------------------------------------------------------
42#
43# MatchesFilter
44#
45#-------------------------------------------------------------------------
46class MatchesFilterBase(Rule):
47    """
48    Rule that checks against another filter.
49
50    This is a base rule for subclassing by specific objects.
51    Subclasses need to define the namespace class attribute.
52
53    """
54    labels = [_('Filter name:')]
55    name = 'Objects matching the <filter>'
56    description = "Matches objects matched by the specified filter name"
57    category = _('General filters')
58
59    def prepare(self, db, user):
60        if gramps.gen.filters.CustomFilters:
61            filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
62            if self.list[0] in filters:
63                filt = filters[self.list[0]]
64                for rule in filt.flist:
65                    rule.requestprepare(db, user)
66            else:
67                LOG.warning(_("Can't find filter %s in the defined custom filters")
68                                    % self.list[0])
69        else:
70            LOG.warning(_("Can't find filter %s in the defined custom filters")
71                                    % self.list[0])
72
73    def reset(self):
74        if gramps.gen.filters.CustomFilters:
75            filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
76            if self.list[0] in filters:
77                filt = filters[self.list[0]]
78                for rule in filt.flist:
79                    rule.requestreset()
80
81    def apply(self, db, obj):
82        if gramps.gen.filters.CustomFilters:
83            filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
84            if self.list[0] in filters:
85                filt = filters[self.list[0]]
86                return filt.check(db, obj.handle)
87        return False
88
89    def find_filter(self):
90        """
91        Return the selected filter or None.
92        """
93        if gramps.gen.filters.CustomFilters:
94            filters = gramps.gen.filters.CustomFilters.get_filters_dict(self.namespace)
95            if self.list[0] in filters:
96                return filters[self.list[0]]
97        return None
98