Verified Commit bdcaa01f authored by Jonah Brüchert's avatar Jonah Brüchert
Browse files

Rewrite user agent switcher in c++

parent 37cd7b69
......@@ -3,6 +3,7 @@ set(angelfish_SRCS
browsermanager.cpp
urlmodel.cpp
urlfilterproxymodel.cpp
useragent.cpp
)
qt5_add_resources(RESOURCES resources.qrc)
......
......@@ -23,10 +23,11 @@ import QtQuick 2.3
import QtQuick.Controls 2.4 as Controls
import QtQuick.Window 2.1
import QtQuick.Layouts 1.3
import QtWebEngine 1.7
import org.kde.kirigami 2.4 as Kirigami
import org.kde.mobile.angelfish 1.0
WebEngineView {
id: webEngineView
......@@ -34,42 +35,21 @@ WebEngineView {
property string errorCode: ""
property string errorString: ""
/**
* User agent used on mobile devices
*/
readonly property string mobileUserAgent: "Mozilla/5.0 (Linux; Plasma Mobile, like Android 9.0 ) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3770.116 Mobile Safari/537.36"
/**
* User agent used on desktop devices,
* Defaults to QtWebEngine's default (it is only supported on desktop devices by Qt currently)
*/
readonly property string desktopUserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.14.0 Chrome/75.0.3770.116 Safari/537.36"
property alias userAgent: userAgent
width: pageWidth
height: pageHeight
state: Kirigami.Settings.isMobile ? "mobile" : "desktop"
states: [
State {
name: "mobile"
PropertyChanges {
target: webEngineView
profile.httpUserAgent: mobileUserAgent
}
},
State {
name: "desktop"
PropertyChanges {
target: webEngineView
profile.httpUserAgent: desktopUserAgent
}
}
]
UserAgentGenerator {
id: userAgent
isMobile: Kirigami.Settings.isMobile
}
profile {
offTheRecord: rootPage.privateMode
httpUserAgent: userAgent.userAgent
onDownloadRequested: {
// if we don't accept the request right away, it will be deleted
download.accept()
......
......@@ -278,18 +278,12 @@ Kirigami.ApplicationWindow {
icon.name: "computer"
text: i18n("Show desktop site")
checkable: true
checked: {
if (currentWebView.state === "mobile") {
false
} else if (currentWebView.state === "desktop") {
true
}
}
checked: !currentWebView.userAgent.isMobile
onTriggered: {
if (currentWebView.state === "desktop") {
currentWebView.state = "mobile"
} else if (currentWebView.state === "mobile") {
currentWebView.state = "desktop"
if (currentWebView.userAgent.isMobile) {
currentWebView.userAgent.isMobile = false
} else {
currentWebView.userAgent.isMobile = true
}
currentWebView.reload()
......
......@@ -28,6 +28,7 @@
#include "browsermanager.h"
#include "urlfilterproxymodel.h"
#include "urlmodel.h"
#include "useragent.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
......@@ -62,9 +63,11 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
// Browser manager
qmlRegisterType<AngelFish::BrowserManager>("org.kde.mobile.angelfish", 1, 0,
"BrowserManager");
"BrowserManager");
qmlRegisterType<UrlFilterProxyModel>("org.kde.mobile.angelfish", 1, 0,
"UrlFilterProxyModel");
"UrlFilterProxyModel");
qmlRegisterType<UserAgent>("org.kde.mobile.angelfish", 1, 0,
"UserAgentGenerator");
engine.load(QUrl(QStringLiteral("qrc:///webbrowser.qml")));
// Error handling
......
#include "useragent.h"
#include <QtWebEngine/qtwebengineversion.h>
#include <QtWebEngine/QQuickWebEngineProfile>
UserAgent::UserAgent(QObject *parent) : QObject(parent)
{
}
QString UserAgent::userAgent() const
{
return QString("Mozilla/5.0 (%1) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/%2 Chrome/75.0.3770.116 %3 Safari/537.36")
.arg(m_isMobile ? QStringLiteral("Linux; Plasma Mobile, like Android 9.0") : QStringLiteral("X11; Linux x86_64"))
.arg(QTWEBENGINE_VERSION_STR)
.arg(m_isMobile ? QStringLiteral("Mobile") : QStringLiteral("Desktop"));
}
bool UserAgent::isMobile() const
{
return m_isMobile;
}
void UserAgent::setIsMobile(bool value)
{
if (m_isMobile != value) {
m_isMobile = value;
emit isMobileChanged();
emit userAgentChanged();
}
}
#ifndef USERAGENT_H
#define USERAGENT_H
#include <QObject>
class UserAgent : public QObject
{
Q_PROPERTY(QString userAgent READ userAgent NOTIFY userAgentChanged)
Q_PROPERTY(bool isMobile READ isMobile WRITE setIsMobile NOTIFY isMobileChanged)
Q_OBJECT
public:
explicit UserAgent(QObject *parent = nullptr);
QString userAgent() const;
bool isMobile() const;
void setIsMobile(bool value);
signals:
void isMobileChanged();
void userAgentChanged();
private:
int m_isMobile;
};
#endif // USERAGENT_H
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