File indexing completed on 2022-08-04 15:34:53

0001 /**
0002  * Copyright (C) 2004 Michael Pyne <mpyne@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "tagrenameroptions.h"
0018 
0019 #include <KLocalizedString>
0020 #include <kconfig.h>
0021 #include <kconfigbase.h>
0022 #include <kconfiggroup.h>
0023 #include <KSharedConfig>
0024 #include <KLazyLocalizedString>
0025 
0026 #include "juk_debug.h"
0027 
0028 TagRenamerOptions::TagRenamerOptions() :
0029     m_emptyAction(IgnoreEmptyTag),
0030     m_trackWidth(0),
0031     m_disabled(true),
0032     m_category(TagUnknown)
0033 {
0034 }
0035 
0036 TagRenamerOptions::TagRenamerOptions(const TagRenamerOptions &other) :
0037     m_prefix(other.m_prefix),
0038     m_suffix(other.m_suffix),
0039     m_emptyAction(other.m_emptyAction),
0040     m_emptyText(other.m_emptyText),
0041     m_trackWidth(other.m_trackWidth),
0042     m_disabled(other.m_disabled),
0043     m_category(other.m_category)
0044 {
0045 }
0046 
0047 TagRenamerOptions::TagRenamerOptions(const CategoryID &category)
0048     : m_category(category.category)
0049 {
0050     // Set some defaults
0051 
0052     bool disabled;
0053     unsigned categoryNum = category.categoryNumber;
0054 
0055     switch(category.category) {
0056     case Title:
0057     case Artist:
0058     case Genre:
0059     case Year:
0060     case Album:
0061     case Track:
0062         disabled = false;
0063         break;
0064     default:
0065         disabled = true;
0066     }
0067 
0068     // Make sure we don't use translated strings for the config file keys.
0069 
0070     QString typeKey = tagTypeText(category.category, false);
0071     KConfigGroup config(KSharedConfig::openConfig(), "FileRenamer");
0072 
0073     if(categoryNum > 0)
0074         typeKey.append(QString::number(categoryNum));
0075 
0076     setSuffix(config.readEntry(QString("%1Suffix").arg(typeKey), QString()));
0077     setPrefix(config.readEntry(QString("%1Prefix").arg(typeKey), QString()));
0078 
0079     // Default the emptyAction to ignoring the empty tag.
0080 
0081     const QString emptyAction = config.readEntry(QString("%1EmptyAction").arg(typeKey), QString()).toLower();
0082     setEmptyAction(IgnoreEmptyTag);
0083 
0084     if(emptyAction == "forceemptyinclude")
0085         setEmptyAction(ForceEmptyInclude);
0086     else if(emptyAction == "usereplacementvalue")
0087         setEmptyAction(UseReplacementValue);
0088 
0089     setEmptyText(config.readEntry(QString("%1EmptyText").arg(typeKey), QString()));
0090     setTrackWidth(config.readEntry(QString("%1TrackWidth").arg(typeKey), 0));
0091     setDisabled(config.readEntry(QString("%1Disabled").arg(typeKey), disabled));
0092 }
0093 
0094 QString TagRenamerOptions::tagTypeText(TagType type, bool translate)
0095 {
0096     KLazyLocalizedString msg;
0097 
0098     switch(type) {
0099         case Title:
0100             msg = kli18nc("song title", "Title");
0101         break;
0102 
0103         case Artist:
0104             msg = kli18n("Artist");
0105         break;
0106 
0107         case Album:
0108             msg = kli18n("Album");
0109         break;
0110 
0111         case Track:
0112             msg = kli18nc("cd track number", "Track");
0113         break;
0114 
0115         case Genre:
0116             msg = kli18n("Genre");
0117         break;
0118 
0119         case Year:
0120             msg = kli18n("Year");
0121         break;
0122 
0123         default:
0124             qCWarning(JUK_LOG) << "I don't know what category we're looking up, this is a problem.";
0125             qCWarning(JUK_LOG) << "The category ID is " << (unsigned) type;
0126             msg = kli18nc("unknown renamer category", "Unknown");
0127     }
0128 
0129     if(translate)
0130         return msg.toString();
0131     else
0132         return msg.untranslatedText();
0133 }
0134 
0135 void TagRenamerOptions::saveConfig(unsigned categoryNum) const
0136 {
0137     // Make sure we don't use translated strings for the config file keys.
0138 
0139     QString typeKey = tagTypeText(false);
0140     if(categoryNum > 0)
0141         typeKey.append(QString::number(categoryNum));
0142 
0143     KConfigGroup config(KSharedConfig::openConfig(), "FileRenamer");
0144 
0145     config.writeEntry(QString("%1Suffix").arg(typeKey), suffix());
0146     config.writeEntry(QString("%1Prefix").arg(typeKey), prefix());
0147 
0148     QString emptyStr;
0149 
0150     switch(emptyAction()) {
0151     case ForceEmptyInclude:
0152         emptyStr = "ForceEmptyInclude";
0153     break;
0154 
0155     case IgnoreEmptyTag:
0156         emptyStr = "IgnoreEmptyTag";
0157     break;
0158 
0159     case UseReplacementValue:
0160         emptyStr = "UseReplacementValue";
0161     break;
0162     }
0163 
0164     config.writeEntry(QString("%1EmptyAction").arg(typeKey), emptyStr);
0165     config.writeEntry(QString("%1EmptyText").arg(typeKey), emptyText());
0166     config.writeEntry(QString("%1Disabled").arg(typeKey), disabled());
0167 
0168     if(category() == Track)
0169         config.writeEntry(QString("%1TrackWidth").arg(typeKey), trackWidth());
0170 
0171     config.sync();
0172 }
0173 
0174 TagType TagRenamerOptions::tagFromCategoryText(const QString &text)
0175 {
0176     for(unsigned i = StartTag; i < NumTypes; ++i)
0177         if(tagTypeText(static_cast<TagType>(i), false) == text)
0178             return static_cast<TagType>(i);
0179 
0180     return TagUnknown;
0181 }
0182 
0183 // vim: set et sw=4 tw=0 sta: