summaryrefslogtreecommitdiffstats
path: root/kcontrol/locale/toplevel.cpp
blob: 4ba70e1ed63f658c66e828855b400ab0418d6930 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
  toplevel.cpp - A KControl Application

  Copyright 1998 Matthias Hoelzer
  Copyright 1999-2003 Hans Petter Bieker <[email protected]>

  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.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  */

#include <qcheckbox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qobjectlist.h>
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <qvgroupbox.h>

#include <kaboutdata.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kmessagebox.h>
#include <kprocess.h>
#include <kservice.h>

#include "localenum.h"
#include "localemon.h"
#include "localetime.h"
#include "localeother.h"
#include "klocalesample.h"
#include "toplevel.h"
#include "kcmlocale.h"
#include "toplevel.moc"

KLocaleApplication::KLocaleApplication(QWidget *parent, const char* /*name*/, 
                                       const QStringList &args)
  : KCModule( KLocaleFactory::instance(), parent, args)
{
  KAboutData* aboutData = new KAboutData("kcmlocale",
        I18N_NOOP("KCMLocale"),
        "3.0",
        I18N_NOOP("Regional settings"),
        KAboutData::License_GPL,
        "(C) 1998 Matthias Hoelzer, "
        "(C) 1999-2003 Hans Petter Bieker",
        0, 0, "[email protected]");
  setAboutData( aboutData );

  m_nullConfig = new KConfig(QString::null, false, false);
  m_globalConfig = new KConfig(QString::null, false, true);

  m_locale = new KLocale(QString::fromLatin1("kcmlocale"), m_nullConfig);
  QVBoxLayout *l = new QVBoxLayout(this, 0, KDialog::spacingHint());
  l->setAutoAdd(TRUE);

  m_tab = new QTabWidget(this);

  m_localemain = new KLocaleConfig(m_locale, this);
  m_tab->addTab( m_localemain, QString::null);
  m_localenum = new KLocaleConfigNumber(m_locale, this);
  m_tab->addTab( m_localenum, QString::null );
  m_localemon = new KLocaleConfigMoney(m_locale, this);
  m_tab->addTab( m_localemon, QString::null );
  m_localetime = new KLocaleConfigTime(m_locale, this);
  m_tab->addTab( m_localetime, QString::null );
  m_localeother = new KLocaleConfigOther(m_locale, this);
  m_tab->addTab( m_localeother, QString::null );

  // Examples
  m_gbox = new QVGroupBox(this);
  m_sample = new KLocaleSample(m_locale, m_gbox);

  // getting signals from childs
  connect(m_localemain, SIGNAL(localeChanged()),
          this, SIGNAL(localeChanged()));
  connect(m_localemain, SIGNAL(languageChanged()),
          this, SIGNAL(languageChanged()));

  // run the slots on the childs
  connect(this, SIGNAL(localeChanged()),
          m_localemain, SLOT(slotLocaleChanged()));
  connect(this, SIGNAL(localeChanged()),
          m_localenum, SLOT(slotLocaleChanged()));
  connect(this, SIGNAL(localeChanged()),
          m_localemon, SLOT(slotLocaleChanged()));
  connect(this, SIGNAL(localeChanged()),
          m_localetime, SLOT(slotLocaleChanged()));
  connect(this, SIGNAL(localeChanged()),
          m_localeother, SLOT(slotLocaleChanged()));

  // keep the example up to date
  // NOTE: this will make the sample be updated 6 times the first time
  // because combo boxes++ emits the change signal not only when the user changes
  // it, but also when it's changed by the program.
  connect(m_localenum, SIGNAL(localeChanged()),
          m_sample, SLOT(slotLocaleChanged()));
  connect(m_localemon, SIGNAL(localeChanged()),
          m_sample, SLOT(slotLocaleChanged()));
  connect(m_localetime, SIGNAL(localeChanged()),
          m_sample, SLOT(slotLocaleChanged()));
  // No examples for this yet
  //connect(m_localeother, SIGNAL(slotLocaleChanged()),
  //m_sample, SLOT(slotLocaleChanged()));
  connect(this, SIGNAL(localeChanged()),
          m_sample, SLOT(slotLocaleChanged()));

  // make sure we always have translated interface
  connect(this, SIGNAL(languageChanged()),
          this, SLOT(slotTranslate()));
  connect(this, SIGNAL(languageChanged()),
          m_localemain, SLOT(slotTranslate()));
  connect(this, SIGNAL(languageChanged()),
          m_localenum, SLOT(slotTranslate()));
  connect(this, SIGNAL(languageChanged()),
          m_localemon, SLOT(slotTranslate()));
  connect(this, SIGNAL(languageChanged()),
          m_localetime, SLOT(slotTranslate()));
  connect(this, SIGNAL(languageChanged()),
          m_localeother, SLOT(slotTranslate()));

  // mark it as changed when we change it.
  connect(m_localemain, SIGNAL(localeChanged()),
          SLOT(slotChanged()));
  connect(m_localenum, SIGNAL(localeChanged()),
          SLOT(slotChanged()));
  connect(m_localemon, SIGNAL(localeChanged()),
          SLOT(slotChanged()));
  connect(m_localetime, SIGNAL(localeChanged()),
          SLOT(slotChanged()));
  connect(m_localeother, SIGNAL(localeChanged()),
          SLOT(slotChanged()));

  load();
}

