barrier/gui/src/AppConfig.cpp

142 lines
3.8 KiB
C++
Raw Normal View History

2010-06-20 17:38:51 +00:00
/*
* synergy -- mouse and keyboard sharing utility
2010-06-20 17:38:51 +00:00
* Copyright (C) 2008 Volker Lanz (vl@fidra.de)
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package 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, see <http://www.gnu.org/licenses/>.
*/
2009-10-26 07:57:53 +00:00
#include "AppConfig.h"
#include <QtCore>
#include <QtNetwork>
#if defined(Q_OS_WIN)
const char AppConfig::m_SynergysName[] = "synergys.exe";
const char AppConfig::m_SynergycName[] = "synergyc.exe";
2010-06-11 21:36:11 +00:00
const char AppConfig::m_SynergyProgramDir[] = "bin/";
2010-06-12 17:20:54 +00:00
const char AppConfig::m_SynergyLogDir[] = "log/";
2009-10-26 07:57:53 +00:00
#else
const char AppConfig::m_SynergysName[] = "synergys";
const char AppConfig::m_SynergycName[] = "synergyc";
const char AppConfig::m_SynergyProgramDir[] = "/usr/bin/";
2010-06-12 17:20:54 +00:00
const char AppConfig::m_SynergyLogDir[] = "/var/log/";
2009-10-26 07:57:53 +00:00
#endif
2010-06-12 17:20:54 +00:00
static const char* logLevelNames[] =
{
"ERROR",
"WARNING",
"NOTE",
"INFO",
"DEBUG",
"DEBUG1",
"DEBUG2"
};
2009-10-26 07:57:53 +00:00
AppConfig::AppConfig(QSettings* settings) :
m_pSettings(settings),
m_AutoConnect(false),
m_Synergyc(),
m_Synergys(),
m_ScreenName(),
m_Port(24800),
m_Interface(),
m_LogLevel(0)
{
Q_ASSERT(m_pSettings);
loadSettings();
}
AppConfig::~AppConfig()
{
saveSettings();
}
2010-06-12 17:20:54 +00:00
QString AppConfig::synergyLogDir()
{
#if defined(Q_OS_WIN)
// on windows, we want to log to program files
return QString(QDir::currentPath() + "/log/");
#else
// on unix, we'll log to the standard log dir
return "/var/log/";
#endif
}
void AppConfig::persistLogDir()
{
QDir dir = synergyLogDir();
// persist the log directory
if (!dir.exists())
{
dir.mkpath(dir.path());
}
}
QString AppConfig::logLevelText() const
{
return logLevelNames[logLevel()];
}
2009-10-26 07:57:53 +00:00
void AppConfig::loadSettings()
{
m_AutoConnect = settings().value("autoConnectChecked", false).toBool();
m_Synergyc = settings().value("synergyc", QString(synergyProgramDir()) + synergycName()).toString();
m_Synergys = settings().value("synergys", QString(synergyProgramDir()) + synergysName()).toString();
m_ScreenName = settings().value("screenName", QHostInfo::localHostName()).toString();
m_Port = settings().value("port", 24800).toInt();
m_Interface = settings().value("interface").toString();
2010-06-12 17:20:54 +00:00
m_LogLevel = settings().value("logLevel", 2).toInt();
2010-06-11 21:36:11 +00:00
m_AutoDetectPaths = settings().value("autoDetectPaths", true).toBool();
2010-06-12 18:32:25 +00:00
m_LogToFile = settings().value("logToFile", false).toBool();
2010-06-12 17:20:54 +00:00
m_LogFilename = settings().value("logFilename", synergyLogDir() + "synergy.log").toString();
2009-10-26 07:57:53 +00:00
}
void AppConfig::saveSettings()
{
settings().setValue("autoConnectChecked", m_AutoConnect);
settings().setValue("synergyc", m_Synergyc);
settings().setValue("synergys", m_Synergys);
settings().setValue("screenName", m_ScreenName);
settings().setValue("port", m_Port);
settings().setValue("interface", m_Interface);
2010-06-12 17:20:54 +00:00
settings().setValue("logLevel", m_LogLevel);
2010-06-11 21:36:11 +00:00
settings().setValue("autoDetectPaths", m_AutoDetectPaths);
2010-06-12 17:20:54 +00:00
settings().setValue("logToFile", m_LogToFile);
settings().setValue("logFilename", m_LogFilename);
2009-10-26 07:57:53 +00:00
}
2010-06-12 17:20:54 +00:00
bool AppConfig::detectPath(const QString& name, QString& path)
{
// look in current working dir and default dir
QStringList searchDirs;
searchDirs.append("./");
searchDirs.append(synergyProgramDir());
// use the first valid path we find
for (int i = 0; i < searchDirs.length(); i++)
{
QFile f(searchDirs[i] + name);
if (f.exists())
{
path = f.fileName();
return true;
}
}
// nothing found!
return false;
}