Falkon Develop
Cross-platform Qt-based web browser
acceptlanguage.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19/* ============================================================
20* Falkon - Qt web browser
21* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
22*
23* This program is free software: you can redistribute it and/or modify
24* it under the terms of the GNU General Public License as published by
25* the Free Software Foundation, either version 3 of the License, or
26* (at your option) any later version.
27*
28* This program is distributed in the hope that it will be useful,
29* but WITHOUT ANY WARRANTY; without even the implied warranty of
30* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31* GNU General Public License for more details.
32*
33* You should have received a copy of the GNU General Public License
34* along with this program. If not, see <http://www.gnu.org/licenses/>.
35* ============================================================ */
36#include "acceptlanguage.h"
37#include "ui_acceptlanguage.h"
38#include "ui_addacceptlanguage.h"
39#include "mainapplication.h"
40#include "networkmanager.h"
41#include "settings.h"
42
44{
45 QString longCode = QLocale::system().name().replace(QLatin1Char('_'), QLatin1Char('-'));
46
47 if (longCode.size() == 5) {
48 QStringList ret;
49 ret << longCode << longCode.left(2);
50 return ret;
51 }
52
53 return QStringList(longCode);
54}
55
56QByteArray AcceptLanguage::generateHeader(const QStringList &langs)
57{
58 if (langs.isEmpty()) {
59 return {};
60 }
61
62 QByteArray header;
63 header.append(langs.at(0).toLatin1());
64
65 int counter = 8;
66 for (int i = 1; i < langs.count(); i++) {
67 QString s = QSL(",") + langs.at(i) + QSL(";q=0.");
68 s.append(QString::number(counter));
69 if (counter != 2) {
70 counter -= 2;
71 }
72
73 header.append(s.toLatin1());
74 }
75
76 return header;
77}
78
80 : QDialog(parent)
81 , ui(new Ui::AcceptLanguage)
82{
83 setAttribute(Qt::WA_DeleteOnClose);
84
85 ui->setupUi(this);
86 ui->listWidget->setLayoutDirection(Qt::LeftToRight);
87
88 Settings settings;
89 settings.beginGroup(QSL("Language"));
90 const QStringList langs = settings.value(QSL("acceptLanguage"), defaultLanguage()).toStringList();
91 settings.endGroup();
92
93 for (const QString &code : langs) {
94 QString code_ = code;
95 QLocale loc = QLocale(code_.replace(QLatin1Char('-'), QLatin1Char('_')));
96 QString label;
97
98 if (loc.language() == QLocale::C) {
99 label = tr("Personal [%1]").arg(code);
100 }
101 else {
102 label = QSL("%1/%2 [%3]").arg(loc.languageToString(loc.language()), loc.countryToString(loc.country()), code);
103 }
104
105 ui->listWidget->addItem(label);
106 }
107
108 connect(ui->add, &QAbstractButton::clicked, this, &AcceptLanguage::addLanguage);
109 connect(ui->remove, &QAbstractButton::clicked, this, &AcceptLanguage::removeLanguage);
110 connect(ui->up, &QAbstractButton::clicked, this, &AcceptLanguage::upLanguage);
111 connect(ui->down, &QAbstractButton::clicked, this, &AcceptLanguage::downLanguage);
112}
113
114QStringList AcceptLanguage::expand(const QLocale::Language &language)
115{
116 QStringList allLanguages;
117 QList<QLocale::Country> countries = QLocale::countriesForLanguage(language);
118 for (int j = 0; j < countries.size(); ++j) {
119 QString languageString;
120 if (countries.count() == 1) {
121 languageString = QString(QLatin1String("%1 [%2]")).arg(
122 QLocale::languageToString(language),
123 QLocale(language).name().split(QLatin1Char('_')).at(0)
124 );
125 }
126 else {
127 languageString = QString(QLatin1String("%1/%2 [%3]")).arg (
128 QLocale::languageToString(language),
129 QLocale::countryToString(countries.at(j)),
130 QLocale(language, countries.at(j)).name().split(QLatin1Char('_')).join(QLatin1Char('-')).toLower()
131 );
132
133 }
134 if (!allLanguages.contains(languageString)) {
135 allLanguages.append(languageString);
136 }
137 }
138 return allLanguages;
139}
140
141void AcceptLanguage::addLanguage()
142{
143 Ui_AddAcceptLanguage acceptLangUi;
144 QDialog dialog(this);
145 acceptLangUi.setupUi(&dialog);
146 acceptLangUi.listWidget->setLayoutDirection(Qt::LeftToRight);
147
148 QStringList allLanguages;
149 for (int i = 1 + (int)QLocale::C; i <= (int)QLocale::LastLanguage; ++i) {
150 allLanguages += expand(QLocale::Language(i));
151 }
152
153 acceptLangUi.listWidget->addItems(allLanguages);
154
155 connect(acceptLangUi.listWidget, &QListWidget::itemDoubleClicked, &dialog, &QDialog::accept);
156
157 if (dialog.exec() == QDialog::Rejected) {
158 return;
159 }
160
161 if (!acceptLangUi.ownDefinition->text().isEmpty()) {
162 QString title = tr("Personal [%1]").arg(acceptLangUi.ownDefinition->text());
163 ui->listWidget->addItem(title);
164 }
165 else {
166 QListWidgetItem* c = acceptLangUi.listWidget->currentItem();
167 if (!c) {
168 return;
169 }
170
171 ui->listWidget->addItem(c->text());
172 }
173}
174
175void AcceptLanguage::removeLanguage()
176{
177 delete ui->listWidget->currentItem();
178}
179
180void AcceptLanguage::upLanguage()
181{
182 int index = ui->listWidget->currentRow();
183 QListWidgetItem* currentItem = ui->listWidget->currentItem();
184
185 if (!currentItem || index == 0) {
186 return;
187 }
188
189 ui->listWidget->takeItem(index);
190 ui->listWidget->insertItem(index - 1, currentItem);
191 ui->listWidget->setCurrentItem(currentItem);
192}
193
194void AcceptLanguage::downLanguage()
195{
196 int index = ui->listWidget->currentRow();
197 QListWidgetItem* currentItem = ui->listWidget->currentItem();
198
199 if (!currentItem || index == ui->listWidget->count() - 1) {
200 return;
201 }
202
203 ui->listWidget->takeItem(index);
204 ui->listWidget->insertItem(index + 1, currentItem);
205 ui->listWidget->setCurrentItem(currentItem);
206}
207
209{
210 QStringList langs;
211 for (int i = 0; i < ui->listWidget->count(); i++) {
212 QString t = ui->listWidget->item(i)->text();
213 QString code = t.mid(t.indexOf(QLatin1Char('[')) + 1);
214 code.remove(QLatin1Char(']'));
215 langs.append(code);
216 }
217
218 Settings settings;
219 settings.beginGroup(QSL("Language"));
220 settings.setValue(QSL("acceptLanguage"), langs);
221
222 mApp->networkManager()->loadSettings();
223
224 QDialog::close();
225}
226
228{
229 delete ui;
230}
void accept() override
static QByteArray generateHeader(const QStringList &langs)
static QStringList defaultLanguage()
AcceptLanguage(QWidget *parent=nullptr)
~AcceptLanguage() override
void beginGroup(const QString &prefix)
Definition: settings.cpp:79
void endGroup()
Definition: settings.cpp:84
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
#define mApp
t
Definition: i18n.py:27
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40