~ubuntu-branches/ubuntu/wily/kid3/wily-proposed

« back to all changes in this revision

Viewing changes to kid3/formatconfig.cpp

  • Committer: Package Import Robot
  • Author(s): Ana Beatriz Guerrero Lopez, Patrick Matthäi, Ana Beatriz Guerrero Lopez
  • Date: 2011-11-13 16:34:13 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20111113163413-5y0anlc4dqf511uh
Tags: 2.0.1-1
* New upstream release.

[ Patrick Matthäi ]
* Adjust build system.
* Add build dependency xsltproc.

[ Ana Beatriz Guerrero Lopez ]
* Some more adjustments to the build system taken from upstream's deb/
* directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * \file formatconfig.cpp
3
 
 * Format configuration.
4
 
 *
5
 
 * \b Project: Kid3
6
 
 * \author Urs Fleisch
7
 
 * \date 17 Sep 2003
8
 
 *
9
 
 * Copyright (C) 2003-2007  Urs Fleisch
10
 
 *
11
 
 * This file is part of Kid3.
12
 
 *
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.
17
 
 *
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.
22
 
 *
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/>.
25
 
 */
26
 
 
27
 
#include "config.h"
28
 
#ifdef CONFIG_USE_KDE
29
 
#include <kconfig.h>
30
 
#include <kconfigskeleton.h>
31
 
#else
32
 
#include <qstring.h>
33
 
#include <qstringlist.h>
34
 
#endif
35
 
#include "qtcompatmac.h"
36
 
#include "generalconfig.h"
37
 
#include "formatconfig.h"
38
 
#include "frame.h"
39
 
 
40
 
/**
41
 
 * Constructor.
42
 
 */
43
 
FormatConfig::FormatConfig(const QString& grp) :
44
 
        GeneralConfig(grp),
45
 
        m_formatWhileEditing(false),
46
 
        m_caseConversion(AllFirstLettersUppercase),
47
 
        m_strRepEnabled(false),
48
 
        m_filenameFormatter(false)
49
 
{
50
 
        m_strRepMap.clear();
51
 
}
52
 
 
53
 
/**
54
 
 * Destructor.
55
 
 */
56
 
FormatConfig::~FormatConfig() {}
57
 
 
58
 
/**
59
 
 * Set specific properties for a filename format.
60
 
 * This will set default string conversions and not touch the file
61
 
 * extension when formatting.
62
 
 */
63
 
void FormatConfig::setAsFilenameFormatter()
64
 
{
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";
80
 
}
81
 
 
82
 
/**
83
 
 * Format a string using this configuration.
84
 
 *
85
 
 * @param str string to format
86
 
 */
87
 
void FormatConfig::formatString(QString& str) const
88
 
{
89
 
        QString ext;
90
 
        int dotPos = -1;
91
 
        if (m_filenameFormatter) {
92
 
                /* Do not format the extension if it is a filename */
93
 
                dotPos = str.QCM_lastIndexOf('.');
94
 
                if (dotPos != -1) {
95
 
                        ext = str.right(str.length() - dotPos);
96
 
                        str = str.left(dotPos);
97
 
                }
98
 
        }
99
 
        if (m_caseConversion != NoChanges) {
100
 
                switch (m_caseConversion) {
101
 
                        case AllLowercase:
102
 
                                str = str.QCM_toLower();
103
 
                                break;
104
 
                        case AllUppercase:
105
 
                                str = str.QCM_toUpper();
106
 
                                break;
107
 
                        case FirstLetterUppercase:
108
 
                                str = str.at(0).QCM_toUpper() + str.right(str.length() - 1).QCM_toLower();
109
 
                                break;
110
 
                        case AllFirstLettersUppercase: {
111
 
                                QString newstr;
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 != '`') {
117
 
                                                wordstart = true;
118
 
                                                newstr.append(ch);
119
 
                                        } else if (wordstart) {
120
 
                                                wordstart = false;
121
 
                                                newstr.append(ch.QCM_toUpper());
122
 
                                        } else {
123
 
                                                newstr.append(ch.QCM_toLower());
124
 
                                        }
125
 
                                }
126
 
                                str = newstr;
127
 
                                break;
128
 
                        }
129
 
                        default:
130
 
                                ;
131
 
                }
132
 
        }
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);
138
 
#else
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);
146
 
                                pos += datalen;
147
 
                        }
148
 
#endif
149
 
                }
150
 
        }
151
 
        /* append extension if it was removed */
152
 
        if (dotPos != -1) {
153
 
                str.append(ext);
154
 
        }
155
 
}
156
 
 
157
 
/**
158
 
 * Format frames using this configuration.
159
 
 *
160
 
 * @param frames frames
161
 
 */
162
 
void FormatConfig::formatFrames(FrameCollection& frames) const
163
 
{
164
 
        for (FrameCollection::iterator it = frames.begin();
165
 
                         it != frames.end();
166
 
                         ++it) {
167
 
                Frame& frame = const_cast<Frame&>(*it);
168
 
                if (frame.getType() != Frame::FT_Genre) {
169
 
                        QString value(frame.getValue());
170
 
                        if (!value.isEmpty()) {
171
 
                                formatString(value);
172
 
                                frame.setValueIfChanged(value);
173
 
                        }
174
 
                }
175
 
        }
176
 
}
177
 
 
178
 
/**
179
 
 * Persist configuration.
180
 
 *
181
 
 * @param config KDE configuration
182
 
 */
183
 
void FormatConfig::writeToConfig(
184
 
#ifdef CONFIG_USE_KDE
185
 
        KConfig* config
186
 
#else
187
 
        Kid3Settings* config
188
 
#endif
189
 
        ) const
190
 
{
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());
198
 
#else
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());
205
 
        config->endGroup();
206
 
#endif
207
 
}
208
 
 
209
 
/**
210
 
 * Read persisted configuration.
211
 
 *
212
 
 * @param config KDE configuration
213
 
 */
214
 
void FormatConfig::readFromConfig(
215
 
#ifdef CONFIG_USE_KDE
216
 
        KConfig* config
217
 
#else
218
 
        Kid3Settings* config
219
 
#endif
220
 
        )
221
 
{
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;
232
 
                m_strRepMap.clear();
233
 
                for (itk = keys.begin(), itv = values.begin();
234
 
                         itk != keys.end() && itv != values.end();
235
 
                         ++itk, ++itv) {
236
 
                        m_strRepMap[*itk] = *itv;
237
 
                }
238
 
        }
239
 
#else
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;
249
 
                m_strRepMap.clear();
250
 
                for (itk = keys.begin(), itv = values.begin();
251
 
                         itk != keys.end() && itv != values.end();
252
 
                         ++itk, ++itv) {
253
 
                        m_strRepMap[*itk] = *itv;
254
 
                }
255
 
        }
256
 
        config->endGroup();
257
 
#endif
258
 
}