File indexing completed on 2024-05-19 04:55:54

0001 /**
0002  * \file filterconfig.cpp
0003  * Configuration for filter dialog.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 16 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 #include "filterconfig.h"
0028 #include <QString>
0029 #include "isettings.h"
0030 #include "config.h"
0031 
0032 int FilterConfig::s_index = -1;
0033 
0034 /**
0035  * Constructor.
0036  */
0037 FilterConfig::FilterConfig()
0038   : StoredConfig(QLatin1String("Filter")), m_filterIdx(0)
0039 {
0040   /**
0041    * Preset filter expressions.
0042    */
0043   m_filterNames <<
0044     QLatin1String("All") <<
0045     QLatin1String("Filename Tag Mismatch") <<
0046     QLatin1String("No Tag 1") <<
0047     QLatin1String("No Tag 2") <<
0048     QLatin1String("ID3v2.2.0 Tag") <<
0049     QLatin1String("ID3v2.3.0 Tag") <<
0050     QLatin1String("ID3v2.4.0 Tag") <<
0051     QLatin1String("Tag 1 != Tag 2") <<
0052     QLatin1String("Tag 1 == Tag 2") <<
0053     QLatin1String("Incomplete") <<
0054     QLatin1String("No Picture") <<
0055     QLatin1String("Marked") <<
0056     QLatin1String("Custom Filter");
0057   m_filterExpressions <<
0058     QLatin1String("") <<
0059     QLatin1String("not (%{filepath} contains "
0060                   "\"%{artist} - %{album}/%{track} %{title}\")") <<
0061     QLatin1String("%{tag1} equals \"\"") <<
0062     QLatin1String("%{tag2} equals \"\"") <<
0063     QLatin1String("%{tag2} equals \"ID3v2.2.0\"") <<
0064     QLatin1String("%{tag2} equals \"ID3v2.3.0\"") <<
0065     QLatin1String("%{tag2} equals \"ID3v2.4.0\"") <<
0066     QLatin1String("not (%1{title} equals %2{title} and "
0067                   "%1{album} equals %2{album} and "
0068                   "%1{artist} equals %2{artist} and "
0069                   "%1{comment} equals %2{comment} and "
0070                   "%1{year} equals %2{year} and "
0071                   "%1{track} equals %2{track} and "
0072                   "%1{genre} equals %2{genre})") <<
0073     QLatin1String("%1{title} equals %2{title} and "
0074                   "%1{album} equals %2{album} and "
0075                   "%1{artist} equals %2{artist} and "
0076                   "%1{comment} equals %2{comment} and "
0077                   "%1{year} equals %2{year} and %1{track} equals %2{track} and "
0078                   "%1{genre} equals %2{genre}") <<
0079     QLatin1String(R"(%{title} equals "" or %{artist} equals "" or )"
0080                   R"(%{album} equals "" or %{year} equals "" or )"
0081                   R"(%{tracknumber} equals "" or %{genre} equals "")") <<
0082     QLatin1String("%{picture} equals \"\"") <<
0083     QLatin1String("not (%{marked} equals \"\")") <<
0084     QLatin1String("");
0085 }
0086 
0087 /**
0088  * Persist configuration.
0089  *
0090  * @param config configuration
0091  */
0092 void FilterConfig::writeToConfig(ISettings* config) const
0093 {
0094   config->beginGroup(m_group);
0095   config->setValue(QLatin1String("FilterNames"), QVariant(m_filterNames));
0096   config->setValue(QLatin1String("FilterExpressions"), QVariant(m_filterExpressions));
0097   config->setValue(QLatin1String("FilterIdx"), QVariant(m_filterIdx));
0098   config->endGroup();
0099   config->beginGroup(m_group, true);
0100   config->setValue(QLatin1String("WindowGeometry"), QVariant(m_windowGeometry));
0101   config->endGroup();
0102 }
0103 
0104 /**
0105  * Read persisted configuration.
0106  *
0107  * @param config configuration
0108  */
0109 void FilterConfig::readFromConfig(ISettings* config)
0110 {
0111   config->beginGroup(m_group);
0112   QStringList names = config->value(QLatin1String("FilterNames"),
0113                                     m_filterNames).toStringList();
0114   QStringList expressions = config->value(QLatin1String("FilterExpressions"),
0115                                           m_filterExpressions).toStringList();
0116   m_filterIdx = config->value(QLatin1String("FilterIdx"), m_filterIdx).toInt();
0117   config->endGroup();
0118   config->beginGroup(m_group, true);
0119   m_windowGeometry = config->value(QLatin1String("WindowGeometry"),
0120                                    m_windowGeometry).toByteArray();
0121 
0122   config->endGroup();
0123 
0124   // KConfig seems to strip empty entries from the end of the string lists,
0125   // so we have to append them again.
0126   const int numNames = names.size();
0127   while (expressions.size() < numNames)
0128     expressions.append(QLatin1String(""));
0129 
0130   /* Use defaults if no configuration found */
0131   for (auto namesIt = names.constBegin(), expressionsIt = expressions.constBegin();
0132        namesIt != names.constEnd() && expressionsIt != expressions.constEnd();
0133        ++namesIt, ++expressionsIt) {
0134     if (int idx = m_filterNames.indexOf(*namesIt); idx >= 0) {
0135       m_filterExpressions[idx] = *expressionsIt;
0136     } else if (!namesIt->isEmpty()) {
0137       m_filterNames.append(*namesIt);
0138       m_filterExpressions.append(*expressionsIt);
0139     }
0140   }
0141 
0142   if (m_filterIdx >= m_filterNames.size())
0143     m_filterIdx = 0;
0144 }
0145 
0146 /**
0147  * Set the filename format in the "Filename Tag Mismatch" filter.
0148  *
0149  * @param format filename format
0150  */
0151 void FilterConfig::setFilenameFormat(const QString& format)
0152 {
0153   if (int idx = m_filterNames.indexOf(QLatin1String("Filename Tag Mismatch"));
0154       idx != -1) {
0155     m_filterExpressions[idx] = QLatin1String("not (%{filepath} contains \"") +
0156       format + QLatin1String("\")");
0157   }
0158 }
0159 
0160 void FilterConfig::setFilterNames(const QStringList& filterNames)
0161 {
0162   if (m_filterNames != filterNames) {
0163     m_filterNames = filterNames;
0164     emit filterNamesChanged(m_filterNames);
0165   }
0166 }
0167 
0168 void FilterConfig::setFilterExpressions(const QStringList& filterExpressions)
0169 {
0170   if (m_filterExpressions != filterExpressions) {
0171     m_filterExpressions = filterExpressions;
0172     emit filterExpressionsChanged(m_filterExpressions);
0173   }
0174 }
0175 
0176 void FilterConfig::setFilterIndex(int filterIndex)
0177 {
0178   if (m_filterIdx != filterIndex) {
0179     m_filterIdx = filterIndex;
0180     emit filterIndexChanged(m_filterIdx);
0181   }
0182 }
0183 
0184 void FilterConfig::setWindowGeometry(const QByteArray& windowGeometry)
0185 {
0186   if (m_windowGeometry != windowGeometry) {
0187     m_windowGeometry = windowGeometry;
0188     emit windowGeometryChanged(m_windowGeometry);
0189   }
0190 }