7#include "voskspeechtotextmodel.h"
8#include "libvoskspeechtotext_debug.h"
10#include <KLocalizedString>
14VoskSpeechToTextModel::VoskSpeechToTextModel(
QObject *parent)
17 updateInstalledLanguage();
20VoskSpeechToTextModel::~VoskSpeechToTextModel() =
default;
22int VoskSpeechToTextModel::rowCount(
const QModelIndex &parent)
const
25 return mSpeechToTextInfos.count();
31 switch (
static_cast<VoskRoles
>(section)) {
32 case VoskRoles::Identifier:
33 case VoskRoles::SizeByte:
36 case VoskRoles::NeedToUpdateLanguage:
37 case VoskRoles::CheckSum:
39 case VoskRoles::LangText:
40 return i18n(
"Language");
41 case VoskRoles::Installed:
42 return i18n(
"Installed");
43 case VoskRoles::InstalledVersion:
44 return i18n(
"Installed Version");
45 case VoskRoles::AvailableVersion:
46 return i18n(
"Available Version");
59 const auto speechToTextInfo = mSpeechToTextInfos.at(
index.
row());
62 if (needToUpdateLanguageModel(speechToTextInfo)) {
63 if (
static_cast<VoskRoles
>(col) == VoskRoles::InstalledVersion) {
69 switch (
static_cast<VoskRoles
>(col)) {
70 case VoskRoles::InstalledVersion: {
72 if (isInstalled(name)) {
73 return versionInstalled(name);
77 case VoskRoles::AvailableVersion: {
78 return speechToTextInfo.version();
80 case VoskRoles::NeedToUpdateLanguage: {
81 return needToUpdateLanguageModel(speechToTextInfo);
83 case VoskRoles::Installed: {
84 if (isInstalled(speechToTextInfo.name())) {
85 return i18n(
"Installed");
89 case VoskRoles::Identifier: {
90 return speechToTextInfo.identifier();
92 case VoskRoles::LangText: {
93 return speechToTextInfo.langText();
95 case VoskRoles::Size: {
99 case VoskRoles::SizeByte: {
100 return speechToTextInfo.size();
102 case VoskRoles::Url: {
103 return speechToTextInfo.url();
105 case VoskRoles::CheckSum: {
106 return speechToTextInfo.md5();
108 case VoskRoles::Name: {
109 return speechToTextInfo.name();
116bool VoskSpeechToTextModel::needToUpdateLanguageModel(
const VoskSpeechToTextInfo &language)
const
118 const QString shortName{language.identifier()};
119 if (isInstalled(shortName)) {
120 if (versionInstalled(shortName) != language.version()) {
127void VoskSpeechToTextModel::clear()
129 if (rowCount() != 0) {
131 mSpeechToTextInfos.clear();
134 updateInstalledLanguage();
137void VoskSpeechToTextModel::updateInstalledLanguage()
139 mLanguageInstalled = VoskEngineUtils::languageLocallyStored();
140 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) <<
"mLanguageInstalled " << mLanguageInstalled;
143void VoskSpeechToTextModel::removeLanguage(
const QString &name)
145 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [name](
const VoskEngineUtils::LanguageInstalled &installed) {
146 return (name == installed.name);
148 if (
index != mLanguageInstalled.end()) {
149 const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
150 if (!
QDir(absoluteLanguageModelPath).removeRecursively()) {
151 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) <<
"Impossible to delete " << absoluteLanguageModelPath;
154 mLanguageInstalled.removeAll(*
index);
158 updateInstalledLanguage();
163 return mSpeechToTextInfos;
168 if (rowCount() != 0) {
170 mSpeechToTextInfos.clear();
173 if (!newSpeechToTextInfo.
isEmpty()) {
175 mSpeechToTextInfos = newSpeechToTextInfo;
178 updateInstalledLanguage();
181int VoskSpeechToTextModel::columnCount(
const QModelIndex &parent)
const
184 return static_cast<int>(VoskRoles::LastColumn) + 1;
187bool VoskSpeechToTextModel::isInstalled(
const QString &name)
const
189 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
190 if (lang.name == name) {
197QString VoskSpeechToTextModel::versionInstalled(
const QString &shortName)
const
199 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
200 if (lang.name == shortName) {
201 return lang.versionStr;
207#include "moc_voskspeechtotextmodel.cpp"
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
qsizetype count() const const
bool isEmpty() const const
QObject * parent() const const