KLocaleApplication::~KLocaleApplication()
{
  delete m_locale;
  delete m_globalConfig;
  delete m_nullConfig;
}

void KLocaleApplication::load()
{
	load( false );
}

void KLocaleApplication::load( bool useDefaults )
{
	m_globalConfig->setReadDefaults( useDefaults );
	m_globalConfig->reparseConfiguration();
	*m_locale = KLocale(QString::fromLatin1("kcmlocale"), m_globalConfig);
	
	emit localeChanged();
	emit languageChanged();
	emit changed(useDefaults);
}

void KLocaleApplication::save()
{
  // temperary use of our locale as the global locale
  KLocale *lsave = KGlobal::_locale;
  KGlobal::_locale = m_locale;
  KMessageBox::information(this, m_locale->translate
                           ("Changed language settings apply only to "
                            "newly started applications.\nTo change the "
                            "language of all programs, you will have to "
                            "logout first."),
                           m_locale->translate("Applying Language Settings"),
                           QString::fromLatin1("LanguageChangesApplyOnlyToNewlyStartedPrograms"));
  // restore the old global locale
  KGlobal::_locale = lsave;

  KConfig *config = KGlobal::config();
  KConfigGroupSaver saver(config, "Locale");

  // ##### this doesn't make sense
  bool langChanged = config->readEntry("Language")
    != m_locale->language();

  m_localemain->save();
  m_localenum->save();
  m_localemon->save();
  m_localetime->save();
  m_localeother->save();

  // rebuild the date base if language was changed
  if (langChanged)
  {
    KService::rebuildKSycoca(this);
  }

  emit changed(false);
}

void KLocaleApplication::defaults()
{
	load( true );
}

QString KLocaleApplication::quickHelp() const
{
  return m_locale->translate("<h1>Country/Region & Language</h1>\n"
          "<p>From here you can configure language, numeric, and time \n"
          "settings for your particular region. In most cases it will be \n"
          "sufficient to choose the country you live in. For instance KDE \n"
          "will automatically choose \"German\" as language if you choose \n"
          "\"Germany\" from the list. It will also change the time format \n"
          "to use 24 hours and and use comma as decimal separator.</p>\n");
}

void KLocaleApplication::slotTranslate()
{
  // The untranslated string for QLabel are stored in
  // the name() so we use that when retranslating
  QObject *wc;
  QObjectList *list = queryList("QWidget");
  QObjectListIt it(*list);
  while ( (wc = it.current()) != 0 )
  {
    ++it;

    // unnamed labels will cause errors and should not be
    // retranslated. E.g. the example box should not be
    // retranslated from here.
    if (wc->name() == 0)
      continue;
    if (::qstrcmp(wc->name(), "") == 0)
      continue;
    if (::qstrcmp(wc->name(), "unnamed") == 0)
      continue;

    if (::qstrcmp(wc->className(), "QLabel") == 0)
      ((QLabel *)wc)->setText( m_locale->translate( wc->name() ) );
    else if (::qstrcmp(wc->className(), "QGroupBox") == 0 ||
             ::qstrcmp(wc->className(), "QVGroupBox") == 0)
      ((QGroupBox *)wc)->setTitle( m_locale->translate( wc->name() ) );
    else if (::qstrcmp(wc->className(), "QPushButton") == 0 ||
             ::qstrcmp(wc->className(), "KMenuButton") == 0)
      ((QPushButton *)wc)->setText( m_locale->translate( wc->name() ) );
    else if (::qstrcmp(wc->className(), "QCheckBox") == 0)
      ((QCheckBox *)wc)->setText( m_locale->translate( wc->name() ) );
  }
  delete list;

  // Here we have the pointer
  m_gbox->setCaption(m_locale->translate("Examples"));
  m_tab->changeTab(m_localemain, m_locale->translate("&Locale"));
  m_tab->changeTab(m_localenum, m_locale->translate("&Numbers"));
  m_tab->changeTab(m_localemon, m_locale->translate("&Money"));
  m_tab->changeTab(m_localetime, m_locale->translate("&Time && Dates"));
  m_tab->changeTab(m_localeother, m_locale->translate("&Other"));

  // FIXME: All widgets are done now. However, there are
  // still some problems. Popup menus from the QLabels are
  // not retranslated.
}

void KLocaleApplication::slotChanged()
{
  emit changed(true);
}