1 /***************************************************************************
2 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.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 *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19
20 #include "localemodels.h"
21
22 #include <KLanguageName>
23 #include <KLocale>
24
25 #include <QLocale>
26 #include <QStandardPaths>
27
28
CountryModel(QObject * parent)29 CountryModel::CountryModel(QObject *parent)
30 : QAbstractListModel(parent)
31 {
32 }
33
data(const QModelIndex & index,int role) const34 QVariant CountryModel::data(const QModelIndex &index, int role) const
35 {
36 if (!index.isValid())
37 {
38 return QVariant();
39 }
40
41 if (role == Qt::DisplayRole)
42 {
43 return m_countryNames.value(index.row());
44 }
45 else if (role == Qt::DecorationRole)
46 {
47 return m_countryIcons.value(index.row());
48 }
49 else if (role == Qt::UserRole)
50 {
51 return m_countryCodes.value(index.row());
52 }
53
54 return QVariant();
55 }
56
rowCount(const QModelIndex & parent) const57 int CountryModel::rowCount(const QModelIndex &parent) const
58 {
59 if (parent.isValid())
60 {
61 return 0;
62 }
63
64 return m_countryCodes.count();
65 }
66
setupModelData()67 void CountryModel::setupModelData()
68 {
69 beginResetModel();
70 for (int c = 1; c <= QLocale::LastCountry; ++c)
71 {
72 QString countryCode;
73 const auto country = static_cast<QLocale::Country>(c);
74 QLocale locale(QLocale::AnyLanguage, country);
75 if (locale.country() == country)
76 {
77 const QString localeName = locale.name();
78 const auto idx = localeName.indexOf(QLatin1Char('_'));
79 if (idx != -1)
80 {
81 countryCode = localeName.mid(idx + 1);
82 }
83 }
84 const QString countryName = KLocale::global()->countryCodeToName(countryCode);
85
86 if (!countryName.isEmpty())
87 {
88 m_countryCodes.append(countryCode);
89 m_countryNames.append(countryName);
90
91 QString path = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QLatin1String("locale/") + QString::fromLatin1("l10n/%1/flag.png").arg(countryCode));
92 if (path.isEmpty())
93 {
94 m_countryIcons.append(QIcon());
95 }
96 else
97 {
98 m_countryIcons.append(QIcon::fromTheme(path));
99 }
100 }
101 }
102 endResetModel();
103 }
104
LanguageModel(QObject * parent)105 LanguageModel::LanguageModel(QObject *parent)
106 : QAbstractListModel(parent)
107 {
108 }
109
data(const QModelIndex & index,int role) const110 QVariant LanguageModel::data(const QModelIndex &index, int role) const
111 {
112 if (!index.isValid())
113 {
114 return QVariant();
115 }
116 else if (role == Qt::DisplayRole)
117 {
118 return m_languageNames.value(index.row());
119 }
120
121 return QVariant();
122 }
123
rowCount(const QModelIndex & parent) const124 int LanguageModel::rowCount(const QModelIndex &parent) const
125 {
126 if (parent.isValid())
127 {
128 return 0;
129 }
130
131 return m_languageNames.count();
132 }
133
setupModelData()134 void LanguageModel::setupModelData()
135 {
136 beginResetModel();
137 for (int l = 1; l <= QLocale::LastLanguage; ++l)
138 {
139 const auto lang = static_cast<QLocale::Language>(l);
140 QLocale locale(lang);
141 if (locale.language() == lang)
142 {
143 const QString localeName = locale.name();
144 const QString languageName = KLanguageName::nameForCode(localeName);
145 if (!languageName.isEmpty())
146 {
147 m_languageNames.append(languageName);
148 }
149 }
150 }
151 endResetModel();
152 }
153
154
155