-
Notifications
You must be signed in to change notification settings - Fork 947
Expand file tree
/
Copy pathutility_unix.cpp
More file actions
176 lines (153 loc) · 5.49 KB
/
utility_unix.cpp
File metadata and controls
176 lines (153 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2014 ownCloud GmbH
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "utility.h"
#include "config.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QStandardPaths>
#include <QtGlobal>
#include <QProcess>
#include <QString>
#include <QTextStream>
namespace OCC {
QString Utility::appImagePath()
{
return qEnvironmentVariable("APPIMAGE");
}
bool Utility::isRunningInAppImage()
{
const auto currentAppImagePath = appImagePath();
return !currentAppImagePath.isEmpty() && QFile::exists(currentAppImagePath);
}
QVector<Utility::ProcessInfosForOpenFile> Utility::queryProcessInfosKeepingFileOpen(const QString &filePath)
{
Q_UNUSED(filePath)
return {};
}
void Utility::setupFavLink(const QString &folder)
{
// Nautilus: add to ~/.config/gtk-3.0/bookmarks
QFile gtkBookmarks(QDir::homePath() + QLatin1String("/.config/gtk-3.0/bookmarks"));
const auto folderUrl = QUrl::fromLocalFile(folder).toEncoded();
if (!gtkBookmarks.open(QFile::ReadWrite)) {
qCWarning(lcUtility).nospace() << "failed to set up fav link"
<< " folder=" << folder
<< " error=" << gtkBookmarks.error()
<< " errorString=" << gtkBookmarks.errorString();
return;
}
auto places = gtkBookmarks.readAll();
if (places.contains(folderUrl)) {
qCDebug(lcUtility).nospace() << "fav link already exists"
<< " folder=" << folder
<< " folderUrl=" << folderUrl;
return;
}
places += folderUrl;
gtkBookmarks.reset();
gtkBookmarks.write(places + '\n');
}
void Utility::removeFavLink(const QString &folder)
{
Q_UNUSED(folder)
}
// returns the autostart directory the linux way
// and respects the XDG_CONFIG_HOME env variable
static QString getUserAutostartDir()
{
QString config = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
config += QLatin1String("/autostart/");
return config;
}
bool Utility::hasSystemLaunchOnStartup(const QString &appName)
{
Q_UNUSED(appName)
return false;
}
bool Utility::hasLaunchOnStartup(const QString &appName)
{
const QString desktopFileLocation = getUserAutostartDir() + appName + QLatin1String(".desktop");
return QFile::exists(desktopFileLocation);
}
void Utility::migrateFavLink(const QString &folder)
{
Q_UNUSED(folder)
}
void Utility::setupDesktopIni(const QString &folder, const QString localizedResourceName)
{
Q_UNUSED(folder)
Q_UNUSED(localizedResourceName)
}
QString Utility::syncFolderDisplayName(const QString &folder, const QString &displayName)
{
Q_UNUSED(folder)
Q_UNUSED(displayName)
return {};
}
void Utility::setLaunchOnStartup(const QString &appName, const QString &guiName, bool enable)
{
const auto userAutoStartPath = getUserAutostartDir();
const QString desktopFileLocation = userAutoStartPath + appName + QLatin1String(".desktop");
if (enable) {
if (!QDir().exists(userAutoStartPath) && !QDir().mkpath(userAutoStartPath)) {
qCWarning(lcUtility) << "Could not create autostart folder" << userAutoStartPath;
return;
}
QFile iniFile(desktopFileLocation);
if (!iniFile.open(QIODevice::WriteOnly)) {
qCWarning(lcUtility) << "Could not write auto start entry" << desktopFileLocation;
return;
}
// When running inside an AppImage, we need to set the path to the
// AppImage instead of the path to the executable
const auto currentAppImagePath = appImagePath();
const auto runningInsideAppImage = isRunningInAppImage();
const auto executablePath = runningInsideAppImage ? currentAppImagePath : QCoreApplication::applicationFilePath();
QTextStream ts(&iniFile);
ts << QLatin1String("[Desktop Entry]\n")
<< QLatin1String("Name=") << guiName << QLatin1Char('\n')
<< QLatin1String("GenericName=") << QLatin1String("File Synchronizer\n")
<< QLatin1String("Exec=\"") << executablePath << "\" --background\n"
<< QLatin1String("Terminal=") << "false\n"
<< QLatin1String("Icon=") << APPLICATION_ICON_NAME << QLatin1Char('\n')
<< QLatin1String("Categories=") << QLatin1String("Network\n")
<< QLatin1String("Type=") << QLatin1String("Application\n")
<< QLatin1String("StartupNotify=") << "false\n"
<< QLatin1String("X-GNOME-Autostart-enabled=") << "true\n"
<< QLatin1String("X-GNOME-Autostart-Delay=10") << Qt::endl;
} else {
if (!QFile::remove(desktopFileLocation)) {
qCWarning(lcUtility) << "Could not remove autostart desktop file";
}
}
}
bool Utility::hasDarkSystray()
{
return true;
}
QString Utility::getCurrentUserName()
{
return {};
}
void Utility::registerUriHandlerForLocalEditing()
{
if (!isRunningInAppImage()) {
// only register x-scheme-handler if running inside appImage
return;
}
// mirall.desktop.in must have an x-scheme-handler mime type specified
const QString desktopFileName = QLatin1String(LINUX_APPLICATION_ID) + QLatin1String(".desktop");
QProcess process;
const QStringList args = {
QLatin1String("default"),
desktopFileName,
QStringLiteral("x-scheme-handler/%1").arg(QStringLiteral(APPLICATION_URI_HANDLER_SCHEME))
};
process.start(QStringLiteral("xdg-mime"), args, QIODevice::ReadOnly);
process.waitForFinished();
}
} // namespace OCC