Commit 4f573250 authored by Rinigus Saar's avatar Rinigus Saar Committed by Jonah Brüchert

Restore pages in the tabs on start

Private tabs are now in a separate tab list. Current tab list is
accessible via tabs property allowing to view private and regular
tabs separately. Regular tabs are restored, private are not. On
new tab creation, regular open the homepage while private ones are
set to the blank page.
parent 10613736
......@@ -22,18 +22,21 @@
#include <QDebug>
#include <QUrl>
#include <QSettings>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
using namespace AngelFish;
BrowserManager::BrowserManager(QObject *parent) : QObject(parent), m_settings(new QSettings)
BrowserManager::BrowserManager(QObject *parent) : QObject(parent), m_settings(new QSettings(this))
{
loadTabs();
}
BrowserManager::~BrowserManager()
{
history()->save();
bookmarks()->save();
delete m_settings;
}
void BrowserManager::reload()
......@@ -114,3 +117,79 @@ QString BrowserManager::searchBaseUrl()
return m_settings->value("browser/searchBaseUrl", "https://start.duckduckgo.com/?q=")
.toString();
}
int BrowserManager::currentTab() const
{
return m_current_tab;
}
QString BrowserManager::tabs() const
{
QJsonArray arr;
for (auto i=m_tabs.constBegin(); i != m_tabs.constEnd(); ++i) {
QJsonObject o;
o["url"] = i->url;
o["isMobile"] = i->isMobile;
arr.append(o);
}
return QJsonDocument(arr).toJson();
}
void BrowserManager::loadTabs()
{
QJsonArray arr = QJsonDocument::fromJson(m_settings->value("browser/tabs").toByteArray()).array();
m_tabs.clear();
for (auto i = arr.constBegin(); i != arr.constEnd(); ++i) {
QJsonObject o = i->toObject();
TabState ts(o.value("url").toString(), o.value("isMobile").toBool());
m_tabs.push_back(ts);
}
m_current_tab = m_settings->value("browser/current_tab", 0).toInt();
}
void BrowserManager::saveTabs()
{
m_settings->setValue("browser/tabs", tabs());
}
void BrowserManager::setCurrentTab(int index)
{
if (m_tabs_readonly) return;
m_current_tab = index;
m_settings->setValue("browser/current_tab", m_current_tab);
}
void BrowserManager::setTab(int index, QString url, bool isMobile)
{
if (m_tabs_readonly) return;
while (m_tabs.length() <= index) {
m_tabs.append(TabState());
}
m_tabs[index] = TabState(url, isMobile);
saveTabs();
}
void BrowserManager::setTabIsMobile(int index, bool isMobile)
{
TabState ts = m_tabs.value(index);
setTab(index, ts.url, isMobile);
}
void BrowserManager::setTabUrl(int index, QString url)
{
TabState ts = m_tabs.value(index);
setTab(index, url, ts.isMobile);
}
void BrowserManager::setTabsWritable()
{
m_tabs_readonly = false;
}
void BrowserManager::rmTab(int index)
{
if (index >= 0 && index < m_tabs.size()) {
m_tabs.removeAt(index);
saveTabs();
}
}
......@@ -57,6 +57,15 @@ public:
QString homepage();
QString searchBaseUrl();
Q_INVOKABLE int currentTab() const;
Q_INVOKABLE QString tabs() const;
Q_INVOKABLE void setCurrentTab(int index);
Q_INVOKABLE void setTab(int index, QString url, bool isMobile);
Q_INVOKABLE void setTabIsMobile(int index, bool isMobile);
Q_INVOKABLE void setTabUrl(int index, QString url);
Q_INVOKABLE void setTabsWritable();
Q_INVOKABLE void rmTab(int index);
Q_INVOKABLE static QString urlFromUserInput(const QString &input);
signals:
......@@ -81,10 +90,24 @@ public slots:
void setHomepage(const QString &homepage);
void setSearchBaseUrl(const QString &searchBaseUrl);
protected:
void loadTabs();
void saveTabs();
private:
UrlModel *m_bookmarks = nullptr;
UrlModel *m_history = nullptr;
QSettings *m_settings;
struct TabState {
TabState(QString u=QString(), bool im=false): url(u), isMobile(im) {}
QString url;
bool isMobile = false;
};
int m_current_tab = 0;
QList<TabState> m_tabs;
bool m_tabs_readonly = true;
};
} // namespace
......
......@@ -31,6 +31,9 @@ Repeater {
id: tabs
clip: true
property bool activeTabs: true
property bool privateTabsMode: false
property int currentIndex: -1
property var currentItem
......@@ -46,15 +49,17 @@ Repeater {
bottom: tabs.bottom
top: tabs.top
}
privateMode: tabs.privateTabsMode
url: pageurl;
width: tabs.width
property bool showView: index === tabs.currentIndex
visible: showView
visible: showView && tabs.activeTabs
x: 0
onShowViewChanged: if (showView) tabs.currentItem = wv
onUrlChanged: if (!privateTabsMode) browserManager.setTabUrl(index, url)
}
function createEmptyTab(front) {
......@@ -90,11 +95,15 @@ Repeater {
tabs.currentItem = tabs.itemAt(0);
}
if (!privateTabsMode) browserManager.rmTab(index);
tabsModel.remove(index);
}
Component.onCompleted: {
createEmptyTab();
if (privateTabsMode)
createEmptyTab();
else
newTab(browserManager.homepage);
if (initialUrl) {
load(initialUrl)
} else {
......@@ -102,4 +111,6 @@ Repeater {
load(browserManager.homepage)
}
}
onCurrentIndexChanged: browserManager.setCurrentTab(tabs.currentIndex)
}
......@@ -34,7 +34,7 @@ import org.kde.kirigami 2.7 as Kirigami
Kirigami.ScrollablePage {
id: tabsRoot
title: i18n("Tabs")
title: rootPage.privateMode ? i18n("Private Tabs") : i18n("Tabs")
leftPadding: 0
rightPadding: 0
......@@ -46,7 +46,7 @@ Kirigami.ScrollablePage {
icon.name: "list-add"
text: i18n("New")
onTriggered: {
tabs.newTab(browserManager.homepage)
tabs.newTab(rootPage.privateMode ? "about:blank" : browserManager.homepage)
tabs.currentIndex = tabs.count - 1;
pageStack.pop()
}
......@@ -164,7 +164,7 @@ Kirigami.ScrollablePage {
border.color: Kirigami.Theme.textColor
border.width: webBrowser.borderWidth
color: "transparent"
opacity: tabs.currentIndex === index ? 0.5 : 0.3
opacity: tabs.currentIndex === index ? 0.75 : 0.3
}
Rectangle {
......@@ -224,5 +224,8 @@ Kirigami.ScrollablePage {
}
}
}
}
Component.onCompleted: grid.currentIndex = tabs.currentIndex
}
......@@ -35,9 +35,13 @@ WebEngineView {
property string errorCode: ""
property string errorString: ""
property bool privateMode: false
property alias userAgent: userAgent
property alias thumb: thumb
property bool reloadOnVisible: false
UserAgentGenerator {
id: userAgent
isMobile: Kirigami.Settings.isMobile
......@@ -59,7 +63,7 @@ WebEngineView {
}
profile {
offTheRecord: rootPage.privateMode
offTheRecord: privateMode
httpUserAgent: userAgent.userAgent
......@@ -155,7 +159,7 @@ WebEngineView {
thumb.source = "";
}
if (loadRequest.status === WebEngineView.LoadSucceededStatus) {
if (!rootPage.privateMode) {
if (!privateMode) {
addHistoryEntry();
}
grabThumb();
......@@ -233,6 +237,10 @@ WebEngineView {
if (visible) {
profile.httpUserAgent = Qt.binding(function() { return userAgent.userAgent; });
snaphotTimer.start();
if (reloadOnVisible) {
reloadOnVisible = false;
reload();
}
}
}
......
......@@ -39,6 +39,12 @@ Kirigami.ApplicationWindow {
*/
property Item currentWebView: tabs.currentItem
// Pointer to the currently active list of tabs.
//
// As there are private and normal tabs, switch between
// them according to the current mode.
property Item tabs: rootPage.privateMode ? privateTabs : regularTabs
onCurrentWebViewChanged: {
print("Current WebView is now : " + tabs.currentIndex);
}
......@@ -158,13 +164,26 @@ Kirigami.ApplicationWindow {
property bool privateMode: false
ListWebView {
id: tabs
id: regularTabs
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: navigation.top
}
activeTabs: !rootPage.privateMode
}
ListWebView {
id: privateTabs
anchors {
top: parent.top
left: parent.left
right: parent.right
bottom: navigation.top
}
activeTabs: rootPage.privateMode
privateTabsMode: true
}
ErrorHandler {
......@@ -297,7 +316,9 @@ Kirigami.ApplicationWindow {
} else {
currentWebView.userAgent.isMobile = true
}
if (!rootPage.privateMode) {
browserManager.setTabIsMobile(tabs.currentIndex, currentWebView.userAgent.isMobile);
}
currentWebView.reload()
}
}
......@@ -377,4 +398,30 @@ Kirigami.ApplicationWindow {
webBrowser.pageStack.pop();
}
}
Component.onCompleted: {
if (!webappcontainer) {
// initialize tabs
var t = JSON.parse(browserManager.tabs());
var ct = browserManager.currentTab();
for (var i = 0; i < t.length; i++) {
if (i < regularTabs.count)
regularTabs.itemAt(i).url = t[i].url;
else
regularTabs.newTab(t[i].url);
regularTabs.itemAt(i).userAgent.isMobile = t[i].isMobile;
regularTabs.itemAt(i).reloadOnVisible = true;
}
if (ct >= 0 && ct < regularTabs.count) regularTabs.currentIndex = ct;
browserManager.setTabsWritable();
if (initialUrl) {
regularTabs.newTab(initialUrl);
}
} else {
if (initialUrl) {
load(initialUrl);
}
}
}
}
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