~ubuntu-branches/ubuntu/feisty/kid3/feisty

« back to all changes in this revision

Viewing changes to kid3/formatconfig.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2004-04-29 02:11:57 UTC
  • Revision ID: james.westby@ubuntu.com-20040429021157-omrdcw8270prqiun
Tags: upstream-0.4
Import upstream version 0.4

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
 
 
10
#include "config.h"
 
11
#ifdef CONFIG_USE_KDE
 
12
#include <kconfig.h>
 
13
#else
 
14
#include <qstring.h>
 
15
#if QT_VERSION >= 300
 
16
#include <qsettings.h>
 
17
#else
 
18
#include <qregexp.h>
 
19
#include "generalconfig.h"
 
20
#endif
 
21
#endif
 
22
#include "standardtags.h"
 
23
#include "formatconfig.h"
 
24
 
 
25
/**
 
26
 * Constructor.
 
27
 */
 
28
FormatConfig::FormatConfig(const QString &grp) : GeneralConfig(grp)
 
29
{
 
30
        caseConversion = AllFirstLettersUppercase;
 
31
        strRepEnabled = false;
 
32
        filenameFormatter = false;
 
33
        strRepMap.clear();
 
34
}
 
35
 
 
36
/**
 
37
 * Destructor.
 
38
 */
 
39
FormatConfig::~FormatConfig() {}
 
40
 
 
41
/**
 
42
 * Set specific properties for a filename format.
 
43
 * This will set default string conversions and not touch the file
 
44
 * extension when formatting.
 
45
 */
 
46
void FormatConfig::setAsFilenameFormatter()
 
47
{
 
48
        filenameFormatter = true;
 
49
        strRepEnabled = true;
 
50
        strRepMap["/"] = "-";
 
51
        strRepMap[":"] = "-";
 
52
        strRepMap["."] = "";
 
53
        strRepMap["?"] = "";
 
54
        strRepMap["*"] = "";
 
55
        strRepMap["�"] = "ae";
 
56
        strRepMap["�"] = "oe";
 
57
        strRepMap["�"] = "ue";
 
58
        strRepMap["�"] = "Ae";
 
59
        strRepMap["�"] = "Oe";
 
60
        strRepMap["�"] = "Ue";
 
61
        strRepMap["�"] = "ss";
 
62
}
 
63
 
 
64
/**
 
65
 * Format a string using this configuration.
 
66
 *
 
67
 * @param str string to format
 
68
 */
 
69
void FormatConfig::formatString(QString& str) const
 
70
{
 
71
        QString ext;
 
72
        int dotPos = -1;
 
73
        if (filenameFormatter) {
 
74
                /* Do not format the extension if it is a filename */
 
75
                dotPos = str.findRev('.');
 
76
                if (dotPos != -1) {
 
77
                        ext = str.right(str.length() - dotPos);
 
78
                        str = str.left(dotPos);
 
79
                }
 
80
        }
 
81
        if (strRepEnabled) {
 
82
                QMap<QString, QString>::ConstIterator it;
 
83
                for (it = strRepMap.begin(); it != strRepMap.end(); ++it) {
 
84
#if QT_VERSION >= 300
 
85
                        str.replace(it.key(), it.data());
 
86
#else
 
87
                        QString key(it.key()), data(it.data());
 
88
                        int pos = 0, keylen = key.length();
 
89
                        int datalen = data.length();
 
90
                        while (pos < (int)str.length()) {
 
91
                                pos = str.find(key);
 
92
                                if (pos == -1) break;
 
93
                                str.replace(pos, keylen, data);
 
94
                                pos += datalen;
 
95
                        }
 
96
#endif
 
97
                }
 
98
        }
 
99
        if (caseConversion != NoChanges) {
 
100
                switch (caseConversion) {
 
101
                        case AllLowercase:
 
102
                                str = str.lower();
 
103
                                break;
 
104
                        case AllUppercase:
 
105
                                str = str.upper();
 
106
                                break;
 
107
                        case FirstLetterUppercase:
 
108
                                str = str.at(0).upper() + str.right(str.length() - 1);
 
109
                                break;
 
110
                        case AllFirstLettersUppercase: {
 
111
                                QString newstr;
 
112
                                bool wordstart = true;
 
113
                                for (uint i = 0; i < 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.upper());
 
122
                                        } else {
 
123
                                                newstr.append(ch.lower());
 
124
                                        }
 
125
                                }
 
126
                                str = newstr;
 
127
                                break;
 
128
                        }
 
129
                        default:
 
130
                                ;
 
131
                }
 
