File indexing completed on 2024-04-21 16:29:35

0001 /***************************************************************************
0002  *   Copyright (C) 2012 by Daniel Nicoletti                                *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "CategoryMatcher.h"
0022 
0023 CategoryMatcher::CategoryMatcher(Kind kind, const QString &term) :
0024     m_kind(kind),
0025     m_term(term)
0026 {
0027 }
0028 
0029 CategoryMatcher::CategoryMatcher(const CategoryMatcher &other) :
0030     m_kind(other.m_kind),
0031     m_term(other.m_term),
0032     m_child(other.m_child)
0033 {
0034 }
0035 
0036 CategoryMatcher::~CategoryMatcher()
0037 {
0038 }
0039 
0040 CategoryMatcher &CategoryMatcher::operator =(const CategoryMatcher &other)
0041 {
0042     m_kind = other.m_kind;
0043     m_term = other.m_term;
0044     m_child = other.m_child;
0045     return *this;
0046 }
0047 
0048 bool CategoryMatcher::match(const QStringList &categories) const
0049 {
0050     if (categories.isEmpty()) {
0051         return false;
0052     }
0053 
0054     bool ret = false;
0055     switch (m_kind) {
0056     case Term:
0057         ret = categories.contains(m_term);
0058         break;
0059     case And:
0060         for (const CategoryMatcher &parser : m_child) {
0061             if (!(ret = parser.match(categories))) {
0062                 break;
0063             }
0064         }
0065         break;
0066     case Or:
0067         for (const CategoryMatcher &parser : m_child) {
0068             if ((ret = parser.match(categories))) {
0069                 break;
0070             }
0071         }
0072         break;
0073     case Not:
0074         // We match like And but negating
0075         for (const CategoryMatcher &parser : m_child) {
0076             if (!(ret = !parser.match(categories))) {
0077                 break;
0078             }
0079         }
0080         break;
0081     }
0082     return ret;
0083 }
0084 
0085 void CategoryMatcher::setChild(const QList<CategoryMatcher> &child)
0086 {
0087     m_child = child;
0088 }
0089 
0090 QList<CategoryMatcher> CategoryMatcher::child() const
0091 {
0092     return m_child;
0093 }
0094 
0095 QString CategoryMatcher::term() const
0096 {
0097     return m_term;
0098 }
0099 
0100 CategoryMatcher::Kind CategoryMatcher::kind() const
0101 {
0102     return m_kind;
0103 }