Skip to content

Commit b8cb723

Browse files
committed
Linux: handle issues with QStandardPaths::writableLocation
According to the documentation the return path may: - be an empty string - not exist Also improve logging.
1 parent 7ff44bf commit b8cb723

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

linux.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,22 @@ bool installShortcuts()
138138
{
139139
// Set up menu and protocol handler
140140
Settings settings;
141-
QString desktopDir = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
142-
QFile::remove(desktopDir + "/unvanquished.desktop"); // updater v0.0.5 and before
143-
QFile::remove(desktopDir + "/net.unvanquished.UnvanquishedProtocolHandler.desktop"); // up to v0.2.0
141+
QString desktopDirString = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
142+
if (desktopDirString.isEmpty()) {
143+
qDebug() << "ApplicationsLocation for desktop files not provided";
144+
return false;
145+
}
146+
QDir desktopDir(desktopDirString);
147+
if (desktopDir.exists()) {
148+
desktopDir.remove("unvanquished.desktop"); // updater v0.0.5 and before
149+
desktopDir.remove("net.unvanquished.UnvanquishedProtocolHandler.desktop"); // up to v0.2.0
150+
} else {
151+
if (!desktopDir.mkpath(".")) {
152+
qDebug() << "Could not create directory for desktop files" << desktopDirString;
153+
return false;
154+
}
155+
qDebug() << "Created directory for desktop files" << desktopDirString;
156+
}
144157

145158
QString desktopFileName = "net.unvanquished.Unvanquished.desktop";
146159
QFile desktopFile(":resources/" + desktopFileName);
@@ -150,22 +163,24 @@ bool installShortcuts()
150163
}
151164
QString desktopStr = QString(desktopFile.readAll().data()).arg(settings.installPath());
152165
{
153-
QFile outputFile(desktopDir + "/" + desktopFileName);
166+
QFile outputFile(desktopDir.filePath(desktopFileName));
154167
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
155-
qDebug() << "error opening" << desktopFileName;
168+
qDebug() << "error opening" << outputFile.fileName();
156169
return false;
157170
}
158171
if (outputFile.write(desktopStr.toUtf8().constData(), desktopStr.size()) != desktopStr.size()) {
159-
qDebug() << "error writing" << desktopFileName;
172+
qDebug() << "error writing" << outputFile.fileName();
160173
return false;
161174
}
175+
qDebug() << "Created desktop file" << outputFile.fileName();
162176
}
177+
163178
int ret = QProcess::execute("xdg-mime",
164179
{QString("default"),
165-
desktopDir + "/" + desktopFileName,
180+
desktopDir.filePath(desktopFileName),
166181
QString("x-scheme-handler/unv")});
167182
qDebug() << "xdg-mime returned" << ret;
168-
ret = QProcess::execute("update-desktop-database", {desktopDir});
183+
ret = QProcess::execute("update-desktop-database", {desktopDirString});
169184
qDebug() << "update-desktop-database returned" << ret;
170185

171186
// install icon

0 commit comments

Comments
 (0)