/**
 * @file MainSetting.cpp
 * @brief 主窗口配置信息管理类源(.cpp)文件
 * @author FastCAE研发小组(fastcae@diso.cn)
 * @version 2.5.0
 * @date 2022-02-13 17:33
 * @copyright Copyright (c) Since 2020 青岛数智船海科技有限公司  All rights reserved.
 *
 * ============================================================================
 * Program:   FastCAE
 *
 * Copyright (c) Since 2020 青岛数智船海科技有限公司  All rights reserved.
 * See License or http://www.fastcae.com/ for details.
 *
 * BSD 3-Clause License
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.
 * ==================================================================================
 */
#include "MainSetting.h"
#include <QSettings>
#include <QDir>
#include <QCoreApplication>

namespace Setting
{
	void MainSetting::writeINI(QSettings *setting)
	{
		setting->setValue("/Path/WorkingDir", _workingDir);
		setting->setValue("/Language/Language", _language);
		setting->setValue("/Recent/RecentFile", _recentFiles);
		setting->setValue("/License/Path", _licensePath);
		setting->setValue("/Plugins/Name", _plugins);
		setting->setValue("/User/GUidance", _showUserGuidance);
		setting->setValue("/Style/Ribbon", _useRibbon);
	}
	void MainSetting::readINI(QSettings *setting)
	{
		_workingDir = setting->value("/Path/WorkingDir").toString();
		_language = setting->value("/Language/Language").toString();
		_recentFiles = setting->value("/Recent/RecentFile").toStringList();
		_licensePath = setting->value("/License/Path").toString();
		_plugins = setting->value("/Plugins/Name").toStringList();
		_showUserGuidance = setting->value("/User/GUidance").toBool();
		_useRibbon = setting->value("/Style/Ribbon").toBool();

		QDir dir(_workingDir);

		if (!dir.exists())
		{
			QString path = qApp->applicationDirPath() + "/../WorkingDir/";
			QDir dir(path);
			if (!dir.exists())
			{
				dir.mkpath(path);
			}
			_workingDir = path;
		}
	}
	void MainSetting::setWorkingDir(const QString &dir)
	{
		QDir d(dir);
		if (!d.exists(dir))
		{
			QString path = qApp->applicationDirPath() + "/../WorkingDir/";
			QDir dirs(path);
			if (!dirs.exists())
			{
				dirs.mkpath(path);
			}
			_workingDir = path;
		}
		else
			_workingDir = dir;
	}
	QString MainSetting::getWorkingDir() const
	{
		return _workingDir;
	}
	void MainSetting::setLanguage(const QString &lang)
	{
		_language = lang;
	}
	QString MainSetting::getLanguage()
	{
		return _language;
	}
	void MainSetting::appendRecentFile(QString f)
	{
		if (_recentFiles.contains(f))
		{
			_recentFiles.removeOne(f);
			_recentFiles.insert(0, f);
		}
		else
		{
			_recentFiles.insert(0, f);
		}
		while (_recentFiles.size() > 5)
		{
			_recentFiles.removeLast();
		}
	}
	QStringList MainSetting::getRencentFile()
	{
		return _recentFiles;
	}

	void MainSetting::isShowUserGuidance(bool s)
	{
		_showUserGuidance = s;
	}

	bool MainSetting::isShowUserGuidance()
	{
		return _showUserGuidance;
	}

	void MainSetting::isUseRibbon(bool on)
	{
		_useRibbon = on;
	}

	bool MainSetting::isUseRibbon()
	{
		return _useRibbon;
	}

	void MainSetting::setLicensePath(const QString &path)
	{
		_licensePath = path;
	}

	QString MainSetting::getLicensePath() const
	{
		return _licensePath;
	}

	void MainSetting::setPlugins(QStringList ps)
	{
		_plugins = ps;
	}

	QStringList MainSetting::getPlugins()
	{
		return _plugins;
	}

}