KTextAddons

voskdownloadlanguagejob.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 "voskdownloadlanguagejob.h"
8#include "generateinstalledlanguageinfojob.h"
9#include "libvoskspeechtotext_debug.h"
10#include "speechtotext/speechtotextengineaccessmanager.h"
11#include "voskextractlanguagejob.h"
12#include <KLocalizedString>
13#include <QFileInfo>
14#include <QNetworkReply>
15#include <QNetworkRequest>
16#include <QTemporaryFile>
17
18VoskDownloadLanguageJob::VoskDownloadLanguageJob(QObject *parent)
19 : QObject{parent}
20{
21}
22
23VoskDownloadLanguageJob::~VoskDownloadLanguageJob()
24{
25 delete mHash;
26}
27
28void VoskDownloadLanguageJob::start()
29{
30 if (!canStart()) {
31 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to start DownloadLanguageJob";
33 return;
34 }
35 mDestination = new QTemporaryFile(this);
36 if (!mDestination->open()) {
37 Q_EMIT errorText(i18n("Cannot open file for downloading."));
39 return;
40 }
41
43
44 QNetworkRequest request(mInfo.url);
45 // qDebug() << " mInfo.url " << mInfo.url;
46 QNetworkReply *reply = TextSpeechToText::SpeechToTextEngineAccessManager::self()->networkManager()->get(request);
49 Q_EMIT errorText(i18n("Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
50 } else {
51 Q_EMIT errorText(i18n("Impossible to access to url: %1", mInfo.url.toString()));
52 }
53 });
54
55 connect(reply, &QNetworkReply::downloadProgress, this, &VoskDownloadLanguageJob::downloadProgress);
56 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
57 mDestination->flush();
58 mDestination->seek(0);
59 reply->deleteLater();
60 if (!mInfo.checksum.isEmpty() && mHash->result().toHex() != mInfo.checksum.toLatin1()) {
61 // qDebug() << " mHash->result() " << mHash->result().toHex() << " mCheckSum " << mCheckSum;
62 Q_EMIT errorText(i18n("CheckSum is not correct."));
64 return;
65 } else {
66 extractLanguage();
67 }
68 });
69 connect(reply, &QIODevice::readyRead, this, [this, reply] {
70 const QByteArray buffer = reply->readAll();
71 if (mDestination->write(buffer) == -1) {
72 Q_EMIT errorText(i18n("Error during writing on disk: %1", mDestination->errorString()));
73 reply->abort();
74 }
75 mHash->addData(buffer);
76 });
77}
78
79bool VoskDownloadLanguageJob::canStart() const
80{
81 return mInfo.isValid();
82}
83
84void VoskDownloadLanguageJob::extractLanguage()
85{
86 auto extraJob = new VoskExtractLanguageJob(this);
87 extraJob->setSource(mDestination->fileName());
88 connect(extraJob, &VoskExtractLanguageJob::errorText, this, &VoskDownloadLanguageJob::errorText);
89 connect(extraJob, &VoskExtractLanguageJob::finished, this, &VoskDownloadLanguageJob::generateInstalledLanguageInfo);
90
91 extraJob->start();
92}
93
94void VoskDownloadLanguageJob::generateInstalledLanguageInfo()
95{
96 auto generateInstalledLanguageJob = new GenerateInstalledLanguageInfoJob(this);
97 GenerateInstalledLanguageInfoJob::LanguageInfo info;
98 info.pathToStore = VoskEngineUtils::storageLanguagePath() + QLatin1Char('/') + mInfo.name;
99 info.info.absoluteLanguageModelPath = VoskEngineUtils::storageLanguagePath() + QLatin1Char('/') + mInfo.name;
100 info.info.name = mInfo.name;
101 info.info.url = mInfo.url.toString();
102 info.info.versionStr = mInfo.version;
103 generateInstalledLanguageJob->setInfo(info);
104
105 connect(generateInstalledLanguageJob, &GenerateInstalledLanguageInfoJob::errorText, this, &VoskDownloadLanguageJob::errorText);
106 connect(generateInstalledLanguageJob,
107 &GenerateInstalledLanguageInfoJob::generatedDone,
108 this,
109 &VoskDownloadLanguageJob::slotGenerateInstalledLanguageInfoDone);
110 generateInstalledLanguageJob->start();
111}
112
113void VoskDownloadLanguageJob::slotGenerateInstalledLanguageInfoDone()
114{
115 Q_EMIT extractDone();
116 deleteLater();
117}
118
119VoskDownloadLanguageJob::DownloadLanguageInfo VoskDownloadLanguageJob::info() const
120{
121 return mInfo;
122}
123
124void VoskDownloadLanguageJob::setInfo(const DownloadLanguageInfo &newInfo)
125{
126 mInfo = newInfo;
127}
128
129QDebug operator<<(QDebug d, const VoskDownloadLanguageJob::DownloadLanguageInfo &t)
130{
131 d << "url " << t.url;
132 d << "checksum " << t.checksum;
133 d << "name " << t.name;
134 d << "version " << t.version;
135 return d;
136}
137
138bool VoskDownloadLanguageJob::DownloadLanguageInfo::isValid() const
139{
140 return !url.isEmpty() && !name.isEmpty();
141}
142
143#include "moc_voskdownloadlanguagejob.cpp"
QString i18n(const char *text, const TYPE &arg...)
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
QByteArray toHex(char separator) const const
bool addData(QIODevice *device)
QByteArray result() const const
bool flush()
virtual bool seek(qint64 pos) override
QString errorString() const const
QByteArray readAll()
void readyRead()
qint64 write(const QByteArray &data)
QNetworkReply * get(const QNetworkRequest &request)
virtual void abort()=0
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void errorOccurred(QNetworkReply::NetworkError code)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
QByteArray toLatin1() const const
virtual QString fileName() const const override
bool isEmpty() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.