File: audiocddevice.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (458 lines) | stat: -rw-r--r-- 11,525 bytes parent folder | download
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 "audiocddevice.h"
#ifdef CDDB_FOUND
#include "cddbinterface.h"
#endif
#ifdef MusicBrainz5_FOUND
#include "musicbrainz.h"
#endif
#include "extractjob.h"
#include "gui/covers.h"
#include "gui/settings.h"
#include "models/mpdlibrarymodel.h"
#include "models/musiclibraryitemsong.h"
#include "models/playqueuemodel.h"
#include "mpd-interface/mpdconnection.h"
#include "solid-lite/block.h"
#include "support/utils.h"
#include "widgets/icons.h"
#include <QDir>
#include <QUrl>
#include <QUrlQuery>

const QLatin1String AudioCdDevice::constAnyDev("-");

QString AudioCdDevice::coverUrl(QString udi)
{
	udi.replace(" ", "_");
	udi.replace("\n", "_");
	udi.replace("\t", "_");
	udi.replace("/", "_");
	udi.replace(":", "_");
	return Song::constCddaProtocol + udi;
}

QString AudioCdDevice::getDevice(const QUrl& url)
{
	if (QLatin1String("cdda") == url.scheme()) {
		QUrlQuery q(url);
		if (q.hasQueryItem("dev")) {
			return q.queryItemValue("dev");
		}
		return constAnyDev;
	}

	QString path = url.path();
	if (path.startsWith("/run/user/")) {
		const QString marker = QLatin1String("/gvfs/cdda:host=");
		int pos = path.lastIndexOf(marker);
		if (-1 != pos) {
			return QLatin1String("/dev/") + path.mid(pos + marker.length());
		}
	}
	return QString();
}

AudioCdDevice::AudioCdDevice(MusicLibraryModel* m, Solid::Device& dev)
	: Device(m, dev, false, true)
#ifdef CDDB_FOUND
	  ,
	  cddb(0)
#endif
#ifdef MusicBrainz5_FOUND
	  ,
	  mb(0)
#endif
	  ,
	  year(0), disc(0), time(0xFFFFFFFF), lookupInProcess(false), autoPlay(false)
{
	icn = Icons::self()->albumMonoIcon;
	drive = dev.parent().as<Solid::OpticalDrive>();
	Solid::Block* block = dev.as<Solid::Block>();
	if (block) {
		device = block->device();
	}
	else {// With UDisks2 we cannot get block from device :-(
		QStringList parts = dev.udi().split("/", CANTATA_SKIP_EMPTY);
		if (!parts.isEmpty()) {
			parts = parts.last().split(":");
			if (!parts.isEmpty()) {
				device = "/dev/" + parts.first();
			}
		}
	}
	if (!device.isEmpty()) {
		static bool registeredTypes = false;
		if (!registeredTypes) {
			qRegisterMetaType<CdAlbum>("CdAlbum");
			qRegisterMetaType<QList<CdAlbum>>("QList<CdAlbum>");
			registeredTypes = true;
		}
		devPath = Song::constCddaProtocol + device + QChar('/');
#if defined CDDB_FOUND && defined MusicBrainz5_FOUND
		connectService(Settings::self()->useCddb());
#else
		connectService(true);
#endif
		detailsString = tr("Reading disc");
		setStatusMessage(detailsString);
		lookupInProcess = true;
		connect(Covers::self(), SIGNAL(cover(const Song&, const QImage&, const QString&)),
		        this, SLOT(setCover(const Song&, const QImage&, const QString&)));
		emit lookup(Settings::self()->cdAuto());
	}
}

AudioCdDevice::~AudioCdDevice()
{
#ifdef CDDB_FOUND
	if (cddb) {
		cddb->deleteLater();
		cddb = 0;
	}
#endif
#ifdef MusicBrainz5_FOUND
	if (mb) {
		mb->deleteLater();
		mb = 0;
	}
#endif
	// Remove any downloaded cover image...
	if (!coverImage.fileName.isEmpty() && coverImage.fileName.startsWith(Utils::cacheDir(Covers::constCddaCoverDir, false))) {
		QFile::remove(coverImage.fileName);
	}
}

void AudioCdDevice::dequeue()
{
	QList<Song> tracks;
	for (const MusicLibraryItem* item : childItems()) {
		if (MusicLibraryItem::Type_Song == item->itemType()) {
			Song song = static_cast<const MusicLibraryItemSong*>(item)->song();
			song.file = path() + song.file;
			tracks.append(song);
		}
	}

	if (!tracks.isEmpty()) {
		PlayQueueModel::self()->remove(tracks);
	}
}

