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 <QNetworkReply>
14#include <QNetworkRequest>
15#include <QTemporaryFile>
17VoskDownloadLanguageJob::VoskDownloadLanguageJob(
QObject *parent)
22VoskDownloadLanguageJob::~VoskDownloadLanguageJob()
27void VoskDownloadLanguageJob::start()
30 qCWarning(LIBVOSKSPEECHTOTEXT_LOG) <<
"Impossible to start DownloadLanguageJob";
34 mDestination =
new QTemporaryFile(
this);
35 if (!mDestination->open()) {
36 Q_EMIT errorText(
i18n(
"Cannot open file for downloading."));
43 QNetworkRequest request(mInfo.url);
45 QNetworkReply *reply = TextSpeechToText::SpeechToTextEngineAccessManager::self()->networkManager()->
get(request);
48 Q_EMIT errorText(
i18n(
"Error: Engine systems have detected suspicious traffic from your computer network. Please try your request again later."));
50 Q_EMIT errorText(
i18n(
"Impossible to access to url: %1", mInfo.url.toString()));
56 mDestination->flush();
57 mDestination->seek(0);
59 if (!mInfo.checksum.isEmpty() && mHash->result().toHex() != mInfo.checksum.toLatin1()) {
61 Q_EMIT errorText(
i18n(
"CheckSum is not correct."));
69 const QByteArray buffer = reply->
readAll();
70 if (mDestination->write(buffer) == -1) {
71 Q_EMIT errorText(
i18n(
"Error during writing on disk: %1", mDestination->errorString()));
74 mHash->addData(buffer);
78bool VoskDownloadLanguageJob::canStart()
const
80 return mInfo.isValid();
83void VoskDownloadLanguageJob::extractLanguage()
85 auto extraJob =
new VoskExtractLanguageJob(
this);
86 extraJob->setSource(mDestination->fileName());
87 connect(extraJob, &VoskExtractLanguageJob::errorText,
this, &VoskDownloadLanguageJob::errorText);
88 connect(extraJob, &VoskExtractLanguageJob::finished,
this, &VoskDownloadLanguageJob::generateInstalledLanguageInfo);
93void VoskDownloadLanguageJob::generateInstalledLanguageInfo()
95 auto generateInstalledLanguageJob =
new GenerateInstalledLanguageInfoJob(
this);
96 GenerateInstalledLanguageInfoJob::LanguageInfo info;
97 info.pathToStore = VoskEngineUtils::storageLanguagePath() + QLatin1Char(
'/') + mInfo.name;
98 info.info.absoluteLanguageModelPath = VoskEngineUtils::storageLanguagePath() + QLatin1Char(
'/') + mInfo.name;
99 info.info.name = mInfo.name;
100 info.info.url = mInfo.url.toString();
101 info.info.versionStr = mInfo.version;
102 generateInstalledLanguageJob->setInfo(info);
104 connect(generateInstalledLanguageJob, &GenerateInstalledLanguageInfoJob::errorText,
this, &VoskDownloadLanguageJob::errorText);
105 connect(generateInstalledLanguageJob,
106 &GenerateInstalledLanguageInfoJob::generatedDone,
108 &VoskDownloadLanguageJob::slotGenerateInstalledLanguageInfoDone);
109 generateInstalledLanguageJob->start();
112void VoskDownloadLanguageJob::slotGenerateInstalledLanguageInfoDone()
118VoskDownloadLanguageJob::DownloadLanguageInfo VoskDownloadLanguageJob::info()
const
123void VoskDownloadLanguageJob::setInfo(
const DownloadLanguageInfo &newInfo)
128QDebug operator<<(
QDebug d,
const VoskDownloadLanguageJob::DownloadLanguageInfo &t)
130 d <<
"url " << t.url;
131 d <<
"checksum " << t.checksum;
132 d <<
"name " << t.name;
133 d <<
"version " << t.version;
137bool VoskDownloadLanguageJob::DownloadLanguageInfo::isValid()
const
139 return !url.isEmpty() && !name.isEmpty();
142#include "moc_voskdownloadlanguagejob.cpp"
QString i18n(const char *text, const TYPE &arg...)
QNetworkReply * get(const QNetworkRequest &request)
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void errorOccurred(QNetworkReply::NetworkError code)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)