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

0001 /**
0002  * \file numbertracksconfig.cpp
0003  * Configuration for track numbering.
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 "numbertracksconfig.h"
0028 #include "isettings.h"
0029 
0030 namespace {
0031 
0032 /**
0033  * Convert tag version to number tracks destination value in configuration.
0034  * @param tagVersion tag version
0035  * @return value used in configuration, kept for backwards compatibility.
0036  */
0037 int tagVersionToNumberTracksDestCfg(Frame::TagVersion tagVersion) {
0038   return static_cast<int>(tagVersion) - 1;
0039 }
0040 
0041 /**
0042  * Convert number tracks destination value in configuration to tag version.
0043  * @param importDest value used in configuration, kept for backwards
0044  *                   compatibility.
0045  * @return tag version.
0046  */
0047 Frame::TagVersion numberTracksDestCfgToTagVersion(int importDest) {
0048   return Frame::tagVersionCast(importDest + 1);
0049 }
0050 
0051 }
0052 
0053 int NumberTracksConfig::s_index = -1;
0054 
0055 /**
0056  * Constructor.
0057  */
0058 NumberTracksConfig::NumberTracksConfig()
0059   : StoredConfig(QLatin1String("NumberTracks")),
0060     m_numberTracksDst(Frame::TagV1),
0061     m_numberTracksStart(1),
0062     m_trackNumberingEnabled(true),
0063     m_directoryCounterResetEnabled(false)
0064 {
0065 }
0066 
0067 /**
0068  * Persist configuration.
0069  *
0070  * @param config configuration
0071  */
0072 void NumberTracksConfig::writeToConfig(ISettings* config) const
0073 {
0074   config->beginGroup(m_group);
0075   config->setValue(QLatin1String("NumberTracksDestination"),
0076                    QVariant(tagVersionToNumberTracksDestCfg(m_numberTracksDst)));
0077   config->setValue(QLatin1String("NumberTracksStartNumber"),
0078                    QVariant(m_numberTracksStart));
0079   config->setValue(QLatin1String("EnableTrackNumbering"),
0080                    QVariant(m_trackNumberingEnabled));
0081   config->setValue(QLatin1String("ResetCounterForEachDirectory"),
0082                    QVariant(m_directoryCounterResetEnabled));
0083   config->endGroup();
0084   config->beginGroup(m_group, true);
0085   config->setValue(QLatin1String("WindowGeometry"), QVariant(m_windowGeometry));
0086   config->endGroup();
0087 }
0088 
0089 /**
0090  * Read persisted configuration.
0091  *
0092  * @param config configuration
0093  */
0094 void NumberTracksConfig::readFromConfig(ISettings* config)
0095 {
0096   config->beginGroup(m_group);
0097   m_numberTracksDst = numberTracksDestCfgToTagVersion(
0098         config->value(QLatin1String("NumberTracksDestination"), 0).toInt());
0099   m_numberTracksStart = config->value(QLatin1String("NumberTracksStartNumber"),
0100                                       1).toInt();
0101   m_trackNumberingEnabled = config->value(QLatin1String("EnableTrackNumbering"),
0102                                           m_trackNumberingEnabled).toBool();
0103   m_directoryCounterResetEnabled =
0104       config->value(QLatin1String("ResetCounterForEachDirectory"),
0105                     m_directoryCounterResetEnabled).toBool();
0106   config->endGroup();
0107   config->beginGroup(m_group, true);
0108   m_windowGeometry = config->value(QLatin1String("WindowGeometry"),
0109                                    m_windowGeometry).toByteArray();
0110   config->endGroup();
0111 }
0112 
0113 void NumberTracksConfig::setNumberTracksDestination(Frame::TagVersion numberTracksDestination)
0114 {
0115   if (m_numberTracksDst != numberTracksDestination) {
0116     m_numberTracksDst = numberTracksDestination;
0117     emit numberTracksDestinationChanged(m_numberTracksDst);
0118   }
0119 }
0120 
0121 void NumberTracksConfig::setNumberTracksStart(int numberTracksStart)
0122 {
0123   if (m_numberTracksStart != numberTracksStart) {
0124     m_numberTracksStart = numberTracksStart;
0125     emit numberTracksStartChanged(m_numberTracksStart);
0126   }
0127 }
0128 
0129 /**
0130  * Enable or disable track numbering.
0131  * @param enable true to enable
0132  */
0133 void NumberTracksConfig::setTrackNumberingEnabled(bool enable)
0134 {
0135   if (m_trackNumberingEnabled != enable) {
0136     m_trackNumberingEnabled = enable;
0137     emit trackNumberingEnabledChanged(m_trackNumberingEnabled);
0138   }
0139 }
0140 
0141 /**
0142  * Enable reset of counter for each directory.
0143  * @param enable true to enable
0144  */
0145 void NumberTracksConfig::setDirectoryCounterResetEnabled(bool enable)
0146 {
0147   if (m_directoryCounterResetEnabled != enable) {
0148     m_directoryCounterResetEnabled = enable;
0149     emit directoryCounterResetEnabledChanged(m_directoryCounterResetEnabled);
0150   }
0151 }
0152 
0153 void NumberTracksConfig::setWindowGeometry(const QByteArray& windowGeometry)
0154 {
0155   if (m_windowGeometry != windowGeometry) {
0156     m_windowGeometry = windowGeometry;
0157     emit windowGeometryChanged(m_windowGeometry);
0158   }
0159 }