diff --git a/companion/src/updates/CMakeLists.txt b/companion/src/updates/CMakeLists.txt index 3277a08e3..9acec0d5c 100644 --- a/companion/src/updates/CMakeLists.txt +++ b/companion/src/updates/CMakeLists.txt @@ -12,6 +12,7 @@ set(updates_NAMES updatesounds updatethemes updatemultiprotocol + chooserdialog ) foreach(name ${updates_NAMES}) @@ -22,6 +23,7 @@ endforeach() set(updates_UIS updatesdialog.ui updateoptionsdialog.ui + chooserdialog.ui ) set(updates_QTS @@ -30,6 +32,7 @@ set(updates_QTS updateoptionsdialog.h repomodels.h updateinterface.h + chooserdialog.h ) qt5_wrap_ui(updates_SRCS ${updates_UIS}) diff --git a/companion/src/updates/chooserdialog.cpp b/companion/src/updates/chooserdialog.cpp new file mode 100644 index 000000000..0147a98a6 --- /dev/null +++ b/companion/src/updates/chooserdialog.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "chooserdialog.h" +#include "ui_chooserdialog.h" + +ChooserDialog::ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel) : + QDialog(parent), + ui(new Ui::ChooserDialog) +{ + ui->setupUi(this); + + setWindowTitle(title); + + ui->listView->setModel(itemModel); + + connect(ui->buttonBox, &QDialogButtonBox::accepted, [=]() { + QDialog::accept(); + }); + + connect(ui->buttonBox, &QDialogButtonBox::rejected, [=]() { + QDialog::reject(); + }); +} + +ChooserDialog::~ChooserDialog() +{ + delete ui; +} + +QItemSelectionModel* ChooserDialog::selectedItems() +{ + return ui->listView->selectionModel(); +} diff --git a/companion/src/updates/chooserdialog.h b/companion/src/updates/chooserdialog.h new file mode 100644 index 000000000..3eb3dd1f4 --- /dev/null +++ b/companion/src/updates/chooserdialog.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#pragma once + +#include +#include +#include +#include + +namespace Ui { + class ChooserDialog; +} + +class ChooserDialog : public QDialog +{ + Q_OBJECT + + public: + ChooserDialog(QWidget * parent, QString title, QStandardItemModel * itemModel); + virtual ~ChooserDialog(); + + QItemSelectionModel* selectedItems(); + + signals: + + private slots: + + private: + Ui::ChooserDialog *ui; +}; diff --git a/companion/src/updates/chooserdialog.ui b/companion/src/updates/chooserdialog.ui new file mode 100644 index 000000000..7a43a07bb --- /dev/null +++ b/companion/src/updates/chooserdialog.ui @@ -0,0 +1,38 @@ + + + ChooserDialog + + + + 0 + 0 + 385 + 234 + + + + Dialog + + + + + + QAbstractItemView::MultiSelection + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/companion/src/updates/updateinterface.cpp b/companion/src/updates/updateinterface.cpp index bec914103..e37aa2426 100644 --- a/companion/src/updates/updateinterface.cpp +++ b/companion/src/updates/updateinterface.cpp @@ -104,18 +104,18 @@ QString UpdateParameters::buildFilterPattern(const UpdateFilterType filterType, pattern.replace("%LANGUAGE%", data.language); switch ((int)filterType) { - case UpdateParameters::UFT_Exact: + case UFT_Exact: pattern.prepend("^"); pattern.append("$"); break; - case UpdateParameters::UFT_Startswith: + case UFT_Startswith: pattern.prepend("^"); break; - case UpdateParameters::UFT_Endswith: + case UFT_Endswith: pattern.append("$"); break; - case UpdateParameters::UFT_Contains: - case UpdateParameters::UFT_Expression: + case UFT_Contains: + case UFT_Expression: break; default: pattern = ""; diff --git a/companion/src/updates/updateinterface.h b/companion/src/updates/updateinterface.h index 58f5eb31e..8e5e03a18 100644 --- a/companion/src/updates/updateinterface.h +++ b/companion/src/updates/updateinterface.h @@ -48,7 +48,7 @@ class UpdateParameters : public QWidget static QString updateFilterTypeToString(UpdateFilterType uft); - struct AssetParams{ + struct AssetParams { int flags; UpdateFilterType filterType; QString filter; @@ -58,7 +58,7 @@ class UpdateParameters : public QWidget QString copyFilter; }; - struct GeneralParams{ + struct GeneralParams { int flags; QString fwFlavour; QString language; diff --git a/companion/src/updates/updatesdcard.cpp b/companion/src/updates/updatesdcard.cpp index dc77797e6..f0e522be9 100644 --- a/companion/src/updates/updatesdcard.cpp +++ b/companion/src/updates/updatesdcard.cpp @@ -88,13 +88,13 @@ bool UpdateSDCard::flagAssets() } } + delete json; + if (!found) { reportProgress(tr("Radio flavour %1 not listed in %2").arg(runParams->data.fwFlavour).arg(mappingfile), QtCriticalMsg); return false; } - delete json; - UpdateParameters::AssetParams sd = ap; sd.filterType = UpdateParameters::UFT_Startswith; sd.filter = sdimage; diff --git a/companion/src/updates/updatesounds.cpp b/companion/src/updates/updatesounds.cpp index 6be894665..1f50d0e9c 100644 --- a/companion/src/updates/updatesounds.cpp +++ b/companion/src/updates/updatesounds.cpp @@ -20,34 +20,130 @@ */ #include "updatesounds.h" +#include "chooserdialog.h" + +#include +#include +#include UpdateSounds::UpdateSounds(QWidget * parent) : - UpdateInterface(parent) + UpdateInterface(parent), + langPacks(new QStandardItemModel()) { setName("Sounds"); setRepo(QString(GH_REPOS_EDGETX).append("/edgetx-sdcard-sounds")); - /* - file: sounds.json - [ - { - "language": "cs_CZ", - "name": "Czech Female", - "description": "Czech Female Voice (cs-CZ-Vlasta)", - "directory": "cz" - }, - - TODO support multiple language sub variants - // eg edgetx-sdcard-sounds-en_gb-libby-2.7.0.zip - ap.filter = QString("^edgetx-sdcard-sounds-%1").arg(language); //return all variants of each language - */ - UpdateParameters::AssetParams &ap = dfltParams->addAsset(); - // only supports default for each language - // eg edgetx-sdcard-sounds-en-2.7.0.zip ap.filterType = UpdateParameters::UFT_Startswith; ap.filter = QString("edgetx-sdcard-sounds-%LANGUAGE%-"); - // if user changes filter then more than one could be returned - //ap.maxExpected = 1; ap.flags = dfltParams->data.flags | UPDFLG_CopyStructure; } + +UpdateSounds::~UpdateSounds() +{ + delete langPacks; +} + +bool UpdateSounds::flagAssets() +{ + progressMessage(tr("Flagging assets")); + + const QString mappingfile = "sounds.json"; + + if (!downloadTextFileToBuffer(mappingfile)) { + return false; + } + + QJsonDocument *json = new QJsonDocument(); + + if (!convertDownloadToJson(json)) { + return false; + } + + /* + { + "language": "en-GB", + "name": "British English Female", + "description": "British English Female Voice (en-GB-Libby)", + "directory": "en_gb-libby" + }, + */ + + // always refresh to allow for language change in radio profile + langPacks->clear(); + + if (json->isArray()) { + const QJsonArray &arr = json->array(); + + foreach (const QJsonValue &v, arr) { + if (v.isObject()) { + const QJsonObject obj = v.toObject(); + QString langVariant; + QString lang; + + if (!obj.value("language").isUndefined()) { + langVariant = obj.value("language").toString(); + lang = langVariant.split("-").at(0); + } + + if (lang == runParams->data.language) { + QStandardItem * item = new QStandardItem(); + + if (!obj.value("language").isUndefined()) + item->setData(obj.value("language").toString(), IMDR_Language); + if (!obj.value("name").isUndefined()) + item->setData(obj.value("name").toString(), IMDR_Name); + if (!obj.value("description").isUndefined()) + item->setData(obj.value("description").toString(), Qt::DisplayRole); + if (!obj.value("directory").isUndefined()) + item->setData(obj.value("directory").toString(), IMDR_Directory); + + langPacks->appendRow(item); + } + } + } + } + + delete json; + + if (langPacks->rowCount() < 1) { + reportProgress(tr("Language %1 not listed in %2").arg(runParams->data.language).arg(mappingfile), QtCriticalMsg); + return false; + } + + if (langPacks->rowCount() > 1) { + ChooserDialog *dlg = new ChooserDialog(nullptr, tr("Choose Language Packs"), langPacks); + + dlg->exec(); + + QItemSelectionModel *selItems = dlg->selectedItems(); + + if (!selItems->hasSelection()) { + QMessageBox::warning(this, CPN_STR_APP_NAME, tr("No language packs have been selected. Sounds update will be skipped!")); + dlg->deleteLater(); + return true; + } + + QModelIndexList selIndexes = selItems->selectedIndexes(); + + for (int i = 0; i < selIndexes.size(); i++) { + if (!flagLanguageAsset(langPacks->data(selIndexes.at(i), IMDR_Directory).toString())) + return false; + } + + dlg->deleteLater(); + } + else if (langPacks->rowCount() == 1) { + if (!flagLanguageAsset(langPacks->data(langPacks->index(0, 0), IMDR_Directory).toString())) + return false; + } + + return true; +} + +bool UpdateSounds::flagLanguageAsset(QString lang) +{ + runParams->data.language = lang; + UpdateParameters::AssetParams ap = runParams->data.assets[0]; + return getSetAssets(ap); +} diff --git a/companion/src/updates/updatesounds.h b/companion/src/updates/updatesounds.h index 9131e5206..789fa622f 100644 --- a/companion/src/updates/updatesounds.h +++ b/companion/src/updates/updatesounds.h @@ -23,6 +23,8 @@ #include "updateinterface.h" +#include + class UpdateSounds : public UpdateInterface { Q_DECLARE_TR_FUNCTIONS(UpdateSounds) @@ -30,6 +32,20 @@ class UpdateSounds : public UpdateInterface public: explicit UpdateSounds(QWidget * parent); - virtual ~UpdateSounds() {} + virtual ~UpdateSounds(); + protected: + virtual bool flagAssets() override; + + private: + enum ItemModelDataRoles { + IMDR_Language = Qt::UserRole, + IMDR_Name, + IMDR_Directory, + }; + Q_ENUM(ItemModelDataRoles) + + QStandardItemModel *langPacks; + + bool flagLanguageAsset(QString lang); };