/*
	@doc: theme.apply
	@type:
		command
	@title:
		theme.apply
	@short:
		Apply a theme.
	@syntax:
		theme.apply <package_name:string>
	@description:
		Attempts to apply the theme specified by <package_name>.
*/
static bool theme_kvs_cmd_apply(KviKvsModuleCommandCall * c)
{
	QString szThemePackFile;

	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("package_name",KVS_PT_STRING,0,szThemePackFile)
	KVSM_PARAMETERS_END(c)
	KviThemeInfo * themeInfo = new KviThemeInfo();
	if(themeInfo->load(szThemePackFile))
	{
		themeInfo->setSubdirectory(szThemePackFile);
		if(KviMessageBox::yesNo(__tr2qs_ctx("Apply theme - KVIrc","theme"),
		__tr2qs_ctx("Do you wish to apply theme \"%Q\" (version %Q)?","theme"),
		&(themeInfo->name()),&(themeInfo->version())))
		{
			KviThemeInfo out;
			if(!KviTheme::load(szThemePackFile,out))
			{
				QString szErr = out.lastError();
				QString szMsg = QString(__tr2qs_ctx("Failed to apply the specified theme: %1","theme")).arg(szErr);
				QMessageBox::critical(0,__tr2qs_ctx("Apply theme - KVIrc","theme"),szMsg,
				QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
			}
			return true;
		}
	}
	c->warning(__tr2qs_ctx("The theme package '%Q' does not exist","theme"),&szThemePackFile);
	return true;
}
bool SaveThemeDialog::saveTheme()
{
	m_pImageSelector->commit();

	KviThemeInfo sto;
	sto.setName(m_pThemeNameEdit->text());
	if(sto.name().isEmpty())
	{
		QMessageBox::critical(this,__tr2qs_ctx("Save Current Theme - KVIrc","theme"),__tr2qs_ctx("You must choose a theme name!","theme"),QMessageBox::Ok,
			QMessageBox::NoButton,QMessageBox::NoButton);
		return false;
	}

	sto.setAuthor(m_pAuthorNameEdit->text());
	sto.setDescription(m_pThemeDescriptionEdit->toPlainText());

	QString szTmp;
	QDateTime date = QDateTime::currentDateTime();
	switch(KVI_OPTION_UINT(KviOption_uintOutputDatetimeFormat))
	{
		case 0:
			// this is the equivalent to an empty date.toString() call, but it's needed
			// to ensure qt4 will use the default() locale and not the system() one
			szTmp = QLocale().toString(date, "ddd MMM d hh:mm:ss yyyy");
			break;
		case 1:
			szTmp = date.toString(Qt::ISODate);
			break;
		case 2:
			szTmp = date.toString(Qt::SystemLocaleShortDate);
			break;
	}

	sto.setDate(szTmp);
	sto.setVersion(m_pThemeVersionEdit->text());
	sto.setApplication("KVIrc " KVI_VERSION "." KVI_SOURCES_DATE);

	if(sto.version().isEmpty())sto.setVersion("1.0.0");

	QString szSubdir = sto.name() + QString("-") + sto.version();
	szSubdir.replace(QRegExp("[ \\\\/:][ \\\\/:]*"),"_");
	sto.setSubdirectory(szSubdir);

	QString szAbsDir;
	g_pApp->getLocalKvircDirectory(szAbsDir,KviApplication::Themes,sto.subdirectory(),true);
	if(!KviFileUtils::makeDir(szAbsDir))
	{
		QMessageBox::critical(this,__tr2qs_ctx("Save Current Theme - KVIrc","theme"),__tr2qs_ctx("Unable to create theme directory.","theme"),
			QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
		return false;
	}


	if(!KviTheme::save(sto))
	{
		QString szErr = sto.lastError();
		QString szMsg2 = QString(__tr2qs_ctx("Unable to save theme: %1","theme")).arg(szErr);
		QMessageBox::critical(this,__tr2qs_ctx("Save Current Theme - KVIrc","theme"),szMsg2,
			QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
		return false;
	}
	// write down the screenshot, if needed

	if(!m_szScreenshotPath.isEmpty())
	{
		if(!KviTheme::saveScreenshots(sto,m_szScreenshotPath))
		{
			QMessageBox::critical(this,__tr2qs_ctx("Save Current Theme - KVIrc","theme"),__tr2qs_ctx("Failed to load the selected screenshot image: please fix it","theme"),
				QMessageBox::Ok,QMessageBox::NoButton,QMessageBox::NoButton);
			setCurrentPage(m_pImageSelectionPage);
			return false;
		}
	}

	QString szMsg = __tr2qs_ctx("Theme saved successfully to %1","theme").arg(szAbsDir);

	QMessageBox::information(this,__tr2qs_ctx("Save Theme - KVIrc","theme"),szMsg,QMessageBox::Ok,
		QMessageBox::NoButton,QMessageBox::NoButton);

	return true;
}