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

0001 /**
0002  * \file rendirconfig.cpp
0003  * Configuration for directory renaming.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 29 Jun 2013
0008  *
0009  * Copyright (C) 2013-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 "rendirconfig.h"
0028 #include "isettings.h"
0029 
0030 namespace {
0031 
0032 /** Default directory format list */
0033 const char* dirFmt[] = {
0034   "%{artist} - %{album}",
0035   R"(%{artist} - %{"["year"] "}%{album})",
0036   "%{artist} - [%{max-year}] %{album}",
0037   "%{artist} - %{album}%{\" (\"year\")\"}",
0038   "%{artist}/%{album}",
0039   R"(%{artist}/%{"["year"] "}%{album})",
0040   "%{album}",
0041   R"(%{"["year"] "}%{album})",
0042   nullptr            // end of StrList
0043 };
0044 
0045 
0046 /**
0047  * Convert tag version to rename directory value in configuration.
0048  * @param tagVersion tag version
0049  * @return value used in configuration, kept for backwards compatibility.
0050  */
0051 int tagVersionToRenDirCfg(Frame::TagVersion tagVersion) {
0052   auto renDirSrc = static_cast<int>(tagVersion);
0053   if (renDirSrc == 3)
0054     renDirSrc = 0;
0055   return renDirSrc;
0056 }
0057 
0058 /**
0059  * Convert rename directory value in configuration to tag version.
0060  * @param renDirSrc value used in configuration, kept for backwards
0061  *                  compatibility.
0062  * @return tag version.
0063  */
0064 Frame::TagVersion renDirCfgToTagVersion(int renDirSrc) {
0065   if (renDirSrc == 0)
0066     renDirSrc = 3;
0067   return Frame::tagVersionCast(renDirSrc);
0068 }
0069 
0070 }
0071 
0072 /** Default directory format list */
0073 const char** RenDirConfig::s_defaultDirFmtList = &dirFmt[0];
0074 int RenDirConfig::s_index = -1;
0075 
0076 /**
0077  * Constructor.
0078  */
0079 RenDirConfig::RenDirConfig()
0080   : StoredConfig(QLatin1String("RenameDirectory")),
0081     m_dirFormatText(QString::fromLatin1(s_defaultDirFmtList[0])),
0082     m_renDirSrc(Frame::TagVAll)
0083 {
0084 }
0085 
0086 /**
0087  * Persist configuration.
0088  *
0089  * @param config configuration
0090  */
0091 void RenDirConfig::writeToConfig(ISettings* config) const
0092 {
0093   config->beginGroup(m_group);
0094   config->setValue(QLatin1String("DirFormatItems"), QVariant(m_dirFormatItems));
0095   config->setValue(QLatin1String("DirFormatText"), QVariant(m_dirFormatText));
0096   config->setValue(QLatin1String("RenameDirectorySource"),
0097                    QVariant(tagVersionToRenDirCfg(m_renDirSrc)));
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 RenDirConfig::readFromConfig(ISettings* config)
0110 {
0111   config->beginGroup(m_group);
0112   m_dirFormatItems =
0113       config->value(QLatin1String("DirFormatItems"),
0114                     m_dirFormatItems).toStringList();
0115   m_renDirSrc = renDirCfgToTagVersion(
0116         config->value(QLatin1String("RenameDirectorySource"), 0).toInt());
0117   m_dirFormatText =
0118       config->value(QLatin1String("DirFormatText"),
0119                     QString::fromLatin1(s_defaultDirFmtList[0])).toString();
0120   config->endGroup();
0121   config->beginGroup(m_group, true);
0122   m_windowGeometry = config->value(QLatin1String("WindowGeometry"),
0123                                    m_windowGeometry).toByteArray();
0124   config->endGroup();
0125 
0126   if (m_dirFormatItems.size() <= 1) {
0127     for (const char** sl = s_defaultDirFmtList; *sl != nullptr; ++sl) {
0128       m_dirFormatItems += QString::fromLatin1(*sl); // clazy:exclude=reserve-candidates
0129     }
0130   }
0131 }
0132 
0133 void RenDirConfig::setDirFormat(const QString& dirFormat)
0134 {
0135   if (m_dirFormatText != dirFormat) {
0136     m_dirFormatText = dirFormat;
0137     emit dirFormatChanged(m_dirFormatText);
0138   }
0139 }
0140 
0141 void RenDirConfig::setDirFormats(const QStringList& dirFormatItems)
0142 {
0143   if (m_dirFormatItems != dirFormatItems) {
0144     m_dirFormatItems = dirFormatItems;
0145     m_dirFormatItems.removeDuplicates();
0146     emit dirFormatsChanged(m_dirFormatItems);
0147   }
0148 }
0149 
0150 void RenDirConfig::setRenDirSource(Frame::TagVersion renDirSource)
0151 {
0152   if (m_renDirSrc != renDirSource) {
0153     m_renDirSrc = renDirSource;
0154     emit renDirSourceChanged(m_renDirSrc);
0155   }
0156 }
0157 
0158 void RenDirConfig::setWindowGeometry(const QByteArray& windowGeometry)
0159 {
0160   if (m_windowGeometry != windowGeometry) {
0161     m_windowGeometry = windowGeometry;
0162     emit windowGeometryChanged(m_windowGeometry);
0163   }
0164 }