Skip to content

Commit

Permalink
[sailfish-browser] Provide an option in site info panel to set user a…
Browse files Browse the repository at this point in the history
…gent override. Contributes to JB#31240
  • Loading branch information
paveltkv committed Mar 16, 2022
1 parent a12fa84 commit 2b31b2d
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
104 changes: 104 additions & 0 deletions apps/browser/qml/pages/SiteUserAgentPage.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/****************************************************************************
**
** Copyright (c) 2022 Open Mobile Platform LLC.
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import QtQuick 2.1
import Sailfish.Silica 1.0
import Sailfish.Browser 1.0
import "useragenthelper.js" as UserAgentHelper

Page {
id: page

property UserAgentModel userAgentModel: UserAgentModel {
currentHost: WebUtils.host(webView.url)
}
property string hostname: userAgentModel.currentHost
property bool isKey: userAgentModel.currentHostUserAgent.isKey
property string userAgent: userAgentModel.currentHostUserAgent.userAgent
property WebPage webPage: null

SilicaListView {
anchors.fill: parent
model: UserAgentHelper.model

header: PageHeader {
//% "User agent override"
title: qsTrId("sailfish_browser-he-user_agent_override")
description: page.hostname
}

delegate: BackgroundItem {
id: delegateItem

onClicked: {
if ("custom" in modelData) {
pageStack.animatorPush(customUserAgentDialog,
{
userAgentModel: userAgentModel,
userAgent: page.isKey ? "" : page.userAgent,
hostname: page.hostname
})
} else {
userAgentModel.setUserAgentOverride(page.hostname, modelData.key, true)
pageStack.pop()
}
}

Label {
width: parent.width
leftPadding: Theme.horizontalPageMargin
rightPadding: Theme.horizontalPageMargin
text: modelData.name + (!page.isKey && "custom" in modelData ? ": " + userAgent : "")
elide: Text.ElideRight
highlighted: page.isKey ? modelData.key === page.userAgent : "custom" in modelData
}
}

VerticalScrollDecorator {}
}

Component {
id: customUserAgentDialog

Dialog {
id: dialog

property UserAgentModel userAgentModel
property string userAgent
property string hostname

canAccept: textField.text !== ""
onAccepted: {
userAgentModel.setUserAgentOverride(hostname, textField.text.trim(), false)
}

DialogHeader {
id: header
//: Accept button text for adding a home page adress
//% "OK"
acceptText: qsTrId("sailfish_browser-he-ok")
}

TextField {
id: textField

anchors.top: header.bottom
focus: true
text: userAgent
//% "User agent to use when loading %1."
description: qsTrId("sailfish_browser-he-user_agent_for_site").arg(hostname)
label: placeholderText
inputMethodHints: Qt.ImhNoPredictiveText
EnterKey.onClicked: dialog.accept()
}
}
}
}

49 changes: 48 additions & 1 deletion apps/browser/qml/pages/components/CertificateInfo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Sailfish.Browser 1.0
import Qt5Mozilla 1.0
import "." as Browser
import Sailfish.WebView.Controls 1.0
import "../useragenthelper.js" as UserAgentHelper

SilicaFlickable {
id: root
Expand All @@ -33,6 +34,10 @@ SilicaFlickable {
})
}

function openSiteUseragentSettings() {
pageStack.push("../SiteUserAgentPage.qml")
}

onSecurityChanged: {
// Jump back to the top
contentY = originY
Expand All @@ -43,6 +48,11 @@ SilicaFlickable {
host: toolBarRow.url
}

UserAgentModel {
id: userAgentModel
currentHost: WebUtils.host(webView.url)
}

VerticalScrollDecorator {}

MouseArea {
Expand Down Expand Up @@ -116,14 +126,51 @@ SilicaFlickable {
onClicked: showCertDetail()
}


Loader {
height: Theme.fontSizeMedium + Theme.iconSizeMedium + Theme.paddingMedium
width: permissionIndicationModel.count === 0 ? implicitWidth : parent.width
anchors.horizontalCenter: parent.horizontalCenter
sourceComponent: permissionIndicationModel.count === 0 ? permissionButtonComponent : permissionComponent
}

MouseArea {
id: userAgentArea

width: parent.width
height: userAgentColumn.height
onClicked: openSiteUseragentSettings()

Column {
id: userAgentColumn

width: parent.width
spacing: Theme.paddingMedium
anchors.horizontalCenter: parent.horizontalCenter

Label {
width: parent.width - 2 * Theme.horizontalPageMargin
x: Theme.horizontalPageMargin
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.Wrap
color: Theme.highlightColor
//% "Current user agent"
text: qsTrId("sailfish_browser-sh-current_user_agent")
}

Text {
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width - 2 * Theme.horizontalPageMargin
horizontalAlignment: Text.AlignHCenter
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall
textFormat: Text.PlainText
wrapMode: Text.Wrap
text: UserAgentHelper.getUserAgentString(userAgentModel.currentHostUserAgent.userAgent,
userAgentModel.currentHostUserAgent.isKey)
}
}
}

Component {
id: permissionButtonComponent

Expand Down
30 changes: 30 additions & 0 deletions apps/browser/qml/pages/useragenthelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var model = [].concat(
[{
key: "",
//: Identifier of the current browser
//% "Gecko-based browser (Default)"
name: qsTrId("sailfish_browser-la-browser_list_current_browser")
}],
UserAgentManager.getBrowserList(),
[{
custom: true,
//: A user-defined user agent
//% "Custom"
name: qsTrId("sailfish_browser-la-browser_list_custom")
}]
)

function getUserAgentString(userAgent, isKey)
{
if (!isKey) {
return userAgent
}

for (var i = 0; i < model.length; i++) {
if (i in model && model[i].key === userAgent) {
return model[i].name
}
}

return model[0].name
}
5 changes: 5 additions & 0 deletions apps/core/declarativewebutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ QString DeclarativeWebUtils::displayableUrl(const QString &fullUrl) const
return !returnUrl.isEmpty() ? returnUrl : fullUrl;
}

QString DeclarativeWebUtils::host(const QString &fullUrl) const
{
return QUrl(fullUrl).host();
}

QString DeclarativeWebUtils::pageName(const QString &fullUrl) const
{
QUrl url(fullUrl);
Expand Down
1 change: 1 addition & 0 deletions apps/core/declarativewebutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class DeclarativeWebUtils : public QObject

Q_INVOKABLE int getLightness(const QColor &color) const;
Q_INVOKABLE QString displayableUrl(const QString &fullUrl) const;
Q_INVOKABLE QString host(const QString &fullUrl) const;
Q_INVOKABLE QString pageName(const QString &fullUrl) const;

public slots:
Expand Down

0 comments on commit 2b31b2d

Please sign in to comment.