Skip to content

Commit

Permalink
Rebase with main
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Apr 9, 2024
1 parent 20ad0b9 commit 4ddc47d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 41 deletions.
38 changes: 23 additions & 15 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
m_server(m_library.getKiwixLibrary(), mp_nameMapper),
mp_session(nullptr)
{
try {
m_translation.setTranslation(QLocale());
} catch (std::exception& e) {
QMessageBox::critical(nullptr, "Translation error", e.what());
return;
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
QString path = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
loadAndInstallTranslations(m_qtTranslator, "qt", path);
loadAndInstallTranslations(m_appTranslator, "kiwix-desktop", ":/i18n/");

setAppLanguage();
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkb.ttf");
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawkl.ttf");
QFontDatabase::addApplicationFont(":/fonts/Selawik/selawksb.ttf");
Expand Down Expand Up @@ -245,6 +231,28 @@ void KiwixApp::openZimFile(const QString &zimfile)
openUrl(QUrl("zim://"+zimId+".zim/"));
}

void KiwixApp::setAppLanguage()
{
const QList<QString> languageCodes = getSettingsManager()->getLanguageCodes();
const QString code = languageCodes.at(getSettingsManager()->getAppLanguageIndex());
QLocale appLanguage(code);
try {
m_translation.setTranslation(appLanguage);
} catch (std::exception& e) {
QMessageBox::critical(nullptr, "Translation error", e.what());
return;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString qtTranslatorSuffix = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
#else
QString qtTranslatorSuffix = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
#endif
m_qtTranslator.load(appLanguage, "qt", "_", qtTranslatorSuffix);
installTranslator(&m_qtTranslator);
m_appTranslator.load(appLanguage, "kiwix-desktop", "_", ":/i18n/");
installTranslator(&m_appTranslator);
}

void KiwixApp::printPage()
{
if(!getTabWidget()->currentZimView())
Expand Down
1 change: 1 addition & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class KiwixApp : public QtSingleApplication
public slots:
void newTab();
void openZimFile(const QString& zimfile="");
void setAppLanguage();
void openUrl(const QString& url, bool newTab=true);
void openUrl(const QUrl& url, bool newTab=true);
void printPage();
Expand Down
45 changes: 32 additions & 13 deletions src/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ SettingsView* SettingsManager::getView()
{
if (m_view == nullptr) {
auto view = new SettingsView();
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir,
m_moveToTrash, m_reopenTab);
view->init(m_zoomFactor * 100, m_downloadDir, m_monitorDir, m_appLangIndex, m_moveToTrash, m_reopenTab);
connect(view, &QObject::destroyed, this, [=]() { m_view = nullptr; });
m_view = view;
}
Expand Down Expand Up @@ -127,13 +126,20 @@ SettingsManager::FilterList SettingsManager::deducePair(QList<QVariant> variantL
return pairList;
}

void SettingsManager::setLanguage(FilterList langList)
void SettingsManager::setLanguage(FilterList langList) // This regards the search filter languages
{
m_langList = flattenPair(langList);
setSettings("language", m_langList);
emit(languageChanged(m_langList));
}

void SettingsManager::setAppLanguage(int languageIndex) // This regards the application language
{
m_appLangIndex = languageIndex;
KiwixApp::instance()->setAppLanguage();
m_settings.setValue("app/languageIndex", languageIndex);
}

void SettingsManager::setCategory(FilterList categoryList)
{
m_categoryList = flattenPair(categoryList);
Expand All @@ -157,8 +163,30 @@ void SettingsManager::initSettings()
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
m_reopenTab = m_settings.value("reopenTab", false).toBool();
// This regards the application language
m_appLangCodes = {
"ar", "bcl", "bn", "br", "ca", "cs", "da", "dag", "de", "diq",
"en", "eo", "es", "fa", "fi", "fr", "gsw", "ha", "he", "hi",
"hy", "ia", "id", "ie", "ig", "igl", "io", "it", "ja", "ko",
"ku-latn", "lb", "mk", "ms", "my", "nb", "nl", "nqo", "or",
"pl", "pt-br", "pt", "qqq", "ro", "roa-tara", "ru", "sc",
"scn", "sdc", "se", "sk", "skr-arab", "sl", "sms", "sq",
"sr-ec", "sro", "sv", "sw", "ta", "te", "th", "tn", "tr",
"uk", "yo", "zgh", "zh-hans", "zh-hant"
};
int i = 0, defaultLanguage = -1;
for (const QString& code : m_appLangCodes) {
QString name = QLocale::languageToString(QLocale(code).language());
if(name == "English") defaultLanguage = i;
i++;
}
m_appLangIndex = m_settings.value("app/languageIndex", defaultLanguage).toInt();
// Below regards the search filters
QString defaultLang = QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0);

QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
defaultLangList.append(defaultLang);
QVariant defaultLangVariant(defaultLangList);
m_langList = m_settings.value("language", defaultLangVariant).toList();
/*
* Qt5 & Qt6 have slightly different behaviors with regards to initializing QVariant.
* The below approach is specifically chosen to work with both versions.
Expand All @@ -170,15 +198,6 @@ void SettingsManager::initSettings()
*
* QList(QVariant(QChar, 'E'), QVariant(QChar, 'n'), QVariant(QChar, 'g'), ...
*/

QList<QString> defaultLangList; // Qt5 QList doesn't support supplying a constructor list
defaultLangList.append(defaultLang);
QVariant defaultLangVariant(defaultLangList);
m_langList = m_settings.value("language", defaultLangVariant).toList();


// ui->comboBoxLanguage->addItems(languageList);

m_categoryList = m_settings.value("category", {}).toList();
m_contentTypeList = m_settings.value("contentType", {}).toList();
}
5 changes: 5 additions & 0 deletions src/settingsmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class SettingsManager : public QObject
QString getMonitorDir() const { return m_monitorDir; }
bool getMoveToTrash() const { return m_moveToTrash; }
bool getReopenTab() const { return m_reopenTab; }
QList<QString> getLanguageCodes() { return m_appLangCodes; }
int getAppLanguageIndex() { return m_appLangIndex; }
FilterList getLanguageList() { return deducePair(m_langList); }
FilterList getCategoryList() { return deducePair(m_categoryList); }
FilterList getContentType() { return deducePair(m_contentTypeList); }
Expand All @@ -43,6 +45,7 @@ public slots:
void setMoveToTrash(bool moveToTrash);
void setReopenTab(bool reopenTab);
void setLanguage(FilterList langList);
void setAppLanguage(int languageIndex);
void setCategory(FilterList categoryList);
void setContentType(FilterList contentTypeList);

Expand Down Expand Up @@ -72,9 +75,11 @@ public slots:
QString m_monitorDir;
bool m_moveToTrash;
bool m_reopenTab;
int m_appLangIndex;
QList<QVariant> m_langList;
QList<QVariant> m_categoryList;
QList<QVariant> m_contentTypeList;
QList<QString> m_appLangCodes;
};

#endif // SETTINGSMANAGER_H
21 changes: 12 additions & 9 deletions src/settingsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ SettingsView::SettingsView(QWidget *parent)
connect(ui->reopenTabToggle, &QCheckBox::clicked, this, &SettingsView::setReopenTab);
connect(ui->browseButton, &QPushButton::clicked, this, &SettingsView::browseDownloadDir);
connect(ui->resetButton, &QPushButton::clicked, this, &SettingsView::resetDownloadDir);
connect(ui->comboBoxLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsView::languageSelected);
connect(ui->monitorBrowse, &QPushButton::clicked, this, &SettingsView::browseMonitorDir);
connect(ui->monitorClear, &QPushButton::clicked, this, &SettingsView::clearMonitorDir);
connect(KiwixApp::instance()->getSettingsManager(), &SettingsManager::downloadDirChanged, this, &SettingsView::onDownloadDirChanged);
Expand Down Expand Up @@ -43,22 +42,28 @@ SettingsView::SettingsView(QWidget *parent)

}

