1 /*
2  * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
3  * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <QStandardPaths>
21 
22 namespace OCC {
23 
setupFavLink_private(const QString & folder)24 static void setupFavLink_private(const QString &folder)
25 {
26     // Nautilus: add to ~/.gtk-bookmarks
27     QFile gtkBookmarks(QDir::homePath() + QLatin1String("/.gtk-bookmarks"));
28     QByteArray folderUrl = "file://" + folder.toUtf8();
29     if (gtkBookmarks.open(QFile::ReadWrite)) {
30         QByteArray places = gtkBookmarks.readAll();
31         if (!places.contains(folderUrl)) {
32             places += folderUrl;
33             gtkBookmarks.reset();
34             gtkBookmarks.write(places + '\n');
35         }
36     }
37 }
38 
39 // returns the autostart directory the linux way
40 // and respects the XDG_CONFIG_HOME env variable
getUserAutostartDir_private()41 QString getUserAutostartDir_private()
42 {
43     QString config = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
44     config += QLatin1String("/autostart/");
45     return config;
46 }
47 
hasLaunchOnStartup_private(const QString & appName)48 bool hasLaunchOnStartup_private(const QString &appName)
49 {
50     QString desktopFileLocation = getUserAutostartDir_private() + appName + QLatin1String(".desktop");
51     return QFile::exists(desktopFileLocation);
52 }
53 
setLaunchOnStartup_private(const QString & appName,const QString & guiName,bool enable)54 void setLaunchOnStartup_private(const QString &appName, const QString &guiName, bool enable)
55 {
56     QString userAutoStartPath = getUserAutostartDir_private();
57     QString desktopFileLocation = userAutoStartPath + appName + QLatin1String(".desktop");
58     if (enable) {
59         if (!QDir().exists(userAutoStartPath) && !QDir().mkpath(userAutoStartPath)) {
60             qCWarning(lcUtility) << "Could not create autostart folder" << userAutoStartPath;
61             return;
62         }
63         QFile iniFile(desktopFileLocation);
64         if (!iniFile.open(QIODevice::WriteOnly)) {
65             qCWarning(lcUtility) << "Could not write auto start entry" << desktopFileLocation;
66             return;
67         }
68         QTextStream ts(&iniFile);
69         ts.setCodec("UTF-8");
70         ts << QLatin1String("[Desktop Entry]") << endl
71            << QLatin1String("Name=") << guiName << endl
72            << QLatin1String("GenericName=") << QLatin1String("File Synchronizer") << endl
73            << QLatin1String("Exec=") << QCoreApplication::applicationFilePath() << endl
74            << QLatin1String("Terminal=") << "false" << endl
75            << QLatin1String("Icon=") << appName.toLower() << endl // always use lowercase for icons
76            << QLatin1String("Categories=") << QLatin1String("Network") << endl
77            << QLatin1String("Type=") << QLatin1String("Application") << endl
78            << QLatin1String("StartupNotify=") << "false" << endl
79            << QLatin1String("X-GNOME-Autostart-enabled=") << "true" << endl
80            << QLatin1String("X-GNOME-Autostart-Delay=10") << endl;
81     } else {
82         if (!QFile::remove(desktopFileLocation)) {
83             qCWarning(lcUtility) << "Could not remove autostart desktop file";
84         }
85     }
86 }
87 
hasDarkSystray_private()88 static inline bool hasDarkSystray_private()
89 {
90     return true;
91 }
92 
93 } // namespace OCC
94