Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sailfish-browser] Provide options to set user agent overrides from browser interface. Contributes to JB#31240 #989

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions apps/browser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include "secureaction.h"
#include "faviconmanager.h"
#include "bookmarkmanager.h"
#include "useragentmodel.h"
#include "useragentfiltermodel.h"

#ifdef HAS_BOOSTER
#include <MDeclarativeCache>
Expand Down Expand Up @@ -153,6 +155,8 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
qmlRegisterType<DeclarativeLoginModel>(uri, 1, 0, "LoginModel");
qmlRegisterType<LoginFilterModel>(uri, 1, 0, "LoginFilterModel");
qmlRegisterSingletonType<BookmarkManager>(uri, 1, 0, "BookmarkManager", bookmarkmanager_factory);
qmlRegisterType<UserAgentModel>(uri, 1, 0, "UserAgentModel");
qmlRegisterType<UserAgentFilterModel>(uri, 1, 0, "UserAgentFilterModel");
}
qmlRegisterSingletonType<FaviconManager>(uri, 1, 0, "FaviconManager", faviconmanager_factory);
qmlRegisterUncreatableType<DownloadStatus>(uri, 1, 0, "DownloadStatus", "");
Expand Down
10 changes: 10 additions & 0 deletions apps/browser/qml/pages/PrivacySettingsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ Page {
checked: true
}

TextSwitch {
id: clearUserAgentOverrides

//% "User аgent overrides"
text: qsTrId("settings_browser-la-user_аgent_overrides")
checked: true
}

// Spacer between Button and switches
Item {
width: parent.width
Expand All @@ -140,6 +148,7 @@ Page {
|| clearCache.checked
|| clearBookmarks.checked
|| clearSitePermissions.checked
|| clearUserAgentOverrides.checked

onClicked: {
var page = pageStack.push(Qt.resolvedUrl("components/PrivacySettingsConfirmDialog.qml"), {
Expand All @@ -149,6 +158,7 @@ Page {
cacheEnabled: clearCache.checked,
bookmarksEnabled: clearBookmarks.checked,
sitePermissionsEnabled: clearSitePermissions.checked,
userAgentOverridesEnabled: clearUserAgentOverrides.checked,
historyPeriod: historyErasingComboBox.currentItem.period,
acceptDestination: previousPage
})
Expand Down
26 changes: 26 additions & 0 deletions apps/browser/qml/pages/SettingsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,32 @@ Page {
onClicked: pageStack.push("PermissionPage.qml")
}

BackgroundItem {
width: parent.width
contentHeight: Theme.itemSizeMedium
Row {
width: parent.width - 2*Theme.horizontalPageMargin
x: Theme.horizontalPageMargin
spacing: Theme.paddingMedium
anchors.verticalCenter: parent.verticalCenter

Icon {
id: userAgentsIcon
source: "image://theme/icon-m-browser-user-agents"
width: Theme.iconSizeMedium
height: Theme.iconSizeMedium
}
Label {
width: parent.width - parent.spacing - userAgentsIcon.width
//: The label for the button for accessing user agent overrides management
//% "User agent overrides"
text: qsTrId("settings_browser-la-user_agent_overrides")
anchors.verticalCenter: userAgentsIcon.verticalCenter
}
}
onClicked: pageStack.push("UserAgentPage.qml")
}

BackgroundItem {
width: parent.width
contentHeight: Theme.itemSizeMedium
Expand Down
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()
}
}
}
}

135 changes: 135 additions & 0 deletions apps/browser/qml/pages/UserAgentPage.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/****************************************************************************
**
** 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.5
import Sailfish.Silica 1.0
import Sailfish.Browser 1.0
import "useragenthelper.js" as UserAgentHelper
import "components"

Page {
UserAgentFilterModel {
id: userAgentFilterModel
sourceModel: UserAgentModel {
id: userAgentModel
}
}

SilicaListView {
id: view

anchors.fill: parent
model: userAgentFilterModel
currentIndex: -1
header: Column {
width: parent.width
PageHeader {
//% "User agent overrides"
title: qsTrId("sailfish_browser-he-user_agent_overrides")
}
SearchField {
width: parent.width
//% "Search"
placeholderText: qsTrId("sailfish_browser-ph-usera_gent_override_search")
EnterKey.onClicked: focus = false
onTextChanged: userAgentFilterModel.search = text
inputMethodHints: Qt.ImhPreferLowercase | Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
visible: userAgentModel.count > 0
}
}

delegate: ListItem {
id: listItem

width: parent.width
contentHeight: Theme.itemSizeMedium
ListView.onAdd: AddAnimation { target: listItem }
ListView.onRemove: animateRemoval()

function remove(host) {
remorseDelete(function() {
userAgentModel.unsetUserAgentOverride(host)
})
}

onClicked: openMenu()

Row {
width: parent.width - 2 * Theme.horizontalPageMargin
x: Theme.horizontalPageMargin
spacing: Theme.paddingMedium
anchors.verticalCenter: parent.verticalCenter

FavoriteIcon {
id: loginsIcon

anchors.verticalCenter: parent.verticalCenter
icon: model.favicon
sourceSize.width: Theme.iconSizeMedium
sourceSize.height: Theme.iconSizeMedium
width: Theme.iconSizeMedium
height: Theme.iconSizeMedium
}

Column {
anchors.verticalCenter: parent.verticalCenter
width: parent.width - parent.spacing - loginsIcon.width
Label {
width: parent.width
text: Theme.highlightText(model.host,
userAgentFilterModel.search,
Theme.highlightColor)
textFormat: Text.StyledText
}

Label {
width: parent.width
text: UserAgentHelper.getUserAgentString(model.userAgent, model.isKey)
font.pixelSize: Theme.fontSizeExtraSmall
elide: Text.ElideRight
color: Theme.secondaryColor
}
}
}

menu: Component {
ContextMenu {
MenuItem {
//% "Edit"
text: qsTrId("sailfish_browser-me-user_agent_override_edit")
onClicked: {
userAgentModel.currentHost = model.host
pageStack.animatorPush("SiteUserAgentPage.qml",
{
userAgentModel: userAgentModel
})
}
}
MenuItem {
//% "Delete"
text: qsTrId("sailfish_browser-me-user_agent_override_delete")
onClicked: listItem.remove(host)
}
}
}
}

ViewPlaceholder {
//% "Your saved user agent overrides show up here"
text: qsTrId("sailfish_browser-la-user_agents_none")
enabled: userAgentModel.count === 0
}

VerticalScrollDecorator {
parent: view
flickable: view
}
}
}
Loading