File indexing completed on 2024-05-12 04:58:18
0001 /* 0002 * Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program; if not, write to the Free Software 0016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301 USA 0018 */ 0019 /* ============================================================ 0020 * Falkon - Qt web browser 0021 * Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com> 0022 * 0023 * This program is free software: you can redistribute it and/or modify 0024 * it under the terms of the GNU General Public License as published by 0025 * the Free Software Foundation, either version 3 of the License, or 0026 * (at your option) any later version. 0027 * 0028 * This program is distributed in the hope that it will be useful, 0029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0031 * GNU General Public License for more details. 0032 * 0033 * You should have received a copy of the GNU General Public License 0034 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0035 * ============================================================ */ 0036 #include "acceptlanguage.h" 0037 #include "ui_acceptlanguage.h" 0038 #include "ui_addacceptlanguage.h" 0039 #include "mainapplication.h" 0040 #include "networkmanager.h" 0041 #include "settings.h" 0042 0043 QStringList AcceptLanguage::defaultLanguage() 0044 { 0045 QString longCode = QLocale::system().name().replace(QLatin1Char('_'), QLatin1Char('-')); 0046 0047 if (longCode.size() == 5) { 0048 QStringList ret; 0049 ret << longCode << longCode.left(2); 0050 return ret; 0051 } 0052 0053 return QStringList(longCode); 0054 } 0055 0056 QByteArray AcceptLanguage::generateHeader(const QStringList &langs) 0057 { 0058 if (langs.isEmpty()) { 0059 return {}; 0060 } 0061 0062 QByteArray header; 0063 header.append(langs.at(0).toLatin1()); 0064 0065 int counter = 8; 0066 for (int i = 1; i < langs.count(); i++) { 0067 QString s = QSL(",") + langs.at(i) + QSL(";q=0."); 0068 s.append(QString::number(counter)); 0069 if (counter != 2) { 0070 counter -= 2; 0071 } 0072 0073 header.append(s.toLatin1()); 0074 } 0075 0076 return header; 0077 } 0078 0079 AcceptLanguage::AcceptLanguage(QWidget* parent) 0080 : QDialog(parent) 0081 , ui(new Ui::AcceptLanguage) 0082 { 0083 setAttribute(Qt::WA_DeleteOnClose); 0084 0085 ui->setupUi(this); 0086 ui->listWidget->setLayoutDirection(Qt::LeftToRight); 0087 0088 Settings settings; 0089 settings.beginGroup(QSL("Language")); 0090 const QStringList langs = settings.value(QSL("acceptLanguage"), defaultLanguage()).toStringList(); 0091 settings.endGroup(); 0092 0093 for (const QString &code : langs) { 0094 QString code_ = code; 0095 QLocale loc = QLocale(code_.replace(QLatin1Char('-'), QLatin1Char('_'))); 0096 QString label; 0097 0098 if (loc.language() == QLocale::C) { 0099 label = tr("Personal [%1]").arg(code); 0100 } 0101 else { 0102 label = QSL("%1/%2 [%3]").arg(loc.languageToString(loc.language()), loc.countryToString(loc.country()), code); 0103 } 0104 0105 ui->listWidget->addItem(label); 0106 } 0107 0108 connect(ui->add, &QAbstractButton::clicked, this, &AcceptLanguage::addLanguage); 0109 connect(ui->remove, &QAbstractButton::clicked, this, &AcceptLanguage::removeLanguage); 0110 connect(ui->up, &QAbstractButton::clicked, this, &AcceptLanguage::upLanguage); 0111 connect(ui->down, &QAbstractButton::clicked, this, &AcceptLanguage::downLanguage); 0112 } 0113 0114 QStringList AcceptLanguage::expand(const QLocale::Language &language) 0115 { 0116 QStringList allLanguages; 0117 QList<QLocale::Country> countries = QLocale::countriesForLanguage(language); 0118 for (int j = 0; j < countries.size(); ++j) { 0119 QString languageString; 0120 if (countries.count() == 1) { 0121 languageString = QString(QLatin1String("%1 [%2]")).arg( 0122 QLocale::languageToString(language), 0123 QLocale(language).name().split(QLatin1Char('_')).at(0) 0124 ); 0125 } 0126 else { 0127 languageString = QString(QLatin1String("%1/%2 [%3]")).arg ( 0128 QLocale::languageToString(language), 0129 QLocale::countryToString(countries.at(j)), 0130 QLocale(language, countries.at(j)).name().split(QLatin1Char('_')).join(QLatin1Char('-')).toLower() 0131 ); 0132 0133 } 0134 if (!allLanguages.contains(languageString)) { 0135 allLanguages.append(languageString); 0136 } 0137 } 0138 return allLanguages; 0139 } 0140 0141 void AcceptLanguage::addLanguage() 0142 { 0143 Ui_AddAcceptLanguage acceptLangUi; 0144 QDialog dialog(this); 0145 acceptLangUi.setupUi(&dialog); 0146 acceptLangUi.listWidget->setLayoutDirection(Qt::LeftToRight); 0147 0148 QStringList allLanguages; 0149 for (int i = 1 + (int)QLocale::C; i <= (int)QLocale::LastLanguage; ++i) { 0150 allLanguages += expand(QLocale::Language(i)); 0151 } 0152 0153 acceptLangUi.listWidget->addItems(allLanguages); 0154 0155 connect(acceptLangUi.listWidget, &QListWidget::itemDoubleClicked, &dialog, &QDialog::accept); 0156 0157 if (dialog.exec() == QDialog::Rejected) { 0158 return; 0159 } 0160 0161 if (!acceptLangUi.ownDefinition->text().isEmpty()) { 0162 QString title = tr("Personal [%1]").arg(acceptLangUi.ownDefinition->text()); 0163 ui->listWidget->addItem(title); 0164 } 0165 else { 0166 QListWidgetItem* c = acceptLangUi.listWidget->currentItem(); 0167 if (!c) { 0168 return; 0169 } 0170 0171 ui->listWidget->addItem(c->text()); 0172 } 0173 } 0174 0175 void AcceptLanguage::removeLanguage() 0176 { 0177 delete ui->listWidget->currentItem(); 0178 } 0179 0180 void AcceptLanguage::upLanguage() 0181 { 0182 int index = ui->listWidget->currentRow(); 0183 QListWidgetItem* currentItem = ui->listWidget->currentItem(); 0184 0185 if (!currentItem || index == 0) { 0186 return; 0187 } 0188 0189 ui->listWidget->takeItem(index); 0190 ui->listWidget->insertItem(index - 1, currentItem); 0191 ui->listWidget->setCurrentItem(currentItem); 0192 } 0193 0194 void AcceptLanguage::downLanguage() 0195 { 0196 int index = ui->listWidget->currentRow(); 0197 QListWidgetItem* currentItem = ui->listWidget->currentItem(); 0198 0199 if (!currentItem || index == ui->listWidget->count() - 1) { 0200 return; 0201 } 0202 0203 ui->listWidget->takeItem(index); 0204 ui->listWidget->insertItem(index + 1, currentItem); 0205 ui->listWidget->setCurrentItem(currentItem); 0206 } 0207 0208 void AcceptLanguage::accept() 0209 { 0210 QStringList langs; 0211 for (int i = 0; i < ui->listWidget->count(); i++) { 0212 QString t = ui->listWidget->item(i)->text(); 0213 QString code = t.mid(t.indexOf(QLatin1Char('[')) + 1); 0214 code.remove(QLatin1Char(']')); 0215 langs.append(code); 0216 } 0217 0218 Settings settings; 0219 settings.beginGroup(QSL("Language")); 0220 settings.setValue(QSL("acceptLanguage"), langs); 0221 0222 mApp->networkManager()->loadSettings(); 0223 0224 QDialog::close(); 0225 } 0226 0227 AcceptLanguage::~AcceptLanguage() 0228 { 0229 delete ui; 0230 }