2
* \file formatconfig.cpp
3
* Format configuration.
9
* Copyright (C) 2003-2007 Urs Fleisch
11
* This file is part of Kid3.
13
* Kid3 is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* Kid3 is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
30
#include <kconfigskeleton.h>
33
#include <qstringlist.h>
35
#include "qtcompatmac.h"
36
#include "generalconfig.h"
37
#include "formatconfig.h"
43
FormatConfig::FormatConfig(const QString& grp) :
45
m_formatWhileEditing(false),
46
m_caseConversion(AllFirstLettersUppercase),
47
m_strRepEnabled(false),
48
m_filenameFormatter(false)
56
FormatConfig::~FormatConfig() {}
59
* Set specific properties for a filename format.
60
* This will set default string conversions and not touch the file
61
* extension when formatting.
63
void FormatConfig::setAsFilenameFormatter()
65
m_filenameFormatter = true;
66
m_strRepEnabled = true;
67
m_strRepMap["/"] = "-";
68
m_strRepMap[":"] = "-";
69
m_strRepMap["."] = "";
70
m_strRepMap["?"] = "";
71
m_strRepMap["*"] = "";
72
m_strRepMap["\""] = "''";
73
m_strRepMap["�"] = "ae";
74
m_strRepMap["�"] = "oe";
75
m_strRepMap["�"] = "ue";
76
m_strRepMap["�"] = "Ae";
77
m_strRepMap["�"] = "Oe";
78
m_strRepMap["�"] = "Ue";
79
m_strRepMap["�"] = "ss";
83
* Format a string using this configuration.
85
* @param str string to format
87
void FormatConfig::formatString(QString& str) const
91
if (m_filenameFormatter) {
92
/* Do not format the extension if it is a filename */
93
dotPos = str.QCM_lastIndexOf('.');
95
ext = str.right(str.length() - dotPos);
96
str = str.left(dotPos);
99
if (m_caseConversion != NoChanges) {
100
switch (m_caseConversion) {
102
str = str.QCM_toLower();
105
str = str.QCM_toUpper();
107
case FirstLetterUppercase:
108
str = str.at(0).QCM_toUpper() + str.right(str.length() - 1).QCM_toLower();
110
case AllFirstLettersUppercase: {
112
bool wordstart = true;
113
for (unsigned i = 0; i < static_cast<unsigned>(str.length()); ++i) {
114
QChar ch = str.at(i);
115
if (!ch.isLetterOrNumber() &&
116
ch != '\'' && ch != '`') {
119
} else if (wordstart) {
121
newstr.append(ch.QCM_toUpper());
123
newstr.append(ch.QCM_toLower());
133
if (m_strRepEnabled) {
134
QMap<QString, QString>::ConstIterator it;
135
for (it = m_strRepMap.begin(); it != m_strRepMap.end(); ++it) {
136
#if QT_VERSION >= 0x030100
137
str.replace(it.key(), *it);
139
QString key(it.key()), data(it.data());
140
int pos = 0, keylen = key.length();
141
int datalen = data.length();
142
while (pos < (int)str.length()) {
143
pos = str.QCM_indexOf(key);
144
if (pos == -1) break;
145
str.replace(pos, keylen, data);
151
/* append extension if it was removed */
158
* Format frames using this configuration.
160
* @param frames frames
162
void FormatConfig::formatFrames(FrameCollection& frames) const
164
for (FrameCollection::iterator it = frames.begin();
167
Frame& frame = const_cast<Frame&>(*it);
168
if (frame.getType() != Frame::FT_Genre) {
169
QString value(frame.getValue());
170
if (!value.isEmpty()) {
172
frame.setValueIfChanged(value);
179
* Persist configuration.
181
* @param config KDE configuration
183
void FormatConfig::writeToConfig(
184
#ifdef CONFIG_USE_KDE
191
#ifdef CONFIG_USE_KDE
192
KCM_KConfigGroup(cfg, config, m_group);
193
cfg.writeEntry("FormatWhileEditing", m_formatWhileEditing);
194
cfg.writeEntry("CaseConversion", static_cast<int>(m_caseConversion));
195
cfg.writeEntry("StrRepEnabled", m_strRepEnabled);
196
cfg.writeEntry("StrRepMapKeys", m_strRepMap.keys());
197
cfg.writeEntry("StrRepMapValues", m_strRepMap.values());
199
config->beginGroup("/" + m_group);
200
config->QCM_writeEntry("/FormatWhileEditing", m_formatWhileEditing);
201
config->QCM_writeEntry("/CaseConversion", m_caseConversion);
202
config->QCM_writeEntry("/StrRepEnabled", m_strRepEnabled);
203
config->QCM_writeEntry("/StrRepMapKeys", m_strRepMap.keys());
204
config->QCM_writeEntry("/StrRepMapValues", m_strRepMap.values());
210
* Read persisted configuration.
212
* @param config KDE configuration
214
void FormatConfig::readFromConfig(
215
#ifdef CONFIG_USE_KDE
222
#ifdef CONFIG_USE_KDE
223
KCM_KConfigGroup(cfg, config, m_group);
224
m_formatWhileEditing = cfg.KCM_readBoolEntry("FormatWhileEditing", m_formatWhileEditing);
225
m_caseConversion = (CaseConversion)cfg.KCM_readNumEntry("CaseConversion",
226
(int)m_caseConversion);
227
m_strRepEnabled = cfg.KCM_readBoolEntry("StrRepEnabled", m_strRepEnabled);
228
QStringList keys = cfg.KCM_readListEntry("StrRepMapKeys");
229
QStringList values = cfg.KCM_readListEntry("StrRepMapValues");
230
if (!keys.empty() && !values.empty()) {
231
QStringList::Iterator itk, itv;
233
for (itk = keys.begin(), itv = values.begin();
234
itk != keys.end() && itv != values.end();
236
m_strRepMap[*itk] = *itv;
240
config->beginGroup("/" + m_group);
241
m_formatWhileEditing = config->QCM_readBoolEntry("/FormatWhileEditing", m_formatWhileEditing);
242
m_caseConversion = (CaseConversion)config->QCM_readNumEntry("/CaseConversion",
243
(int)m_caseConversion);
244
m_strRepEnabled = config->QCM_readBoolEntry("/StrRepEnabled", m_strRepEnabled);
245
QStringList keys = config->QCM_readListEntry("/StrRepMapKeys");
246
QStringList values = config->QCM_readListEntry("/StrRepMapValues");
247
if (!keys.empty() && !values.empty()) {
248
QStringList::Iterator itk, itv;
250
for (itk = keys.begin(), itv = values.begin();
251
itk != keys.end() && itv != values.end();
253
m_strRepMap[*itk] = *itv;