#include <QApplication>
#if defined(Q_OS_UNIX) && !defined(__APPLE__) && !defined(__MACH__)
#include "autorun.h"
#include <QDir>
#include <QProcessEnvironment>
namespace Platform {
QString getAutostartDirPath()
{
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString config = env.value("XDG_CONFIG_HOME");
if (config.isEmpty())
config = QDir::homePath() + "/" + ".config";
return config + "/autostart";
}
QString getAutostartFilePath(QString dir)
{
return dir + "/Qt - " + QApplication::applicationName() + ".desktop";
}
inline QString currentCommandLine()
{
return "\"" + QApplication::applicationFilePath() + "\" -p \""
+ QApplication::applicationName() + "\"";
}
}
bool Platform::setAutorun(bool on)
{
QString dirPath = getAutostartDirPath();
QFile desktop(getAutostartFilePath(dirPath));
if (on) {
if (!QDir().mkpath(dirPath) || !desktop.open(QFile::WriteOnly | QFile::Truncate))
return false;
desktop.write("[Desktop Entry]\n");
desktop.write("Type=Application\n");
desktop.write("Name=Qt\n");
desktop.write("Exec=");
desktop.write(currentCommandLine().toUtf8());
desktop.write("\n");
desktop.close();
return true;
} else
return desktop.remove();
}
bool Platform::getAutorun()
{
return QFile(getAutostartFilePath(getAutostartDirPath())).exists();
}
#endif // defined(Q_OS_UNIX) && !defined(__APPLE__) && !defined(__MACH__)