File indexing completed on 2024-05-19 04:56:01

0001 /**
0002  * \file filefilter.h
0003  * Filter for tagged files.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 19 Jan 2008
0008  *
0009  * Copyright (C) 2008-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include "expressionparser.h"
0030 #include "trackdata.h"
0031 #include "iabortable.h"
0032 #include <QObject>
0033 #include <QString>
0034 
0035 class TaggedFile;
0036 
0037 /**
0038  * Filter for tagged files.
0039  */
0040 class KID3_CORE_EXPORT FileFilter : public QObject, public IAbortable {
0041   Q_OBJECT
0042   Q_ENUMS(FilterEventType)
0043 public:
0044   /** Type of filter event. */
0045   enum FilterEventType {
0046     Started, Directory, ParseError, FilePassed, FileFilteredOut,
0047     Finished, Aborted
0048   };
0049 
0050   /**
0051    * Constructor.
0052    * @param parent parent object
0053    */
0054   explicit FileFilter(QObject* parent = nullptr);
0055 
0056   /**
0057    * Destructor.
0058    */
0059   ~FileFilter() override = default;
0060 
0061   /**
0062    * Set filter expression.
0063    * @param filterExpression filter expression
0064    */
0065   void setFilterExpression(const QString& filterExpression) {
0066     m_filterExpression = filterExpression;
0067   }
0068 
0069   /**
0070    * Check if filter expression is empty.
0071    * @return true if filter expression is empty.
0072    */
0073   bool isEmptyFilterExpression() const { return m_filterExpression.isEmpty(); }
0074 
0075   /**
0076    * Initialize the parser.
0077    * This method has to be called before the first call to parse()
0078    * and afterwards when the expression has been changed.
0079    */
0080   void initParser();
0081 
0082   /**
0083    * Check if file passes through filter.
0084    *
0085    * @param taggedFile file to check
0086    * @param ok         if not 0, false is returned here when parsing fails
0087    *
0088    * @return true if file passes through filter.
0089    */
0090   bool filter(TaggedFile& taggedFile, bool* ok = nullptr);
0091 
0092   /**
0093    * Clear abort flag.
0094    */
0095   void clearAborted() override;
0096 
0097   /**
0098    * Check if dialog was aborted.
0099    * @return true if aborted.
0100    */
0101   bool isAborted() const override;
0102 
0103   /**
0104    * Get help text for format codes supported by formatString().
0105    *
0106    * @param onlyRows if true only the tr elements are returned,
0107    *                 not the surrounding table
0108    *
0109    * @return help text.
0110    */
0111   static QString getFormatToolTip(bool onlyRows = false);
0112 
0113 public slots:
0114   /**
0115    * Set abort flag.
0116    */
0117   void abort() override;
0118 
0119 private:
0120   /**
0121    * Format a string from tag data.
0122    *
0123    * @param format format specification
0124    *
0125    * @return formatted string.
0126    */
0127   QString formatString(const QString& format) const;
0128 
0129   /**
0130    * Evaluate the expression to a boolean result.
0131    * @see initParser()
0132    * @return result of expression.
0133    */
0134   bool parse();
0135 
0136   QString m_filterExpression;
0137   ExpressionParser m_parser;
0138   ImportTrackData m_trackData1;
0139   ImportTrackData m_trackData2;
0140   ImportTrackData m_trackData12;
0141   bool m_aborted;
0142 };