summaryrefslogtreecommitdiffstats
path: root/kpersonalizer/kcountrypage.cpp
blob: bce43a15230f783be7613fb8821d0d9d18e5c72c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
                          kcountrypage.cpp  -  description
                             -------------------
    begin                : Tue May 22 2001
    copyright            : (C) 2001 by Ralf Nolden
    email                : nolden@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqstringlist.h>
#include <tqlabel.h>
#include <tqmap.h>

#include <kapplication.h>
#include <ksimpleconfig.h>
#include <klocale.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <dcopclient.h>
#include <kprocess.h>
#include <klanguagebutton.h>

#include "kfindlanguage.h"

#include "kcountrypage.h"

KCountryPage::KCountryPage(TQWidget *parent, const char *name ) : KCountryPageDlg(parent,name) {

	px_introSidebar->setPixmap(UserIcon("step1.png"));

	connect(cb_country, TQT_SIGNAL(activated(const TQString &)), TQT_SLOT(setLangForCountry(const TQString &)));
	connect(cb_language, TQT_SIGNAL(activated(const TQString &)), TQT_SLOT(setLanguageChanged()));

	// naturally, the language is not changed on startup
	b_savedLanguageChanged = false;
	b_startedLanguageChanged = false;

	// set appropriate Trinity version (kapplication.h)
	txt_welcome->setText(i18n("<h3>Welcome to Trinity %1</h3>").arg(KDE_VERSION_STRING));

	flang = new KFindLanguage();

	// need this ones for decision over restarts of kp, kicker, etc
	s_oldlocale = KGlobal::locale()->language();

	// load the Menus and guess the selection
	loadCountryList(cb_country);
	fillLanguageMenu(cb_language);
	cb_language->setCurrentItem(flang->getBestLang());
	cb_country->setCurrentItem("C");

	// Highlight the users's country
	for(int i = 0; i < cb_country->count(); i++) {
		if(cb_country->id(i) == flang->getCountry()) {
			cb_country->setCurrentItem(cb_country->id(i));
			break;
		}
	}

	setLanguageChanged();
}

KCountryPage::~KCountryPage(){
	delete flang;
}


void KCountryPage::loadCountryList(KLanguageButton *combo) {

	TQString sub = TQString::tqfromLatin1("l10n/");

	// clear the list
	combo->clear();

	TQStringList regionfiles = KGlobal::dirs()->findAllResources("locale", sub + "*.desktop");
	TQMap<TQString,TQString> regionnames;

	for ( TQStringList::ConstIterator it = regionfiles.begin(); it != regionfiles.end(); ++it ) {
		KSimpleConfig entry(*it);
		entry.setGroup(TQString::tqfromLatin1("KCM Locale"));
		TQString name = entry.readEntry(TQString::tqfromLatin1("Name"), i18n("without name"));

		TQString tag = *it;
		int index;

		index = tag.findRev('/');
		if (index != -1)
			tag = tag.mid(index + 1);

		index = tag.findRev('.');
		if (index != -1)
			tag.truncate(index);

		regionnames.insert(name, tag);
	}

	for ( TQMap<TQString,TQString>::ConstIterator mit = regionnames.begin(); mit != regionnames.end(); ++mit ) {
		combo->insertSubmenu( mit.key(), '-' + mit.data(), sub );
	}

	// add all languages to the list
	TQStringList countrylist = KGlobal::dirs()->findAllResources("locale", sub + "*/entry.desktop");
	countrylist.sort();

	for ( TQStringList::ConstIterator it = countrylist.begin(); it != countrylist.end(); ++it ) {
		KSimpleConfig entry(*it);
		entry.setGroup(TQString::tqfromLatin1("KCM Locale"));
		TQString name = entry.readEntry(TQString::tqfromLatin1("Name"), i18n("without name"));
		TQString submenu = '-' + entry.readEntry("Region");

		TQString tag = *it;
		int index = tag.findRev('/');
		tag.truncate(index);
		index = tag.findRev('/');
		tag = tag.mid(index+1);

		TQPixmap flag( locate( "locale", TQString::tqfromLatin1("l10n/%1/flag.png").arg(tag) ) );
		TQIconSet icon( flag );
		combo->insertItem( icon, name, tag, submenu );
	}
}

void KCountryPage::fillLanguageMenu(KLanguageButton *combo) {
	combo->clear();
	TQString submenu; // we are working on this menu
	TQStringList langlist = flang->getLangList();
	TQMap<TQString,TQString> langmap = flang->getLangMap();
	TQStringList::ConstIterator it;
	for ( it = langlist.begin(); it != langlist.end(); ++it ) {
		if ((*it).isNull()) {
			combo->insertSeparator();
			submenu = TQString::tqfromLatin1("all");
			combo->insertSubmenu(i18n("All"), submenu, TQString::null);
			continue;
		}
		combo->insertItem(langmap[(*it)], (*it), submenu );
	}
}

/** No descriptions */
bool KCountryPage::save(KLanguageButton *comboCountry, KLanguageButton *comboLang) {
	kdDebug() << "KCountryPage::save()" << endl;
	KConfigBase *config = KGlobal::config();

	config->setGroup(TQString::tqfromLatin1("Locale"));
	config->writeEntry(TQString::tqfromLatin1("Country"), comboCountry->current(), true, true);
	config->writeEntry(TQString::tqfromLatin1("Language"), comboLang->current(), true, true);
	config->sync();

	// only make the system reload the language, if the selected one deferes from the old saved one.
	if (b_savedLanguageChanged) {
		// Tell kdesktop about the new language
		TQCString replyType; TQByteArray replyData;
		TQByteArray data, da;
		TQDataStream stream( data, IO_WriteOnly );
		stream << comboLang->current();
		if ( !kapp->dcopClient()->isAttached() )
			kapp->dcopClient()->attach();
		// ksycoca needs to be rebuilt
		KProcess proc;
		proc << TQString::tqfromLatin1("kbuildsycoca");
		proc.start(KProcess::DontCare);
		kdDebug() << "KLocaleConfig::save : sending signal to kdesktop" << endl;
		// inform kicker and kdeskop about the new language
		kapp->dcopClient()->send( "kicker", "Panel", "restart()", TQString::null);
		// call, not send, so that we know it's done before coming back
		// (we both access kdeglobals...)
		kapp->dcopClient()->call( "kdesktop", "KDesktopIface", "languageChanged(TQString)", data, replyType, replyData );
	}
	// KPersonalizer::next() probably waits for a return-value
	return true;
}

void KCountryPage::setLangForCountry(const TQString &country) {
	KSimpleConfig ent(locate("locale", "l10n/" + country + "/entry.desktop"), true);
	ent.setGroup(TQString::tqfromLatin1("KCM Locale"));
	langs = ent.readListEntry(TQString::tqfromLatin1("Languages"));

	TQString lang = TQString::tqfromLatin1("en_US");
	// use the first INSTALLED langauge in the list, or default to C
	for ( TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it ) {
		if (cb_language->contains(*it)) {
			lang = *it;
			break;
		}
    }

	cb_language->setCurrentItem(lang);
	setLanguageChanged();
}

void KCountryPage::setLanguageChanged() {
	// is the selcted language the same like the one in kdeglobals from before the start?
	b_savedLanguageChanged = (flang->getOldLang() != cb_language->current().lower());
	// is the selected language the same like the one we started kp with from main.cpp?
	b_startedLanguageChanged = (s_oldlocale != cb_language->current());
}


#include "kcountrypage.moc"