4 * Copyright (c) 1998 Matthias Hoelzer (hoelzer@physik.uni-wuerzburg.de)
5 * Copyright (c) 1999 Preston Brown <pbrown@kde.org>
6 * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
8 * Requires the Qt widget libraries, available at no cost at
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <QPushButton>
32 #include <KIconLoader>
34 #include <KConfigGroup>
35 #include <KStandardDirs>
37 #include "kcmlocale.h"
38 #include "kcmlocale.moc"
40 #include "klanguagebutton.h"
41 #include "countryselectordialog.h"
42 #include "kcontrollocale.h"
44 static QStringList
languageList(const QString
& country
)
46 QString fileName
= KStandardDirs::locate("locale",
47 QString::fromLatin1("l10n/%1/entry.desktop")
50 KConfig
_entry( fileName
, KConfig::SimpleConfig
);
51 KConfigGroup
entry(&_entry
, "KCM Locale");
53 return entry
.readEntry("Languages", QStringList());
56 static QString
readLocale(const QString
&language
, const QString
&path
, const QString
&sub
=QString())
59 QString filepath
= QString::fromLatin1("%1%2/entry.desktop")
63 KConfig
entry(KStandardDirs::locate("locale", filepath
));
64 entry
.setLocale(language
);
65 KConfigGroup entryGroup
= entry
.group("KCM Locale");
66 return entryGroup
.readEntry("Name");
71 KLocaleConfig::KLocaleConfig(KControlLocale
*locale
, QWidget
*parent
)
77 m_crLabel
->setObjectName(I18N_NOOP("Country or region:"));
78 m_languagesLabel
->setObjectName(I18N_NOOP("Languages:"));
79 languageRemove
->setObjectName(I18N_NOOP("Remove"));
80 m_upButton
->setObjectName(QString());
81 m_downButton
->setObjectName(QString());
82 m_selectedCountryLabel
->setObjectName(QString());
84 connect(languageRemove
, SIGNAL(clicked()),
85 SLOT(slotRemoveLanguage()));
86 connect(m_upButton
, SIGNAL(clicked()),
87 SLOT(slotLanguageUp()));
88 connect(m_downButton
, SIGNAL(clicked()),
89 SLOT(slotLanguageDown()));
91 connect(m_selectedCountryLabel
, SIGNAL(linkActivated(const QString
&)), SLOT(changeCountry()));
92 connect(languageAdd
, SIGNAL(activated(const QString
&)), SLOT(slotAddLanguage(const QString
&)));
93 connect(m_languages
, SIGNAL(itemSelectionChanged()), SLOT(slotCheckButtons()));
95 m_upButton
->setIcon(KIcon("arrow-up"));
96 m_downButton
->setIcon(KIcon("arrow-down"));
98 languageAdd
->loadAllLanguages();
100 KConfigGroup configGroup
= KGlobal::config()->group("Locale");
101 m_languageList
= configGroup
.readEntry("Language").split(':',QString::SkipEmptyParts
);
104 void KLocaleConfig::slotAddLanguage(const QString
& code
)
106 int pos
= m_languages
->currentRow();
110 // If it's already in list, just move it (delete the old, then insert a new)
111 int oldPos
= m_languageList
.indexOf( code
);
113 m_languageList
.removeAll( code
);
115 if ( oldPos
!= -1 && oldPos
< pos
)
118 m_languageList
.insert( pos
, code
);
120 m_locale
->setLanguage( m_languageList
);
122 emit
localeChanged();
124 emit
languageChanged();
127 void KLocaleConfig::slotRemoveLanguage()
129 int pos
= m_languages
->currentRow();
133 m_languageList
.removeAt(pos
);
135 m_locale
->setLanguage( m_languageList
);
137 emit
localeChanged();
139 emit
languageChanged();
143 void KLocaleConfig::languageMove(Direction direcition
)
145 int pos
= m_languages
->currentRow();
147 QStringList::Iterator it1
= m_languageList
.begin() + pos
- 1*(direcition
==Up
);
148 QStringList::Iterator it2
= m_languageList
.begin() + pos
+ 1*(direcition
==Down
);
150 if ( it1
!= m_languageList
.end() && it2
!= m_languageList
.end() )
156 m_locale
->setLanguage( m_languageList
);
158 emit
localeChanged();
159 if ( pos
== 1*(direcition
==Up
) ) //Up: at the top, Down: at the lang before the top
160 emit
languageChanged();
164 void KLocaleConfig::slotLanguageUp()
169 void KLocaleConfig::slotLanguageDown()
174 void KLocaleConfig::changeCountry()
176 CountrySelectorDialog
*csd
= new CountrySelectorDialog(this);
177 if (csd
->editCountry(m_locale
)) emit
localeChanged();
182 void KLocaleConfig::save()
184 KSharedConfigPtr config
= KGlobal::config();
186 KConfigGroup
configGroup(config
, "Locale");
188 configGroup
.writeEntry("Country", m_locale
->country(), KConfig::Persistent
|KConfig::Global
);
189 configGroup
.writeEntry("Language",
190 m_languageList
.join(":"), KConfig::Persistent
|KConfig::Global
);
195 void KLocaleConfig::slotCheckButtons()
197 languageRemove
->setEnabled( m_languages
->currentRow() != -1 && m_languages
->count() > 1 );
198 m_upButton
->setEnabled( m_languages
->currentRow() > 0 );
199 m_downButton
->setEnabled( m_languages
->currentRow() != -1 &&
200 m_languages
->currentRow() < (signed)(m_languages
->count() - 1) );
203 void KLocaleConfig::slotLocaleChanged()
205 // update language widget
206 m_languages
->clear();
207 foreach (const QString
& langCode
, m_languageList
)
208 m_languages
->addItem(readLocale(m_locale
->language(),langCode
));
211 QString country
= m_locale
->countryCodeToName(m_locale
->country());
212 if (country
.isEmpty()) country
= i18nc("@item:intext Country", "Not set (Generic English)");
213 m_selectedCountryLabel
->setText(i18nc("@info %1 is country name", "<html>%1 (<a href=\"changeCountry\">change...</a>)</html>", country
));
216 void KLocaleConfig::slotTranslate()
218 languageAdd
->setText( ki18n("Add Language").toString(m_locale
) );
220 m_selectedCountryLabel
->setToolTip( ki18n
221 ( "This is where you live. KDE will use the defaults for "
222 "this country or region.").toString(m_locale
) );
223 languageAdd
->setToolTip( ki18n
224 ( "This will add a language to the list. If the language is already "
225 "in the list, the old one will be moved instead." ).toString(m_locale
) );
227 languageRemove
->setToolTip( ki18n
228 ( "This will remove the highlighted language from the list." ).toString(m_locale
) );
230 m_languages
->setToolTip( ki18n
231 ( "KDE programs will be displayed in the first available language in "
232 "this list.\nIf none of the languages are available, US English "
233 "will be used.").toString(m_locale
) );
238 ( "Here you can choose your country or region. The settings "
239 "for languages, numbers etc. will automatically switch to the "
240 "corresponding values." ).toString(m_locale
);
241 m_crLabel
->setWhatsThis( str
);
242 m_selectedCountryLabel
->setWhatsThis( str
);
245 ( "<p>Here you can choose the languages that will be used by KDE. If the "
246 "first language in the list is not available, the second will be used, "
247 "etc. If only US English is available, no translations "
248 "have been installed. You can get translation packages for many "
249 "languages from the place you got KDE from.</p><p>"
250 "Note that some applications may not be translated to your languages; "
251 "in this case, they will automatically fall back to US English.</p>" ).toString(m_locale
);
252 m_languagesLabel
->setWhatsThis( str
);
253 m_languages
->setWhatsThis( str
);
254 languageAdd
->setWhatsThis( str
);
255 languageRemove
->setWhatsThis( str
);
259 void KLocaleConfig::changedCountry(const QString
& code
)
261 m_locale
->setCountry(code
);
263 // change to the preferred languages in that country, installed only
264 QStringList languages
= languageList(m_locale
->country());
265 QStringList newLanguageList
;
266 foreach (const QString
& langCode
, languages
)
268 if (!readLocale(m_locale
->language(),langCode
).isEmpty())
269 newLanguageList
+= langCode
;
271 m_locale
->setLanguage( newLanguageList
);
273 emit
localeChanged();
274 emit
languageChanged();