Verified Commit ebacbd7b authored by Jonah Brüchert's avatar Jonah Brüchert 🌳

Add generator for webapp desktop file

parent d3ebc525
......@@ -33,7 +33,7 @@ include(KDECompilerSettings NO_POLICY_SCOPE)
################# Find dependencies #################
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS Core Quick Test Gui Svg QuickControls2 Sql)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 Purpose I18n)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 Purpose I18n Config)
# Necessary to support QtWebEngine installed in a different prefix than the rest of Qt (e.g flatpak)
find_package(Qt5WebEngine REQUIRED)
......
find_package(KF5 REQUIRED COMPONENTS Config CoreAddons)
find_package(KF5 REQUIRED COMPONENTS CoreAddons)
set(angelfish_webapp_SRCS
main.cpp
......
......@@ -70,7 +70,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
if (parser.positionalArguments().isEmpty()) {
return 1;
}
const QString fileName = parser.positionalArguments().first();
KDesktopFile desktopFile(fileName);
if (desktopFile.readUrl().isEmpty()) {
......@@ -86,7 +86,7 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
i18n("Copyright 2020 Angelfish developers"));
QApplication::setWindowIcon(QIcon::fromTheme(desktopFile.readIcon()));
aboutData.addAuthor(i18n("Marco Martin"), QString(), "mart@kde.org");
KAboutData::setApplicationData(aboutData);
// Exported types
......
......@@ -8,11 +8,21 @@ set(angelfish_SRCS
urlutils.cpp
useragent.cpp
tabsmodel.cpp
desktopfilegenerator.cpp
)
qt5_add_resources(RESOURCES resources.qrc)
add_executable(angelfish ${angelfish_SRCS} ${RESOURCES})
target_link_libraries(angelfish Qt5::Core Qt5::Qml Qt5::Quick Qt5::Sql Qt5::Svg Qt5::WebEngine KF5::I18n)
target_link_libraries(angelfish
Qt5::Core
Qt5::Qml
Qt5::Quick
Qt5::Sql
Qt5::Svg
Qt5::WebEngine
KF5::I18n
KF5::ConfigCore
)
install(TARGETS angelfish ${KF5_INSTALL_TARGETS_DEFAULT_ARGS})
......@@ -240,6 +240,15 @@ Kirigami.ApplicationWindow {
sheetLoader.item.open()
}
},
Kirigami.Action {
icon.name: "list-add"
text: i18n("Add to homescreen")
onTriggered: {
DesktopFileGenerator.createDesktopFile(currentWebView.title,
currentWebView.url,
currentWebView.icon)
}
},
Kirigami.Action {
enabled: currentWebView.canGoBack
icon.name: "go-previous"
......
/***************************************************************************
* *
* Copyright 2020 Jonah Brüchert <jbb@kaidan.im> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
* *
***************************************************************************/
#include "desktopfilegenerator.h"
#include <QStandardPaths>
#include <QQmlEngine>
#include <QQuickImageProvider>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <KConfigCore/KDesktopFile>
#include <KConfigCore/KConfigGroup>
DesktopFileGenerator::DesktopFileGenerator(QQmlEngine *engine, QObject *parent)
: QObject(parent)
, m_engine(engine)
{
}
void DesktopFileGenerator::createDesktopFile(const QString &name, const QString &url, const QString &icon)
{
QString location = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
QString filename = name.toLower();
filename.replace(" ", "_");
QString path = QStringLiteral("%1/%2.desktop").arg(location, filename);
KConfig desktopFile(path, KConfig::SimpleConfig);
storeIcon(icon, filename);
auto desktopEntry = desktopFile.group("Desktop Entry");
desktopEntry.writeEntry(QStringLiteral("URL"), url);
desktopEntry.writeEntry(QStringLiteral("Name"), name);
desktopEntry.writeEntry(QStringLiteral("Exec"), QStringLiteral("%1 %2.desktop").arg(webappCommand(), filename));
desktopEntry.writeEntry(QStringLiteral("Icon"), filename);
desktopFile.sync();
}
void DesktopFileGenerator::storeIcon(const QString &url, const QString &fileName)
{
auto *provider = static_cast<QQuickImageProvider *>(m_engine->imageProvider("favicon"));
QLatin1String prefix_favicon = QLatin1String("image://favicon/");
QString providerIconName = url.mid(prefix_favicon.size());
QSize szRequested;
QSize szObtained;
QString iconLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
+ "/icons/hicolor/16x16/apps/";
QDir().mkpath(iconLocation);
auto imageFile = new QFile(iconLocation + fileName + ".png");
if (!imageFile->open(QIODevice::WriteOnly)) {
qDebug() << Q_FUNC_INFO << "Failed to open image file";
}
switch (provider->imageType()) {
case QQmlImageProviderBase::Image: {
QImage image = provider->requestImage(providerIconName, &szObtained, szRequested);
if (!image.save(imageFile, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save image" << url;
return;
}
break;
}
case QQmlImageProviderBase::Pixmap: {
QPixmap image = provider->requestPixmap(providerIconName, &szObtained, szRequested);
if (!image.save(imageFile, "PNG")) {
qWarning() << Q_FUNC_INFO << "Failed to save pixmap" << url;
return;
}
break;
}
default:
qDebug() << "Failed to save unhandled image type";
}
}
QString DesktopFileGenerator::webappCommand()
{
if (!QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty()) {
return QStringLiteral("flatpak run "
"--command=angelfish-webapp "
"--filesystem=%1 "
"org.kde.mobile.angelfish")
.arg(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation));
}
return QStringLiteral("angelfish-webapp");
}
/***************************************************************************
* *
* Copyright 2020 Jonah Brüchert <jbb@kaidan.im> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
* *
***************************************************************************/
#ifndef DESKTOPFILEGENERATOR_H
#define DESKTOPFILEGENERATOR_H
#include <QObject>
class QQmlEngine;
class DesktopFileGenerator : public QObject
{
Q_OBJECT
public:
explicit DesktopFileGenerator(QQmlEngine *engine, QObject *parent = nullptr);
Q_INVOKABLE void createDesktopFile(const QString &name, const QString &url, const QString &icon);
private:
void storeIcon(const QString &url, const QString &fileName);
QString webappCommand();
QQmlEngine *m_engine;
};
#endif // DESKTOPFILEGENERATOR_H
......@@ -32,6 +32,7 @@
#include "tabsmodel.h"
#include "urlutils.h"
#include "useragent.h"
#include "desktopfilegenerator.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
......@@ -87,6 +88,10 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
return static_cast<QObject *>(BrowserManager::instance());
});
qmlRegisterSingletonType<DesktopFileGenerator>("org.kde.mobile.angelfish", 1, 0, "DesktopFileGenerator", [](QQmlEngine *engine, QJSEngine *) -> QObject * {
return static_cast<QObject *>(new DesktopFileGenerator(engine));
});
// Load QML
engine.load(QUrl(QStringLiteral("qrc:///webbrowser.qml")));
......
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