132
        }
 
133
        /* append extension if it was removed */
 
134
        if (dotPos != -1) {
 
135
                str.append(ext);
 
136
        }
 
137
}
 
138
 
 
139
/**
 
140
 * Format title, artist and album using this configuration.
 
141
 *
 
142
 * @param st standard tags
 
143
 */
 
144
void FormatConfig::formatStandardTags(StandardTags& st) const
 
145
{
 
146
        formatString(st.title);
 
147
        formatString(st.artist);
 
148
        formatString(st.album);
 
149
}
 
150
 
 
151
/**
 
152
 * Persist configuration.
 
153
 *
 
154
 * @param config KDE configuration
 
155
 */
 
156
void FormatConfig::writeToConfig(
 
157
#ifdef CONFIG_USE_KDE
 
158
        KConfig *config
 
159
#else
 
160
        QSettings *config
 
161
#endif
 
162
        ) const
 
163
{
 
164
#ifdef CONFIG_USE_KDE
 
165
        config->setGroup(group);
 
166
        config->writeEntry("CaseConversion", caseConversion);
 
167
        config->writeEntry("StrRepEnabled", strRepEnabled);
 
168
        config->writeEntry("StrRepMapKeys", strRepMap.keys());
 
169
        config->writeEntry("StrRepMapValues", strRepMap.values());
 
170
#else
 
171
        config->beginGroup("/" + group);
 
172
        config->writeEntry("/CaseConversion", caseConversion);
 
173
        config->writeEntry("/StrRepEnabled", strRepEnabled);
 
174
#if QT_VERSION >= 300
 
175
        config->writeEntry("/StrRepMapKeys", strRepMap.keys());
 
176
        config->writeEntry("/StrRepMapValues", strRepMap.values());
 
177
#else
 
178
        config->writeEntry("/StrRepMap", strRepMap);
 
179
#endif
 
180
        config->endGroup();
 
181
#endif
 
182
}
 
183
 
 
184
/**
 
185
 * Read persisted configuration.
 
186
 *
 
187
 * @param config KDE configuration
 
188
 */
 
189
void FormatConfig::readFromConfig(
 
190
#ifdef CONFIG_USE_KDE
 
191
        KConfig *config
 
192
#else
 
193
        QSettings *config
 
194
#endif
 
195
        )
 
196
{
 
197
#ifdef CONFIG_USE_KDE
 
198
        config->setGroup(group);
 
199
        caseConversion = (CaseConversion)config->readNumEntry("CaseConversion",
 
200
                                                                                                                  (int)caseConversion);
 
201
        strRepEnabled = config->readBoolEntry("StrRepEnabled", strRepEnabled);
 
202
        QStringList keys = config->readListEntry("StrRepMapKeys");
 
203
        QStringList values = config->readListEntry("StrRepMapValues");
 
204
        if (!keys.empty() && !values.empty()) {
 
205
                QStringList::Iterator itk, itv;
 
206
                strRepMap.clear();
 
207
                for (itk = keys.begin(), itv = values.begin();
 
208
                         itk != keys.end() && itv != values.end();
 
209
                         ++itk, ++itv) {
 
210
                        strRepMap[*itk] = *itv;
 
211
                }
 
212
        }
 
213
#else
 
214
        config->beginGroup("/" + group);
 
215
        caseConversion = (CaseConversion)config->readNumEntry("/CaseConversion",
 
216
                                                                                                                  (int)caseConversion);
 
217
        strRepEnabled = config->readBoolEntry("/StrRepEnabled", strRepEnabled);
 
218
#if QT_VERSION >= 300
 
219
        QStringList keys = config->readListEntry("/StrRepMapKeys");
 
220
        QStringList values = config->readListEntry("/StrRepMapValues");
 
221
        if (!keys.empty() && !values.empty()) {
 
222
                QStringList::Iterator itk, itv;
 
223
                strRepMap.clear();
 
224
                for (itk = keys.begin(), itv = values.begin();
 
225
                         itk != keys.end() && itv != values.end();
 
226
                         ++itk, ++itv) {
 
227
                        strRepMap[*itk] = *itv;
 
228
                }
 
229
        }
 
230
#else
 
231
        strRepMap = config->readMapEntry("/StrRepMap", strRepMap);
 
232
#endif
 
233
        config->endGroup();
 
234
#endif
 
235
}