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
26 return mSpeechToTextInfos.count();
32 switch (
static_cast<VoskRoles
>(section)) {
33 case VoskRoles::Identifier:
34 case VoskRoles::SizeByte:
37 case VoskRoles::NeedToUpdateLanguage:
38 case VoskRoles::CheckSum:
40 case VoskRoles::LangText:
41 return i18n(
"Language");
42 case VoskRoles::Installed:
43 return i18n(
"Installed");
44 case VoskRoles::InstalledVersion:
45 return i18n(
"Installed Version");
46 case VoskRoles::AvailableVersion:
47 return i18n(
"Available Version");
57 if (
index.row() < 0 ||
index.row() >= mSpeechToTextInfos.count()) {
60 const auto speechToTextInfo = mSpeechToTextInfos.at(
index.row());
61 const int col =
index.column();
63 if (needToUpdateLanguageModel(speechToTextInfo)) {
64 if (
static_cast<VoskRoles
>(col) == VoskRoles::InstalledVersion) {
70 switch (
static_cast<VoskRoles
>(col)) {
71 case VoskRoles::InstalledVersion: {
72 const QString
name{speechToTextInfo.name()};
73 if (isInstalled(name)) {
74 return versionInstalled(name);
78 case VoskRoles::AvailableVersion: {
79 return speechToTextInfo.version();
81 case VoskRoles::NeedToUpdateLanguage: {
82 return needToUpdateLanguageModel(speechToTextInfo);
84 case VoskRoles::Installed: {
85 if (isInstalled(speechToTextInfo.name())) {
86 return i18n(
"Installed");
90 case VoskRoles::Identifier: {
91 return speechToTextInfo.identifier();
93 case VoskRoles::LangText: {
94 return speechToTextInfo.langText();
96 case VoskRoles::Size: {
100 case VoskRoles::SizeByte: {
101 return speechToTextInfo.size();
103 case VoskRoles::Url: {
104 return speechToTextInfo.url();
106 case VoskRoles::CheckSum: {
107 return speechToTextInfo.md5();
109 case VoskRoles::Name: {
110 return speechToTextInfo.name();
117bool VoskSpeechToTextModel::needToUpdateLanguageModel(
const VoskSpeechToTextInfo &language)
const
119 const QString shortName{language.identifier()};
120 if (isInstalled(shortName)) {
121 if (versionInstalled(shortName) != language.version()) {
128void VoskSpeechToTextModel::clear()
130 if (rowCount() != 0) {
132 mSpeechToTextInfos.clear();
135 updateInstalledLanguage();
138void VoskSpeechToTextModel::updateInstalledLanguage()
140 mLanguageInstalled = VoskEngineUtils::languageLocallyStored();
141 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) <<
"mLanguageInstalled " << mLanguageInstalled;
144void VoskSpeechToTextModel::removeLanguage(
const QString &name)
146 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [name](
const VoskEngineUtils::LanguageInstalled &installed) {
147 return (name == installed.name);
149 if (
index != mLanguageInstalled.end()) {
150 const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
151 if (!QDir(absoluteLanguageModelPath).removeRecursively()) {
152 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) <<
"Impossible to delete " << absoluteLanguageModelPath;
155 mLanguageInstalled.removeAll(*
index);
159 updateInstalledLanguage();
164 return mSpeechToTextInfos;
169 if (rowCount() != 0) {
171 mSpeechToTextInfos.clear();
174 if (!newSpeechToTextInfo.
isEmpty()) {
176 mSpeechToTextInfos = newSpeechToTextInfo;
179 updateInstalledLanguage();
182int VoskSpeechToTextModel::columnCount(
const QModelIndex &parent)
const
185 return static_cast<int>(VoskRoles::LastColumn) + 1;
188bool VoskSpeechToTextModel::isInstalled(
const QString &name)
const
190 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
191 if (lang.name == name) {
198QString VoskSpeechToTextModel::versionInstalled(
const QString &shortName)
const
200 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
201 if (lang.name == shortName) {
202 return lang.versionStr;
208#include "moc_voskspeechtotextmodel.cpp"
QString i18n(const char *text, const TYPE &arg...)
QString name(const QVariant &location)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
qsizetype count() const const
bool isEmpty() const const