| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/settings/settingslocalization.h" |
| 4 | |
| 5 | #include "gui/messagebox.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "miscellaneous/localization.h" |
| 9 | #include "miscellaneous/settings.h" |
| 10 | #include "network-web/networkfactory.h" |
| 11 | #include "network-web/webfactory.h" |
| 12 | |
| 13 | #include <QJsonArray> |
| 14 | #include <QJsonDocument> |
| 15 | #include <QJsonObject> |
| 16 | |
| 17 | SettingsLocalization::SettingsLocalization(Settings* settings, QWidget* parent) |
| 18 | : SettingsPanel(settings, parent), m_ui(new Ui::SettingsLocalization), |
| 19 | m_urlPercentages(QUrl(QSL("https://api.crowdin.com/api/v2/projects/608575/languages/progress?limit=100"))), |
| 20 | m_urlPeople(QUrl(QSL("https://api.crowdin.com/api/v2/projects/608575/members?limit=500"))) { |
| 21 | m_ui->setupUi(this); |
| 22 | m_ui->m_lblAuthors->label()->setWordWrap(true); |
| 23 | m_ui->m_treeLanguages->setColumnCount(3); |
| 24 | m_ui->m_treeLanguages->setHeaderHidden(false); |
| 25 | m_ui->m_treeLanguages->setHeaderLabels(QStringList() << tr("Language") << tr("Code") << tr("Translation progress")); |
| 26 | |
| 27 | m_ui->m_lblHelp->setText(tr(R"(Help us to improve %1 <a href="%2">translations</a>.)") |
| 28 | .arg(QSL(APP_NAME), QSL("https://crowdin.com/project/rssguard"))); |
| 29 | |
| 30 | connect(m_ui->m_lblHelp, &QLabel::linkActivated, qApp->web(), &WebFactory::openUrlInExternalBrowser); |
| 31 | |
| 32 | // Setup languages. |
| 33 | m_ui->m_treeLanguages->header()->setSectionResizeMode(0, QHeaderView::ResizeMode::ResizeToContents); |
| 34 | m_ui->m_treeLanguages->header()->setSectionResizeMode(1, QHeaderView::ResizeMode::ResizeToContents); |
| 35 | m_ui->m_treeLanguages->header()->setSectionResizeMode(2, QHeaderView::ResizeMode::ResizeToContents); |
| 36 | |
| 37 | connect(m_ui->m_treeLanguages, &QTreeWidget::currentItemChanged, this, &SettingsLocalization::requireRestart); |
| 38 | connect(m_ui->m_treeLanguages, &QTreeWidget::currentItemChanged, this, &SettingsLocalization::dirtifySettings); |
| 39 | } |
| 40 | |
| 41 | SettingsLocalization::~SettingsLocalization() { |
| 42 | delete m_ui; |
| 43 | } |
| 44 | |
| 45 | QIcon SettingsLocalization::icon() const { |
| 46 | return qApp->icons()->fromTheme(QSL("text-x-gettext-translation")); |
| 47 | } |
| 48 | |
| 49 | void SettingsLocalization::langMetadataDownloaded(const QUrl& url, |
| 50 | QNetworkReply::NetworkError status, |
| 51 | int http_code, |
| 52 | QByteArray contents) { |
| 53 | auto* down = qobject_cast<Downloader*>(sender()); |
| 54 | |
| 55 | if (url == m_urlPercentages) { |
| 56 | if (status == QNetworkReply::NetworkError::NoError) { |
| 57 | m_dataPercentages = contents; |
| 58 | down->downloadFile(m_urlPeople.toString(), |
| 59 | qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt()); |
| 60 | } |
| 61 | else { |
| 62 | qCriticalNN << LOGSEC_NETWORK << "Failed to download languages metadata."; |
| 63 | |
| 64 | if (down != nullptr) { |
| 65 | down->deleteLater(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | else { |
| 70 | if (status == QNetworkReply::NetworkError::NoError) { |
| 71 | m_dataPeople = contents; |
| 72 | |
| 73 | QMap<QString, int> percentages_langs; |
| 74 | QString all_translators; |
| 75 | |
| 76 | QJsonDocument stats_doc = QJsonDocument::fromJson(m_dataPercentages); |
| 77 | QJsonDocument people_doc = QJsonDocument::fromJson(m_dataPeople); |
| 78 | QJsonArray people_arr = people_doc.object()["data"].toArray(); |
| 79 | std::vector<QString> people_desc; |
| 80 | |
| 81 | std::transform(people_arr.begin(), people_arr.end(), std::back_inserter(people_desc), [](const QJsonValue& b) { |
| 82 | return b.toObject()["data"].toObject()["username"].toString(); |
| 83 | }); |
| 84 | |
| 85 | all_translators = std::accumulate(std::next(people_desc.begin()), |
| 86 | people_desc.end(), |
| 87 | people_desc.at(0), |
| 88 | [](const QString& lhs, const QString& rhs) { |
| 89 | return QString(lhs + ", " + rhs); |
| 90 | }); |
| 91 | |
| 92 | for (const QJsonValue& val_lang : stats_doc.object()["data"].toArray()) { |
| 93 | QString lang_id = val_lang.toObject()["data"].toObject()["languageId"].toString().replace(QSL("-"), QSL("_")); |
| 94 | int lang_completion = val_lang.toObject()["data"].toObject()["translationProgress"].toInt(); |
| 95 | |
| 96 | if (lang_id == QSL("es_ES")) { |
| 97 | lang_id = QSL("es"); |
| 98 | } |
| 99 | |
| 100 | if (lang_id == QSL("en_US")) { |
| 101 | lang_completion = 100; |
| 102 | } |
| 103 | |
| 104 | percentages_langs.insert(lang_id, lang_completion); |
| 105 | } |
| 106 | |
| 107 | if (all_translators.isEmpty()) { |
| 108 | m_ui->m_lblAuthors->setStatus(WidgetWithStatus::StatusType::Information, |
| 109 | tr("Big thanks to all translators!"), |
| 110 | tr("Big thanks to all translators!")); |
| 111 | } |
| 112 | else { |
| 113 | m_ui->m_lblAuthors->setStatus(WidgetWithStatus::StatusType::Information, |
| 114 | tr("Translations provided by: %1").arg(all_translators), |
| 115 | tr("Big thanks to all translators!")); |
| 116 | } |
| 117 | |
| 118 | for (int i = 0; i < m_ui->m_treeLanguages->topLevelItemCount(); i++) { |
| 119 | auto* it = m_ui->m_treeLanguages->topLevelItem(i); |
| 120 | int perc_translated = percentages_langs.value(it->text(1)); |
| 121 | QColor col_translated = QColor::fromHsv(perc_translated, 200, 230); |
| 122 | |
| 123 | it->setText(2, QSL("%1 %").arg(perc_translated > 0 ? QString::number(perc_translated) : QSL("?"))); |
| 124 | it->setIcon(2, IconFactory::generateIcon(col_translated)); |
| 125 | } |
| 126 | } |
| 127 | else { |
| 128 | qCriticalNN << LOGSEC_NETWORK << "Failed to download languages people metadata."; |
| 129 | } |
| 130 | |
| 131 | if (down != nullptr) { |
| 132 | down->deleteLater(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void SettingsLocalization::loadSettings() { |
| 138 | onBeginLoadSettings(); |
| 139 | |
| 140 | auto langs = qApp->localization()->installedLanguages(); |
| 141 | auto* downl = new Downloader(this); |
| 142 | |
| 143 | // Also, load statistics with restricted access token. |
| 144 | QList<QPair<QByteArray, QByteArray>> hdrs = { |
| 145 | {"Authorization", |
| 146 | "Bearer " |
| 147 | "0fbcad4c39d21a55f63f8a1b6d07cc56bb1e2eb2047bfaf1ee22425e3edf1c2b217f4d13b3cebba9"}}; |
| 148 | |
| 149 | downl->appendRawHeaders(hdrs); |
| 150 | connect(downl, |
| 151 | &Downloader::completed, |
| 152 | this, |
| 153 | &SettingsLocalization::langMetadataDownloaded, |
| 154 | Qt::ConnectionType::QueuedConnection); |
| 155 | downl->downloadFile(m_urlPercentages.toString(), |
| 156 | qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt()); |
| 157 | |
| 158 | for (const Language& language : std::as_const(langs)) { |
| 159 | auto* item = new QTreeWidgetItem(m_ui->m_treeLanguages); |
| 160 | |
| 161 | item->setText(0, language.m_name); |
| 162 | item->setText(1, language.m_code); |
| 163 | item->setText(2, QSL("? %")); |
| 164 | |
| 165 | item->setIcon(0, qApp->icons()->miscIcon(QSL(FLAG_ICON_SUBFOLDER) + QDir::separator() + language.m_code)); |
| 166 | item->setIcon(2, IconFactory::generateIcon(Qt::GlobalColor::blue)); |
| 167 | } |
| 168 | |
| 169 | m_ui->m_treeLanguages->sortByColumn(0, Qt::SortOrder::AscendingOrder); |
| 170 | |
| 171 | QList<QTreeWidgetItem*> matching_items = |
| 172 | m_ui->m_treeLanguages->findItems(qApp->localization()->loadedLanguage(), Qt::MatchFlag::MatchContains, 1); |
| 173 | |
| 174 | if (!matching_items.isEmpty()) { |
| 175 | m_ui->m_treeLanguages->setCurrentItem(matching_items[0]); |
| 176 | } |
| 177 | |
| 178 | onEndLoadSettings(); |
| 179 | } |
| 180 | |
| 181 | void SettingsLocalization::saveSettings() { |
| 182 | onBeginSaveSettings(); |
| 183 | |
| 184 | if (m_ui->m_treeLanguages->currentItem() == nullptr) { |
| 185 | qWarningNN << LOGSEC_GUI << "No localizations loaded in settings dialog, so no saving for them."; |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | const QString actual_lang = qApp->localization()->loadedLanguage(); |
| 190 | const QString new_lang = m_ui->m_treeLanguages->currentItem()->text(1); |
| 191 | |
| 192 | // Save prompt for restart if language has changed. |
| 193 | if (new_lang != actual_lang) { |
| 194 | int perc = m_ui->m_treeLanguages->currentItem()->toolTip(2).toInt(); |
| 195 | |
| 196 | if (perc > 0 && perc < 75) { |
| 197 | const QMessageBox::StandardButton clicked_button = MsgBox:: |
| 198 | show(this, |
| 199 | QMessageBox::Icon::Question, |
| 200 | tr("Translators needed!"), |
| 201 | tr("The translation '%1' is incomplete and anyone able to help with translating %2 is greatly welcomed.") |
| 202 | .arg(new_lang, QSL(APP_NAME)), |
| 203 | tr("Do you want to help with the translation now?"), |
| 204 | {}, |
| 205 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, |
| 206 | QMessageBox::StandardButton::Yes); |
| 207 | |
| 208 | if (clicked_button == QMessageBox::StandardButton::Yes) { |
| 209 | qApp->web()->openUrlInExternalBrowser(QSL("https://crowdin.com/project/rssguard")); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | requireRestart(); |
| 214 | settings()->setValue(GROUP(General), General::Language, new_lang); |
| 215 | } |
| 216 | |
| 217 | onEndSaveSettings(); |
| 218 | } |