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
|
/*
* 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 "device.h"
#include "config.h"
#include "gui/covers.h"
#include "mpd-interface/mpdconnection.h"
#include "support/utils.h"
#include <QBuffer>
#include <QDir>
#include <QTemporaryFile>
#include <QTimer>
#ifdef ENABLE_DEVICES_SUPPORT
#include "models/devicesmodel.h"
#include "umsdevice.h"
#ifdef MTP_FOUND
#include "mtpdevice.h"
#endif// MTP_FOUND
#if defined CDDB_FOUND || defined MusicBrainz5_FOUND
#include "audiocddevice.h"
#endif// defined CDDB_FOUND || defined MusicBrainz5_FOUND
#include "encoders.h"
#include "models/musiclibraryitemalbum.h"
#include "models/musiclibraryitemartist.h"
#include "models/musiclibraryitemsong.h"
#include "models/musiclibrarymodel.h"
#include "mpd-interface/mpdparseutils.h"
#include "mpd-interface/song.h"
#include "solid-lite/genericinterface.h"
#include "solid-lite/opticaldisc.h"
#include "solid-lite/portablemediaplayer.h"
#include "solid-lite/storageaccess.h"
#include "solid-lite/storagedrive.h"
#include "tags/tags.h"
#include "widgets/icons.h"
#endif// ENABLE_DEVICES_SUPPORT
#include <QDebug>
#define DBUG_CLASS(CLASS) \
if (DevicesModel::debugEnabled()) qWarning() << CLASS << __FUNCTION__
#define DBUG DBUG_CLASS(metaObject()->className())
void Device::moveDir(const QString& from, const QString& to, const QString& base, const QString& coverFile)
{
QDir d(from);
if (d.exists()) {
QFileInfoList entries = d.entryInfoList(QDir::Files | QDir::NoSymLinks | QDir::Dirs | QDir::NoDotAndDotDot);
QSet<QString> fileTypes = QSet<QString>() << "jpg"
<< "png"
<< "pamp"
<< "txt"
<< "lyrics";
for (const QFileInfo& info : entries) {
if (!info.isDir() && (MPDConnection::isPlaylist(info.fileName()) || fileTypes.contains(info.suffix().toLower()))) {
QFile::rename(from + '/' + info.fileName(), to + '/' + info.fileName());
}
}
cleanDir(from, base, coverFile);
}
}
void Device::cleanDir(const QString& dir, const QString& base, const QString& coverFile, int level)
{
QDir d(dir);
if (d.exists()) {
QFileInfoList entries = d.entryInfoList(QDir::Files | QDir::NoSymLinks | QDir::Dirs | QDir::NoDotAndDotDot);
QList<QString> extraFiles;
QSet<QString> others = Utils::listToSet(Covers::standardNames());
others << coverFile << "albumart.pamp";
for (const QFileInfo& info : entries) {
if (info.isDir()) {
return;
}
if (!others.contains(info.fileName())) {
return;
}
extraFiles.append(info.absoluteFilePath());
}
for (const QString& cf : extraFiles) {
if (!QFile::remove(cf)) {
return;
}
}
if (Utils::fixPath(dir) == Utils::fixPath(base)) {
return;
}
QString dirName = d.dirName();
if (dirName.isEmpty()) {
return;
}
d.cdUp();
if (!d.rmdir(dirName)) {
return;
}
if (level >= 3) {
return;
}
QString upDir = d.absolutePath();
if (Utils::fixPath(upDir) != Utils::fixPath(base)) {
cleanDir(upDir, base, coverFile, level + 1);
}
}
}
Song Device::fixPath(const Song& orig, bool fullPath) const
{
Song s = orig;
if (fullPath) {
QString p = path();
if (!p.isEmpty()) {
s.file = p + s.file;
}
}
return s;
}
#ifdef ENABLE_DEVICES_SUPPORT
#include <unistd.h>
const QLatin1String Device::constNoCover("-");
const QLatin1String Device::constEmbedCover("+");
Device* Device::create(MusicLibraryModel* m, const QString& udi)
{
Solid::Device device = Solid::Device(udi);
DBUG_CLASS("Device") << udi;
if (device.is<Solid::OpticalDisc>()) {
#if defined CDDB_FOUND || defined MusicBrainz5_FOUND
DBUG_CLASS("Device") << "AudioCD";
return Encoders::getAvailable().isEmpty() ? 0 : new AudioCdDevice(m, device);
#endif
}
else if (device.is<Solid::PortableMediaPlayer>()) {
#ifdef MTP_FOUND
Solid::PortableMediaPlayer* pmp = device.as<Solid::PortableMediaPlayer>();
if (pmp->supportedProtocols().contains(QLatin1String("mtp"))) {
Solid::GenericInterface* iface = device.as<Solid::GenericInterface>();
if (iface) {
QMap<QString, QVariant> properties = iface->allProperties();
QMap<QString, QVariant>::ConstIterator bus = properties.find(QLatin1String("BUSNUM"));
QMap<QString, QVariant>::ConstIterator dev = properties.find(QLatin1String("DEVNUM"));
DBUG_CLASS("Device") << "MTP";
return properties.constEnd() == bus || properties.end() == dev
? nullptr
: new MtpDevice(m, device, bus.value().toUInt(), dev.value().toUInt());
}
}
#endif
}
else if (device.is<Solid::StorageAccess>()) {
DBUG_CLASS("Device") << "Storage";
if ((!device.parent().as<Solid::StorageDrive>() || Solid::StorageDrive::Usb != device.parent().as<Solid::StorageDrive>()->bus()) && (!device.as<Solid::StorageDrive>() || Solid::StorageDrive::Usb != device.as<Solid::StorageDrive>()->bus())) {
DBUG_CLASS("Device") << "Not drive / usb ?";
return nullptr;
}
DBUG_CLASS("Device") << "UMS, Vendor:" << device.vendor();
return new UmsDevice(m, device);
}
return nullptr;
}
bool Device::fixVariousArtists(const QString& file, Song& song, bool applyFix)
{
Song orig = song;
if (!file.isEmpty() && song.albumartist.isEmpty()) {
song = Tags::read(file);
}
if (song.artist.isEmpty() || song.albumartist.isEmpty() || !Song::isVariousArtists(song.albumartist)) {
song = orig;
return false;
}
bool needsUpdating = false;
if (!applyFix) {// Then real artist is embedded in track title...
needsUpdating = song.revertVariousArtists();
}
else if (applyFix) {// We must be copying to device, and need to place song artist into title...
needsUpdating = song.fixVariousArtists();
}
if (needsUpdating && (file.isEmpty() || Tags::Update_Modified == Tags::updateArtistAndTitle(file, song))) {
return true;
}
song = orig;
return false;
}
static QByteArray save(const QImage& img)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "JPG");
buffer.close();
return ba;
}
void Device::embedCover(const QString& file, Song& song, unsigned int coverMaxSize)
{
if (Tags::readImage(file).isNull()) {
Covers::Image coverImage = Covers::self()->getImage(song);
if (!coverImage.img.isNull()) {
QByteArray imgData;
if (coverMaxSize && (coverImage.img.width() > (int)coverMaxSize || coverImage.img.height() > (int)coverMaxSize)) {
imgData = save(coverImage.img.scaled(QSize(coverMaxSize, coverMaxSize), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
else if (!coverImage.fileName.endsWith(".jpg", Qt::CaseInsensitive) || !QFile::exists(coverImage.fileName)) {
imgData = save(coverImage.img);
}
else {
QFile f(coverImage.fileName);
if (f.open(QIODevice::ReadOnly)) {
imgData = f.readAll();
}
else {
imgData = save(coverImage.img);
}
}
Tags::embedImage(file, imgData);
}
}
}
QTemporaryFile* Device::copySongToTemp(Song& song)
{
QTemporaryFile* temp = new QTemporaryFile();
int index = song.file.lastIndexOf('.');
if (index > 0) {
temp = new QTemporaryFile("cantata_XXXXXX" + song.file.mid(index));
}
else {
temp = new QTemporaryFile("cantata_XXXXXX");
}
temp->setAutoRemove(false);
if (temp->open()) {
temp->close();
if (QFile::exists(temp->fileName())) {
QFile::remove(temp->fileName());// Copy will *not* overwrite file!
}
if (!QFile::copy(song.file, temp->fileName())) {
temp->remove();
delete temp;
temp = nullptr;
}
}
return temp;
}
bool Device::isLossless(const QString& file)
{
static QStringList lossless = QStringList() << ".flac"
<< ".wav";// TODO: ALAC?? Its in .m4a!!!
QString lower = file.toLower();
for (const auto& e : lossless) {
if (lower.endsWith(e)) {
return true;
}
}
return false;
}
Device::Device(MusicLibraryModel* m, Solid::Device& dev, bool albumArtistSupport, bool flat)
: MusicLibraryItemRoot(dev.product().startsWith(dev.vendor()) ? dev.product() : (dev.vendor() + QChar(' ') + dev.product()), albumArtistSupport, flat), configured(false), solidDev(dev), deviceId(dev.udi()), update(nullptr), needToFixVa(false), jobAbortRequested(false), transcoding(false)
{
m_model = m;
icn = Icons::self()->folderListIcon;
m_itemData[0] = m_itemData[0].toUpper();
}
Device::Device(MusicLibraryModel* m, const QString& name, const QString& id)
: MusicLibraryItemRoot(name), configured(false), deviceId(id), update(nullptr), needToFixVa(false), jobAbortRequested(false), transcoding(false)
{
m_model = m;
icn = solidDev.isValid() ? Icon::get(solidDev.icon()) : Icons::self()->folderListIcon;
}
void Device::saveCache()
{
QTimer::singleShot(0, this, SIGNAL(cacheSaved()));
}
void Device::applyUpdate()
{
if (!update) {
return;
}
/*if (m_childItems.count() && update && update->childCount()) {
QSet<Song> currentSongs=allSongs();
QSet<Song> updateSongs=update->allSongs();
QSet<Song> removed=currentSongs-updateSongs;
QSet<Song> added=updateSongs-currentSongs;
for (const Song &s: removed) {
removeSongFromList(s);
}
for (const Song &s: added) {
addSongToList(s);
}
delete update;
} else*/
{
int oldCount = childCount();
if (oldCount > 0) {
m_model->beginRemoveRows(index(), 0, oldCount - 1);
clearItems();
m_model->endRemoveRows();
}
int newCount = newRows();
if (newCount > 0) {
m_model->beginInsertRows(index(), 0, newCount - 1);
foreach (MusicLibraryItem* item, update->childItems()) {
item->setParent(this);
}
if (AudioCd != devType()) {
refreshIndexes();
}
m_model->endInsertRows();
}
}
delete update;
update = nullptr;
}
QModelIndex Device::index() const
{
return m_model->createIndex(m_model->row((void*)this), 0, (void*)this);
}
void Device::setStatusMessage(const QString& msg)
{
statusMsg = msg;
updateStatus();
}
void Device::updateStatus()
{
QModelIndex modelIndex = index();
emit m_model->dataChanged(modelIndex, modelIndex);
}
void Device::songCount(int c)
{
setStatusMessage(tr("Updating (%1)...").arg(c));
}
void Device::updatePercentage(int pc)
{
setStatusMessage(tr("Updating (%1%)...").arg(pc));
}
#endif// ENABLE_DEVICES_SUPPORT
#include "moc_device.cpp"
|