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 
defaultLanguage()43 QStringList AcceptLanguage::defaultLanguage()
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 
generateHeader(const QStringList & langs)56 QByteArray AcceptLanguage::generateHeader(const QStringList &langs)
57 {
58     if (langs.isEmpty()) {
59         return QByteArray();
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 = "," + langs.at(i) + ";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 
AcceptLanguage(QWidget * parent)79 AcceptLanguage::AcceptLanguage(QWidget* parent)
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("Language");
90     const QStringList langs = settings.value("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 = QString("%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 
expand(const QLocale::Language & language)114 QStringList 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]"))
122                              .arg(QLocale::languageToString(language))
123                              .arg(QLocale(language).name().split(QLatin1Char('_')).at(0));
124         }
125         else {
126             languageString = QString(QLatin1String("%1/%2 [%3]"))
127                              .arg(QLocale::languageToString(language))
128                              .arg(QLocale::countryToString(countries.at(j)))
129                              .arg(QLocale(language, countries.at(j)).name().split(QLatin1Char('_')).join(QLatin1Char('-')).toLower());
130 
131         }
132         if (!allLanguages.contains(languageString)) {
133             allLanguages.append(languageString);
134         }
135     }
136     return allLanguages;
137 }
138 
addLanguage()139 void AcceptLanguage::addLanguage()
140 {
141     Ui_AddAcceptLanguage acceptLangUi;
142     QDialog dialog(this);
143     acceptLangUi.setupUi(&dialog);
144     acceptLangUi.listWidget->setLayoutDirection(Qt::LeftToRight);
145 
146     QStringList allLanguages;
147     for (int i = 1 + (int)QLocale::C; i <= (int)QLocale::LastLanguage; ++i) {
148         allLanguages += expand(QLocale::Language(i));
149     }
150 
151     acceptLangUi.listWidget->addItems(allLanguages);
152 
153     connect(acceptLangUi.listWidget, &QListWidget::itemDoubleClicked, &dialog, &QDialog::accept);
154 
155     if (dialog.exec() == QDialog::Rejected) {
156         return;
157     }
158 
159     if (!acceptLangUi.ownDefinition->text().isEmpty()) {
160         QString title = tr("Personal [%1]").arg(acceptLangUi.ownDefinition->text());
161         ui->listWidget->addItem(title);
162     }
163     else {
164         QListWidgetItem* c = acceptLangUi.listWidget->currentItem();
165         if (!c) {
166             return;
167         }
168 
169         ui->listWidget->addItem(c->text());
170     }
171 }
172 
removeLanguage()173 void AcceptLanguage::removeLanguage()
174 {
175     delete ui->listWidget->currentItem();
176 }
177 
upLanguage()178 void AcceptLanguage::upLanguage()
179 {
180     int index = ui->listWidget->currentRow();
181     QListWidgetItem* currentItem = ui->listWidget->currentItem();
182 
183     if (!currentItem || index == 0) {
184         return;
185     }
186 
187     ui->listWidget->takeItem(index);
188     ui->listWidget->insertItem(index - 1, currentItem);
189     ui->listWidget->setCurrentItem(currentItem);
190 }
191 
downLanguage()192 void AcceptLanguage::downLanguage()
193 {
194     int index = ui->listWidget->currentRow();
195     QListWidgetItem* currentItem = ui->listWidget->currentItem();
196 
197     if (!currentItem || index == ui->listWidget->count() - 1) {
198         return;
199     }
200 
201     ui->listWidget->takeItem(index);
202     ui->listWidget->insertItem(index + 1, currentItem);
203     ui->listWidget->setCurrentItem(currentItem);
204 }
205 
accept()206 void AcceptLanguage::accept()
207 {
208     QStringList langs;
209     for (int i = 0; i < ui->listWidget->count(); i++) {
210         QString t = ui->listWidget->item(i)->text();
211         QString code = t.mid(t.indexOf(QLatin1Char('[')) + 1);
212         code.remove(QLatin1Char(']'));
213         langs.append(code);
214     }
215 
216     Settings settings;
217     settings.beginGroup("Language");
218     settings.setValue("acceptLanguage", langs);
219 
220     mApp->networkManager()->loadSettings();
221 
222     QDialog::close();
223 }
224 
~AcceptLanguage()225 AcceptLanguage::~AcceptLanguage()
226 {
227     delete ui;
228 }
229