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

0001 /**
0002  * \file playlistconfig.cpp
0003  * Configuration for playlist dialog.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 16 Sep 2009
0008  *
0009  * Copyright (C) 2009-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 "playlistconfig.h"
0028 #include "isettings.h"
0029 #include "config.h"
0030 
0031 int PlaylistConfig::s_index = -1;
0032 
0033 namespace {
0034 
0035 /** Default file name format list */
0036 const char* defaultFileNameFormats[] = {
0037   "%{artist} - %{album}",
0038  R"(%{artist} - %{"["year"] "}%{album})",
0039   "%{album}",
0040   "playlist_%{artist}_-_%{album}",
0041   "playlist",
0042   nullptr
0043 };
0044 
0045 }
0046 
0047 /**
0048  * Constructor.
0049  */
0050 PlaylistConfig::PlaylistConfig()
0051   : StoredConfig(QLatin1String("Playlist")),
0052     m_location(PL_CurrentDirectory), m_format(PF_M3U),
0053     m_fileNameFormat(QLatin1String(defaultFileNameFormats[0])),
0054     m_sortTagField(QLatin1String("%{track.3}")),
0055     m_infoFormat(QLatin1String("%{artist} - %{title}")),
0056     m_useFileNameFormat(false),
0057     m_onlySelectedFiles(false),
0058     m_useSortTagField(false), m_useFullPath(false), m_writeInfo(false)
0059 {
0060   initFormatListsIfEmpty();
0061 }
0062 
0063 /**
0064  * Copy constructor.
0065  * @param other instance to be copied
0066  */
0067 PlaylistConfig::PlaylistConfig(const PlaylistConfig& other)
0068   : StoredConfig(QLatin1String("Playlist")),
0069     m_location(other.m_location),
0070     m_format(other.m_format),
0071     m_fileNameFormat(other.m_fileNameFormat),
0072     m_fileNameFormatItems(other.m_fileNameFormatItems),
0073     m_sortTagField(other.m_sortTagField),
0074     m_infoFormat(other.m_infoFormat),
0075     m_useFileNameFormat(other.m_useFileNameFormat),
0076     m_onlySelectedFiles(other.m_onlySelectedFiles),
0077     m_useSortTagField(other.m_useSortTagField),
0078     m_useFullPath(other.m_useFullPath),
0079     m_writeInfo(other.m_writeInfo)
0080 {
0081 }
0082 
0083 /**
0084  * Assignment operator.
0085  * @param other instance to be copied
0086  * @return reference to this instance.
0087  */
0088 PlaylistConfig& PlaylistConfig::operator=(const PlaylistConfig& other)
0089 {
0090   if (&other != this) {
0091     m_location = other.m_location;
0092     m_format = other.m_format;
0093     m_fileNameFormat = other.m_fileNameFormat;
0094     m_fileNameFormatItems = other.m_fileNameFormatItems;
0095     m_sortTagField = other.m_sortTagField;
0096     m_infoFormat = other.m_infoFormat;
0097     m_useFileNameFormat = other.m_useFileNameFormat;
0098     m_onlySelectedFiles = other.m_onlySelectedFiles;
0099     m_useSortTagField = other.m_useSortTagField;
0100     m_useFullPath = other.m_useFullPath;
0101     m_writeInfo = other.m_writeInfo;
0102   }
0103   return *this;
0104 }
0105 
0106 /**
0107  * Persist configuration.
0108  *
0109  * @param config configuration
0110  */
0111 void PlaylistConfig::writeToConfig(ISettings* config) const
0112 {
0113   config->beginGroup(m_group);
0114   config->setValue(QLatin1String("UseFileNameFormat"),
0115                    QVariant(m_useFileNameFormat));
0116   config->setValue(QLatin1String("OnlySelectedFiles"),
0117                    QVariant(m_onlySelectedFiles));
0118   config->setValue(QLatin1String("UseSortTagField"),
0119                    QVariant(m_useSortTagField));
0120   config->setValue(QLatin1String("UseFullPath"), QVariant(m_useFullPath));
0121   config->setValue(QLatin1String("WriteInfo"), QVariant(m_writeInfo));
0122   config->setValue(QLatin1String("Location"),
0123                    QVariant(static_cast<int>(m_location)));
0124   config->setValue(QLatin1String("Format"),
0125                    QVariant(static_cast<int>(m_format)));
0126   config->setValue(QLatin1String("FileNameFormat"), QVariant(m_fileNameFormat));
0127   config->setValue(QLatin1String("FileNameFormatItems"), QVariant(m_fileNameFormatItems));
0128   config->setValue(QLatin1String("SortTagField"), QVariant(m_sortTagField));
0129   config->setValue(QLatin1String("InfoFormat"), QVariant(m_infoFormat));
0130   config->endGroup();
0131   config->beginGroup(m_group, true);
0132   config->setValue(QLatin1String("WindowGeometry"), QVariant(m_windowGeometry));
0133   config->endGroup();
0134 }
0135 
0136 /**
0137  * Read persisted configuration.
0138  *
0139  * @param config configuration
0140  */
0141 void PlaylistConfig::readFromConfig(ISettings* config)
0142 {
0143   config->beginGroup(m_group);
0144   m_useFileNameFormat = config->value(QLatin1String("UseFileNameFormat"),
0145                                       m_useFileNameFormat).toBool();
0146   m_onlySelectedFiles = config->value(QLatin1String("OnlySelectedFiles"),
0147                                       m_onlySelectedFiles).toBool();
0148   m_useSortTagField = config->value(QLatin1String("UseSortTagField"),
0149                                     m_useSortTagField).toBool();
0150   m_useFullPath = config->value(QLatin1String("UseFullPath"),
0151                                 m_useFullPath).toBool();
0152   m_writeInfo = config->value(QLatin1String("WriteInfo"), m_writeInfo).toBool();
0153   m_location = static_cast<PlaylistLocation>(
0154         config->value(QLatin1String("Location"),
0155                       static_cast<int>(m_location)).toInt());
0156   m_format = static_cast<PlaylistFormat>(config->value(QLatin1String("Format"),
0157     static_cast<int>(m_format)).toInt());
0158   m_fileNameFormat = config->value(QLatin1String("FileNameFormat"),
0159                                    m_fileNameFormat).toString();
0160   m_fileNameFormatItems = config->value(QLatin1String("FileNameFormatItems"),
0161                                         m_fileNameFormatItems).toStringList();
0162   m_sortTagField = config->value(QLatin1String("SortTagField"),
0163                                  m_sortTagField).toString();
0164   m_infoFormat = config->value(QLatin1String("InfoFormat"),
0165                                m_infoFormat).toString();
0166   config->endGroup();
0167   config->beginGroup(m_group, true);
0168   m_windowGeometry = config->value(QLatin1String("WindowGeometry"),
0169                                    m_windowGeometry).toByteArray();
0170   config->endGroup();
0171 
0172   initFormatListsIfEmpty();
0173 }
0174 
0175 void PlaylistConfig::initFormatListsIfEmpty()
0176 {
0177   if (m_fileNameFormatItems.size() <= 1) {
0178     for (const char** sl = defaultFileNameFormats; *sl != nullptr; ++sl) {
0179       m_fileNameFormatItems += QString::fromLatin1(*sl);
0180     }
0181   }
0182 }
0183 
0184 
0185 void PlaylistConfig::setLocation(PlaylistLocation location)
0186 {
0187   if (m_location != location) {
0188     m_location = location;
0189     emit locationChanged(m_location);
0190   }
0191 }
0192 
0193 void PlaylistConfig::setFormat(PlaylistFormat format)
0194 {
0195   if (m_format != format) {
0196     m_format = format;
0197     emit formatChanged(m_format);
0198   }
0199 }
0200 
0201 void PlaylistConfig::setFileNameFormat(const QString& fileNameFormat)
0202 {
0203   if (m_fileNameFormat != fileNameFormat) {
0204     m_fileNameFormat = fileNameFormat;
0205     emit fileNameFormatChanged(m_fileNameFormat);
0206   }
0207 }
0208 
0209 void PlaylistConfig::setFileNameFormats(const QStringList& fileNameFormatItems)
0210 {
0211   if (m_fileNameFormatItems != fileNameFormatItems) {
0212     m_fileNameFormatItems = fileNameFormatItems;
0213     m_fileNameFormatItems.removeDuplicates();
0214     emit fileNameFormatsChanged(m_fileNameFormatItems);
0215   }
0216 }
0217 
0218 void PlaylistConfig::setSortTagField(const QString& sortTagField)
0219 {
0220   if (m_sortTagField != sortTagField) {
0221     m_sortTagField = sortTagField;
0222     emit sortTagFieldChanged(m_sortTagField);
0223   }
0224 }
0225 
0226 void PlaylistConfig::setInfoFormat(const QString& infoFormat)
0227 {
0228   if (m_infoFormat != infoFormat) {
0229     m_infoFormat = infoFormat;
0230     emit infoFormatChanged(m_infoFormat);
0231   }
0232 }
0233 
0234 void PlaylistConfig::setUseFileNameFormat(bool useFileNameFormat)
0235 {
0236   if (m_useFileNameFormat != useFileNameFormat) {
0237     m_useFileNameFormat = useFileNameFormat;
0238     emit useFileNameFormatChanged(m_useFileNameFormat);
0239   }
0240 }
0241 
0242 void PlaylistConfig::setOnlySelectedFiles(bool onlySelectedFiles)
0243 {
0244   if (m_onlySelectedFiles != onlySelectedFiles) {
0245     m_onlySelectedFiles = onlySelectedFiles;
0246     emit onlySelectedFilesChanged(m_onlySelectedFiles);
0247   }
0248 }
0249 
0250 void PlaylistConfig::setUseSortTagField(bool useSortTagField)
0251 {
0252   if (m_useSortTagField != useSortTagField) {
0253     m_useSortTagField = useSortTagField;
0254     emit useSortTagFieldChanged(m_useSortTagField);
0255   }
0256 }
0257 
0258 void PlaylistConfig::setUseFullPath(bool useFullPath)
0259 {
0260   if (m_useFullPath != useFullPath) {
0261     m_useFullPath = useFullPath;
0262     emit useFullPathChanged(m_useFullPath);
0263   }
0264 }
0265 
0266 void PlaylistConfig::setWriteInfo(bool writeInfo)
0267 {
0268   if (m_writeInfo != writeInfo) {
0269     m_writeInfo = writeInfo;
0270     emit writeInfoChanged(m_writeInfo);
0271   }
0272 }
0273 
0274 void PlaylistConfig::setWindowGeometry(const QByteArray& windowGeometry)
0275 {
0276   if (m_windowGeometry != windowGeometry) {
0277     m_windowGeometry = windowGeometry;
0278     emit windowGeometryChanged(m_windowGeometry);
0279   }
0280 }
0281 
0282 QString PlaylistConfig::fileExtensionForFormat() const
0283 {
0284   switch (m_format) {
0285   case PlaylistConfig::PF_M3U:
0286     return QLatin1String(".m3u");
0287   case PlaylistConfig::PF_PLS:
0288     return QLatin1String(".pls");
0289   case PlaylistConfig::PF_XSPF:
0290     return QLatin1String(".xspf");
0291   }
0292   return QString();
0293 }
0294 
0295 PlaylistConfig::PlaylistFormat PlaylistConfig::formatFromFileExtension(
0296     const QString& path, bool* ok)
0297 {
0298   PlaylistFormat format = PlaylistConfig::PF_M3U;
0299   bool unknown = false;
0300   if (path.endsWith(QLatin1String(".m3u"), Qt::CaseInsensitive)) {
0301     format = PlaylistConfig::PF_M3U;
0302   } else if (path.endsWith(QLatin1String(".pls"), Qt::CaseInsensitive)) {
0303     format = PlaylistConfig::PF_PLS;
0304   } else if (path.endsWith(QLatin1String(".xspf"), Qt::CaseInsensitive)) {
0305     format = PlaylistConfig::PF_XSPF;
0306   } else {
0307     unknown = true;
0308   }
0309   if (ok) {
0310     *ok = !unknown;
0311   }
0312   return format;
0313 }