summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/mpc/kfile_mpc.cpp
blob: 4b6800dfa20e11521c55a41507f8052073573c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* This file is part of the KDE project
 * Copyright (C) 2003-2004 Allan Sandfeld Jensen <kde@carewolf.com>
 *
 * Originally based upon the kfile_ogg plugin:
 *  Copyright (C) 2001, 2002 Rolf Magnus <ramagnus@kde.org>
 * Interfacing to TagLib is copied from kfile_mp3 plugin:
 *  Copyright (C) 2003 Scott Wheeler <wheeler@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kfile_mpc.h"

#include <tqcstring.h>
#include <tqfile.h>
#include <tqdatetime.h>
#include <tqdict.h>
#include <tqvalidator.h>
#include <tqfileinfo.h>

#include <kdebug.h>
#include <kurl.h>
#include <kprocess.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <ksavefile.h>

#include <tstring.h>
#include <tfile.h>
#include <mpcfile.h>
#include <tag.h>

#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>

K_EXPORT_COMPONENT_FACTORY(kfile_mpc, KGenericFactory<KMpcPlugin>("kfile_mpc"))

KMpcPlugin::KMpcPlugin( TQObject *parent, const char *name,
                        const TQStringList &args )
    : KFilePlugin( parent, name, args )
{
    kdDebug(7034) << "mpc plugin\n";

    KFileMimeTypeInfo* info = addMimeTypeInfo( "audio/x-musepack" );

    KFileMimeTypeInfo::GroupInfo* group = 0;

    // comment group
    group = addGroupInfo(info, "Comment", i18n("Comment"));
    setAttributes(group, KFileMimeTypeInfo::Addable |
                         KFileMimeTypeInfo::Removable);

    KFileMimeTypeInfo::ItemInfo* item = 0;

    item = addItemInfo(group, "Artist", i18n("Artist"), TQVariant::String);
    setHint(item, KFileMimeTypeInfo::Author);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Title", i18n("Title"), TQVariant::String);
    setHint(item, KFileMimeTypeInfo::Name);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Album", i18n("Album"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Genre", i18n("Genre"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Tracknumber", i18n("Track Number"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Date", i18n("Date"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Description", i18n("Description"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Organization", i18n("Organization"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Location", i18n("Location"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);

    item = addItemInfo(group, "Copyright", i18n("Copyright"), TQVariant::String);
    setAttributes(item, KFileMimeTypeInfo::Modifiable);


    addVariableInfo(group, TQVariant::String, KFileMimeTypeInfo::Addable |
                                             KFileMimeTypeInfo::Removable |
                                             KFileMimeTypeInfo::Modifiable);

    // technical group
    group = addGroupInfo(info, "Technical", i18n("Technical Details"));
    setAttributes(group, 0);

    addItemInfo(group, "Channels", i18n("Channels"), TQVariant::Int);

    addItemInfo(group, "Version", i18n("Version"), TQVariant::Int);

    item = addItemInfo(group, "Sample Rate", i18n("Sample Rate"), TQVariant::Int);
    setSuffix(item, i18n(" Hz"));

    item = addItemInfo(group, "Bitrate", i18n("Average Bitrate"),
                       TQVariant::Int);
    setAttributes(item, KFileMimeTypeInfo::Averaged);
    setHint(item, KFileMimeTypeInfo::Bitrate);
    setSuffix(item, i18n( " kbps"));

    item = addItemInfo(group, "Length", i18n("Length"), TQVariant::Int);
    setAttributes(item, KFileMimeTypeInfo::Cummulative);
    setHint(item, KFileMimeTypeInfo::Length);
    setUnit(item, KFileMimeTypeInfo::Seconds);
}

bool KMpcPlugin::readInfo( KFileMetaInfo& info, uint what )
{

    bool readComment = false;
    bool readTech = false;
    if (what & (KFileMetaInfo::Fastest |
                KFileMetaInfo::DontCare |
                KFileMetaInfo::ContentInfo)) readComment = true;

    if (what & (KFileMetaInfo::Fastest |
                KFileMetaInfo::DontCare |
                KFileMetaInfo::TechnicalInfo)) readTech = true;

    if ( info.path().isEmpty() ) // remote file
        return false;

    TagLib::File *file = new TagLib::MPC::File(TQFile::encodeName(info.path()).data(), readTech);

    if (!file->isOpen())
    {
        kdDebug(7034) << "Couldn't open " << file->name() << endl;
        delete file;
        return false;
    }

    if(readComment)
    {
        KFileMetaInfoGroup commentgroup = appendGroup(info, "Comment");

        TQString date  = file->tag()->year() > 0 ? TQString::number(file->tag()->year()) : TQString::null;
        TQString track = file->tag()->track() > 0 ? TQString::number(file->tag()->track()) : TQString::null;

        appendItem(commentgroup, "Title",       TStringToQString(file->tag()->title()).stripWhiteSpace());
        appendItem(commentgroup, "Artist",      TStringToQString(file->tag()->artist()).stripWhiteSpace());
        appendItem(commentgroup, "Album",       TStringToQString(file->tag()->album()).stripWhiteSpace());
        appendItem(commentgroup, "Date",        date);
        appendItem(commentgroup, "Comment",     TStringToQString(file->tag()->comment()).stripWhiteSpace());
        appendItem(commentgroup, "Tracknumber", track);
        appendItem(commentgroup, "Genre",       TStringToQString(file->tag()->genre()).stripWhiteSpace());
    }

    if (readTech)
    {
        KFileMetaInfoGroup techgroup = appendGroup(info, "Technical");
        TagLib::MPC::Properties *properties =
                   (TagLib::MPC::Properties*)(file->audioProperties());

        appendItem(techgroup, "Bitrate",      properties->bitrate());
        appendItem(techgroup, "Sample Rate",  properties->sampleRate());
        appendItem(techgroup, "Channels",     properties->channels());
        appendItem(techgroup, "Length",       properties->length());
        appendItem(techgroup, "Version",      properties->mpcVersion());
    }

    delete file;
    return true;

}

/**
 * Do translation between KFileMetaInfo items and TagLib::String in a tidy way.
 */

class Translator
{
public:
    Translator(const KFileMetaInfo &info) : m_info(info) {}
    TagLib::String operator[](const char *key) const
    {
        return QStringToTString(m_info["Comment"][key].value().toString());
    }
    int toInt(const char *key) const
    {
        return m_info["Comment"][key].value().toInt();
    }
private:
    const KFileMetaInfo &m_info;
};

bool KMpcPlugin::writeInfo(const KFileMetaInfo& info) const
{
    TagLib::File *file;

    if (!TagLib::File::isWritable(TQFile::encodeName(info.path()).data())) {
        kdDebug(7034) << "can't write to " << info.path() << endl;
        return false;
    }

    file = new TagLib::MPC::File(TQFile::encodeName(info.path()).data(), false);

    if(!file->isOpen())
    {
        kdDebug(7034) << "couldn't open " << info.path() << endl;
        delete file;
        return false;
    }

    Translator t(info);

    file->tag()->setTitle(t["Title"]);
    file->tag()->setArtist(t["Artist"]);
    file->tag()->setAlbum(t["Album"]);
    file->tag()->setYear(t.toInt("Date"));
    file->tag()->setComment(t["Comment"]);
    file->tag()->setTrack(t.toInt("Tracknumber"));
    file->tag()->setGenre(t["Genre"]);

    file->save();

    delete file;
    return true;
}

TQValidator* KMpcPlugin::createValidator( const TQString&,
                                         const TQString &group, const TQString &key,
                                         TQObject* parent, const char* name) const
{
    if(key == "Tracknumber" || key == "Date")
    {
        return new TQIntValidator(0, 9999, parent, name);
    }
    else
	return new TQRegExpValidator(TQRegExp(".*"), parent, name);
}

#include "kfile_mpc.moc"