Commit 13139605 authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Allow deleting a desktop file again

parent 4bd1c58f
......@@ -245,10 +245,16 @@ Kirigami.ApplicationWindow {
Kirigami.Action {
icon.name: "list-add"
text: i18n("Add to homescreen")
onTriggered: {
DesktopFileGenerator.createDesktopFile(currentWebView.title,
currentWebView.url,
currentWebView.icon)
checkable: true
checked: DesktopFileGenerator.desktopFileExists(currentWebView.title)
onCheckedChanged: {
if (checked) {
DesktopFileGenerator.createDesktopFile(currentWebView.title,
currentWebView.url,
currentWebView.icon)
} else {
DesktopFileGenerator.removeDesktopFile(currentWebView.title)
}
}
},
Kirigami.Action {
......
......@@ -41,12 +41,9 @@ DesktopFileGenerator::DesktopFileGenerator(QQmlEngine *engine, QObject *parent)
void DesktopFileGenerator::createDesktopFile(const QString &name, const QString &url, const QString &icon)
{
QString location = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
QString filename = name.toLower();
filename.replace(QStringLiteral(" "), QStringLiteral("_"));
filename.replace(QStringLiteral("'"), QStringLiteral(""));
filename.replace(QStringLiteral("\""), QStringLiteral(""));
QString path = QStringLiteral("%1/%2.desktop").arg(location, filename);
const QString location = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
const QString filename = generateFileName(name);
const QString path = QStringLiteral("%1/%2.desktop").arg(location, filename);
KConfig desktopFile(path, KConfig::SimpleConfig);
storeIcon(icon, filename);
......@@ -65,17 +62,33 @@ void DesktopFileGenerator::createDesktopFile(const QString &name, const QString
buildsycoca.startDetached();
}
bool DesktopFileGenerator::desktopFileExists(const QString &name)
{
const QString location = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
const QString filename = generateFileName(name);
auto exists = QFile::exists(QStringLiteral("%1/%2.desktop").arg(location, filename));
return exists;
}
bool DesktopFileGenerator::removeDesktopFile(const QString &name)
{
const QString location = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
const QString filename = generateFileName(name);
return QFile::remove(QStringLiteral("%1/%2.desktop").arg(location, filename));
}
void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName)
{
auto *provider = dynamic_cast<QQuickImageProvider *>(m_engine->imageProvider(QStringLiteral("favicon")));
QLatin1String prefix_favicon = QLatin1String("image://favicon/");
QString providerIconName = url.mid(prefix_favicon.size());
const QLatin1String prefix_favicon = QLatin1String("image://favicon/");
const QString providerIconName = url.mid(prefix_favicon.size());
QSize szRequested;
const QSize szRequested;
QSize szObtained;
QString iconLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
const QString iconLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
+ QStringLiteral("/icons/hicolor/16x16/apps/");
QDir().mkpath(iconLocation);
......@@ -108,6 +121,15 @@ void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName
}
}
QString DesktopFileGenerator::generateFileName(const QString &name)
{
QString filename = name.toLower();
filename.replace(QStringLiteral(" "), QStringLiteral("_"));
filename.replace(QStringLiteral("'"), QStringLiteral(""));
filename.replace(QStringLiteral("\""), QStringLiteral(""));
return filename;
}
QString DesktopFileGenerator::webappCommand()
{
if (!QStandardPaths::locate(QStandardPaths::RuntimeLocation, QStringLiteral("flatpak-info")).isEmpty()) {
......
......@@ -32,9 +32,12 @@ public:
explicit DesktopFileGenerator(QQmlEngine *engine, QObject *parent = nullptr);
Q_INVOKABLE void createDesktopFile(const QString &name, const QString &url, const QString &icon);
Q_INVOKABLE bool desktopFileExists(const QString &name);
Q_INVOKABLE bool removeDesktopFile(const QString &name);
private:
void storeIcon(const QString &url, const QString &fileName);
QString generateFileName(const QString &name);
QString webappCommand();
QQmlEngine *m_engine;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment