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
|
/*
* 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 "filejob.h"
#include "context/songview.h"
#include "device.h"
#include "gui/covers.h"
#include "support/globalstatic.h"
#include "support/thread.h"
#include "support/utils.h"
#include <QDebug>
#include <QFile>
#include <QTemporaryFile>
#include <QTimer>
GLOBAL_STATIC(FileThread, instance)
FileThread::FileThread()
: thread(nullptr)
{
}
FileThread::~FileThread()
{
// stop();
}
void FileThread::addJob(FileJob* job)
{
if (!thread) {
thread = new Thread(metaObject()->className());
thread->start();
}
job->moveToThread(thread);
}
void FileThread::stop()
{
if (thread) {
thread->stop();
thread = nullptr;
}
}
FileJob::FileJob()
: stopRequested(false), progressPercent(0)
{
FileThread::self()->addJob(this);
// Cant call deleteLater here, as in the device's xxResult() slots "sender()" returns
// null. Therefore, xxResult() slots need to call finished()
//connect(this, SIGNAL(result(int)), SLOT(deleteLater()));
}
void FileJob::start()
{
QTimer::singleShot(0, this, SLOT(run()));
}
void FileJob::setPercent(int pc)
{
if (pc != progressPercent) {
progressPercent = pc;
emit percent(progressPercent);
}
}
CopyJob::~CopyJob()
{
if (temp) {
temp->remove();
delete temp;
}
}
static const int constChunkSize = 32 * 1024;
QString CopyJob::updateTagsLocal()
{
// First, if we are going to update tags (e.g. to fix various artists), then check if we want to do that locally, before
// copying to device. For UMS devices, we just modify on device, but for remote (e.g. sshfs) then it'd be better to do locally :-)
if (copyOpts & OptsFixLocal && (copyOpts & OptsApplyVaFix || copyOpts & OptsUnApplyVaFix || Device::constEmbedCover == deviceOpts.coverName)) {
song.file = srcFile;
temp = Device::copySongToTemp(song);
if (!temp) {
emit result(Device::FailedToUpdateTags);
return QString();
}
if ((copyOpts & OptsApplyVaFix || copyOpts & OptsUnApplyVaFix) && !Device::fixVariousArtists(temp->fileName(), song, copyOpts & OptsApplyVaFix)) {
emit result(Device::FailedToUpdateTags);
return QString();
}
if (Device::constEmbedCover == deviceOpts.coverName) {
Device::embedCover(temp->fileName(), song, deviceOpts.coverMaxSize);
}
return temp->fileName();
}
return srcFile;
}
void CopyJob::updateTagsDest()
{
if (!stopRequested && !(copyOpts & OptsFixLocal) && (copyOpts & OptsApplyVaFix || copyOpts & OptsUnApplyVaFix || Device::constEmbedCover == deviceOpts.coverName)) {
if (copyOpts & OptsApplyVaFix || copyOpts & OptsUnApplyVaFix) {
Device::fixVariousArtists(destFile, song, copyOpts & OptsApplyVaFix);
}
if (!stopRequested && Device::constEmbedCover == deviceOpts.coverName) {
Device::embedCover(destFile, song, deviceOpts.coverMaxSize);
}
}
}
void CopyJob::copyCover(const QString& origSrcFile)
{
if (!stopRequested && !deviceOpts.coverName.isEmpty() && Device::constNoCover != deviceOpts.coverName && Device::constEmbedCover != deviceOpts.coverName) {
song.file = destFile;
copiedCover = Covers::copyCover(song, Utils::getDir(origSrcFile), Utils::getDir(destFile), deviceOpts.coverName, deviceOpts.coverMaxSize);
}
}
void CopyJob::run()
{
QString origSrcFile(srcFile);
srcFile = updateTagsLocal();
if (srcFile.isEmpty()) {
return;
}
if (stopRequested) {
emit result(Device::Cancelled);
return;
}
QFile src(srcFile);
if (!src.open(QIODevice::ReadOnly)) {
emit result(Device::ReadFailed);
return;
}
QFile dest(destFile);
if (!dest.open(QIODevice::WriteOnly)) {
emit result(Device::WriteFailed);
return;
}
char buffer[constChunkSize];
qint64 totalBytes = src.size();
qint64 readPos = 0;
qint64 bytesRead = 0;
qint64 adjustTotal = Device::constNoCover != deviceOpts.coverName ? 16384 : 0;
do {
if (stopRequested) {
emit result(Device::Cancelled);
return;
}
bytesRead = src.read(buffer, constChunkSize);
readPos += bytesRead;
if (bytesRead < 0) {
emit result(Device::ReadFailed);
return;
}
if (stopRequested) {
emit result(Device::Cancelled);
return;
}
qint64 writePos = 0;
do {
qint64 bytesWritten = dest.write(&buffer[writePos], bytesRead - writePos);
if (stopRequested) {
emit result(Device::Cancelled);
return;
}
if (-1 == bytesWritten) {
emit result(Device::WriteFailed);
return;
}
writePos += bytesWritten;
} while (writePos < bytesRead);
setPercent(((readPos + bytesRead) * 100.0) / (totalBytes + adjustTotal));
if (src.atEnd()) {
break;
}
} while (readPos < totalBytes);
updateTagsDest();
copyCover(origSrcFile);
setPercent(100);
emit result(Device::Ok);
}
void DeleteJob::run()
{
int status = QFile::remove(fileName) ? Device::Ok : Device::Failed;
if (remLyrics && Device::Ok == status) {
QString lyrics = Utils::changeExtension(fileName, SongView::constExtension);
if (lyrics != fileName) {
QFile::remove(lyrics);
}
}
emit result(status);
emit percent(100);
}
void CleanJob::run()
{
int total = dirs.count();
int current = 0;
for (const QString& d : dirs) {
if (stopRequested) {
emit result(Device::Cancelled);
return;
}
Device::cleanDir(d, base, coverFile);
emit percent((++current * 100) / total);
}
emit percent(100);
emit result(Device::Ok);
}
#include "moc_filejob.cpp"
|