//
//   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 <tqdir.h>
#include <tqmessagebox.h>

#include <algorithm>
#include <filesystem>
#include <vector>

TQString g_szPrevSettedLocale;

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

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

	m_pTextEncodingCombo = new TQComboBox(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(KviTQString::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 TQComboBox(this);
	
	addWidgetToLayout(m_pForcedLocaleCombo,1,1,1,1);
	
	TQLabel * l = new TQLabel(__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"));
	
	TQString szLangFile=TQString("%1/.kvirc_force_locale").arg(TQDir::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();
	}
	
	TQString szLocaleDir;
	g_pApp->getGlobalKvircDirectory(szLocaleDir,KviApp::Locale);

	// Find available translations by checking for existance of kvirc.mo files
	namespace fs = std::filesystem;
	std::vector<fs::path> translationPaths;
	const fs::path localeDir { szLocaleDir.local8Bit().data() };
	for (const auto &dir : fs::directory_iterator(localeDir))
	{
		if (!dir.is_directory())
		{
			continue;
		}

		if (fs::exists(dir.path()/"LC_MESSAGES/kvirc.mo"))
		{
			translationPaths.push_back(dir.path().filename());
		}
	}
	std::sort(translationPaths.begin(), translationPaths.end());

	i = 0;
	iMatch = 0;
	for (const auto &filename : translationPaths)
	{
		TQString szTmp { filename.c_str() };
		m_pForcedLocaleCombo->insertItem(szTmp);
		if(KviTQString::equalCI(szTmp,m_szLanguage))
			iMatch = i + 2;
		i++;
	}
	if(bIsDefaultLocale)
		m_pForcedLocaleCombo->setCurrentItem(0);
	else if(KviTQString::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();
	TQString szLangFile=TQString("%1/.kvirc_force_locale").arg(TQDir::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)))
		{
			TQMessageBox::critical(this,"KVIrc",__tr2qs_ctx("Unable to write language information to","options")+"\n"+szLangFile,__tr2qs_ctx("Ok","options"));
		}
	}
/*	if(!KviTQString::equalCI(m_pForcedLocaleCombo->text(idx),m_szLanguage))
		TQMessageBox::information(0,"KVIrc",__tr2qs_ctx("You need to restart KVirc to apply a language changing","options"),TQMessageBox::Ok);*/
}

#include "optw_textencoding.moc"
