File indexing completed on 2024-12-22 04:28:23

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "voskextractlanguagejob.h"
0008 #include "libvoskspeechtotext_debug.h"
0009 #include "voskengineutils.h"
0010 #include <KLocalizedString>
0011 #include <KZip>
0012 #include <QDir>
0013 
0014 VoskExtractLanguageJob::VoskExtractLanguageJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 VoskExtractLanguageJob::~VoskExtractLanguageJob() = default;
0020 
0021 bool VoskExtractLanguageJob::canStart() const
0022 {
0023     return !mSource.isEmpty();
0024 }
0025 
0026 void VoskExtractLanguageJob::extractRecursive(const KArchiveDirectory *dir, const QString &path)
0027 {
0028     // qDebug() << " path " << path;
0029     const QStringList lst = dir->entries();
0030     // qDebug() << " list entries : " << lst;
0031     for (const QString &it : lst) {
0032         const KArchiveEntry *entry = dir->entry(it);
0033         if (entry->isDirectory()) {
0034             // qDebug() << " directory ********" << it << "sss " << (path + it + QLatin1Char('/'));
0035             extractRecursive(static_cast<const KArchiveDirectory *>(entry), path + it + QLatin1Char('/'));
0036         } else if (entry->isFile()) {
0037             const KArchiveEntry *filePathEntry = dir->entry(it);
0038             const auto filePath = static_cast<const KArchiveFile *>(filePathEntry);
0039             const QString storeDirectory{VoskEngineUtils::storageLanguagePath() + QLatin1Char('/') + path};
0040             // qDebug() << "storeDirectory  " << storeDirectory << " ddd " << it;
0041             if (!QDir().mkpath(storeDirectory)) {
0042                 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to create :" << storeDirectory;
0043                 continue;
0044             }
0045             if (!filePath->copyTo(storeDirectory)) {
0046                 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to copy to " << storeDirectory;
0047             }
0048         }
0049     }
0050 }
0051 
0052 void VoskExtractLanguageJob::start()
0053 {
0054     if (!canStart()) {
0055         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to start ExtractLanguageJob";
0056         Q_EMIT errorText(i18n("Impossible to extract language"));
0057         Q_EMIT finished();
0058         deleteLater();
0059         return;
0060     }
0061     auto zip = new KZip(mSource);
0062     if (!zip->open(QIODevice::ReadOnly)) {
0063         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open temporary file" << mSource;
0064         Q_EMIT finished();
0065         deleteLater();
0066         return;
0067     }
0068     if (!QDir().mkpath(VoskEngineUtils::storageLanguagePath())) {
0069         qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to create path" << VoskEngineUtils::storageLanguagePath();
0070         Q_EMIT finished();
0071         deleteLater();
0072         return;
0073     }
0074     const KArchiveDirectory *zipDir = zip->directory();
0075     extractRecursive(zipDir, QString());
0076     delete zip;
0077     Q_EMIT finished();
0078     deleteLater();
0079 }
0080 
0081 QString VoskExtractLanguageJob::source() const
0082 {
0083     return mSource;
0084 }
0085 
0086 void VoskExtractLanguageJob::setSource(const QString &newSource)
0087 {
0088     mSource = newSource;
0089 }
0090 
0091 #include "moc_voskextractlanguagejob.cpp"