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

0001 /**
0002  * \file fileconfig.cpp
0003  * File related configuration.
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 "fileconfig.h"
0028 #include <QCoreApplication>
0029 #include "isettings.h"
0030 
0031 int FileConfig::s_index = -1;
0032 
0033 namespace {
0034 
0035 /** Default to filename format list */
0036 const char* defaultToFilenameFormats[] = {
0037   "%{track} %{title}",
0038   "%{track}. %{title}",
0039   "%{track} - %{artist} - %{title}",
0040   "%{track}. %{artist} - %{title}",
0041   "%{artist} - %{track} - %{title}",
0042   "%{artist} - %{album} - %{track} - %{title}",
0043  R"(%{artist} - %{"["year"] "}%{album} - %{track} - %{title})",
0044   "%{artist} - %{title}",
0045   "%{artist}-%{title}",
0046   "(%{artist}) %{title}",
0047   "%{artist}-%{title}-%{album}",
0048   nullptr
0049 };
0050 
0051 /** Default from filename format list */
0052 const char* defaultFromFilenameFormats[] = {
0053   "%{artist} - %{album}/%{track} %{title}",
0054   "%{artist} - %{album}/%{track}. %{title}",
0055   "%{artist} - [%{year}] %{album}/%{track} %{title}",
0056   "%{artist} - [%{year}] %{album}/%{track}. %{title}",
0057   "%{artist} - %{album} (%{year})/%{track} - %{title}",
0058   "%{artist}/%{album}/%{track} %{title}",
0059   "%{artist}/%{album}/%{track}. %{title}",
0060   "%{artist}/[%{year}] %{album}/%{track} %{title}",
0061   "%{artist}/[%{year}] %{album}/%{track}. %{title}",
0062   "%{album}/%{track} - %{artist} - %{title}",
0063   "%{album}/%{track}. %{artist} - %{title}",
0064   "%{album}/%{artist} - %{track} - %{title}",
0065   "[%{year}] %{album}/%{track} - %{artist} - %{title}",
0066   "%{artist} - %{album} - %{track} - %{title}",
0067   "%{artist} - [%{year}] %{album} - %{track} - %{title}",
0068   "%{album}/%{artist} - %{track} - %{title}",
0069   "[%{year}] %{album}/%{artist} - %{track} - %{title}",
0070   "%{album}/%{artist} - %{title}",
0071   "%{album}/%{artist}-%{title}",
0072   "%{album}/(%{artist}) %{title}",
0073   "%{artist}-%{title}-%{album}",
0074   nullptr
0075 };
0076 
0077 }
0078 
0079 /**
0080  * Constructor.
0081  */
0082 FileConfig::FileConfig()
0083   : StoredConfig(QLatin1String("Files")),
0084     m_nameFilter(QLatin1String("")),
0085     m_formatText(QString::fromLatin1(defaultToFilenameFormats[0])),
0086     m_formatFromFilenameText(QString::fromLatin1(defaultFromFilenameFormats[0])),
0087     m_defaultCoverFileName(QLatin1String("folder.jpg")),
0088     m_textEncoding(QLatin1String("System")),
0089     m_preserveTime(false),
0090     m_markChanges(true),
0091     m_loadLastOpenedFile(true),
0092     m_showHiddenFiles(false),
0093     m_sortIgnoringPunctuation(false)
0094 {
0095   initFormatListsIfEmpty();
0096 }
0097 
0098 /**
0099  * Persist configuration.
0100  *
0101  * @param config configuration
0102  */
0103 void FileConfig::writeToConfig(ISettings* config) const
0104 {
0105   config->beginGroup(m_group);
0106   config->setValue(QLatin1String("NameFilter"), QVariant(m_nameFilter));
0107   config->setValue(QLatin1String("IncludeFolders"), QVariant(m_includeFolders));
0108   config->setValue(QLatin1String("ExcludeFolders"), QVariant(m_excludeFolders));
0109   config->setValue(QLatin1String("ShowHiddenFiles"), QVariant(m_showHiddenFiles));
0110   config->setValue(QLatin1String("SortIgnoringPunctuation"), QVariant(m_sortIgnoringPunctuation));
0111   config->setValue(QLatin1String("FormatItems"), QVariant(m_formatItems));
0112   config->setValue(QLatin1String("FormatText"), QVariant(m_formatText));
0113   config->setValue(QLatin1String("FormatFromFilenameItems"), QVariant(m_formatFromFilenameItems));
0114   config->setValue(QLatin1String("FormatFromFilenameText"), QVariant(m_formatFromFilenameText));
0115   config->setValue(QLatin1String("PreserveTime"), QVariant(m_preserveTime));
0116   config->setValue(QLatin1String("MarkChanges"), QVariant(m_markChanges));
0117   config->setValue(QLatin1String("LoadLastOpenedFile"), QVariant(m_loadLastOpenedFile));
0118   config->setValue(QLatin1String("TextEncoding"), QVariant(m_textEncoding));
0119   config->setValue(QLatin1String("DefaultCoverFileName"), QVariant(m_defaultCoverFileName));
0120   config->endGroup();
0121   config->beginGroup(m_group, true);
0122   config->setValue(QLatin1String("LastOpenedFile"), QVariant(m_lastOpenedFile));
0123   config->endGroup();
0124 }
0125 
0126 /**
0127  * Read persisted configuration.
0128  *
0129  * @param config configuration
0130  */
0131 void FileConfig::readFromConfig(ISettings* config)
0132 {
0133   config->beginGroup(m_group);
0134   m_nameFilter =
0135       config->value(QLatin1String("NameFilter"), QLatin1String("")).toString();
0136   m_includeFolders =
0137       config->value(QLatin1String("IncludeFolders"),
0138                     m_includeFolders).toStringList();
0139   m_excludeFolders =
0140       config->value(QLatin1String("ExcludeFolders"),
0141                     m_excludeFolders).toStringList();
0142   m_showHiddenFiles = config->value(QLatin1String("ShowHiddenFiles"),
0143                                     m_showHiddenFiles).toBool();
0144   m_sortIgnoringPunctuation = config->value(
0145         QLatin1String("SortIgnoringPunctuation"),
0146         m_sortIgnoringPunctuation).toBool();
0147   m_formatItems =
0148       config->value(QLatin1String("FormatItems"),
0149                     m_formatItems).toStringList();
0150   m_formatFromFilenameItems =
0151       config->value(QLatin1String("FormatFromFilenameItems"),
0152                     m_formatFromFilenameItems).toStringList();
0153   m_preserveTime = config->value(QLatin1String("PreserveTime"),
0154                                  m_preserveTime).toBool();
0155   m_markChanges = config->value(QLatin1String("MarkChanges"),
0156                                 m_markChanges).toBool();
0157 
0158   m_formatText =
0159       config->value(QLatin1String("FormatText"),
0160                     QString::fromLatin1(defaultToFilenameFormats[0])).toString();
0161   m_formatFromFilenameText =
0162       config->value(QLatin1String("FormatFromFilenameText"),
0163                     QString::fromLatin1(defaultFromFilenameFormats[0])).toString();
0164   m_loadLastOpenedFile = config->value(QLatin1String("LoadLastOpenedFile"),
0165                                        m_loadLastOpenedFile).toBool();
0166   m_textEncoding = config->value(QLatin1String("TextEncoding"),
0167                                  QLatin1String("System")).toString();
0168   m_defaultCoverFileName = config->value(QLatin1String("DefaultCoverFileName"),
0169                                          m_defaultCoverFileName).toString();
0170   config->endGroup();
0171   config->beginGroup(m_group, true);
0172   m_lastOpenedFile = config->value(QLatin1String("LastOpenedFile"),
0173                                    m_lastOpenedFile).toString();
0174   config->endGroup();
0175 
0176   initFormatListsIfEmpty();
0177   if (ConfigStore::getConfigVersion() < 4) {
0178     // Reset file name filter if it is set to "All Supported Files" in order
0179     // to introduce newly supported file formats (e.g. *.dsf) when the
0180     // configuration version is increased.
0181     if (m_nameFilter.startsWith(QCoreApplication::translate(
0182                                   "Kid3Application", "All Supported Files"))) {
0183       m_nameFilter.clear();
0184     }
0185   }
0186 }
0187 
0188 void FileConfig::initFormatListsIfEmpty()
0189 {
0190   if (m_formatItems.size() <= 1) {
0191     for (const char** sl = defaultToFilenameFormats; *sl != nullptr; ++sl) {
0192       m_formatItems += QString::fromLatin1(*sl);
0193     }
0194   }
0195   if (m_formatFromFilenameItems.size() <= 1) {
0196     for (const char** sl = defaultFromFilenameFormats; *sl != nullptr; ++sl) {
0197       m_formatFromFilenameItems += QString::fromLatin1(*sl);
0198     }
0199   }
0200 }
0201 
0202 void FileConfig::setNameFilter(const QString& nameFilter)
0203 {
0204   if (m_nameFilter != nameFilter) {
0205     m_nameFilter = nameFilter;
0206     emit nameFilterChanged(m_nameFilter);
0207   }
0208 }
0209 
0210 void FileConfig::setIncludeFolders(const QStringList& includeFolders)
0211 {
0212   if (m_includeFolders != includeFolders) {
0213     m_includeFolders = includeFolders;
0214     emit includeFoldersChanged(m_includeFolders);
0215   }
0216 }
0217 
0218 void FileConfig::setExcludeFolders(const QStringList& excludeFolders)
0219 {
0220   if (m_excludeFolders != excludeFolders) {
0221     m_excludeFolders = excludeFolders;
0222     emit excludeFoldersChanged(m_excludeFolders);
0223   }
0224 }
0225 
0226 void FileConfig::setShowHiddenFiles(bool showHiddenFiles)
0227 {
0228   if (m_showHiddenFiles != showHiddenFiles) {
0229     m_showHiddenFiles = showHiddenFiles;
0230     emit showHiddenFilesChanged(m_showHiddenFiles);
0231   }
0232 }
0233 
0234 void FileConfig::setSortIgnoringPunctuation(bool sortIgnoringPunctuation)
0235 {
0236   if (m_sortIgnoringPunctuation != sortIgnoringPunctuation) {
0237     m_sortIgnoringPunctuation = sortIgnoringPunctuation;
0238     emit sortIgnoringPunctuationChanged(m_sortIgnoringPunctuation);
0239   }
0240 }
0241 
0242 void FileConfig::setToFilenameFormat(const QString& formatText)
0243 {
0244   if (m_formatText != formatText) {
0245     m_formatText = formatText;
0246     emit toFilenameFormatChanged(m_formatText);
0247   }
0248 }
0249 
0250 void FileConfig::setToFilenameFormats(const QStringList& formatItems)
0251 {
0252   if (m_formatItems != formatItems) {
0253     m_formatItems = formatItems;
0254     m_formatItems.removeDuplicates();
0255     emit toFilenameFormatsChanged(m_formatItems);
0256   }
0257 }
0258 
0259 void FileConfig::setFromFilenameFormat(const QString& formatFromFilenameText)
0260 {
0261   if (m_formatFromFilenameText != formatFromFilenameText) {
0262     m_formatFromFilenameText = formatFromFilenameText;
0263     emit fromFilenameFormatChanged(m_formatFromFilenameText);
0264   }
0265 }
0266 
0267 void FileConfig::setFromFilenameFormats(const QStringList& fromFilenameFormats)
0268 {
0269   if (m_formatFromFilenameItems != fromFilenameFormats) {
0270     m_formatFromFilenameItems = fromFilenameFormats;
0271     m_formatFromFilenameItems.removeDuplicates();
0272     emit fromFilenameFormatsChanged(m_formatFromFilenameItems);
0273   }
0274 }
0275 
0276 void FileConfig::setDefaultCoverFileName(const QString& defaultCoverFileName)
0277 {
0278   if (m_defaultCoverFileName != defaultCoverFileName) {
0279     m_defaultCoverFileName = defaultCoverFileName;
0280     emit defaultCoverFileNameChanged(m_defaultCoverFileName);
0281   }
0282 }
0283 
0284 void FileConfig::setLastOpenedFile(const QString& lastOpenedFile)
0285 {
0286   if (m_lastOpenedFile != lastOpenedFile) {
0287     m_lastOpenedFile = lastOpenedFile;
0288     emit lastOpenedFileChanged(m_lastOpenedFile);
0289   }
0290 }
0291 
0292 void FileConfig::setTextEncoding(const QString& textEncoding)
0293 {
0294   if (m_textEncoding != textEncoding) {
0295     m_textEncoding = textEncoding;
0296     emit textEncodingChanged(m_textEncoding);
0297   }
0298 }
0299 
0300 int FileConfig::textEncodingIndex() const
0301 {
0302   return indexFromTextCodecName(m_textEncoding);
0303 }
0304 
0305 void FileConfig::setTextEncodingIndex(int index)
0306 {
0307   if (QString encoding = indexToTextCodecName(index); !encoding.isNull()) {
0308     setTextEncoding(encoding);
0309   }
0310 }
0311 
0312 void FileConfig::setPreserveTime(bool preserveTime)
0313 {
0314   if (m_preserveTime != preserveTime) {
0315     m_preserveTime = preserveTime;
0316     emit preserveTimeChanged(m_preserveTime);
0317   }
0318 }
0319 
0320 void FileConfig::setMarkChanges(bool markChanges)
0321 {
0322   if (m_markChanges != markChanges) {
0323     m_markChanges = markChanges;
0324     emit markChangesChanged(m_markChanges);
0325   }
0326 }
0327 
0328 void FileConfig::setLoadLastOpenedFile(bool loadLastOpenedFile)
0329 {
0330   if (m_loadLastOpenedFile != loadLastOpenedFile) {
0331     m_loadLastOpenedFile = loadLastOpenedFile;
0332     emit loadLastOpenedFileChanged(m_loadLastOpenedFile);
0333   }
0334 }