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

0001 /**
0002  * \file batchimportconfig.cpp
0003  * Configuration for batch import.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 3 Jan 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 "batchimportconfig.h"
0028 #include <QString>
0029 #include "batchimportprofile.h"
0030 #include "isettings.h"
0031 #include "config.h"
0032 
0033 int BatchImportConfig::s_index = -1;
0034 
0035 /**
0036  * Constructor.
0037  */
0038 BatchImportConfig::BatchImportConfig()
0039   : StoredConfig(QLatin1String("BatchImport")),
0040     m_importDest(Frame::TagV2), m_profileIdx(0)
0041 {
0042   /**
0043    * Preset profile expressions.
0044    */
0045   m_profileNames <<
0046     QLatin1String("All") <<
0047     QLatin1String("MusicBrainz") <<
0048     QLatin1String("Discogs") <<
0049     QLatin1String("Cover Art") <<
0050     QLatin1String("Custom Profile");
0051   m_profileSources <<
0052     QLatin1String("MusicBrainz Release:75:SAC;Discogs:75:SAC;Amazon:75:SAC;"
0053                   "gnudb.org:75:S") <<
0054     QLatin1String("MusicBrainz Release:75:SAC") <<
0055     QLatin1String("Discogs:75:SAC") <<
0056     QLatin1String("Amazon:75:C;Discogs:75:C;MusicBrainz Release:75:C") <<
0057     QLatin1String("");
0058 }
0059 
0060 /**
0061  * Persist configuration.
0062  *
0063  * @param config configuration
0064  */
0065 void BatchImportConfig::writeToConfig(ISettings* config) const
0066 {
0067   config->beginGroup(m_group);
0068   config->setValue(QLatin1String("ImportDestination"), QVariant(m_importDest));
0069   config->setValue(QLatin1String("ProfileNames"), QVariant(m_profileNames));
0070   config->setValue(QLatin1String("ProfileSources"), QVariant(m_profileSources));
0071   config->setValue(QLatin1String("ProfileIdx"), QVariant(m_profileIdx));
0072   config->endGroup();
0073   config->beginGroup(m_group, true);
0074   config->setValue(QLatin1String("WindowGeometry"), QVariant(m_windowGeometry));
0075   config->endGroup();
0076 }
0077 
0078 /**
0079  * Read persisted configuration.
0080  *
0081  * @param config configuration
0082  */
0083 void BatchImportConfig::readFromConfig(ISettings* config)
0084 {
0085   config->beginGroup(m_group);
0086   m_importDest = Frame::tagVersionCast(
0087         config->value(QLatin1String("ImportDestination"), m_importDest).toInt());
0088   QStringList names = config->value(QLatin1String("ProfileNames"),
0089                                     m_profileNames).toStringList();
0090   QStringList sources = config->value(QLatin1String("ProfileSources"),
0091                                       m_profileSources).toStringList();
0092   m_profileIdx = config->value(QLatin1String("ProfileIdx"), m_profileIdx).toInt();
0093   config->endGroup();
0094   config->beginGroup(m_group, true);
0095   m_windowGeometry = config->value(QLatin1String("WindowGeometry"),
0096                                    m_windowGeometry).toByteArray();
0097   config->endGroup();
0098 
0099   // KConfig seems to strip empty entries from the end of the string lists,
0100   // so we have to append them again.
0101   const int numNames = names.size();
0102   while (sources.size() < numNames)
0103     sources.append(QLatin1String(""));
0104   /* Use defaults if no configuration found */
0105   for (auto namesIt = names.constBegin(), sourcesIt = sources.constBegin();
0106        namesIt != names.constEnd() && sourcesIt != sources.constEnd();
0107        ++namesIt, ++sourcesIt) {
0108     if (int idx = m_profileNames.indexOf(*namesIt); idx >= 0) {
0109       m_profileSources[idx] = *sourcesIt;
0110     } else if (!namesIt->isEmpty()) {
0111       m_profileNames.append(*namesIt);
0112       m_profileSources.append(*sourcesIt);
0113     }
0114   }
0115 
0116   if (m_profileIdx >= m_profileNames.size())
0117     m_profileIdx = 0;
0118 }
0119 
0120 /**
0121  * Get a batch import profile.
0122  *
0123  * @param name name of profile
0124  * @param profile the profile will be returned here
0125  * @return true if profile with @a name found.
0126  */
0127 bool BatchImportConfig::getProfileByName(const QString& name,
0128                                          BatchImportProfile& profile) const
0129 {
0130   for (auto namesIt = m_profileNames.constBegin(),
0131        sourcesIt = m_profileSources.constBegin();
0132        namesIt != m_profileNames.constEnd() &&
0133        sourcesIt != m_profileSources.constEnd();
0134        ++namesIt, ++sourcesIt) {
0135     if (name == *namesIt) {
0136       profile.setName(*namesIt);
0137       profile.setSourcesFromString(*sourcesIt);
0138       return true;
0139     }
0140   }
0141   return false;
0142 }
0143 
0144 void BatchImportConfig::setImportDest(Frame::TagVersion importDest)
0145 {
0146   if (m_importDest != importDest) {
0147     m_importDest = importDest;
0148     emit importDestChanged(m_importDest);
0149   }
0150 }
0151 
0152 void BatchImportConfig::setWindowGeometry(const QByteArray& windowGeometry)
0153 {
0154   if (m_windowGeometry != windowGeometry) {
0155     m_windowGeometry = windowGeometry;
0156     emit windowGeometryChanged(m_windowGeometry);
0157   }
0158 }
0159 
0160 void BatchImportConfig::setProfileIndex(int profileIdx)
0161 {
0162   if (m_profileIdx != profileIdx) {
0163     m_profileIdx = profileIdx;
0164     emit profileIndexChanged(m_profileIdx);
0165   }
0166 }
0167 
0168 void BatchImportConfig::setProfileSources(const QStringList& profileSources)
0169 {
0170   if (m_profileSources != profileSources) {
0171     m_profileSources = profileSources;
0172     emit profileSourcesChanged(m_profileSources);
0173   }
0174 }
0175 
0176 void BatchImportConfig::setProfileNames(const QStringList& profileNames)
0177 {
0178   if (m_profileNames != profileNames) {
0179     m_profileNames = profileNames;
0180     emit profileNamesChanged(m_profileNames);
0181   }
0182 }