void SettingsView::init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab)

void SettingsView::init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab)
{
ui->zoomPercentSpinBox->setValue(zoomPercent);
ui->downloadDirPath->setText(downloadDir);
if (monitorDir == QString()) {
ui->monitorClear->hide();
}
ui->monitorDirPath->setText(monitorDir);
// Application Language Code
const QList<QString> languageCodes = KiwixApp::instance()->getSettingsManager()->getLanguageCodes();
QStringList languageList;
languageList << "English" << "Spanish" << "French" << "German" << "Chinese";
for (const QString& code : languageCodes) {
QString name = QLocale::languageToString(QLocale(code).language());
languageList.append(name);
}
ui->comboBoxLanguage->addItems(languageList);
ui->comboBoxLanguage->setCurrentText("Spanish");
ui->comboBoxLanguage->setCurrentIndex(langIndex);
ui->moveToTrashToggle->setChecked(moveToTrash);
ui->reopenTabToggle->setChecked(reopentab);
// Connect comboBox to change handler after initialization to avoid false calls
connect(ui->comboBoxLanguage, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsView::languageSelected);
}
bool SettingsView::confirmDialog( QString messageText, QString messageTitle)
{
Expand Down Expand Up @@ -166,10 +171,8 @@ void SettingsView::onDownloadDirChanged(const QString &dir)

void SettingsView::languageSelected(const int &languageIndex)
{
qInfo() << languageIndex;
// ui->comboBoxLanguage->setCurrentText(language);
ui->comboBoxLanguage->setCurrentIndex(languageIndex);
//KiwixApp::instance()->getSettingsManager()->setDownloadDir(dir);
KiwixApp::instance()->getSettingsManager()->setAppLanguage(languageIndex);
}

void SettingsView::onMonitorDirChanged(const QString &dir)
Expand Down
6 changes: 2 additions & 4 deletions src/settingsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ class SettingsView : public QWidget
public:
SettingsView(QWidget *parent = nullptr);
~SettingsView(){};
void init(int zoomPercent, const QString &downloadDir,
const QString &monitorDir, const bool moveToTrash,
bool reopentab);
public Q_SLOTS:
void init(int zoomPercent, const QString &downloadDir, const QString &monitorDir, const int &langIndex, const bool moveToTrash, bool reopentab);
public Q_SLOTS:
void resetDownloadDir();
void browseDownloadDir();
void browseMonitorDir();
Expand Down

0 comments on commit 4ddc47d

Please sign in to comment.