1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 * Copyright (C) 2016 Piotr Wójcik <chocimier@tlen.pl>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 **************************************************************************/
20 
21 #include "LocaleDialog.h"
22 #include "../core/Application.h"
23 #include "../core/SettingsManager.h"
24 
25 #include "ui_LocaleDialog.h"
26 
27 #include <QtCore/QCollator>
28 
29 namespace Otter
30 {
31 
LocaleDialog(QWidget * parent)32 LocaleDialog::LocaleDialog(QWidget *parent) : Dialog(parent),
33 	m_ui(new Ui::LocaleDialog)
34 {
35 	m_ui->setupUi(this);
36 
37 	const QList<QFileInfo> locales(QDir(Application::getLocalePath()).entryInfoList({QLatin1String("otter-browser_*.qm")}, QDir::Files, QDir::Name));
38 	QVector<QPair<QString, QString> > entries;
39 
40 	for (int i = 0; i < locales.count(); ++i)
41 	{
42 		const QString name(locales.at(i).baseName().remove(QLatin1String("otter-browser_")));
43 		const QLocale locale(Utils::createLocale(name));
44 
45 		if (locale.nativeCountryName().isEmpty() || locale.nativeLanguageName().isEmpty())
46 		{
47 			entries.append({tr("Unknown [%1]").arg(name), name});
48 		}
49 		else
50 		{
51 			entries.append({QStringLiteral("%1 - %2 [%3]").arg(locale.nativeLanguageName()).arg(locale.nativeCountryName()).arg(locale.name()), locale.name()});
52 		}
53 	}
54 
55 	QCollator collator;
56 	collator.setCaseSensitivity(Qt::CaseInsensitive);
57 
58 	std::sort(entries.begin(), entries.end(), [&](const QPair<QString, QString> &first, const QPair<QString, QString> &second)
59 	{
60 		return (collator.compare(first.first, second.first) < 0);
61 	});
62 
63 	for (int i = 0; i < entries.count(); ++i)
64 	{
65 		m_ui->languageComboBox->addItem(entries.at(i).first, entries.at(i).second);
66 	}
67 
68 	const QString currentLocale(SettingsManager::getOption(SettingsManager::Browser_LocaleOption).toString());
69 
70 	m_ui->languageComboBox->setCurrentIndex((currentLocale.endsWith(QLatin1String(".qm"))) ? 1 : qMax(0, m_ui->languageComboBox->findData(currentLocale)));
71 	m_ui->customFilePathWidget->setEnabled(m_ui->languageComboBox->currentIndex() == 1);
72 	m_ui->customFilePathWidget->setFilters({tr("Translation files (*.qm)")});
73 
74 	if (m_ui->languageComboBox->currentIndex() == 1)
75 	{
76 		m_ui->customFilePathWidget->setPath(currentLocale);
77 	}
78 
79 	connect(this, &LocaleDialog::accepted, this, &LocaleDialog::save);
80 	connect(m_ui->languageComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &LocaleDialog::handleCurrentIndexChanged);
81 }
82 
~LocaleDialog()83 LocaleDialog::~LocaleDialog()
84 {
85 	delete m_ui;
86 }
87 
changeEvent(QEvent * event)88 void LocaleDialog::changeEvent(QEvent *event)
89 {
90 	QDialog::changeEvent(event);
91 
92 	if (event->type() == QEvent::LanguageChange)
93 	{
94 		m_ui->retranslateUi(this);
95 	}
96 }
97 
handleCurrentIndexChanged(int index)98 void LocaleDialog::handleCurrentIndexChanged(int index)
99 {
100 	m_ui->customFilePathWidget->setEnabled(index == 1);
101 }
102 
save()103 void LocaleDialog::save()
104 {
105 	QString locale;
106 
107 	if (m_ui->languageComboBox->currentIndex() == 0)
108 	{
109 		locale = QLatin1String("system");
110 	}
111 	else if (m_ui->languageComboBox->currentIndex() == 1)
112 	{
113 		locale = m_ui->customFilePathWidget->getPath();
114 	}
115 	else
116 	{
117 		locale = m_ui->languageComboBox->currentData(Qt::UserRole).toString();
118 	}
119 
120 	SettingsManager::setOption(SettingsManager::Browser_LocaleOption, locale);
121 }
122 
123 }
124