~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to noatun/noatun/modules/id3tag/id3tag.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "id3tag.h"
 
2
#include <klocale.h>
 
3
#include <noatunapp.h>
 
4
#include <qfile.h>
 
5
#include <kglobal.h>
 
6
#include <string.h>
 
7
#include <qlabel.h>
 
8
#include <klineedit.h>
 
9
#include <kglobal.h>
 
10
#include <klocale.h>
 
11
#include <kconfig.h>
 
12
#include <qlayout.h>
 
13
#include <kaction.h>
 
14
#include <kpopupmenu.h>
 
15
#include "editor.h"
 
16
 
 
17
extern "C"
 
18
{
 
19
        Plugin *create_plugin()
 
20
        {
 
21
                return new NID3();
 
22
        }
 
23
}
 
24
 
 
25
 
 
26
NID3::NID3() : QObject(0, "NID3"), Plugin(), mEditor(0)
 
27
{
 
28
        NOATUNPLUGINC(NID3);
 
29
        connect(napp->player(), SIGNAL(newSong()), SLOT(newSong()));
 
30
        menuID = napp->pluginMenuAdd(i18n("&ID3 Edit"), this, SLOT(editID3()));
 
31
        connect(new IDPrefs(this), SIGNAL(saved()), SLOT(newSong()));
 
32
}
 
33
 
 
34
NID3::~NID3()
 
35
{
 
36
        napp->pluginMenuRemove(menuID);
 
37
}
 
38
 
 
39
void NID3::editID3()
 
40
{
 
41
        if (mEditor)
 
42
                if (mEditor->isVisible())
 
43
                        mEditor->hide();
 
44
                else
 
45
                        mEditor->show();
 
46
 
 
47
 
 
48
}
 
49
 
 
50
static QString parseFormat(const QString &format, const QString &name, const QString &artist, const QString &album,
 
51
                           const QString &comment, const QString &year, const QString &genre);
 
52
 
 
53
void NID3::newSong()
 
54
{
 
55
        PlaylistItem *item=napp->player()->current();
 
56
        if (!item) return;
 
57
        
 
58
        if (mEditor && !mEditor->isVisible())
 
59
        {
 
60
                delete mEditor;
 
61
                mEditor=0;
 
62
        }
 
63
 
 
64
        {
 
65
                mEditor=new Editor(this);
 
66
                mEditor->open(item->file());
 
67
                connect(mEditor, SIGNAL(saved()), SLOT(newSong()));
 
68
        }
 
69
 
 
70
        if (!mEditor->name().stripWhiteSpace().length()) return;
 
71
 
 
72
        KConfig *config = KGlobal::config();
 
73
        KConfigGroupSaver saver(config, "ID3 Tags");
 
74
        QString format=config->readEntry("ID3Format", "%t");
 
75
 
 
76
        QString title = parseFormat(format,mEditor->name(), mEditor->artist(),
 
77
                                    mEditor->album(), mEditor->comment(), mEditor->year(), mEditor->genre());
 
78
        // some titles/authors/songs might contain a / which is encoded as %2f
 
79
        // so replace the encoding with the /
 
80
        title.replace( QRegExp("%2f"), "/" );
 
81
        item->setTitle( title );
 
82
        
 
83
//      item->setProperty("title", mEditor->name());
 
84
        item->setProperty("artist", mEditor->artist());
 
85
        item->setProperty("album", mEditor->album());
 
86
        item->setProperty("date", mEditor->year());
 
87
        item->setProperty("comment", mEditor->comment());
 
88
}
 
89
 
 
90
static QString parseFormat(const QString &format, const QString &name, const QString &artist, const QString &album,
 
91
                           const QString &comment, const QString &year, const QString &genre)
 
92
{
 
93
        QString parsed(format);
 
94
        parsed=' ' +parsed;
 
95
        for (unsigned int pos=1; pos< parsed.length()-1; pos++)
 
96
        {
 
97
                if ((parsed[pos]=='%') && (parsed[pos-1]!='%'))
 
98
                {
 
99
                        switch (parsed[pos+1].latin1())
 
100
                        {
 
101
                        case('%'):
 
102
                                parsed.replace(pos, 2, "%");
 
103
                                break;
 
104
                        case('a'):
 
105
                                parsed.replace(pos, 2, artist);
 
106
                                break;
 
107
                        case('A'):
 
108
                                parsed.replace(pos, 2, album);
 
109
                                break;
 
110
                        case('c'):
 
111
                                parsed.replace(pos, 2, comment);
 
112
                                break;
 
113
                        case('t'):
 
114
                                parsed.replace(pos, 2, name);
 
115
                                break;
 
116
                        case('y'):
 
117
                                parsed.replace(pos, 2, year);
 
118
                                break;
 
119
                        case('Y'):
 
120
                                parsed.replace(pos, 2, year.right(2));
 
121
                                break;
 
122
                        case('g'):
 
123
                                parsed.replace(pos, 2, genre);
 
124
                                break;
 
125
                        }
 
126
                }
 
127
        }
 
128
        return parsed.remove(0, 1);
 
129
}
 
130
 
 
131
 
 
132
IDPrefs::IDPrefs(QObject *parent)
 
133
        : CModule(i18n("ID3 Tag Loading"), i18n("Format Strings for ID3 Tag Loading"), parent)
 
134
{
 
135
        (new QVBoxLayout(this))->setAutoAdd(true);
 
136
 
 
137
        KConfig *config = KGlobal::config();
 
138
        KConfigGroupSaver saver(config, "ID3 Tags");
 
139
        mString=new KLineEdit(config->readEntry("ID3Format", "%t"), this);
 
140
        (mPreview=new QLineEdit(this))->setReadOnly(true);
 
141
        QLabel *l=new QLabel(i18n("Enter the format for the ID Tag.  Every %[something] will be "
 
142
                                  "replaced with the following field, with the exception of '%%', which is replaced with '%'"),
 
143
                             this);
 
144
        l->setAlignment(AlignLeft | AlignVCenter | ExpandTabs | WordBreak);
 
145
 
 
146
        new QLabel(i18n("%a - Artist"), this);
 
147
        new QLabel(i18n("%A - Album"), this);
 
148
        new QLabel(i18n("%c - Comment"), this);
 
149
        new QLabel(i18n("%t - Title"), this);
 
150
        new QLabel(i18n("%y - Year (4 Digits)"), this);
 
151
        new QLabel(i18n("%Y - Year (2 Digits)"), this);
 
152
        new QLabel(i18n("%g - Genre"), this);
 
153
 
 
154
        connect(mString, SIGNAL(textChanged(const QString &)), SLOT(changed(const QString&)));
 
155
        changed(mString->text());
 
156
}
 
157
 
 
158
void IDPrefs::changed(const QString &t)
 
159
{
 
160
        mPreview->setText(parseFormat(t, "Mea Culpa", "Enigma", "MCMXC a.D.", "I'm guilty", "1990", "New Age"));
 
161
}
 
162
 
 
163
void IDPrefs::save()
 
164
{
 
165
        KConfig *config=KGlobal::config();
 
166
        KConfigGroupSaver saver(config, "ID3 Tags");
 
167
        config->writeEntry("ID3Format", mString->text());
 
168
        config->sync();
 
169
        emit saved();
 
170
}
 
171
 
 
172
 
 
173
#include "id3tag.moc"