summaryrefslogtreecommitdiff
path: root/src/plugins/ivimedia/media_simulator/mediaindexerbackend.cpp
blob: 96335c030aed1d0fb4689d045da0548e05034e61 (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
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
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtIvi module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "mediaindexerbackend.h"
#include "logging.h"

#include <QtConcurrent/QtConcurrent>

#include <QDirIterator>
#include <QImage>
#include <QSqlError>
#include <QSqlQuery>
#include <QStandardPaths>
#include <QThreadPool>
#include <QtDebug>

#ifndef QTIVI_NO_TAGLIB
#include <attachedpictureframe.h>
#include <fileref.h>
#include <id3v2frame.h>
#include <id3v2header.h>
#include <id3v2tag.h>
#include <mpegfile.h>
#include <tag.h>
#include <taglib.h>
#include <tstring.h>
#endif

MediaIndexerBackend::MediaIndexerBackend(const QSqlDatabase &database, QObject *parent)
    : QIviMediaIndexerControlBackendInterface(parent)
    , m_db(database)
    , m_state(QIviMediaIndexerControl::Idle)
    , m_threadPool(new QThreadPool(this))
{
    m_threadPool->setMaxThreadCount(1);

    connect(&m_watcher, &QFutureWatcherBase::finished, this, &MediaIndexerBackend::onScanFinished);

    QStringList mediaFolderList;
    const QByteArray customMediaFolder = qgetenv("QTIVIMEDIA_SIMULATOR_LOCALMEDIAFOLDER");
    if (!customMediaFolder.isEmpty()) {
        qCInfo(media) << "QTIVIMEDIA_SIMULATOR_LOCALMEDIAFOLDER environment variable is set to:" << customMediaFolder;
        mediaFolderList.append(customMediaFolder);
    } else {
        mediaFolderList = QStandardPaths::standardLocations(QStandardPaths::MusicLocation);
        qCInfo(media) << "Searching for music files in the following locations: " << mediaFolderList;
    }

#ifdef QTIVI_NO_TAGLIB
    qCCritical(media) << "The indexer simulation doesn't work without an installed taglib";
#endif

    ScanData data;
    data.operation = ScanData::Verify;
    m_folderQueue.append(data);
    scanNext();

    //We want to have the indexer running also when the Indexing interface is not used.
    for (const QString &folder : qAsConst(mediaFolderList))
        addMediaFolder(folder);
}

void MediaIndexerBackend::initialize()
{
    emit stateChanged(m_state);
    emit initializationDone();
}

void MediaIndexerBackend::pause()
{
    static const QLatin1String error("SIMULATION: Pausing the indexing is not supported");
    qCWarning(media) << error;
    emit errorChanged(QIviAbstractFeature::InvalidOperation, error);
}

void MediaIndexerBackend::resume()
{
    static const QLatin1String error("SIMULATION: Resuming the indexing is not supported");
    qCWarning(media) << error;
    emit errorChanged(QIviAbstractFeature::InvalidOperation, error);
}

qreal MediaIndexerBackend::progress() const
{
    return m_progress;
}

QIviMediaIndexerControl::State MediaIndexerBackend::state() const
{
    return m_state;
}

void MediaIndexerBackend::addMediaFolder(const QString &path)
{
    ScanData data;
    data.operation = ScanData::Add;
    data.folder = path;
    m_folderQueue.append(data);

    scanNext();
}

void MediaIndexerBackend::removeMediaFolder(const QString &path)
{
    ScanData data;
    data.operation = ScanData::Remove;
    data.folder = path;
    m_folderQueue.append(data);

    scanNext();
}

bool MediaIndexerBackend::scanWorker(const ScanData &scanData)
{
    setState(QIviMediaIndexerControl::Active);

    auto removeDataFunc = [this](QSqlQuery &query, const QStringList &ids) {
        const QString idsToRemove = ids.join(QStringLiteral(", "));
        if (!query.exec(QStringLiteral("DELETE from queue WHERE track_index IN (%1)").arg(idsToRemove))) {
            setState(QIviMediaIndexerControl::Error);
            sqlError(this, query.lastQuery(), query.lastError().text());
            return false;
        }
        if (!query.exec(QStringLiteral("DELETE from track WHERE id IN (%1)").arg(idsToRemove))) {
            setState(QIviMediaIndexerControl::Error);
            sqlError(this, query.lastQuery(), query.lastError().text());
            return false;
        }
        return true;
    };

    if (scanData.operation == ScanData::Verify) {
        qCInfo(media) << "Checking Database";
        QSqlQuery query(m_db);

        QStringList idsToRemove;
        bool ret = query.exec(QStringLiteral("SELECT track.id, queue.qindex, track.file FROM track LEFT JOIN queue ON queue.track_index=track.id"));
        if (ret) {
            while (query.next()) {
                if (!QFile::exists(query.value(2).toString())) {
                    qCInfo(media) << "Removing stale track: " << query.value(2).toString();
                    idsToRemove.append(query.value(0).toString());
                    if (!query.value(1).isNull())
                        emit removeFromQueue(query.value(1).toInt());
                }
            }

            if (!removeDataFunc(query, idsToRemove))
                return false;

            m_db.commit();
            return true;
        } else {
            setState(QIviMediaIndexerControl::Error);
            sqlError(this, query.lastQuery(), query.lastError().text());
            return false;
        }
    } else if (scanData.operation == ScanData::Remove) {
        qCInfo(media) << "Removing content: " << scanData.folder;
        QSqlQuery query(m_db);

        QStringList idsToRemove;
        bool ret = query.exec(QStringLiteral("SELECT track.id, queue.qindex FROM track LEFT JOIN queue ON queue.track_index=track.id WHERE file LIKE '%1%'").arg(scanData.folder));
        if (ret) {
            while (query.next()) {
                idsToRemove.append(query.value(0).toString());
                if (!query.value(1).isNull())
                    emit removeFromQueue(query.value(1).toInt());
            }
        } else {
            setState(QIviMediaIndexerControl::Error);
            sqlError(this, query.lastQuery(), query.lastError().text());
            return false;
        }

        if (!removeDataFunc(query, idsToRemove))
            return false;

        m_db.commit();
        return true;
    }

    qCInfo(media) << "Scanning path: " << scanData.folder;

    QStringList mediaFiles{QStringLiteral("*.mp3")};

    QVector<QString> files;
    QDirIterator it(scanData.folder, mediaFiles, QDir::Files, QDirIterator::Subdirectories);
    qCInfo(media) << "Calculating total file count";

    while (it.hasNext())
        files.append(it.next());
    int totalFileCount = files.size();
    qCInfo(media) << "total files: " << totalFileCount;
    int currentFileIndex = 0;
    for (const QString &fileName : qAsConst(files)) {
        qCInfo(media) << "Processing file:" << fileName;

        if (qApp->closingDown())
            return false;

        QString defaultCoverArtUrl = fileName + QStringLiteral(".png");
        QString coverArtUrl;
#ifndef QTIVI_NO_TAGLIB
        TagLib::FileRef f(TagLib::FileName(QFile::encodeName(fileName)));
        if (f.isNull())
            continue;
        QString trackName = TStringToQString(f.tag()->title());
        QString albumName = TStringToQString(f.tag()->album());
        QString artistName = TStringToQString(f.tag()->artist());
        QString genre = TStringToQString(f.tag()->genre());
        unsigned int number = f.tag()->track();

        // Extract cover art
        if (fileName.endsWith(QLatin1String("mp3"))) {
            auto *file = static_cast<TagLib::MPEG::File*>(f.file());
            TagLib::ID3v2::Tag *tag = file->ID3v2Tag(true);
            TagLib::ID3v2::FrameList frameList = tag->frameList("APIC");

            if (frameList.isEmpty()) {
                qCWarning(media) << "No cover art was found";
            } else if (!QFile::exists(defaultCoverArtUrl)) {
                auto *coverImage = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(frameList.front());

                QImage coverQImg;
                coverArtUrl = defaultCoverArtUrl;

                coverQImg.loadFromData((const uchar *)coverImage->picture().data(), coverImage->picture().size());
                coverQImg.save(coverArtUrl, "PNG");
            } else {
                coverArtUrl = defaultCoverArtUrl;
            }
        }

        QSqlQuery query(m_db);

        query.prepare("INSERT OR IGNORE INTO track (trackName, albumName, artistName, genre, number, file, coverArtUrl) "
                      "VALUES (:trackName, :albumName, :artistName, :genre, :number, :file, :coverArtUrl)");

        query.bindValue(QStringLiteral(":trackName"), trackName);
        query.bindValue(QStringLiteral(":albumName"), albumName);
        query.bindValue(QStringLiteral(":artistName"), artistName);
        query.bindValue(QStringLiteral(":genre"), genre);
        query.bindValue(QStringLiteral(":number"), number);
        query.bindValue(QStringLiteral(":file"), fileName);
        query.bindValue(QStringLiteral(":coverArtUrl"), coverArtUrl);

        bool ret = query.exec();

        if (!ret) {
            setState(QIviMediaIndexerControl::Error);
            sqlError(this, query.lastQuery(), query.lastError().text());
            return false;
        } else {
            setProgress(qreal(++currentFileIndex)/qreal(totalFileCount));
        }
#else
        setProgress(qreal(++currentFileIndex)/qreal(totalFileCount));
#endif // QTIVI_NO_TAGLIB
    }

    return true;
}

void MediaIndexerBackend::onScanFinished()
{
    if (!m_folderQueue.isEmpty()) {
        scanNext();
        return;
    }

    qCInfo(media) << "Scanning done";
#ifdef QTIVI_NO_TAGLIB
    qCCritical(media) << "No data was added, this is just a simulation";
#endif
    setProgress(1);
    emit indexingDone();


    //If the last run didn't succeed we will stay in the Error state
    if (m_watcher.future().result())
        setState(QIviMediaIndexerControl::Idle);
}

void MediaIndexerBackend::scanNext()
{
    if (m_watcher.isRunning())
        return;

    ScanData data = m_folderQueue.dequeue();
    m_watcher.setFuture(QtConcurrent::run(this, &MediaIndexerBackend::scanWorker, data));
}

void MediaIndexerBackend::setProgress(qreal progress)
{
    m_progress = progress;
    emit progressChanged(progress);
}

void MediaIndexerBackend::setState(QIviMediaIndexerControl::State state)
{
    m_state = state;
    emit stateChanged(state);
}