2011-10-18 17:39:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-10-18 17:39:51 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "acceptlanguage.h"
|
|
|
|
#include "ui_acceptlanguage.h"
|
|
|
|
#include "ui_addacceptlanguage.h"
|
|
|
|
#include "mainapplication.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
QStringList AcceptLanguage::defaultLanguage()
|
|
|
|
{
|
2013-08-02 20:16:06 +02:00
|
|
|
QString longCode = QLocale::system().name().replace(QLatin1Char('_'), QLatin1Char('-'));
|
|
|
|
|
|
|
|
if (longCode.size() == 5) {
|
|
|
|
QStringList ret;
|
|
|
|
ret << longCode << longCode.left(2);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList(longCode);
|
2011-10-18 17:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray AcceptLanguage::generateHeader(const QStringList &langs)
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (langs.count() == 0) {
|
2011-10-18 17:39:51 +02:00
|
|
|
return QByteArray();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
QByteArray header;
|
|
|
|
header.append(langs.at(0));
|
|
|
|
|
|
|
|
int counter = 8;
|
|
|
|
for (int i = 1; i < langs.count(); i++) {
|
2013-08-02 20:16:06 +02:00
|
|
|
QString s = "," + langs.at(i) + ";q=0.";
|
2011-10-18 17:39:51 +02:00
|
|
|
s.append(QString::number(counter));
|
2011-11-06 17:01:23 +01:00
|
|
|
if (counter != 2) {
|
|
|
|
counter -= 2;
|
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
header.append(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
AcceptLanguage::AcceptLanguage(QWidget* parent)
|
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::AcceptLanguage)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2013-07-11 18:18:32 +02:00
|
|
|
ui->listWidget->setLayoutDirection(Qt::LeftToRight);
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-10-18 17:39:51 +02:00
|
|
|
settings.beginGroup("Language");
|
|
|
|
QStringList langs = settings.value("acceptLanguage", defaultLanguage()).toStringList();
|
2013-01-19 01:05:17 +01:00
|
|
|
settings.endGroup();
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &code, langs) {
|
2011-10-18 17:39:51 +02:00
|
|
|
QString code_ = code;
|
2012-09-04 12:42:45 +02:00
|
|
|
QLocale loc = QLocale(code_.replace(QLatin1Char('-'), QLatin1Char('_')));
|
2011-10-18 17:39:51 +02:00
|
|
|
QString label;
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (loc.language() == QLocale::C) {
|
2011-10-18 17:39:51 +02:00
|
|
|
label = tr("Personal [%1]").arg(code);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-10-18 17:39:51 +02:00
|
|
|
label = QString("%1/%2 [%3]").arg(loc.languageToString(loc.language()), loc.countryToString(loc.country()), code);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
ui->listWidget->addItem(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(ui->add, SIGNAL(clicked()), this, SLOT(addLanguage()));
|
|
|
|
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeLanguage()));
|
|
|
|
connect(ui->up, SIGNAL(clicked()), this, SLOT(upLanguage()));
|
|
|
|
connect(ui->down, SIGNAL(clicked()), this, SLOT(downLanguage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList AcceptLanguage::expand(const QLocale::Language &language)
|
|
|
|
{
|
|
|
|
QStringList allLanguages;
|
|
|
|
QList<QLocale::Country> countries = QLocale::countriesForLanguage(language);
|
|
|
|
for (int j = 0; j < countries.size(); ++j) {
|
|
|
|
QString languageString;
|
|
|
|
if (countries.count() == 1) {
|
|
|
|
languageString = QString(QLatin1String("%1 [%2]"))
|
2011-11-06 17:01:23 +01:00
|
|
|
.arg(QLocale::languageToString(language))
|
|
|
|
.arg(QLocale(language).name().split(QLatin1Char('_')).at(0));
|
|
|
|
}
|
|
|
|
else {
|
2011-10-18 17:39:51 +02:00
|
|
|
languageString = QString(QLatin1String("%1/%2 [%3]"))
|
2011-11-06 17:01:23 +01:00
|
|
|
.arg(QLocale::languageToString(language))
|
|
|
|
.arg(QLocale::countryToString(countries.at(j)))
|
|
|
|
.arg(QLocale(language, countries.at(j)).name().split(QLatin1Char('_')).join(QLatin1String("-")).toLower());
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!allLanguages.contains(languageString)) {
|
2011-10-18 17:39:51 +02:00
|
|
|
allLanguages.append(languageString);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
}
|
|
|
|
return allLanguages;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AcceptLanguage::addLanguage()
|
|
|
|
{
|
2013-05-03 12:00:08 +02:00
|
|
|
Ui_AddAcceptLanguage acceptLangUi;
|
|
|
|
QDialog dialog(this);
|
|
|
|
acceptLangUi.setupUi(&dialog);
|
2013-07-11 18:18:32 +02:00
|
|
|
acceptLangUi.listWidget->setLayoutDirection(Qt::LeftToRight);
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
QStringList allLanguages;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 1 + (int)QLocale::C; i <= (int)QLocale::LastLanguage; ++i) {
|
2011-10-18 17:39:51 +02:00
|
|
|
allLanguages += expand(QLocale::Language(i));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2013-05-03 12:00:08 +02:00
|
|
|
acceptLangUi.listWidget->addItems(allLanguages);
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2013-05-03 12:00:08 +02:00
|
|
|
connect(acceptLangUi.listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), &dialog, SLOT(accept()));
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2013-05-03 12:00:08 +02:00
|
|
|
if (dialog.exec() == QDialog::Rejected) {
|
2011-10-18 17:39:51 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
2013-05-03 12:00:08 +02:00
|
|
|
if (!acceptLangUi.ownDefinition->text().isEmpty()) {
|
|
|
|
QString title = tr("Personal [%1]").arg(acceptLangUi.ownDefinition->text());
|
2011-10-18 17:39:51 +02:00
|
|
|
ui->listWidget->addItem(title);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-05-03 12:00:08 +02:00
|
|
|
QListWidgetItem* c = acceptLangUi.listWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!c) {
|
2011-10-18 17:39:51 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
ui->listWidget->addItem(c->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AcceptLanguage::removeLanguage()
|
|
|
|
{
|
2012-09-11 11:43:11 +02:00
|
|
|
delete ui->listWidget->currentItem();
|
2011-10-18 17:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AcceptLanguage::upLanguage()
|
|
|
|
{
|
|
|
|
int index = ui->listWidget->currentRow();
|
|
|
|
QListWidgetItem* currentItem = ui->listWidget->currentItem();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!currentItem || index == 0) {
|
2011-10-18 17:39:51 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
ui->listWidget->takeItem(index);
|
|
|
|
ui->listWidget->insertItem(index - 1, currentItem);
|
|
|
|
ui->listWidget->setCurrentItem(currentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AcceptLanguage::downLanguage()
|
|
|
|
{
|
|
|
|
int index = ui->listWidget->currentRow();
|
|
|
|
QListWidgetItem* currentItem = ui->listWidget->currentItem();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!currentItem || index == ui->listWidget->count() - 1) {
|
2011-10-18 17:39:51 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-18 17:39:51 +02:00
|
|
|
|
|
|
|
ui->listWidget->takeItem(index);
|
|
|
|
ui->listWidget->insertItem(index + 1, currentItem);
|
|
|
|
ui->listWidget->setCurrentItem(currentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AcceptLanguage::accept()
|
|
|
|
{
|
|
|
|
QStringList langs;
|
|
|
|
for (int i = 0; i < ui->listWidget->count(); i++) {
|
|
|
|
QString t = ui->listWidget->item(i)->text();
|
2012-09-04 12:42:45 +02:00
|
|
|
QString code = t.mid(t.indexOf(QLatin1Char('[')) + 1);
|
|
|
|
code.remove(QLatin1Char(']'));
|
2011-10-18 17:39:51 +02:00
|
|
|
langs.append(code);
|
|
|
|
}
|
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-10-18 17:39:51 +02:00
|
|
|
settings.beginGroup("Language");
|
|
|
|
settings.setValue("acceptLanguage", langs);
|
|
|
|
|
|
|
|
QDialog::close();
|
|
|
|
}
|
|
|
|
|
|
|
|
AcceptLanguage::~AcceptLanguage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|