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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/***************************************************************************
copyright : (C) 2004-2007 by Robby Stephenson
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include <config.h>
#include "audiofileimporter.h"
#include "../collections/musiccollection.h"
#include "../entry.h"
#include "../field.h"
#include "../latin1literal.h"
#include "../imagefactory.h"
#include "../tellico_utils.h"
#include "../tellico_kernel.h"
#include "../progressmanager.h"
#include "../tellico_debug.h"
#ifdef HAVE_TAGLIB
#include <taglib/fileref.h>
#include <taglib/tag.h>
#include <taglib/id3v2tag.h>
#include <taglib/mpegfile.h>
#include <taglib/vorbisfile.h>
#include <taglib/flacfile.h>
#include <taglib/audioproperties.h>
#endif
#include <klocale.h>
#include <kapplication.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqvgroupbox.h>
#include <tqcheckbox.h>
#include <tqdir.h>
#include <tqwhatsthis.h>
using Tellico::Import::AudioFileImporter;
AudioFileImporter::AudioFileImporter(const KURL& url_) : Tellico::Import::Importer(url_)
, m_coll(0)
, m_widget(0)
, m_cancelled(false) {
}
bool AudioFileImporter::canImport(int type) const {
return type == Data::Collection::Album;
}
Tellico::Data::CollPtr AudioFileImporter::collection() {
#ifndef HAVE_TAGLIB
return 0;
#else
if(m_coll) {
return m_coll;
}
ProgressItem& item = ProgressManager::self()->newProgressItem(this, i18n("Scanning audio files..."), true);
item.setTotalSteps(100);
connect(&item, TQT_SIGNAL(signalCancelled(ProgressItem*)), TQT_SLOT(slotCancel()));
ProgressItem::Done done(this);
// TODO: allow remote audio file importing
TQStringList dirs = url().path();
if(m_recursive->isChecked()) {
dirs += Tellico::findAllSubDirs(dirs[0]);
}
if(m_cancelled) {
return 0;
}
const bool showProgress = options() & ImportProgress;
TQStringList files;
for(TQStringList::ConstIterator it = dirs.begin(); !m_cancelled && it != dirs.end(); ++it) {
if((*it).isEmpty()) {
continue;
}
TQDir dir(*it);
dir.setFilter(TQDir::Files | TQDir::Readable | TQDir::Hidden); // hidden since I want directory files
const TQStringList list = dir.entryList();
for(TQStringList::ConstIterator it2 = list.begin(); it2 != list.end(); ++it2) {
files += dir.absFilePath(*it2);
}
// kapp->processEvents(); not needed ?
}
if(m_cancelled) {
return 0;
}
item.setTotalSteps(files.count());
const TQString title = TQString::tqfromLatin1("title");
const TQString artist = TQString::tqfromLatin1("artist");
const TQString year = TQString::tqfromLatin1("year");
const TQString genre = TQString::tqfromLatin1("genre");
const TQString track = TQString::tqfromLatin1("track");
const TQString comments = TQString::tqfromLatin1("comments");
const TQString file = TQString::tqfromLatin1("file");
m_coll = new Data::MusicCollection(true);
const bool addFile = m_addFilePath->isChecked();
const bool addBitrate = m_addBitrate->isChecked();
Data::FieldPtr f;
if(addFile) {
f = m_coll->fieldByName(file);
if(!f) {
f = new Data::Field(file, i18n("Files"), Data::Field::Table);
m_coll->addField(f);
}
f->setProperty(TQString::tqfromLatin1("column1"), i18n("Files"));
if(addBitrate) {
f->setProperty(TQString::tqfromLatin1("columns"), TQChar('2'));
f->setProperty(TQString::tqfromLatin1("column2"), i18n("Bitrate"));
} else {
f->setProperty(TQString::tqfromLatin1("columns"), TQChar('1'));
}
}
TQMap<TQString, Data::EntryPtr> albumMap;
TQStringList directoryFiles;
const uint stepSize = TQMAX(static_cast<size_t>(1), files.count() / 100);
bool changeTrackTitle = true;
uint j = 0;
for(TQStringList::ConstIterator it = files.begin(); !m_cancelled && it != files.end(); ++it, ++j) {
TagLib::FileRef f(TQFile::encodeName(*it));
if(f.isNull() || !f.tag()) {
if((*it).endsWith(TQString::tqfromLatin1("/.directory"))) {
directoryFiles += *it;
}
continue;
}
TagLib::Tag* tag = f.tag();
TQString album = TQString(TStringToQString(tag->album())).stripWhiteSpace();
if(album.isEmpty()) {
// can't do anything since tellico entries are by album
kdWarning() << "Skipping: no album listed for " << *it << endl;
continue;
}
int disc = discNumber(f);
if(disc > 1 && !m_coll->hasField(TQString::tqfromLatin1("track%1").tqarg(disc))) {
Data::FieldPtr f2 = new Data::Field(TQString::tqfromLatin1("track%1").tqarg(disc),
i18n("Tracks (Disc %1)").tqarg(disc),
Data::Field::Table);
f2->setFormatFlag(Data::Field::FormatTitle);
f2->setProperty(TQString::tqfromLatin1("columns"), TQChar('3'));
f2->setProperty(TQString::tqfromLatin1("column1"), i18n("Title"));
f2->setProperty(TQString::tqfromLatin1("column2"), i18n("Artist"));
f2->setProperty(TQString::tqfromLatin1("column3"), i18n("Length"));
m_coll->addField(f2);
if(changeTrackTitle) {
Data::FieldPtr newTrack = new Data::Field(*m_coll->fieldByName(track));
newTrack->setTitle(i18n("Tracks (Disc %1)").tqarg(1));
m_coll->modifyField(newTrack);
changeTrackTitle = false;
}
}
bool various = false;
bool exists = true;
Data::EntryPtr entry = 0;
if(!(entry = albumMap[album.lower()])) {
entry = new Data::Entry(m_coll);
albumMap.insert(album.lower(), entry);
exists = false;
}
// album entries use the album name as the title
entry->setField(title, album);
TQString a = TQString(TStringToQString(tag->artist())).stripWhiteSpace();
if(!a.isEmpty()) {
if(exists && entry->field(artist).lower() != a.lower()) {
various = true;
entry->setField(artist, i18n("(Various)"));
} else {
entry->setField(artist, a);
}
}
if(tag->year() > 0) {
entry->setField(year, TQString::number(tag->year()));
}
if(!tag->genre().isEmpty()) {
entry->setField(genre, TQString(TStringToQString(tag->genre())).stripWhiteSpace());
}
if(!tag->title().isEmpty()) {
int trackNum = tag->track();
if(trackNum <= 0) { // try to figure out track number from file name
TQFileInfo f(*it);
TQString fileName = f.baseName();
TQString numString;
int i = 0;
const int len = fileName.length();
while(fileName[i].isNumber() && i < len) {
i++;
}
if(i == 0) { // does not start with a number
i = len - 1;
while(i >= 0 && fileName[i].isNumber()) {
i--;
}
// file name ends with a number
if(i != len - 1) {
numString = fileName.mid(i + 1);
}
} else {
numString = fileName.mid(0, i);
}
bool ok;
int number = numString.toInt(&ok);
if(ok) {
trackNum = number;
}
}
if(trackNum > 0) {
TQString t = TQString(TStringToQString(tag->title())).stripWhiteSpace();
t += "::" + a;
const int len = f.audioProperties()->length();
if(len > 0) {
t += "::" + Tellico::minutes(len);
}
TQString realTrack = disc > 1 ? track + TQString::number(disc) : track;
entry->setField(realTrack, insertValue(entry->field(realTrack), t, trackNum));
if(addFile) {
TQString fileValue = *it;
if(addBitrate) {
fileValue += "::" + TQString::number(f.audioProperties()->bitrate());
}
entry->setField(file, insertValue(entry->field(file), fileValue, trackNum));
}
} else {
myDebug() << *it << " contains no track number and track number cannot be determined, so the track is not imported." << endl;
}
} else {
myDebug() << *it << " has an empty title, so the track is not imported." << endl;
}
if(!tag->comment().stripWhiteSpace().isEmpty()) {
TQString c = entry->field(comments);
if(!c.isEmpty()) {
c += TQString::tqfromLatin1("<br/>");
}
if(!tag->title().isEmpty()) {
c += TQString::tqfromLatin1("<em>") + TQString(TStringToQString(tag->title())).stripWhiteSpace() + TQString::tqfromLatin1("</em> - ");
}
c += TQString(TStringToQString(tag->comment().stripWhiteSpace())).stripWhiteSpace();
entry->setField(comments, c);
}
if(!exists) {
m_coll->addEntries(entry);
}
if(showProgress && j%stepSize == 0) {
ProgressManager::self()->setTotalSteps(this, files.count() + directoryFiles.count());
ProgressManager::self()->setProgress(this, j);
kapp->processEvents();
}
/* kdDebug() << "-- TAG --" << endl;
kdDebug() << "title - \"" << tag->title().to8Bit() << "\"" << endl;
kdDebug() << "artist - \"" << tag->artist().to8Bit() << "\"" << endl;
kdDebug() << "album - \"" << tag->album().to8Bit() << "\"" << endl;
kdDebug() << "year - \"" << tag->year() << "\"" << endl;
kdDebug() << "comment - \"" << tag->comment().to8Bit() << "\"" << endl;
kdDebug() << "track - \"" << tag->track() << "\"" << endl;
kdDebug() << "genre - \"" << tag->genre().to8Bit() << "\"" << endl;*/
}
if(m_cancelled) {
m_coll = 0;
return 0;
}
TQTextStream ts;
TQRegExp iconRx(TQString::tqfromLatin1("Icon\\s*=\\s*(.*)"));
for(TQStringList::ConstIterator it = directoryFiles.begin(); !m_cancelled && it != directoryFiles.end(); ++it, ++j) {
TQFile file(*it);
if(!file.open(IO_ReadOnly)) {
continue;
}
ts.unsetDevice();
ts.setDevice(TQT_TQIODEVICE(&file));
for(TQString line = ts.readLine(); !line.isNull(); line = ts.readLine()) {
if(!iconRx.exactMatch(line)) {
continue;
}
TQDir thisDir(*it);
thisDir.cdUp();
TQFileInfo fi(thisDir, iconRx.cap(1));
Data::EntryPtr entry = albumMap[thisDir.dirName()];
if(!entry) {
continue;
}
KURL u;
u.setPath(fi.absFilePath());
TQString id = ImageFactory::addImage(u, true);
if(!id.isEmpty()) {
entry->setField(TQString::tqfromLatin1("cover"), id);
}
break;
}
if(showProgress && j%stepSize == 0) {
ProgressManager::self()->setProgress(this, j);
kapp->processEvents();
}
}
if(m_cancelled) {
m_coll = 0;
return 0;
}
return m_coll;
#endif
}
TQWidget* AudioFileImporter::widget(TQWidget* parent_, const char* name_) {
if(m_widget) {
return m_widget;
}
m_widget = new TQWidget(parent_, name_);
TQVBoxLayout* l = new TQVBoxLayout(m_widget);
TQVGroupBox* box = new TQVGroupBox(i18n("Audio File Options"), m_widget);
m_recursive = new TQCheckBox(i18n("Recursive &folder search"), box);
TQWhatsThis::add(m_recursive, i18n("If checked, folders are recursively searched for audio files."));
// by default, make it checked
m_recursive->setChecked(true);
m_addFilePath = new TQCheckBox(i18n("Include file &location"), box);
TQWhatsThis::add(m_addFilePath, i18n("If checked, the file names for each track are added to the entries."));
m_addFilePath->setChecked(false);
connect(m_addFilePath, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotAddFileToggled(bool)));
m_addBitrate = new TQCheckBox(i18n("Include &bitrate"), box);
TQWhatsThis::add(m_addBitrate, i18n("If checked, the bitrate for each track is added to the entries."));
m_addBitrate->setChecked(false);
m_addBitrate->setEnabled(false);
l->addWidget(box);
l->addStretch(1);
return m_widget;
}
// pos_ is NOT zero-indexed!
TQString AudioFileImporter::insertValue(const TQString& str_, const TQString& value_, uint pos_) {
TQStringList list = Data::Field::split(str_, true);
for(uint i = list.count(); i < pos_; ++i) {
list += TQString();
}
if(!list[pos_-1].isNull()) {
myDebug() << "AudioFileImporter::insertValue() - overwriting track " << pos_ << endl;
myDebug() << "*** Old value: " << list[pos_-1] << endl;
myDebug() << "*** New value: " << value_ << endl;
}
list[pos_-1] = value_;
return list.join(TQString::tqfromLatin1("; "));
}
void AudioFileImporter::slotCancel() {
m_cancelled = true;
}
void AudioFileImporter::slotAddFileToggled(bool on_) {
m_addBitrate->setEnabled(on_);
if(!on_) {
m_addBitrate->setChecked(false);
}
}
int AudioFileImporter::discNumber(const TagLib::FileRef& ref_) const {
// default to 1 unless otherwise
int num = 1;
#ifdef HAVE_TAGLIB
TQString disc;
if(TagLib::MPEG::File* file = dynamic_cast<TagLib::MPEG::File*>(ref_.file())) {
if(file->ID3v2Tag() && !file->ID3v2Tag()->frameListMap()["TPOS"].isEmpty()) {
disc = TQString(TStringToQString(file->ID3v2Tag()->frameListMap()["TPOS"].front()->toString())).stripWhiteSpace();
}
} else if(TagLib::Ogg::Vorbis::File* file = dynamic_cast<TagLib::Ogg::Vorbis::File*>(ref_.file())) {
if(file->tag() && !file->tag()->fieldListMap()["DISCNUMBER"].isEmpty()) {
disc = TQString(TStringToQString(file->tag()->fieldListMap()["DISCNUMBER"].front())).stripWhiteSpace();
}
} else if(TagLib::FLAC::File* file = dynamic_cast<TagLib::FLAC::File*>(ref_.file())) {
if(file->xiphComment() && !file->xiphComment()->fieldListMap()["DISCNUMBER"].isEmpty()) {
disc = TQString(TStringToQString(file->xiphComment()->fieldListMap()["DISCNUMBER"].front())).stripWhiteSpace();
}
}
if(!disc.isEmpty()) {
int pos = disc.tqfind('/');
int n;
bool ok;
if(pos == -1) {
n = disc.toInt(&ok);
} else {
n = disc.left(pos).toInt(&ok);
}
if(ok && n > 0) {
num = n;
}
}
#endif
return num;
}
#include "audiofileimporter.moc"
|