kswitchlanguagedialog.cpp
00001 /* 00002 * This file is part of the KDE Libraries 00003 * Copyright (C) 2007 Krzysztof Lichota (lichota@mimuw.edu.pl) 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #include "kswitchlanguagedialog.h" 00023 00024 #include <tqlayout.h> 00025 #include <tqtooltip.h> 00026 #include <tqlabel.h> 00027 #include <tqmap.h> 00028 00029 #include <klanguagebutton.h> 00030 #include <tdeconfig.h> 00031 #include <tdelocale.h> 00032 #include <tdemessagebox.h> 00033 #include <kdebug.h> 00034 #include <kpushbutton.h> 00035 00036 struct LanguageRowData 00037 { 00038 TQLabel *label; 00039 KLanguageButton *languageButton; 00040 KPushButton *removeButton; 00041 00042 void setRowWidgets( 00043 TQLabel *label, 00044 KLanguageButton *languageButton, 00045 KPushButton *removeButton 00046 ) 00047 { 00048 this->label = label; 00049 this->languageButton = languageButton; 00050 this->removeButton = removeButton; 00051 } 00052 00053 }; 00054 00055 class KSwitchLanguageDialogPrivate 00056 { 00057 public: 00058 KSwitchLanguageDialogPrivate(KSwitchLanguageDialog *parent); 00059 00060 KSwitchLanguageDialog *p; //parent class 00061 00065 void fillApplicationLanguages(KLanguageButton *button); 00066 00070 void addLanguageButton(const TQString & languageCode, bool primaryLanguage); 00071 00075 TQStringList applicationLanguageList(); 00076 00077 TQMap<KPushButton*, LanguageRowData> languageRows; 00078 TQPtrList<KLanguageButton> languageButtons; 00079 TQGridLayout *languagesLayout; 00080 TQWidget *page; 00081 }; 00082 00083 /*************************** KSwitchLanguageDialog **************************/ 00084 00085 KSwitchLanguageDialog::KSwitchLanguageDialog( 00086 TQWidget *parent, 00087 const char *name, 00088 bool modal 00089 ): 00090 KDialogBase(parent, name, modal, i18n("Switch application language"), Ok|Cancel, Ok, true ), 00091 d(new KSwitchLanguageDialogPrivate(this)) 00092 { 00093 d->page = new TQWidget( this ); 00094 setMainWidget(d->page); 00095 TQVBoxLayout *topLayout = new TQVBoxLayout( d->page, 0, spacingHint() ); 00096 TQLabel *label = new TQLabel( i18n("Please choose language which should be used for this application"), d->page, "label1" ); 00097 topLayout->addWidget( label ); 00098 00099 TQHBoxLayout *languageHorizontalLayout = new TQHBoxLayout(); 00100 topLayout->addLayout(languageHorizontalLayout); 00101 00102 d->languagesLayout = new TQGridLayout(0 , 2); 00103 languageHorizontalLayout->addLayout(TQT_TQLAYOUT(d->languagesLayout)); 00104 languageHorizontalLayout->addStretch(); 00105 00106 TQStringList defaultLanguages = d->applicationLanguageList(); 00107 00108 for ( TQStringList::ConstIterator it = defaultLanguages.begin(); it != defaultLanguages.end(); ++it ) 00109 { 00110 TQString language = *it; 00111 bool primaryLanguage = (it == defaultLanguages.begin()); 00112 d->addLanguageButton(language, primaryLanguage); 00113 } 00114 00115 if (defaultLanguages.count() == 0) 00116 { 00117 d->addLanguageButton(TDEGlobal::locale()->defaultLanguage(), true); 00118 } 00119 00120 TQHBoxLayout *addButtonHorizontalLayout = new TQHBoxLayout(); 00121 topLayout->addLayout(addButtonHorizontalLayout); 00122 00123 KPushButton *addLangButton = new KPushButton(i18n("Add fallback language"), d->page, "addLangButton"); 00124 TQToolTip::add(addLangButton, i18n("Adds one more language which will be used if other translations do not contain proper translation")); 00125 connect(addLangButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddLanguageButton())); 00126 addButtonHorizontalLayout->addWidget(addLangButton); 00127 addButtonHorizontalLayout->addStretch(); 00128 00129 topLayout->addStretch(10); 00130 } 00131 00132 KSwitchLanguageDialog::~KSwitchLanguageDialog() 00133 { 00134 delete this->d; 00135 } 00136 00137 void KSwitchLanguageDialog::slotAddLanguageButton() 00138 { 00139 //adding new button with en_US as it should always be present 00140 d->addLanguageButton("en_US", d->languageButtons.isEmpty() ? true : false); 00141 } 00142 00143 void KSwitchLanguageDialog::removeButtonClicked() 00144 { 00145 TQObject const *signalSender = TQT_TQOBJECT_CONST(sender()); 00146 00147 if (signalSender == NULL) 00148 { 00149 kdError() << "KSwitchLanguageDialog::removeButtonClicked() called directly, not using signal"; 00150 return; 00151 } 00152 00153 KPushButton *removeButton = const_cast<KPushButton*>(::tqqt_cast<const KPushButton*>(signalSender)); 00154 00155 if (removeButton == NULL) 00156 { 00157 kdError() << "KSwitchLanguageDialog::removeButtonClicked() called from something else than KPushButton"; 00158 return; 00159 } 00160 00161 TQMap<KPushButton *, LanguageRowData>::iterator it = d->languageRows.find(removeButton); 00162 00163 if (it == d->languageRows.end()) 00164 { 00165 kdError() << "KSwitchLanguageDialog::removeButtonClicked called from unknown KPushButton"; 00166 return; 00167 } 00168 00169 LanguageRowData languageRowData = it.data(); 00170 00171 d->languageButtons.removeRef(languageRowData.languageButton); 00172 00173 languageRowData.label->deleteLater(); 00174 languageRowData.languageButton->deleteLater(); 00175 languageRowData.removeButton->deleteLater(); 00176 d->languageRows.erase(it); 00177 } 00178 00179 void KSwitchLanguageDialog::languageOnButtonChanged(const TQString & languageCode) 00180 { 00181 for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it ) 00182 { 00183 KLanguageButton *languageButton = *it; 00184 if (languageButton->current() == languageCode) 00185 { 00186 //update all buttons which have matching id 00187 //might update buttons which were not changed, but well... 00188 languageButton->setText(TDEGlobal::locale()->twoAlphaToLanguageName(languageCode)); 00189 } 00190 } 00191 } 00192 00193 void KSwitchLanguageDialog::slotOk() 00194 { 00195 TQString languageString; 00196 bool first = true; 00197 00198 for ( TQPtrList<KLanguageButton>::ConstIterator it = d->languageButtons.begin(); it != d->languageButtons.end(); ++it ) 00199 { 00200 KLanguageButton *languageButton = *it; 00201 00202 if (first == false) 00203 { 00204 languageString += ':'; 00205 } 00206 languageString += languageButton->current(); 00207 first = false; 00208 } 00209 00210 TDEConfig *config = TDEGlobal::config(); 00211 00212 if (d->applicationLanguageList().join(":") != languageString) 00213 { 00214 //list is different from defaults or saved languages list 00215 TDEConfigGroup group(config, "Locale"); 00216 00217 group.writeEntry("Language", languageString); 00218 config->sync(); 00219 00220 KMessageBox::information( 00221 this, 00222 i18n("Language for this application has been changed. The change will take effect upon next start of application"), //text 00223 i18n("Application language changed"), //caption 00224 "ApplicationLanguageChangedWarning" //dontShowAgainName 00225 ); 00226 } 00227 00228 emit okClicked(); 00229 accept(); 00230 } 00231 00232 /************************ KSwitchLanguageDialogPrivate ***********************/ 00233 00234 KSwitchLanguageDialogPrivate::KSwitchLanguageDialogPrivate( 00235 KSwitchLanguageDialog *parent 00236 ): 00237 p(parent) 00238 { 00239 //NOTE: do NOT use "p" in constructor, it is not fully constructed 00240 } 00241 00242 void KSwitchLanguageDialogPrivate::fillApplicationLanguages(KLanguageButton *button) 00243 { 00244 TDELocale *locale = TDEGlobal::locale(); 00245 TQStringList allLanguages = locale->allLanguagesTwoAlpha(); 00246 for ( TQStringList::ConstIterator it = allLanguages.begin(); it != allLanguages.end(); ++it ) 00247 { 00248 TQString languageCode = *it; 00249 if (locale->isApplicationTranslatedInto(languageCode)) 00250 { 00251 button->insertItem( 00252 locale->twoAlphaToLanguageName(languageCode), 00253 languageCode 00254 ); 00255 } 00256 } 00257 } 00258 00259 TQStringList KSwitchLanguageDialogPrivate::applicationLanguageList() 00260 { 00261 TDEConfig *config = TDEGlobal::config(); 00262 TQStringList languagesList; 00263 00264 if (config->hasGroup("Locale")) 00265 { 00266 TDEConfigGroupSaver saver(config, "Locale"); 00267 00268 if (config->hasKey("Language")) 00269 { 00270 languagesList = config->readListEntry("Language", ':'); 00271 } 00272 } 00273 if (languagesList.empty()) 00274 { 00275 languagesList = TDEGlobal::locale()->languageList(); 00276 } 00277 return languagesList; 00278 } 00279 00280 void KSwitchLanguageDialogPrivate::addLanguageButton(const TQString & languageCode, bool primaryLanguage) 00281 { 00282 TQString labelText = primaryLanguage ? i18n("Primary language:") : i18n("Fallback language:"); 00283 00284 KLanguageButton *languageButton = new KLanguageButton(page); 00285 00286 languageButton->setText(TDEGlobal::locale()->twoAlphaToLanguageName(languageCode)); 00287 00288 fillApplicationLanguages(languageButton); 00289 00290 languageButton->setCurrentItem(languageCode); 00291 00292 TQObject::connect( 00293 languageButton, 00294 TQT_SIGNAL(activated( const TQString &)), 00295 p, 00296 TQT_SLOT(languageOnButtonChanged(const TQString &)) 00297 ); 00298 00299 LanguageRowData languageRowData; 00300 KPushButton *removeButton = NULL; 00301 00302 if (primaryLanguage == false) 00303 { 00304 removeButton = new KPushButton(i18n("Remove"), page); 00305 00306 TQObject::connect( 00307 removeButton, 00308 TQT_SIGNAL(clicked()), 00309 p, 00310 TQT_SLOT(removeButtonClicked()) 00311 ); 00312 } 00313 00314 if (primaryLanguage) 00315 { 00316 TQToolTip::add(languageButton, i18n("This is main application language which will be used first before any other languages")); 00317 } 00318 else 00319 { 00320 TQToolTip::add(languageButton, i18n("This is language which will be used if any previous languages does not contain proper translation")); 00321 } 00322 00323 int numRows = languagesLayout->numRows(); 00324 00325 TQLabel *languageLabel = new TQLabel(labelText, page); 00326 languagesLayout->addWidget( languageLabel, numRows + 1, 1, (TQ_Alignment)TQt::AlignAuto ); 00327 languagesLayout->addWidget( languageButton, numRows + 1, 2, (TQ_Alignment)TQt::AlignAuto ); 00328 00329 if (primaryLanguage == false) 00330 { 00331 languagesLayout->addWidget( removeButton, numRows + 1, 3, (TQ_Alignment)TQt::AlignAuto ); 00332 00333 languageRowData.setRowWidgets( 00334 languageLabel, 00335 languageButton, 00336 removeButton 00337 ); 00338 removeButton->show(); 00339 } 00340 00341 languageRows.insert(removeButton, languageRowData); 00342 00343 languageButtons.append(languageButton); 00344 languageButton->show(); 00345 languageLabel->show(); 00346 } 00347 00348 #include "kswitchlanguagedialog.moc" 00349