KTextAddons

voskextractlanguagejob.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "voskextractlanguagejob.h"
8#include "libvoskspeechtotext_debug.h"
9#include "voskengineutils.h"
10#include <KLocalizedString>
11#include <KZip>
12#include <QDir>
13
14VoskExtractLanguageJob::VoskExtractLanguageJob(QObject *parent)
15 : QObject{parent}
16{
17}
18
19VoskExtractLanguageJob::~VoskExtractLanguageJob() = default;
20
21bool VoskExtractLanguageJob::canStart() const
22{
23 return !mSource.isEmpty();
24}
25
26void VoskExtractLanguageJob::extractRecursive(const KArchiveDirectory *dir, const QString &path)
27{
28 // qDebug() << " path " << path;
29 const QStringList lst = dir->entries();
30 // qDebug() << " list entries : " << lst;
31 for (const QString &it : lst) {
32 const KArchiveEntry *entry = dir->entry(it);
33 if (entry->isDirectory()) {
34 // qDebug() << " directory ********" << it << "sss " << (path + it + QLatin1Char('/'));
35 extractRecursive(static_cast<const KArchiveDirectory *>(entry), path + it + QLatin1Char('/'));
36 } else if (entry->isFile()) {
37 const KArchiveEntry *filePathEntry = dir->entry(it);
38 const auto filePath = static_cast<const KArchiveFile *>(filePathEntry);
39 const QString storeDirectory{VoskEngineUtils::storageLanguagePath() + QLatin1Char('/') + path};
40 // qDebug() << "storeDirectory " << storeDirectory << " ddd " << it;
41 if (!QDir().mkpath(storeDirectory)) {
42 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to create :" << storeDirectory;
43 continue;
44 }
45 if (!filePath->copyTo(storeDirectory)) {
46 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to copy to " << storeDirectory;
47 }
48 }
49 }
50}
51
52void VoskExtractLanguageJob::start()
53{
54 if (!canStart()) {
55 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to start ExtractLanguageJob";
56 Q_EMIT errorText(i18n("Impossible to extract language"));
57 Q_EMIT finished();
59 return;
60 }
61 auto zip = new KZip(mSource);
62 if (!zip->open(QIODevice::ReadOnly)) {
63 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to open temporary file" << mSource;
64 Q_EMIT finished();
66 return;
67 }
68 if (!QDir().mkpath(VoskEngineUtils::storageLanguagePath())) {
69 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to create path" << VoskEngineUtils::storageLanguagePath();
70 Q_EMIT finished();
72 return;
73 }
74 const KArchiveDirectory *zipDir = zip->directory();
75 extractRecursive(zipDir, QString());
76 delete zip;
77 Q_EMIT finished();
79}
80
81QString VoskExtractLanguageJob::source() const
82{
83 return mSource;
84}
85
86void VoskExtractLanguageJob::setSource(const QString &newSource)
87{
88 mSource = newSource;
89}
90
91#include "moc_voskextractlanguagejob.cpp"
virtual bool isDirectory() const
virtual bool isFile() const
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT MkpathJob * mkpath(const QUrl &url, const QUrl &baseUrl=QUrl(), JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
Q_EMITQ_EMIT
void deleteLater()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.