summaryrefslogtreecommitdiffstats
path: root/src/modules/options/optw_textencoding.cpp
blob: 431cdad0727d5b638c89b3c016eae71d02d1828a (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
//
//   File : optw_textencoding.cpp
//   Creation date : Sat Aug 11 2001 03:29:52 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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 "optw_textencoding.h"

#include "kvi_options.h"
#include "kvi_locale.h"
#include "kvi_qstring.h"
#include "kvi_app.h"
#include "kvi_fileutils.h"

#include <qdir.h>
#include <qmessagebox.h>

QString g_szPrevSettedLocale;

KviTextEncodingOptionsWidget::KviTextEncodingOptionsWidget(QWidget * parent)
: KviOptionsWidget(parent,"textencoding_options_widget")
{
	createLayout(5,2);

	addLabel(0,0,0,0,__tr2qs_ctx("Default text encoding:","options"));

	m_pTextEncodingCombo = new QComboBox(this);
	addWidgetToLayout(m_pTextEncodingCombo,1,0,1,0);

	m_pTextEncodingCombo->insertItem(__tr2qs_ctx("Use Language Encoding","options"));
	int i = 0;
	int iMatch = 0;
	KviLocale::EncodingDescription * d = KviLocale::encodingDescription(i);
	while(d->szName)
	{
		if(KviQString::equalCI(d->szName,KVI_OPTION_STRING(KviOption_stringDefaultTextEncoding)))iMatch = i + 1;
		m_pTextEncodingCombo->insertItem(d->szName);
		i++;
		d = KviLocale::encodingDescription(i);
	}

	m_pTextEncodingCombo->setCurrentItem(iMatch);
	
 	addLabel(0,1,0,1,__tr2qs_ctx("Force language:","options"));
	
	m_pForcedLocaleCombo = new QComboBox(this);
	
	addWidgetToLayout(m_pForcedLocaleCombo,1,1,1,1);
	
	QLabel * l = new QLabel(__tr2qs_ctx("<b>Note:</b> You need to restart KVirc to apply a language changing","options"),this);
	addWidgetToLayout(l,0,2,1,2);
	
	m_pForcedLocaleCombo->insertItem(__tr2qs_ctx("Automatic detection","options"));
	m_pForcedLocaleCombo->insertItem(__tr2qs_ctx("en","options"));
	
	QString szLangFile=QString("%1/.kvirc_force_locale").arg(QDir::homeDirPath());

	bool bIsDefaultLocale = !KviFileUtils::fileExists(szLangFile);
	//We Have setted locale, but not restarted kvirc
	if(!g_szPrevSettedLocale.isEmpty())
	{
		m_szLanguage=g_szPrevSettedLocale;
	} else {
		m_szLanguage=KviLocale::localeName();
	}
	
	QString szLocaleDir;
	g_pApp->getGlobalKvircDirectory(szLocaleDir,KviApp::Locale);

	QStringList list=QDir(szLocaleDir).entryList("kvirc_*.mo",QDir::Files);
	
	i = 0;
	iMatch = 0;
	
	for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
		QString szTmp=*it;
		szTmp.replace("kvirc_","");
		szTmp.replace(".mo","");
		m_pForcedLocaleCombo->insertItem(szTmp);
		if(KviQString::equalCI(szTmp,m_szLanguage))
			iMatch = i + 2;
		i++;
	}
	if(bIsDefaultLocale)
		m_pForcedLocaleCombo->setCurrentItem(0);
	else if(KviQString::equalCI(m_szLanguage,"en"))
		m_pForcedLocaleCombo->setCurrentItem(1);
	else
		m_pForcedLocaleCombo->setCurrentItem(iMatch);
	addRowSpacer(0,3,1,3);
}

KviTextEncodingOptionsWidget::~KviTextEncodingOptionsWidget()
{
}

void KviTextEncodingOptionsWidget::commit()
{
	int idx = m_pTextEncodingCombo->currentItem();
	if(idx <= 0)
	{
		// guess from locale
		KVI_OPTION_STRING(KviOption_stringDefaultTextEncoding) = "";
	} else {
		KVI_OPTION_STRING(KviOption_stringDefaultTextEncoding) = m_pTextEncodingCombo->text(idx);
	}
	
	idx=m_pForcedLocaleCombo->currentItem();
	QString szLangFile=QString("%1/.kvirc_force_locale").arg(QDir::homeDirPath());
	if(idx==0) {
		if(KviFileUtils::fileExists(szLangFile))
			KviFileUtils::removeFile(szLangFile);
	} else {
		g_szPrevSettedLocale=m_pForcedLocaleCombo->text(idx);
		if(!KviFileUtils::writeFile(szLangFile,m_pForcedLocaleCombo->text(idx)))
		{
			QMessageBox::critical(this,"KVIrc",__tr2qs_ctx("Unable to write language information to","options")+"\n"+szLangFile,__tr2qs_ctx("Ok","options"));
		}
	}
/*	if(!KviQString::equalCI(m_pForcedLocaleCombo->text(idx),m_szLanguage))
		QMessageBox::information(0,"KVIrc",__tr2qs_ctx("You need to restart KVirc to apply a language changing","options"),QMessageBox::Ok);*/
}

#include "m_optw_textencoding.moc"