qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
autorun_xdg.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <QApplication>
22 #include "src/platform/autorun.h"
23 #include <QDir>
24 #include <QProcessEnvironment>
25 
26 namespace Platform {
28 {
29  QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
30  QString config = env.value("XDG_CONFIG_HOME");
31  if (config.isEmpty())
32  config = QDir::homePath() + "/" + ".config";
33  return config + "/autostart";
34 }
35 
36 QString getAutostartFilePath(QString dir)
37 {
38  return dir + "/qTox - " + Settings::getInstance().getCurrentProfile() + ".desktop";
39 }
40 
41 QString currentBinPath()
42 {
43  const auto env = QProcessEnvironment::systemEnvironment();
44  const auto appImageEnvKey = QStringLiteral("APPIMAGE");
45 
46  if (env.contains(appImageEnvKey)) {
47  return env.value(appImageEnvKey);
48  } else {
49  return QApplication::applicationFilePath();
50  }
51 }
52 
53 inline QString profileRunCommand()
54 {
55  return "\"" + currentBinPath() + "\" -p \""
57 }
58 }
59 
60 bool Platform::setAutorun(bool on)
61 {
62  QString dirPath = getAutostartDirPath();
63  QFile desktop(getAutostartFilePath(dirPath));
64  if (on) {
65  if (!QDir().mkpath(dirPath) || !desktop.open(QFile::WriteOnly | QFile::Truncate))
66  return false;
67  desktop.write("[Desktop Entry]\n");
68  desktop.write("Type=Application\n");
69  desktop.write("Name=qTox\n");
70  desktop.write("Exec=");
71  desktop.write(profileRunCommand().toUtf8());
72  desktop.write("\n");
73  desktop.close();
74  return true;
75  } else
76  return desktop.remove();
77 }
78 
79 bool Platform::getAutorun()
80 {
81  return QFile(getAutostartFilePath(getAutostartDirPath())).exists();
82 }
settings.h
Platform::currentBinPath
QString currentBinPath()
Definition: autorun_xdg.cpp:41
Platform::getAutostartDirPath
QString getAutostartDirPath()
Definition: autorun_xdg.cpp:27
Platform::profileRunCommand
QString profileRunCommand()
Definition: autorun_xdg.cpp:53
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
Platform
Definition: autorun_win.cpp:45
autorun.h
Settings::getCurrentProfile
QString getCurrentProfile() const
Definition: settings.cpp:1210
Platform::getAutostartFilePath
QString getAutostartFilePath(QString dir)
Definition: autorun_xdg.cpp:36