bool AudioCdDevice::isAudioDevice(const QString& dev) const
{
	return constAnyDev == dev || device == dev;
}

void AudioCdDevice::connectService(bool useCddb)
{
#if defined CDDB_FOUND && defined MusicBrainz5_FOUND
	if (cddb && !useCddb) {
		cddb->deleteLater();
		cddb = 0;
	}
	if (mb && useCddb) {
		mb->deleteLater();
		mb = 0;
	}
#else
	Q_UNUSED(useCddb)
#endif

#ifdef CDDB_FOUND
	if (!cddb
#ifdef MusicBrainz5_FOUND
	    && useCddb
#endif
	) {
		cddb = new CddbInterface(device);
		connect(cddb, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
		connect(cddb, SIGNAL(initialDetails(CdAlbum)), this, SLOT(setDetails(CdAlbum)));
		connect(cddb, SIGNAL(matches(const QList<CdAlbum>&)), SLOT(cdMatches(const QList<CdAlbum>&)));
		connect(this, SIGNAL(lookup(bool)), cddb, SLOT(lookup(bool)));
	}
#endif

#ifdef MusicBrainz5_FOUND
	if (!mb
#ifdef CDDB_FOUND
	    && !useCddb
#endif
	) {
		mb = new MusicBrainz(device);
		connect(mb, SIGNAL(error(QString)), this, SIGNAL(error(QString)));
		connect(mb, SIGNAL(initialDetails(CdAlbum)), this, SLOT(setDetails(CdAlbum)));
		connect(mb, SIGNAL(matches(const QList<CdAlbum>&)), SLOT(cdMatches(const QList<CdAlbum>&)));
		connect(this, SIGNAL(lookup(bool)), mb, SLOT(lookup(bool)));
	}
#endif
}

void AudioCdDevice::rescan(bool useCddb)
{
	if (!device.isEmpty()) {
		connectService(useCddb);
		lookupInProcess = true;
		emit lookup(true);
	}
}

void AudioCdDevice::toggle()
{
	if (drive) {
		stop();
		drive->eject();
		PlayQueueModel::self()->removeCantataStreams(true);
	}
}

void AudioCdDevice::stop()
{
}

void AudioCdDevice::copySongTo(const Song& s, const QString& musicPath, bool overwrite, bool copyCover)
{
	jobAbortRequested = false;
	if (!isConnected()) {
		emit actionStatus(NotConnected);
		return;
	}

	needToFixVa = opts.fixVariousArtists && s.isVariousArtists();

	if (!overwrite) {
		Song check = s;

		if (needToFixVa) {
			Device::fixVariousArtists(QString(), check, false);
		}
		if (MpdLibraryModel::self()->songExists(check)) {
			emit actionStatus(SongExists);
			return;
		}
	}

	DeviceOptions mpdOpts;
	mpdOpts.load(MPDConnectionDetails::configGroupName(MPDConnection::self()->getDetails().name), true);

	Encoders::Encoder encoder = Encoders::getEncoder(mpdOpts.transcoderCodec);
	if (encoder.codec.isEmpty()) {
		emit actionStatus(CodecNotAvailable);
		return;
	}

	QString source = device;
	QString baseDir = MPDConnection::self()->getDetails().dir;
	currentDestFile = encoder.changeExtension(baseDir + musicPath);
	QDir dir(Utils::getDir(currentDestFile));
	if (!dir.exists() && !Utils::createWorldReadableDir(dir.absolutePath(), baseDir)) {
		emit actionStatus(DirCreationFaild);
		return;
	}

	currentSong = s;
	ExtractJob* job = new ExtractJob(encoder, mpdOpts.transcoderValue, source, currentDestFile, currentSong, copyCover ? coverImage.fileName : QString());
	connect(job, SIGNAL(result(int)), SLOT(copySongToResult(int)));
	connect(job, SIGNAL(percent(int)), SLOT(percent(int)));
	job->start();
}

quint32 AudioCdDevice::totalTime()
{
	if (0xFFFFFFFF == time) {
		time = 0;
		for (MusicLibraryItem* i : childItems()) {
			time += static_cast<MusicLibraryItemSong*>(i)->song().time;
		}
	}

	return time;
}

void AudioCdDevice::percent(int pc)
{
	if (jobAbortRequested && 100 != pc) {
		FileJob* job = qobject_cast<FileJob*>(sender());
		if (job) {
			job->stop();
		}
		return;
	}
	emit progress(pc);
}

void AudioCdDevice::copySongToResult(int status)
{
	ExtractJob* job = qobject_cast<ExtractJob*>(sender());
	FileJob::finished(job);
	if (jobAbortRequested) {
		if (job && job->wasStarted() && QFile::exists(currentDestFile)) {
			QFile::remove(currentDestFile);
		}
		return;
	}
	if (Ok != status) {
		emit actionStatus(status);
	}
	else {
		currentSong.file = currentDestFile.mid(MPDConnection::self()->getDetails().dir.length());
		QString origPath;
		if (MPDConnection::self()->isMopidy()) {
			origPath = currentSong.file;
			currentSong.file = Song::encodePath(currentSong.file);
		}
		if (needToFixVa) {
			currentSong.revertVariousArtists();
		}
		Utils::setFilePerms(currentDestFile);
		//        MusicLibraryModel::self()->addSongToList(currentSong);
		//        DirViewModel::self()->addFileToList(origPath.isEmpty() ? currentSong.file : origPath,
		//                                            origPath.isEmpty() ? QString() : currentSong.file);
		emit actionStatus(Ok, job && job->coverCopied());
	}
}

static const int constBytesPerSecond = 44100 * 4;

void AudioCdDevice::setDetails(const CdAlbum& a)
{
	bool differentAlbum = album != a.name || artist != a.artist;
	lookupInProcess = false;
	setData(a.artist);
	album = a.name;
	artist = a.artist;
	composer = a.composer;
	genre = a.genre;
	year = a.year;
	disc = a.disc;
	update = new MusicLibraryItemRoot();
	int totalDuration = 0;
	for (Song s : a.tracks) {
		totalDuration += s.time;
		s.size = s.time * constBytesPerSecond;
		update->append(new MusicLibraryItemSong(s, update));
	}
	setStatusMessage(QString());
	detailsString = tr("%n Tracks (%1)", "", a.tracks.count()).arg(Utils::formatTime(totalDuration));
	emit updating(id(), false);
	if (differentAlbum && !a.isDefault) {
		Song s;
		s.artist = s.albumartist = artist;
		s.album = album;
		s.file = AudioCdDevice::coverUrl(id());
		s.title = id();
		s.type = Song::Cdda;
		Covers::Image img = Covers::self()->requestImage(s, true);
		if (!img.img.isNull()) {
			setCover(img);
		}
	}

	if (autoPlay) {
		autoPlay = false;
		playTracks();
	}
	else {
		updateDetails();
	}
}

void AudioCdDevice::cdMatches(const QList<CdAlbum>& albums)
{
	lookupInProcess = false;
	if (1 == albums.count()) {
		setDetails(albums.at(0));
	}
	else if (albums.count() > 1) {
		// More than 1 match, so prompt user!
		emit matches(id(), albums);
	}
}

void AudioCdDevice::setCover(const Covers::Image& img)
{
	coverImage = img;
	updateStatus();
}

void AudioCdDevice::scaleCoverPix(int size) const
{
	if (!coverImage.img.isNull()) {
		if (scaledCover.width() != size && scaledCover.height() != size) {
			scaledCover = QPixmap::fromImage(coverImage.img.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
		}
	}
}

void AudioCdDevice::setCover(const Song& song, const QImage& img, const QString& file)
{
	if (song.isCdda() && song.albumartist == artist && song.album == album) {
		setCover(Covers::Image(img, file));
	}
}

void AudioCdDevice::autoplay()
{
	if (childCount()) {
		playTracks();
	}
	else {
		autoPlay = true;
	}
}

void AudioCdDevice::playTracks()
{
	QList<Song> tracks;
	for (const MusicLibraryItem* item : childItems()) {
		if (MusicLibraryItem::Type_Song == item->itemType()) {
			Song song = static_cast<const MusicLibraryItemSong*>(item)->song();
			song.file = path() + song.file;
			tracks.append(song);
		}
	}

	if (!tracks.isEmpty()) {
		emit play(tracks);
	}
}

void AudioCdDevice::updateDetails()
{
	QList<Song> tracks;
	for (const MusicLibraryItem* item : childItems()) {
		if (MusicLibraryItem::Type_Song == item->itemType()) {
			Song song = static_cast<const MusicLibraryItemSong*>(item)->song();
			song.file = path() + song.file;
			tracks.append(song);
		}
	}

	if (!tracks.isEmpty()) {
		emit updatedDetails(tracks);
	}
}

#include "moc_audiocddevice.cpp"